2017-02-21 51 views
0

我有一個關於在AudioGraph節點連接的問題。我的想法是從輸入接收語音,用混頻器調高音量,並用低通濾波器進行濾波。如何在AudioGraph中將RemoteIO,混音器和濾波器連接在一起?

讀「學習核心音頻」我能夠連接和操作耦合到濾波器的輸入,耦合到混頻器的輸入,但我無法加入這三個元素。

我也看了一下蘋果提供的示例代碼,但是將它適配爲我的項目對我來說不起作用。我舉報我的代碼:

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

所以我尋求幫助。我該怎麼辦?我做得好的連接?我必須設置其他屬性?

感謝您的關注。

回答

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