node-web-audio-api 0.3.0 → 0.5.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.
- package/.eslintrc +3 -0
- package/CHANGELOG.md +8 -0
- package/README.md +11 -8
- package/dummy.mjs +19 -0
- package/{index.js → index.cjs} +23 -21
- package/index.mjs +39 -0
- package/monkey-patch.js +107 -9
- package/node-web-audio-api.darwin-arm64.node +0 -0
- package/node-web-audio-api.darwin-x64.node +0 -0
- package/node-web-audio-api.linux-arm-gnueabihf.node +0 -0
- package/node-web-audio-api.linux-x64-gnu.node +0 -0
- package/node-web-audio-api.win32-arm64-msvc.node +0 -0
- package/node-web-audio-api.win32-x64-msvc.node +0 -0
- package/package.json +9 -3
- package/simple-test.mjs +2 -3
package/.eslintrc
ADDED
package/CHANGELOG.md
ADDED
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ npm install [--save] node-web-audio-api
|
|
|
11
11
|
## Example
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
|
-
|
|
14
|
+
import { AudioContext, OscillatorNode, GainNode } from 'node-web-audio-api';
|
|
15
15
|
|
|
16
16
|
const audioContext = new AudioContext();
|
|
17
17
|
|
|
@@ -30,14 +30,13 @@ setInterval(() => {
|
|
|
30
30
|
osc.connect(env);
|
|
31
31
|
osc.start(now);
|
|
32
32
|
osc.stop(now + 1);
|
|
33
|
-
},
|
|
33
|
+
}, 80);
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
or using with
|
|
36
|
+
or using with old fashionned commonjs syntax
|
|
37
37
|
|
|
38
38
|
```js
|
|
39
|
-
|
|
40
|
-
const { AudioContext, OscillatorNode, GainNode } = webaudioapi;
|
|
39
|
+
const { AudioContext, OscillatorNode, GainNode } = require('node-web-audio-api');
|
|
41
40
|
|
|
42
41
|
const audioContext = new AudioContext();
|
|
43
42
|
//...
|
|
@@ -46,13 +45,17 @@ const audioContext = new AudioContext();
|
|
|
46
45
|
## Caveats
|
|
47
46
|
|
|
48
47
|
- Currently the library does not provide any way of chosing the output interface, system default interface will be used. As the spec and web-audio-api evolve evolve, thus should change in the future see [https://github.com/orottier/web-audio-api-rs/issues/216](https://github.com/orottier/web-audio-api-rs/issues/216)
|
|
49
|
-
- On Linux systems, the audio backend is Alsa,
|
|
48
|
+
- On Linux systems, the audio backend is Alsa, which limits the number of online
|
|
49
|
+
AudioContext to 1. This is subject to change in the future.
|
|
50
50
|
|
|
51
51
|
### Raspberry Pi
|
|
52
52
|
|
|
53
53
|
On Raspberry Pi, the default render quantum size (128) is too small and underruns
|
|
54
|
-
occurs frequently. To prevent that you
|
|
55
|
-
|
|
54
|
+
occurs frequently. To prevent that, if you do not explicitely provide a latency hint
|
|
55
|
+
in the AudioContext options, the value is automatically set to 'playback' which uses
|
|
56
|
+
a buffer of 1024 samples. While this is not per se spec compliant, it allow usage
|
|
57
|
+
of the library in a more user friendly manner. In the future, this might change according
|
|
58
|
+
to the support of other audio backend, which is now alsa.
|
|
56
59
|
|
|
57
60
|
```js
|
|
58
61
|
const audioContext = new AudioContext({ latencyHint: 'playback' });
|
package/dummy.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { AudioContext } from './index.mjs';
|
|
2
|
+
|
|
3
|
+
const audioContext = new AudioContext();
|
|
4
|
+
|
|
5
|
+
const now = audioContext.currentTime;
|
|
6
|
+
|
|
7
|
+
const gain = audioContext.createGain();
|
|
8
|
+
gain.gain.setValueAtTime(0, now);
|
|
9
|
+
gain.connect(audioContext.destination);
|
|
10
|
+
|
|
11
|
+
const src = audioContext.createOscillator();
|
|
12
|
+
src.connect(gain);
|
|
13
|
+
src.start();
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
gain.gain.setValueCurveAtTime(new Float32Array([0, 1, 0]), now + 1, 2);
|
|
17
|
+
// gain.gain.setValueCurveAtTime(new Float32Array([0, 1, 0]), now + 1, 2);
|
|
18
|
+
|
|
19
|
+
gain.gain.linear(new Float32Array([0, 1, 0]), now + 1, 2);
|
package/{index.js → index.cjs}
RENAMED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
const { existsSync, readFileSync } = require('fs');
|
|
2
|
-
const { join } = require('path');
|
|
3
|
-
|
|
4
1
|
const { platform, arch } = process;
|
|
5
2
|
|
|
6
3
|
let nativeBinding = null;
|
|
@@ -13,67 +10,67 @@ switch (platform) {
|
|
|
13
10
|
try {
|
|
14
11
|
nativeBinding = require('./node-web-audio-api.win32-x64-msvc.node');
|
|
15
12
|
} catch (e) {
|
|
16
|
-
loadError = e
|
|
13
|
+
loadError = e;
|
|
17
14
|
}
|
|
18
|
-
break
|
|
15
|
+
break;
|
|
19
16
|
case 'arm64':
|
|
20
17
|
try {
|
|
21
18
|
nativeBinding = require('./node-web-audio-api.win32-arm64-msvc.node');
|
|
22
19
|
} catch (e) {
|
|
23
|
-
loadError = e
|
|
20
|
+
loadError = e;
|
|
24
21
|
}
|
|
25
|
-
break
|
|
22
|
+
break;
|
|
26
23
|
default:
|
|
27
24
|
throw new Error(`Unsupported architecture on Windows: ${arch}`);
|
|
28
25
|
}
|
|
29
|
-
break
|
|
26
|
+
break;
|
|
30
27
|
case 'darwin':
|
|
31
28
|
switch (arch) {
|
|
32
29
|
case 'x64':
|
|
33
30
|
try {
|
|
34
31
|
nativeBinding = require('./node-web-audio-api.darwin-x64.node');
|
|
35
32
|
} catch (e) {
|
|
36
|
-
loadError = e
|
|
33
|
+
loadError = e;
|
|
37
34
|
}
|
|
38
|
-
break
|
|
35
|
+
break;
|
|
39
36
|
case 'arm64':
|
|
40
37
|
try {
|
|
41
38
|
nativeBinding = require('./node-web-audio-api.darwin-arm64.node');
|
|
42
39
|
} catch (e) {
|
|
43
|
-
loadError = e
|
|
40
|
+
loadError = e;
|
|
44
41
|
}
|
|
45
|
-
break
|
|
42
|
+
break;
|
|
46
43
|
default:
|
|
47
44
|
throw new Error(`Unsupported architecture on macOS: ${arch}`);
|
|
48
45
|
}
|
|
49
|
-
break
|
|
46
|
+
break;
|
|
50
47
|
case 'linux':
|
|
51
48
|
switch (arch) {
|
|
52
49
|
case 'x64':
|
|
53
50
|
try {
|
|
54
51
|
nativeBinding = require('./node-web-audio-api.linux-x64-gnu.node');
|
|
55
52
|
} catch (e) {
|
|
56
|
-
loadError = e
|
|
53
|
+
loadError = e;
|
|
57
54
|
}
|
|
58
|
-
break
|
|
55
|
+
break;
|
|
59
56
|
case 'arm64':
|
|
60
57
|
try {
|
|
61
58
|
nativeBinding = require('./node-web-audio-api.linux-arm64-gnu.node');
|
|
62
59
|
} catch (e) {
|
|
63
|
-
loadError = e
|
|
60
|
+
loadError = e;
|
|
64
61
|
}
|
|
65
|
-
break
|
|
62
|
+
break;
|
|
66
63
|
case 'arm':
|
|
67
64
|
try {
|
|
68
65
|
nativeBinding = require('./node-web-audio-api.linux-arm-gnueabihf.node');
|
|
69
66
|
} catch (e) {
|
|
70
|
-
loadError = e
|
|
67
|
+
loadError = e;
|
|
71
68
|
}
|
|
72
|
-
break
|
|
69
|
+
break;
|
|
73
70
|
default:
|
|
74
71
|
throw new Error(`Unsupported architecture on Linux: ${arch}`);
|
|
75
72
|
}
|
|
76
|
-
break
|
|
73
|
+
break;
|
|
77
74
|
default:
|
|
78
75
|
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`);
|
|
79
76
|
}
|
|
@@ -86,9 +83,14 @@ if (!nativeBinding) {
|
|
|
86
83
|
throw new Error(`Failed to load native binding for OS: ${platform}, architecture: ${arch}`);
|
|
87
84
|
}
|
|
88
85
|
|
|
89
|
-
const {
|
|
86
|
+
const {
|
|
87
|
+
patchAudioContext,
|
|
88
|
+
patchOfflineAudioContext,
|
|
89
|
+
load,
|
|
90
|
+
} = require('./monkey-patch.js');
|
|
90
91
|
|
|
91
92
|
nativeBinding.AudioContext = patchAudioContext(nativeBinding.AudioContext);
|
|
93
|
+
nativeBinding.OfflineAudioContext = patchOfflineAudioContext(nativeBinding.OfflineAudioContext);
|
|
92
94
|
nativeBinding.load = load;
|
|
93
95
|
|
|
94
96
|
module.exports = nativeBinding;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// ---------------------------------------------------------- //
|
|
2
|
+
// ---------------------------------------------------------- //
|
|
3
|
+
// - WARNING - DO NOT EDIT - //
|
|
4
|
+
// - This file has been generated - //
|
|
5
|
+
// ---------------------------------------------------------- //
|
|
6
|
+
// ---------------------------------------------------------- //
|
|
7
|
+
|
|
8
|
+
// re-export index.js to support clean esm syntax
|
|
9
|
+
// see https://github.com/nodejs/node/issues/40541#issuecomment-951609570
|
|
10
|
+
|
|
11
|
+
import { createRequire } from 'module';
|
|
12
|
+
const require = createRequire(import.meta.url);
|
|
13
|
+
|
|
14
|
+
const nativeModule = require('./index.cjs');
|
|
15
|
+
export const {
|
|
16
|
+
AudioContext,
|
|
17
|
+
OfflineAudioContext,
|
|
18
|
+
AudioBuffer,
|
|
19
|
+
PeriodicWave,
|
|
20
|
+
// generated supported nodes
|
|
21
|
+
AudioBufferSourceNode,
|
|
22
|
+
BiquadFilterNode,
|
|
23
|
+
ChannelMergerNode,
|
|
24
|
+
ChannelSplitterNode,
|
|
25
|
+
ConstantSourceNode,
|
|
26
|
+
DelayNode,
|
|
27
|
+
DynamicsCompressorNode,
|
|
28
|
+
GainNode,
|
|
29
|
+
IIRFilterNode,
|
|
30
|
+
OscillatorNode,
|
|
31
|
+
StereoPannerNode,
|
|
32
|
+
WaveShaperNode,
|
|
33
|
+
// helper methods
|
|
34
|
+
load,
|
|
35
|
+
} = nativeModule;
|
|
36
|
+
|
|
37
|
+
export default nativeModule;
|
|
38
|
+
|
|
39
|
+
|
package/monkey-patch.js
CHANGED
|
@@ -1,15 +1,49 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
|
|
3
|
-
let contextId = 0;
|
|
4
|
-
|
|
5
3
|
const isPlainObject = function(obj) {
|
|
6
4
|
return Object.prototype.toString.call(obj) === '[object Object]';
|
|
7
5
|
};
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
const isPositiveInt = function(n) {
|
|
8
|
+
return Number.isSafeInteger(n) && 0 < n;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const isPositiveNumber = function(n) {
|
|
12
|
+
return Number(n) === n && 0 < n;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
class NotSupportedError extends Error {
|
|
16
|
+
constructor(message) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = 'NotSupportedError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const { platform, arch } = process;
|
|
23
|
+
let contextId = 0;
|
|
24
|
+
|
|
25
|
+
function patchAudioContext(NativeAudioContext) {
|
|
10
26
|
class AudioContext extends NativeAudioContext {
|
|
11
|
-
constructor(
|
|
12
|
-
|
|
27
|
+
constructor(options = {}) {
|
|
28
|
+
|
|
29
|
+
// special handling of options on linux, these are not spec compliant but are
|
|
30
|
+
// ment to be more user-friendly than what we have now (is subject to change)
|
|
31
|
+
if (platform === 'linux') {
|
|
32
|
+
// throw meaningfull error if several contexts are created on linux,
|
|
33
|
+
// because of alsa backend we currently use
|
|
34
|
+
if (contextId === 1) {
|
|
35
|
+
throw new Error(`[node-web-audio-api] node-web-audio-api currently uses alsa as backend, therefore only one context can be safely created`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// fallback latencyHint to "playback" on RPi if not explicitely defined
|
|
39
|
+
if (arch === 'arm') {
|
|
40
|
+
if (!('latencyHint' in options)) {
|
|
41
|
+
options.latencyHint = 'playback';
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
super(options);
|
|
13
47
|
// prevent garbage collection
|
|
14
48
|
const processId = `__AudioContext_${contextId}`;
|
|
15
49
|
process[processId] = this;
|
|
@@ -44,9 +78,9 @@ module.exports.patchAudioContext = function(NativeAudioContext) {
|
|
|
44
78
|
}
|
|
45
79
|
|
|
46
80
|
close() {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
81
|
+
delete process[this.__processId];
|
|
82
|
+
clearTimeout(this.__keepAwakeId);
|
|
83
|
+
return Promise.resolve(super.close());
|
|
50
84
|
}
|
|
51
85
|
|
|
52
86
|
decodeAudioData(audioData) {
|
|
@@ -66,6 +100,70 @@ module.exports.patchAudioContext = function(NativeAudioContext) {
|
|
|
66
100
|
return AudioContext;
|
|
67
101
|
}
|
|
68
102
|
|
|
103
|
+
function patchOfflineAudioContext(NativeOfflineAudioContext) {
|
|
104
|
+
class OfflineAudioContext extends NativeOfflineAudioContext {
|
|
105
|
+
constructor(...args) {
|
|
106
|
+
// handle initialisation with either an options object or a sequence of parameters
|
|
107
|
+
// https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-constructor-contextoptions-contextoptions
|
|
108
|
+
if (typeof args[0] === 'object'
|
|
109
|
+
&& 'numberOfChannels' in args[0] && 'length' in args[0] && 'sampleRate' in args[0]
|
|
110
|
+
) {
|
|
111
|
+
const { numberOfChannels, length, sampleRate } = args[0];
|
|
112
|
+
args = [numberOfChannels, length, sampleRate];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!isPositiveInt(args[0])) {
|
|
116
|
+
throw new NotSupportedError(`Unsupported value for numberOfChannels: ${args[0]}`);
|
|
117
|
+
} else if (!isPositiveInt(args[1])) {
|
|
118
|
+
throw new NotSupportedError(`Unsupported value for length: ${args[1]}`);
|
|
119
|
+
} else if (!isPositiveNumber(args[2])) {
|
|
120
|
+
throw new NotSupportedError(`Unsupported value for sampleRate: ${args[2]}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
super(...args);
|
|
124
|
+
|
|
125
|
+
// not sure this is usefull, to be tested
|
|
126
|
+
const keepAwakeId = setInterval(() => {}, 10000);
|
|
127
|
+
Object.defineProperty(this, '__keepAwakeId', {
|
|
128
|
+
value: keepAwakeId,
|
|
129
|
+
enumerable: false,
|
|
130
|
+
writable: true,
|
|
131
|
+
configurable: false,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// promisify sync APIs
|
|
136
|
+
startRendering() {
|
|
137
|
+
try {
|
|
138
|
+
const audioBuffer = super.startRendering();
|
|
139
|
+
|
|
140
|
+
clearTimeout(this.__keepAwakeId);
|
|
141
|
+
return Promise.resolve(audioBuffer);
|
|
142
|
+
} catch (err) {
|
|
143
|
+
return Promise.reject(err);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
decodeAudioData(audioData) {
|
|
148
|
+
if (!isPlainObject(audioData) || !('path' in audioData)) {
|
|
149
|
+
throw new Error(`Invalid argument, please consider using the load helper`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
const audioBuffer = super.decodeAudioData(audioData);
|
|
154
|
+
return Promise.resolve(audioBuffer);
|
|
155
|
+
} catch (err) {
|
|
156
|
+
return Promise.reject(err);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return OfflineAudioContext;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
module.exports.patchAudioContext = patchAudioContext;
|
|
165
|
+
module.exports.patchOfflineAudioContext = patchOfflineAudioContext;
|
|
166
|
+
|
|
69
167
|
// dumb method provided to mock an xhr call and mimick browser's API
|
|
70
168
|
// see also `AudioContext.decodeAudioData`
|
|
71
169
|
module.exports.load = function(path) {
|
|
@@ -74,4 +172,4 @@ module.exports.load = function(path) {
|
|
|
74
172
|
}
|
|
75
173
|
|
|
76
174
|
return { path };
|
|
77
|
-
}
|
|
175
|
+
};
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-web-audio-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"author": "Benjamin Matuszewski",
|
|
5
5
|
"description": "Node.js bindings for web-audio-api-rs using napi-rs",
|
|
6
|
-
"
|
|
6
|
+
"exports": {
|
|
7
|
+
"import": "./index.mjs",
|
|
8
|
+
"require": "./index.cjs"
|
|
9
|
+
},
|
|
7
10
|
"repository": "https://github.com/ircam-ismm/node-web-audio-api",
|
|
8
11
|
"license": "BSD-3-Clause",
|
|
9
12
|
"keywords": [
|
|
@@ -28,14 +31,17 @@
|
|
|
28
31
|
"build:debug": "npm run generate && napi build --platform",
|
|
29
32
|
"check": "cargo fmt && cargo clippy",
|
|
30
33
|
"generate": "node generator/index.mjs && cargo fmt",
|
|
34
|
+
"lint": "eslint monkey-patch.js index.cjs index.mjs && eslint examples/*.mjs",
|
|
31
35
|
"preversion": "yarn install && npm run generate",
|
|
32
36
|
"postversion": "cargo bump $npm_package_version && git commit -am \"v$npm_package_version\""
|
|
33
37
|
},
|
|
34
38
|
"devDependencies": {
|
|
39
|
+
"@ircam/eslint-config": "^1.2.0",
|
|
35
40
|
"@sindresorhus/slugify": "^2.1.0",
|
|
36
|
-
"chalk": "^5.0.1",
|
|
37
41
|
"camelcase": "^6.3.0",
|
|
42
|
+
"chalk": "^5.0.1",
|
|
38
43
|
"dotenv": "^16.0.2",
|
|
44
|
+
"eslint": "^8.27.0",
|
|
39
45
|
"node-ssh": "^13.0.0",
|
|
40
46
|
"octokit": "^2.0.7",
|
|
41
47
|
"ping": "^0.4.2",
|
package/simple-test.mjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { AudioContext } from './index.mjs';
|
|
2
2
|
|
|
3
|
-
const audioContext = new
|
|
4
|
-
// process.audioContext = audioContext;
|
|
3
|
+
const audioContext = new AudioContext();
|
|
5
4
|
|
|
6
5
|
setInterval(() => {
|
|
7
6
|
const now = audioContext.currentTime;
|