我正在嘗試爲遠程I/O音頻單元的輸入流設置不同的採樣率(如32kHz,24kHz等)。但是輸出始終以這些採樣率之一播放 - 無論我設置了什麼,這些採樣率都是 - 22.05kHz,33.1kHz,11.0kHz。令人奇怪的是,當我呼籲kAudioUnitScope_Output
AudioUnitGetProperty
爲kAudioUnitProperty_SampleRate
,它總是返回44.1音頻單元中可能的輸出採樣率
- (void)startToneUnit
{
AudioComponentDescription defaultOutputDescription;
defaultOutputDescription.componentType = kAudioUnitType_Output;
defaultOutputDescription.componentSubType = kAudioUnitSubType_RemoteIO;
defaultOutputDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
defaultOutputDescription.componentFlags = 0;
defaultOutputDescription.componentFlagsMask = 0;
// Get the default playback output unit
AudioComponent defaultOutput = AudioComponentFindNext(NULL, &defaultOutputDescription);
NSAssert(defaultOutput, @"Can't find default output");
// Create a new unit based on this that we'll use for output
OSErr err = AudioComponentInstanceNew(defaultOutput, &toneUnit);
NSAssert1(toneUnit, @"Error creating unit: %hd", err);
// Set our tone rendering function on the unit
AURenderCallbackStruct input;
input.inputProc = RenderTone;
input.inputProcRefCon = (__bridge void *)(self);
err = AudioUnitSetProperty(toneUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
&input,
sizeof(input));
NSAssert1(err == noErr, @"Error setting callback: %hd", err);
// Set the format to 32 bit, single channel, floating point, linear PCM
const int four_bytes_per_float = 4;
const int eight_bits_per_byte = 8;
AudioStreamBasicDescription streamFormat;
streamFormat.mSampleRate = SAMPLE_RATE;
streamFormat.mFormatID = kAudioFormatLinearPCM;
streamFormat.mFormatFlags =
kAudioFormatFlagsNativeFloatPacked;
streamFormat.mBytesPerPacket = four_bytes_per_float;
streamFormat.mFramesPerPacket = 1;
streamFormat.mBytesPerFrame = four_bytes_per_float;
streamFormat.mChannelsPerFrame = 1;
streamFormat.mBitsPerChannel = four_bytes_per_float * eight_bits_per_byte;
err = AudioUnitSetProperty (toneUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0,
&streamFormat,
sizeof(AudioStreamBasicDescription));
NSAssert1(err == noErr, @"Error setting stream format: %hd", err);
// Stop changing parameters on the unit
err = AudioUnitInitialize(toneUnit);
NSAssert1(err == noErr, @"Error initializing unit: %hd", err);
// Start playback
err = AudioOutputUnitStart(toneUnit);
NSAssert1(err == noErr, @"Error starting unit: %hd", err);
Float64 outSampleRate = 0.0;
UInt32 size = sizeof(Float64);
AudioUnitGetProperty (toneUnit,
kAudioUnitProperty_SampleRate,
kAudioUnitScope_Output,
0,
&outSampleRate,
&size);
NSLog(@"Output sample rate is now at %f Hz", outSampleRate);
}
什麼都支持音頻單元的所有可能的輸出採樣率?蘋果文檔的任何引用上,這將是很大的幫助
感謝
謝謝!但是,調用AudioUnitGetProperty獲取採樣率總是會返回44.1kHz,即使音頻實際上是以不同的頻率(如22.05kHz或33.1kHz或11kHz)播放(通過除以音頻播放持續時間呈現的總採樣來找到這些頻率)。任何錯誤的代碼我的代碼? – srinivas 2013-03-20 05:46:29
細節---你知道當你設置RemoteIO的輸入屬性時,你必須將它們定位到總線'1',對吧?當你將所有'SetProperty'calls設置爲'kAudioUnitScope_Input'而不是總線'0'時,它們不會在正確的實體上設置屬性。 – iluvcapra 2013-03-20 16:58:51