音频单元:将生成的音频转换为渲染回调

时间:2013-01-07 06:18:31

标签: cocoa audio core-audio audiounit

我一直试图通过在后面将AURenderCallbackStruct附加到音频单元的输入来操纵kAudioUnitType_Generator音频单元产生的样本,但没有成功。我设法使用以下简单的图表来使用OS X:

(input callback) -> multichannel mixer -> (intput callback) -> default output

但是我用以下(甚至更简单的)以发电机组开始的图表失败了:

speech synthesis -> (intput callback) -> default output           | fails in render callback with kAudioUnitErr_Uninitialized
audiofile player -> (intput callback) -> default output           | fails when scheduling file region with kAudioUnitErr_Uninitialized

我已经尝试了我能想到的一切,从设置ASBD格式到采样率,但我总是得到这些错误。有谁知道如何设置图表,我们可以从这些漂亮的发电机组操作样品?

下面是使用语音合成的尝试失败的渲染回调函数和图形实例化方法。除了设置文件播放之外,音频文件播放器几乎完全相同。如果我删除回调并在其中添加AUGraphConnectNodeInput,这两个设置都有效...

static OSStatus RenderCallback(void *inRefCon,
                                    AudioUnitRenderActionFlags *ioActionFlags,
                                    const AudioTimeStamp *inTimeStamp,
                                    UInt32 inBusNumber,
                                    UInt32 inNumberFrames,
                                    AudioBufferList *ioData) {

    AppDelegate *app = (AppDelegate *)inRefCon;
    AudioUnit inputUnit = app->_speechUnit;
    OSStatus status = noErr;

    status = AudioUnitRender(inputUnit, ioActionFlags, inTimeStamp, 0, inNumberFrames, ioData);
    // *** ERROR *** kAudioUnitErr_Uninitialized, code: -10867

    // ... insert processing code here...

    return status;
}

- (int)createSynthGraph {
    AUGRAPH_CHECK( NewAUGraph( &_graph ) );

    AUNode speechNode, outputNode;

    // speech synthesizer
    AudioComponentDescription speechCD = {0};
    speechCD.componentType = kAudioUnitType_Generator;
    speechCD.componentSubType = kAudioUnitSubType_SpeechSynthesis;
    speechCD.componentManufacturer = kAudioUnitManufacturer_Apple;

    // output device (speakers)
    AudioComponentDescription outputCD = {0};
    outputCD.componentType = kAudioUnitType_Output;
    outputCD.componentSubType = kAudioUnitSubType_DefaultOutput;
    outputCD.componentManufacturer = kAudioUnitManufacturer_Apple;

    AUGRAPH_CHECK( AUGraphAddNode( _graph, &outputCD, &outputNode ) );
    AUGRAPH_CHECK( AUGraphAddNode( _graph, &speechCD, &speechNode ) );

    AUGRAPH_CHECK( AUGraphOpen( _graph ) );
    AUGRAPH_CHECK( AUGraphNodeInfo( _graph, outputNode, NULL, &_outputUnit ) );
    AUGRAPH_CHECK( AUGraphNodeInfo( _graph, speechNode, NULL, &_speechUnit ) );

    // setup stream formats:
    AudioStreamBasicDescription streamFormat = [self streamFormat];
    AU_CHECK( AudioUnitSetProperty( _speechUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &streamFormat, sizeof(streamFormat) ) );

    // setup callback:
    AURenderCallbackStruct callback;
    callback.inputProc = RenderCallback;
    callback.inputProcRefCon = self;
    AUGRAPH_CHECK( AUGraphSetNodeInputCallback ( _graph, outputNode, 0, &callback ) );

    // init and start
    AUGRAPH_CHECK( AUGraphInitialize( _graph ) );
    AUGRAPH_CHECK( AUGraphStart( _graph ) );
    return 0;
}

2 个答案:

答案 0 :(得分:1)

您必须将这些节点与AUGraphConnectNodeInput连接在一起。除非连接,否则AUGraph不会在其中初始化AudioUnits 。图表中未连接到其他单位的单位将不会被初始化。

您也可以尝试在启动图表之前手动启动它们。

答案 1 :(得分:1)

kAudioUnitErr_未初始化音频单元时出现未初始化的错误。只需在设置问题属性之前初始化图表。这将初始化图形中所有打开的音频单元。来自AUGraphInitialize在AUGraph.h中的讨论:

  

在每个打开的节点/ AudioUnit上调用AudioUnitInitialize()                   (准备渲染)和参与的一个SubGraph                   相互作用。