Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of ogg encoding #40

Closed
Phil-Barber opened this issue Sep 25, 2020 · 4 comments
Closed

Example of ogg encoding #40

Phil-Barber opened this issue Sep 25, 2020 · 4 comments
Labels

Comments

@Phil-Barber
Copy link

Phil-Barber commented Sep 25, 2020

Is your feature request related to a problem? Please describe.
I'm using this library to encode Opus buffers like so:

      const opusEncoder = new OpusEncoder(48000, chunk.numberOfChannels)
      const opusBuffer = opusEncoder.encode(Buffer.from(pcmBuffer))

I want to encapsulate this with ogg in order to send it to the google speech-to-text api (see OGG_OPUS)

I'm finding this process really painful and so far have been unsuccessful, if there are any known examples of what is required I would be incredibly grateful.

Here's what I'm trying atm:
Using node-ogg

const oggEncoder = ogg.Encoder();
const oggStream = oggEncoder.stream();

const audioInputStreamTransform = new Writable({
  write(frame, encoding, next) {
    if (frame) {
        oggStream.write(frame);
      }
    }
    next();
  },
});

voiceStream.pipe(audioInputStreamTransform)
oggEncoder.pipe(google-speech2textStream)

// Neither of these work - nothing appears to be happening
// No data events emitted from either stream
// oggEncoderStream.pipe(google-speech2textStream)

Describe the ideal solution
I appreciate this isn't strictly a problem with this library but given that ogg is the recommended encapsulation I think it would be beneficial to show an example of how to achieve this.

Describe alternatives you've considered
I've also tried to use the ogg-packet library to wrap my buffer in the ogg_packet struct before sending off to oggStream.write. This also leads to no data events being emitted. I'm pretty sure this is the wrong approach given ogg-packet says:

You'll most likely not need to use this module for any practical purposes

but I thought I would try anyway.

What I've tried

          const packet = new ogg_packet();
          packet.packet = frame;
          packet.bytes = frame.length;

          // this will be the first packet in the ogg stream
          packet.b_o_s = 1;
          // there will be more `ogg_packet`s after this one in the ogg stream
          packet.e_o_s = 0;

          // the "packetno" should increment by one for each packet in the ogg stream
          packet.packetno = packetno++;

          // No joy with any of these
          //oggStream.write(ogg.ogg_packet(packet));
          //oggStream.write(packet);
          //oggStream.write(packet.buffer);

Additional context
I'm a real novice when it comes to audio encoding so I'm probably misunderstanding some part of this process - apologies if it's something trivial but I've been doing this for about a week now 😅

If there's a better place to ask for help please feel free to move me along - thanks :)

Also tried something like this example from node-opus with no luck

@Phil-Barber
Copy link
Author

Any chance of any pointers for where to look or where to ask for hints?

@Phil-Barber
Copy link
Author

OK so further digging:

I downloaded node-opus which is documented to work with node-ogg

I have noticed the result of Encoder.encode is not the same between node-opus and @discordjs/opus. It seems node-opus spits out what I believe to be an ogg_packet, and discordjs/opus gives a buffer.

ie.:
opus stream -> discord/opus.decode -> node-opus.encode -> log:

{ packet: <Buffer 4f>,
  bytes: 19,
  b_o_s: 1,
  e_o_s: 0,
  granulepos: -1,
  packetno: 0,
  'ref.buffer':
   <Buffer 18 33 11 04 01 00 00 00 13 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff ff ff ff ff ff ff ff 00 00 00 00 00 00 00 00> }

compared to
opus stream -> discord/opus.decode -> discord/opus.encode -> log:

<Buffer 78 80 64 26 7e d0 2f e8 f5 a5 6d 1c da 41 04 0b 33 d9 ee 3a 0b ee 53 a6 f6 bb cf 55 c8 e3 36 e1 18 4a 9f e9 7f 94 8d a3 0c 96 b3 a1 f7 03 e7 9a 78 db ... >

So that will be my problem. I need to create ogg packets from these buffers is my guess...

I'm curious though why these two opus encoding libraries are so different though unless I've royally messed something up

@Phil-Barber
Copy link
Author

The answer on how to do this is in here and only builds in node 10 I think so I suppose this can be closed
https://github.com/Rantanen/node-opus/blob/master/lib/Encoder.js#L66

Unless there is an easier way you know of to achieve this?

@iCrawl
Copy link
Member

iCrawl commented Apr 16, 2021

Because we cannot depend on node-ogg and we should not either. This library is stripped of all dependencies that come with node-opus usally.

Reason being is that TooTallNate/node-ogg#23
And it doesn't look like it's really maintained, nor do I have the interest in maintaining another native library myself to keep it up to date for node LTS changes.

@iCrawl iCrawl closed this as completed Apr 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants