在AUHAL上设置采样率

Jun*_*Liu 7 macos voip core-audio sample-rate audiounit

我正在使用Audio Unit Framework在mac os x上开发VOIP应用程序.在我的程序中,我设置了输入AUHAL并使用默认流格式(44.1kHz,32位/通道)从麦克风中捕获音频.在这种情况下,我的程序工作正常.

这是代码:

//The default setting in my program
CheckError(AudioUnitGetProperty(m_audCapUnit,
                                        kAudioUnitProperty_StreamFormat,
                                        kAudioUnitScope_Output,     //the value is 0
                                        inputBus,           //the value is 1
                                        &m_audCapUnitOutputStreamFormat,
                                        &propertySize),
                   "Couldn't get OutputSample ASBD from input unit") ;  

    //the inOutputSampleRate is 44100.0
        m_audCapUnitOutputStreamFormat.mSampleRate = inOutputSampleRate ; 

CheckError(AudioUnitSetProperty(m_audCapUnit,
                                        kAudioUnitProperty_StreamFormat,
                                        kAudioUnitScope_Output,
                                        inputBus,
                                        &m_audCapUnitOutputStreamFormat,
                                        propertySize),
                   "Couldn't set OutputSample ASBD on input unit"); 

//
Run Code Online (Sandbox Code Playgroud)

由于我正在开发VOIP应用程序,默认格式(44.1kHz,32位/通道)不适合我的程序,所以我想将采样率更改为8kHz.我写了这段代码来改变程序中的格式:

//......
    inOutputFormat.mSampleRate = 8000.  ;
    inOutputFormat.mFormatID = kAudioFormatLinearPCM ;
    inOutputFormat.mChannelsPerFrame = 2 ;
    inOutputFormat.mBitsPerChannel  = 16 ;
    inOutputFormat.mBytesPerFrame = 2 ;
    inOutputFormat.mBytesPerPacket = 2 ;
    inOutputFormat.mFramesPerPacket = 1 ;
    inOutputFormat.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical ;   
    inOutputFormat.mReserved = 0 ;



    CheckError(AudioUnitSetProperty(m_audCapUnit,
                                    kAudioUnitProperty_StreamFormat,
                                    kAudioUnitScope_Output,
                                    inputBus, 
                                    &inOutputFormat, 
                                    ui32PropSize),
               "Couldn't set AUHAL Unit Output Format") ;

//.......
Run Code Online (Sandbox Code Playgroud)

在这种情况下,程序正常工作,直到我的程序调用AudioUnitRender回调函数; 它不能称之为AudioUnitRender一个错误代码-10876,这意味着 kAudioUnitErr_NoConnection该文档.错误代码让我很困惑,所以我用谷歌搜索它但我找不到任何有用的信息.有人能告诉我错误实际意味着什么吗?

这不是结束,我通过这段代码再次改变了格式:

//.....
    inOutputFormat.mSampleRate = 8000.  ;
    inOutputFormat.mFormatID = kAudioFormatLinearPCM ;
    inOutputFormat.mChannelsPerFrame = 2 ;
    inOutputFormat.mBitsPerChannel  = 32 ;
    inOutputFormat.mBytesPerFrame = 4 ;
    inOutputFormat.mBytesPerPacket = 4 ;
    inOutputFormat.mFramesPerPacket = 1 ;
    inOutputFormat.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical ;   
    inOutputFormat.mReserved = 0 ;



    CheckError(AudioUnitSetProperty(m_audCapUnit,
                                    kAudioUnitProperty_StreamFormat,
                                    kAudioUnitScope_Output,
                                    inputBus, 
                                    &inOutputFormat, 
                                    ui32PropSize),
               "Couldn't set AUHAL Unit Output Format") ;
//........
Run Code Online (Sandbox Code Playgroud)

在这种情况下,程序无法AudioUnitRender再次调用并返回另一个错误代码-10863(kAudioUnitErr_CannotDoInCurrentContext).我用Google搜索,但我发现了 一些有用的东西.在那里读取信息之后,我想我在AUHAL上设置的采样率或格式可能不受硬件支持.

所以我写了一些代码来检查默认输入设备上的可用采样率:

//..........
    UInt32 propertySize = sizeof(AudioDeviceID) ;
    Boolean isWritable = false ;

    CheckError(AudioDeviceGetPropertyInfo(inDeviceID,       //the inDeviceID is the default input device
                                          0,
                                          true,
                                          kAudioDevicePropertyAvailableNominalSampleRates,
                                          &propertySize, 
                                          &isWritable), 
               "Get the Available Sample Rate Count Failed") ;

    m_valueCount = propertySize / sizeof(AudioValueRange) ;
    printf("Available %d Sample Rate\n",m_valueCount) ;

    CheckError(AudioDeviceGetProperty(inDeviceID,
                                      0,
                                      false,
                                      kAudioDevicePropertyAvailableNominalSampleRates, 
                                      &propertySize, 
                                      m_valueTabe), 
               "Get the Available Sample Rate Count Failed") ;


    for(UInt32 i = 0 ; i < m_valueCount ; ++i)
    {
        printf("Available Sample Rate value : %ld\n",(long)m_valueTabe[i].mMinimum) ;
    }
//..............
Run Code Online (Sandbox Code Playgroud)

然后我发现可用的采样率是8000,16000,32000,44100,48000,88200和96000.

8000采样率是我之前设置的,但我通过调用得到错误代码AudioUnitRender,我只是想说,为什么?

我有谷歌这么多,也阅读了很多文件,但我无法得到答案,有人能解决我遇到的问题吗?

换一种说法; 如何在仅输入AUHAL上更改采样率或格式?

Jun*_*Liu 4

昨天我终于自己解决了这个问题。

这是我的解决方案:

  1. 首先,我用来AudioDeviceGetProperty获取默认输入设备上的可用格式列表,然后我发现可用格式列表包含:(8khz, 16khz, 32khz, 44.1khz, 48khz, 88.2khz,96khz我只在此处列出了采样率字段,但列表中还有其他字段)。
  2. 然后我选择第一步中获得的可用格式之一。以我的程序为例, 我选择格式(8khz,32bits/Channel)并用于AudioDeviceSetProperty 在默认设备上设置它,而不是在AUHAL上设置它,这是在AUHAL(OutputScope,inputBus)上设置格式后我的程序正常工作的关键。
  3. 最后一步,我使用AudioUnitSetProperty设置我想要的格式,程序运行良好。

通过这个问题和解决方案,我想如果我想在仅输入的AUHAL上设置格式,格式必须匹配或者可以转换为输入设备正在使用的可用格式。所以我需要做的是首先在输入设备上设置格式,然后在仅输入的 AUHAL 上设置格式。