類似AVAudioEngine的功能,一個Engine可以將N個connect連接(串聯和並聯)在一起,這樣來實現多個輸入源,多層處理效果的混合輸出。實現這個所需功能也是通過這樣的方案來實現的。也就是說,在使用的音效和目標輸出之間增加一個自己的自定義音效處理,在實現這個音效處理時,將最終BUFFER數據設置成為上一音效處理效果輸入的BUFFER數據,並且存儲這些數據寫入文件。
1.綁定兩個DSP
system->playSound(sound, 0, false, &channel); FMOD::DSP *mydsp = configuration(system); int result = system->getMasterChannelGroup(&mastergroup); //mastergroup應該類似MixOutNode //dsp需要的音效處理效果 //mydsp自定義的音效處理 ERRCHECK(result); //分別綁定,注意先后順序 mastergroup->addDSP(0, dsp); result = mastergroup->addDSP(0, mydsp); ERRCHECK(result);
2.在自定義的音效效果處理函數中,截取數據並且將上一步處理結果設定為輸出
/* dspdesc.read = myDSPCallback; system->createDSP(&dspdesc, &mydsp); */ FMOD_RESULT F_CALLBACK myDSPCallback(FMOD_DSP_STATE *dsp_state, float *inbuffer, float *outbuffer, unsigned int length, int inchannels, int *outchannels) { printf("+++++++++"); mydsp_data_t *data = (mydsp_data_t *)dsp_state->plugindata; /* This loop assumes inchannels = outchannels, which it will be if the DSP is created with '0' as the number of channels in FMOD_DSP_DESCRIPTION. Specifying an actual channel count will mean you have to take care of any number of channels coming in, but outputting the number of channels specified. Generally it is best to keep the channel count at 0 for maximum compatibility. */ for (unsigned int samp = 0; samp < length; samp++) { /* Feel free to unroll this. */ for (int chan = 0; chan < *outchannels; chan++) { /* This DSP filter just halves the volume! Input is modified, and sent to output. 將輸入賦予輸出,並且保存這個輸入結果(上一音效處理過后的PCM數據) */ data->buffer[(samp * *outchannels) + chan] = outbuffer[(samp * inchannels) + chan] = inbuffer[(samp * inchannels) + chan] * data->volume_linear; } } printf("--->%u\n",length); data->channels = inchannels; //這里有個插曲,原來長度設定為length,結果PCM數據播出一直是斷斷續續的 fwrite(data->buffer, sizeof(float), length**outchannels, saveFile); return FMOD_OK; }
3.注意點(采樣率和輸出通道數)
經設定為
system->setDSPBufferSize(44100, 2);
但是測定為獲取下來的兩項指標為:44100和1(設定其它數據無法正常獲取,出現很大的雜音等,急需熟手指導)
3ps:實際效果是指標可能為22050和2,也可能設定和輸出並沒有關系只是讀取的要求
end:以上就是成功實現該項功能的最主要說明,歡迎大家一起探討