【问题标题】:Capture audio samples with specific Sample Rate like Android in iOS Swift在 iOS Swift 中捕获具有特定采样率的音频样本,例如 Android
【发布时间】:2019-01-14 12:51:58
【问题描述】:

我是在 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 时,它可以正常工作!

谢谢,

【问题讨论】:

  • 这可能对你有帮助github.com/google/ringdroid
  • AVAudioEngineattachNode 方法,您可以将自定义AVAudioNode 传递给该方法
  • @SatenderKumar Android 库有什么帮助?
  • @mag_zbc 你能举个例子说明一下吗?
  • 我想说这回答了你的问题,但我看到你已经找到了! stackoverflow.com/a/50434804/22147

标签: ios swift avfoundation pcm avaudioengine


【解决方案1】:

你检查过settings参数

let format16KHzMono = AVAudioFormat(settings: [AVFormatIDKey: AVAudioCommonFormat.pcmFormatInt16,
                                                               AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue,
                                                               AVEncoderBitRateKey: 16,
                                                               AVNumberOfChannelsKey: 1,
                                                               AVSampleRateKey: 8000.0] as [String : AnyObject])

【讨论】:

  • 其实我已经找到了解决办法,但是赏金会在 15 小时后到期。所以,因为你是唯一回答这个问题的人,你应该得到它。
猜你喜欢
  • 1970-01-01
  • 2021-08-12
  • 2015-09-06
  • 1970-01-01
  • 1970-01-01
  • 2022-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多