无法在格式转换器音频单元上设置格式

Cannot set the format on a format converter audio unit

我正在尝试设置一个非常简单的 AU 图 - 只是一个附有变速输出的 RemoteIO。不幸的是,我似乎无法在变速装置上设置音频格式。我见过的其他示例似乎只是毫无问题地设置了格式。我得到的确切错误是格式不受支持。然而,RemoteIO 单元内的格式转换器会很乐意采用我的格式,这是一个非常简单的 16khz 单声道 16 位签名 int PCM 格式。

这是我目前正在尝试使用的代码:

 // Describe the output unit.
AudioComponentDescription inputDescription = {0};   
inputDescription.componentType = kAudioUnitType_Output;
inputDescription.componentSubType = kAudioUnitSubType_RemoteIO;
inputDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
AUNode ioNode;
CheckError(self, AUGraphAddNode(_graph, &inputDescription, &ioNode), "Couldn't add ioNode to AU graph");

AudioComponentDescription varispeedDescription = {0};
varispeedDescription.componentType = kAudioUnitType_FormatConverter;
varispeedDescription.componentSubType = kAudioUnitSubType_Varispeed;
varispeedDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
AUNode varispeedNode;
CheckError(self, AUGraphAddNode(_graph, &varispeedDescription, &varispeedNode), "Couldn't add varispeed node to AU graph");

CheckError(self, AUGraphOpen(_graph), "Couldn't open AU graph");
AudioUnit varispeedUnit;
CheckError(self, AUGraphNodeInfo(_graph, varispeedNode, NULL, &varispeedUnit), "Couldn't get varispeed audio unit");

CheckError(self, AUGraphNodeInfo(_graph, ioNode, NULL, &_ioUnit), "Couldn't get io unit");

// Enable input
// TODO: Conditionally disable input if option has not been specified
UInt32 one = 1;
CheckError(self, AudioUnitSetProperty(_ioUnit,
                                 kAudioOutputUnitProperty_EnableIO,
                                 kAudioUnitScope_Input,
                                 kInputBus,
                                 &one,
                                 sizeof(one)), "Couldn't enable IO on the input scope of output unit");

// TODO: check this works on iOS!
_format.mBitsPerChannel = 16;
_format.mBytesPerFrame = 2;
_format.mBytesPerPacket = 2;
_format.mChannelsPerFrame = 1;
_format.mFramesPerPacket = 1;
_format.mReserved = 0;
_format.mSampleRate = 16000.0;
_format.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
_format.mFormatID = kAudioFormatLinearPCM;

self.samplingRate = _format.mSampleRate;
UInt32 size;
size = sizeof(AudioStreamBasicDescription);
CheckError(self, AudioUnitSetProperty(_ioUnit,
                                kAudioUnitProperty_StreamFormat,
                                kAudioUnitScope_Output,
                                kInputBus,
                                &_format,
                                size),
           "Couldn't set the ASBD on the audio unit (after setting its sampling rate)");
size = sizeof(AudioStreamBasicDescription);
CheckError(self, AudioUnitSetProperty(varispeedUnit,
                                      kAudioUnitProperty_StreamFormat,
                                      kAudioUnitScope_Output,
                                      0,
                                      &_format,
                                      size),
           "Couldn't set the ASBD on the audio unit (after setting its sampling rate)");
size = sizeof(AudioStreamBasicDescription);
CheckError(self, AudioUnitSetProperty(varispeedUnit,
                                      kAudioUnitProperty_StreamFormat,
                                      kAudioUnitScope_Input,
                                      0,
                                      &_format,
                                      size),
           "Couldn't set the ASBD on the audio unit (after setting its sampling rate)");

我也试过简单地设置 RemoteIO 的输出总线的输入格式,这对转换器的输出格式来说效果很好,但它仍然不接受我的格式作为转换器的输入格式。

我如何link 以我想要的格式设置变速和 RemoteIO?

我在旧邮件列表中发现 Varispeed 单元只是浮点数,实际上并不进行格式转换,尽管主要类型是 FormatConverter。解决方案是使用另一个具有专用转换器子类型的转换器将我自己的格式转换为 Varispeed 想要的 FP 格式。据我所知,Varispeed 的格式限制实际上并没有记录在任何地方,所以我不知道它到底想要什么格式,除了扬声器输出格式对我有用,它应该是浮点线性 PCM 格式。

仅供参考,Varispeed 单元不进行任何音调校正,这在改变播放速率时听起来很荒谬。您可以使用响应相同变速参数的 NewTimePitch 单元,但结果听起来不像废话。