@mediabunny/mp3-encoder 1.17.0 → 1.17.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundles/mediabunny-mp3-encoder.js +165 -37
- package/dist/bundles/mediabunny-mp3-encoder.min.js +1 -1
- package/dist/bundles/mediabunny-mp3-encoder.min.mjs +1 -1
- package/dist/bundles/mediabunny-mp3-encoder.mjs +165 -37
- package/dist/modules/src/index.d.ts.map +1 -1
- package/dist/modules/src/index.js +1 -1
- package/package.json +1 -1
- package/src/index.ts +3 -1
|
@@ -60,35 +60,156 @@ var MediabunnyMp3Encoder = (() => {
|
|
|
60
60
|
|
|
61
61
|
// shared/mp3-misc.ts
|
|
62
62
|
var FRAME_HEADER_SIZE = 4;
|
|
63
|
-
var
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
//
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
63
|
+
var SAMPLING_RATES = [44100, 48e3, 32e3];
|
|
64
|
+
var KILOBIT_RATES = [
|
|
65
|
+
// lowSamplingFrequency === 0
|
|
66
|
+
-1,
|
|
67
|
+
-1,
|
|
68
|
+
-1,
|
|
69
|
+
-1,
|
|
70
|
+
-1,
|
|
71
|
+
-1,
|
|
72
|
+
-1,
|
|
73
|
+
-1,
|
|
74
|
+
-1,
|
|
75
|
+
-1,
|
|
76
|
+
-1,
|
|
77
|
+
-1,
|
|
78
|
+
-1,
|
|
79
|
+
-1,
|
|
80
|
+
-1,
|
|
81
|
+
-1,
|
|
82
|
+
// layer = 0
|
|
83
|
+
-1,
|
|
84
|
+
32,
|
|
85
|
+
40,
|
|
86
|
+
48,
|
|
87
|
+
56,
|
|
88
|
+
64,
|
|
89
|
+
80,
|
|
90
|
+
96,
|
|
91
|
+
112,
|
|
92
|
+
128,
|
|
93
|
+
160,
|
|
94
|
+
192,
|
|
95
|
+
224,
|
|
96
|
+
256,
|
|
97
|
+
320,
|
|
98
|
+
-1,
|
|
99
|
+
// layer 1
|
|
100
|
+
-1,
|
|
101
|
+
32,
|
|
102
|
+
48,
|
|
103
|
+
56,
|
|
104
|
+
64,
|
|
105
|
+
80,
|
|
106
|
+
96,
|
|
107
|
+
112,
|
|
108
|
+
128,
|
|
109
|
+
160,
|
|
110
|
+
192,
|
|
111
|
+
224,
|
|
112
|
+
256,
|
|
113
|
+
320,
|
|
114
|
+
384,
|
|
115
|
+
-1,
|
|
116
|
+
// layer = 2
|
|
117
|
+
-1,
|
|
118
|
+
32,
|
|
119
|
+
64,
|
|
120
|
+
96,
|
|
121
|
+
128,
|
|
122
|
+
160,
|
|
123
|
+
192,
|
|
124
|
+
224,
|
|
125
|
+
256,
|
|
126
|
+
288,
|
|
127
|
+
320,
|
|
128
|
+
352,
|
|
129
|
+
384,
|
|
130
|
+
416,
|
|
131
|
+
448,
|
|
132
|
+
-1,
|
|
133
|
+
// layer = 3
|
|
134
|
+
// lowSamplingFrequency === 1
|
|
135
|
+
-1,
|
|
136
|
+
-1,
|
|
137
|
+
-1,
|
|
138
|
+
-1,
|
|
139
|
+
-1,
|
|
140
|
+
-1,
|
|
141
|
+
-1,
|
|
142
|
+
-1,
|
|
143
|
+
-1,
|
|
144
|
+
-1,
|
|
145
|
+
-1,
|
|
146
|
+
-1,
|
|
147
|
+
-1,
|
|
148
|
+
-1,
|
|
149
|
+
-1,
|
|
150
|
+
-1,
|
|
151
|
+
// layer = 0
|
|
152
|
+
-1,
|
|
153
|
+
8,
|
|
154
|
+
16,
|
|
155
|
+
24,
|
|
156
|
+
32,
|
|
157
|
+
40,
|
|
158
|
+
48,
|
|
159
|
+
56,
|
|
160
|
+
64,
|
|
161
|
+
80,
|
|
162
|
+
96,
|
|
163
|
+
112,
|
|
164
|
+
128,
|
|
165
|
+
144,
|
|
166
|
+
160,
|
|
167
|
+
-1,
|
|
168
|
+
// layer = 1
|
|
169
|
+
-1,
|
|
170
|
+
8,
|
|
171
|
+
16,
|
|
172
|
+
24,
|
|
173
|
+
32,
|
|
174
|
+
40,
|
|
175
|
+
48,
|
|
176
|
+
56,
|
|
177
|
+
64,
|
|
178
|
+
80,
|
|
179
|
+
96,
|
|
180
|
+
112,
|
|
181
|
+
128,
|
|
182
|
+
144,
|
|
183
|
+
160,
|
|
184
|
+
-1,
|
|
185
|
+
// layer = 2
|
|
186
|
+
-1,
|
|
187
|
+
32,
|
|
188
|
+
48,
|
|
189
|
+
56,
|
|
190
|
+
64,
|
|
191
|
+
80,
|
|
192
|
+
96,
|
|
193
|
+
112,
|
|
194
|
+
128,
|
|
195
|
+
144,
|
|
196
|
+
160,
|
|
197
|
+
176,
|
|
198
|
+
192,
|
|
199
|
+
224,
|
|
200
|
+
256,
|
|
201
|
+
-1
|
|
202
|
+
// layer = 3
|
|
203
|
+
];
|
|
204
|
+
var computeMp3FrameSize = (lowSamplingFrequency, layer, bitrate, sampleRate, padding) => {
|
|
205
|
+
if (layer === 0) {
|
|
206
|
+
return 0;
|
|
207
|
+
} else if (layer === 1) {
|
|
208
|
+
return Math.round(144 * bitrate / (sampleRate << lowSamplingFrequency)) + padding;
|
|
209
|
+
} else if (layer === 2) {
|
|
210
|
+
return Math.round(144 * bitrate / sampleRate) + padding;
|
|
90
211
|
} else {
|
|
91
|
-
return Math.
|
|
212
|
+
return (Math.round(12 * bitrate / sampleRate) + padding) * 4;
|
|
92
213
|
}
|
|
93
214
|
};
|
|
94
215
|
var readFrameHeader = (word, remainingBytes) => {
|
|
@@ -108,26 +229,31 @@ var MediabunnyMp3Encoder = (() => {
|
|
|
108
229
|
if ((secondByte & 224) !== 224) {
|
|
109
230
|
return { header: null, bytesAdvanced: 1 };
|
|
110
231
|
}
|
|
232
|
+
let lowSamplingFrequency = 0;
|
|
233
|
+
let mpeg25 = 0;
|
|
234
|
+
if (secondByte & 1 << 4) {
|
|
235
|
+
lowSamplingFrequency = secondByte & 1 << 3 ? 0 : 1;
|
|
236
|
+
} else {
|
|
237
|
+
lowSamplingFrequency = 1;
|
|
238
|
+
mpeg25 = 1;
|
|
239
|
+
}
|
|
111
240
|
const mpegVersionId = secondByte >> 3 & 3;
|
|
112
241
|
const layer = secondByte >> 1 & 3;
|
|
113
242
|
const bitrateIndex = thirdByte >> 4 & 15;
|
|
114
|
-
const frequencyIndex = thirdByte >> 2 & 3;
|
|
243
|
+
const frequencyIndex = (thirdByte >> 2 & 3) % 3;
|
|
115
244
|
const padding = thirdByte >> 1 & 1;
|
|
116
245
|
const channel = fourthByte >> 6 & 3;
|
|
117
246
|
const modeExtension = fourthByte >> 4 & 3;
|
|
118
247
|
const copyright = fourthByte >> 3 & 1;
|
|
119
248
|
const original = fourthByte >> 2 & 1;
|
|
120
249
|
const emphasis = fourthByte & 3;
|
|
121
|
-
const kilobitRate =
|
|
122
|
-
if (
|
|
250
|
+
const kilobitRate = KILOBIT_RATES[lowSamplingFrequency * 16 * 4 + layer * 16 + bitrateIndex];
|
|
251
|
+
if (kilobitRate === -1) {
|
|
123
252
|
return { header: null, bytesAdvanced: 1 };
|
|
124
253
|
}
|
|
125
254
|
const bitrate = kilobitRate * 1e3;
|
|
126
|
-
const sampleRate = SAMPLING_RATES[
|
|
127
|
-
|
|
128
|
-
return { header: null, bytesAdvanced: 1 };
|
|
129
|
-
}
|
|
130
|
-
const frameLength = computeMp3FrameSize(layer, bitrate, sampleRate, padding);
|
|
255
|
+
const sampleRate = SAMPLING_RATES[frequencyIndex] >> lowSamplingFrequency + mpeg25;
|
|
256
|
+
const frameLength = computeMp3FrameSize(lowSamplingFrequency, layer, bitrate, sampleRate, padding);
|
|
131
257
|
if (remainingBytes !== null && remainingBytes < frameLength) {
|
|
132
258
|
return { header: null, bytesAdvanced: 1 };
|
|
133
259
|
}
|
|
@@ -201,7 +327,9 @@ var MediabunnyMp3Encoder = (() => {
|
|
|
201
327
|
this.chunkMetadata = {};
|
|
202
328
|
}
|
|
203
329
|
static supports(codec, config) {
|
|
204
|
-
return codec === "mp3" && (config.numberOfChannels === 1 || config.numberOfChannels === 2) && Object.values(SAMPLING_RATES).some(
|
|
330
|
+
return codec === "mp3" && (config.numberOfChannels === 1 || config.numberOfChannels === 2) && Object.values(SAMPLING_RATES).some(
|
|
331
|
+
(x) => x === config.sampleRate || x / 2 === config.sampleRate || x / 4 === config.sampleRate
|
|
332
|
+
);
|
|
205
333
|
}
|
|
206
334
|
async init() {
|
|
207
335
|
this.worker = await Worker2();
|