@ac-kit/format-wav 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +77 -0
- package/LICENSE +28 -0
- package/README.md +146 -2
- package/dist/decode/decode-wav-stream.d.ts +29 -0
- package/dist/decode/decode-wav-stream.d.ts.map +1 -0
- package/dist/decode/decode-wav-stream.js +70 -0
- package/dist/decode/decode-wav-stream.js.map +1 -0
- package/dist/decode/parse-wav.d.ts +58 -0
- package/dist/decode/parse-wav.d.ts.map +1 -0
- package/dist/decode/parse-wav.js +91 -0
- package/dist/decode/parse-wav.js.map +1 -0
- package/dist/decode/wav-decoder.d.ts +189 -0
- package/dist/decode/wav-decoder.d.ts.map +1 -0
- package/dist/decode/wav-decoder.js +709 -0
- package/dist/decode/wav-decoder.js.map +1 -0
- package/dist/decode/wav-warning.d.ts +46 -0
- package/dist/decode/wav-warning.d.ts.map +1 -0
- package/dist/decode/wav-warning.js +2 -0
- package/dist/decode/wav-warning.js.map +1 -0
- package/dist/encode/_sample-block.d.ts +12 -0
- package/dist/encode/_sample-block.d.ts.map +1 -0
- package/dist/encode/_sample-block.js +50 -0
- package/dist/encode/_sample-block.js.map +1 -0
- package/dist/encode/_wav-layout.d.ts +69 -0
- package/dist/encode/_wav-layout.d.ts.map +1 -0
- package/dist/encode/_wav-layout.js +272 -0
- package/dist/encode/_wav-layout.js.map +1 -0
- package/dist/encode/create-wav-writable-stream.d.ts +31 -0
- package/dist/encode/create-wav-writable-stream.d.ts.map +1 -0
- package/dist/encode/create-wav-writable-stream.js +82 -0
- package/dist/encode/create-wav-writable-stream.js.map +1 -0
- package/dist/encode/encode-wav.d.ts +21 -0
- package/dist/encode/encode-wav.d.ts.map +1 -0
- package/dist/encode/encode-wav.js +26 -0
- package/dist/encode/encode-wav.js.map +1 -0
- package/dist/encode/wav-encode-stream.d.ts +28 -0
- package/dist/encode/wav-encode-stream.d.ts.map +1 -0
- package/dist/encode/wav-encode-stream.js +46 -0
- package/dist/encode/wav-encode-stream.js.map +1 -0
- package/dist/encode/wav-encoder.d.ts +128 -0
- package/dist/encode/wav-encoder.d.ts.map +1 -0
- package/dist/encode/wav-encoder.js +125 -0
- package/dist/encode/wav-encoder.js.map +1 -0
- package/dist/errors.d.ts +39 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +50 -0
- package/dist/errors.js.map +1 -0
- package/dist/format/_fmt-chunk.d.ts +36 -0
- package/dist/format/_fmt-chunk.d.ts.map +1 -0
- package/dist/format/_fmt-chunk.js +191 -0
- package/dist/format/_fmt-chunk.js.map +1 -0
- package/dist/format/wav-format-chunk.d.ts +34 -0
- package/dist/format/wav-format-chunk.d.ts.map +1 -0
- package/dist/format/wav-format-chunk.js +2 -0
- package/dist/format/wav-format-chunk.js.map +1 -0
- package/dist/format/wav-format.d.ts +113 -0
- package/dist/format/wav-format.d.ts.map +1 -0
- package/dist/format/wav-format.js +125 -0
- package/dist/format/wav-format.js.map +1 -0
- package/dist/format/wav-speaker.d.ts +52 -0
- package/dist/format/wav-speaker.d.ts.map +1 -0
- package/dist/format/wav-speaker.js +99 -0
- package/dist/format/wav-speaker.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/riff/_info.d.ts +24 -0
- package/dist/riff/_info.d.ts.map +1 -0
- package/dist/riff/_info.js +98 -0
- package/dist/riff/_info.js.map +1 -0
- package/dist/riff/_riff.d.ts +28 -0
- package/dist/riff/_riff.d.ts.map +1 -0
- package/dist/riff/_riff.js +63 -0
- package/dist/riff/_riff.js.map +1 -0
- package/dist/riff/wav-chunk.d.ts +27 -0
- package/dist/riff/wav-chunk.d.ts.map +1 -0
- package/dist/riff/wav-chunk.js +2 -0
- package/dist/riff/wav-chunk.js.map +1 -0
- package/dist/samples/wav-samples.d.ts +77 -0
- package/dist/samples/wav-samples.d.ts.map +1 -0
- package/dist/samples/wav-samples.js +268 -0
- package/dist/samples/wav-samples.js.map +1 -0
- package/package.json +66 -3
|
@@ -0,0 +1,709 @@
|
|
|
1
|
+
import { WavFormatError, WavLimitError } from "../errors.js";
|
|
2
|
+
import { MAX_FORMAT_BYTES_, MIN_FORMAT_BYTES_, readFormatChunk_, } from "../format/_fmt-chunk.js";
|
|
3
|
+
import { readInfoList_ } from "../riff/_info.js";
|
|
4
|
+
import { CHUNK_HEADER_BYTES_, DS64_ENTRY_BYTES_, DS64_MIN_BYTES_, ID_BW64_, ID_DATA_, ID_DS64_, ID_FACT_, ID_FMT_, ID_INFO_, ID_LIST_, ID_RF64_, ID_RIFF_, ID_WAVE_, isPrintableFourCc_, MAX_UINT32_, readFourCc_, readUint64Le_, RIFF_HEADER_BYTES_, viewOf_, } from "../riff/_riff.js";
|
|
5
|
+
const DEFAULT_MAX_CHUNK_BYTES_ = 1024 * 1024;
|
|
6
|
+
const LIST_TYPE_BYTES_ = 4;
|
|
7
|
+
const EMPTY_ = new Uint8Array(0);
|
|
8
|
+
function keepNone_() {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
// Whether a pad byte and the first three bytes after it make a printable
|
|
12
|
+
// identifier: the sign that a writer left the pad byte out.
|
|
13
|
+
function isShiftedIdentifier_(padByte, header) {
|
|
14
|
+
const shifted = Uint8Array.of(padByte, header[0], header[1], header[2]);
|
|
15
|
+
return isPrintableFourCc_(shifted, 0);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A WAV reader fed the file in arrays of any size, as they arrive, holding
|
|
19
|
+
* nothing but headers, a partial frame and the chunks it is asked to keep: the
|
|
20
|
+
* samples of the `data` chunk are handed over as views of the input, whole
|
|
21
|
+
* frames at a time. Reads `RIFF`, `RF64` and `BW64` containers, every `fmt `
|
|
22
|
+
* chunk (converting PCM of 8 to 32 bits, IEEE float of 32 and 64 bits and their
|
|
23
|
+
* `WAVE_FORMAT_EXTENSIBLE` forms), `fact` and `LIST`/`INFO`, and skips the rest
|
|
24
|
+
* unless `keepChunk` asks for it.
|
|
25
|
+
*
|
|
26
|
+
* It reads past the defects real files carry — placeholder sizes left by
|
|
27
|
+
* streaming writers, odd chunks without their pad byte, a truncated `data`
|
|
28
|
+
* chunk — reporting each as a `warning` item; `strict` refuses them. Malformed
|
|
29
|
+
* structure is a {@link WavFormatError}, after which the decoder takes no more
|
|
30
|
+
* input.
|
|
31
|
+
*
|
|
32
|
+
* Every call is linear in the bytes given; only kept chunks, headers and frames
|
|
33
|
+
* split between two arrays are copied.
|
|
34
|
+
*/
|
|
35
|
+
export class WavDecoder {
|
|
36
|
+
strict;
|
|
37
|
+
keepChunk;
|
|
38
|
+
maxChunkBytes;
|
|
39
|
+
state = "riff";
|
|
40
|
+
items = [];
|
|
41
|
+
input = EMPTY_;
|
|
42
|
+
offset = 0;
|
|
43
|
+
position = 0;
|
|
44
|
+
gathered = null;
|
|
45
|
+
gatheredLength = 0;
|
|
46
|
+
container = "RIFF";
|
|
47
|
+
riffEnd = null;
|
|
48
|
+
riffSizePlaceholder = false;
|
|
49
|
+
riffSizeReported = false;
|
|
50
|
+
trailingReported = false;
|
|
51
|
+
expectDs64 = false;
|
|
52
|
+
ds64DataBytes = null;
|
|
53
|
+
ds64FrameCount = null;
|
|
54
|
+
ds64Sizes = new Map();
|
|
55
|
+
formatChunk = null;
|
|
56
|
+
dataSeen = false;
|
|
57
|
+
dataRemaining = 0;
|
|
58
|
+
frameCount = 0;
|
|
59
|
+
residue = null;
|
|
60
|
+
residueLength = 0;
|
|
61
|
+
// The chunk being read: its identifier, where its header starts, the
|
|
62
|
+
// payload size its header declares (which decides the pad byte), and what
|
|
63
|
+
// is left to read or skip of it.
|
|
64
|
+
chunkId = "";
|
|
65
|
+
chunkStart = 0;
|
|
66
|
+
chunkPayloadBytes = 0;
|
|
67
|
+
remaining = 0;
|
|
68
|
+
bodyKind = "chunk";
|
|
69
|
+
listType = null;
|
|
70
|
+
padByte = 0;
|
|
71
|
+
padPosition = 0;
|
|
72
|
+
constructor(options) {
|
|
73
|
+
this.strict = options?.strict ?? false;
|
|
74
|
+
this.keepChunk = options?.keepChunk ?? keepNone_;
|
|
75
|
+
this.maxChunkBytes =
|
|
76
|
+
options?.maxChunkByteLength ?? DEFAULT_MAX_CHUNK_BYTES_;
|
|
77
|
+
if (!(this.maxChunkBytes >= 0)) {
|
|
78
|
+
throw new RangeError(`WavDecoder: maxChunkByteLength must be non-negative, got ${this.maxChunkBytes}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Reads the next bytes of the file.
|
|
83
|
+
*
|
|
84
|
+
* @returns The items they complete, in file order; often none.
|
|
85
|
+
* @throws {WavFormatError} When the file is malformed, or, in strict mode,
|
|
86
|
+
* has a defect.
|
|
87
|
+
* @throws {WavLimitError} When a size is past a limit.
|
|
88
|
+
* @throws {TypeError} After {@link end}, or after an error.
|
|
89
|
+
*/
|
|
90
|
+
write(bytes) {
|
|
91
|
+
this.begin_("write");
|
|
92
|
+
this.input = bytes;
|
|
93
|
+
this.offset = 0;
|
|
94
|
+
return this.complete_(false);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Ends the input: reports what it cut short, checks that a `data` chunk was
|
|
98
|
+
* found, and returns the last items, `end` included.
|
|
99
|
+
*
|
|
100
|
+
* @throws {WavFormatError} When no `data` chunk was found, the input ends
|
|
101
|
+
* inside the RIFF header or a `fmt ` or `ds64` chunk, or, in strict mode,
|
|
102
|
+
* ends early anywhere.
|
|
103
|
+
* @throws {TypeError} After a first call, or after an error.
|
|
104
|
+
*/
|
|
105
|
+
end() {
|
|
106
|
+
this.begin_("end");
|
|
107
|
+
return this.complete_(true);
|
|
108
|
+
}
|
|
109
|
+
begin_(method) {
|
|
110
|
+
if (this.state === "done" || this.state === "failed") {
|
|
111
|
+
throw new TypeError(`WavDecoder.${method}: the decoder ${this.state === "done" ? "has ended" : "has failed"}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
complete_(atEnd) {
|
|
115
|
+
try {
|
|
116
|
+
this.run_();
|
|
117
|
+
if (atEnd) {
|
|
118
|
+
this.end_();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
this.state = "failed";
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
this.input = EMPTY_;
|
|
127
|
+
this.offset = 0;
|
|
128
|
+
}
|
|
129
|
+
const items = this.items;
|
|
130
|
+
this.items = [];
|
|
131
|
+
return items;
|
|
132
|
+
}
|
|
133
|
+
emit_(item) {
|
|
134
|
+
this.items.push(item);
|
|
135
|
+
}
|
|
136
|
+
warn_(code, message, position) {
|
|
137
|
+
if (this.strict) {
|
|
138
|
+
throw code === "chunkTooLarge"
|
|
139
|
+
? new WavLimitError(`${message} at ${position}`)
|
|
140
|
+
: new WavFormatError(message, position);
|
|
141
|
+
}
|
|
142
|
+
this.emit_({ type: "warning", warning: { code, message, position } });
|
|
143
|
+
}
|
|
144
|
+
available_() {
|
|
145
|
+
return this.input.length - this.offset;
|
|
146
|
+
}
|
|
147
|
+
consume_(count) {
|
|
148
|
+
this.offset += count;
|
|
149
|
+
this.position += count;
|
|
150
|
+
}
|
|
151
|
+
// `length` bytes from the input: a view when they are all in the current
|
|
152
|
+
// array and nothing is gathered yet, else a copy completed across arrays;
|
|
153
|
+
// `null` when the input ran out first, the bytes so far kept. While
|
|
154
|
+
// gathering, every call asks for the same length. A completed read always
|
|
155
|
+
// ends with a byte of the current array, which `checkPad_` relies on.
|
|
156
|
+
read_(length) {
|
|
157
|
+
if (this.gathered === null) {
|
|
158
|
+
if (this.available_() >= length) {
|
|
159
|
+
const view = this.input.subarray(this.offset, this.offset + length);
|
|
160
|
+
this.consume_(length);
|
|
161
|
+
return view;
|
|
162
|
+
}
|
|
163
|
+
if (this.available_() === 0) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
this.gathered = new Uint8Array(length);
|
|
167
|
+
this.gatheredLength = 0;
|
|
168
|
+
}
|
|
169
|
+
const count = Math.min(length - this.gatheredLength, this.available_());
|
|
170
|
+
this.gathered.set(this.input.subarray(this.offset, this.offset + count), this.gatheredLength);
|
|
171
|
+
this.gatheredLength += count;
|
|
172
|
+
this.consume_(count);
|
|
173
|
+
if (this.gatheredLength < length) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
const gathered = this.gathered;
|
|
177
|
+
this.gathered = null;
|
|
178
|
+
this.gatheredLength = 0;
|
|
179
|
+
return gathered;
|
|
180
|
+
}
|
|
181
|
+
// Each state either moves on without input or reads some; the loop ends
|
|
182
|
+
// when a state finds the input exhausted.
|
|
183
|
+
run_() {
|
|
184
|
+
for (;;) {
|
|
185
|
+
switch (this.state) {
|
|
186
|
+
case "riff": {
|
|
187
|
+
const header = this.read_(RIFF_HEADER_BYTES_);
|
|
188
|
+
if (header === null) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
this.readRiffHeader_(header);
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
case "header": {
|
|
195
|
+
if (!this.expectDs64 &&
|
|
196
|
+
this.riffEnd !== null &&
|
|
197
|
+
this.position >= this.riffEnd) {
|
|
198
|
+
if (this.dataSeen) {
|
|
199
|
+
this.state = "trailing";
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
// A file needs its data chunk: the size is wrong, not the file.
|
|
203
|
+
this.distrustRiffSize_("The RIFF size ends the file before its data chunk", this.position);
|
|
204
|
+
}
|
|
205
|
+
const start = this.position - this.gatheredLength;
|
|
206
|
+
const header = this.read_(CHUNK_HEADER_BYTES_);
|
|
207
|
+
if (header === null) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
this.startChunk_(header, start);
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
case "pad":
|
|
214
|
+
if (!this.readPad_()) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
break;
|
|
218
|
+
case "padCheck": {
|
|
219
|
+
const header = this.read_(CHUNK_HEADER_BYTES_);
|
|
220
|
+
if (header === null) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
this.checkPad_(header);
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
case "listType": {
|
|
227
|
+
const type = this.read_(LIST_TYPE_BYTES_);
|
|
228
|
+
if (type === null) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
this.startList_(type);
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
case "body": {
|
|
235
|
+
const payload = this.read_(this.remaining);
|
|
236
|
+
if (payload === null) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
this.readBody_(payload);
|
|
240
|
+
this.endChunk_();
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
case "skip": {
|
|
244
|
+
const count = Math.min(this.remaining, this.available_());
|
|
245
|
+
this.consume_(count);
|
|
246
|
+
this.remaining -= count;
|
|
247
|
+
if (this.remaining > 0) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
this.endChunk_();
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
case "data":
|
|
254
|
+
if (this.remaining === 0) {
|
|
255
|
+
this.dropResidue_();
|
|
256
|
+
this.endChunk_();
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
if (this.available_() === 0) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
this.readData_();
|
|
263
|
+
break;
|
|
264
|
+
case "trailing":
|
|
265
|
+
if (this.available_() > 0) {
|
|
266
|
+
this.reportTrailing_(this.position);
|
|
267
|
+
this.consume_(this.available_());
|
|
268
|
+
}
|
|
269
|
+
return;
|
|
270
|
+
default:
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
reportTrailing_(position) {
|
|
276
|
+
if (!this.trailingReported) {
|
|
277
|
+
this.trailingReported = true;
|
|
278
|
+
this.warn_("trailingBytes", "Bytes after the end of the RIFF chunk, or too few for a chunk", position);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
readRiffHeader_(header) {
|
|
282
|
+
const id = readFourCc_(header, 0);
|
|
283
|
+
if (id === ID_RIFF_) {
|
|
284
|
+
this.container = "RIFF";
|
|
285
|
+
}
|
|
286
|
+
else if (id === ID_RF64_ || id === ID_BW64_) {
|
|
287
|
+
this.container = id;
|
|
288
|
+
this.expectDs64 = true;
|
|
289
|
+
}
|
|
290
|
+
else if (id === "RIFX") {
|
|
291
|
+
throw new WavFormatError("A big-endian RIFX file, which this decoder does not read", 0);
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
throw new WavFormatError(`Not a RIFF file: it starts with ${JSON.stringify(id)}`, 0);
|
|
295
|
+
}
|
|
296
|
+
const form = readFourCc_(header, 8);
|
|
297
|
+
if (form !== ID_WAVE_) {
|
|
298
|
+
throw new WavFormatError(`A RIFF file of form ${JSON.stringify(form)}, not WAVE`, 8);
|
|
299
|
+
}
|
|
300
|
+
if (this.container === "RIFF") {
|
|
301
|
+
const size = viewOf_(header).getUint32(4, true);
|
|
302
|
+
if (size === 0 || size === MAX_UINT32_) {
|
|
303
|
+
this.riffSizePlaceholder = true;
|
|
304
|
+
this.warn_("riffSizeMismatch", `The RIFF size is a placeholder (${size})`, 4);
|
|
305
|
+
}
|
|
306
|
+
else if (size < LIST_TYPE_BYTES_) {
|
|
307
|
+
this.warn_("riffSizeMismatch", `A RIFF size of ${size}, shorter than its form type`, 4);
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
this.riffEnd = CHUNK_HEADER_BYTES_ + size;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
this.state = "header";
|
|
314
|
+
}
|
|
315
|
+
// The byte after an odd-sized chunk: answers false for want of input.
|
|
316
|
+
readPad_() {
|
|
317
|
+
if (this.riffEnd !== null && this.position >= this.riffEnd) {
|
|
318
|
+
this.warn_("missingPadByte", `The RIFF size ends the file before the pad byte of the ${JSON.stringify(this.chunkId)} chunk`, this.position);
|
|
319
|
+
this.state = "header";
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
if (this.available_() === 0) {
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
const byte = this.input[this.offset];
|
|
326
|
+
this.padPosition = this.position;
|
|
327
|
+
this.consume_(1);
|
|
328
|
+
if (byte === 0) {
|
|
329
|
+
this.state = "header";
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
// A pad byte should be 0; one that is not may be the first byte of the
|
|
333
|
+
// next chunk header, which the next 8 bytes tell.
|
|
334
|
+
this.padByte = byte;
|
|
335
|
+
this.state = "padCheck";
|
|
336
|
+
}
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
checkPad_(header) {
|
|
340
|
+
if (!isPrintableFourCc_(header, 0) &&
|
|
341
|
+
isShiftedIdentifier_(this.padByte, header)) {
|
|
342
|
+
this.warn_("missingPadByte", `No pad byte after the odd-sized ${JSON.stringify(this.chunkId)} chunk`, this.padPosition);
|
|
343
|
+
const shifted = new Uint8Array(CHUNK_HEADER_BYTES_);
|
|
344
|
+
shifted[0] = this.padByte;
|
|
345
|
+
shifted.set(header.subarray(0, CHUNK_HEADER_BYTES_ - 1), 1);
|
|
346
|
+
// Give back the header's last byte, which belongs to what follows.
|
|
347
|
+
this.offset -= 1;
|
|
348
|
+
this.position -= 1;
|
|
349
|
+
this.startChunk_(shifted, this.padPosition);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
this.startChunk_(header, this.padPosition + 1);
|
|
353
|
+
}
|
|
354
|
+
startChunk_(header, start) {
|
|
355
|
+
const id = readFourCc_(header, 0);
|
|
356
|
+
let size = viewOf_(header).getUint32(4, true);
|
|
357
|
+
if (this.expectDs64) {
|
|
358
|
+
if (id !== ID_DS64_) {
|
|
359
|
+
throw new WavFormatError(`An ${this.container} file whose first chunk is ${JSON.stringify(id)}, not ds64`, start);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
else if (this.container !== "RIFF" && size === MAX_UINT32_) {
|
|
363
|
+
const large = id === ID_DATA_ ? this.ds64DataBytes : this.ds64Sizes.get(id);
|
|
364
|
+
if (large === undefined || large === null) {
|
|
365
|
+
throw new WavFormatError(`A ${JSON.stringify(id)} chunk whose size is in ds64, which has none for it`, start);
|
|
366
|
+
}
|
|
367
|
+
size = large;
|
|
368
|
+
}
|
|
369
|
+
this.chunkId = id;
|
|
370
|
+
this.chunkStart = start;
|
|
371
|
+
this.chunkPayloadBytes = size;
|
|
372
|
+
this.remaining = size;
|
|
373
|
+
this.checkRiffOverrun_(id, size, start);
|
|
374
|
+
switch (id) {
|
|
375
|
+
case ID_DS64_:
|
|
376
|
+
if (this.expectDs64) {
|
|
377
|
+
this.startDs64_(size, start);
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
this.startSkip_();
|
|
381
|
+
}
|
|
382
|
+
break;
|
|
383
|
+
case ID_FMT_:
|
|
384
|
+
this.startFormat_(size, start);
|
|
385
|
+
break;
|
|
386
|
+
case ID_DATA_:
|
|
387
|
+
this.startData_(size, start);
|
|
388
|
+
break;
|
|
389
|
+
case ID_FACT_:
|
|
390
|
+
if (size >= 4 && size <= this.maxChunkBytes) {
|
|
391
|
+
this.startBody_("fact");
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
this.startSkip_();
|
|
395
|
+
}
|
|
396
|
+
break;
|
|
397
|
+
case ID_LIST_:
|
|
398
|
+
if (size >= LIST_TYPE_BYTES_) {
|
|
399
|
+
this.state = "listType";
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
this.startKeptOrSkipped_(id, size, start);
|
|
403
|
+
}
|
|
404
|
+
break;
|
|
405
|
+
default:
|
|
406
|
+
this.startKeptOrSkipped_(id, size, start);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
// A chunk past the RIFF size shows the size is wrong: from then on the
|
|
410
|
+
// chunks run to the end of the input, as most readers take them.
|
|
411
|
+
checkRiffOverrun_(id, size, start) {
|
|
412
|
+
if (this.riffEnd !== null &&
|
|
413
|
+
start + CHUNK_HEADER_BYTES_ + size > this.riffEnd &&
|
|
414
|
+
!(id === ID_DATA_ && this.isDataSizeUnknown_(size))) {
|
|
415
|
+
this.distrustRiffSize_(`The ${JSON.stringify(id)} chunk ends past the RIFF size`, start);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
distrustRiffSize_(message, position) {
|
|
419
|
+
this.riffEnd = null;
|
|
420
|
+
if (!this.riffSizeReported) {
|
|
421
|
+
this.riffSizeReported = true;
|
|
422
|
+
this.warn_("riffSizeMismatch", message, position);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
startDs64_(size, start) {
|
|
426
|
+
if (size < DS64_MIN_BYTES_) {
|
|
427
|
+
throw new WavFormatError(`A ds64 chunk of ${size} bytes, shorter than ${DS64_MIN_BYTES_}`, start);
|
|
428
|
+
}
|
|
429
|
+
if (size > this.maxChunkBytes) {
|
|
430
|
+
throw new WavLimitError(`A ds64 chunk of ${size} bytes, past the ${this.maxChunkBytes} held, at ${start}`);
|
|
431
|
+
}
|
|
432
|
+
this.startBody_("ds64");
|
|
433
|
+
}
|
|
434
|
+
startFormat_(size, start) {
|
|
435
|
+
if (this.formatChunk !== null) {
|
|
436
|
+
if (!this.dataSeen) {
|
|
437
|
+
throw new WavFormatError("A second fmt chunk", start);
|
|
438
|
+
}
|
|
439
|
+
this.warn_("duplicateChunk", "A fmt chunk after the data chunk", start);
|
|
440
|
+
this.startSkip_();
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
if (size < MIN_FORMAT_BYTES_ || size > MAX_FORMAT_BYTES_) {
|
|
444
|
+
throw new WavFormatError(`A fmt chunk of ${size} bytes, outside ${MIN_FORMAT_BYTES_} to ${MAX_FORMAT_BYTES_}`, start);
|
|
445
|
+
}
|
|
446
|
+
this.startBody_("fmt");
|
|
447
|
+
}
|
|
448
|
+
startKeptOrSkipped_(id, size, start) {
|
|
449
|
+
if (!this.keepChunk(id, size)) {
|
|
450
|
+
this.startSkip_();
|
|
451
|
+
}
|
|
452
|
+
else if (size > this.maxChunkBytes) {
|
|
453
|
+
this.reportTooLarge_(id, size, start);
|
|
454
|
+
this.startSkip_();
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
this.startBody_("chunk");
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
reportTooLarge_(id, size, start) {
|
|
461
|
+
this.warn_("chunkTooLarge", `A ${JSON.stringify(id)} chunk of ${size} bytes, past the ${this.maxChunkBytes} held`, start);
|
|
462
|
+
}
|
|
463
|
+
startList_(type) {
|
|
464
|
+
const size = this.chunkPayloadBytes;
|
|
465
|
+
const isInfo = readFourCc_(type, 0) === ID_INFO_;
|
|
466
|
+
this.remaining = size - LIST_TYPE_BYTES_;
|
|
467
|
+
if (!isInfo && !this.keepChunk(ID_LIST_, size)) {
|
|
468
|
+
this.startSkip_();
|
|
469
|
+
}
|
|
470
|
+
else if (size > this.maxChunkBytes) {
|
|
471
|
+
this.reportTooLarge_(isInfo ? "LIST/INFO" : ID_LIST_, size, this.chunkStart);
|
|
472
|
+
this.startSkip_();
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
this.listType = isInfo ? null : type.slice();
|
|
476
|
+
this.startBody_(isInfo ? "info" : "list");
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
startBody_(kind) {
|
|
480
|
+
this.bodyKind = kind;
|
|
481
|
+
this.state = "body";
|
|
482
|
+
}
|
|
483
|
+
startSkip_() {
|
|
484
|
+
this.state = "skip";
|
|
485
|
+
}
|
|
486
|
+
endChunk_() {
|
|
487
|
+
this.state = this.chunkPayloadBytes % 2 === 1 ? "pad" : "header";
|
|
488
|
+
}
|
|
489
|
+
readBody_(payload) {
|
|
490
|
+
const payloadStart = this.chunkStart + CHUNK_HEADER_BYTES_;
|
|
491
|
+
switch (this.bodyKind) {
|
|
492
|
+
case "ds64":
|
|
493
|
+
this.readDs64_(payload, payloadStart);
|
|
494
|
+
break;
|
|
495
|
+
case "fmt":
|
|
496
|
+
this.readFormat_(payload, payloadStart);
|
|
497
|
+
break;
|
|
498
|
+
case "fact": {
|
|
499
|
+
const frames = viewOf_(payload).getUint32(0, true);
|
|
500
|
+
this.emit_({
|
|
501
|
+
type: "fact",
|
|
502
|
+
frameCount: this.container !== "RIFF" &&
|
|
503
|
+
frames === MAX_UINT32_ &&
|
|
504
|
+
this.ds64FrameCount !== null
|
|
505
|
+
? this.ds64FrameCount
|
|
506
|
+
: frames,
|
|
507
|
+
});
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
510
|
+
case "info": {
|
|
511
|
+
const { entries, malformedAt } = readInfoList_(payload);
|
|
512
|
+
if (malformedAt !== -1) {
|
|
513
|
+
this.warn_("infoMalformed", "A LIST/INFO entry cut short, or whose identifier is not printable", payloadStart + LIST_TYPE_BYTES_ + malformedAt);
|
|
514
|
+
}
|
|
515
|
+
this.emit_({ type: "info", entries });
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
518
|
+
case "list": {
|
|
519
|
+
const bytes = new Uint8Array(LIST_TYPE_BYTES_ + payload.length);
|
|
520
|
+
bytes.set(this.listType, 0);
|
|
521
|
+
bytes.set(payload, LIST_TYPE_BYTES_);
|
|
522
|
+
this.listType = null;
|
|
523
|
+
this.emit_({ type: "chunk", chunk: { id: ID_LIST_, bytes } });
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
default:
|
|
527
|
+
this.emit_({
|
|
528
|
+
type: "chunk",
|
|
529
|
+
chunk: { id: this.chunkId, bytes: payload },
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
readFormat_(payload, payloadStart) {
|
|
534
|
+
const read = readFormatChunk_(payload, payloadStart);
|
|
535
|
+
this.formatChunk = read.chunk;
|
|
536
|
+
if (read.byteRateMismatch) {
|
|
537
|
+
this.warn_("byteRateMismatch", `A byte rate of ${read.chunk.byteRate}, not ${read.chunk.sampleRate * read.chunk.blockAlign}`, payloadStart + 8);
|
|
538
|
+
}
|
|
539
|
+
if (read.validBitsUnset) {
|
|
540
|
+
this.warn_("validBitsUnset", "WAVE_FORMAT_EXTENSIBLE with 0 valid bits per sample", payloadStart + 18);
|
|
541
|
+
}
|
|
542
|
+
if (read.bitsPerSampleMismatch) {
|
|
543
|
+
this.warn_("bitsPerSampleMismatch", `${read.chunk.bitsPerSample} bits per sample in a ${read.chunk.blockAlign}-byte block for ${read.chunk.channelCount} channels`, payloadStart + 14);
|
|
544
|
+
}
|
|
545
|
+
this.emit_({
|
|
546
|
+
type: "format",
|
|
547
|
+
container: this.container,
|
|
548
|
+
format: read.format,
|
|
549
|
+
formatChunk: read.chunk,
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
readDs64_(payload, payloadStart) {
|
|
553
|
+
const view = viewOf_(payload);
|
|
554
|
+
const riffSize = readUint64Le_(view, 0);
|
|
555
|
+
const dataSize = readUint64Le_(view, 8);
|
|
556
|
+
const frameCount = readUint64Le_(view, 16);
|
|
557
|
+
const tableLength = view.getUint32(24, true);
|
|
558
|
+
if (riffSize === null || dataSize === null || frameCount === null) {
|
|
559
|
+
throw new WavLimitError(`A ds64 size past 2^53 at ${payloadStart}`);
|
|
560
|
+
}
|
|
561
|
+
if (tableLength > (payload.length - DS64_MIN_BYTES_) / DS64_ENTRY_BYTES_) {
|
|
562
|
+
throw new WavFormatError(`A ds64 table of ${tableLength} entries, past the chunk's end`, payloadStart + 24);
|
|
563
|
+
}
|
|
564
|
+
for (let index = 0; index < tableLength; index++) {
|
|
565
|
+
const offset = DS64_MIN_BYTES_ + index * DS64_ENTRY_BYTES_;
|
|
566
|
+
const size = readUint64Le_(view, offset + 4);
|
|
567
|
+
if (size === null) {
|
|
568
|
+
throw new WavLimitError(`A ds64 table size past 2^53 at ${payloadStart + offset}`);
|
|
569
|
+
}
|
|
570
|
+
this.ds64Sizes.set(readFourCc_(payload, offset), size);
|
|
571
|
+
}
|
|
572
|
+
this.expectDs64 = false;
|
|
573
|
+
this.ds64DataBytes = dataSize;
|
|
574
|
+
this.ds64FrameCount = frameCount;
|
|
575
|
+
if (riffSize < LIST_TYPE_BYTES_) {
|
|
576
|
+
this.warn_("riffSizeMismatch", `A ds64 RIFF size of ${riffSize}`, payloadStart);
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
this.riffEnd = CHUNK_HEADER_BYTES_ + riffSize;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
isDataSizeUnknown_(size) {
|
|
583
|
+
return (this.container === "RIFF" &&
|
|
584
|
+
(size === MAX_UINT32_ || (size === 0 && this.riffSizePlaceholder)));
|
|
585
|
+
}
|
|
586
|
+
startData_(size, start) {
|
|
587
|
+
if (this.formatChunk === null) {
|
|
588
|
+
throw new WavFormatError("A data chunk before the fmt chunk", start);
|
|
589
|
+
}
|
|
590
|
+
if (this.dataSeen) {
|
|
591
|
+
this.warn_("duplicateChunk", "A second data chunk", start);
|
|
592
|
+
this.startSkip_();
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
const dataStart = start + CHUNK_HEADER_BYTES_;
|
|
596
|
+
this.dataSeen = true;
|
|
597
|
+
if (this.isDataSizeUnknown_(size)) {
|
|
598
|
+
this.warn_("dataSizeUnknown", `The data size is a placeholder (${size})`, start + 4);
|
|
599
|
+
// Up to the end of the RIFF chunk when it is known, with no pad byte to
|
|
600
|
+
// expect: there is no size to be odd.
|
|
601
|
+
this.chunkPayloadBytes = 0;
|
|
602
|
+
this.remaining =
|
|
603
|
+
this.riffEnd === null
|
|
604
|
+
? Number.POSITIVE_INFINITY
|
|
605
|
+
: Math.max(this.riffEnd - dataStart, 0);
|
|
606
|
+
this.emit_({ type: "data", frameCount: null });
|
|
607
|
+
}
|
|
608
|
+
else {
|
|
609
|
+
this.emit_({
|
|
610
|
+
type: "data",
|
|
611
|
+
frameCount: Math.floor(size / this.formatChunk.blockAlign),
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
this.state = "data";
|
|
615
|
+
}
|
|
616
|
+
readData_() {
|
|
617
|
+
const blockAlign = this.formatChunk.blockAlign;
|
|
618
|
+
const count = Math.min(this.available_(), this.remaining);
|
|
619
|
+
const end = this.offset + count;
|
|
620
|
+
let start = this.offset;
|
|
621
|
+
if (this.residue !== null) {
|
|
622
|
+
const filled = Math.min(blockAlign - this.residueLength, count);
|
|
623
|
+
this.residue.set(this.input.subarray(start, start + filled), this.residueLength);
|
|
624
|
+
this.residueLength += filled;
|
|
625
|
+
start += filled;
|
|
626
|
+
if (this.residueLength === blockAlign) {
|
|
627
|
+
this.emit_({ type: "samples", bytes: this.residue, frameCount: 1 });
|
|
628
|
+
this.frameCount += 1;
|
|
629
|
+
this.residue = null;
|
|
630
|
+
this.residueLength = 0;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
const frames = Math.floor((end - start) / blockAlign);
|
|
634
|
+
if (frames > 0) {
|
|
635
|
+
const wholeEnd = start + frames * blockAlign;
|
|
636
|
+
this.emit_({
|
|
637
|
+
type: "samples",
|
|
638
|
+
bytes: this.input.subarray(start, wholeEnd),
|
|
639
|
+
frameCount: frames,
|
|
640
|
+
});
|
|
641
|
+
this.frameCount += frames;
|
|
642
|
+
start = wholeEnd;
|
|
643
|
+
}
|
|
644
|
+
if (start < end) {
|
|
645
|
+
this.residue = new Uint8Array(blockAlign);
|
|
646
|
+
this.residue.set(this.input.subarray(start, end));
|
|
647
|
+
this.residueLength = end - start;
|
|
648
|
+
}
|
|
649
|
+
this.consume_(count);
|
|
650
|
+
this.remaining -= count;
|
|
651
|
+
}
|
|
652
|
+
dropResidue_() {
|
|
653
|
+
if (this.residueLength > 0) {
|
|
654
|
+
this.warn_("partialFrame", `The data chunk ends ${this.residueLength} bytes into a frame`, this.position - this.residueLength);
|
|
655
|
+
this.residue = null;
|
|
656
|
+
this.residueLength = 0;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
end_() {
|
|
660
|
+
let truncated = false;
|
|
661
|
+
switch (this.state) {
|
|
662
|
+
case "riff":
|
|
663
|
+
throw new WavFormatError(this.position === 0
|
|
664
|
+
? "An empty input"
|
|
665
|
+
: "The input ends inside the RIFF header", this.position);
|
|
666
|
+
case "header":
|
|
667
|
+
case "padCheck":
|
|
668
|
+
if (this.expectDs64) {
|
|
669
|
+
throw new WavFormatError(`An ${this.container} file without its ds64 chunk`, this.position);
|
|
670
|
+
}
|
|
671
|
+
if (this.gatheredLength > 0) {
|
|
672
|
+
this.reportTrailing_(this.position - this.gatheredLength);
|
|
673
|
+
}
|
|
674
|
+
break;
|
|
675
|
+
case "pad":
|
|
676
|
+
this.warn_("missingPadByte", `The input ends before the pad byte of the ${JSON.stringify(this.chunkId)} chunk`, this.position);
|
|
677
|
+
break;
|
|
678
|
+
case "listType":
|
|
679
|
+
case "body":
|
|
680
|
+
case "skip":
|
|
681
|
+
if (this.state === "body" &&
|
|
682
|
+
(this.bodyKind === "fmt" || this.bodyKind === "ds64")) {
|
|
683
|
+
throw new WavFormatError(`The input ends inside the ${this.chunkId} chunk`, this.chunkStart);
|
|
684
|
+
}
|
|
685
|
+
truncated = true;
|
|
686
|
+
this.warn_("chunkTruncated", `The input ends inside the ${JSON.stringify(this.chunkId)} chunk`, this.chunkStart);
|
|
687
|
+
break;
|
|
688
|
+
case "data":
|
|
689
|
+
if (Number.isFinite(this.remaining)) {
|
|
690
|
+
truncated = true;
|
|
691
|
+
this.warn_("dataTruncated", `The input ends ${this.remaining} bytes before the end of the data chunk`, this.position);
|
|
692
|
+
}
|
|
693
|
+
this.dropResidue_();
|
|
694
|
+
break;
|
|
695
|
+
default:
|
|
696
|
+
break;
|
|
697
|
+
}
|
|
698
|
+
if (!this.dataSeen) {
|
|
699
|
+
throw new WavFormatError("No data chunk", this.position);
|
|
700
|
+
}
|
|
701
|
+
if (!truncated && this.riffEnd !== null && this.position < this.riffEnd) {
|
|
702
|
+
this.warn_("riffSizeMismatch", `The input ends ${this.riffEnd - this.position} bytes before the RIFF size says`, this.position);
|
|
703
|
+
}
|
|
704
|
+
this.gathered = null;
|
|
705
|
+
this.state = "done";
|
|
706
|
+
this.emit_({ type: "end", frameCount: this.frameCount });
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
//# sourceMappingURL=wav-decoder.js.map
|