anthumchris/fetch-stream-audio

Can't decode stream of ogg opus formatted encoded bytes

kboniadi opened this issue · 0 comments

my program is getting stuck on the third call to the decode function and I don't understand why. I'm not using a file, but rather a stream of opus encoded PCM bytes that I format with ogg library. Here is what I have:

This is one packet being passed into the decode function:

79,103,103,83,0,2,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,111,75,149,162,1,143,252,83,140,10,53,54,15,255,22,5,134,58,120,205,49,138,53,47,147,89,211,6,124,209,74,125,139,252,55,153,104,38,75,55,49,243,131,133,10,89,17,151,152,88,149,215,133,1,120,138,16,99,227,80,29,189,40,53,9,0,214,63,216,27,170,57,239,190,140,211,42,124,218,157,214,104,114,45,15,7,136,94,243,179,184,119,200,234,10,70,234,238,7,145,236,231,234,102,79,147,167,95,129,67,206,25,176,219,4,236,255,69,167,253,2,123,223,35,250,109,188,237,152,159,138,2,30,59,169,245,194,213,74,89,192,147,111,245,95,2,191,48,197

This is my index.js:

import { OpusStreamDecoder } from 'opus-stream-decoder';

const decoder = new OpusStreamDecoder({ onDecode });

function onDecode({left, right, samplesDecoded, sampleRate}) {
  console.log(`Decoded ${samplesDecoded} samples`);
  // play back the left/right audio, write to a file, etc
}

function buf2hex(buffer) { // buffer is an ArrayBuffer
  return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join(' ');
}

function main() {
  var mySocketAudio = new WebSocket(
    'ws://random.webaddress:8080', 'audio-only')
  
  mySocketAudio.binaryType = 'arraybuffer'
  
  mySocketAudio.onerror = function () {
    alert("failed to connect")
  };

  mySocketAudio.addEventListener('message', async function(event) {
    // packet length
    // console.log(new Uint8Array(event.data).byteLength)

    // bytes in dec
    //console.log(Array.apply([], new Uint8Array(event.data)).join(","));

    //await decoder.ready;
    //decoder.decode(new Uint8Array(event.data));
    //await decoder.ready
    //decoder.free()

    // bytes in hex
    // var buffer = new Buffer.from(new Uint8Array(event.data));
    // console.log(buf2hex(buffer))

    try {
      decoder.ready.then(_ => decoder.decode(new Uint8Array(event.data)));
    } catch (e) {
      decoder.ready.then(_ => decoder.free());
    }

    // free up the decoder's memory in WebAssembly (also resets decoder for reuse)
    decoder.ready.then(_ => decoder.free());
  })
}

main()

Also if I don't call decoder.free() on every single perceived frame then onDecode never gets called. When calling free() as shown, samplesDecoded prints out -131.