lamejs not working with angular 8 -- Uncaught ReferenceError: Lame is not defined

1.3k views Asked by At

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 .

1

There are 1 answers

1
Arun Girivasan On

Download lame.all.js and lame.min.js files from https://github.com/zhuker/lamejs and place them into your assets folder.

src/assets/lamejs/lame.all.js
src/assets/lamejs/lame.min.js

Add a script tag to your index.html file

<script src='assets/lamejs/lame.all.js'></script>

Decalare a variable of lamejs after all imports in component.ts or service.ts then you can use it.

declare var lamejs;