Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert ArrayBuffer to AudioBuffer?

I am streaming an arrayBuffer to convert to an audioBuffer in order to be able to listen to it.

I am receiving a stream via a websocket event

retrieveAudioStream(){
  this.socket.on('stream', (buffer) => {
    console.log('buffer', buffer)
  })
}

the buffer is an arrayBuffer and I need it to be an audioBuffer in order to be able to listen to it on my application.

How can I do this?

like image 647
Stretch0 Avatar asked May 24 '18 14:05

Stretch0


2 Answers

You can use BaseAudioContext.createBuffer() method. It is used to

create a new, empty AudioBuffer object, which can then be populated by data, and played via an AudioBufferSourceNode

See MDN for more info: https://developer.mozilla.org/en-US/docs/Web/API/BaseAudioContext/createBuffer

like image 52
Orkhan Huseynli Avatar answered Oct 12 '22 21:10

Orkhan Huseynli


Since you're streaming media rather than downloading the file and then decoding the audio data, AudioContext.createMediaStreamSource() will be much better suited for your usecase.

Read more here https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createMediaStreamSource

like image 34
Hemant Parashar Avatar answered Oct 12 '22 22:10

Hemant Parashar