Mac OS X中的音频捕获

时间:2011-11-23 14:48:49

标签: macos audio core-audio audiounit

我正在开发一个音频捕获应用程序。如何使用Audio Unit API使用单个chanel以8 kHz采样率在Mac OS X上捕获音频?

这是我尝试的代码。

    Component                   component;
ComponentDescription        description;
OSStatus    err = noErr;
UInt32  param;
AURenderCallbackStruct  callback;

description.componentType = kAudioUnitType_Output;
description.componentSubType = kAudioUnitSubType_HALOutput;
description.componentManufacturer = kAudioUnitManufacturer_Apple;
description.componentFlags = 0;
description.componentFlagsMask = 0;
if(component = FindNextComponent(NULL, &description))
{
    err = OpenAComponent(component, &fAudioUnit);
    if(err != noErr)
    {
        fAudioUnit = NULL;
        return err;
    }
}
param = 1;
err = AudioUnitSetProperty(fAudioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &param, sizeof(UInt32));
if(err == noErr)
{
    param = 0;
    err = AudioUnitSetProperty(fAudioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &param, sizeof(UInt32));
}
param = sizeof(AudioDeviceID);
err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &param, &fInputDeviceID);
if(err != noErr)
{
    fprintf(stderr, "failed to get default input device\n");
    return err;
}
err = AudioUnitSetProperty(fAudioUnit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fInputDeviceID, sizeof(AudioDeviceID));
if(err != noErr)
{
    fprintf(stderr, "failed to set AU input device\n");
    return err;
}
callback.inputProc = AudioInputProc; 
callback.inputProcRefCon = NULL;
err = AudioUnitSetProperty(fAudioUnit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &callback, sizeof(AURenderCallbackStruct));
param = sizeof(AudioStreamBasicDescription);
err = AudioUnitGetProperty(fAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &fDeviceFormat, &param);
if(err != noErr)
{
    printf("failed to get input device ASBD\n");
    return err;
}
fDeviceFormat.mSampleRate = 8000.0;
fDeviceFormat.mChannelsPerFrame = 1;
fDeviceFormat.mBitsPerChannel = 16;
fDeviceFormat.mFormatID = kAudioFormatLinearPCM;
fDeviceFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
err = AudioUnitSetProperty(fAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &fDeviceFormat, sizeof(AudioStreamBasicDescription));
if(err != noErr)
{
    printf( "failed to set input device ASBD= %4.4s\n",(char *)&err);
    if(err == kAudioUnitErr_FormatNotSupported)
    {
        printf("kAudioUnitErr_FormatNotSupported\n");
    }
    return err;
}
err = AudioUnitGetProperty(fAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &fDeviceFormat, &param);
if(err != noErr)
{
    printf( "failed to get input device ASBD\n");
    return err;
}   
param = sizeof(UInt32);
err = AudioUnitGetProperty(fAudioUnit, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, 0, &fAudioSamples, &param);
if(err != noErr)
{
    fprintf(stderr, "failed to get audio sample size\n");
    return err;
}
err = AudioUnitInitialize(fAudioUnit);
if(err != noErr)
{
    fprintf(stderr, "failed to initialize AU\n");
    return err;
}

在这里,我无法将采样率和每个样本的位数从32更改为16.请任何人帮我这样做。

谢谢&问候。

3 个答案:

答案 0 :(得分:2)

使用支持的格式进行捕获并将其转换为 8000 kHz,

使用上述代码 AudioUnitSetProperty 将会失败。

您需要使用AudioConvertNew创建音频转换器,并使用AudioConverterConvertBuffer将缓冲区转换为所需格式。

答案 1 :(得分:0)

您应该查看source code for SoundFlower。这可能有助于指出你正确的方向。

答案 2 :(得分:0)

我有类似的问题,我不得不修改沙箱权限。检查您的应用程序是否装有沙盒,您可以访问麦克风。