2016-09-20 74 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。 –