我有一个问题,下采样音频从麦克风。我使用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上工作得很好,因为麦克风输入是8000 is,缓冲区中充满了来自麦克风的数据。
问题是,我想要能够记录从iPhone 6s (及以上)的麦克风记录16000赫兹。奇怪的是,如果我将混合节点与引擎连接到主混合节点(用下面的代码):
engine.connect(mixer, to: mainMixer, format: audioFormat)这实际上很有效,我得到的缓冲器的格式是8000 it,声音的采样非常低,唯一的问题是声音也来自我不想要的扬声器(如果我不连接它,缓冲器是空的)。
有人知道如何解决这个问题吗?
任何帮助、输入或想法都是非常感谢的。
发布于 2020-06-05 12:46:32
另一种方法,在AVAudioConverter中使用Swift 5
let engine = AVAudioEngine()
func setup() {
let input = engine.inputNode
let bus = 0
let inputFormat = input.outputFormat(forBus: bus )
guard let outputFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: true), let converter = AVAudioConverter(from: inputFormat, to: outputFormat) else{
return
}
input.installTap(onBus: bus, bufferSize: 1024, format: inputFormat) { (buffer, time) -> Void in
var newBufferAvailable = true
let inputCallback: AVAudioConverterInputBlock = { inNumPackets, outStatus in
if newBufferAvailable {
outStatus.pointee = .haveData
newBufferAvailable = false
return buffer
} else {
outStatus.pointee = .noDataNow
return nil
}
}
if let convertedBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: AVAudioFrameCount(outputFormat.sampleRate) * buffer.frameLength / AVAudioFrameCount(buffer.format.sampleRate)){
var error: NSError?
let status = converter.convert(to: convertedBuffer, error: &error, withInputFrom: inputCallback)
assert(status != .error)
// 8kHz buffers
print(convertedBuffer.format)
}
}
do {
try engine.start()
} catch { print(error) }
}发布于 2016-09-21 15:45:03
我解决了这个问题,只需将我的混频器卷更改为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)
}发布于 2019-07-19 00:11:44
我发现唯一能改变采样率的方法是
AVAudioSettings.sharedInstance().setPreferredSampleRate(...)不幸的是,这并不能保证你能得到你想要的样本率,尽管这看起来像是8000个,12000,16000,22050,44100都起作用了。
以下几点不起作用:
https://stackoverflow.com/questions/39595444
复制相似问题