如何在AudioGraph中连接RemoteIO,Mixer和Filter?

时间:2017-02-21 08:43:22

标签: filter audiounit mixer remoteio augraph

我对AudioGraph中的节点连接有疑问。我的想法是从输入接收语音,用调音台调高音量,用低通滤波器过滤。

阅读本书"学习核心音频"我能够连接并操作耦合到滤波器的输入,输入耦合到混频器,但我无法加入这三个元素。

我还看了Apple提供的示例代码,但是为我的项目调整它并不适合我。我报告了我的代码:

    AUGraph AudioGraph;



    CheckError(NewAUGraph(&AudioGraph), "No new Graph");




    AUNode rioNode;
    AUNode mixerNode;
    AUNode filterNode;

    AudioComponentDescription AudioCompDescRIO;
    AudioCompDescRIO.componentType  = kAudioUnitType_Output;
    AudioCompDescRIO.componentSubType = kAudioUnitSubType_RemoteIO;
    AudioCompDescRIO.componentManufacturer = kAudioUnitManufacturer_Apple;
    AudioCompDescRIO.componentFlags = 0;
    AudioCompDescRIO.componentFlagsMask = 0;

    CheckError(AUGraphAddNode(AudioGraph, &AudioCompDescRIO, &rioNode), "No add Node");


    AudioComponentDescription AudioCompDescMixer;
    AudioCompDescMixer.componentType = kAudioUnitType_Mixer;
    AudioCompDescMixer.componentSubType = kAudioUnitSubType_MultiChannelMixer;
    AudioCompDescMixer.componentManufacturer = kAudioUnitManufacturer_Apple;
    AudioCompDescMixer.componentFlags = 0;
    AudioCompDescMixer.componentFlagsMask = 0;

    CheckError(AUGraphAddNode(AudioGraph, &AudioCompDescMixer, &mixerNode), "No add Node");


    AudioComponentDescription AudioCompDescFiler;
    AudioCompDescFiler.componentType = kAudioUnitType_Effect;
    AudioCompDescFiler.componentSubType = kAudioUnitSubType_LowPassFilter;
    AudioCompDescFiler.componentManufacturer = kAudioUnitManufacturer_Apple;
    AudioCompDescFiler.componentFlags = 0;
    AudioCompDescFiler.componentFlagsMask = 0;

    CheckError(AUGraphAddNode(AudioGraph, &AudioCompDescFiler, &filterNode), "No add Node");




    CheckError(AUGraphOpen(AudioGraph), "No open Graph");



    AudioUnit rioUnit;
    AudioUnit mixerUnit;
    AudioUnit filterUnit;



    CheckError(AUGraphNodeInfo(AudioGraph, rioNode, NULL, &rioUnit), "No node info");
    CheckError(AUGraphNodeInfo(AudioGraph, mixerNode, NULL, &mixerUnit), "No node info");
    CheckError(AUGraphNodeInfo(AudioGraph, filterNode, NULL, &filterUnit), "No node info");







    UInt32 bytesPerSample = sizeof(Float32);

    AudioStreamBasicDescription asbd;

    bzero(&asbd, sizeof(asbd));
    asbd.mSampleRate = 44100;
    asbd.mFormatID = kAudioFormatLinearPCM;
    asbd.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved;
    asbd.mBitsPerChannel = 8 * bytesPerSample;
    asbd.mBytesPerFrame = bytesPerSample;
    asbd.mBytesPerPacket = bytesPerSample;
    asbd.mFramesPerPacket = 1;
    asbd.mChannelsPerFrame = 2;





    AudioUnitElement inputElement = 1;


    UInt32 enableIO = 1;
    CheckError(AudioUnitSetProperty(rioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, inputElement, &enableIO, sizeof(enableIO)), "No set property");


    AudioUnitSetProperty(mixerUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 1, &asbd, sizeof(asbd));
    AudioUnitSetProperty(mixerUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &asbd, sizeof(asbd));


    AURenderCallbackStruct callbackStruct;
    callbackStruct.inputProc = inputRenderCallback;
    callbackStruct.inputProcRefCon = (__bridge void * _Nullable)(self);
    AUGraphSetNodeInputCallback(AudioGraph, mixerNode, 0, &callbackStruct);




    AUGraphConnectNodeInput(AudioGraph, rioNode, 1, mixerNode, 0);
    AUGraphConnectNodeInput(AudioGraph, mixerNode, 0, filterNode, 0);
    AUGraphConnectNodeInput(AudioGraph, filterNode, 0, rioNode, 0);




    CheckError(AUGraphInitialize(AudioGraph), "No initialize graph");

所以我请求帮助。我该怎么办?我做得很好吗?我不得不设置其他属性?

感谢您的关注。

1 个答案:

答案 0 :(得分:0)

我终于设法运行了所有三个元素。我犯的错误与属性的设置有关。我留下了工作代码,希望它可以帮助需要它的人。再见!

    CheckError(NewAUGraph(&AudioGraph), "No new Graph");

    AUNode rioNode;
    AUNode mixerNode;
    AUNode filterNode;


    AudioComponentDescription AudioCompDescRIO;
    AudioCompDescRIO.componentType  = kAudioUnitType_Output;
    AudioCompDescRIO.componentSubType = kAudioUnitSubType_RemoteIO;
    AudioCompDescRIO.componentManufacturer = kAudioUnitManufacturer_Apple;
    AudioCompDescRIO.componentFlags = 0;
    AudioCompDescRIO.componentFlagsMask = 0;

    CheckError(AUGraphAddNode(AudioGraph, &AudioCompDescRIO, &rioNode), "No add Node");


    AudioComponentDescription AudioCompDescMixer;
    AudioCompDescMixer.componentType = kAudioUnitType_Mixer;
    AudioCompDescMixer.componentSubType = kAudioUnitSubType_MultiChannelMixer;
    AudioCompDescMixer.componentManufacturer = kAudioUnitManufacturer_Apple;
    AudioCompDescMixer.componentFlags = 0;
    AudioCompDescMixer.componentFlagsMask = 0;

    CheckError(AUGraphAddNode(AudioGraph, &AudioCompDescMixer, &mixerNode), "No add Node");


    AudioComponentDescription AudioCompDescFiler;
    AudioCompDescFiler.componentType = kAudioUnitType_Effect;
    AudioCompDescFiler.componentSubType = kAudioUnitSubType_LowPassFilter;
    AudioCompDescFiler.componentManufacturer = kAudioUnitManufacturer_Apple;
    AudioCompDescFiler.componentFlags = 0;
    AudioCompDescFiler.componentFlagsMask = 0;

    CheckError(AUGraphAddNode(AudioGraph, &AudioCompDescFiler, &filterNode), "No add Node");


    CheckError(AUGraphOpen(AudioGraph), "No open Graph");

    AudioUnit rioUnit;
    AudioUnit mixerUnit;
    AudioUnit filterUnit;


    CheckError(AUGraphNodeInfo(AudioGraph, rioNode, NULL, &rioUnit), "No node info");
    CheckError(AUGraphNodeInfo(AudioGraph, mixerNode, NULL, &mixerUnit), "No node info");
    CheckError(AUGraphNodeInfo(AudioGraph, filterNode, NULL, &filterUnit), "No node info");


    UInt32 bytesPerSample = sizeof(Float32);

    AudioStreamBasicDescription asbd;
    bzero(&asbd, sizeof(asbd));
    asbd.mSampleRate = 44100;
    asbd.mFormatID = kAudioFormatLinearPCM;
    asbd.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved;
    asbd.mBitsPerChannel = 8 * bytesPerSample;
    asbd.mBytesPerFrame = bytesPerSample;
    asbd.mBytesPerPacket = bytesPerSample;
    asbd.mFramesPerPacket = 1;
    asbd.mChannelsPerFrame = 2;

    AudioUnitElement inputElement = 1;

    AudioUnitSetProperty(rioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, inputElement, &asbd, sizeof(asbd));


    UInt32 enableIO = 1;
    AudioUnitSetProperty(rioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, inputElement,  &enableIO, sizeof(enableIO));


    UInt32 mixerElementCount = 3;
    AudioUnitSetProperty(mixerUnit, kAudioUnitProperty_ElementCount, kAudioUnitScope_Input, 0, &mixerElementCount, sizeof(mixerElementCount));

    AURenderCallbackStruct callbackStruct;
    callbackStruct.inputProc = &inputRenderCallback;
    callbackStruct.inputProcRefCon = (__bridge void *)self;

    AudioUnitSetProperty(mixerUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 1, &callbackStruct, sizeof(callbackStruct));

    AudioUnitSetProperty(mixerUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 2, &callbackStruct, sizeof(callbackStruct));

    AudioUnitSetParameter (mixerUnit, kMultiChannelMixerParam_Volume, kAudioUnitScope_Output, 0, 1, 0);

    AudioUnitSetParameter(filterUnit, kLowPassParam_CutoffFrequency, kAudioUnitScope_Global, 0, 2500, 0);

    AUGraphConnectNodeInput(AudioGraph, rioNode, 1, mixerNode, 0);
    AUGraphConnectNodeInput(AudioGraph, mixerNode, 0, filterNode, 0);
    AUGraphConnectNodeInput(AudioGraph, filterNode, 0, rioNode, 0);

    CAShow(AudioGraph);

    CheckError(AUGraphInitialize(AudioGraph), "No initialize graph");

    AUGraphStart(AudioGraph);