-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.js
66 lines (41 loc) · 1.63 KB
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
importScripts('https://cdn.jsdelivr.net/npm/lamejs@1.2.0/lame.min.js');
onmessage = function (event) {
const myArray = event.data;
if(event.data.data2 != null){
const array = myArray.data1;
const array2 = myArray.data2
const mp3Data = [];
const samplesLeft = new Int16Array(array.length);
const samplesRight = new Int16Array(array.length);
for (let i = 0; i < array.length; i++) {
// Split the stereo audio data into left and right channels
samplesLeft[i] = array[i] * 0x7FFF;
samplesRight[i] = array2[i] * 0x7FFF;
}
// Create a Lame encoder with 2 channels (stereo) and the desired bitrate
const mp3encoder = new lamejs.Mp3Encoder(2, myArray.samplerate, 128);
// Encode the left and right channels separately
const mp3TmpLeft = mp3encoder.encodeBuffer(samplesLeft, samplesRight);
mp3encoder.flush();
// Push the encoded data for both channels into the mp3Data array
mp3Data.push(mp3TmpLeft);
postMessage(mp3Data);
}
else{
const array = myArray.data1;
const mp3Data = [];
const samplesLeft = new Int16Array(array.length);
for (let i = 0; i < array.length; i++) {
// Split the stereo audio data into left and right channels
samplesLeft[i] = array[i] * 0x7FFF;
}
// Create a Lame encoder with 2 channels (stereo) and the desired bitrate
const mp3encoder = new lamejs.Mp3Encoder(1, myArray.samplerate, 128);
// Encode the left and right channels separately
const mp3TmpLeft = mp3encoder.encodeBuffer(samplesLeft);
mp3encoder.flush();
// Push the encoded data for both channels into the mp3Data array
mp3Data.push(mp3TmpLeft);
postMessage(mp3Data);
}
}