2012-05-29 74 views
5

我在macosx中找到使用內置麥克風/揚聲器與kAudioUnitSubType_VoiceProcessingIO子類型(非kAudioUnitSubType_HALOutput)的簡單播放應用程序的示例。關於核心音頻api的評論說,kAudioUnitSubType_VoiceProcessingIO可以在桌面上使用,並且使用iPhone 3.0或更高版本,所以我認爲macos應該有一個例子。如何在mac os中使用「kAudioUnitSubType_VoiceProcessingIO」核心音頻API的子類型?

你知道樣本在哪裏嗎?或者是否有人知道如何在macos中使用kAudioUnitSubType_VoiceProcessingIO子類型?我已經嘗試了與我在iOS中所做的相同的方式,但沒有奏效。

回答

5

我發現啓用這個IO單元的一些東西。

  1. 流格式真的很挑剔。它必須是
    • LinearPCM
    • FlagsCononical
    • 32%的通道位
    • (我做了1個頻道,但它可能更多的工作) -
    • 採樣率44100(可能與其他人的工作可能不是)
  2. 您不能在其上設置EnableIO。 IO默認情況下啓用,並且該屬性不可寫。
  3. 在初始化之前設置流格式。

與其他核心音頻工作一樣,您只需檢查每個函數調用的錯誤狀態,確定錯誤是什麼,並在每一步中做出微小更改,直到最終使其工作。

+0

謝謝,它也適用於其他採樣率(我使用16 000)。 FlagsCanonical格式表示在MAC OS X Float32中,範圍從-1.0到1.0。 – sarsonj

+0

@sarsonj:你確定你可以使用默認的44100以外的採樣率嗎?嘗試設置16000或48000時,我得到kAudioUnitErr_FormatNotSupported。 – Erfan

+0

我在Mac上使用一個通道,16000與kAudioFormatFlagsCanonical並且工作正常。 – sarsonj

2

我有兩個不同的kAudioUnitProperty_StreamFormat設置基於通道的數量。

size_t bytesPerSample = sizeof (AudioUnitSampleType); 
stereoStreamFormat.mFormatID   = kAudioFormatLinearPCM; 
stereoStreamFormat.mFormatFlags  = kAudioFormatFlagsAudioUnitCanonical; 
stereoStreamFormat.mBytesPerPacket = bytesPerSample; 
stereoStreamFormat.mFramesPerPacket = 1; 
stereoStreamFormat.mBytesPerFrame  = bytesPerSample; 
stereoStreamFormat.mChannelsPerFrame = 2; 
stereoStreamFormat.mBitsPerChannel = 8 * bytesPerSample; 
stereoStreamFormat.mSampleRate  = graphSampleRate; 

size_t bytesPerSample = sizeof (AudioUnitSampleType); 
monoStreamFormat.mFormatID   = kAudioFormatLinearPCM; 
monoStreamFormat.mFormatFlags  = kAudioFormatFlagsAudioUnitCanonical; 
monoStreamFormat.mBytesPerPacket = bytesPerSample; 
monoStreamFormat.mFramesPerPacket = 1; 
monoStreamFormat.mBytesPerFrame  = bytesPerSample; 
monoStreamFormat.mChannelsPerFrame = 1;     // 1 indicates mono 
monoStreamFormat.mBitsPerChannel = 8 * bytesPerSample; 
monoStreamFormat.mSampleRate  = graphSampleRate; 

與此音頻流格式使用I/O單元時,爲kAudioUnitSubType_VoiceProcessingIO

AudioComponentDescription iOUnitDescription; 
iOUnitDescription.componentType = kAudioUnitType_Output; 
iOUnitDescription.componentSubType = kAudioUnitSubType_VoiceProcessingIO; 
iOUnitDescription.componentManufacturer = kAudioUnitManufacturer_Apple; 
iOUnitDescription.componentFlags = 0; 
iOUnitDescription.componentFlagsMask = 0; 

我可以清楚地看到在所述音頻輸出的中斷,作爲緩衝區大小比來自此AudioUnit的緩衝區大小要小。

切換回kAudioUnitSubType_RemoteIO

iOUnitDescription.componentSubType = kAudioUnitSubType_RemoteIO;

中斷消失。

我正在處理來自麥克風的音頻輸入並對音頻緩衝區應用一些實時計算。

在方法graphSampleRate是AVSession採樣率

graphSampleRate = [AVAudioSession sharedInstance] sampleRate]; 

,也許在這裏,我錯了。

在最後的配置參數值如下:

立體聲流格式:

Sample Rate:    44100 
Format ID:     lpcm 
Format Flags:    3116 
Bytes per Packet:    4 
Frames per Packet:   1 
Bytes per Frame:    4 
Channels per Frame:   2 
Bits per Channel:   32 

單聲道流格式:

Sample Rate:    44100 
Format ID:     lpcm 
Format Flags:    3116 
Bytes per Packet:    4 
Frames per Packet:   1 
Bytes per Frame:    4 
Channels per Frame:   1 
Bits per Channel:   32 
0

由於SO post here我發現我應該已使用此標誌:

audioFormat.mFormatFlags  = kAudioFormatFlagsCanonical; 
相關問題