You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.5 KiB
JavaScript

2 years ago
importScripts('libmp3lame.min.js');
var mp3codec;
this.addEventListener('message', function(e) {
switch (e.data.cmd) {
case 'init':
if (!e.data.config) {
e.data.config = { };
}
mp3codec = Lame.init();
Lame.set_mode(mp3codec, e.data.config.mode || Lame.JOINT_STEREO);
Lame.set_num_channels(mp3codec, e.data.config.channels || 2);
Lame.set_num_samples(mp3codec, e.data.config.samples || -1);
Lame.set_in_samplerate(mp3codec, e.data.config.samplerate || 44100);
Lame.set_out_samplerate(mp3codec, e.data.config.samplerate || 44100);
Lame.set_bitrate(mp3codec, e.data.config.bitrate || 128);
Lame.init_params(mp3codec);
// console.log('Version :', Lame.get_version() + ' / ',
// 'Mode: '+Lame.get_mode(mp3codec) + ' / ',
// 'Samples: '+Lame.get_num_samples(mp3codec) + ' / ',
// 'Channels: '+Lame.get_num_channels(mp3codec) + ' / ',
// 'Input Samplate: '+ Lame.get_in_samplerate(mp3codec) + ' / ',
// 'Output Samplate: '+ Lame.get_in_samplerate(mp3codec) + ' / ',
// 'Bitlate :' +Lame.get_bitrate(mp3codec) + ' / ');
// 'VBR :' + Lame.get_VBR(mp3codec));
break;
case 'encode':
var mp3data = Lame.encode_buffer_ieee_float(mp3codec, e.data.buf, e.data.buf);
self.postMessage({cmd: 'data', buffer: mp3data.data});
break;
case 'finish':
var mp3data = Lame.encode_flush(mp3codec);
self.postMessage({cmd: 'end', buffer: mp3data.data});
Lame.close(mp3codec);
mp3codec = null;
break;
}
});