Skip to content

Encode audioBuffer to wave or mp3 using an unified API

License

Notifications You must be signed in to change notification settings

cstoquer/audio-encoder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

bitrate sample rate force to mono
96 kBps and higher 44.1 kHz false
64 kBps 44.1 kHz true
56 kBps 32 kHz true
48 kBps 32 kHz true
40 kBps 24 kHz true
32 kBps 22 kHz true

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');
});

About

Encode audioBuffer to wave or mp3 using an unified API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published