Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you select audio input device in core audio?

I am writing a program that needs to deal with multiple audio inputs.

I am currently using AudioQueues to get the input, but this is only from the default input device.

Is there any way to either:

  • Select which input device the AudioQueues use.
  • Change the default input device.

I know that I can use kAudioHardwarePropertyDevices in Core-Audio to get a list of output devices, is there a similar one I can use for input devices?

like image 680
DanielGibbs Avatar asked Jul 07 '10 21:07

DanielGibbs


2 Answers

I banged my head against how to do this for a while, and finally figured it out:

BOOL isMic = NO;
BOOL isSpeaker = NO;

AudioDeviceID device        = audioDevices[i];

// Determine direction of the device by asking for the number of input or 
// output streams.
propertyAddress.mSelector   = kAudioDevicePropertyStreams;
propertyAddress.mScope      = kAudioDevicePropertyScopeInput;

UInt32 dataSize             = 0;
OSStatus status             = AudioObjectGetPropertyDataSize(device, 
                                                             &propertyAddress, 
                                                             0, 
                                                             NULL, 
                                                             &dataSize);        
UInt32 streamCount          = dataSize / sizeof(AudioStreamID);

if (streamCount > 0) 
{
    isMic = YES;
}

propertyAddress.mScope  = kAudioDevicePropertyScopeOutput;      
dataSize                = 0;
status                  = AudioObjectGetPropertyDataSize(device, 
                                                         &propertyAddress, 
                                                         0, 
                                                         NULL,  
                                                         &dataSize);        
streamCount             = dataSize / sizeof(AudioStreamID);

if (streamCount > 0) 
{
    isSpeaker = YES;
}

As you can see, the key part is to use the ScopeInput/ScopeOutput parameter values.

like image 86
voidref Avatar answered Oct 07 '22 04:10

voidref


kAudioHardwarePropertyDevices is used for both output and input devices. Devices can have both input and output channels, or can have only input or output channels.

Most of the AudioDevice... functions take a Boolean isInput parameter so that you ca query the input side of the device.

like image 24
lucius Avatar answered Oct 07 '22 05:10

lucius