JorisH
JorisH

Reputation: 428

AUHAL with USB audio device

I've got an USB audio device (PreSonus audiobox) and want to be able to switch the audio output of my program between that USB device and the default output device (the internal speakers). The apple documentation tells me I have to use AUHAL:

If you need to connect to [..] a hardware device other than the default output device, you need to use the AUHAL.

Source

Normally, I would make a device description and then look if it exists. I wrote a function to test this:

-(OSStatus) showAUHALDevices
{
    //Show all available AUHAL devices
    // Create description
    AudioComponentDescription test_desc;
    test_desc.componentType = kAudioUnitType_Output;
    test_desc.componentSubType = kAudioUnitSubType_HALOutput;
    test_desc.componentManufacturer = kAudioUnitManufacturer_Apple;
    test_desc.componentFlags = 0;
    test_desc.componentFlagsMask = 0;

    // Determine number of available components
    UInt32 count = AudioComponentCount(&test_desc);

    // Print result
    OSStatus err = noErr;
    CFStringRef compName;
    AudioComponent test_comp;

    for (UInt32 i = 0; i < count; i++)
    {
        test_comp = AudioComponentFindNext(nil, &test_desc);
        err = AudioComponentCopyName(test_comp, &compName);
        NSLog(@"Audio component number %u/%u: %@", (i+1), count, compName);
    }

    return err;
}

The problem here is that I cannot set test_desc.componentManufacturer = kAudioUnitManufacturer_Apple; to another manufacturer than Apple, so the USB device obviously doesn't show up in the list that is printed in the log.

What am I missing? And as far as I know, this is not a driver issue.

Thanks in advance.

EDIT

I've set all the properties to zero, but the USB device still doesn't show up between the 98 different components. I can find it with [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] though, so I can set it as input, but not as output..

Upvotes: 0

Views: 446

Answers (1)

jaket
jaket

Reputation: 9341

componentManufacture can be set to zero to treat it as a wildcard (as can componentType and componentSubType).

//Show all available AUHAL devices
// Create description
AudioComponentDescription test_desc;
test_desc.componentType = kAudioUnitType_Output;
test_desc.componentSubType = kAudioUnitSubType_HALOutput;
test_desc.componentManufacturer = 0;
test_desc.componentFlags = 0;
test_desc.componentFlagsMask = 0;

Additionally, your usage of AudioComponentFindNext is wrong in how always pass nil for the first parameter. The first parameter should be the component that you wish to begin the search from. You would pass nil the first time and then the result of the first call the second time and so on.

AudioComponent test_comp = nullptr;

for (UInt32 i = 0; i < count; i++)
{
    test_comp = AudioComponentFindNext(test_comp, &test_desc);
    ...
}

Upvotes: 1

Related Questions