1.0.4 • Published 2 years ago

audio-encoder v1.0.4

Weekly downloads
125
License
MIT
Repository
github
Last release
2 years ago

audioEncoder

Encode audioBuffer to wave or mp3 from the browser, using an unified API

Install with NPM

USAGE:

Import the library as script: (Copy the dist/audioEncoder.js file in your project)

<script src="audioEncoder.js" type="text/javascript"></script>

Or require as a module:

var audioEncoder = require('audio-encoder');

Encode an AudioBuffer instance:

audioEncoder(audioBuffer, encoding, onProgress, onComplete);

encoding is either:

  • 'WAV' or 0 or null -> will encode to a wave file
  • any valid mp3 bitrate: 320, 256, 192, 128, 96, 64, 56, 48, 40, 32

onComplete gets called with a Blob instance as result.

NOTE:

Bitrate lower that 96 will force audio to be MONO. On top of that, the sample rate will be degraded according to the following table:

bitratesample rateforce to mono
96 kBps and higher44.1 kHzfalse
64 kBps44.1 kHztrue
56 kBps32 kHztrue
48 kBps32 kHztrue
40 kBps24 kHztrue
32 kBps22 kHztrue

Example

const audioEncoder = require('audio-encoder');
const fileSaver    = require('file-saver');

// create audioBuffer
const audioContext = new AudioContext();
const length = 44100; // one second @ 44.1KHz
const audioBuffer = audioContext.createBuffer(1, length, 44100);
const channelData = audioBuffer.getChannelData(0);

// fill some audio
for (let i = 0; i < length; i++) {
	channelData[i] = Math.sin(i * 0.03);
}

// convert as mp3 and save file using file-saver
audioEncoder(audioBuffer, 128, null, function onComplete(blob) {
	fileSaver.saveAs(blob, 'sound.mp3');
});
1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago