AVAudioEngine的使用心得

1.AVAudioEngine負(fù)責(zé):

  ? 管理所有的音頻節(jié)點(diǎn)(audio nodes)
  ? 連接所有的音頻節(jié)點(diǎn)使其運(yùn)作形成鏈條(active chains)
  ? 動(dòng)態(tài)的獲取(attach)和配置所有音頻的節(jié)點(diǎn)。
  ? 開啟和停止API

2.有多種node,負(fù)責(zé)實(shí)現(xiàn)不同的功能,而AVAudioEngine在運(yùn)行中可以自由組合、拆卸node,node經(jīng)由它們各自的輸入被連接到下一級的輸出,從而可以靈活實(shí)現(xiàn)想要的效果,但是AVAudioEngine在運(yùn)行中不能拆卸(detach)inputNode和OutputNode,且disconectNodes(兩個(gè)node)時(shí),必須同時(shí)disconect掉前一個(gè)node的output和后一個(gè)node的input。由于沒有發(fā)現(xiàn)這些細(xì)節(jié),導(dǎo)致采用比較復(fù)雜的方式解決插扒耳機(jī)的問題以至于觸發(fā)了新的問題。

3.AVAudioEngine實(shí)例化后默認(rèn)有三個(gè)node,分別為inputNode、outputNode,這兩個(gè)node不可以被detach,還有一個(gè)可選的mainMixerNode。

4.在使用AVAudioEngine時(shí),inputNode、outputNode分別對應(yīng)硬件的麥克風(fēng)和揚(yáng)聲器,需要檢查其采樣率和聲道數(shù),若采樣率為0,則不可用。

5.使用實(shí)例,實(shí)現(xiàn)錄音+耳返+音樂,且耳返和錄音結(jié)果中有混響效果:

inputNode(麥克風(fēng))
                     mixerNode(混音)–> reverbNode(混響)–> outputNode(輸出)
playerNode(播放背景音樂)
// attach EngineNodes
self.input = self.engine.inputNode;// 輸入
self.output = self.engine.outputNode;// 輸出
[self.engine attachNode:self.reverb];// 混響
[self.engine attachNode:self.mixer];
[self.engine attachNode:self.player];
// make Engine Connections
[self.engine connect:self.player to:self.mixer fromBus:0 toBus:0 format:[file processingFormat]];
[self.engine connect:self.input to:self.mixer fromBus:0 toBus:1 format:[self.input inputFormatForBus:0]];
[self.engine connect:self.mixer to:self.reverb  format:self.audioFormat];
[self.reverb installTapOnBus:0 bufferSize:8192 format:self.audioFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
    if (self->_isRecording) {// write buffer to file
        [weakSelf.audioFile writeFromBuffer:buffer error:&writeBufferError];
        weakSelf.currentRecTime += buffer.frameLength/when.sampleRate;        
    }
}];
[self.engine connect:self.reverb to:self.output format:self.audioFormat];
// start && play
[self.engine prepare];
[self.engine startAndReturnError:&startError];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容