如何在 AudioGraph 中将 RemoteIO、Mixer 和 Filter 连接在一起?

How to connect together RemoteIO, Mixer and Filter in AudioGraph?

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

读这本书"Learning Core Audio"我能够连接和操作耦合到滤波器的输入,耦合到混频器的输入,但我无法连接这三个元素。

我也查看了 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");

所以我寻求帮助。我应该怎么办?我做的连接好吗?我必须设置其他属性吗?

感谢您的关注。

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

    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);