Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

[Question] How do you use this? #9

Closed
izy521 opened this issue Oct 25, 2015 · 4 comments
Closed

[Question] How do you use this? #9

izy521 opened this issue Oct 25, 2015 · 4 comments

Comments

@izy521
Copy link

izy521 commented Oct 25, 2015

I'm sorry for appearing inexperienced, I have no clue what to do with all these pipes and streams.

Right now, I'm trying to see if I can take data from the 'lame' library and have it encode and output with this 'opus' library. However, I'm running into issue after issue and I'm sure it's just because I don't understand how this works. I've looked at the example provided and it looks like I need to get a buffer from somewhere, I've also looked at the examples folder and none of them really help.

var mp3File = fs.createReadStream('moon.mp3');
var opusFile = fs.createWriteStream('test.opus');

var mp3Decoder = new Lame.Decoder();
mp3Decoder.on('format', function(format) {
    var rate = 48000;
    var channels = 2;
    var frame_size = rate / 100;

    var opusEncoder = new Opus.OpusEncoder( rate );
    var encoded = opusEncoder.encode( format, frame_size );
});

mp3File.pipe(mp3Decoder);

Throws a C++ error:

node: ../src/node_buffer.cc:103: char* node::Buffer::Data(v8::Handle<v8::Object>): Assertion `obj->HasIndexedPropertiesInExternalArrayData()' failed.
Aborted

I assume because the format isn't an array.

var mp3File = fs.createReadStream('moon.mp3');
var opusFile = fs.createWriteStream('test.opus');

var mp3Decoder = new Lame.Decoder();
mp3Decoder.on('format', function(format) {
    var rate = 48000;
    var channels = 2;
    var frame_size = rate / 100;
    var opusEncodeStream = new Opus.Encoder(rate, channels, frame_size);

    mp3Decoder.pipe(opusEncodeStream).pipe(opusFile);
});

mp3File.pipe(mp3Decoder);

Throws this

events.js:85
      throw er; // Unhandled 'error' event
            ^
TypeError: Invalid non-string/buffer chunk

I guess that means I did that incorrectly.

var mp3File = fs.createReadStream('moon.mp3');
var opusFile = fs.createWriteStream('test.opus');

var mp3Decoder = new Lame.Decoder();
mp3Decoder.on('format', function(format) {
    var rate = 48000;
    var channels = 2;
    var frame_size = rate / 100;

    var opusEncoder = new Opus.OpusEncoder( rate );

    mp3Decoder.pipe(opusEncoder).pipe(opusFile);
});

mp3File.pipe(mp3Decoder);

Throws an undefined error

js:516
  dest.on('unpipe', onunpipe);
       ^
TypeError: undefined is not a function

I have tons of other failed examples, but I figure instead of posting them all, maybe someone who's done this can help me.

@Rantanen
Copy link
Owner

Not really sure of what you're trying to do. The following code might be helpful. Note that opus doesn't define a binary encoding for the stream so trying to write it to a file as such is probably not something you want.

"use strict";

var lame = require( 'lame' );
var opus = require( 'opus' );

var mp3Decoder = new lame.Decoder();
var opusEncoder = new opus.Encoder( 48000, 2 );

var output = process.stdin.pipe( mp3Decoder ).pipe( opusEncoder );

output.on( 'data', function( packet ) {
    console.log( console.log( packet ) );
});

@izy521
Copy link
Author

izy521 commented Oct 25, 2015

Ah, well right now I'm attempting to package opus data into an RTP packet, but I'm just trying to get the data from an mp3 file to the opus encoder first, and oh my god thank you. I think this is exactly what I needed.

@Rantanen
Copy link
Owner

Ah, good. Yes. For that the above code should work. The audio data for each opus frame can be read from the output-data packets.

@izy521
Copy link
Author

izy521 commented Oct 25, 2015

I'm sorry, it all seems to be working now, but I get very distorted audio on the server. I'm told I'm supposed to be using 20ms frames and I think that might be the issue because I can't find anywhere that I can define that. I tried typing 20 within the frame_size argument area, but apparently that's meant for something different? Do I handle this on my part or is there something I'm missing?

Edit: Nevermind, corrupted packets, bad coding on my part.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants