How to play received raw PCM Audio coming through Web Socket in the browser

986 Views Asked by At

Having a spring-boot project which has to stream audio coming from a SIP client connected to asterisk with codec G711 ALAW.

I am able to get and send the pcm data through a UDP connection between the sip client and spring-boot project.

The goal is to send this packets through a web socket to the client and play those audio bytes in the browser.

In the browser I hear the audio with a lot of noise using this player to play pcm packets I receive from the web socket with this parameters:

<script>
 var player = new PCMPlayer({
   encoding: '16bitFloat',
   channels: 1,
   sampleRate: 8000,
   flushingTime: 1000
});

function StartSession() {
  ws = new WebSocket(socketURL);
  ws.binaryType = 'arraybuffer';
  ws.addEventListener('message', function (event) {
    player.feed(new Int16Array(event.data));
  });
}

function CloseSession() {
  if (ws != null) {
    ws.close();
  }
}
</script>
0

There are 0 best solutions below