我有兩個不同的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
謝謝,它也適用於其他採樣率(我使用16 000)。 FlagsCanonical格式表示在MAC OS X Float32中,範圍從-1.0到1.0。 – sarsonj
@sarsonj:你確定你可以使用默認的44100以外的採樣率嗎?嘗試設置16000或48000時,我得到kAudioUnitErr_FormatNotSupported。 – Erfan
我在Mac上使用一個通道,16000與kAudioFormatFlagsCanonical並且工作正常。 – sarsonj