1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2017-2018 Rafael da Silva Rocha. MIT License. |
3
|
|
|
* https://github.com/rochars/wavefile |
4
|
|
|
* |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** @private */ |
8
|
|
|
const bitDepth_ = require("bitdepth"); |
9
|
|
|
/** @private */ |
10
|
|
|
const riffChunks_ = require("riff-chunks"); |
11
|
|
|
/** @private */ |
12
|
|
|
const imaadpcm_ = require("imaadpcm"); |
13
|
|
|
/** @private */ |
14
|
|
|
const alawmulaw_ = require("alawmulaw"); |
15
|
|
|
/** @private */ |
16
|
|
|
const byteData_ = require("byte-data"); |
17
|
|
|
/** @private */ |
18
|
|
|
const encodeBase64_ = require("base64-arraybuffer").encode; |
19
|
|
|
/** @private */ |
20
|
|
|
const decodeBase64_ = require("base64-arraybuffer").decode; |
21
|
|
|
/** @private */ |
22
|
|
|
const uInt16_ = {"bits": 16}; |
23
|
|
|
/** @private */ |
24
|
|
|
const uInt32_ = {"bits": 32}; |
25
|
|
|
/** @private */ |
26
|
|
|
const fourCC_ = {"bits": 32, "char": true}; |
27
|
|
|
/** @private */ |
28
|
|
|
const chr_ = {"bits": 8, "char": true}; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class representing a wav file. |
32
|
|
|
*/ |
33
|
|
|
class WaveFile { |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param {Uint8Array} bytes A wave file buffer. |
37
|
|
|
* @throws {Error} If no "RIFF" chunk is found. |
38
|
|
|
* @throws {Error} If no "fmt " chunk is found. |
39
|
|
|
* @throws {Error} If no "fact" chunk is found and "fact" is needed. |
40
|
|
|
* @throws {Error} If no "data" chunk is found. |
41
|
|
|
*/ |
42
|
|
|
constructor(bytes) { |
43
|
|
|
/** |
44
|
|
|
* The container identifier. |
45
|
|
|
* Only "RIFF" and "RIFX" are supported. |
46
|
|
|
* @type {string} |
47
|
|
|
* @export |
48
|
|
|
*/ |
49
|
|
|
this.container = ""; |
50
|
|
|
/** |
51
|
|
|
* @type {number} |
52
|
|
|
* @export |
53
|
|
|
*/ |
54
|
|
|
this.chunkSize = 0; |
55
|
|
|
/** |
56
|
|
|
* The format. |
57
|
|
|
* Always "WAVE". |
58
|
|
|
* @type {string} |
59
|
|
|
* @export |
60
|
|
|
*/ |
61
|
|
|
this.format = ""; |
62
|
|
|
/** |
63
|
|
|
* The data of the "fmt" chunk. |
64
|
|
|
* @type {!Object<string, *>} |
65
|
|
|
* @export |
66
|
|
|
*/ |
67
|
|
|
this.fmt = { |
68
|
|
|
/** @export @type {string} */ |
69
|
|
|
"chunkId": "", |
70
|
|
|
/** @export @type {number} */ |
71
|
|
|
"chunkSize": 0, |
72
|
|
|
/** @export @type {number} */ |
73
|
|
|
"audioFormat": 0, |
74
|
|
|
/** @export @type {number} */ |
75
|
|
|
"numChannels": 0, |
76
|
|
|
/** @export @type {number} */ |
77
|
|
|
"sampleRate": 0, |
78
|
|
|
/** @export @type {number} */ |
79
|
|
|
"byteRate": 0, |
80
|
|
|
/** @export @type {number} */ |
81
|
|
|
"blockAlign": 0, |
82
|
|
|
/** @export @type {number} */ |
83
|
|
|
"bitsPerSample": 0, |
84
|
|
|
/** @export @type {number} */ |
85
|
|
|
"cbSize": 0, |
86
|
|
|
/** @export @type {number} */ |
87
|
|
|
"validBitsPerSample": 0, |
88
|
|
|
/** @export @type {number} */ |
89
|
|
|
"dwChannelMask": 0, |
90
|
|
|
/** |
91
|
|
|
* 4 32-bit values representing a 128-bit ID |
92
|
|
|
* @export @type {!Array<number>} |
93
|
|
|
*/ |
94
|
|
|
"subformat": [] |
95
|
|
|
}; |
96
|
|
|
/** |
97
|
|
|
* The data of the "fact" chunk. |
98
|
|
|
* @type {!Object<string, *>} |
99
|
|
|
* @export |
100
|
|
|
*/ |
101
|
|
|
this.fact = { |
102
|
|
|
/** @export @type {string} */ |
103
|
|
|
"chunkId": "", |
104
|
|
|
/** @export @type {number} */ |
105
|
|
|
"chunkSize": 0, |
106
|
|
|
/** @export @type {number} */ |
107
|
|
|
"dwSampleLength": 0 |
108
|
|
|
}; |
109
|
|
|
/** |
110
|
|
|
* The data of the "cue " chunk. |
111
|
|
|
* @type {!Object<string, *>} |
112
|
|
|
* @export |
113
|
|
|
*/ |
114
|
|
|
this.cue = { |
115
|
|
|
/** @export @type {string} */ |
116
|
|
|
"chunkId": "", |
117
|
|
|
/** @export @type {number} */ |
118
|
|
|
"chunkSize": 0, |
119
|
|
|
/** @export @type {number} */ |
120
|
|
|
"dwCuePoints": 0, |
121
|
|
|
/** @export @type {!Array<!Object>} */ |
122
|
|
|
"points": [], |
123
|
|
|
}; |
124
|
|
|
/** |
125
|
|
|
* The data of the "bext" chunk. |
126
|
|
|
* @type {!Object<string, *>} |
127
|
|
|
* @export |
128
|
|
|
*/ |
129
|
|
|
this.bext = { |
130
|
|
|
/** @export @type {string} */ |
131
|
|
|
"chunkId": "", |
132
|
|
|
/** @export @type {number} */ |
133
|
|
|
"chunkSize": 0, |
134
|
|
|
/** @export @type {string} */ |
135
|
|
|
"description": "", //256 |
136
|
|
|
/** @export @type {string} */ |
137
|
|
|
"originator": "", //32 |
138
|
|
|
/** @export @type {string} */ |
139
|
|
|
"originatorReference": "", //32 |
140
|
|
|
/** @export @type {string} */ |
141
|
|
|
"originationDate": "", //10 |
142
|
|
|
/** @export @type {string} */ |
143
|
|
|
"originationTime": "", //8 |
144
|
|
|
/** |
145
|
|
|
* 2 32-bit values, timeReference high and low |
146
|
|
|
* @export @type {!Array<number>} |
147
|
|
|
*/ |
148
|
|
|
"timeReference": [], |
149
|
|
|
/** @export @type {number} */ |
150
|
|
|
"version": 0, //WORD |
151
|
|
|
/** @export @type {string} */ |
152
|
|
|
"UMID": "", // 64 chars |
153
|
|
|
/** @export @type {number} */ |
154
|
|
|
"loudnessValue": 0, //WORD |
155
|
|
|
/** @export @type {number} */ |
156
|
|
|
"loudnessRange": 0, //WORD |
157
|
|
|
/** @export @type {number} */ |
158
|
|
|
"maxTruePeakLevel": 0, //WORD |
159
|
|
|
/** @export @type {number} */ |
160
|
|
|
"maxMomentaryLoudness": 0, //WORD |
161
|
|
|
/** @export @type {number} */ |
162
|
|
|
"maxShortTermLoudness": 0, //WORD |
163
|
|
|
/** @export @type {string} */ |
164
|
|
|
"reserved": "", //180 |
165
|
|
|
/** @export @type {string} */ |
166
|
|
|
"codingHistory": "" // string, unlimited |
167
|
|
|
}; |
168
|
|
|
/** |
169
|
|
|
* The data of the "ds64" chunk. |
170
|
|
|
* Used only with RF64 files. |
171
|
|
|
* @type {!Object<string, *>} |
172
|
|
|
* @export |
173
|
|
|
*/ |
174
|
|
|
this.ds64 = { |
175
|
|
|
/** @type {string} */ |
176
|
|
|
"chunkId": "", |
177
|
|
|
/** @export @type {number} */ |
178
|
|
|
"chunkSize": 0, |
179
|
|
|
/** @export @type {number} */ |
180
|
|
|
"riffSizeHigh": 0, // DWORD |
181
|
|
|
/** @export @type {number} */ |
182
|
|
|
"riffSizeLow": 0, // DWORD |
183
|
|
|
/** @export @type {number} */ |
184
|
|
|
"dataSizeHigh": 0, // DWORD |
185
|
|
|
/** @export @type {number} */ |
186
|
|
|
"dataSizeLow": 0, // DWORD |
187
|
|
|
/** @export @type {number} */ |
188
|
|
|
"originationTime": 0, // DWORD |
189
|
|
|
/** @export @type {number} */ |
190
|
|
|
"sampleCountHigh": 0, // DWORD |
191
|
|
|
/** @export @type {number} */ |
192
|
|
|
"sampleCountLow": 0, // DWORD |
193
|
|
|
/** @export @type {number} */ |
194
|
|
|
//"tableLength": 0, // DWORD |
195
|
|
|
/** @export @type {!Array<number>} */ |
196
|
|
|
//"table": [] |
197
|
|
|
}; |
198
|
|
|
/** |
199
|
|
|
* The data of the "data" chunk. |
200
|
|
|
* @type {!Object<string, *>} |
201
|
|
|
* @export |
202
|
|
|
*/ |
203
|
|
|
this.data = { |
204
|
|
|
/** @export @type {string} */ |
205
|
|
|
"chunkId": "", |
206
|
|
|
/** @export @type {number} */ |
207
|
|
|
"chunkSize": 0, |
208
|
|
|
/** @export @type {!Array<number>} */ |
209
|
|
|
"samples": [] |
210
|
|
|
}; |
211
|
|
|
/** |
212
|
|
|
* The data of the "LIST" chunks. |
213
|
|
|
* Each item in this list must have this signature: |
214
|
|
|
* { |
215
|
|
|
* "chunkId": "", |
216
|
|
|
* "chunkSize": 0, |
217
|
|
|
* "format": "", |
218
|
|
|
* "subChunks": [] |
219
|
|
|
* } |
220
|
|
|
* @type {!Array<!Object>} |
221
|
|
|
* @export |
222
|
|
|
*/ |
223
|
|
|
this.LIST = []; |
224
|
|
|
/** |
225
|
|
|
* The data of the "junk" chunk. |
226
|
|
|
* @type {!Object<string, *>} |
227
|
|
|
* @export |
228
|
|
|
*/ |
229
|
|
|
this.junk = { |
230
|
|
|
/** @export @type {string} */ |
231
|
|
|
"chunkId": "", |
232
|
|
|
/** @export @type {number} */ |
233
|
|
|
"chunkSize": 0, |
234
|
|
|
/** @export @type {!Array<number>} */ |
235
|
|
|
"chunkData": [] |
236
|
|
|
}; |
237
|
|
|
/** |
238
|
|
|
* If the data in data.samples is interleaved or not. |
239
|
|
|
* @type {boolean} |
240
|
|
|
* @export |
241
|
|
|
*/ |
242
|
|
|
this.isInterleaved = true; |
243
|
|
|
/** |
244
|
|
|
* @type {string} |
245
|
|
|
* @export |
246
|
|
|
*/ |
247
|
|
|
this.bitDepth = "0"; |
248
|
|
|
/** |
249
|
|
|
* Audio formats. |
250
|
|
|
* Formats not listed here will be set to 65534 |
251
|
|
|
* and treated as WAVE_FORMAT_EXTENSIBLE |
252
|
|
|
* @enum {number} |
253
|
|
|
* @private |
254
|
|
|
*/ |
255
|
|
|
this.audioFormats_ = { |
256
|
|
|
"4": 17, |
257
|
|
|
"8": 1, |
258
|
|
|
"8a": 6, |
259
|
|
|
"8m": 7, |
260
|
|
|
"16": 1, |
261
|
|
|
"24": 1, |
262
|
|
|
"32": 1, |
263
|
|
|
"32f": 3, |
264
|
|
|
"40": 65534, |
265
|
|
|
"48": 65534, |
266
|
|
|
"64": 3 |
267
|
|
|
}; |
268
|
|
|
/** |
269
|
|
|
* @type {number} |
270
|
|
|
* @private |
271
|
|
|
*/ |
272
|
|
|
this.head_ = 0; |
273
|
|
|
// Load a file from the buffer if one was passed |
274
|
|
|
// when creating the object |
275
|
|
|
if(bytes) { |
276
|
|
|
this.fromBuffer(bytes); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Set up a WaveFile object based on the arguments passed. |
282
|
|
|
* @param {number} numChannels The number of channels |
283
|
|
|
* (Integer numbers: 1 for mono, 2 stereo and so on). |
284
|
|
|
* @param {number} sampleRate The sample rate. |
285
|
|
|
* Integer numbers like 8000, 44100, 48000, 96000, 192000. |
286
|
|
|
* @param {string} bitDepth The audio bit depth. |
287
|
|
|
* One of "4", "8", "8a", "8m", "16", "24", "32", "32f", "64" |
288
|
|
|
* or any value between "8" and "32". |
289
|
|
|
* @param {!Array<number>} samples Array of samples to be written. |
290
|
|
|
* The samples must be in the correct range according to the |
291
|
|
|
* bit depth. |
292
|
|
|
* @param {Object} options Optional. Used to force the container |
293
|
|
|
* as RIFX with {"container": "RIFX"} |
294
|
|
|
* @throws {Error} If any argument does not meet the criteria. |
295
|
|
|
* @export |
296
|
|
|
*/ |
297
|
|
|
fromScratch(numChannels, sampleRate, bitDepth, samples, options={}) { |
298
|
|
|
if (!options["container"]) { |
299
|
|
|
options["container"] = "RIFF"; |
300
|
|
|
} |
301
|
|
|
this.bitDepth = bitDepth; |
302
|
|
|
// interleave the samples if they were passed de-interleaved |
303
|
|
|
this.data.samples = samples; |
304
|
|
|
if (samples.length > 0) { |
305
|
|
|
if (samples[0].constructor === Array) { |
306
|
|
|
this.isInterleaved = false; |
307
|
|
|
this.assureInterleaved_(); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
/** type {number} */ |
311
|
|
|
let numBytes = (((parseInt(bitDepth, 10) - 1) | 7) + 1) / 8; |
312
|
|
|
// Normal PCM file header |
313
|
|
|
if (["8","16","24","32","32f","64"].indexOf(bitDepth) > -1) { |
314
|
|
|
this.createPCMHeader_( |
315
|
|
|
bitDepth, numChannels, sampleRate, numBytes, options); |
316
|
|
|
// IMA ADPCM header |
317
|
|
|
} else if (bitDepth == "4") { |
318
|
|
|
this.createADPCMHeader_( |
319
|
|
|
bitDepth, numChannels, sampleRate, numBytes, options); |
320
|
|
|
// A-Law and mu-Law header |
321
|
|
|
} else if (bitDepth == "8a" || bitDepth == "8m") { |
322
|
|
|
this.createALawMulawHeader_( |
323
|
|
|
bitDepth, numChannels, sampleRate, numBytes, options); |
324
|
|
|
// WAVE_FORMAT_EXTENSIBLE |
325
|
|
|
} else { |
326
|
|
|
this.createExtensibleHeader_( |
327
|
|
|
bitDepth, numChannels, sampleRate, numBytes, options); |
328
|
|
|
} |
329
|
|
|
// the data chunk |
330
|
|
|
this.data.chunkId = "data"; |
331
|
|
|
this.data.chunkSize = this.data.samples.length * numBytes; |
332
|
|
|
this.validateHeader_(); |
333
|
|
|
this.LEorBE_(); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Init a WaveFile object from a byte buffer. |
338
|
|
|
* @param {!Uint8Array} bytes The buffer. |
339
|
|
|
* @throws {Error} If container is not RIFF or RIFX. |
340
|
|
|
* @throws {Error} If no "fmt " chunk is found. |
341
|
|
|
* @throws {Error} If no "fact" chunk is found and "fact" is needed. |
342
|
|
|
* @throws {Error} If no "data" chunk is found. |
343
|
|
|
* @export |
344
|
|
|
*/ |
345
|
|
|
fromBuffer(bytes) { |
346
|
|
|
this.clearHeader_(); |
347
|
|
|
this.readRIFFChunk_(bytes); |
348
|
|
|
/** @type {!Object} */ |
349
|
|
|
let chunk = riffChunks_.read(bytes); |
350
|
|
|
this.readDs64Chunk_(chunk["subChunks"]); |
351
|
|
|
this.readFmtChunk_(chunk["subChunks"]); |
352
|
|
|
this.readFactChunk_(chunk["subChunks"]); |
353
|
|
|
this.readBextChunk_(chunk["subChunks"]); |
354
|
|
|
this.readCueChunk_(chunk["subChunks"]); |
355
|
|
|
this.readDataChunk_(chunk["subChunks"]); |
356
|
|
|
this.readLISTChunk_(chunk["subChunks"]); |
357
|
|
|
this.readJunkChunk_(chunk["subChunks"]); |
358
|
|
|
this.bitDepthFromFmt_(); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Return a byte buffer representig the WaveFile object as a .wav file. |
363
|
|
|
* The return value of this method can be written straight to disk. |
364
|
|
|
* @return {!Uint8Array} A .wav file. |
365
|
|
|
* @throws {Error} If any property of the object appears invalid. |
366
|
|
|
* @export |
367
|
|
|
*/ |
368
|
|
|
toBuffer() { |
369
|
|
|
this.validateHeader_(); |
370
|
|
|
this.assureInterleaved_(); |
371
|
|
|
return this.createWaveFile_(); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Use a .wav file encoded as a base64 string to load the WaveFile object. |
376
|
|
|
* @param {string} base64String A .wav file as a base64 string. |
377
|
|
|
* @throws {Error} If any property of the object appears invalid. |
378
|
|
|
* @export |
379
|
|
|
*/ |
380
|
|
|
fromBase64(base64String) { |
381
|
|
|
this.fromBuffer(new Uint8Array(decodeBase64_(base64String))); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Return a base64 string representig the WaveFile object as a .wav file. |
386
|
|
|
* @return {string} A .wav file as a base64 string. |
387
|
|
|
* @throws {Error} If any property of the object appears invalid. |
388
|
|
|
* @export |
389
|
|
|
*/ |
390
|
|
|
toBase64() { |
391
|
|
|
return encodeBase64_(this.toBuffer()); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Return a DataURI string representig the WaveFile object as a .wav file. |
396
|
|
|
* The return of this method can be used to load the audio in browsers. |
397
|
|
|
* @return {string} A .wav file as a DataURI. |
398
|
|
|
* @throws {Error} If any property of the object appears invalid. |
399
|
|
|
* @export |
400
|
|
|
*/ |
401
|
|
|
toDataURI() { |
402
|
|
|
return "data:audio/wav;base64," + this.toBase64(); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Use a .wav file encoded as a DataURI to load the WaveFile object. |
407
|
|
|
* @param {string} dataURI A .wav file as DataURI. |
408
|
|
|
* @throws {Error} If any property of the object appears invalid. |
409
|
|
|
* @export |
410
|
|
|
*/ |
411
|
|
|
fromDataURI(dataURI) { |
412
|
|
|
this.fromBase64(dataURI.replace("data:audio/wav;base64,", "")); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Force a file as RIFF. |
417
|
|
|
* @export |
418
|
|
|
*/ |
419
|
|
|
toRIFF() { |
420
|
|
|
if (this.container == "RF64") { |
421
|
|
|
this.fromScratch( |
422
|
|
|
this.fmt.numChannels, |
423
|
|
|
this.fmt.sampleRate, |
424
|
|
|
this.bitDepth, |
425
|
|
|
this.data.samples); |
426
|
|
|
} else { |
427
|
|
|
this.container = "RIFF"; |
428
|
|
|
this.LEorBE_(); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Force a file as RIFX. |
434
|
|
|
* @export |
435
|
|
|
*/ |
436
|
|
|
toRIFX() { |
437
|
|
|
if (this.container == "RF64") { |
438
|
|
|
this.fromScratch( |
439
|
|
|
this.fmt.numChannels, |
440
|
|
|
this.fmt.sampleRate, |
441
|
|
|
this.bitDepth, |
442
|
|
|
this.data.samples, |
443
|
|
|
{"container": "RIFX"}); |
444
|
|
|
} else { |
445
|
|
|
this.container = "RIFX"; |
446
|
|
|
this.LEorBE_(); |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Change the bit depth of the samples. |
452
|
|
|
* @param {string} bitDepth The new bit depth of the samples. |
453
|
|
|
* One of "8" ... "32" (integers), "32f" or "64" (floats) |
454
|
|
|
* @param {boolean} changeResolution A boolean indicating if the |
455
|
|
|
* resolution of samples should be actually changed or not. |
456
|
|
|
* @throws {Error} If the bit depth is not valid. |
457
|
|
|
* @export |
458
|
|
|
*/ |
459
|
|
|
toBitDepth(bitDepth, changeResolution=true) { |
460
|
|
|
let toBitDepth = bitDepth; |
461
|
|
|
let thisBitDepth = this.bitDepth; |
462
|
|
|
if (!changeResolution) { |
463
|
|
|
toBitDepth = this.realBitDepth_(bitDepth); |
464
|
|
|
thisBitDepth = this.realBitDepth_(this.bitDepth); |
465
|
|
|
} |
466
|
|
|
this.assureInterleaved_(); |
467
|
|
|
this.assureUncompressed_(); |
468
|
|
|
bitDepth_.toBitDepth(this.data.samples, thisBitDepth, toBitDepth); |
469
|
|
|
this.fromScratch( |
470
|
|
|
this.fmt.numChannels, |
471
|
|
|
this.fmt.sampleRate, |
472
|
|
|
bitDepth, |
473
|
|
|
this.data.samples, |
474
|
|
|
{"container": this.correctContainer_()}); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Interleave multi-channel samples. |
479
|
|
|
* @export |
480
|
|
|
*/ |
481
|
|
|
interleave() { |
482
|
|
|
if (!this.isInterleaved) { |
483
|
|
|
/** @type {!Array<number>} */ |
484
|
|
|
let finalSamples = []; |
485
|
|
|
for (let i=0; i < this.data.samples[0].length; i++) { |
486
|
|
|
for (let j=0; j < this.data.samples.length; j++) { |
487
|
|
|
finalSamples.push(this.data.samples[j][i]); |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
this.data.samples = finalSamples; |
491
|
|
|
this.isInterleaved = true; |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* De-interleave samples into multiple channels. |
497
|
|
|
* @export |
498
|
|
|
*/ |
499
|
|
|
deInterleave() { |
500
|
|
|
if (this.isInterleaved) { |
501
|
|
|
/** @type {!Array<!Array<number>>} */ |
502
|
|
|
let finalSamples = []; |
503
|
|
|
for (let i=0; i < this.fmt.numChannels; i++) { |
504
|
|
|
finalSamples[i] = []; |
505
|
|
|
} |
506
|
|
|
for (let i=0; i < this.data.samples.length; i++) { |
507
|
|
|
for (let j=0; j < this.fmt.numChannels; j++) { |
508
|
|
|
finalSamples[j].push(this.data.samples[i+j]); |
509
|
|
|
} |
510
|
|
|
i += this.fmt.numChannels - 1; |
|
|
|
|
511
|
|
|
} |
512
|
|
|
this.data.samples = finalSamples; |
513
|
|
|
this.isInterleaved = false; |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* Encode a 16-bit wave file as 4-bit IMA ADPCM. |
519
|
|
|
* @throws {Error} If sample rate is not 8000. |
520
|
|
|
* @throws {Error} If number of channels is not 1. |
521
|
|
|
* @export |
522
|
|
|
*/ |
523
|
|
|
toIMAADPCM() { |
524
|
|
|
if (this.fmt.sampleRate != 8000) { |
525
|
|
|
throw new Error( |
526
|
|
|
"Only 8000 Hz files can be compressed as IMA-ADPCM."); |
527
|
|
|
} else if(this.fmt.numChannels != 1) { |
|
|
|
|
528
|
|
|
throw new Error( |
529
|
|
|
"Only mono files can be compressed as IMA-ADPCM."); |
530
|
|
|
} else { |
531
|
|
|
this.assure16Bit_(); |
532
|
|
|
this.fromScratch( |
533
|
|
|
this.fmt.numChannels, |
534
|
|
|
this.fmt.sampleRate, |
535
|
|
|
"4", |
536
|
|
|
imaadpcm_.encode(this.data.samples), |
537
|
|
|
{"container": this.correctContainer_()}); |
538
|
|
|
} |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Decode a 4-bit IMA ADPCM wave file as a 16-bit wave file. |
543
|
|
|
* @param {string} bitDepth The new bit depth of the samples. |
544
|
|
|
* One of "8" ... "32" (integers), "32f" or "64" (floats). |
545
|
|
|
* Optional. Default is 16. |
546
|
|
|
* @export |
547
|
|
|
*/ |
548
|
|
|
fromIMAADPCM(bitDepth="16") { |
549
|
|
|
this.fromScratch( |
550
|
|
|
this.fmt.numChannels, |
551
|
|
|
this.fmt.sampleRate, |
552
|
|
|
"16", |
553
|
|
|
imaadpcm_.decode(this.data.samples, this.fmt.blockAlign), |
554
|
|
|
{"container": this.correctContainer_()}); |
555
|
|
|
if (bitDepth != "16") { |
556
|
|
|
this.toBitDepth(bitDepth); |
557
|
|
|
} |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
/** |
561
|
|
|
* Encode 16-bit wave file as 8-bit A-Law. |
562
|
|
|
* @export |
563
|
|
|
*/ |
564
|
|
|
toALaw() { |
565
|
|
|
this.assure16Bit_(); |
566
|
|
|
this.assureInterleaved_(); |
567
|
|
|
this.fromScratch( |
568
|
|
|
this.fmt.numChannels, |
569
|
|
|
this.fmt.sampleRate, |
570
|
|
|
"8a", |
571
|
|
|
alawmulaw_.alaw.encode(this.data.samples), |
572
|
|
|
{"container": this.correctContainer_()}); |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* Decode a 8-bit A-Law wave file into a 16-bit wave file. |
577
|
|
|
* @param {string} bitDepth The new bit depth of the samples. |
578
|
|
|
* One of "8" ... "32" (integers), "32f" or "64" (floats). |
579
|
|
|
* Optional. Default is 16. |
580
|
|
|
* @export |
581
|
|
|
*/ |
582
|
|
|
fromALaw(bitDepth="16") { |
583
|
|
|
this.fromScratch( |
584
|
|
|
this.fmt.numChannels, |
585
|
|
|
this.fmt.sampleRate, |
586
|
|
|
"16", |
587
|
|
|
alawmulaw_.alaw.decode(this.data.samples), |
588
|
|
|
{"container": this.correctContainer_()}); |
589
|
|
|
if (bitDepth != "16") { |
590
|
|
|
this.toBitDepth(bitDepth); |
591
|
|
|
} |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
/** |
595
|
|
|
* Encode 16-bit wave file as 8-bit mu-Law. |
596
|
|
|
* @export |
597
|
|
|
*/ |
598
|
|
|
toMuLaw() { |
599
|
|
|
this.assure16Bit_(); |
600
|
|
|
this.assureInterleaved_(); |
601
|
|
|
this.fromScratch( |
602
|
|
|
this.fmt.numChannels, |
603
|
|
|
this.fmt.sampleRate, |
604
|
|
|
"8m", |
605
|
|
|
alawmulaw_.mulaw.encode(this.data.samples), |
606
|
|
|
{"container": this.correctContainer_()}); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
/** |
610
|
|
|
* Decode a 8-bit mu-Law wave file into a 16-bit wave file. |
611
|
|
|
* @param {string} bitDepth The new bit depth of the samples. |
612
|
|
|
* One of "8" ... "32" (integers), "32f" or "64" (floats). |
613
|
|
|
* Optional. Default is 16. |
614
|
|
|
* @export |
615
|
|
|
*/ |
616
|
|
|
fromMuLaw(bitDepth="16") { |
617
|
|
|
this.fromScratch( |
618
|
|
|
this.fmt.numChannels, |
619
|
|
|
this.fmt.sampleRate, |
620
|
|
|
"16", |
621
|
|
|
alawmulaw_.mulaw.decode(this.data.samples), |
622
|
|
|
{"container": this.correctContainer_()}); |
623
|
|
|
if (bitDepth != "16") { |
624
|
|
|
this.toBitDepth(bitDepth); |
625
|
|
|
} |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* Write a RIFF tag in the INFO chunk. If the tag do not exist, |
630
|
|
|
* then it is created. It if exists, it is overwritten. |
631
|
|
|
* @param {string} tag The tag name. |
632
|
|
|
* @param {string} value The tag value. |
633
|
|
|
* @throws {Error} If the tag name is not valid. |
634
|
|
|
* @export |
635
|
|
|
*/ |
636
|
|
|
setTag(tag, value) { |
637
|
|
|
tag = this.fixTagName_(tag); |
638
|
|
|
/** @type {!Object} */ |
639
|
|
|
let index = this.getTagIndex_(tag); |
640
|
|
|
if (index.TAG !== null) { |
641
|
|
|
this.LIST[index.LIST]["subChunks"][index.TAG]["chunkSize"] = |
642
|
|
|
value.length + 1; |
643
|
|
|
this.LIST[index.LIST]["subChunks"][index.TAG]["value"] = value; |
644
|
|
|
} else if (index.LIST !== null) { |
645
|
|
|
this.LIST[index.LIST]["subChunks"].push({ |
646
|
|
|
"chunkId": tag, |
647
|
|
|
"chunkSize": value.length + 1, |
648
|
|
|
"value": value}); |
649
|
|
|
} else { |
650
|
|
|
this.LIST.push({ |
651
|
|
|
"chunkId": "LIST", |
652
|
|
|
"chunkSize": 8 + value.length + 1, |
653
|
|
|
"format": "INFO", |
654
|
|
|
"chunkData": [], |
655
|
|
|
"subChunks": []}); |
656
|
|
|
this.LIST[this.LIST.length - 1]["subChunks"].push({ |
657
|
|
|
"chunkId": tag, |
658
|
|
|
"chunkSize": value.length + 1, |
659
|
|
|
"value": value}); |
660
|
|
|
} |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
/** |
664
|
|
|
* Return the value of a RIFF tag in the INFO chunk. |
665
|
|
|
* @param {string} tag The tag name. |
666
|
|
|
* @return {string|null} The value if the tag is found, null otherwise. |
667
|
|
|
* @export |
668
|
|
|
*/ |
669
|
|
|
getTag(tag) { |
670
|
|
|
/** @type {!Object} */ |
671
|
|
|
let index = this.getTagIndex_(tag); |
672
|
|
|
if (index.TAG !== null) { |
673
|
|
|
return this.LIST[index.LIST]["subChunks"][index.TAG]["value"]; |
674
|
|
|
} |
675
|
|
|
return null; |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* Remove a RIFF tag in the INFO chunk. |
680
|
|
|
* @param {string} tag The tag name. |
681
|
|
|
* @return {boolean} True if a tag was deleted. |
682
|
|
|
* @export |
683
|
|
|
*/ |
684
|
|
|
deleteTag(tag) { |
685
|
|
|
/** @type {!Object} */ |
686
|
|
|
let index = this.getTagIndex_(tag); |
687
|
|
|
if (index.TAG !== null) { |
688
|
|
|
this.LIST[index.LIST]["subChunks"].splice(index.TAG, 1); |
689
|
|
|
return true; |
690
|
|
|
} |
691
|
|
|
return false; |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* Create a cue point in the wave file. |
696
|
|
|
* @param {number} position The cue point position in milliseconds. |
697
|
|
|
* @param {string} labl The LIST adtl labl text of the marker. Optional. |
698
|
|
|
* @export |
699
|
|
|
*/ |
700
|
|
|
setCuePoint(position, labl="") { |
701
|
|
|
this.cue.chunkId = "cue "; |
702
|
|
|
position = (position * this.fmt.sampleRate) / 1000; |
703
|
|
|
/** @type {!Array<Object>} */ |
704
|
|
|
let existingPoints = this.getCuePoints_(); |
705
|
|
|
this.clearLISTadtl_(); |
706
|
|
|
/** @type {number} */ |
707
|
|
|
let len = this.cue.points.length; |
708
|
|
|
this.cue.points = []; |
709
|
|
|
let hasSet = false; |
710
|
|
|
if (len == 0) { |
|
|
|
|
711
|
|
|
this.setCuePoint_(position, 1, labl); |
712
|
|
|
} else { |
713
|
|
|
for (let i=0; i<len; i++) { |
714
|
|
|
if (existingPoints[i]["dwPosition"] > position && !hasSet) { |
715
|
|
|
this.setCuePoint_(position, i + 1, labl); |
716
|
|
|
this.setCuePoint_( |
717
|
|
|
existingPoints[i]["dwPosition"], |
718
|
|
|
i + 2, |
719
|
|
|
existingPoints[i]["label"]); |
720
|
|
|
hasSet = true; |
721
|
|
|
} else { |
722
|
|
|
this.setCuePoint_( |
723
|
|
|
existingPoints[i]["dwPosition"], |
724
|
|
|
i + 1, |
725
|
|
|
existingPoints[i]["label"]); |
726
|
|
|
} |
727
|
|
|
} |
728
|
|
|
if (!hasSet) { |
729
|
|
|
this.setCuePoint_(position, this.cue.points.length + 1, labl); |
730
|
|
|
} |
731
|
|
|
} |
732
|
|
|
this.cue.dwCuePoints = this.cue.points.length; |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
/** |
736
|
|
|
* Remove a cue point from a wave file. |
737
|
|
|
* @param {number} index the index of the point. First is 1, |
738
|
|
|
* second is 2, and so on. |
739
|
|
|
* @export |
740
|
|
|
*/ |
741
|
|
|
deleteCuePoint(index) { |
742
|
|
|
this.cue.chunkId = "cue "; |
743
|
|
|
/** @type {!Array<Object>} */ |
744
|
|
|
let existingPoints = this.getCuePoints_(); |
745
|
|
|
this.clearLISTadtl_(); |
746
|
|
|
let len = this.cue.points.length; |
747
|
|
|
this.cue.points = []; |
748
|
|
|
for (let i=0; i<len; i++) { |
749
|
|
|
if (i + 1 != index) { |
750
|
|
|
this.setCuePoint_( |
751
|
|
|
existingPoints[i]["dwPosition"], |
752
|
|
|
i + 1, |
753
|
|
|
existingPoints[i]["label"]); |
754
|
|
|
} |
755
|
|
|
} |
756
|
|
|
this.cue.dwCuePoints = this.cue.points.length; |
757
|
|
|
if (this.cue.dwCuePoints) { |
758
|
|
|
this.cue.chunkId = 'cue '; |
759
|
|
|
} else { |
760
|
|
|
this.cue.chunkId = ''; |
761
|
|
|
this.clearLISTadtl_(); |
762
|
|
|
} |
763
|
|
|
} |
764
|
|
|
|
765
|
|
|
/** |
766
|
|
|
* Update the label of a cue point. |
767
|
|
|
* @param {number} pointIndex The ID of the cue point. |
768
|
|
|
* @param {string} label The new text for the label. |
769
|
|
|
* @export |
770
|
|
|
*/ |
771
|
|
|
updateLabel(pointIndex, label) { |
772
|
|
|
/** @type {number|null} */ |
773
|
|
|
let adtlIndex = this.getAdtlChunk_(); |
774
|
|
|
if (adtlIndex !== null) { |
775
|
|
|
for (let i=0; i<this.LIST[adtlIndex]["subChunks"].length; i++) { |
776
|
|
|
if (this.LIST[adtlIndex]["subChunks"][i]["dwName"] == |
777
|
|
|
pointIndex) { |
778
|
|
|
this.LIST[adtlIndex]["subChunks"][i]["value"] = label; |
779
|
|
|
} |
780
|
|
|
} |
781
|
|
|
} |
782
|
|
|
} |
783
|
|
|
|
784
|
|
|
/** |
785
|
|
|
* Push a new cue point in this.cue.points. |
786
|
|
|
* @param {number} position The position in milliseconds. |
787
|
|
|
* @param {number} dwName the dwName of the cue point |
788
|
|
|
* @private |
789
|
|
|
*/ |
790
|
|
|
setCuePoint_(position, dwName, label) { |
791
|
|
|
this.cue.points.push({ |
792
|
|
|
"dwName": dwName, |
793
|
|
|
"dwPosition": position, |
794
|
|
|
"fccChunk": "data", |
795
|
|
|
"dwChunkStart": 0, |
796
|
|
|
"dwBlockStart": 0, |
797
|
|
|
"dwSampleOffset": position, |
798
|
|
|
}); |
799
|
|
|
this.setLabl_(dwName, label); |
800
|
|
|
} |
801
|
|
|
|
802
|
|
|
/** |
803
|
|
|
* Return an array with the position of all cue points in the file. |
804
|
|
|
* @return {!Array<!Object>} |
805
|
|
|
* @private |
806
|
|
|
*/ |
807
|
|
|
getCuePoints_() { |
808
|
|
|
/** @type {!Array<Object>} */ |
809
|
|
|
let points = []; |
810
|
|
|
for (let i=0; i<this.cue.points.length; i++) { |
811
|
|
|
points.push({ |
812
|
|
|
'dwPosition': this.cue.points[i]["dwPosition"], |
813
|
|
|
'label': this.getLabelForCuePoint_( |
814
|
|
|
this.cue.points[i]["dwName"])}); |
815
|
|
|
} |
816
|
|
|
return points; |
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
/** |
820
|
|
|
* Return the label of a cue point. |
821
|
|
|
* @param {number} pointDwName The ID of the cue point. |
822
|
|
|
* @return {string} |
823
|
|
|
* @private |
824
|
|
|
*/ |
825
|
|
|
getLabelForCuePoint_(pointDwName) { |
826
|
|
|
/** @type {number|null} */ |
827
|
|
|
let adtlIndex = this.getAdtlChunk_(); |
828
|
|
|
if (adtlIndex !== null) { |
829
|
|
|
for (let i=0; i<this.LIST[adtlIndex]["subChunks"].length; i++) { |
830
|
|
|
if (this.LIST[adtlIndex]["subChunks"][i]["dwName"] == |
831
|
|
|
pointDwName) { |
832
|
|
|
return this.LIST[adtlIndex]["subChunks"][i]["value"]; |
833
|
|
|
} |
834
|
|
|
} |
835
|
|
|
} |
836
|
|
|
return ""; |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
/** |
840
|
|
|
* Clear any LIST chunk labeled as "adtl". |
841
|
|
|
* @private |
842
|
|
|
*/ |
843
|
|
|
clearLISTadtl_() { |
844
|
|
|
for (let i=0; i<this.LIST.length; i++) { |
845
|
|
|
if (this.LIST[i]["format"] == 'adtl') { |
846
|
|
|
this.LIST.splice(i); |
847
|
|
|
} |
848
|
|
|
} |
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
/** |
852
|
|
|
* Create a new "labl" subchunk in a "LIST" chunk of type "adtl". |
853
|
|
|
* @param {number} dwName The ID of the cue point. |
854
|
|
|
* @param {string} label The label for the cue point. |
855
|
|
|
* @private |
856
|
|
|
*/ |
857
|
|
|
setLabl_(dwName, label) { |
858
|
|
|
/** @type {number|null} */ |
859
|
|
|
let adtlIndex = this.getAdtlChunk_(); |
860
|
|
|
if (adtlIndex === null) { |
861
|
|
|
this.LIST.push({ |
862
|
|
|
"chunkId": "LIST", |
863
|
|
|
"chunkSize": 4, |
864
|
|
|
"format": "adtl", |
865
|
|
|
"chunkData": [], |
866
|
|
|
"subChunks": []}); |
867
|
|
|
adtlIndex = this.LIST.length - 1; |
868
|
|
|
} |
869
|
|
|
this.setLabelText_(adtlIndex === null ? 0 : adtlIndex, dwName, label); |
870
|
|
|
} |
871
|
|
|
|
872
|
|
|
/** |
873
|
|
|
* Create a new "labl" subchunk in a "LIST" chunk of type "adtl". |
874
|
|
|
* @param {number} adtlIndex The index of the "adtl" LIST in this.LIST. |
875
|
|
|
* @param {number} dwName The ID of the cue point. |
876
|
|
|
* @param {string} label The label for the cue point. |
877
|
|
|
* @private |
878
|
|
|
*/ |
879
|
|
|
setLabelText_(adtlIndex, dwName, label) { |
880
|
|
|
this.LIST[adtlIndex]["subChunks"].push({ |
881
|
|
|
"chunkId": "labl", |
882
|
|
|
"chunkSize": label.length, |
883
|
|
|
"dwName": dwName, |
884
|
|
|
"value": label |
885
|
|
|
}); |
886
|
|
|
this.LIST[adtlIndex]["chunkSize"] += label.length + 4 + 4 + 4 + 1; |
887
|
|
|
} |
888
|
|
|
|
889
|
|
|
/** |
890
|
|
|
* Return the index of the "adtl" LIST in this.LIST. |
891
|
|
|
* @return {number|null} |
892
|
|
|
* @private |
893
|
|
|
*/ |
894
|
|
|
getAdtlChunk_() { |
895
|
|
|
for (let i=0; i<this.LIST.length; i++) { |
896
|
|
|
if(this.LIST[i]["format"] == 'adtl') { |
897
|
|
|
return i; |
898
|
|
|
} |
899
|
|
|
} |
900
|
|
|
return null; |
901
|
|
|
} |
902
|
|
|
|
903
|
|
|
/** |
904
|
|
|
* Return the index of a tag in a FILE chunk. |
905
|
|
|
* @param {string} tag The tag name. |
906
|
|
|
* @return {!Object<string, number|null>} |
907
|
|
|
* Object.LIST is the INFO index in LIST |
908
|
|
|
* Object.TAG is the tag index in the INFO |
909
|
|
|
* @private |
910
|
|
|
*/ |
911
|
|
|
getTagIndex_(tag) { |
912
|
|
|
/** @type {!Object} */ |
913
|
|
|
let index = {LIST: null, TAG: null}; |
914
|
|
|
for (let i=0; i<this.LIST.length; i++) { |
915
|
|
|
if (this.LIST[i]["format"] == "INFO") { |
916
|
|
|
index.LIST = i; |
917
|
|
|
for (let j=0; j<this.LIST[i]["subChunks"].length; j++) { |
918
|
|
|
if (this.LIST[i]["subChunks"][j]["chunkId"] == tag) { |
919
|
|
|
index.TAG = j; |
920
|
|
|
break; |
921
|
|
|
} |
922
|
|
|
} |
923
|
|
|
break; |
924
|
|
|
} |
925
|
|
|
} |
926
|
|
|
return index; |
927
|
|
|
} |
928
|
|
|
|
929
|
|
|
/** |
930
|
|
|
* Fix a RIFF tag format if possible, throw an error otherwise. |
931
|
|
|
* @param {string} tag The tag name. |
932
|
|
|
* @return {string} The tag name in proper fourCC format. |
933
|
|
|
* @private |
934
|
|
|
*/ |
935
|
|
|
fixTagName_(tag) { |
936
|
|
|
if (tag.constructor !== String) { |
937
|
|
|
throw new Error("Invalid tag name."); |
938
|
|
|
} else if(tag.length < 4) { |
939
|
|
|
for (let i=0; i<4-tag.length; i++) { |
940
|
|
|
tag += ' '; |
941
|
|
|
} |
942
|
|
|
} |
943
|
|
|
return tag; |
944
|
|
|
} |
945
|
|
|
|
946
|
|
|
/** |
947
|
|
|
* Create the header of a ADPCM wave file. |
948
|
|
|
* @param {string} bitDepth The audio bit depth |
949
|
|
|
* @param {number} numChannels The number of channels |
950
|
|
|
* @param {number} sampleRate The sample rate. |
951
|
|
|
* @param {number} numBytes The number of bytes each sample use. |
952
|
|
|
* @param {!Object} options The extra options, like container defintion. |
953
|
|
|
* @private |
954
|
|
|
*/ |
955
|
|
|
createADPCMHeader_(bitDepth, numChannels, sampleRate, numBytes, options) { |
956
|
|
|
this.createPCMHeader_( |
957
|
|
|
bitDepth, numChannels, sampleRate, numBytes, options); |
958
|
|
|
this.chunkSize = 40 + this.data.samples.length; |
959
|
|
|
this.fmt.chunkSize = 20; |
960
|
|
|
this.fmt.byteRate = 4055; |
961
|
|
|
this.fmt.blockAlign = 256; |
962
|
|
|
this.fmt.bitsPerSample = 4; |
963
|
|
|
this.fmt.cbSize = 2; |
964
|
|
|
this.fmt.validBitsPerSample = 505; |
965
|
|
|
this.fact.chunkId = "fact"; |
966
|
|
|
this.fact.chunkSize = 4; |
967
|
|
|
this.fact.dwSampleLength = this.data.samples.length * 2; |
968
|
|
|
this.data.chunkSize = this.data.samples.length; |
969
|
|
|
} |
970
|
|
|
|
971
|
|
|
/** |
972
|
|
|
* Create the header of WAVE_FORMAT_EXTENSIBLE file. |
973
|
|
|
* @param {string} bitDepth The audio bit depth |
974
|
|
|
* @param {number} numChannels The number of channels |
975
|
|
|
* @param {number} sampleRate The sample rate. |
976
|
|
|
* @param {number} numBytes The number of bytes each sample use. |
977
|
|
|
* @param {!Object} options The extra options, like container defintion. |
978
|
|
|
* @private |
979
|
|
|
*/ |
980
|
|
|
createExtensibleHeader_( |
981
|
|
|
bitDepth, numChannels, sampleRate, numBytes, options) { |
982
|
|
|
this.createPCMHeader_( |
983
|
|
|
bitDepth, numChannels, sampleRate, numBytes, options); |
984
|
|
|
this.chunkSize = 36 + 24 + this.data.samples.length * numBytes; |
985
|
|
|
this.fmt.chunkSize = 40; |
986
|
|
|
this.fmt.bitsPerSample = ((parseInt(bitDepth, 10) - 1) | 7) + 1; |
987
|
|
|
this.fmt.cbSize = 22; |
988
|
|
|
this.fmt.validBitsPerSample = parseInt(bitDepth, 10); |
989
|
|
|
this.fmt.dwChannelMask = 0; |
990
|
|
|
// subformat 128-bit GUID as 4 32-bit values |
991
|
|
|
// only supports uncompressed integer PCM samples |
992
|
|
|
this.fmt.subformat = [1, 1048576, 2852126848, 1905997824]; |
993
|
|
|
} |
994
|
|
|
|
995
|
|
|
/** |
996
|
|
|
* Create the header of mu-Law and A-Law wave files. |
997
|
|
|
* @param {string} bitDepth The audio bit depth |
998
|
|
|
* @param {number} numChannels The number of channels |
999
|
|
|
* @param {number} sampleRate The sample rate. |
1000
|
|
|
* @param {number} numBytes The number of bytes each sample use. |
1001
|
|
|
* @param {!Object} options The extra options, like container defintion. |
1002
|
|
|
* @private |
1003
|
|
|
*/ |
1004
|
|
|
createALawMulawHeader_( |
1005
|
|
|
bitDepth, numChannels, sampleRate, numBytes, options) { |
1006
|
|
|
this.createPCMHeader_( |
1007
|
|
|
bitDepth, numChannels, sampleRate, numBytes, options); |
1008
|
|
|
this.chunkSize = 40 + this.data.samples.length; |
1009
|
|
|
this.fmt.chunkSize = 20; |
1010
|
|
|
this.fmt.cbSize = 2; |
1011
|
|
|
this.fmt.validBitsPerSample = 8; |
1012
|
|
|
this.fact.chunkId = "fact"; |
1013
|
|
|
this.fact.chunkSize = 4; |
1014
|
|
|
this.fact.dwSampleLength = this.data.samples.length; |
1015
|
|
|
} |
1016
|
|
|
|
1017
|
|
|
/** |
1018
|
|
|
* Create the header of a linear PCM wave file. |
1019
|
|
|
* @param {string} bitDepth The audio bit depth |
1020
|
|
|
* @param {number} numChannels The number of channels |
1021
|
|
|
* @param {number} sampleRate The sample rate. |
1022
|
|
|
* @param {number} numBytes The number of bytes each sample use. |
1023
|
|
|
* @param {!Object} options The extra options, like container defintion. |
1024
|
|
|
* @private |
1025
|
|
|
*/ |
1026
|
|
|
createPCMHeader_(bitDepth, numChannels, sampleRate, numBytes, options) { |
1027
|
|
|
this.clearHeader_(); |
1028
|
|
|
this.container = options["container"]; |
1029
|
|
|
this.chunkSize = 36 + this.data.samples.length * numBytes; |
1030
|
|
|
this.format = "WAVE"; |
1031
|
|
|
this.fmt.chunkId = "fmt "; |
1032
|
|
|
this.fmt.chunkSize = 16; |
1033
|
|
|
this.fmt.byteRate = (numChannels * numBytes) * sampleRate; |
1034
|
|
|
this.fmt.blockAlign = numChannels * numBytes; |
1035
|
|
|
this.fmt.audioFormat = this.audioFormats_[bitDepth] ? |
1036
|
|
|
this.audioFormats_[bitDepth] : 65534; |
1037
|
|
|
this.fmt.numChannels = numChannels; |
1038
|
|
|
this.fmt.sampleRate = sampleRate; |
1039
|
|
|
this.fmt.bitsPerSample = parseInt(bitDepth, 10); |
1040
|
|
|
this.fmt.cbSize = 0; |
1041
|
|
|
this.fmt.validBitsPerSample = 0; |
1042
|
|
|
} |
1043
|
|
|
|
1044
|
|
|
/** |
1045
|
|
|
* Return the closest greater number of bits for a number of bits that |
1046
|
|
|
* do not fill a full sequence of bytes. |
1047
|
|
|
* @param {string} bitDepth The bit depth. |
1048
|
|
|
* @return {string} |
1049
|
|
|
* @private |
1050
|
|
|
*/ |
1051
|
|
|
realBitDepth_(bitDepth) { |
1052
|
|
|
if (bitDepth != "32f") { |
1053
|
|
|
bitDepth = (((parseInt(bitDepth, 10) - 1) | 7) + 1).toString(); |
1054
|
|
|
} |
1055
|
|
|
return bitDepth; |
1056
|
|
|
} |
1057
|
|
|
|
1058
|
|
|
/** |
1059
|
|
|
* Validate the header of the file. |
1060
|
|
|
* @throws {Error} If any property of the object appears invalid. |
1061
|
|
|
* @private |
1062
|
|
|
*/ |
1063
|
|
|
validateHeader_() { |
1064
|
|
|
this.validateBitDepth_(); |
1065
|
|
|
this.validateNumChannels_(); |
1066
|
|
|
this.validateSampleRate_(); |
1067
|
|
|
} |
1068
|
|
|
|
1069
|
|
|
/** |
1070
|
|
|
* Validate the bit depth. |
1071
|
|
|
* @return {boolean} True is the bit depth is valid. |
1072
|
|
|
* @throws {Error} If bit depth is invalid. |
1073
|
|
|
* @private |
1074
|
|
|
*/ |
1075
|
|
|
validateBitDepth_() { |
1076
|
|
|
if (!this.audioFormats_[this.bitDepth]) { |
1077
|
|
|
if (parseInt(this.bitDepth, 10) > 8 && |
1078
|
|
|
parseInt(this.bitDepth, 10) < 54) { |
1079
|
|
|
return true; |
1080
|
|
|
} |
1081
|
|
|
throw new Error("Invalid bit depth."); |
1082
|
|
|
} |
1083
|
|
|
return true; |
1084
|
|
|
} |
1085
|
|
|
|
1086
|
|
|
/** |
1087
|
|
|
* Validate the number of channels. |
1088
|
|
|
* @return {boolean} True is the number of channels is valid. |
1089
|
|
|
* @throws {Error} If the number of channels is invalid. |
1090
|
|
|
* @private |
1091
|
|
|
*/ |
1092
|
|
|
validateNumChannels_() { |
1093
|
|
|
/** @type {number} */ |
1094
|
|
|
let blockAlign = this.fmt.numChannels * this.fmt.bitsPerSample / 8; |
1095
|
|
|
if (this.fmt.numChannels < 1 || blockAlign > 65535) { |
1096
|
|
|
throw new Error("Invalid number of channels."); |
1097
|
|
|
} |
1098
|
|
|
return true; |
1099
|
|
|
} |
1100
|
|
|
|
1101
|
|
|
/** |
1102
|
|
|
* Validate the sample rate value. |
1103
|
|
|
* @return {boolean} True is the sample rate is valid. |
1104
|
|
|
* @throws {Error} If the sample rate is invalid. |
1105
|
|
|
* @private |
1106
|
|
|
*/ |
1107
|
|
|
validateSampleRate_() { |
1108
|
|
|
/** @type {number} */ |
1109
|
|
|
let byteRate = this.fmt.numChannels * |
1110
|
|
|
(this.fmt.bitsPerSample / 8) * this.fmt.sampleRate; |
1111
|
|
|
if (this.fmt.sampleRate < 1 || byteRate > 4294967295) { |
1112
|
|
|
throw new Error("Invalid sample rate."); |
1113
|
|
|
} |
1114
|
|
|
return true; |
1115
|
|
|
} |
1116
|
|
|
|
1117
|
|
|
/** |
1118
|
|
|
* Reset attributes that should emptied when a file is |
1119
|
|
|
* created with the fromScratch() or fromBuffer() methods. |
1120
|
|
|
* @private |
1121
|
|
|
*/ |
1122
|
|
|
clearHeader_() { |
1123
|
|
|
this.fmt.cbSize = 0; |
1124
|
|
|
this.fmt.validBitsPerSample = 0; |
1125
|
|
|
this.fact.chunkId = ""; |
1126
|
|
|
this.ds64.chunkId = ""; |
1127
|
|
|
} |
1128
|
|
|
|
1129
|
|
|
/** |
1130
|
|
|
* Make the file 16-bit if it is not. |
1131
|
|
|
* @private |
1132
|
|
|
*/ |
1133
|
|
|
assure16Bit_() { |
1134
|
|
|
this.assureUncompressed_(); |
1135
|
|
|
if (this.bitDepth != "16") { |
1136
|
|
|
this.toBitDepth("16"); |
1137
|
|
|
} |
1138
|
|
|
} |
1139
|
|
|
|
1140
|
|
|
/** |
1141
|
|
|
* Uncompress the samples in case of a compressed file. |
1142
|
|
|
* @private |
1143
|
|
|
*/ |
1144
|
|
|
assureUncompressed_() { |
1145
|
|
|
if (this.bitDepth == "8a") { |
1146
|
|
|
this.fromALaw(); |
1147
|
|
|
} else if(this.bitDepth == "8m") { |
1148
|
|
|
this.fromMuLaw(); |
1149
|
|
|
} else if (this.bitDepth == "4") { |
1150
|
|
|
this.fromIMAADPCM(); |
1151
|
|
|
} |
1152
|
|
|
} |
1153
|
|
|
|
1154
|
|
|
/** |
1155
|
|
|
* Interleave the samples in case they are de-Interleaved. |
1156
|
|
|
* @private |
1157
|
|
|
*/ |
1158
|
|
|
assureInterleaved_() { |
1159
|
|
|
if (!this.isInterleaved) { |
1160
|
|
|
this.interleave(); |
1161
|
|
|
} |
1162
|
|
|
} |
1163
|
|
|
|
1164
|
|
|
/** |
1165
|
|
|
* Set up to work wih big-endian or little-endian files. |
1166
|
|
|
* The types used are changed to LE or BE. If the |
1167
|
|
|
* the file is big-endian (RIFX), true is returned. |
1168
|
|
|
* @return {boolean} True if the file is RIFX. |
1169
|
|
|
* @private |
1170
|
|
|
*/ |
1171
|
|
|
LEorBE_() { |
1172
|
|
|
/** @type {boolean} */ |
1173
|
|
|
let bigEndian = this.container === "RIFX"; |
1174
|
|
|
uInt16_["be"] = bigEndian; |
1175
|
|
|
uInt32_["be"] = bigEndian; |
1176
|
|
|
return bigEndian; |
1177
|
|
|
} |
1178
|
|
|
|
1179
|
|
|
/** |
1180
|
|
|
* Find a chunk by its fourCC_ in a array of RIFF chunks. |
1181
|
|
|
* @param {!Array<!Object>} chunks The wav file chunks. |
1182
|
|
|
* @param {string} chunkId The chunk fourCC_. |
1183
|
|
|
* @param {boolean} multiple True if there may be multiple chunks |
1184
|
|
|
* with the same chunkId. |
1185
|
|
|
* @return {Object|Array<!Object>|null} |
1186
|
|
|
* @private |
1187
|
|
|
*/ |
1188
|
|
|
findChunk_(chunks, chunkId, multiple=false) { |
1189
|
|
|
/** @type {!Array<!Object>} */ |
1190
|
|
|
let chunk = []; |
1191
|
|
|
for (let i=0; i<chunks.length; i++) { |
1192
|
|
|
if (chunks[i]["chunkId"] == chunkId) { |
1193
|
|
|
if (multiple) { |
1194
|
|
|
chunk.push(chunks[i]); |
1195
|
|
|
} else { |
1196
|
|
|
return chunks[i]; |
1197
|
|
|
} |
1198
|
|
|
} |
1199
|
|
|
} |
1200
|
|
|
if (chunkId == "LIST") { |
1201
|
|
|
return chunk.length ? chunk : null; |
1202
|
|
|
} |
1203
|
|
|
return null; |
1204
|
|
|
} |
1205
|
|
|
|
1206
|
|
|
/** |
1207
|
|
|
* Read the RIFF chunk a wave file. |
1208
|
|
|
* @param {!Uint8Array} bytes A wav buffer. |
1209
|
|
|
* @throws {Error} If no "RIFF" chunk is found. |
1210
|
|
|
* @private |
1211
|
|
|
*/ |
1212
|
|
|
readRIFFChunk_(bytes) { |
1213
|
|
|
this.head_ = 0; |
1214
|
|
|
this.container = this.readString_(bytes, 4); |
1215
|
|
|
if (["RIFF", "RIFX", "RF64"].indexOf(this.container) === -1) { |
1216
|
|
|
throw Error("Not a supported format."); |
1217
|
|
|
} |
1218
|
|
|
this.LEorBE_(); |
1219
|
|
|
this.chunkSize = this.read_(bytes, uInt32_); |
1220
|
|
|
this.format = this.readString_(bytes, 4); |
1221
|
|
|
if (this.format != "WAVE") { |
1222
|
|
|
throw Error("Could not find the 'WAVE' format identifier"); |
1223
|
|
|
} |
1224
|
|
|
} |
1225
|
|
|
|
1226
|
|
|
/** |
1227
|
|
|
* Read the "fmt " chunk of a wave file. |
1228
|
|
|
* @param {!Array<!Object>} chunks The wav file chunks. |
1229
|
|
|
* @throws {Error} If no "fmt " chunk is found. |
1230
|
|
|
* @private |
1231
|
|
|
*/ |
1232
|
|
|
readFmtChunk_(chunks) { |
1233
|
|
|
/** type {Array<!Object>} */ |
1234
|
|
|
let chunk = this.findChunk_(chunks, "fmt "); |
1235
|
|
|
if (chunk) { |
1236
|
|
|
this.head_ = 0; |
1237
|
|
|
let chunkData = chunk["chunkData"]; |
1238
|
|
|
this.fmt.chunkId = chunk["chunkId"]; |
1239
|
|
|
this.fmt.chunkSize = chunk["chunkSize"]; |
1240
|
|
|
this.fmt.audioFormat = this.read_(chunkData, uInt16_); |
1241
|
|
|
this.fmt.numChannels = this.read_(chunkData, uInt16_); |
1242
|
|
|
this.fmt.sampleRate = this.read_(chunkData, uInt32_); |
1243
|
|
|
this.fmt.byteRate = this.read_(chunkData, uInt32_); |
1244
|
|
|
this.fmt.blockAlign = this.read_(chunkData, uInt16_); |
1245
|
|
|
this.fmt.bitsPerSample = this.read_(chunkData, uInt16_); |
1246
|
|
|
this.readFmtExtension_(chunkData); |
1247
|
|
|
} else { |
1248
|
|
|
throw Error("Could not find the 'fmt ' chunk"); |
1249
|
|
|
} |
1250
|
|
|
} |
1251
|
|
|
|
1252
|
|
|
/** |
1253
|
|
|
* Read the "fmt " chunk extension. |
1254
|
|
|
* @param {!Array<number>} chunkData The "fmt " chunk. |
1255
|
|
|
* @private |
1256
|
|
|
*/ |
1257
|
|
|
readFmtExtension_(chunkData) { |
1258
|
|
|
if (this.fmt.chunkSize > 16) { |
1259
|
|
|
this.fmt.cbSize = this.read_( |
1260
|
|
|
chunkData, uInt16_); |
1261
|
|
|
if (this.fmt.chunkSize > 18) { |
1262
|
|
|
this.fmt.validBitsPerSample = this.read_(chunkData, uInt16_); |
1263
|
|
|
if (this.fmt.chunkSize > 20) { |
1264
|
|
|
this.fmt.dwChannelMask = this.read_(chunkData, uInt32_); |
1265
|
|
|
this.fmt.subformat = [ |
1266
|
|
|
this.read_(chunkData, uInt32_), |
1267
|
|
|
this.read_(chunkData, uInt32_), |
1268
|
|
|
this.read_(chunkData, uInt32_), |
1269
|
|
|
this.read_(chunkData, uInt32_)]; |
1270
|
|
|
} |
1271
|
|
|
} |
1272
|
|
|
} |
1273
|
|
|
} |
1274
|
|
|
|
1275
|
|
|
/** |
1276
|
|
|
* Read the "fact" chunk of a wav file. |
1277
|
|
|
* @param {!Array<Object>} chunks The wav file chunks. |
1278
|
|
|
* @throws {Error} If no "fact" chunk is found. |
1279
|
|
|
* @private |
1280
|
|
|
*/ |
1281
|
|
|
readFactChunk_(chunks) { |
1282
|
|
|
/** type {Array<!Object>} */ |
1283
|
|
|
let chunk = this.findChunk_(chunks, "fact"); |
1284
|
|
|
if (chunk) { |
1285
|
|
|
this.head_ = 0; |
1286
|
|
|
this.fact.chunkId = chunk["chunkId"]; |
1287
|
|
|
this.fact.chunkSize = chunk["chunkSize"]; |
1288
|
|
|
this.fact.dwSampleLength = this.read_(chunk["chunkData"], uInt32_); |
1289
|
|
|
} |
1290
|
|
|
} |
1291
|
|
|
|
1292
|
|
|
/** |
1293
|
|
|
* Read the "cue " chunk of a wave file. |
1294
|
|
|
* @param {!Array<Object>} chunks The RIFF file chunks. |
1295
|
|
|
* @private |
1296
|
|
|
*/ |
1297
|
|
|
readCueChunk_(chunks) { |
1298
|
|
|
/** type {Array<!Object>} */ |
1299
|
|
|
let chunk = this.findChunk_(chunks, "cue "); |
1300
|
|
|
if (chunk) { |
1301
|
|
|
this.head_ = 0; |
1302
|
|
|
let chunkData = chunk["chunkData"]; |
1303
|
|
|
this.cue.chunkId = chunk["chunkId"]; |
1304
|
|
|
this.cue.chunkSize = chunk["chunkSize"]; |
1305
|
|
|
this.cue.dwCuePoints = this.read_(chunkData, uInt32_); |
1306
|
|
|
for (let i=0; i<this.cue.dwCuePoints; i++) { |
1307
|
|
|
this.cue.points.push({ |
1308
|
|
|
"dwName": this.read_(chunkData, uInt32_), |
1309
|
|
|
"dwPosition": this.read_(chunkData, uInt32_), |
1310
|
|
|
"fccChunk": this.readString_(chunkData, 4), |
1311
|
|
|
"dwChunkStart": this.read_(chunkData, uInt32_), |
1312
|
|
|
"dwBlockStart": this.read_(chunkData, uInt32_), |
1313
|
|
|
"dwSampleOffset": this.read_(chunkData, uInt32_), |
1314
|
|
|
}); |
1315
|
|
|
} |
1316
|
|
|
} |
1317
|
|
|
} |
1318
|
|
|
|
1319
|
|
|
/** |
1320
|
|
|
* Read the "data" chunk of a wave file. |
1321
|
|
|
* @param {!Array<Object>} chunks The RIFF file chunks. |
1322
|
|
|
* @throws {Error} If no "data" chunk is found. |
1323
|
|
|
* @private |
1324
|
|
|
*/ |
1325
|
|
|
readDataChunk_(chunks) { |
1326
|
|
|
/** type {Array<!Object>} */ |
1327
|
|
|
let chunk = this.findChunk_(chunks, "data"); |
1328
|
|
|
if (chunk) { |
1329
|
|
|
this.data.chunkId = "data"; |
1330
|
|
|
this.data.chunkSize = chunk["chunkSize"]; |
1331
|
|
|
this.samplesFromBytes_(chunk["chunkData"]); |
1332
|
|
|
} else { |
1333
|
|
|
throw Error("Could not find the 'data' chunk"); |
1334
|
|
|
} |
1335
|
|
|
} |
1336
|
|
|
|
1337
|
|
|
/** |
1338
|
|
|
* Read the "bext" chunk of a wav file. |
1339
|
|
|
* @param {!Array<Object>} chunks The wav file chunks. |
1340
|
|
|
* @private |
1341
|
|
|
*/ |
1342
|
|
|
readBextChunk_(chunks) { |
1343
|
|
|
/** type {Array<!Object>} */ |
1344
|
|
|
let chunk = this.findChunk_(chunks, "bext"); |
1345
|
|
|
if (chunk) { |
1346
|
|
|
this.head_ = 0; |
1347
|
|
|
let chunkData = chunk["chunkData"]; |
1348
|
|
|
this.bext.chunkId = chunk["chunkId"]; |
1349
|
|
|
this.bext.chunkSize = chunk["chunkSize"]; |
1350
|
|
|
this.bext.description = this.readString_(chunkData, 256); |
1351
|
|
|
this.bext.originator = this.readString_(chunkData, 32); |
1352
|
|
|
this.bext.originatorReference = this.readString_(chunkData, 32); |
1353
|
|
|
this.bext.originationDate = this.readString_(chunkData, 10); |
1354
|
|
|
this.bext.originationTime = this.readString_(chunkData, 8); |
1355
|
|
|
this.bext.timeReference = [ |
1356
|
|
|
this.read_(chunkData, uInt32_), |
1357
|
|
|
this.read_(chunkData, uInt32_)]; |
1358
|
|
|
this.bext.version = this.read_(chunkData, uInt16_); |
1359
|
|
|
this.bext.UMID = this.readString_(chunkData, 64); |
1360
|
|
|
this.bext.loudnessValue = this.read_(chunkData, uInt16_); |
1361
|
|
|
this.bext.loudnessRange = this.read_(chunkData, uInt16_); |
1362
|
|
|
this.bext.maxTruePeakLevel = this.read_(chunkData, uInt16_); |
1363
|
|
|
this.bext.maxMomentaryLoudness = this.read_(chunkData, uInt16_); |
1364
|
|
|
this.bext.maxShortTermLoudness = this.read_(chunkData, uInt16_); |
1365
|
|
|
this.bext.reserved = this.readString_(chunkData, 180); |
1366
|
|
|
this.bext.codingHistory = this.readString_( |
1367
|
|
|
chunkData, this.bext.chunkSize - 602); |
1368
|
|
|
} |
1369
|
|
|
} |
1370
|
|
|
|
1371
|
|
|
/** |
1372
|
|
|
* Read the "ds64" chunk of a wave file. |
1373
|
|
|
* @param {!Array<Object>} chunks The wav file chunks. |
1374
|
|
|
* @throws {Error} If no "ds64" chunk is found and the file is RF64. |
1375
|
|
|
* @private |
1376
|
|
|
*/ |
1377
|
|
|
readDs64Chunk_(chunks) { |
1378
|
|
|
/** type {Array<!Object>} */ |
1379
|
|
|
let chunk = this.findChunk_(chunks, "ds64"); |
1380
|
|
|
if (chunk) { |
1381
|
|
|
this.head_ = 0; |
1382
|
|
|
let chunkData = chunk["chunkData"]; |
1383
|
|
|
this.ds64.chunkId = chunk["chunkId"]; |
1384
|
|
|
this.ds64.chunkSize = chunk["chunkSize"]; |
1385
|
|
|
this.ds64.riffSizeHigh = this.read_(chunkData, uInt32_); |
1386
|
|
|
this.ds64.riffSizeLow = this.read_(chunkData, uInt32_); |
1387
|
|
|
this.ds64.dataSizeHigh = this.read_(chunkData, uInt32_); |
1388
|
|
|
this.ds64.dataSizeLow = this.read_(chunkData, uInt32_); |
1389
|
|
|
this.ds64.originationTime = this.read_(chunkData, uInt32_); |
1390
|
|
|
this.ds64.sampleCountHigh = this.read_(chunkData, uInt32_); |
1391
|
|
|
this.ds64.sampleCountLow = this.read_(chunkData, uInt32_); |
1392
|
|
|
//if (this.ds64.chunkSize > 28) { |
1393
|
|
|
// this.ds64.tableLength = byteData_.unpack( |
1394
|
|
|
// chunkData.slice(28, 32), uInt32_); |
1395
|
|
|
// this.ds64.table = chunkData.slice( |
1396
|
|
|
// 32, 32 + this.ds64.tableLength); |
1397
|
|
|
//} |
1398
|
|
|
} else { |
1399
|
|
|
if (this.container == "RF64") { |
1400
|
|
|
throw Error("Could not find the 'ds64' chunk"); |
1401
|
|
|
} |
1402
|
|
|
} |
1403
|
|
|
} |
1404
|
|
|
|
1405
|
|
|
/** |
1406
|
|
|
* Read the "LIST" chunks of a wave file. |
1407
|
|
|
* @param {!Array<Object>} chunks The wav file chunks. |
1408
|
|
|
* @private |
1409
|
|
|
*/ |
1410
|
|
|
readLISTChunk_(chunks) { |
1411
|
|
|
/** type {Array<Array<!Object>>>} */ |
1412
|
|
|
let listChunks = this.findChunk_(chunks, "LIST", true); |
1413
|
|
|
if (listChunks === null) { |
1414
|
|
|
return; |
1415
|
|
|
} |
1416
|
|
|
for (let j=0; j<listChunks.length; j++) { |
1417
|
|
|
let subChunk = listChunks[j]; |
1418
|
|
|
this.LIST.push({ |
1419
|
|
|
"chunkId": subChunk["chunkId"], |
1420
|
|
|
"chunkSize": subChunk["chunkSize"], |
1421
|
|
|
"format": subChunk["format"], |
1422
|
|
|
"chunkData": subChunk["chunkData"], |
1423
|
|
|
"subChunks": []}); |
1424
|
|
|
for (let x=0; x<subChunk["subChunks"].length; x++) { |
1425
|
|
|
this.readLISTSubChunks_(subChunk["subChunks"][x], |
1426
|
|
|
subChunk["format"]); |
1427
|
|
|
} |
1428
|
|
|
} |
1429
|
|
|
} |
1430
|
|
|
|
1431
|
|
|
/** |
1432
|
|
|
* Read the sub chunks of a "LIST" chunk. |
1433
|
|
|
* @param {!Object} subChunk The "LIST" subchunks. |
1434
|
|
|
* @param {string} format The "LIST" format, "adtl" or "INFO". |
1435
|
|
|
* @private |
1436
|
|
|
*/ |
1437
|
|
|
readLISTSubChunks_(subChunk, format) { |
1438
|
|
|
// 'labl', 'note', 'ltxt', 'file' |
1439
|
|
|
if (format == 'adtl') { |
1440
|
|
|
if (["labl", "note"].indexOf(subChunk["chunkId"]) > -1) { |
1441
|
|
|
this.LIST[this.LIST.length - 1]["subChunks"].push({ |
1442
|
|
|
"chunkId": subChunk["chunkId"], |
1443
|
|
|
"chunkSize": subChunk["chunkSize"], |
1444
|
|
|
"dwName": byteData_.unpack( |
1445
|
|
|
subChunk["chunkData"].slice(0, 4),uInt32_), |
1446
|
|
|
"value": this.readZSTR_(subChunk["chunkData"].slice(4)) |
1447
|
|
|
}); |
1448
|
|
|
} |
1449
|
|
|
// RIFF 'INFO' tags like ICRD, ISFT, ICMT |
1450
|
|
|
// https://sno.phy.queensu.ca/~phil/exiftool/TagNames/RIFF.html#Info |
1451
|
|
|
} else if(format == 'INFO') { |
1452
|
|
|
this.LIST[this.LIST.length - 1]["subChunks"].push({ |
1453
|
|
|
"chunkId": subChunk["chunkId"], |
1454
|
|
|
"chunkSize": subChunk["chunkSize"], |
1455
|
|
|
"value": this.readZSTR_(subChunk["chunkData"].slice(0)) |
1456
|
|
|
}); |
1457
|
|
|
} //else { |
1458
|
|
|
// this.LIST[this.LIST.length - 1]["subChunks"].push({ |
1459
|
|
|
// "chunkId": subChunk["chunkId"], |
1460
|
|
|
// "chunkSize": subChunk["chunkSize"], |
1461
|
|
|
// "value": subChunk["chunkData"] |
1462
|
|
|
// }); |
1463
|
|
|
//} |
1464
|
|
|
} |
1465
|
|
|
|
1466
|
|
|
/** |
1467
|
|
|
* Read the "junk" chunk of a wave file. |
1468
|
|
|
* @param {!Array<Object>} chunks The wav file chunks. |
1469
|
|
|
* @private |
1470
|
|
|
*/ |
1471
|
|
|
readJunkChunk_(chunks) { |
1472
|
|
|
/** type {Array<!Object>} */ |
1473
|
|
|
let chunk = this.findChunk_(chunks, "junk"); |
1474
|
|
|
if (chunk) { |
1475
|
|
|
this.junk = { |
1476
|
|
|
"chunkId": chunk["chunkId"], |
1477
|
|
|
"chunkSize": chunk["chunkSize"], |
1478
|
|
|
"chunkData": chunk["chunkData"] |
1479
|
|
|
}; |
1480
|
|
|
} |
1481
|
|
|
} |
1482
|
|
|
|
1483
|
|
|
/** |
1484
|
|
|
* Read bytes as a ZSTR string. |
1485
|
|
|
* @param {!Array<number>|!Uint8Array} bytes The bytes. |
1486
|
|
|
* @return {string} The string. |
1487
|
|
|
* @private |
1488
|
|
|
*/ |
1489
|
|
|
readZSTR_(bytes) { |
1490
|
|
|
/** type {string} */ |
1491
|
|
|
let str = ""; |
1492
|
|
|
for (let i=0; i<bytes.length; i++) { |
1493
|
|
|
if (bytes[i] === 0) { |
1494
|
|
|
break; |
1495
|
|
|
} |
1496
|
|
|
str += byteData_.unpack([bytes[i]], chr_); |
1497
|
|
|
} |
1498
|
|
|
return str; |
1499
|
|
|
} |
1500
|
|
|
|
1501
|
|
|
/** |
1502
|
|
|
* Read bytes as a string from a RIFF chunk. |
1503
|
|
|
* @param {!Array<number>|!Uint8Array} bytes The bytes. |
1504
|
|
|
* @param {number} maxSize the max size of the string. |
1505
|
|
|
* @return {string} The string. |
1506
|
|
|
* @private |
1507
|
|
|
*/ |
1508
|
|
|
readString_(bytes, maxSize) { |
1509
|
|
|
/** type {string} */ |
1510
|
|
|
let str = ""; |
1511
|
|
|
for (let i=0; i<maxSize; i++) { |
1512
|
|
|
str += byteData_.unpack([bytes[this.head_]], chr_); |
1513
|
|
|
this.head_++; |
1514
|
|
|
} |
1515
|
|
|
return str; |
1516
|
|
|
} |
1517
|
|
|
|
1518
|
|
|
/** |
1519
|
|
|
* Read a number from a chunk. |
1520
|
|
|
* @param {!Array<number>|!Uint8Array} bytes The chunk bytes. |
1521
|
|
|
* @param {!Object} bdType The type definition. |
1522
|
|
|
* @return {number} The number. |
1523
|
|
|
* @private |
1524
|
|
|
*/ |
1525
|
|
|
read_(bytes, bdType) { |
1526
|
|
|
let size = bdType["bits"] / 8; |
1527
|
|
|
let value = byteData_.unpack( |
1528
|
|
|
bytes.slice(this.head_, this.head_ + size), bdType); |
1529
|
|
|
this.head_ += size; |
1530
|
|
|
return value; |
1531
|
|
|
} |
1532
|
|
|
|
1533
|
|
|
/** |
1534
|
|
|
* Write a variable size string as bytes. If the string is smaller |
1535
|
|
|
* than the max size the output array is filled with 0s. |
1536
|
|
|
* @param {string} str The string to be written as bytes. |
1537
|
|
|
* @param {number} maxSize the max size of the string. |
1538
|
|
|
* @return {!Array<number>} The bytes. |
1539
|
|
|
* @private |
1540
|
|
|
*/ |
1541
|
|
|
writeString_(str, maxSize, push=true) { |
1542
|
|
|
/** type {!Array<number>} */ |
1543
|
|
|
let bytes = byteData_.packArray(str, chr_); |
1544
|
|
|
if (push) { |
1545
|
|
|
for (let i=bytes.length; i<maxSize; i++) { |
1546
|
|
|
bytes.push(0); |
1547
|
|
|
} |
1548
|
|
|
} |
1549
|
|
|
return bytes; |
1550
|
|
|
} |
1551
|
|
|
|
1552
|
|
|
/** |
1553
|
|
|
* Turn the samples to bytes. |
1554
|
|
|
* @return {!Array<number>} The bytes. |
1555
|
|
|
* @private |
1556
|
|
|
*/ |
1557
|
|
|
samplesToBytes_() { |
1558
|
|
|
return byteData_.packArray( |
1559
|
|
|
this.data.samples, this.getSamplesType_()); |
1560
|
|
|
} |
1561
|
|
|
|
1562
|
|
|
/** |
1563
|
|
|
* Turn bytes to samples and load them in the data.samples property. |
1564
|
|
|
* @param {!Array<number>} bytes The bytes. |
1565
|
|
|
* @private |
1566
|
|
|
*/ |
1567
|
|
|
samplesFromBytes_(bytes) { |
1568
|
|
|
this.data.samples = byteData_.unpackArray( |
1569
|
|
|
bytes, this.getSamplesType_()); |
1570
|
|
|
} |
1571
|
|
|
|
1572
|
|
|
/** |
1573
|
|
|
* Get the data type definition for the samples. |
1574
|
|
|
* @return {!Object<string, number|boolean>} The type definition. |
1575
|
|
|
* @private |
1576
|
|
|
*/ |
1577
|
|
|
getSamplesType_() { |
1578
|
|
|
/** type {!Object<string, number|boolean>} */ |
1579
|
|
|
let bdType = { |
1580
|
|
|
"be": this.container === "RIFX", |
1581
|
|
|
"bits": this.fmt.bitsPerSample == 4 ? 8 : this.fmt.bitsPerSample, |
1582
|
|
|
"float": this.fmt.audioFormat == 3 ? true : false |
1583
|
|
|
}; |
1584
|
|
|
bdType["signed"] = bdType["bits"] == 8 ? false : true; |
1585
|
|
|
return bdType; |
1586
|
|
|
} |
1587
|
|
|
|
1588
|
|
|
/** |
1589
|
|
|
* Return the bytes of the "bext" chunk. |
1590
|
|
|
* @return {!Array<number>} The "bext" chunk bytes. |
1591
|
|
|
* @private |
1592
|
|
|
*/ |
1593
|
|
|
getBextBytes_() { |
1594
|
|
|
/** type {!Array<number>} */ |
1595
|
|
|
let bytes = []; |
1596
|
|
|
if (this.bext.chunkId) { |
1597
|
|
|
bytes = bytes.concat( |
1598
|
|
|
byteData_.pack(this.bext.chunkId, fourCC_), |
1599
|
|
|
byteData_.pack(602 + this.bext.codingHistory.length, uInt32_), |
1600
|
|
|
this.writeString_(this.bext.description, 256), |
1601
|
|
|
this.writeString_(this.bext.originator, 32), |
1602
|
|
|
this.writeString_(this.bext.originatorReference, 32), |
1603
|
|
|
this.writeString_(this.bext.originationDate, 10), |
1604
|
|
|
this.writeString_(this.bext.originationTime, 8), |
1605
|
|
|
byteData_.pack(this.bext.timeReference[0], uInt32_), |
1606
|
|
|
byteData_.pack(this.bext.timeReference[1], uInt32_), |
1607
|
|
|
byteData_.pack(this.bext.version, uInt16_), |
1608
|
|
|
this.writeString_(this.bext.UMID, 64), |
1609
|
|
|
byteData_.pack(this.bext.loudnessValue, uInt16_), |
1610
|
|
|
byteData_.pack(this.bext.loudnessRange, uInt16_), |
1611
|
|
|
byteData_.pack(this.bext.maxTruePeakLevel, uInt16_), |
1612
|
|
|
byteData_.pack(this.bext.maxMomentaryLoudness, uInt16_), |
1613
|
|
|
byteData_.pack(this.bext.maxShortTermLoudness, uInt16_), |
1614
|
|
|
this.writeString_(this.bext.reserved, 180), |
1615
|
|
|
this.writeString_( |
1616
|
|
|
this.bext.codingHistory, this.bext.codingHistory.length)); |
1617
|
|
|
} |
1618
|
|
|
return bytes; |
1619
|
|
|
} |
1620
|
|
|
|
1621
|
|
|
/** |
1622
|
|
|
* Return the bytes of the "ds64" chunk. |
1623
|
|
|
* @return {!Array<number>} The "ds64" chunk bytes. |
1624
|
|
|
* @private |
1625
|
|
|
*/ |
1626
|
|
|
getDs64Bytes_() { |
1627
|
|
|
/** type {!Array<number>} */ |
1628
|
|
|
let bytes = []; |
1629
|
|
|
if (this.ds64.chunkId) { |
1630
|
|
|
bytes = bytes.concat( |
1631
|
|
|
byteData_.pack(this.ds64.chunkId, fourCC_), |
1632
|
|
|
byteData_.pack(this.ds64.chunkSize, uInt32_), // |
1633
|
|
|
byteData_.pack(this.ds64.riffSizeHigh, uInt32_), |
1634
|
|
|
byteData_.pack(this.ds64.riffSizeLow, uInt32_), |
1635
|
|
|
byteData_.pack(this.ds64.dataSizeHigh, uInt32_), |
1636
|
|
|
byteData_.pack(this.ds64.dataSizeLow, uInt32_), |
1637
|
|
|
byteData_.pack(this.ds64.originationTime, uInt32_), |
1638
|
|
|
byteData_.pack(this.ds64.sampleCountHigh, uInt32_), |
1639
|
|
|
byteData_.pack(this.ds64.sampleCountLow, uInt32_)); |
1640
|
|
|
} |
1641
|
|
|
//if (this.ds64.tableLength) { |
1642
|
|
|
// ds64Bytes = ds64Bytes.concat( |
1643
|
|
|
// byteData_.pack(this.ds64.tableLength, uInt32_), |
1644
|
|
|
// this.ds64.table); |
1645
|
|
|
//} |
1646
|
|
|
return bytes; |
1647
|
|
|
} |
1648
|
|
|
|
1649
|
|
|
/** |
1650
|
|
|
* Return the bytes of the "cue " chunk. |
1651
|
|
|
* @return {!Array<number>} The "cue " chunk bytes. |
1652
|
|
|
* @private |
1653
|
|
|
*/ |
1654
|
|
|
getCueBytes_() { |
1655
|
|
|
/** type {!Array<number>} */ |
1656
|
|
|
let bytes = []; |
1657
|
|
|
if (this.cue.chunkId) { |
1658
|
|
|
let cuePointsBytes = this.getCuePointsBytes_(); |
1659
|
|
|
bytes = bytes.concat( |
1660
|
|
|
byteData_.pack(this.cue.chunkId, fourCC_), |
1661
|
|
|
byteData_.pack(cuePointsBytes.length + 4, uInt32_), |
1662
|
|
|
byteData_.pack(this.cue.dwCuePoints, uInt32_), |
1663
|
|
|
cuePointsBytes); |
1664
|
|
|
} |
1665
|
|
|
return bytes; |
1666
|
|
|
} |
1667
|
|
|
|
1668
|
|
|
/** |
1669
|
|
|
* Return the bytes of the "cue " points. |
1670
|
|
|
* @return {!Array<number>} The "cue " points as an array of bytes. |
1671
|
|
|
* @private |
1672
|
|
|
*/ |
1673
|
|
|
getCuePointsBytes_() { |
1674
|
|
|
/** type {!Array<number>} */ |
1675
|
|
|
let points = []; |
1676
|
|
|
for (let i=0; i<this.cue.dwCuePoints; i++) { |
1677
|
|
|
points = points.concat( |
1678
|
|
|
byteData_.pack(this.cue.points[i]["dwName"], uInt32_), |
1679
|
|
|
byteData_.pack(this.cue.points[i]["dwPosition"], uInt32_), |
1680
|
|
|
byteData_.pack(this.cue.points[i]["fccChunk"], fourCC_), |
1681
|
|
|
byteData_.pack(this.cue.points[i]["dwChunkStart"], uInt32_), |
1682
|
|
|
byteData_.pack(this.cue.points[i]["dwBlockStart"], uInt32_), |
1683
|
|
|
byteData_.pack(this.cue.points[i]["dwSampleOffset"], uInt32_)); |
1684
|
|
|
} |
1685
|
|
|
return points; |
1686
|
|
|
} |
1687
|
|
|
|
1688
|
|
|
/** |
1689
|
|
|
* Return the bytes of the "fact" chunk. |
1690
|
|
|
* @return {!Array<number>} The "fact" chunk bytes. |
1691
|
|
|
* @private |
1692
|
|
|
*/ |
1693
|
|
|
getFactBytes_() { |
1694
|
|
|
/** type {!Array<number>} */ |
1695
|
|
|
let bytes = []; |
1696
|
|
|
if (this.fact.chunkId) { |
1697
|
|
|
bytes = bytes.concat( |
1698
|
|
|
byteData_.pack(this.fact.chunkId, fourCC_), |
1699
|
|
|
byteData_.pack(this.fact.chunkSize, uInt32_), |
1700
|
|
|
byteData_.pack(this.fact.dwSampleLength, uInt32_)); |
1701
|
|
|
} |
1702
|
|
|
return bytes; |
1703
|
|
|
} |
1704
|
|
|
|
1705
|
|
|
/** |
1706
|
|
|
* Return the bytes of the "fmt " chunk. |
1707
|
|
|
* @return {!Array<number>} The "fmt" chunk bytes. |
1708
|
|
|
* @throws {Error} if no "fmt " chunk is present. |
1709
|
|
|
* @private |
1710
|
|
|
*/ |
1711
|
|
|
getFmtBytes_() { |
1712
|
|
|
if (this.fmt.chunkId) { |
1713
|
|
|
return [].concat( |
1714
|
|
|
byteData_.pack(this.fmt.chunkId, fourCC_), |
1715
|
|
|
byteData_.pack(this.fmt.chunkSize, uInt32_), |
1716
|
|
|
byteData_.pack(this.fmt.audioFormat, uInt16_), |
1717
|
|
|
byteData_.pack(this.fmt.numChannels, uInt16_), |
1718
|
|
|
byteData_.pack(this.fmt.sampleRate, uInt32_), |
1719
|
|
|
byteData_.pack(this.fmt.byteRate, uInt32_), |
1720
|
|
|
byteData_.pack(this.fmt.blockAlign, uInt16_), |
1721
|
|
|
byteData_.pack(this.fmt.bitsPerSample, uInt16_), |
1722
|
|
|
this.getFmtExtensionBytes_() |
1723
|
|
|
); |
1724
|
|
|
} |
1725
|
|
|
throw Error("Could not find the 'fmt ' chunk"); |
1726
|
|
|
} |
1727
|
|
|
|
1728
|
|
|
/** |
1729
|
|
|
* Return the bytes of the fmt extension fields. |
1730
|
|
|
* @return {!Array<number>} The fmt extension bytes. |
1731
|
|
|
* @private |
1732
|
|
|
*/ |
1733
|
|
|
getFmtExtensionBytes_() { |
1734
|
|
|
/** type {!Array<number>} */ |
1735
|
|
|
let extension = []; |
1736
|
|
|
if (this.fmt.chunkSize > 16) { |
1737
|
|
|
extension = extension.concat( |
1738
|
|
|
byteData_.pack(this.fmt.cbSize, uInt16_)); |
1739
|
|
|
} |
1740
|
|
|
if (this.fmt.chunkSize > 18) { |
1741
|
|
|
extension = extension.concat( |
1742
|
|
|
byteData_.pack(this.fmt.validBitsPerSample, uInt16_)); |
1743
|
|
|
} |
1744
|
|
|
if (this.fmt.chunkSize > 20) { |
1745
|
|
|
extension = extension.concat( |
1746
|
|
|
byteData_.pack(this.fmt.dwChannelMask, uInt32_)); |
1747
|
|
|
} |
1748
|
|
|
if (this.fmt.chunkSize > 24) { |
1749
|
|
|
extension = extension.concat( |
1750
|
|
|
byteData_.pack(this.fmt.subformat[0], uInt32_), |
1751
|
|
|
byteData_.pack(this.fmt.subformat[1], uInt32_), |
1752
|
|
|
byteData_.pack(this.fmt.subformat[2], uInt32_), |
1753
|
|
|
byteData_.pack(this.fmt.subformat[3], uInt32_)); |
1754
|
|
|
} |
1755
|
|
|
return extension; |
1756
|
|
|
} |
1757
|
|
|
|
1758
|
|
|
/** |
1759
|
|
|
* Return the bytes of the "LIST" chunk. |
1760
|
|
|
* @return {!Array<number>} The "LIST" chunk bytes. |
1761
|
|
|
* @export for tests |
1762
|
|
|
*/ |
1763
|
|
|
getLISTBytes_() { |
1764
|
|
|
/** type {!Array<number>} */ |
1765
|
|
|
let bytes = []; |
1766
|
|
|
for (let i=0; i<this.LIST.length; i++) { |
1767
|
|
|
let subChunksBytes = this.getLISTSubChunksBytes_( |
1768
|
|
|
this.LIST[i]["subChunks"], this.LIST[i]["format"]); |
1769
|
|
|
bytes = bytes.concat( |
1770
|
|
|
byteData_.pack(this.LIST[i]["chunkId"], fourCC_), |
1771
|
|
|
byteData_.pack(subChunksBytes.length + 4, uInt32_), |
1772
|
|
|
byteData_.pack(this.LIST[i]["format"], fourCC_), |
1773
|
|
|
subChunksBytes); |
1774
|
|
|
} |
1775
|
|
|
return bytes; |
1776
|
|
|
} |
1777
|
|
|
|
1778
|
|
|
/** |
1779
|
|
|
* Return the bytes of the sub chunks of a "LIST" chunk. |
1780
|
|
|
* @param {!Array<Object>} subChunks The "LIST" sub chunks. |
1781
|
|
|
* @param {string} format The format of the "LIST" chunk. |
1782
|
|
|
* Currently supported values are "adtl" or "INFO". |
1783
|
|
|
* @return {!Array<number>} The sub chunk bytes. |
1784
|
|
|
* @private |
1785
|
|
|
*/ |
1786
|
|
|
getLISTSubChunksBytes_(subChunks, format) { |
1787
|
|
|
/** type {!Array<number>} */ |
1788
|
|
|
let bytes = []; |
1789
|
|
|
for (let i=0; i<subChunks.length; i++) { |
1790
|
|
|
if (format == "INFO") { |
1791
|
|
|
bytes = bytes.concat( |
1792
|
|
|
byteData_.pack(subChunks[i]["chunkId"], fourCC_), |
1793
|
|
|
byteData_.pack(subChunks[i]["value"].length + 1, uInt32_), |
1794
|
|
|
this.writeString_( |
1795
|
|
|
subChunks[i]["value"], subChunks[i]["value"].length)); |
1796
|
|
|
bytes.push(0); |
1797
|
|
|
} else if (format == "adtl") { |
1798
|
|
|
if (["labl", "note"].indexOf(subChunks[i]["chunkId"]) > -1) { |
1799
|
|
|
bytes = bytes.concat( |
1800
|
|
|
byteData_.pack(subChunks[i]["chunkId"], fourCC_), |
1801
|
|
|
byteData_.pack( |
1802
|
|
|
subChunks[i]["value"].length + 4 + 1, uInt32_), |
1803
|
|
|
byteData_.pack(subChunks[i]["dwName"], uInt32_), |
1804
|
|
|
this.writeString_( |
1805
|
|
|
subChunks[i]["value"], |
1806
|
|
|
subChunks[i]["value"].length)); |
1807
|
|
|
bytes.push(0); |
1808
|
|
|
} |
1809
|
|
|
} //else { |
1810
|
|
|
// bytes = bytes.concat( |
1811
|
|
|
// byteData_.pack( |
1812
|
|
|
// subChunks[i]["chunkData"].length, uInt32_), |
1813
|
|
|
// subChunks[i]["chunkData"] |
1814
|
|
|
// ); |
1815
|
|
|
//} |
1816
|
|
|
if (bytes.length % 2) { |
1817
|
|
|
bytes.push(0); |
1818
|
|
|
} |
1819
|
|
|
} |
1820
|
|
|
return bytes; |
1821
|
|
|
} |
1822
|
|
|
|
1823
|
|
|
/** |
1824
|
|
|
* Return the bytes of the "junk" chunk. |
1825
|
|
|
* @return {!Array<number>} The "junk" chunk bytes. |
1826
|
|
|
* @private |
1827
|
|
|
*/ |
1828
|
|
|
getJunkBytes_() { |
1829
|
|
|
/** type {!Array<number>} */ |
1830
|
|
|
let bytes = []; |
1831
|
|
|
if (this.junk.chunkId) { |
1832
|
|
|
return bytes.concat( |
1833
|
|
|
byteData_.pack(this.junk.chunkId, fourCC_), |
1834
|
|
|
byteData_.pack(this.junk.chunkData.length, uInt32_), |
1835
|
|
|
this.junk.chunkData); |
1836
|
|
|
} |
1837
|
|
|
return bytes; |
1838
|
|
|
} |
1839
|
|
|
|
1840
|
|
|
/** |
1841
|
|
|
* Return "RIFF" if the container is "RF64", the current container name |
1842
|
|
|
* otherwise. Used to enforce "RIFF" when RF64 is not allowed. |
1843
|
|
|
* @return {string} |
1844
|
|
|
* @private |
1845
|
|
|
*/ |
1846
|
|
|
correctContainer_() { |
1847
|
|
|
return this.container == "RF64" ? "RIFF" : this.container; |
1848
|
|
|
} |
1849
|
|
|
|
1850
|
|
|
/** |
1851
|
|
|
* Set the string code of the bit depth based on the "fmt " chunk. |
1852
|
|
|
* @private |
1853
|
|
|
*/ |
1854
|
|
|
bitDepthFromFmt_() { |
1855
|
|
|
if (this.fmt.audioFormat == 3 && this.fmt.bitsPerSample == 32) { |
1856
|
|
|
this.bitDepth = "32f"; |
1857
|
|
|
} else if (this.fmt.audioFormat == 6) { |
1858
|
|
|
this.bitDepth = "8a"; |
1859
|
|
|
} else if (this.fmt.audioFormat == 7) { |
1860
|
|
|
this.bitDepth = "8m"; |
1861
|
|
|
} else { |
1862
|
|
|
this.bitDepth = this.fmt.bitsPerSample.toString(); |
1863
|
|
|
} |
1864
|
|
|
} |
1865
|
|
|
|
1866
|
|
|
/** |
1867
|
|
|
* Return a .wav file byte buffer with the data from the WaveFile object. |
1868
|
|
|
* The return value of this method can be written straight to disk. |
1869
|
|
|
* @return {!Uint8Array} The wav file bytes. |
1870
|
|
|
* @private |
1871
|
|
|
*/ |
1872
|
|
|
createWaveFile_() { |
1873
|
|
|
/** type {!Array<number>} */ |
1874
|
|
|
let samplesBytes = this.samplesToBytes_(); |
1875
|
|
|
/** type {!Array<number>} */ |
1876
|
|
|
let fileBody = [].concat( |
1877
|
|
|
byteData_.pack(this.format, fourCC_), |
1878
|
|
|
this.getJunkBytes_(), |
1879
|
|
|
this.getDs64Bytes_(), |
1880
|
|
|
this.getBextBytes_(), |
1881
|
|
|
this.getFmtBytes_(), |
1882
|
|
|
this.getFactBytes_(), |
1883
|
|
|
byteData_.pack(this.data.chunkId, fourCC_), |
1884
|
|
|
byteData_.pack(samplesBytes.length, uInt32_), |
1885
|
|
|
samplesBytes, |
1886
|
|
|
this.getCueBytes_(), |
1887
|
|
|
this.getLISTBytes_()); |
1888
|
|
|
// concat with the main header and return a .wav file |
1889
|
|
|
return new Uint8Array([].concat( |
1890
|
|
|
byteData_.pack(this.container, fourCC_), |
1891
|
|
|
byteData_.pack(fileBody.length, uInt32_), |
1892
|
|
|
fileBody)); |
1893
|
|
|
} |
1894
|
|
|
} |
1895
|
|
|
|
1896
|
|
|
module.exports = WaveFile; |
1897
|
|
|
|