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

时间:2015-09-11 08:13:18

标签: ios core-audio

我正在尝试设置一个非常简单的AU图 - 只是一个附有varispeed的RemoteIO用于输出。不幸的是,我似乎无法在varispeed单元上设置音频格式。我见过的其他例子似乎只是设置格式而没有麻烦。我得到的确切错误是不支持的格式。然而,RemoteIO单元内的格式转换器将很乐意采用我的格式,这是一种简单的16khz单声道16bit signed 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输出总线的输入格式,这对转换器的输出格式工作正常,但它仍然不接受我的格式作为转换器的输入格式。

如何将varispeed和RemoteIO与我想要的格式链接起来?

1 个答案:

答案 0 :(得分:2)

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

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