@ac-kit/format-wav 0.0.0-stage → 0.1.0

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/LICENSE +28 -0
  3. package/README.md +146 -2
  4. package/dist/decode/decode-wav-stream.d.ts +29 -0
  5. package/dist/decode/decode-wav-stream.d.ts.map +1 -0
  6. package/dist/decode/decode-wav-stream.js +70 -0
  7. package/dist/decode/decode-wav-stream.js.map +1 -0
  8. package/dist/decode/parse-wav.d.ts +58 -0
  9. package/dist/decode/parse-wav.d.ts.map +1 -0
  10. package/dist/decode/parse-wav.js +91 -0
  11. package/dist/decode/parse-wav.js.map +1 -0
  12. package/dist/decode/wav-decoder.d.ts +189 -0
  13. package/dist/decode/wav-decoder.d.ts.map +1 -0
  14. package/dist/decode/wav-decoder.js +709 -0
  15. package/dist/decode/wav-decoder.js.map +1 -0
  16. package/dist/decode/wav-warning.d.ts +46 -0
  17. package/dist/decode/wav-warning.d.ts.map +1 -0
  18. package/dist/decode/wav-warning.js +2 -0
  19. package/dist/decode/wav-warning.js.map +1 -0
  20. package/dist/encode/_sample-block.d.ts +12 -0
  21. package/dist/encode/_sample-block.d.ts.map +1 -0
  22. package/dist/encode/_sample-block.js +50 -0
  23. package/dist/encode/_sample-block.js.map +1 -0
  24. package/dist/encode/_wav-layout.d.ts +69 -0
  25. package/dist/encode/_wav-layout.d.ts.map +1 -0
  26. package/dist/encode/_wav-layout.js +272 -0
  27. package/dist/encode/_wav-layout.js.map +1 -0
  28. package/dist/encode/create-wav-writable-stream.d.ts +31 -0
  29. package/dist/encode/create-wav-writable-stream.d.ts.map +1 -0
  30. package/dist/encode/create-wav-writable-stream.js +82 -0
  31. package/dist/encode/create-wav-writable-stream.js.map +1 -0
  32. package/dist/encode/encode-wav.d.ts +21 -0
  33. package/dist/encode/encode-wav.d.ts.map +1 -0
  34. package/dist/encode/encode-wav.js +26 -0
  35. package/dist/encode/encode-wav.js.map +1 -0
  36. package/dist/encode/wav-encode-stream.d.ts +28 -0
  37. package/dist/encode/wav-encode-stream.d.ts.map +1 -0
  38. package/dist/encode/wav-encode-stream.js +46 -0
  39. package/dist/encode/wav-encode-stream.js.map +1 -0
  40. package/dist/encode/wav-encoder.d.ts +128 -0
  41. package/dist/encode/wav-encoder.d.ts.map +1 -0
  42. package/dist/encode/wav-encoder.js +125 -0
  43. package/dist/encode/wav-encoder.js.map +1 -0
  44. package/dist/errors.d.ts +39 -0
  45. package/dist/errors.d.ts.map +1 -0
  46. package/dist/errors.js +50 -0
  47. package/dist/errors.js.map +1 -0
  48. package/dist/format/_fmt-chunk.d.ts +36 -0
  49. package/dist/format/_fmt-chunk.d.ts.map +1 -0
  50. package/dist/format/_fmt-chunk.js +191 -0
  51. package/dist/format/_fmt-chunk.js.map +1 -0
  52. package/dist/format/wav-format-chunk.d.ts +34 -0
  53. package/dist/format/wav-format-chunk.d.ts.map +1 -0
  54. package/dist/format/wav-format-chunk.js +2 -0
  55. package/dist/format/wav-format-chunk.js.map +1 -0
  56. package/dist/format/wav-format.d.ts +113 -0
  57. package/dist/format/wav-format.d.ts.map +1 -0
  58. package/dist/format/wav-format.js +125 -0
  59. package/dist/format/wav-format.js.map +1 -0
  60. package/dist/format/wav-speaker.d.ts +52 -0
  61. package/dist/format/wav-speaker.d.ts.map +1 -0
  62. package/dist/format/wav-speaker.js +99 -0
  63. package/dist/format/wav-speaker.js.map +1 -0
  64. package/dist/index.d.ts +15 -0
  65. package/dist/index.d.ts.map +1 -0
  66. package/dist/index.js +15 -0
  67. package/dist/index.js.map +1 -0
  68. package/dist/riff/_info.d.ts +24 -0
  69. package/dist/riff/_info.d.ts.map +1 -0
  70. package/dist/riff/_info.js +98 -0
  71. package/dist/riff/_info.js.map +1 -0
  72. package/dist/riff/_riff.d.ts +28 -0
  73. package/dist/riff/_riff.d.ts.map +1 -0
  74. package/dist/riff/_riff.js +63 -0
  75. package/dist/riff/_riff.js.map +1 -0
  76. package/dist/riff/wav-chunk.d.ts +27 -0
  77. package/dist/riff/wav-chunk.d.ts.map +1 -0
  78. package/dist/riff/wav-chunk.js +2 -0
  79. package/dist/riff/wav-chunk.js.map +1 -0
  80. package/dist/samples/wav-samples.d.ts +77 -0
  81. package/dist/samples/wav-samples.d.ts.map +1 -0
  82. package/dist/samples/wav-samples.js +268 -0
  83. package/dist/samples/wav-samples.js.map +1 -0
  84. package/package.json +66 -3
@@ -0,0 +1,82 @@
1
+ import { raceAbortSignal } from "@ac-kit/core";
2
+ import { WavEncoder, } from "./wav-encoder.js";
3
+ /**
4
+ * A `WritableStream` of sample blocks that writes a WAV file to a seekable
5
+ * sink: the header first, each block as it comes, and on `close()` the trailer
6
+ * then the sizes, written back into the header. The frame count need not be
7
+ * known: a recording stops when it stops. Past 4 GiB the file becomes `RF64` in
8
+ * place (see {@link WavRf64Mode}).
9
+ *
10
+ * The stream writes nothing until the sink accepted the previous bytes, so its
11
+ * backpressure is the sink's; it never opens, closes or truncates the sink.
12
+ * Aborting the stream, or `options.signal`, leaves the sink with a partial file
13
+ * and placeholder sizes.
14
+ *
15
+ * @throws {RangeError} When an option or the format is invalid, as
16
+ * {@link WavEncoder}'s constructor.
17
+ * @throws {WavLimitError} When `options.frameCount` makes a file past what its
18
+ * container holds.
19
+ */
20
+ export function createWavWritableStream(sink, format, options) {
21
+ const encoder = new WavEncoder(format, options);
22
+ const signal = options?.signal;
23
+ let handleAbort = null;
24
+ const stopListening = () => {
25
+ if (handleAbort !== null) {
26
+ signal?.removeEventListener("abort", handleAbort);
27
+ handleAbort = null;
28
+ }
29
+ };
30
+ const write = async (bytes) => {
31
+ signal?.throwIfAborted();
32
+ await raceAbortSignal(Promise.resolve(sink.write(bytes)), signal);
33
+ };
34
+ return new WritableStream({
35
+ async start(controller) {
36
+ if (signal !== undefined) {
37
+ handleAbort = () => {
38
+ controller.error(signal.reason);
39
+ };
40
+ signal.addEventListener("abort", handleAbort, { once: true });
41
+ }
42
+ try {
43
+ await write(encoder.header);
44
+ }
45
+ catch (error) {
46
+ stopListening();
47
+ throw error;
48
+ }
49
+ },
50
+ async write(block) {
51
+ try {
52
+ const bytes = encoder.encode(block);
53
+ if (bytes.length > 0) {
54
+ await write(bytes);
55
+ }
56
+ }
57
+ catch (error) {
58
+ stopListening();
59
+ throw error;
60
+ }
61
+ },
62
+ async close() {
63
+ try {
64
+ const { trailer, patches } = encoder.finish();
65
+ if (trailer.length > 0) {
66
+ await write(trailer);
67
+ }
68
+ for (const patch of patches) {
69
+ signal?.throwIfAborted();
70
+ await raceAbortSignal(Promise.resolve(sink.writeAt(patch.bytes, patch.position)), signal);
71
+ }
72
+ }
73
+ finally {
74
+ stopListening();
75
+ }
76
+ },
77
+ abort() {
78
+ stopListening();
79
+ },
80
+ });
81
+ }
82
+ //# sourceMappingURL=create-wav-writable-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-wav-writable-stream.js","sourceRoot":"","sources":["../../src/encode/create-wav-writable-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAI/C,OAAO,EACN,UAAU,GAGV,MAAM,kBAAkB,CAAC;AAY1B;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,uBAAuB,CACtC,IAAsB,EACtB,MAAiB,EACjB,OAAwC;IAExC,MAAM,OAAO,GAAG,IAAI,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChD,MAAM,MAAM,GAAG,OAAO,EAAE,MAAM,CAAC;IAC/B,IAAI,WAAW,GAAwB,IAAI,CAAC;IAE5C,MAAM,aAAa,GAAG,GAAS,EAAE;QAChC,IAAI,WAAW,KAAK,IAAI,EAAE,CAAC;YAC1B,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;YAClD,WAAW,GAAG,IAAI,CAAC;QACpB,CAAC;IACF,CAAC,CAAC;IACF,MAAM,KAAK,GAAG,KAAK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E,CAAC;oBACxB,MAAM,KAAK,CAAC,OAAO,CAAC,CAAC;gBACtB,CAAC;gBACD,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;oBAC7B,MAAM,EAAE,cAAc,EAAE,CAAC;oBACzB,MAAM,eAAe,CACpB,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC,EAC1D,MAAM,CACN,CAAC;gBACH,CAAC;YACF,CAAC;oBAAS,CAAC;gBACV,aAAa,EAAE,CAAC;YACjB,CAAC;QACF,CAAC;QACD,KAAK;YACJ,aAAa,EAAE,CAAC;QACjB,CAAC;KACD,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,21 @@
1
+ import type { WavFormat } from "../format/wav-format.js";
2
+ import type { WavEncoderOptions, WavSampleBlock } from "./wav-encoder.js";
3
+ /** What {@link encodeWav} takes. */
4
+ export interface EncodeWavInit extends Omit<WavEncoderOptions, "frameCount"> {
5
+ readonly format: WavFormat;
6
+ /** Every frame of the file, in any of the forms a block takes. */
7
+ readonly samples: WavSampleBlock;
8
+ }
9
+ /**
10
+ * Encodes a whole WAV file into one new array, allocated once at its final
11
+ * size, the samples converted straight into it: header, `data`, trailing
12
+ * chunks, every size final. What {@link WavEncoder} writes for the same frames,
13
+ * `RF64` included when the file needs it. Linear in the file's size.
14
+ *
15
+ * @throws {RangeError} When the samples are not whole frames or a sample is
16
+ * `NaN` for an integer format, or a chunk or INFO entry is malformed or one
17
+ * the encoder writes itself.
18
+ * @throws {WavLimitError} When the file is past what its container holds.
19
+ */
20
+ export declare function encodeWav(init: EncodeWavInit): Uint8Array<ArrayBuffer>;
21
+ //# sourceMappingURL=encode-wav.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encode-wav.d.ts","sourceRoot":"","sources":["../../src/encode/encode-wav.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAGzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAE1E,oCAAoC;AACpC,MAAM,WAAW,aAAc,SAAQ,IAAI,CAAC,iBAAiB,EAAE,YAAY,CAAC;IAC3E,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,kEAAkE;IAClE,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;CACjC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,aAAa,GAAG,UAAU,CAAC,WAAW,CAAC,CAkBtE"}
@@ -0,0 +1,26 @@
1
+ import { blockFrameCount_, writeBlock_ } from "./_sample-block.js";
2
+ import { WavLayout_ } from "./_wav-layout.js";
3
+ /**
4
+ * Encodes a whole WAV file into one new array, allocated once at its final
5
+ * size, the samples converted straight into it: header, `data`, trailing
6
+ * chunks, every size final. What {@link WavEncoder} writes for the same frames,
7
+ * `RF64` included when the file needs it. Linear in the file's size.
8
+ *
9
+ * @throws {RangeError} When the samples are not whole frames or a sample is
10
+ * `NaN` for an integer format, or a chunk or INFO entry is malformed or one
11
+ * the encoder writes itself.
12
+ * @throws {WavLimitError} When the file is past what its container holds.
13
+ */
14
+ export function encodeWav(init) {
15
+ const { format, samples } = init;
16
+ const frameCount = blockFrameCount_("encodeWav", format, samples);
17
+ const layout = new WavLayout_(format, init, frameCount);
18
+ const dataBytes = layout.dataBytes(frameCount);
19
+ const dataEnd = layout.headerBytes + dataBytes;
20
+ const bytes = new Uint8Array(dataEnd + layout.trailerBytes(frameCount));
21
+ layout.writeHeader(bytes, frameCount);
22
+ writeBlock_(format, samples, frameCount, bytes.subarray(layout.headerBytes, dataEnd));
23
+ layout.writeTrailer(bytes, dataEnd, frameCount);
24
+ return bytes;
25
+ }
26
+ //# sourceMappingURL=encode-wav.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encode-wav.js","sourceRoot":"","sources":["../../src/encode/encode-wav.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAU9C;;;;;;;;;;GAUG;AACH,MAAM,UAAU,SAAS,CAAC,IAAmB;IAC5C,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IACjC,MAAM,UAAU,GAAG,gBAAgB,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IAClE,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC;IACxD,MAAM,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;IAC/C,MAAM,OAAO,GAAG,MAAM,CAAC,WAAW,GAAG,SAAS,CAAC;IAC/C,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,OAAO,GAAG,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC,CAAC;IAExE,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;IACtC,WAAW,CACV,MAAM,EACN,OAAO,EACP,UAAU,EACV,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,CAC3C,CAAC;IACF,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC;IAEhD,OAAO,KAAK,CAAC;AACd,CAAC"}
@@ -0,0 +1,28 @@
1
+ import type { WavFormat } from "../format/wav-format.js";
2
+ import { type WavEncoderOptions, type WavSampleBlock } from "./wav-encoder.js";
3
+ /** Options of {@link WavEncodeStream}: the frame count is required. */
4
+ export interface WavEncodeStreamOptions extends WavEncoderOptions {
5
+ readonly frameCount: number;
6
+ }
7
+ /**
8
+ * Blocks of samples in, the bytes of a WAV file out, for a destination that
9
+ * cannot be rewound — a pipe, a socket, an HTTP body: the frame count is given
10
+ * up front, so the header the stream starts with is final. Writing more or
11
+ * fewer frames errors the stream with a {@link WavFrameCountError}. Abort it
12
+ * through the `signal` of `pipeTo` or `pipeThrough`.
13
+ *
14
+ * Each block's bytes are one chunk out (a `Uint8Array` block is passed on as it
15
+ * is), after a first chunk holding the header; the trailer, when there is one,
16
+ * is the last.
17
+ */
18
+ export declare class WavEncodeStream extends TransformStream<WavSampleBlock, Uint8Array<ArrayBuffer>> {
19
+ /**
20
+ * @throws {TypeError} When `options.frameCount` is missing.
21
+ * @throws {RangeError} When an option or the format is invalid, as
22
+ * {@link WavEncoder}'s constructor.
23
+ * @throws {WavLimitError} When the frame count makes a file past what its
24
+ * container holds.
25
+ */
26
+ constructor(format: WavFormat, options: WavEncodeStreamOptions);
27
+ }
28
+ //# sourceMappingURL=wav-encode-stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wav-encode-stream.d.ts","sourceRoot":"","sources":["../../src/encode/wav-encode-stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAEN,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACnB,MAAM,kBAAkB,CAAC;AAE1B,uEAAuE;AACvE,MAAM,WAAW,sBAAuB,SAAQ,iBAAiB;IAChE,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;;;;GAUG;AACH,qBAAa,eAAgB,SAAQ,eAAe,CACnD,cAAc,EACd,UAAU,CAAC,WAAW,CAAC,CACvB;IACA;;;;;;OAMG;IACH,YAAY,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,sBAAsB,EA6B7D;CACD"}
@@ -0,0 +1,46 @@
1
+ import { WavEncoder, } from "./wav-encoder.js";
2
+ /**
3
+ * Blocks of samples in, the bytes of a WAV file out, for a destination that
4
+ * cannot be rewound — a pipe, a socket, an HTTP body: the frame count is given
5
+ * up front, so the header the stream starts with is final. Writing more or
6
+ * fewer frames errors the stream with a {@link WavFrameCountError}. Abort it
7
+ * through the `signal` of `pipeTo` or `pipeThrough`.
8
+ *
9
+ * Each block's bytes are one chunk out (a `Uint8Array` block is passed on as it
10
+ * is), after a first chunk holding the header; the trailer, when there is one,
11
+ * is the last.
12
+ */
13
+ export class WavEncodeStream extends TransformStream {
14
+ /**
15
+ * @throws {TypeError} When `options.frameCount` is missing.
16
+ * @throws {RangeError} When an option or the format is invalid, as
17
+ * {@link WavEncoder}'s constructor.
18
+ * @throws {WavLimitError} When the frame count makes a file past what its
19
+ * container holds.
20
+ */
21
+ constructor(format, options) {
22
+ // Without it the header would hold placeholders nothing could patch.
23
+ if (options.frameCount === undefined) {
24
+ throw new TypeError("WavEncodeStream: a stream cannot be rewound, so frameCount is required; use createWavWritableStream for a seekable sink");
25
+ }
26
+ const encoder = new WavEncoder(format, options);
27
+ super({
28
+ start(controller) {
29
+ controller.enqueue(encoder.header);
30
+ },
31
+ transform(block, controller) {
32
+ const bytes = encoder.encode(block);
33
+ if (bytes.length > 0) {
34
+ controller.enqueue(bytes);
35
+ }
36
+ },
37
+ flush(controller) {
38
+ const { trailer } = encoder.finish();
39
+ if (trailer.length > 0) {
40
+ controller.enqueue(trailer);
41
+ }
42
+ },
43
+ });
44
+ }
45
+ }
46
+ //# sourceMappingURL=wav-encode-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wav-encode-stream.js","sourceRoot":"","sources":["../../src/encode/wav-encode-stream.ts"],"names":[],"mappings":"AACA,OAAO,EACN,UAAU,GAGV,MAAM,kBAAkB,CAAC;AAO1B;;;;;;;;;;GAUG;AACH,MAAM,OAAO,eAAgB,SAAQ,eAGpC;IACA;;;;;;OAMG;IACH,YAAY,MAAiB,EAAE,OAA+B;QAC7D,qEAAqE;QACrE,IAAK,OAA2C,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;YAC3E,MAAM,IAAI,SAAS,CAClB,yHAAyH,CACzH,CAAC;QACH,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAEhD,KAAK,CAAC;YACL,KAAK,CAAC,UAAU;gBACf,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YACpC,CAAC;YACD,SAAS,CAAC,KAAK,EAAE,UAAU;gBAC1B,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAEpC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACtB,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;gBAC3B,CAAC;YACF,CAAC;YACD,KAAK,CAAC,UAAU;gBACf,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;gBAErC,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACxB,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;gBAC7B,CAAC;YACF,CAAC;SACD,CAAC,CAAC;IACJ,CAAC;CACD"}
@@ -0,0 +1,128 @@
1
+ import type { WavFormat } from "../format/wav-format.js";
2
+ import type { WavChunk, WavInfoEntry } from "../riff/wav-chunk.js";
3
+ /**
4
+ * When to write the 64-bit `RF64` container (EBU Tech 3306), whose sizes are in
5
+ * a `ds64` chunk, rather than `RIFF`, whose sizes stop at 4 GiB:
6
+ *
7
+ * - `auto` — only when the file needs it. When its length is not known up front,
8
+ * the header reserves the place of the `ds64` chunk with a `JUNK` chunk,
9
+ * which every reader skips, and the sizes written at the end turn it into
10
+ * `ds64` if the file has grown past 4 GiB.
11
+ * - `never` — always `RIFF`: a file past 4 GiB is a {@link WavLimitError}, and no
12
+ * `JUNK` chunk is written.
13
+ * - `always` — `RF64` whatever the size.
14
+ */
15
+ export type WavRf64Mode = "auto" | "never" | "always";
16
+ /** One write that puts a final size in a header written before it was known. */
17
+ export interface WavPatch {
18
+ /** Byte offset from the start of the file. */
19
+ readonly position: number;
20
+ readonly bytes: Uint8Array<ArrayBuffer>;
21
+ }
22
+ /**
23
+ * Samples to encode, whole frames:
24
+ *
25
+ * - A `Uint8Array` — frames already in the file's sample format, as they go in
26
+ * the `data` chunk, written as they are;
27
+ * - A `Float32Array` or `Float64Array` — normalised samples, interleaved;
28
+ * - An array of `Float32Array` or `Float64Array`, one per channel, all of the
29
+ * same length — normalised samples, planar (an `AudioBuffer`'s channels).
30
+ *
31
+ * Normalised samples are converted as {@link writeWavSamples} does.
32
+ */
33
+ export type WavSampleBlock = Uint8Array<ArrayBuffer> | Float32Array | Float64Array | readonly (Float32Array | Float64Array)[];
34
+ /** Options of {@link WavEncoder} and of every writer built on it. */
35
+ export interface WavEncoderOptions {
36
+ /**
37
+ * The frames the file will hold, when known up front: the header is then
38
+ * final, and encoding more or fewer frames is a {@link WavFrameCountError}.
39
+ * Without it, the header holds placeholder sizes that
40
+ * {@link WavEncoder.finish} returns the patches for, which only a seekable
41
+ * destination can apply.
42
+ */
43
+ readonly frameCount?: number;
44
+ /** Text entries, written as a `LIST`/`INFO` chunk before the data. */
45
+ readonly info?: readonly WavInfoEntry[];
46
+ /** Other chunks, written in order before the data. */
47
+ readonly chunks?: readonly WavChunk[];
48
+ /** Other chunks, written in order after the data. */
49
+ readonly trailingChunks?: readonly WavChunk[];
50
+ /** When to write `RF64` rather than `RIFF`. Default `auto`. */
51
+ readonly rf64?: WavRf64Mode;
52
+ }
53
+ /** What {@link WavEncoder.finish} returns. */
54
+ export interface WavEncoderEnd {
55
+ /** The last bytes of the file: the data's pad byte, the trailing chunks. */
56
+ readonly trailer: Uint8Array<ArrayBuffer>;
57
+ /**
58
+ * The writes that replace the placeholder sizes of the header, to apply after
59
+ * the trailer; empty when the frame count was given up front.
60
+ */
61
+ readonly patches: readonly WavPatch[];
62
+ }
63
+ /**
64
+ * Encodes a WAV file a block of samples at a time, performing no I/O: the
65
+ * caller writes {@link header}, then what each {@link encode} returns, then the
66
+ * trailer {@link finish} returns, and applies its patches.
67
+ *
68
+ * With `frameCount`, every size is in the header and the bytes can go to any
69
+ * destination, a pipe or a socket. Without it, the header holds placeholders
70
+ * and the patches put the sizes in place once the last frame is known: a
71
+ * recording of unknown length, to a file or a buffer. The 4 GiB of `RIFF` is no
72
+ * limit in either case: see {@link WavRf64Mode}.
73
+ *
74
+ * Writes the `fmt ` chunk the format calls for (plain PCM, plain float with
75
+ * `cbSize` 0, or `WAVE_FORMAT_EXTENSIBLE`), a `fact` chunk for every format but
76
+ * plain PCM, as the RIFF specification asks, then the `LIST`/`INFO` chunk and
77
+ * the other chunks given, the data, its pad byte when it is odd-sized, and the
78
+ * trailing chunks.
79
+ */
80
+ export declare class WavEncoder {
81
+ readonly format: WavFormat;
82
+ /** The bytes that start the file, through the `data` chunk's header. */
83
+ readonly header: Uint8Array<ArrayBuffer>;
84
+ private readonly layout;
85
+ private readonly declaredFrameCount;
86
+ private frames;
87
+ private isFinished;
88
+ /**
89
+ * @throws {RangeError} When a chunk or an INFO entry is malformed or one the
90
+ * encoder writes itself, `frameCount` is not a non-negative integer, or the
91
+ * format's byte rate is past 2^32 − 1.
92
+ * @throws {WavLimitError} When `frameCount` makes a file past 4 GiB and
93
+ * `rf64` is `never`, or past 2^53 bytes.
94
+ */
95
+ constructor(format: WavFormat, options?: WavEncoderOptions);
96
+ /** Frames encoded so far. */
97
+ get frameCount(): number;
98
+ /**
99
+ * Encodes a block of whole frames into the bytes of the `data` chunk that
100
+ * follow the previous block's: the block itself when it is a `Uint8Array`, a
101
+ * new array otherwise.
102
+ *
103
+ * @throws {RangeError} When the block is not whole frames, its channels
104
+ * differ in length or number from the format's, or a sample is `NaN` for an
105
+ * integer format.
106
+ * @throws {WavFrameCountError} When the block takes the file past the frame
107
+ * count given up front.
108
+ * @throws {WavLimitError} When the block takes the file past what its
109
+ * container holds.
110
+ * @throws {TypeError} After {@link finish}.
111
+ */
112
+ encode(block: WavSampleBlock): Uint8Array<ArrayBuffer>;
113
+ private checkGrowth_;
114
+ /**
115
+ * Ends the file: returns its last bytes and, when its frame count was not
116
+ * given up front, the patches that put the sizes in the header.
117
+ *
118
+ * @param trailingChunks Chunks to write after the data, besides the option's;
119
+ * only for a file whose sizes are patched.
120
+ * @throws {WavFrameCountError} When fewer frames were encoded than the frame
121
+ * count given up front.
122
+ * @throws {WavLimitError} When the file is past what its container holds.
123
+ * @throws {TypeError} When called twice, or given trailing chunks for a file
124
+ * whose header is final. The encoder is finished after any error.
125
+ */
126
+ finish(trailingChunks?: readonly WavChunk[]): WavEncoderEnd;
127
+ }
128
+ //# sourceMappingURL=wav-encoder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wav-encoder.d.ts","sourceRoot":"","sources":["../../src/encode/wav-encoder.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAInE;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEtD,gFAAgF;AAChF,MAAM,WAAW,QAAQ;IACxB,8CAA8C;IAC9C,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;CACxC;AAED;;;;;;;;;;GAUG;AACH,MAAM,MAAM,cAAc,GACvB,UAAU,CAAC,WAAW,CAAC,GACvB,YAAY,GACZ,YAAY,GACZ,SAAS,CAAC,YAAY,GAAG,YAAY,CAAC,EAAE,CAAC;AAE5C,qEAAqE;AACrE,MAAM,WAAW,iBAAiB;IACjC;;;;;;OAMG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAE7B,sEAAsE;IACtE,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,YAAY,EAAE,CAAC;IAExC,sDAAsD;IACtD,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,QAAQ,EAAE,CAAC;IAEtC,qDAAqD;IACrD,QAAQ,CAAC,cAAc,CAAC,EAAE,SAAS,QAAQ,EAAE,CAAC;IAE9C,+DAA+D;IAC/D,QAAQ,CAAC,IAAI,CAAC,EAAE,WAAW,CAAC;CAC5B;AAED,8CAA8C;AAC9C,MAAM,WAAW,aAAa;IAC7B,4EAA4E;IAC5E,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;IAE1C;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,SAAS,QAAQ,EAAE,CAAC;CACtC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,UAAU;IAiBrB,QAAQ,CAAC,MAAM,EAAE,SAAS;IAhB3B,wEAAwE;IACxE,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;IAEzC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAa;IACpC,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAgB;IACnD,OAAO,CAAC,MAAM,CAAK;IACnB,OAAO,CAAC,UAAU,CAAS;IAE3B;;;;;;OAMG;IACH,YACU,MAAM,EAAE,SAAS,EAC1B,OAAO,CAAC,EAAE,iBAAiB,EAiB3B;IAED,6BAA6B;IAC7B,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAC,KAAK,EAAE,cAAc,GAAG,UAAU,CAAC,WAAW,CAAC,CAsBrD;IAED,OAAO,CAAC,YAAY;IAUpB;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,cAAc,CAAC,EAAE,SAAS,QAAQ,EAAE,GAAG,aAAa,CA0B1D;CACD"}
@@ -0,0 +1,125 @@
1
+ import { WavFrameCountError } from "../errors.js";
2
+ import { blockFrameCount_, writeBlock_ } from "./_sample-block.js";
3
+ import { WavLayout_ } from "./_wav-layout.js";
4
+ /**
5
+ * Encodes a WAV file a block of samples at a time, performing no I/O: the
6
+ * caller writes {@link header}, then what each {@link encode} returns, then the
7
+ * trailer {@link finish} returns, and applies its patches.
8
+ *
9
+ * With `frameCount`, every size is in the header and the bytes can go to any
10
+ * destination, a pipe or a socket. Without it, the header holds placeholders
11
+ * and the patches put the sizes in place once the last frame is known: a
12
+ * recording of unknown length, to a file or a buffer. The 4 GiB of `RIFF` is no
13
+ * limit in either case: see {@link WavRf64Mode}.
14
+ *
15
+ * Writes the `fmt ` chunk the format calls for (plain PCM, plain float with
16
+ * `cbSize` 0, or `WAVE_FORMAT_EXTENSIBLE`), a `fact` chunk for every format but
17
+ * plain PCM, as the RIFF specification asks, then the `LIST`/`INFO` chunk and
18
+ * the other chunks given, the data, its pad byte when it is odd-sized, and the
19
+ * trailing chunks.
20
+ */
21
+ export class WavEncoder {
22
+ format;
23
+ /** The bytes that start the file, through the `data` chunk's header. */
24
+ header;
25
+ layout;
26
+ declaredFrameCount;
27
+ frames = 0;
28
+ isFinished = false;
29
+ /**
30
+ * @throws {RangeError} When a chunk or an INFO entry is malformed or one the
31
+ * encoder writes itself, `frameCount` is not a non-negative integer, or the
32
+ * format's byte rate is past 2^32 − 1.
33
+ * @throws {WavLimitError} When `frameCount` makes a file past 4 GiB and
34
+ * `rf64` is `never`, or past 2^53 bytes.
35
+ */
36
+ constructor(format, options) {
37
+ this.format = format;
38
+ const frameCount = options?.frameCount ?? null;
39
+ if (frameCount !== null &&
40
+ (!Number.isSafeInteger(frameCount) || frameCount < 0)) {
41
+ throw new RangeError(`WavEncoder: frameCount must be a non-negative integer, got ${frameCount}`);
42
+ }
43
+ this.declaredFrameCount = frameCount;
44
+ this.layout = new WavLayout_(format, options, frameCount);
45
+ this.header = new Uint8Array(this.layout.headerBytes);
46
+ this.layout.writeHeader(this.header, frameCount);
47
+ }
48
+ /** Frames encoded so far. */
49
+ get frameCount() {
50
+ return this.frames;
51
+ }
52
+ /**
53
+ * Encodes a block of whole frames into the bytes of the `data` chunk that
54
+ * follow the previous block's: the block itself when it is a `Uint8Array`, a
55
+ * new array otherwise.
56
+ *
57
+ * @throws {RangeError} When the block is not whole frames, its channels
58
+ * differ in length or number from the format's, or a sample is `NaN` for an
59
+ * integer format.
60
+ * @throws {WavFrameCountError} When the block takes the file past the frame
61
+ * count given up front.
62
+ * @throws {WavLimitError} When the block takes the file past what its
63
+ * container holds.
64
+ * @throws {TypeError} After {@link finish}.
65
+ */
66
+ encode(block) {
67
+ if (this.isFinished) {
68
+ throw new TypeError("WavEncoder.encode: the encoder has finished");
69
+ }
70
+ const { format } = this;
71
+ const frameCount = blockFrameCount_("WavEncoder.encode", format, block);
72
+ this.checkGrowth_(this.frames + frameCount);
73
+ let bytes;
74
+ if (block instanceof Uint8Array) {
75
+ bytes = block;
76
+ }
77
+ else {
78
+ bytes = new Uint8Array(frameCount * format.blockAlign);
79
+ writeBlock_(format, block, frameCount, bytes);
80
+ }
81
+ this.frames += frameCount;
82
+ return bytes;
83
+ }
84
+ checkGrowth_(frameCount) {
85
+ if (this.declaredFrameCount !== null &&
86
+ frameCount > this.declaredFrameCount) {
87
+ throw new WavFrameCountError(this.declaredFrameCount, frameCount);
88
+ }
89
+ this.layout.checkGrowth(frameCount);
90
+ }
91
+ /**
92
+ * Ends the file: returns its last bytes and, when its frame count was not
93
+ * given up front, the patches that put the sizes in the header.
94
+ *
95
+ * @param trailingChunks Chunks to write after the data, besides the option's;
96
+ * only for a file whose sizes are patched.
97
+ * @throws {WavFrameCountError} When fewer frames were encoded than the frame
98
+ * count given up front.
99
+ * @throws {WavLimitError} When the file is past what its container holds.
100
+ * @throws {TypeError} When called twice, or given trailing chunks for a file
101
+ * whose header is final. The encoder is finished after any error.
102
+ */
103
+ finish(trailingChunks) {
104
+ if (this.isFinished) {
105
+ throw new TypeError("WavEncoder.finish: the encoder has finished");
106
+ }
107
+ this.isFinished = true;
108
+ if (this.declaredFrameCount !== null) {
109
+ if (this.frames !== this.declaredFrameCount) {
110
+ throw new WavFrameCountError(this.declaredFrameCount, this.frames);
111
+ }
112
+ if (trailingChunks !== undefined && trailingChunks.length > 0) {
113
+ throw new TypeError("WavEncoder.finish: a header with a frame count is final; give trailing chunks as an option");
114
+ }
115
+ }
116
+ if (trailingChunks !== undefined) {
117
+ this.layout.addTrailingChunks(trailingChunks);
118
+ }
119
+ const patches = this.declaredFrameCount === null ? this.layout.patches(this.frames) : [];
120
+ const trailer = new Uint8Array(this.layout.trailerBytes(this.frames));
121
+ this.layout.writeTrailer(trailer, 0, this.frames);
122
+ return { trailer, patches };
123
+ }
124
+ }
125
+ //# sourceMappingURL=wav-encoder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wav-encoder.js","sourceRoot":"","sources":["../../src/encode/wav-encoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAGlD,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AA4E9C;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,UAAU;IAiBZ,MAAM;IAhBhB,wEAAwE;IAC/D,MAAM,CAA0B;IAExB,MAAM,CAAa;IACnB,kBAAkB,CAAgB;IAC3C,MAAM,GAAG,CAAC,CAAC;IACX,UAAU,GAAG,KAAK,CAAC;IAE3B;;;;;;OAMG;IACH,YACU,MAAiB,EAC1B,OAA2B;sBADlB,MAAM;QAGf,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,IAAI,IAAI,CAAC;QAE/C,IACC,UAAU,KAAK,IAAI;YACnB,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC,IAAI,UAAU,GAAG,CAAC,CAAC,EACpD,CAAC;YACF,MAAM,IAAI,UAAU,CACnB,8DAA8D,UAAU,EAAE,CAC1E,CAAC;QACH,CAAC;QAED,IAAI,CAAC,kBAAkB,GAAG,UAAU,CAAC;QACrC,IAAI,CAAC,MAAM,GAA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@@ -0,0 +1,39 @@
1
+ /**
2
+ * A WAV file whose structure is broken or contradicts itself: not RIFF/WAVE, a
3
+ * chunk header cut short, a `fmt ` chunk missing, too short or inconsistent, a
4
+ * `data` chunk before the `fmt ` chunk, an RF64 file without its `ds64` chunk.
5
+ * In strict mode, also every defect a {@link WavWarning} would report.
6
+ *
7
+ * Thrown by {@link parseWav}, {@link WavDecoder} and {@link decodeWavStream}.
8
+ */
9
+ export declare class WavFormatError extends SyntaxError {
10
+ /** Byte offset in the file where reading gave up. */
11
+ readonly position: number;
12
+ constructor(message: string,
13
+ /** Byte offset in the file where reading gave up. */
14
+ position: number);
15
+ }
16
+ /**
17
+ * A size past a limit: a chunk the decoder must hold past its
18
+ * `maxChunkByteLength`, a size past `Number.MAX_SAFE_INTEGER`, or a file
19
+ * written past 4 GiB with `rf64: "never"`.
20
+ */
21
+ export declare class WavLimitError extends RangeError {
22
+ constructor(message: string);
23
+ }
24
+ /**
25
+ * An encoder given more or fewer frames than the `frameCount` it announced in
26
+ * its header, which a sink that cannot be rewound cannot correct.
27
+ */
28
+ export declare class WavFrameCountError extends RangeError {
29
+ /** The frame count the header announced. */
30
+ readonly expected: number;
31
+ /** The frame count written, so far or at the end. */
32
+ readonly actual: number;
33
+ constructor(
34
+ /** The frame count the header announced. */
35
+ expected: number,
36
+ /** The frame count written, so far or at the end. */
37
+ actual: number);
38
+ }
39
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,qBAAa,cAAe,SAAQ,WAAW;IAG7C,qDAAqD;IACrD,QAAQ,CAAC,QAAQ,EAAE,MAAM;IAH1B,YACC,OAAO,EAAE,MAAM;IACf,qDAAqD;IAC5C,QAAQ,EAAE,MAAM,EAIzB;CACD;AAED;;;;GAIG;AACH,qBAAa,aAAc,SAAQ,UAAU;IAC5C,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD;AAED;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,UAAU;IAEhD,4CAA4C;IAC5C,QAAQ,CAAC,QAAQ,EAAE,MAAM;IACzB,qDAAqD;IACrD,QAAQ,CAAC,MAAM,EAAE,MAAM;IAJxB;IACC,4CAA4C;IACnC,QAAQ,EAAE,MAAM;IACzB,qDAAqD;IAC5C,MAAM,EAAE,MAAM,EAQvB;CACD"}
package/dist/errors.js ADDED
@@ -0,0 +1,50 @@
1
+ /**
2
+ * A WAV file whose structure is broken or contradicts itself: not RIFF/WAVE, a
3
+ * chunk header cut short, a `fmt ` chunk missing, too short or inconsistent, a
4
+ * `data` chunk before the `fmt ` chunk, an RF64 file without its `ds64` chunk.
5
+ * In strict mode, also every defect a {@link WavWarning} would report.
6
+ *
7
+ * Thrown by {@link parseWav}, {@link WavDecoder} and {@link decodeWavStream}.
8
+ */
9
+ export class WavFormatError extends SyntaxError {
10
+ position;
11
+ constructor(message,
12
+ /** Byte offset in the file where reading gave up. */
13
+ position) {
14
+ super(`${message} at ${position}`);
15
+ this.position = position;
16
+ this.name = "WavFormatError";
17
+ }
18
+ }
19
+ /**
20
+ * A size past a limit: a chunk the decoder must hold past its
21
+ * `maxChunkByteLength`, a size past `Number.MAX_SAFE_INTEGER`, or a file
22
+ * written past 4 GiB with `rf64: "never"`.
23
+ */
24
+ export class WavLimitError extends RangeError {
25
+ constructor(message) {
26
+ super(message);
27
+ this.name = "WavLimitError";
28
+ }
29
+ }
30
+ /**
31
+ * An encoder given more or fewer frames than the `frameCount` it announced in
32
+ * its header, which a sink that cannot be rewound cannot correct.
33
+ */
34
+ export class WavFrameCountError extends RangeError {
35
+ expected;
36
+ actual;
37
+ constructor(
38
+ /** The frame count the header announced. */
39
+ expected,
40
+ /** The frame count written, so far or at the end. */
41
+ actual) {
42
+ super(actual > expected
43
+ ? `More than the ${expected} frames announced: ${actual}`
44
+ : `Fewer than the ${expected} frames announced: ${actual}`);
45
+ this.expected = expected;
46
+ this.actual = actual;
47
+ this.name = "WavFrameCountError";
48
+ }
49
+ }
50
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,OAAO,cAAe,SAAQ,WAAW;IAIpC,QAAQ;IAHlB,YACC,OAAe;IACf,qDAAqD;IAC5C,QAAgB;QAEzB,KAAK,CAAC,GAAG,OAAO,OAAO,QAAQ,EAAE,CAAC,CAAC;wBAF1B,QAAQ;QAGjB,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC9B,CAAC;CACD;AAED;;;;GAIG;AACH,MAAM,OAAO,aAAc,SAAQ,UAAU;IAC5C,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC7B,CAAC;CACD;AAED;;;GAGG;AACH,MAAM,OAAO,kBAAmB,SAAQ,UAAU;IAGvC,QAAQ;IAER,MAAM;IAJhB;IACC,4CAA4C;IACnC,QAAgB;IACzB,qDAAqD;IAC5C,MAAc;QAEvB,KAAK,CACJ,MAAM,GAAG,QAAQ;YAChB,CAAC,CAAC,iBAAiB,QAAQ,sBAAsB,MAAM,EAAE;YACzD,CAAC,CAAC,kBAAkB,QAAQ,sBAAsB,MAAM,EAAE,CAC3D,CAAC;wBARO,QAAQ;sBAER,MAAM;QAOf,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IAClC,CAAC;CACD"}
@@ -0,0 +1,36 @@
1
+ import type { WavFormatChunk } from "./wav-format-chunk.js";
2
+ import { WavFormatTag, type WavFormat } from "./wav-format.js";
3
+ /** The longest `fmt ` payload: `WAVEFORMATEX` and the most `cbSize` holds. */
4
+ export declare const MAX_FORMAT_BYTES_: number;
5
+ export declare const MIN_FORMAT_BYTES_ = 16;
6
+ /** What reading a `fmt ` chunk found. */
7
+ export interface FormatRead_ {
8
+ readonly chunk: WavFormatChunk;
9
+ /** `null` for a format this package does not convert. */
10
+ readonly format: WavFormat | null;
11
+ readonly byteRateMismatch: boolean;
12
+ readonly validBitsUnset: boolean;
13
+ /** A plain chunk whose bits per sample do not fit its block alignment. */
14
+ readonly bitsPerSampleMismatch: boolean;
15
+ }
16
+ /**
17
+ * Reads a `fmt ` payload of at least 16 bytes found at file offset `position`.
18
+ *
19
+ * @throws {WavFormatError} When a field is zero that no format allows, or the
20
+ * fields of a PCM, float or extensible chunk contradict each other.
21
+ */
22
+ export declare function readFormatChunk_(payload: Uint8Array, position: number): FormatRead_;
23
+ /** The `fmt ` payload bytes {@link writeFormatChunk_} writes for `format`. */
24
+ export declare function formatChunkBytes_(format: WavFormat): number;
25
+ /** The format tag {@link writeFormatChunk_} writes for `format`. */
26
+ export declare function formatTagOf_(format: WavFormat): WavFormatTag;
27
+ /**
28
+ * Writes the `fmt ` payload of `format` at `offset`: 16 bytes for plain PCM
29
+ * (`PCMWAVEFORMAT`), 18 for plain float (`WAVEFORMATEX`, `cbSize` 0), 40 for
30
+ * `WAVE_FORMAT_EXTENSIBLE`. A plain PCM chunk writes the valid bits as its bits
31
+ * per sample, its containers being the block alignment's.
32
+ *
33
+ * @throws {RangeError} When the byte rate is past 2^32 − 1.
34
+ */
35
+ export declare function writeFormatChunk_(format: WavFormat, bytes: Uint8Array, offset: number): void;
36
+ //# sourceMappingURL=_fmt-chunk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_fmt-chunk.d.ts","sourceRoot":"","sources":["../../src/format/_fmt-chunk.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAKN,YAAY,EACZ,KAAK,SAAS,EAEd,MAAM,iBAAiB,CAAC;AAMzB,8EAA8E;AAC9E,eAAO,MAAM,iBAAiB,QAA4B,CAAC;AAC3D,eAAO,MAAM,iBAAiB,KAAoB,CAAC;AAYnD,yCAAyC;AACzC,MAAM,WAAW,WAAW;IAC3B,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;IAC/B,yDAAyD;IACzD,QAAQ,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAAC;IAClC,QAAQ,CAAC,gBAAgB,EAAE,OAAO,CAAC;IACnC,QAAQ,CAAC,cAAc,EAAE,OAAO,CAAC;IACjC,0EAA0E;IAC1E,QAAQ,CAAC,qBAAqB,EAAE,OAAO,CAAC;CACxC;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC/B,OAAO,EAAE,UAAU,EACnB,QAAQ,EAAE,MAAM,GACd,WAAW,CAwJb;AAED,8EAA8E;AAC9E,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAM3D;AAED,oEAAoE;AACpE,wBAAgB,YAAY,CAAC,MAAM,EAAE,SAAS,GAAG,YAAY,CAM5D;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,SAAS,EACjB,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,MAAM,GACZ,IAAI,CAiCN"}