AVAudioPCMBuffer able to get audio with static/white noise only

做~自己de王妃 提交于 2020-03-25 18:38:13

问题


Thank you for taking the time and reading my question.

Target:

I am getting some audio data from UDP server and I have to convert them into PCM format to play the actual audio realtime.

Query:

For some reason using below code did not work for the format of my AVAudioPCMBuffer and yielded no sound, or a memory error. However, I am able to get white noise/static to play from the speakers by converting the NSData to a AVAudioPCMBuffer. I have a feeling that I am getting close to the answer, because whenever I speak into the microphone I hear a different frequency of static.

Here is my code:

1. Converting received data to PCM Buffer:

func toPCMBuffer(format: AVAudioFormat, data: NSData) -> AVAudioPCMBuffer {
    let PCMBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: UInt32(data.count) / format.streamDescription.pointee.mBytesPerFrame)
    PCMBuffer!.frameLength = PCMBuffer!.frameCapacity
    let channels = UnsafeBufferPointer(start: PCMBuffer?.int16ChannelData, count: Int(PCMBuffer!.format.channelCount))
    data.getBytes(UnsafeMutableRawPointer(channels[0]) , length: data.count)
    return PCMBuffer!
}

2. Playing the converted buffer into player.

if let receivedData = notification.userInfo?["data"] as? NSData {
        let inputFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 8000, channels: 1, interleaved: false)

        let pcmBufferData = self.toPCMBuffer(format: inputFormat!, data: receivedData) // converting raw data to pcm buffer

        let mainMixer = audioEngine.inputNode
        audioEngine.attach(audioFilePlayer)
        let outputFormat = mainMixer.outputFormat(forBus: 1)
        audioEngine.connect(audioFilePlayer, to:audioEngine.outputNode, format: outputFormat)
        audioFilePlayer.scheduleBuffer(pcmBufferData, at: nil, completionHandler: nil)
        audioEngine.prepare()
        do {
            try audioEngine.start()
        } catch _{

            print("error")
        }
        audioFilePlayer.play()
    }

I don't understand what the issue is? Your help will be much appreciated. Thanks :)

Edit 1:

As @ Vladimir's suggestion, I updated my code as below:

func configAudioEngine(){ // Config audio Engine only once.
    let mainMixer = audioEngine.inputNode
    audioEngine.attach(audioFilePlayer)
    let outputFormat = mainMixer.outputFormat(forBus: 0)
    //            let outputFormat = mainMixer.outputFormat(forBus: bus)
    audioEngine.connect(audioFilePlayer, to:audioEngine.outputNode, format: outputFormat)
    audioEngine.prepare()
    do {
        try audioEngine.start()
    } catch _{

        print("error")
    }
}

// Receiving Data in notification from UDP socket delegate, converting it to pcm buffer and then playing.
// Note : This callback gets called everytime I receive data from UDP

@objc func didReceiveData(notification:NSNotification) {
    if let receivedData = notification.userInfo?["data"] as? NSData
    {
        let inputFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 80000, channels: 1, interleaved: false)

        let pcmBufferData = self.toPCMBuffer(format: inputFormat!, data: receivedData)

        audioFilePlayer.play()
    }
}
// Still getting the white noise without any improvement in Audio.

//Here is my received Data in UDP
//incoming message: 1600 bytes
<48003c00 36003400 36002800 1600f5ff d2ffb8ff afffb6ff d1fff3ff 19003900 45004200 26000600 e1ffc2ff b7ffbbff c8ffd5ff dfffe3ff e1ffe9ff f4ff1200 3c006f00 93009900 88006000 2400e1ff a9ff8aff 87ff8eff a9ffc1ff d1ffdaff dcffe9ff 04002c00 52006c00 74005e00 2900e3ff 9fff76ff 78ffa2ff ebff2d00 5e007400 69005700 3e003000 2d002f00 28000a00 d7ff93ff 51ff24ff 23ff59ff a8ff1200 6700a100 af009500 77004a00 36002100 1e001700 0300deff abff77ff 5eff6fff 9cffebff 2d006800 80007500 4f001900 f3ffdbff d1ffd6ff d2ffd5ff ceffc8ff d1ffe0ff 0c003800 6e009900 b000a800 7e003e00 f8ffb6ff 7cff55ff 3fff3fff 50ff71ff 99ffc8ff 00003000 64008b00 ab00b000 a0008000 55002b00 f7ffdbff b2ffb1ff c2ffe2ff 0b002800 41003c00 30000b00 e8ffbdff 96ff86ff 74ff74ff 6bff72ff 7eff9aff daff1900 6500a500 da00f200 f400dc00 af007900 44001700 efffcaff a7ff87ff 77ff71ff 75ff87ff 9bffbbff d8fff4ff fbfff4ff daffc3ff bdffc1ff ddfff4ff 17003d00 5a007800 88009900 a0009f00 9a008500 69003700 0000caff 95ff79ff 66ff5fff 69ff77ff 8affa6ff bdffd5ff e9fffbff 12001f00 2d002900 2b002700 27003000 36004200 46004900 47003800 30002300 1a000b00 f7ffe3ff c8ffc0ff b1ffb4ff b8ffbeff ceffdeff f4ff0000 10001700 19001300 0c000300 fffffdff f7fff9ff fcff0c00 12001500 11001500 11001500 19001200 11000100 f3ffe8ff e2ffe7ff f2ff0700 1c002900 2d002700 20001100 0500fcff f2ffecff e5ffd7ff c7ffbaff b1ffb3ff bcffd7ff f4ff1500 2b003600 3b002e00 2e002600 2b003200 34003600 27001b00 0400f4ff ecffecff faff0100 0b000900 f9ffe9ff d9ffcdff c7ffc3ff c6ffc3ff c3ffc3ff c3ffd2ff eaff0f00 35005600 73007800 75006100 4c003600 23001100 ffffefff e1ffd6ff cbffc7ff c4ffd2ff dbffebff efffedff e5ffd9ff d9ffd1ff daffe3ff eefff8ff 06001100 17002500 32004400 57006300 68005f00 48002c00 0c00edff cdffb8ff a7ff9dff 96ff97ff a0ffb2ff d1fff0ff 0a002100 2e003200 2d001f00 17000c00 0a000f00 15001f00 24002400 25002600 25002400 1f001800 0b00fbff e7ffceff bbffacff a2ffa2ff acffbdff d5fff2ff 0f002400 31003900 3f004200 42004300 42003a00 28001200 ffffefff deffd1ff d1ffd5ff e0ffe6ff effff5ff f7fff5ff f5fff3ff effff0ff ecffedff effff2ff fbff0200 0d001e00 2c003b00 45004a00 42003300 1d000500 efffdbff c6ffb8ff a7ff9fff a1ffafff cefff0ff 1a003e00 5d006b00 69005e00 49002d00 0f00f3ff deffcdff c6ffc3ff c6ffcfff ddfff4ff 09002100 2e002c00 1e000600 e9ffd3ff bfffadff a9ffacff bbffdcff 05003c00 65008700 a100a600 a3008d00 65002c00 f0ffb6ff 82ff5eff 4aff47ff 56ff76ff a5ffd1ff 06003000 51006200 67005c00 47003100 1700ffff e6ffdeff e4fff1ff 0a002600 3b004e00 5b006000 5e004d00 2a00feff cbff97ff 6aff53ff 42ff45ff 59ff79ff aeffeaff 2c006100 84009d00 a300a800 95008200 5d003100 0700e3ff cbffc4ff c4ffc5ff ceffd2ff e5fff4ff 0d001300 0a00f4ff d7ffc3ff b6ffadff adffa7ff b3ffc1ff e9ff1600 3b006000 70007f00 7e008200 7b006700 4b002600 feffdcff bbffadff 9eff94ff 8fff95ff adffc4ff e2ffedff f7ffffff 02000d00 1b002200 1e001800 12001300 16001e00 28003000 38003900 42003a00 36002900 12000100 e8ffd5ff bbffabff 9cff99ff 9bffa2ff b5ffc9ff ddfff3ff 06002100 37004800 59005d00 5c005500 48004000 2b002b00 19001300 0100f0ff eaffd4ff dfffc2ff dfffbeff d1ffb6ff afffb1ff 9fffbdff b3ffe4ff e4ff1800 24004200 63005d00 80007100 83007500 6a005f00 41003100 0500e8ff baff9bff 85ff76ff 76ff7eff 89ff97ff a6ffc1ff d1fff5ff 04002300 32003d00 51004700 56004f00 5b005800 56006200 4b006200 3f003c00 1f000100 e3ffbcff a3ff7dff 76ff62ff 67ff6aff 7bff91ff acffc7ff eaff0300 28004000 68008300 9e00b000 a200a000 7a006700 3f002700 0d00edff d8ffb5ff a4ff93ff 8cff95ff 92ffa4ff b7ffc7ff d8ffedff eefff6ff f7ffebff edfff0ff 0c002000 42005a00 73008500 8c008c00 76006400 45002000 ffffd7ff b4ff97ff 83ff79ff 75ff70ff 7eff94ff b4ffe1ff ffff2b00>

Edit 2:

As @ Vladimir's suggestion, I updated my configAudioEngine() as below:

func configAudioEngine(){
    let mainMixer = audioEngine.outputNode
    audioEngine.attach(audioFilePlayer)
    let outputFormat = mainMixer.outputFormat(forBus: 0)
    //            let outputFormat = mainMixer.outputFormat(forBus: bus)
    audioEngine.connect(audioFilePlayer, to:mainMixer, format: outputFormat)
    audioEngine.prepare()
    do {
        try audioEngine.start()
    } catch _{

        print("error")
    }
    NotificationCenter.default.addObserver(self, selector: #selector(didReceiveData), name: Global.inSocket.notificationAlertMessage, object: nil)
}

This has resolved my exception and I am receiving data. But No sound is coming. It's mute.

来源:https://stackoverflow.com/questions/60453332/avaudiopcmbuffer-able-to-get-audio-with-static-white-noise-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!