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