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

mp3 is half tempo and bad quality #27

Closed
sevenuz opened this issue Jun 20, 2016 · 6 comments
Closed

mp3 is half tempo and bad quality #27

sevenuz opened this issue Jun 20, 2016 · 6 comments

Comments

@sevenuz
Copy link

sevenuz commented Jun 20, 2016

First of all thank you for that great lib!

I want to convert a 1411 kbps pcm_s16le 44100 Hz stereo, s16(recorded with recordRTC) to mp3.
The problem is that the wav file sounds perfect but the converted mp3 file sounds bad, sounds lower than the original and is twice as long.

Full js: http://julius.athenstaedt.net/rep/rcx2/background.js
recorded wav: http://julius.athenstaedt.net/rep/wav%20(16).wav
converted mp3 http://julius.athenstaedt.net/rep/mpeg%20(14).mp3


function convertToMP3(audioBlob) {
    //audioBlob is the recorded Blob from recordRTC api
    //with FileReader get the dataform like with XMLHttpRequest (maybe the problem?)
    console.log(audioBlob);
    var audioData;
    fileReader = fileReader || new FileReader();
    fileReader.onload = function() {
        audioData = this.result;

                var wav = lamejs.WavHeader.readHeader(new DataView(audioData));
                console.log("wav:",wav);
                mp3encoder = new lamejs.Mp3Encoder(wav.channels, wav.sampleRate, 128);
                var mp3Data = [];

                left = new Int16Array(audioData, wav.dataOffset, wav.dataLen /2); 
                right = new Int16Array(audioData, wav.dataOffset, wav.dataLen /2); 

                sampleBlockSize = 1152; 

                var mp3buf=[];
                for (var i = 0; i < left.length; i += sampleBlockSize) {
                    leftChunk = left.subarray(i, i + sampleBlockSize);
                    rightChunk = right.subarray(i, i + sampleBlockSize);
                    mp3buf = mp3encoder.encodeBuffer(leftChunk, rightChunk);
                    if (mp3buf.length > 0) {
                        mp3Data.push(new Int8Array(mp3buf));
                    }
                }
                mp3buf = mp3encoder.flush(); //finish writing mp3

                if (mp3buf.length > 0) {
                    mp3Data.push(new Int8Array(mp3buf));
                }

                var blob = new Blob(mp3Data, {
                    type: 'audio/mp3'
                });
                var url = window.URL.createObjectURL(blob);
        saveFile(url, "mpeg.mp3");
    };
    fileReader.readAsArrayBuffer(audioBlob);
}

Thank You for help!!
(sorry for bad english)

@zhuker
Copy link
Owner

zhuker commented Jun 20, 2016

Looks like you encode stereo as mono

Sent from my iPhone

On Jun 21, 2016, at 00:56, Julius Athenstaedt notifications@github.com wrote:

First of all thank you for that great lib!

I want to convert a 1411 kbps pcm_s16le 44100 Hz stereo, s16(recorded with recordRTC) to mp3.
The problem is that the wav file sounds perfect but the converted mp3 file sounds bad, sounds lower than the original and is twice as long.

Full js: http://julius.athenstaedt.net/rep/rcx2/background.js
recorded wav: http://julius.athenstaedt.net/rep/wav%20(16).wav
converted mp3 http://julius.athenstaedt.net/rep/mpeg%20(14).mp3

function convertToMP3(audioBlob) {
//audioBlob is the recorded Blob from recordRTC api
//with FileReader get the dataform like with XMLHttpRequest (maybe the problem?)
console.log(audioBlob);
var audioData;
fileReader = fileReader || new FileReader();
fileReader.onload = function() {
audioData = this.result;

            var wav = lamejs.WavHeader.readHeader(new DataView(audioData));
            console.log("wav:",wav);
            mp3encoder = new lamejs.Mp3Encoder(wav.channels, wav.sampleRate, 128);
            var mp3Data = [];

            left = new Int16Array(audioData, wav.dataOffset, wav.dataLen /2); 
            right = new Int16Array(audioData, wav.dataOffset, wav.dataLen /2); 

            sampleBlockSize = 1152; 

            var mp3buf=[];
            for (var i = 0; i < left.length; i += sampleBlockSize) {
                leftChunk = left.subarray(i, i + sampleBlockSize);
                rightChunk = right.subarray(i, i + sampleBlockSize);
                mp3buf = mp3encoder.encodeBuffer(leftChunk, rightChunk);
                if (mp3buf.length > 0) {
                    mp3Data.push(new Int8Array(mp3buf));
                }
            }
            mp3buf = mp3encoder.flush(); //finish writing mp3

            if (mp3buf.length > 0) {
                mp3Data.push(new Int8Array(mp3buf));
            }

            var blob = new Blob(mp3Data, {
                type: 'audio/mp3'
            });
            var url = window.URL.createObjectURL(blob);
    saveFile(url, "mpeg.mp3");
};
fileReader.readAsArrayBuffer(audioBlob);

}
Thank You for help!!
(sorry for bad english)


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

@sevenuz
Copy link
Author

sevenuz commented Jun 24, 2016

And what I have to change? I have used the code for stereo from the example.

@zhuker
Copy link
Owner

zhuker commented Jun 24, 2016

Did you split your stereo input into left and right channels?

Sent from my iPhone

On Jun 24, 2016, at 18:34, Julius Athenstaedt notifications@github.com wrote:

And what I have to change? I have used the code for stereo from the example.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

@sevenuz
Copy link
Author

sevenuz commented Jun 24, 2016

Yes like in the example, but their was only one second silence. So maybe I splitted wrong:

left = new Int16Array(audioData, wav.dataOffset, wav.dataLen /2); 
right = new Int16Array(audioData, wav.dataOffset, wav.dataLen /2); 

@zhuker
Copy link
Owner

zhuker commented Jun 24, 2016

Yes this is wrong.
Split by taking every other sample into separate channel.
For i=0 i<data.length i+=2
Left= data [i]
Right = data[i+1]

And then put left and right samples into their own int16 arrays

Sent from my iPhone

On Jun 24, 2016, at 19:18, Julius Athenstaedt notifications@github.com wrote:

Yes like in the example, but their was only one second silence. So maybe I splitted wrong:

left = new Int16Array(audioData, wav.dataOffset, wav.dataLen /2);
right = new Int16Array(audioData, wav.dataOffset, wav.dataLen /2);

You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

@sevenuz
Copy link
Author

sevenuz commented Jun 26, 2016

Thank you that was the error.
correctly splitted:

//audioData is the wav-blob converted to an ArrayBuffer
var data = new Int16Array(audioData, wav.dataOffset, wav.dataLen/2);

 for (i = 0; i < data.length; i += 2) {
         left.push(data[i]);
         right.push(data[i + 1]);
}

left = new Int16Array(left);
right = new Int16Array(right);

@sevenuz sevenuz closed this as completed Jun 26, 2016
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

2 participants