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

Noise #91

Open
ithustle opened this issue Jan 24, 2019 · 12 comments
Open

Noise #91

ithustle opened this issue Jan 24, 2019 · 12 comments

Comments

@ithustle
Copy link

ithustle commented Jan 24, 2019

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
Copy link
Collaborator

LinusU commented Jan 29, 2019

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
Copy link
Author

ithustle commented Jan 29, 2019

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

@LinusU
Copy link
Collaborator

LinusU commented Jan 30, 2019

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
Copy link

blubbll commented Feb 10, 2019

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
Copy link
Collaborator

LinusU commented Feb 11, 2019

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
Copy link

blubbll commented Feb 11, 2019

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
Copy link
Author

@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
Copy link

blubbll commented Feb 17, 2019

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
Copy link
Author

@blubbll , awesome. It's working.

@ithustle ithustle reopened this Feb 18, 2019
@ithustle
Copy link
Author

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

@blubbll
Copy link

blubbll commented Feb 18, 2019

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
Copy link
Author

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

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

No branches or pull requests

3 participants