12

I am trying to convert a WAV file to Opus, using Node's fs.readFile and passing that buffer to @Discord/opus converter. I get neither the result or a error thrown with a explanation of what is wrong. This is basically the example you have in the docs... right?

Bug? By design? or me missing something? I would expect the try/catch to be called but somehow the error is "absorbed" and the script just exits silently.

Online example: https://codesandbox.io/s/funny-jones-d7sls?file=/src/index.js

const audioBuffer = await fs
    .readFile(file)
    .catch(err => console.log("Error reading input file:", err));

  console.log("LENGTH:", audioBuffer.length); // all good so far...
  const encoder = new OpusEncoder(41000, 2);

  let encoded = null;
  try {
    console.log("Trying to encode..."); // log runs
    encoded = encoder.encode(audioBuffer);
    console.log("Encoded!"); // log doesn't run :(
  } catch (err) {
    console.log("Encoding failed:", err); // no error thrown... :'(
  }
  return encoded;

Further details:

  • @discordjs/opus version: ^0.3.2
  • Node.js version: v14.2.0
  • Operating system: Mac Mojave (10.14.6)

Related issue in repo: https://github.com/discordjs/opus/issues/26

Rikard
  • 7,485
  • 11
  • 55
  • 92
  • wild guess is there is a buffer type mismatch .. the async read op is returing a node type incompat with the api signature for the encode op. For example , verify that one or the other is NOT expecting an Array of buffers . – Robert Rowntree Jul 01 '20 at 17:31
  • @RobertRowntree maybe... don't know how to check that though. Node's `fs` gives a Buffer, not sure what kinf of buffer Opus expects :/ – Rikard Jul 01 '20 at 18:22
  • suggest a review of https://stackoverflow.com/questions/43769047/node-fs-readfilesync-returns-a-uint8-array-instead-of-raw-buffer-array try " .encode([audioBuffer]) just to verify that it does not resolve???? – Robert Rowntree Jul 01 '20 at 18:36
  • @RobertRowntree no change, doing `.encode(new Uint16Array(audioBuffer.buffer))` or passing the `.buffer` property both break `../src/node_buffer.cc:221:char *node::Buffer::Data(Local): Assertion 'val->IsArrayBufferView()' failed.` – Rikard Jul 01 '20 at 19:04
  • i dont want to confuse the issue. i've recently dev'd in node.opus using a lib rather than native node for encode. If u wish to take some more time investigate the buffer issue then u can start with : https://github.com/chris-rudmin/opus-recorder/blob/master/src/encoderWorker.js#L13 . this is the opus encoding i used and it also gets into handle on the mic buffer passed to the opus.encoder. NOTE distinction between "e.data" and "e.data.buffers".. IMO node can be really opaque in this area to devs who are not total expert in node buffering. – Robert Rowntree Jul 02 '20 at 13:27
  • @RobertRowntree agree Node is opaque in some areas, this being one of them. Thanks for the repo pointer. I see its for the browser/Worker so I guess the problem there is not really the same, although its JavaScript in both Node and Browser. – Rikard Jul 02 '20 at 13:49
  • make sure you are using the correct reference on the .encode call and that you are not passing "nulls" or "undefined" or a shell ref to an array ( enum of array elements )to the encoder when it wants the actuall data.buffers. – Robert Rowntree Jul 02 '20 at 14:39
  • 1
    node ffmpeg is a straight forward way to convert wav to opus. u could look into "fluent" or some other node port of ffmpeg – Robert Rowntree Jul 02 '20 at 17:01
  • fs.readFile() reads the file into a buffer, as raw data. That is, you get an array of bytes with the image of a .wav file in RIFF format, chunks and all (see https://en.wikipedia.org/wiki/WAV). It does not do the work of de-chunking the RIFF data into raw samples. opus.encode wants a byte-array representing raw samples at the rate you specify when you call it. It assumes you have unpacked the samples for it. It's not failing because you're giving it binary data. It thinks it's encoding raw audio samples. I concur with @RobertRowntree. You need a smart wav-reader object. ffmpeg is your friend. – cycollins Jul 10 '20 at 02:42

0 Answers0