2016-09-20 92 views
1

我對從麥克風採集的音頻下采樣有問題。我使用AVAudioEngine從下面的代碼話筒取樣品:AVAudioEngine下采樣問題

assert(self.engine.inputNode != nil) 
let input = self.engine.inputNode! 

let audioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false)  
let mixer = AVAudioMixerNode() 
engine.attach(mixer) 
engine.connect(input, to: mixer, format: input.inputFormat(forBus: 0)) 

do { 
    try engine.start() 

    mixer.installTap(onBus: 0, bufferSize: 1024, format: audioFormat, block: { 
      (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in 
     //some code here 
    }) 

} catch let error { 
    print(error.localizedDescription) 
} 

此代碼工作在iPhone 5S偉大的,因爲麥克風輸入8000Hz的,緩衝器被裝滿了來自麥克風的數據。

問題是我想能夠從iPhone 6s(及更高版本)錄製哪些麥克風以16000Hz記錄。而什麼奇怪的是,如果我連與發動機mainmixernode的mixernode(用下面的代碼):

engine.connect(mixer, to: mainMixer, format: audioFormat) 

這種實際工作,我也得到了緩衝器具有8000Hz的格式和聲音出來完美間苗,唯一的問題是聲音也從我不想要的揚聲器中發出(如果我不連接它,緩衝區是空的)。

有誰知道如何解決這個問題?

任何幫助,輸入或思想非常感激。

回答

1

我通過簡單地改變我的混頻器的體積爲0

mixer.volume = 0 

這使我能夠把發動機的優勢,重新取樣任意採樣率,以我想要的採樣率主調音臺真棒能力解決了這個問題,而不是聽麥克風反饋迴路直接從揚聲器輸出。如果有人需要澄清這一點,請讓我知道。

這是我現在的代碼:

assert(self.engine.inputNode != nil) 
let input = self.engine.inputNode! 

let audioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false)  
let mixer = AVAudioMixerNode() 
engine.attach(mixer) 
engine.connect(input, to: mixer, format: input.inputFormat(forBus: 0)) 
mixer.volume = 0 
engine.connect(mixer, to: mainMixer, format: audioFormat) 

do { 
    try engine.start() 

    mixer.installTap(onBus: 0, bufferSize: 1024, format: audioFormat, block: { 
     (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in 
     //some code here 
    }) 

} catch let error { 
    print(error.localizedDescription) 
} 
+0

在其中定義 「mainMixer」? –

+0

很早以前,我寫了這段代碼,但我95%確定這是AVAudioEngines主混音器節點。 – nullforlife

+1

如果我使用這段代碼,它會給我緩衝區中的所有零。你知道我做錯了什麼嗎?我正在使用具有44100Hz麥克風輸入採樣率的iPhone 7。 –