如何使用 8000 PCM 格式的 AVAudioEngine 录制和播放?

Posted

技术标签:

【中文标题】如何使用 8000 PCM 格式的 AVAudioEngine 录制和播放?【英文标题】:how to record and play with AVAudioEngine with 8000 PCM format? 【发布时间】:2018-03-13 08:18:23 【问题描述】:

我想将此代码用于 VoIP 服务。

我正在使用 web-socket 并使用它发送:let data = self.toNSData(PCMBuffer: buffer) 和播放:let audioBuffer = self.toPCMBuffer(data: data) 在另一台设备上)

我用过:https://github.com/Lkember/IntercomTest 并工作,但数据量很大。我觉得 41100 速率对于发送数据来说是一个非常大的大小,我想将较低速率的缓冲区大小减小到 8000。

但我不知道如何在没有错误的情况下降低采样率!

我的失败代码如下:

@IBAction func start(_ sender: Any) 


        var engine = AVAudioEngine()
        let input = engine.inputNode
        let bus = 0

        let localAudioPlayer: AVAudioPlayerNode = AVAudioPlayerNode()

        let mixer = AVAudioMixerNode()

        let fmt = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false)

        engine.attach(mixer)

        engine.connect(input, to: mixer, format: input.outputFormat(forBus: 0))

        mixer.volume = 0
        engine.connect(mixer, to: localAudioPlayer, format: fmt)

        localAudioPlayer.installTap(onBus: bus, bufferSize: 512, format: fmt)  (buffer, time) -> Void in
            // 8kHz buffers!
            print(buffer.format)
            localAudioPlayer.scheduleBuffer(buffer)
        

        let data = self.toNSData(PCMBuffer: buffer)

        let audioBuffer = self.toPCMBuffer(data: data)

        localAudioPlayer.scheduleBuffer(audioBuffer)
        if (!localAudioPlayer.isPlaying) 
            localAudioPlayer.play()


        try! engine.start()

    

    func toNSData(PCMBuffer: AVAudioPCMBuffer) -> NSData 
        let channelCount = 1  // given PCMBuffer channel count is 1
        let channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: channelCount)
        let ch0Data = NSData(bytes: channels[0], length:Int(PCMBuffer.frameCapacity * PCMBuffer.format.streamDescription.pointee.mBytesPerFrame))
        return ch0Data
    

    func toPCMBuffer(data: NSData) -> AVAudioPCMBuffer 
        let audioFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false)  // given NSData audio format
        let PCMBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: UInt32(data.length) / audioFormat.streamDescription.pointee.mBytesPerFrame)
        PCMBuffer.frameLength = PCMBuffer.frameCapacity
        let channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: Int(PCMBuffer.format.channelCount))
        data.getBytes(UnsafeMutableRawPointer(channels[0]) , length: data.length)
        return PCMBuffer
    

【问题讨论】:

您找到解决方案了吗?我面临同样的问题 【参考方案1】:

您可以根据需要使用以下代码进行转换。

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()

另外

您可以使用commonFormat 代替settings 参数。

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

【讨论】:

以上是关于如何使用 8000 PCM 格式的 AVAudioEngine 录制和播放?的主要内容,如果未能解决你的问题,请参考以下文章

将 a-law 转换为 pcm

如何使用 FFT 从 PCM 获取频率数据

如何将wav音频文件格式为pcm转化为ima adpcm格式

如何在 Windows 中从任何格式转换为 PCM

Naudio wav格式问题

wav格式和pcm格式怎么相互转换?