lamejs
Fast mp3 encoder written in JavaScript. On my machine it works 20x faster than realtime (it will encode 132 second long sample in 6.5 seconds) both on node and chrome. lamejs is a rewrite of jump3r-code which is a rewrite of libmp3lame.
Installation
To install via Bower or npm, simply do the following:
$ bower install lamejs --save
$ npm install lamejs
Quick Start
<script src='lame.all.js'></script><script>var mp3Data = ; var mp3encoder = 1 44100 128; //mono 44.1khz encode to 128kbpsvar samples = 44100; //one second of silence replace that with your own samplesvar mp3Tmp = mp3encoder; //encode mp3 //Push encode buffer to mp3Data variablemp3Data; // Get end part of mp3mp3Tmp = mp3encoder; // Write last data to the output data, too// mp3Data contains now the complete mp3Datamp3Data; console;</script>
To use lamejs in Node.js build, you can install it from npm
:
npm install lamejs
Then use it:
var lamejs = require("lamejs");
Real Example
Either see example.html for full example of wav file encoding in browser or use this:
<script src='lame.all.js'></script><script>channels = 1; //1 for mono or 2 for stereosampleRate = 44100; //44.1khz (normal mp3 samplerate)kbps = 128; //encode 128kbps mp3mp3encoder = channels sampleRate kbps;var mp3Data = ; samples = 44100; //one second of silence (get your data from the source you have)sampleBlockSize = 1152; //can be anything but make it a multiple of 576 to make encoders life easier var mp3Data = ;for var i = 0; i < sampleslength; i += sampleBlockSize sampleChunk = samples; var mp3buf = mp3encoder; if mp3buflength > 0 mp3Data; var mp3buf = mp3encoder; //finish writing mp3 if mp3buflength > 0 mp3Data; var blob = mp3Data type: 'audio/mp3';var url = windowURL;console;</script>
Stereo
If you want to encode stereo mp3 use separate sample buffers for left and right channel
<script src='lame.all.js'></script><script>mp3encoder = 2 44100 128;var mp3Data = ; left = 44100; //one second of silence (get your data from the source you have)right = 44100; //one second of silence (get your data from the source you have)sampleBlockSize = 1152; //can be anything but make it a multiple of 576 to make encoders life easier for var i = 0; i < sampleslength; i += sampleBlockSize leftChunk = left; rightChunk = right; var mp3buf = mp3encoder; if mp3buflength > 0 mp3Data; var mp3buf = mp3encoder; //finish writing mp3 if mp3buflength > 0 mp3Data; console;</script>