@thi.ng/dsp-io-wav 2.1.81 → 2.1.83

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 (4) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/api.js +0 -1
  3. package/package.json +12 -9
  4. package/write.js +66 -71
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-09T19:12:03Z
3
+ - **Last updated**: 2023-12-18T13:41:19Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
package/api.js CHANGED
@@ -1 +0,0 @@
1
- export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/dsp-io-wav",
3
- "version": "2.1.81",
3
+ "version": "2.1.83",
4
4
  "description": "WAV file format generation",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -24,7 +24,9 @@
24
24
  "author": "Karsten Schmidt (https://thi.ng)",
25
25
  "license": "Apache-2.0",
26
26
  "scripts": {
27
- "build": "yarn clean && tsc --declaration",
27
+ "build": "yarn build:esbuild && yarn build:decl",
28
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
29
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
28
30
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
29
31
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
32
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
@@ -33,14 +35,15 @@
33
35
  "test": "bun test"
34
36
  },
35
37
  "dependencies": {
36
- "@thi.ng/api": "^8.9.11",
37
- "@thi.ng/binary": "^3.3.40",
38
- "@thi.ng/errors": "^2.4.5",
39
- "@thi.ng/transducers": "^8.8.14",
40
- "@thi.ng/transducers-binary": "^2.1.81"
38
+ "@thi.ng/api": "^8.9.13",
39
+ "@thi.ng/binary": "^3.4.1",
40
+ "@thi.ng/errors": "^2.4.7",
41
+ "@thi.ng/transducers": "^8.8.16",
42
+ "@thi.ng/transducers-binary": "^2.1.83"
41
43
  },
42
44
  "devDependencies": {
43
45
  "@microsoft/api-extractor": "^7.38.3",
46
+ "esbuild": "^0.19.8",
44
47
  "rimraf": "^5.0.5",
45
48
  "tools": "^0.0.1",
46
49
  "typedoc": "^0.25.4",
@@ -66,7 +69,7 @@
66
69
  "access": "public"
67
70
  },
68
71
  "engines": {
69
- "node": ">=12.7"
72
+ "node": ">=18"
70
73
  },
71
74
  "files": [
72
75
  "./*.js",
@@ -87,5 +90,5 @@
87
90
  "parent": "@thi.ng/dsp",
88
91
  "year": 2020
89
92
  },
90
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
93
+ "gitHead": "25a42a81fac8603a1e440a7aa8bc343276211ff4\n"
91
94
  }
package/write.js CHANGED
@@ -1,6 +1,13 @@
1
1
  import { f32u16, f32u24, f32u32, f32u8 } from "@thi.ng/binary/float";
2
2
  import { assert } from "@thi.ng/errors/assert";
3
- import { asBytes, bytes, u16, u24, u32, u8, } from "@thi.ng/transducers-binary/bytes";
3
+ import {
4
+ asBytes,
5
+ bytes,
6
+ u16,
7
+ u24,
8
+ u32,
9
+ u8
10
+ } from "@thi.ng/transducers-binary/bytes";
4
11
  import { comp } from "@thi.ng/transducers/comp";
5
12
  import { concat } from "@thi.ng/transducers/concat";
6
13
  import { iterator } from "@thi.ng/transducers/iterator";
@@ -9,77 +16,65 @@ import { reduce } from "@thi.ng/transducers/reduce";
9
16
  import { take } from "@thi.ng/transducers/take";
10
17
  const HEADER_SIZE = 44;
11
18
  const CONVERTERS = {
12
- 8: (x) => u8(f32u8(x)),
13
- 16: (x) => u16(f32u16(x), true),
14
- 24: (x) => u24(f32u24(x), true),
15
- 32: (x) => u32(f32u32(x), true),
19
+ 8: (x) => u8(f32u8(x)),
20
+ 16: (x) => u16(f32u16(x), true),
21
+ 24: (x) => u24(f32u24(x), true),
22
+ 32: (x) => u32(f32u32(x), true)
16
23
  };
17
- export const wavHeader = (spec) => {
18
- const bytesPerSample = spec.bits >> 3;
19
- const blockAlign = spec.channels * bytesPerSample;
20
- const dataLength = spec.length * blockAlign;
21
- return [
22
- u32(0x52494646, false), // 'RIFF'
23
- u32(dataLength + HEADER_SIZE - 8, true), // riff len
24
- u32(0x57415645, false), // 'WAVE'
25
- u32(0x666d7420, false), // 'fmt '
26
- u32(16, true), // fmt len,
27
- u16(1, true), // audio format id
28
- u16(spec.channels, true),
29
- u32(spec.sampleRate, true),
30
- u32(spec.sampleRate * blockAlign, true), // byte rate
31
- u16(blockAlign, true),
32
- u16(spec.bits, true),
33
- u32(0x64617461, false), // 'data'
34
- u32(dataLength, true),
35
- ];
24
+ const wavHeader = (spec) => {
25
+ const bytesPerSample = spec.bits >> 3;
26
+ const blockAlign = spec.channels * bytesPerSample;
27
+ const dataLength = spec.length * blockAlign;
28
+ return [
29
+ u32(1380533830, false),
30
+ // 'RIFF'
31
+ u32(dataLength + HEADER_SIZE - 8, true),
32
+ // riff len
33
+ u32(1463899717, false),
34
+ // 'WAVE'
35
+ u32(1718449184, false),
36
+ // 'fmt '
37
+ u32(16, true),
38
+ // fmt len,
39
+ u16(1, true),
40
+ // audio format id
41
+ u16(spec.channels, true),
42
+ u32(spec.sampleRate, true),
43
+ u32(spec.sampleRate * blockAlign, true),
44
+ // byte rate
45
+ u16(blockAlign, true),
46
+ u16(spec.bits, true),
47
+ u32(1684108385, false),
48
+ // 'data'
49
+ u32(dataLength, true)
50
+ ];
36
51
  };
37
- /**
38
- * Takes a {@link WavSpec} and iterable of normalized float samples,
39
- * creates WAV format header, converts samples to specified bit depth
40
- * and returns `Uint8Array` of complete WAV file, ready for export.
41
- *
42
- * @remarks
43
- * If using more than one channel, the source samples need to be already
44
- * interleaved, i.e. for stereo: left, right, left, right etc.
45
- *
46
- * At most, only the number of samples specified in the given header
47
- * spec are consumed & written. The source iterable does not need to be
48
- * limited explicitly.
49
- *
50
- * @example
51
- * ```ts
52
- * import { osc, sin } from "@thi.ng/dsp";
53
- *
54
- * const FS = 48000;
55
- *
56
- * // write 1 second 24bit mono WAV file, 440Hz sine
57
- * fs.writeFileSync(
58
- * "sine-440.wav",
59
- * wavByteArray(
60
- * { sampleRate: FS, channels: 1, length: FS, bits: 24 },
61
- * osc(sin, 440 / FS)
62
- * )
63
- * )
64
- * ```
65
- *
66
- * @param spec -
67
- * @param src -
68
- */
69
- export const wavByteArray = (spec, src) => {
70
- const convert = CONVERTERS[spec.bits];
71
- assert(!!convert, `unsupported bits/sample: ${spec.bits}`);
72
- return reduce(bytes(), new Uint8Array(HEADER_SIZE + spec.length * spec.channels * (spec.bits >> 3)), concat(wavHeader(spec), iterator(comp(take(spec.length * spec.channels), map(convert)), src)));
52
+ const wavByteArray = (spec, src) => {
53
+ const convert = CONVERTERS[spec.bits];
54
+ assert(!!convert, `unsupported bits/sample: ${spec.bits}`);
55
+ return reduce(
56
+ bytes(),
57
+ new Uint8Array(
58
+ HEADER_SIZE + spec.length * spec.channels * (spec.bits >> 3)
59
+ ),
60
+ concat(
61
+ wavHeader(spec),
62
+ iterator(comp(take(spec.length * spec.channels), map(convert)), src)
63
+ )
64
+ );
73
65
  };
74
- /**
75
- * Similar to {@link wavByteArray}, but yields an iterator of the result
76
- * bytes, not an actual byte array.
77
- *
78
- * @param spec -
79
- * @param src -
80
- */
81
- export const wavBytes = (spec, src) => {
82
- const convert = CONVERTERS[spec.bits];
83
- assert(!!convert, `unsupported bits/sample: ${spec.bits}`);
84
- return asBytes(concat(wavHeader(spec), iterator(comp(take(spec.length * spec.channels), map(convert)), src)));
66
+ const wavBytes = (spec, src) => {
67
+ const convert = CONVERTERS[spec.bits];
68
+ assert(!!convert, `unsupported bits/sample: ${spec.bits}`);
69
+ return asBytes(
70
+ concat(
71
+ wavHeader(spec),
72
+ iterator(comp(take(spec.length * spec.channels), map(convert)), src)
73
+ )
74
+ );
75
+ };
76
+ export {
77
+ wavByteArray,
78
+ wavBytes,
79
+ wavHeader
85
80
  };