如何在Core Audio中设置Splitter单元

时间:2015-09-20 04:49:52

标签: core-audio core

从“学习核心音频”一书中,我学会了如何混合流1(来自麦克风的语音)和流2(语音合成器)然后连接其输出 输出单位(发言人)。 现在我尝试在监听扬声器输出的同时记录其输出。 所以我设置了一个Splitter单元,将mixerUnit的输出连接到它的输入, 将其outputScope 0连接到outputUnit,将renderCallback(用于记录)设置为其outputScope 1.(我认为Splitter Unit将输入流分割为总线0和总线1) 结果是我可以从扬声器中听到它,但是由于它的“拉模型”,renderCallback永远不会被调用,因为它没有连接到任何输出单元。 但如果我设置另一个通用输出单元然后连接到它,AUGraph会向我显示一条错误消息。(允许输出2号输出单元) 有人可以帮帮我吗?

CheckError(AUGraphNodeInfo(player->graph,
                           outputNode,
                           NULL,
                           &player->outputUnit),
           "AUGraphNodeInfo failed");

CheckError(AUGraphNodeInfo(player->graph,
                           speechNode,
                           NULL,
                           &player->speechUnit),
           "AUGraphInfo failed");

AudioUnit mixerUnit;
CheckError(AUGraphNodeInfo(player->graph,
                           mixerNode,
                           NULL,
                           &mixerUnit),
           "AUGraphNOdeInfo failed");

AudioUnit splitterUnit;
CheckError(AUGraphNodeInfo(player->graph,
                           splitterNode,
                           NULL,
                           &splitterUnit),
           "AUGraphInfo failed");

CheckError(AudioUnitSetProperty(mixerUnit,
                                kAudioUnitProperty_StreamFormat,
                                kAudioUnitScope_Input,
                                0,
                                &player->streamFormat,
                                propertySize),
           "Couldn't set stream format on mixer unit bus 0");

CheckError(AudioUnitSetProperty(mixerUnit,
                                kAudioUnitProperty_StreamFormat,
                                kAudioUnitScope_Input,
                                1,
                                &player->streamFormat,
                                propertySize),
           "Couldn't set stream format on mixer unit bus");

CheckError(AudioUnitSetProperty(splitterUnit,
                                kAudioUnitProperty_StreamFormat,
                                kAudioUnitScope_Input,
                                0,
                                &player->streamFormat,
                                propertySize),
           "Couldn't set stream format on Splitter unit");

CheckError(AUGraphConnectNodeInput(player->graph,
                                   splitterNode,
                                   0,
                                   outputNode,
                                   0),
           "Couldn't connect splitter 0 to outputNode");

CheckError(AUGraphConnectNodeInput(player->graph,
                                   speechNode,
                                   0,
                                   mixerNode,
                                   1),
           "Couldn't connect speech speechNode to mixer input(1)");

AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = GraphRenderProc;
callbackStruct.inputProcRefCon = player;
CheckError(AudioUnitSetProperty(mixerUnit,//was outputUnit
                                kAudioUnitProperty_SetRenderCallback,
                                kAudioUnitScope_Input,
                                0,
                                &callbackStruct,
                                sizeof(callbackStruct)),
           "Couldn't set render callback on mixer unit");

AURenderCallbackStruct recorderCallback;
recorderCallback.inputProc = recordRenderProc;
recorderCallback.inputProcRefCon = player;

CheckError(AudioUnitSetProperty(splitterUnit,
                                kAudioUnitProperty_SetRenderCallback,
                                kAudioUnitScope_Output,
                                0,
                                &recorderCallback,
                                sizeof(recorderCallback)),
           "Couldn't set render callback on splitter output");

1 个答案:

答案 0 :(得分:0)

您是否只是想将最终的混合输出录制到文件中?

如果是这样,您可以在渲染回调中写入该文件。但是,你不能阻止很长时间。你必须在下一次回调之前完成你的写作。最简单的编写方法可能是使用宏中央调度来异步写入文件。