TooTallNate / node-lame

Node.js native bindings to libmp3lame & libmpg123
MIT License
567 stars 113 forks source link

Noise #91

Open ithustle opened 5 years ago

ithustle commented 5 years ago

I'm getting noise when I use this lib. My code:

const encoder = new lame.Encoder({ channels: 2, bitDepth: 16, sampleRate: 44100, bitRate: 128, outSampleRate: 22050, mode: lame.STEREO }); const song = fs.createReadStream("./music.mp3"); song.pipe(encoder).pipe(res)

Any help?

LinusU commented 5 years ago

It seems like you are trying to encode mp3 data, treated as raw PCM data, into another mp3. Did you mean to use lame.Decode? 🤔

ithustle commented 5 years ago

Noup, using lame.Encode. Exactly the code that I post here. Any solution?

LinusU commented 5 years ago

You are piping the content of the file "music.mp3" as input to the Encoder. The Encoder takes raw PCM data and turns it into MP3 data. Thus it will treat whatever bytes are in "music.mp3" as raw PCM data, which will be heard as noise.

blubbll commented 5 years ago

me too, i used the following code:

                    var encoder = new lame.Encoder({
                    // input
                    channels: 2, // 2 channels (left and right)
                    bitDepth: 16, // 16-bit samples
                    sampleRate: 44100, // 44,100 Hz sample rate
                    // output
                    bitRate: bitrate,
                    outSampleRate: 44100,
                    mode: lame.STEREO // STEREO (default), JOINTSTEREO, DUALCHANNEL or MONO
                });
                var stream = fs.createReadStream(tmpRaw);
                stream.pipe(encoder);
                encoder.pipe(fs.createWriteStream(pipeOut)); 

where tmpRaw is the path to the default mp3 and pipeOut is the path for the output.

I can't seem to find a working example on how to decode the mp3 to buffer to convert it.

LinusU commented 5 years ago

I can't seem to find a working example on how to decode the mp3 to buffer to convert it.

If you want to decode the MP3 data, you need to use lame.Decoder. Not lame.Encoder.

blubbll commented 5 years ago

Yeah, i figured that out. But i couldn't find the proper syntax for that. I don't normally work with node filestreans, so idk what to pipe where. The examples didn't help and looking for other peoples code on github didn't help either, a lot just used a decoded stream for the node 'speaker' package. I wish it was more documented in examples, like the node-lame package, which i cannot use due to technical limitations. That one has all usecases about decoding documented.

ithustle commented 5 years ago

@LinusU , I want to reduce bitrate on fly while the music is streaming, e.g.: "from a music file with 256kbps to 96kbps". Is it possible you post here an example to do that using node-lame?

blubbll commented 5 years ago

Finished my mp3-converter script by looking into streams and piping:

                    console.log(`${p}Converting raw file to stream for ${size}.0..`);
                    const decoder = new lame.Decoder();
                    fs.createReadStream(tmpRaw).pipe(decoder);//pipe inputfile to decoder, puts out pcm as buffer

                    var encoder = new lame.Encoder({
                        // input
                        channels: 2, // 2 channels (left and right)
                        bitDepth: 16, // 16-bit samples
                        sampleRate: 44100, // 44,100 Hz sample rate

                        // output
                        bitRate: bitrate,
                        outSampleRate: 44100,
                        mode: lame.STEREO // STEREO (default), JOINTSTEREO, DUALCHANNEL or MONO
                    });
                    decoder.pipe(encoder);//pipe decoded stream to encoder
                    const outstream = fs.createWriteStream(`${tmpRaw}_${size}_converting}`); //create outstream to file
                    encoder.pipe(outstream); //write converted stream tro file
                    outstream.on('finish', () => {
                      console.log(`${p}Converted ${size}, sending to pump...`);
                      fs.rename(`${tmpRaw}_${size}_converting}`, pipeOut, (error) => {}); //move converted file to uploadpipe when done
                    });

This will create a read stream from a "raw" file and convert it to a lower bitrate and output the "lower quality" file. @ithustle i believe you can use this script to pipe / use the output stream somewhere else (take a look at 'encoder.pipe'). Depends on what you want to do with the stream (buffer?). Hope this will help.

ithustle commented 5 years ago

@blubbll , awesome. It's working.

ithustle commented 5 years ago

@blubbll , actually is working but stuck after fews seconds song playing ... :(

blubbll commented 5 years ago

I had a similar issue, you may need to wait until the conversion is done (that's why i put "on finish" in my mp3-converter).

ithustle commented 5 years ago

Maybe @TooTallNate could do some kind of magic to solve my problem...