2

I'm Developing an iOS application. In that I'm receiving raw data stream from BLE

I'm using AVAudioEngine with buffers

let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 8000.0, channels: 1, interleaved: false)!

I have sample rate 8000 and linear PCM format

Initialising AVM Audio Engine using below code.

init() {
    self.audioSession = AVCaptureSession()
    self.engine = AVAudioEngine()
    self.playerNode = AVAudioPlayerNode()
    self.engine.attach(self.playerNode)
    engine.connect(self.playerNode,
            to: engine.outputNode,
            format:AVAudioFormat.init(standardFormatWithSampleRate: 8000, channels: 1))
    engine.prepare()
    try! engine.start()
    self.playerNode.play()
  }

& playing using buffers, using below code

func play(buffer: [Int16]) {
    let interleavedChannelCount = 1
    let frameLength = buffer.count / interleavedChannelCount
    let audioBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(frameLength))!
    // buffer contains 2 channel interleaved data
    // audioBuffer contains 2 channel interleaved data
    var buf = buffer
    memcpy(audioBuffer.mutableAudioBufferList.pointee.mBuffers.mData, &buf, MemoryLayout<Int16>.stride * interleavedChannelCount * frameLength)
    audioBuffer.frameLength = AVAudioFrameCount(frameLength)
    self.playerNode.scheduleBuffer(audioBuffer) {
      print("Played")
    }
  }

& filling buffer like this,

let i16array = decoded.withUnsafeBytes {
            UnsafeBufferPointer<Int16>(start: $0, count: decoded.count).map(Int16.init(littleEndian:))
          }
          voicePlayer.play(buffer: i16array)

I'm getting crackling noise and not getting proper output.

I'm not finding any easy solution then this. Please help me in correcting this.

Or else I am using below tutorial for playing raw audio, this approach gives me sound, but it gives me crackling noise. https://atastypixel.com/using-remoteio-audio-unit/

Which ius better way. May afterwords i need to add filter also. Please help any suggestions/code.

Arpit B Parekh
  • 1,932
  • 5
  • 36
  • 57
Dhruvi
  • 21
  • 3

0 Answers0