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

时间:2018-03-13 08:18:24

标签: swift buffer avfoundation avaudioplayer avaudioengine

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

我正在使用网络套接字并随之发送: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 个答案:

答案 0 :(得分: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])