Git Product home page Git Product logo

lame.js's Introduction

Lame.js

MP3 encoding in JavaScript

Lame.js is an MP3 encoder that runs entirely in the web browser. It is built on an MP3 encoder written in C (Lame) converted into JavaScript using Emscripten.

A compiled version of lame.js is available in the dist folder.

1. Compilation of Lame.js

You will need:

  • emscripten (which implies also node.js and LLVM-fastcomp compiler, see emscripten docs for instructions on how to get it),
  • CMake.

The build is a classic CMake cross-compilation, using the toolchain provided by emscripten:

$ cd .../lame.js # This folder
$ mkdir build
$ cd build
$ cmake -DEMSCRIPTEN=1 -DCMAKE_TOOLCHAIN_FILE=path_to_emscripten/cmake/Modules/Platform/Emscripten.cmake ..
$ make

This generates lame.js.

2. Usage of Lame.js

The API is rather simple:

  • 3 objects must be created: an encoder, an input buffer, an output buffer.
  • To start a new file, call Start.
  • To process samples, use Encode, you provide input raw PCM samples, and receive encoded MP3 frames.
  • To stop the file, call Stop, which will also give you the final remaining frames.
  • To release the memory used by the structures, you must explicitely call delete() on them.

For instance:

var encoder = new Module.Encoder();
encoder.Start();

var inBuffer = new Module.AudioBuffer();
var outBuffer = new Module.MP3Buffer();
var encodedData = [];
/* ... */
inBuffer.push_back(sample) // Must be 16kHz, 16 bits audio sample

/* Looping for every recorded samples, putting them in an array 
that will contain the MP3 file */
outBuffer.clear();
encoder.Encode(inBuffer, outBuffer);
for (var i = 0; i < outBuffer.size(); i++) {
    encodedData.push(outBuffer.get(i));
}
outBuffer.clear();
encoder.Stop(outBuffer);
for (var i = 0; i < outBuffer.size(); i++) {
    encodedData.push(outBuffer.get(i));
}
/* ... */
// We finally create a binary blob that contains the MP3 file
var audioBlob = new Blob([new Uint8Array(encodedData)], { type: 'audio/mp3' });
outBuffer.delete();
inBuffer.delete();
encoder.delete();

3. Test suite

There is a test suite in the tests folder. Since it used lame.js in the dist folder, you can start it from the root folder of this repository:

$ cd .../lame.js
$ python -m SimpleHTTPServer 8080

Then open http://localhost:8080/tests/index.html in your browser.

lame.js's People

Contributors

syl22-00 avatar

Watchers

Sumanta Sarkar avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.