@grame/faustwasm 0.0.25

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 (111) hide show
  1. package/README.md +181 -0
  2. package/assets/standalone/faust-ui/index.css +414 -0
  3. package/assets/standalone/faust-ui/index.css.map +1 -0
  4. package/assets/standalone/faust-ui/index.d.ts +1 -0
  5. package/assets/standalone/faust-ui/index.js +3478 -0
  6. package/assets/standalone/faust-ui/index.js.map +1 -0
  7. package/assets/standalone/faustwasm/index.d.ts +1252 -0
  8. package/assets/standalone/faustwasm/index.js +3772 -0
  9. package/assets/standalone/faustwasm/index.js.map +7 -0
  10. package/assets/standalone/index.html +73 -0
  11. package/assets/standalone/index.js +198 -0
  12. package/assets/standalone/types.d.ts +9 -0
  13. package/dist/cjs/index.d.ts +1252 -0
  14. package/dist/cjs/index.js +3752 -0
  15. package/dist/cjs/index.js.map +7 -0
  16. package/dist/cjs-bundle/index.d.ts +1259 -0
  17. package/dist/cjs-bundle/index.js +11507 -0
  18. package/dist/cjs-bundle/index.js.map +7 -0
  19. package/dist/esm/index.d.ts +1252 -0
  20. package/dist/esm/index.js +3772 -0
  21. package/dist/esm/index.js.map +7 -0
  22. package/dist/esm-bundle/index.d.ts +1259 -0
  23. package/dist/esm-bundle/index.js +11495 -0
  24. package/dist/esm-bundle/index.js.map +7 -0
  25. package/fileutils.js +40 -0
  26. package/libfaust-wasm/libfaust-wasm.d.cts +3 -0
  27. package/libfaust-wasm/libfaust-wasm.d.ts +3 -0
  28. package/libfaust-wasm/libfaust-wasm.data +0 -0
  29. package/libfaust-wasm/libfaust-wasm.data.d.ts +2 -0
  30. package/libfaust-wasm/libfaust-wasm.js +21 -0
  31. package/libfaust-wasm/libfaust-wasm.wasm +0 -0
  32. package/libfaust-wasm/libfaust-wasm.wasm.d.ts +2 -0
  33. package/library.md +91 -0
  34. package/package.json +43 -0
  35. package/postbuild-bundled.js +11 -0
  36. package/postbuild.js +63 -0
  37. package/prebuild-bundled.js +11 -0
  38. package/rsrc/overview.png +0 -0
  39. package/rsrc/overview.xlsx +0 -0
  40. package/scripts/faust2sndfile.js +43 -0
  41. package/scripts/faust2svg.js +17 -0
  42. package/scripts/faust2wasm.js +29 -0
  43. package/src/FaustAudioWorkletNode.ts +264 -0
  44. package/src/FaustAudioWorkletProcessor.ts +273 -0
  45. package/src/FaustCompiler.ts +252 -0
  46. package/src/FaustDspGenerator.ts +498 -0
  47. package/src/FaustDspInstance.ts +141 -0
  48. package/src/FaustFFTAudioWorkletProcessor.ts +554 -0
  49. package/src/FaustOfflineProcessor.ts +141 -0
  50. package/src/FaustScriptProcessorNode.ts +80 -0
  51. package/src/FaustSvgDiagrams.ts +37 -0
  52. package/src/FaustWasmInstantiator.ts +202 -0
  53. package/src/FaustWebAudioDsp.ts +1060 -0
  54. package/src/LibFaust.ts +57 -0
  55. package/src/WavDecoder.ts +207 -0
  56. package/src/WavEncoder.ts +204 -0
  57. package/src/copyWebStandaloneAssets.d.ts +3 -0
  58. package/src/copyWebStandaloneAssets.js +20 -0
  59. package/src/exports-bundle.ts +5 -0
  60. package/src/exports.ts +24 -0
  61. package/src/faust2svgFiles.d.ts +3 -0
  62. package/src/faust2svgFiles.js +43 -0
  63. package/src/faust2wasmFiles.d.ts +5 -0
  64. package/src/faust2wasmFiles.js +93 -0
  65. package/src/faust2wavFiles.d.ts +3 -0
  66. package/src/faust2wavFiles.js +58 -0
  67. package/src/index-bundle-iife.ts +7 -0
  68. package/src/index-bundle.ts +5 -0
  69. package/src/index.ts +5 -0
  70. package/src/instantiateFaustModule.ts +32 -0
  71. package/src/instantiateFaustModuleFromFile.ts +57 -0
  72. package/src/types.ts +234 -0
  73. package/test/clarinet.dsp +1 -0
  74. package/test/faustlive-wasm/faust-ui/index.css +414 -0
  75. package/test/faustlive-wasm/faust-ui/index.css.map +1 -0
  76. package/test/faustlive-wasm/faust-ui/index.d.ts +1 -0
  77. package/test/faustlive-wasm/faust-ui/index.js +3478 -0
  78. package/test/faustlive-wasm/faust-ui/index.js.map +1 -0
  79. package/test/faustlive-wasm/faustwasm/index.d.ts +1252 -0
  80. package/test/faustlive-wasm/faustwasm/index.js +3772 -0
  81. package/test/faustlive-wasm/faustwasm/index.js.map +7 -0
  82. package/test/faustlive-wasm/index.css +97 -0
  83. package/test/faustlive-wasm/index.html +130 -0
  84. package/test/faustlive-wasm/index.js +638 -0
  85. package/test/guitar.dsp +1 -0
  86. package/test/guitar1.dsp +2 -0
  87. package/test/libfaust-in-worklet/index.html +13 -0
  88. package/test/libfaust-in-worklet/index.js +40 -0
  89. package/test/mono.dsp +8 -0
  90. package/test/node/test.js +17 -0
  91. package/test/noise.dsp +3 -0
  92. package/test/organ.dsp +5 -0
  93. package/test/organ1.dsp +6 -0
  94. package/test/osc.dsp +4 -0
  95. package/test/osc2.dsp +5 -0
  96. package/test/osc3.dsp +4 -0
  97. package/test/p-dj.dsp +4 -0
  98. package/test/poly.dsp +13 -0
  99. package/test/rev.dsp +2 -0
  100. package/test/t1.dsp +1 -0
  101. package/test/web/fft.html +87 -0
  102. package/test/web/fft1.html +88 -0
  103. package/test/web/fft2.html +114 -0
  104. package/test/web/fftw.js +1907 -0
  105. package/test/web/index.html +12 -0
  106. package/test/web/index.js +235 -0
  107. package/test/web/kissfft.js +904 -0
  108. package/test/web/mono.html +46 -0
  109. package/test/web/poly-key.html +62 -0
  110. package/test/web/poly.html +59 -0
  111. package/tsconfig.json +22 -0
@@ -0,0 +1,57 @@
1
+ import type { FaustModule, LibFaustWasm, FaustInfoType } from "./types";
2
+
3
+ export interface ILibFaust extends LibFaustWasm {
4
+ module(): FaustModule;
5
+ fs(): typeof FS;
6
+ }
7
+
8
+ class LibFaust implements ILibFaust {
9
+ private fModule: FaustModule;
10
+ private fCompiler: LibFaustWasm;
11
+ private fFileSystem: typeof FS;
12
+
13
+ constructor(module: FaustModule) {
14
+ this.fModule = module;
15
+ this.fCompiler = new module.libFaustWasm();
16
+ this.fFileSystem = this.fModule.FS;
17
+ }
18
+ module() {
19
+ return this.fModule;
20
+ }
21
+ fs() {
22
+ return this.fFileSystem;
23
+ }
24
+ version() {
25
+ return this.fCompiler.version();
26
+ }
27
+ createDSPFactory(name: string, code: string, args: string, useInternalMemory: boolean) {
28
+ return this.fCompiler.createDSPFactory(name, code, args, useInternalMemory);
29
+ }
30
+ deleteDSPFactory(cFactory: number) {
31
+ return this.fCompiler.deleteDSPFactory(cFactory);
32
+ }
33
+ expandDSP(name: string, code: string, args: string) {
34
+ return this.fCompiler.expandDSP(name, code, args);
35
+ }
36
+ generateAuxFiles(name: string, code: string, args: string) {
37
+ return this.fCompiler.generateAuxFiles(name, code, args);
38
+ }
39
+ deleteAllDSPFactories() {
40
+ return this.fCompiler.deleteAllDSPFactories();
41
+ }
42
+ getErrorAfterException() {
43
+ return this.fCompiler.getErrorAfterException();
44
+ }
45
+ cleanupAfterException() {
46
+ return this.fCompiler.cleanupAfterException();
47
+ }
48
+ getInfos(what: FaustInfoType) {
49
+ return this.fCompiler.getInfos(what);
50
+ }
51
+ toString() {
52
+ return `LibFaust module: ${this.fModule}, compiler: ${this.fCompiler}`;
53
+ }
54
+
55
+ }
56
+
57
+ export default LibFaust;
@@ -0,0 +1,207 @@
1
+ export interface WavDecoderOptions {
2
+ symmetric?: boolean;
3
+ shared?: boolean;
4
+ }
5
+ interface Format {
6
+ formatId: number;
7
+ float: boolean;
8
+ numberOfChannels: number;
9
+ sampleRate: number;
10
+ byteRate: number;
11
+ blockSize: number;
12
+ bitDepth: number;
13
+ }
14
+
15
+ /**
16
+ * Code from https://github.com/mohayonao/wav-decoder
17
+ */
18
+ class WavDecoder {
19
+ static decode(buffer: ArrayBuffer, options?: WavDecoderOptions) {
20
+ const dataView = new DataView(buffer);
21
+ const reader = new Reader(dataView);
22
+ if (reader.string(4) !== "RIFF") {
23
+ throw new TypeError("Invalid WAV file");
24
+ }
25
+ reader.uint32(); // skip file length
26
+ if (reader.string(4) !== "WAVE") {
27
+ throw new TypeError("Invalid WAV file");
28
+ }
29
+ let format: Format | null = null;
30
+ let audioData: {
31
+ numberOfChannels: number;
32
+ length: number;
33
+ sampleRate: number;
34
+ channelData: Float32Array[];
35
+ } | null = null;
36
+ do {
37
+ const chunkType = reader.string(4);
38
+ const chunkSize = reader.uint32();
39
+ if (chunkType === "fmt ") {
40
+ format = this.decodeFormat(reader, chunkSize);
41
+ } else if (chunkType === "data") {
42
+ audioData = this.decodeData(reader, chunkSize, format as Format, options || {});
43
+ } else {
44
+ reader.skip(chunkSize);
45
+ }
46
+ } while (audioData === null);
47
+ return audioData;
48
+ }
49
+ private static decodeFormat(reader: Reader, chunkSize: number) {
50
+ const formats = {
51
+ 0x0001: "lpcm",
52
+ 0x0003: "lpcm"
53
+ };
54
+ const formatId = reader.uint16();
55
+ if (!formats.hasOwnProperty(formatId)) {
56
+ throw new TypeError("Unsupported format in WAV file: 0x" + formatId.toString(16));
57
+ }
58
+ const format: Format = {
59
+ formatId: formatId,
60
+ float: formatId === 0x0003,
61
+ numberOfChannels: reader.uint16(),
62
+ sampleRate: reader.uint32(),
63
+ byteRate: reader.uint32(),
64
+ blockSize: reader.uint16(),
65
+ bitDepth: reader.uint16()
66
+ };
67
+ reader.skip(chunkSize - 16);
68
+ return format;
69
+ }
70
+ private static decodeData(reader: Reader, chunkSizeIn: number, format: Format, options: WavDecoderOptions) {
71
+ const chunkSize = Math.min(chunkSizeIn, reader.remain());
72
+ const length = Math.floor(chunkSize / format.blockSize);
73
+ const numberOfChannels = format.numberOfChannels;
74
+ const sampleRate = format.sampleRate;
75
+ const channelData: Float32Array[] = new Array(numberOfChannels);
76
+ for (let ch = 0; ch < numberOfChannels; ch++) {
77
+ const AB = options.shared ? (globalThis.SharedArrayBuffer || globalThis.ArrayBuffer) : globalThis.ArrayBuffer;
78
+ const ab = new AB(length * Float32Array.BYTES_PER_ELEMENT);
79
+ channelData[ch] = new Float32Array(ab);
80
+ }
81
+ this.readPCM(reader, channelData, length, format, options);
82
+ return {
83
+ numberOfChannels,
84
+ length,
85
+ sampleRate,
86
+ channelData
87
+ };
88
+ }
89
+ private static readPCM(reader: Reader, channelData: Float32Array[], length: number, format: Format, options: WavDecoderOptions) {
90
+ const bitDepth = format.bitDepth;
91
+ const decoderOption = format.float ? "f" : options.symmetric ? "s" : "";
92
+ const methodName = "pcm" + bitDepth + decoderOption as `pcm${8 | 16 | 32}${"f" | "s" | ""}`;
93
+ if (!(reader as any)[methodName]) {
94
+ throw new TypeError("Not supported bit depth: " + format.bitDepth);
95
+ }
96
+ const read: () => number = (reader as any)[methodName].bind(reader);
97
+ const numberOfChannels = format.numberOfChannels;
98
+ for (let i = 0; i < length; i++) {
99
+ for (let ch = 0; ch < numberOfChannels; ch++) {
100
+ channelData[ch][i] = read();
101
+ }
102
+ }
103
+ }
104
+ }
105
+
106
+ class Reader {
107
+ pos = 0;
108
+ dataView: DataView;
109
+ constructor(dataView: DataView) {
110
+ this.dataView = dataView;
111
+ }
112
+ remain() {
113
+ return this.dataView.byteLength - this.pos;
114
+ }
115
+ skip(n: number) {
116
+ this.pos += n;
117
+ }
118
+ uint8() {
119
+ const data = this.dataView.getUint8(this.pos);
120
+ this.pos += 1;
121
+ return data;
122
+ }
123
+ int16() {
124
+ const data = this.dataView.getInt16(this.pos, true);
125
+ this.pos += 2;
126
+ return data;
127
+ }
128
+ uint16() {
129
+ const data = this.dataView.getUint16(this.pos, true);
130
+ this.pos += 2;
131
+ return data;
132
+ }
133
+ uint32() {
134
+ const data = this.dataView.getUint32(this.pos, true);
135
+ this.pos += 4;
136
+ return data;
137
+ }
138
+ string(n: number) {
139
+ let data = "";
140
+ for (let i = 0; i < n; i++) {
141
+ data += String.fromCharCode(this.uint8());
142
+ }
143
+ return data;
144
+ }
145
+ pcm8() {
146
+ const data = this.dataView.getUint8(this.pos) - 128;
147
+ this.pos += 1;
148
+ return data < 0 ? data / 128 : data / 127;
149
+ }
150
+ pcm8s() {
151
+ const data = this.dataView.getUint8(this.pos) - 127.5;
152
+ this.pos += 1;
153
+ return data / 127.5;
154
+ }
155
+ pcm16() {
156
+ const data = this.dataView.getInt16(this.pos, true);
157
+ this.pos += 2;
158
+ return data < 0 ? data / 32768 : data / 32767;
159
+ }
160
+ pcm16s() {
161
+ const data = this.dataView.getInt16(this.pos, true);
162
+ this.pos += 2;
163
+ return data / 32768;
164
+ }
165
+ pcm24() {
166
+ const x0 = this.dataView.getUint8(this.pos + 0);
167
+ const x1 = this.dataView.getUint8(this.pos + 1);
168
+ const x2 = this.dataView.getUint8(this.pos + 2);
169
+ const xx = (x0 + (x1 << 8) + (x2 << 16));
170
+
171
+ const data = xx > 0x800000 ? xx - 0x1000000 : xx;
172
+ this.pos += 3;
173
+ return data < 0 ? data / 8388608 : data / 8388607;
174
+ }
175
+ pcm24s() {
176
+ const x0 = this.dataView.getUint8(this.pos + 0);
177
+ const x1 = this.dataView.getUint8(this.pos + 1);
178
+ const x2 = this.dataView.getUint8(this.pos + 2);
179
+ const xx = (x0 + (x1 << 8) + (x2 << 16));
180
+
181
+ const data = xx > 0x800000 ? xx - 0x1000000 : xx;
182
+ this.pos += 3;
183
+ return data / 8388608;
184
+ }
185
+ pcm32() {
186
+ const data = this.dataView.getInt32(this.pos, true);
187
+ this.pos += 4;
188
+ return data < 0 ? data / 2147483648 : data / 2147483647;
189
+ }
190
+ pcm32s() {
191
+ const data = this.dataView.getInt32(this.pos, true);
192
+ this.pos += 4;
193
+ return data / 2147483648;
194
+ }
195
+ pcm32f() {
196
+ const data = this.dataView.getFloat32(this.pos, true);
197
+ this.pos += 4;
198
+ return data;
199
+ }
200
+ pcm64f() {
201
+ const data = this.dataView.getFloat64(this.pos, true);
202
+ this.pos += 8;
203
+ return data;
204
+ }
205
+ }
206
+
207
+ export default WavDecoder;
@@ -0,0 +1,204 @@
1
+ export interface WavEncoderOptions {
2
+ bitDepth: number;
3
+ float?: boolean;
4
+ symmetric?: boolean;
5
+ shared?: boolean;
6
+ sampleRate: number;
7
+ }
8
+ interface Format {
9
+ formatId: number;
10
+ float: boolean;
11
+ symmetric: boolean;
12
+ numberOfChannels: number;
13
+ sampleRate: number;
14
+ length: number;
15
+ bitDepth: number;
16
+ byteDepth: number;
17
+ }
18
+
19
+ /**
20
+ * Code from https://github.com/mohayonao/wav-encoder
21
+ */
22
+ class WavEncoder {
23
+ static encode(audioBuffer: Float32Array[], options: WavEncoderOptions) {
24
+ const numberOfChannels = audioBuffer.length;
25
+ const length = audioBuffer[0].length;
26
+ const { shared, float } = options;
27
+ const bitDepth = float ? 32 : ((options.bitDepth | 0) || 16);
28
+ const byteDepth = bitDepth >> 3;
29
+ const byteLength = length * numberOfChannels * byteDepth;
30
+ // eslint-disable-next-line no-undef
31
+ const AB = shared ? (globalThis.SharedArrayBuffer || globalThis.ArrayBuffer) : globalThis.ArrayBuffer;
32
+ const ab = new AB((44 + byteLength) * Uint8Array.BYTES_PER_ELEMENT);
33
+ const dataView = new DataView(ab);
34
+ const writer = new Writer(dataView);
35
+ const format: Format = {
36
+ formatId: float ? 0x0003 : 0x0001,
37
+ float: !!float,
38
+ numberOfChannels,
39
+ sampleRate: options.sampleRate,
40
+ symmetric: !!options.symmetric,
41
+ length,
42
+ bitDepth,
43
+ byteDepth
44
+ };
45
+ this.writeHeader(writer, format);
46
+ this.writeData(writer, audioBuffer, format);
47
+ return ab;
48
+ }
49
+ private static writeHeader(writer: Writer, format: Format) {
50
+ const { formatId, sampleRate, bitDepth, numberOfChannels, length, byteDepth } = format;
51
+ writer.string("RIFF");
52
+ writer.uint32(writer.dataView.byteLength - 8);
53
+ writer.string("WAVE");
54
+ writer.string("fmt ");
55
+ writer.uint32(16);
56
+ writer.uint16(formatId);
57
+ writer.uint16(numberOfChannels);
58
+ writer.uint32(sampleRate);
59
+ writer.uint32(sampleRate * numberOfChannels * byteDepth);
60
+ writer.uint16(numberOfChannels * byteDepth);
61
+ writer.uint16(bitDepth);
62
+ writer.string("data");
63
+ writer.uint32(length * numberOfChannels * byteDepth);
64
+ return writer.pos;
65
+ }
66
+ private static writeData(writer: Writer, audioBuffer: Float32Array[], format: Format) {
67
+ const { bitDepth, float, length, numberOfChannels, symmetric } = format;
68
+ if (bitDepth === 32 && float) {
69
+ const { dataView, pos } = writer;
70
+ const ab = dataView.buffer;
71
+ const f32View = new Float32Array(ab, pos);
72
+ if (numberOfChannels === 1) {
73
+ f32View.set(audioBuffer[0]);
74
+ return;
75
+ }
76
+ for (let ch = 0; ch < numberOfChannels; ch++) {
77
+ const channel = audioBuffer[ch];
78
+ for (let i = 0; i < length; i++) {
79
+ f32View[i * numberOfChannels + ch] = channel[i];
80
+ }
81
+ }
82
+ return;
83
+ }
84
+ const encoderOption = float ? "f" : symmetric ? "s" : "";
85
+ const methodName = "pcm" + bitDepth + encoderOption;
86
+
87
+ if (!(writer as any)[methodName]) {
88
+ throw new TypeError("Not supported bit depth: " + bitDepth);
89
+ }
90
+
91
+ const write: (value: number) => void = (writer as any)[methodName].bind(writer);
92
+
93
+ for (let i = 0; i < length; i++) {
94
+ for (let j = 0; j < numberOfChannels; j++) {
95
+ write(audioBuffer[j][i]);
96
+ }
97
+ }
98
+ }
99
+ }
100
+
101
+ class Writer {
102
+ pos = 0;
103
+ dataView: DataView;
104
+ constructor(dataView: DataView) {
105
+ this.dataView = dataView;
106
+ }
107
+ int16(value: number) {
108
+ this.dataView.setInt16(this.pos, value, true);
109
+ this.pos += 2;
110
+ }
111
+ uint16(value: number) {
112
+ this.dataView.setUint16(this.pos, value, true);
113
+ this.pos += 2;
114
+ }
115
+ uint32(value: number) {
116
+ this.dataView.setUint32(this.pos, value, true);
117
+ this.pos += 4;
118
+ }
119
+ string(value: string) {
120
+ for (let i = 0, imax = value.length; i < imax; i++) {
121
+ this.dataView.setUint8(this.pos++, value.charCodeAt(i));
122
+ }
123
+ }
124
+ pcm8(valueIn: number) {
125
+ let value = valueIn;
126
+ value = Math.max(-1, Math.min(value, +1));
127
+ value = (value * 0.5 + 0.5) * 255;
128
+ value = Math.round(value) | 0;
129
+ this.dataView.setUint8(this.pos, value/* , true*/);
130
+ this.pos += 1;
131
+ }
132
+ pcm8s(valueIn: number) {
133
+ let value = valueIn;
134
+ value = Math.round(value * 128) + 128;
135
+ value = Math.max(0, Math.min(value, 255));
136
+ this.dataView.setUint8(this.pos, value/* , true*/);
137
+ this.pos += 1;
138
+ }
139
+ pcm16(valueIn: number) {
140
+ let value = valueIn;
141
+ value = Math.max(-1, Math.min(value, +1));
142
+ value = value < 0 ? value * 32768 : value * 32767;
143
+ value = Math.round(value) | 0;
144
+ this.dataView.setInt16(this.pos, value, true);
145
+ this.pos += 2;
146
+ }
147
+ pcm16s(valueIn: number) {
148
+ let value = valueIn;
149
+ value = Math.round(value * 32768);
150
+ value = Math.max(-32768, Math.min(value, 32767));
151
+ this.dataView.setInt16(this.pos, value, true);
152
+ this.pos += 2;
153
+ }
154
+ pcm24(valueIn: number) {
155
+ let value = valueIn;
156
+ value = Math.max(-1, Math.min(value, +1));
157
+ value = value < 0 ? 0x1000000 + value * 8388608 : value * 8388607;
158
+ value = Math.round(value) | 0;
159
+
160
+ const x0 = (value >> 0) & 0xFF;
161
+ const x1 = (value >> 8) & 0xFF;
162
+ const x2 = (value >> 16) & 0xFF;
163
+
164
+ this.dataView.setUint8(this.pos + 0, x0);
165
+ this.dataView.setUint8(this.pos + 1, x1);
166
+ this.dataView.setUint8(this.pos + 2, x2);
167
+ this.pos += 3;
168
+ }
169
+ pcm24s(valueIn: number) {
170
+ let value = valueIn;
171
+ value = Math.round(value * 8388608);
172
+ value = Math.max(-8388608, Math.min(value, 8388607));
173
+
174
+ const x0 = (value >> 0) & 0xFF;
175
+ const x1 = (value >> 8) & 0xFF;
176
+ const x2 = (value >> 16) & 0xFF;
177
+
178
+ this.dataView.setUint8(this.pos + 0, x0);
179
+ this.dataView.setUint8(this.pos + 1, x1);
180
+ this.dataView.setUint8(this.pos + 2, x2);
181
+ this.pos += 3;
182
+ }
183
+ pcm32(valueIn: number) {
184
+ let value = valueIn;
185
+ value = Math.max(-1, Math.min(value, +1));
186
+ value = value < 0 ? value * 2147483648 : value * 2147483647;
187
+ value = Math.round(value) | 0;
188
+ this.dataView.setInt32(this.pos, value, true);
189
+ this.pos += 4;
190
+ }
191
+ pcm32s(valueIn: number) {
192
+ let value = valueIn;
193
+ value = Math.round(value * 2147483648);
194
+ value = Math.max(-2147483648, Math.min(value, +2147483647));
195
+ this.dataView.setInt32(this.pos, value, true);
196
+ this.pos += 4;
197
+ }
198
+ pcm32f(value: number) {
199
+ this.dataView.setFloat32(this.pos, value, true);
200
+ this.pos += 4;
201
+ }
202
+ }
203
+
204
+ export default WavEncoder;
@@ -0,0 +1,3 @@
1
+ declare const copyWebStandaloneAssets: (outputDir: string) => void;
2
+
3
+ export default copyWebStandaloneAssets;
@@ -0,0 +1,20 @@
1
+ //@ts-check
2
+ import * as fs from "fs";
3
+ import * as path from "path";
4
+ import { cpSync } from "../fileutils.js";
5
+ import { fileURLToPath } from "url";
6
+
7
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
8
+ const __filename = fileURLToPath(import.meta.url);
9
+
10
+ /**
11
+ * @param {string} outputDir
12
+ */
13
+ const copyWebStandaloneAssets = (outputDir) => {
14
+ console.log(`Writing assets files.`)
15
+ const assetsPath = path.join(__dirname, "../assets/standalone");
16
+ if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir);
17
+ cpSync(assetsPath, outputDir);
18
+ };
19
+
20
+ export default copyWebStandaloneAssets;
@@ -0,0 +1,5 @@
1
+ export { default as instantiateFaustModule } from "./instantiateFaustModule";
2
+
3
+ export * from "./instantiateFaustModule";
4
+
5
+ export * from "./exports";
package/src/exports.ts ADDED
@@ -0,0 +1,24 @@
1
+ export { default as instantiateFaustModuleFromFile } from "./instantiateFaustModuleFromFile";
2
+ export { default as getFaustAudioWorkletProcessor } from "./FaustAudioWorkletProcessor";
3
+ export { default as getFaustFFTAudioWorkletProcessor } from "./FaustFFTAudioWorkletProcessor";
4
+ export { default as FaustCompiler } from "./FaustCompiler";
5
+ export { default as FaustDspInstance } from "./FaustDspInstance";
6
+ export { default as FaustWasmInstantiator } from "./FaustWasmInstantiator";
7
+ export { default as FaustOfflineProcessor } from "./FaustOfflineProcessor";
8
+ export { default as FaustSvgDiagrams } from "./FaustSvgDiagrams";
9
+ export { default as LibFaust } from "./LibFaust";
10
+ export { default as WavEncoder } from "./WavEncoder";
11
+ export { default as WavDecoder } from "./WavDecoder";
12
+
13
+ export * from "./FaustAudioWorkletNode";
14
+ export * from "./FaustAudioWorkletProcessor";
15
+ export * from "./FaustFFTAudioWorkletProcessor";
16
+ export * from "./FaustCompiler";
17
+ export * from "./FaustDspInstance";
18
+ export * from "./FaustOfflineProcessor";
19
+ export * from "./FaustScriptProcessorNode";
20
+ export * from "./FaustWebAudioDsp";
21
+ export * from "./FaustDspGenerator";
22
+ export * from "./LibFaust";
23
+
24
+ export * from "./types";
@@ -0,0 +1,3 @@
1
+ declare const faust2svgFiles: (inputFile: string, outputDir: string, argv?: string[]) => Promise<Record<string, string>>;
2
+
3
+ export default faust2svgFiles;
@@ -0,0 +1,43 @@
1
+ //@ts-check
2
+ import * as fs from "fs";
3
+ import * as path from "path";
4
+ import {
5
+ instantiateFaustModuleFromFile,
6
+ LibFaust,
7
+ FaustCompiler,
8
+ FaustSvgDiagrams
9
+ } from "../dist/esm/index.js";
10
+ import { fileURLToPath } from "url";
11
+
12
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
13
+ const __filename = fileURLToPath(import.meta.url);
14
+
15
+ /**
16
+ * @param {string} inputFile
17
+ * @param {string} outputDir
18
+ * @param {string[]} [argv]
19
+ */
20
+ const faust2svgFiles = async (inputFile, outputDir, argv = []) => {
21
+ const faustModule = await instantiateFaustModuleFromFile(path.join(__dirname, "../libfaust-wasm/libfaust-wasm.js"));
22
+ const libFaust = new LibFaust(faustModule);
23
+ const compiler = new FaustCompiler(libFaust);
24
+ console.log(`Faust Compiler version: ${compiler.version()}`);
25
+ console.log(`Reading file ${inputFile}`);
26
+ const code = fs.readFileSync(inputFile, { encoding: "utf8" });
27
+
28
+ if (!argv.find(a => a === "-I")) argv.push("-I", "libraries/");
29
+ const { name } = path.parse(inputFile);
30
+ const diagram = new FaustSvgDiagrams(compiler);
31
+ const svgs = diagram.from(name, code, argv.join(" "));
32
+ console.log(`Generated ${Object.keys(svgs).length} files.`);
33
+
34
+ console.log(`Writing files to ${outputDir}`);
35
+ if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir);
36
+ for (const file in svgs) {
37
+ const svgPath = path.join(outputDir, file);
38
+ fs.writeFileSync(svgPath, svgs[file]);
39
+ }
40
+ return svgs;
41
+ };
42
+
43
+ export default faust2svgFiles;
@@ -0,0 +1,5 @@
1
+ import { FaustDspMeta } from "./types";
2
+
3
+ declare const faust2wasmFiles: (inputFile: string, outputDir: string, argv?: string[], poly?: boolean) => Promise<{ dspMeta: FaustDspMeta; effectMeta: FaustDspMeta | null }>;
4
+
5
+ export default faust2wasmFiles;
@@ -0,0 +1,93 @@
1
+ //@ts-check
2
+ import * as fs from "fs";
3
+ import * as path from "path";
4
+ import {
5
+ instantiateFaustModuleFromFile,
6
+ LibFaust,
7
+ FaustCompiler,
8
+ FaustMonoDspGenerator,
9
+ FaustPolyDspGenerator
10
+ } from "../dist/esm/index.js";
11
+ import { fileURLToPath } from "url";
12
+
13
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
14
+ const __filename = fileURLToPath(import.meta.url);
15
+
16
+ /**
17
+ * @param {string} inputFile
18
+ * @param {string} outputDir
19
+ * @param {string[]} [argv]
20
+ * @param {boolean} [poly]
21
+ */
22
+ const faust2wasmFiles = async (inputFile, outputDir, argv = [], poly = false) => {
23
+ const faustModule = await instantiateFaustModuleFromFile(path.join(__dirname, "../libfaust-wasm/libfaust-wasm.js"));
24
+ const libFaust = new LibFaust(faustModule);
25
+ const compiler = new FaustCompiler(libFaust);
26
+ console.log(`Faust Compiler version: ${compiler.version()}`);
27
+ console.log(`Reading file ${inputFile}`);
28
+ const code = fs.readFileSync(inputFile, { encoding: "utf8" });
29
+
30
+ if (!argv.find(a => a === "-I")) argv.push("-I", "libraries/");
31
+ const dspModulePath = path.join(outputDir, "dspModule.wasm");
32
+ const dspMetaPath = path.join(outputDir, "dspMeta.json");
33
+ const effectModulePath = path.join(outputDir, "effectModule.wasm");
34
+ const effectMetaPath = path.join(outputDir, "effectMeta.json");
35
+ const mixerModulePath = path.join(outputDir, "mixerModule.wasm");
36
+ /** @type {Uint8Array} */
37
+ let dspModule;
38
+ /** @type {import("./types").FaustDspMeta} */
39
+ let dspMeta;
40
+ /** @type {Uint8Array | null} */
41
+ let effectModule = null;
42
+ /** @type {import("./types").FaustDspMeta | null} */
43
+ let effectMeta = null;
44
+ /** @type {Uint8Array | null} */
45
+ let mixerModule = null;
46
+ const { name } = path.parse(inputFile);
47
+ if (poly) {
48
+ const generator = new FaustPolyDspGenerator();
49
+ const t1 = Date.now();
50
+ const dsp = await generator.compile(compiler, name, code, argv.join(" "));
51
+ if (!dsp) throw new Error("Faust DSP not compiled");
52
+ const { voiceFactory, effectFactory, mixerBuffer } = dsp;
53
+ if (!voiceFactory) throw new Error("Faust DSP Factory not compiled");
54
+ console.log(`Compilation successful (${Date.now() - t1} ms).`);
55
+ dspModule = voiceFactory.code;
56
+ dspMeta = JSON.parse(voiceFactory.json);
57
+ mixerModule = mixerBuffer;
58
+
59
+ if (effectFactory) {
60
+ effectModule = effectFactory.code;
61
+ effectMeta = JSON.parse(effectFactory.json);
62
+ }
63
+ } else {
64
+ const generator = new FaustMonoDspGenerator();
65
+ const t1 = Date.now();
66
+ const dsp = await generator.compile(compiler, name, code, argv.join(" "));
67
+ if (!dsp) throw new Error("Faust DSP not compiled");
68
+ const { factory } = dsp;
69
+ if (!factory) throw new Error("Faust DSP Factory not compiled");
70
+ console.log(`Compilation successful (${Date.now() - t1} ms).`);
71
+ dspModule = factory.code;
72
+ dspMeta = JSON.parse(factory.json);
73
+ }
74
+ const files = [dspModulePath, dspMetaPath];
75
+ if (mixerModule) files.push(mixerModulePath);
76
+ if (effectModule) files.push(effectModulePath, effectMetaPath);
77
+
78
+ console.log(`Writing files to ${outputDir}`);
79
+ if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir);
80
+ for (const filePath in files) {
81
+ if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
82
+ }
83
+ fs.writeFileSync(dspModulePath, dspModule);
84
+ fs.writeFileSync(dspMetaPath, JSON.stringify(dspMeta, null, 4));
85
+ if (effectModule && effectMeta) {
86
+ fs.writeFileSync(effectModulePath, effectModule);
87
+ fs.writeFileSync(effectMetaPath, JSON.stringify(effectMeta, null, 4));
88
+ }
89
+ if (mixerModule) fs.writeFileSync(mixerModulePath, mixerModule);
90
+ return { dspMeta, effectMeta };
91
+ };
92
+
93
+ export default faust2wasmFiles;
@@ -0,0 +1,3 @@
1
+ declare const faust2wavFiles: (inputFile: string, inputWav: string, outputWav: string, bufferSize?: number, sampleRate?: number, samples?: number, bitDepth?: number, argv?: string[]) => Promise<void>;
2
+
3
+ export default faust2wavFiles;