使用单声道input和立体声输出设置audio单元iOS

我正在尝试设置一个具有单声道input和立体声输出的audio单元。 打算通过左声道输出播放正弦波音,并通过右声道周期性地输出不同的符号波。

我收到错误,

'NSInternalInconsistencyException', reason: ' Error initialing unit: -50; 

当我尝试在这里初始化我的audio单元时,

 // Initialize audio unit OSErr err = AudioUnitInitialize(self.ioUnit); NSAssert1(err == noErr, @"Error initializing unit: %hd", err); 

我相信这与我如何设置audio设备有关,

 // Audio component description AudioComponentDescription desc; desc.componentType = kAudioUnitType_Output; desc.componentSubType = kAudioUnitSubType_RemoteIO; desc.componentManufacturer = kAudioUnitManufacturer_Apple; desc.componentFlags = 0; desc.componentFlagsMask = 0; // Get component AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc); // Get Audio units AudioComponentInstanceNew(inputComponent, &ioUnit); // Enable input, which disabled by default. Output enable by default UInt32 enableInput = 1; AudioUnitSetProperty(ioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &enableInput, sizeof(enableInput)); AudioStreamBasicDescription monoStreamFormat; monoStreamFormat.mSampleRate = 44100.00; monoStreamFormat.mFormatID = kAudioFormatLinearPCM; monoStreamFormat.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical; monoStreamFormat.mBytesPerPacket = 2; monoStreamFormat.mBytesPerFrame = 2; monoStreamFormat.mFramesPerPacket = 1; monoStreamFormat.mChannelsPerFrame = 1; monoStreamFormat.mBitsPerChannel = 16; // Apply format to input of ioUnit AudioUnitSetProperty(ioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kInputBus, &monoStreamFormat, sizeof(monoStreamFormat)); AudioStreamBasicDescription stereoStreamFormat; stereoStreamFormat.mSampleRate = 44100.00; stereoStreamFormat.mFormatID = kAudioFormatLinearPCM; stereoStreamFormat.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical; stereoStreamFormat.mBytesPerPacket = 4; stereoStreamFormat.mBytesPerFrame = 4; stereoStreamFormat.mFramesPerPacket = 1; stereoStreamFormat.mChannelsPerFrame = 2; stereoStreamFormat.mBitsPerChannel = 32; // Apply format to output of ioUnit AudioUnitSetProperty(ioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kOutputBus, &stereoStreamFormat, sizeof(stereoStreamFormat)); // Set input callback AURenderCallbackStruct callbackStruct; callbackStruct.inputProc = inputCallback; callbackStruct.inputProcRefCon = (__bridge void *)(self); AudioUnitSetProperty(ioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, kInputBus, &callbackStruct, sizeof(callbackStruct)); // Set output callback callbackStruct.inputProc = outputCallback; callbackStruct.inputProcRefCon = (__bridge void *)(self); AudioUnitSetProperty(ioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, kOutputBus, &callbackStruct, sizeof(callbackStruct)); 

但我找不到任何关于错误代码-50的东西,所以我不确定它不喜欢什么。 我在截止date,所以任何帮助,非常感谢。

kAudioFormatFlagsAudioUnitCanonical是32位8.24定点格式。 这意味着mBytesPerFrame对于单声道应该是4,而对于立体声应该是8,并且mBitsPerChannel对于立体声和单声道应该是32。 将它设置为16位可能会产生这个错误。

这就是说,我怀疑你想要8.24。 你可能想要kAudioFormatFlagsCanonical来获得标准的16位audio。 或者,如果你想要32位,使用32位浮点(kAudioFormatFlagIsFloat | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved)。 在处理过程中更容易处理math。

此外,您正在虚拟input和输出上设置格式,而不是客户端input和输出,我不认为是你想要的。 这里的API相当混乱,但要设置客户端input格式,请使用kAudioUnitScope_Input,kOutputBus。 这里的逻辑是你设置内部核心audioinput转换器单元输出的格式,从你的代码angular度来看,它是inputaudio。 混乱,我知道。 有关更多详细信息,请参阅Audio Unit文档。 同样,要设置客户端输出格式,请使用kAudioUnitScope_Output,kInputBus。

每通道32位是太多的位,以便将2个通道合并到4个字节中。 试试16。