我是使用IOS中的声音和AVAudioEngine的初学者,并且正在开发一个将音频样本捕获为缓冲区并进行分析的应用程序。此外,采样率必须为8000 kHz,并且还必须编码为PCM16Bit,但是AVAudioEngine中的默认inputNode为44.1 kHz。
在Android中,过程非常简单:
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
8000, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize);
然后启动缓冲区的读取功能。
我进行了很多搜索,但没有找到任何类似的示例。相反,我遇到的所有示例都以默认节点的采样率(44.1 kHz)捕获采样,如下所示:
let input = audioEngine.inputNode
let inputFormat = input.inputFormat(forBus: 0)
input.installTap(onBus: 0, bufferSize: 640, format: inputFormat) { (buffer, time) -> Void in
print(inputFormat)
if let channel1Buffer = buffer.floatChannelData?[0] {
for i in 0...Int(buffer.frameLength-1) {
print(channel1Buffer[i])
}
}
}
try! audioEngine.start()
因此,我想使用具有8000 kHz采样率和PCM16Bit编码的AVAudioEngine捕获音频样本。
*编辑:
我找到了将输入转换为8 kHz的解决方案:
let inputNode = audioEngine.inputNode
let downMixer = AVAudioMixerNode()
let main = audioEngine.mainMixerNode
let format = inputNode.inputFormat(forBus: 0)
let format16KHzMono = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 8000, channels: 1, interleaved: true)
audioEngine.attach(downMixer)
downMixer.installTap(onBus: 0, bufferSize: 640, format: format16KHzMono) { (buffer, time) -> Void in
do{
print(buffer.description)
if let channel1Buffer = buffer.int16ChannelData?[0] {
// print(channel1Buffer[0])
for i in 0 ... Int(buffer.frameLength-1) {
print((channel1Buffer[i]))
}
}
}
}
audioEngine.connect(inputNode, to: downMixer, format: format)
audioEngine.connect(downMixer, to: main, format: format16KHzMono)
audioEngine.prepare()
try! audioEngine.start()
,但是当我使用
.pcmFormatInt16
时不起作用。但是,当我使用.pcmFormatFloat32
时,效果很好!谢谢,,
最佳答案
您是否使用settings
参数进行了检查
let format16KHzMono = AVAudioFormat(settings: [AVFormatIDKey: AVAudioCommonFormat.pcmFormatInt16,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue,
AVEncoderBitRateKey: 16,
AVNumberOfChannelsKey: 1,
AVSampleRateKey: 8000.0] as [String : AnyObject])