lamejs
lamejs copied to clipboard
lamejs not working with angular 8 -- Uncaught ReferenceError: Lame is not defined
I added "lamejs": "^1.2.0", in package.json , and then in app.component.ts i added these lines :
var lamejs = require("lamejs");
let channels = 1; //1 for mono or 2 for stereo
let sampleRate = 44100; //44.1khz (normal mp3 samplerate)
let kbps = 128; //encode 128kbps mp3
let mp3encoder = new lamejs.Mp3Encoder(channels, sampleRate, kbps);
var mp3Data = [];
let samples = new Int16Array(44100); //one second of silence (get your data from the source you have)
let sampleBlockSize = 1152; //can be anything but make it a multiple of 576 to make encoders life easier
var mp3Data = [];
let sampleChunk;
for (var i = 0; i < samples.length; i += sampleBlockSize) {
sampleChunk = samples.subarray(i, i + sampleBlockSize);
var mp3buf = mp3encoder.encodeBuffer(sampleChunk);
if (mp3buf.length > 0) {
mp3Data.push(mp3buf);
}
}
var mp3buf = mp3encoder.flush(); //finish writing mp3
if (mp3buf.length > 0) {
mp3Data.push(new `Int8Array(mp3buf));`
}
var blob = new Blob(mp3Data, { type: 'audio/mp3' });
var url = window.URL.createObjectURL(blob);
console.log('MP3 URl: ', url);
i am getting this error in console , index.js:17 Uncaught ReferenceError: Lame is not defined at Object../node_modules/lamejs/src/js/index.js
when i open this index.js at that line , i see Lame = require('./Lame.js');
Lame variable is being used without declaration , how do i proceed , i kept fixing these kind of errors of declaration issue but the list just goes on and on .
Any update?
Looks like a module loading/resolution issue...Lame is back on CommonJS and Angular is well into ES Modules. I see Lame is fully included in the webpack bundle, but it isn't resolving for some reason, with the error being Lame = require('./Lame.js');
in lamejs/src/js/index.js when ./Lame.js is indeed right there in that same directory and by rights should resolve. I searched the whole web on this and haven't found anything useful yet other than suggestions to go back to umd or something brutal.
OK here's a kludge that worked for me just now with Angular 13. Using await import
in an IIAFE:
let lamejs: any;
(async () => {
lamejs = await import('lamejs');
})().catch((err) => {
console.error(err);
});
I don't know if this is the best way and you probably want to try rolling back to the more usual import lamejs from 'lamejs';
from time to time to see if this has gotten fixed. But maybe it unsticks you for the moment.