2013-03-19 36 views
1

我正在尝试为远程I/O音频单元的输入流设置不同的采样率(如32kHz,24kHz等)。但是输出始终以这些采样率之一播放 - 无论我设置了什么,这些采样率都是 - 22.05kHz,33.1kHz,11.0kHz。令人奇怪的是,当我呼吁kAudioUnitScope_OutputAudioUnitGetPropertykAudioUnitProperty_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); 

    } 

什么都支持音频单元的所有可能的输出采样率?苹果文档的任何引用上,这将是很大的帮助

感谢

回答

3

AudioUnit秒,在一般情况下,可以在你指定的任何采样速率运行。(或Mac OS X上的HAL)AudioUnits作为硬件的外观受到更多限制 - 变速时钟发生器,可以以任意任意采样率运行的采样率发生器,都非常昂贵,通常不适合电话:)

不同的iOS硬件,硬件模型或硬件或修订版本可能支持不同的采样率。 RemoteIO只接受您请求的费率,并将板载转换器设置为最接近您请求的费率。你总是必须做一个AudioUnitGetProperty来看看你实际得到了什么。如果您想以不同的速率录制或工作,则需要使用转换器插件。

+0

谢谢!但是,调用AudioUnitGetProperty获取采样率总是会返回44.1kHz,即使音频实际上是以不同的频率(如22.05kHz或33.1kHz或11kHz)播放(通过除以音频播放持续时间呈现的总采样来找到这些频率)。任何错误的代码我的代码? – srinivas 2013-03-20 05:46:29

+1

细节---你知道当你设置RemoteIO的输入属性时,你必须将它们定位到总线'1',对吧?当你将所有'SetProperty'calls设置为'kAudioUnitScope_Input'而不是总线'0'时,它们不会在正确的实体上设置属性。 – iluvcapra 2013-03-20 16:58:51