@thi.ng/dsp-io-wav 2.1.81 → 2.1.82
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 +1 -1
- package/api.js +0 -1
- package/package.json +11 -8
- package/write.js +66 -71
package/CHANGELOG.md
CHANGED
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.
|
|
3
|
+
"version": "2.1.82",
|
|
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
|
|
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.
|
|
37
|
-
"@thi.ng/binary": "^3.
|
|
38
|
-
"@thi.ng/errors": "^2.4.
|
|
39
|
-
"@thi.ng/transducers": "^8.8.
|
|
40
|
-
"@thi.ng/transducers-binary": "^2.1.
|
|
38
|
+
"@thi.ng/api": "^8.9.12",
|
|
39
|
+
"@thi.ng/binary": "^3.4.0",
|
|
40
|
+
"@thi.ng/errors": "^2.4.6",
|
|
41
|
+
"@thi.ng/transducers": "^8.8.15",
|
|
42
|
+
"@thi.ng/transducers-binary": "^2.1.82"
|
|
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",
|
|
@@ -87,5 +90,5 @@
|
|
|
87
90
|
"parent": "@thi.ng/dsp",
|
|
88
91
|
"year": 2020
|
|
89
92
|
},
|
|
90
|
-
"gitHead": "
|
|
93
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\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 {
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
* spec
|
|
48
|
-
|
|
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
};
|