node-web-audio-api 0.20.0 → 0.21.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/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## v0.21.0 (17/05/2024)
2
+
3
+ - Feat: implement AudioWorkletNode
4
+
1
5
  ## v0.20.0 (29/04/2024)
2
6
 
3
7
  - Update upstream crate to [v0.44.0](https://github.com/orottier/web-audio-api-rs/blob/main/CHANGELOG.md#version-0440-2024-04-22)
package/README.md CHANGED
@@ -65,9 +65,7 @@ node examples/granular-scrub.mjs
65
65
 
66
66
  ## Caveats
67
67
 
68
- - Missing nodes: `ScriptProcessorNode`, `AudioWorkletNode`.
69
- - Streams: only a minimial audio input stream and the `MediaStreamSourceNode` are provided. All other `MediaStream` features are left on the side for now as they principaly concern a different API specification, which is not a trivial problem.
70
- - Some async methods (e.g. `decodeAudioData`, `setSinkId`) are not trully async yet. This will evolve with the implemetation of the async version in the upstream crate.
68
+ - `Streams`: only a minimial audio input stream and the `MediaStreamSourceNode` are provided. All other `MediaStream` features are left on the side for now as they principaly concern a different API specification, which is not a trivial problem.
71
69
 
72
70
  ## Supported Platforms
73
71
 
package/index.cjs CHANGED
@@ -1,90 +1,88 @@
1
- const { platform, arch } = process;
1
+ // -------------------------------------------------------------------------- //
2
+ // -------------------------------------------------------------------------- //
3
+ // //
4
+ // //
5
+ // //
6
+ // ██╗ ██╗ █████╗ ██████╗ ███╗ ██╗██╗███╗ ██╗ ██████╗ //
7
+ // ██║ ██║██╔══██╗██╔══██╗████╗ ██║██║████╗ ██║██╔════╝ //
8
+ // ██║ █╗ ██║███████║██████╔╝██╔██╗ ██║██║██╔██╗ ██║██║ ███╗ //
9
+ // ██║███╗██║██╔══██║██╔══██╗██║╚██╗██║██║██║╚██╗██║██║ ██║ //
10
+ // ╚███╔███╔╝██║ ██║██║ ██║██║ ╚████║██║██║ ╚████║╚██████╔╝ //
11
+ // ╚══╝╚══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝╚═╝ ╚═══╝ ╚═════╝ //
12
+ // //
13
+ // //
14
+ // - This file has been generated --------------------------- //
15
+ // //
16
+ // //
17
+ // -------------------------------------------------------------------------- //
18
+ // -------------------------------------------------------------------------- //
2
19
 
3
- let nativeBinding = null;
4
- let loadError = null;
20
+ const nativeBinding = require('./load-native.cjs');
21
+ const jsExport = {};
5
22
 
6
- switch (platform) {
7
- case 'win32':
8
- switch (arch) {
9
- case 'x64':
10
- try {
11
- nativeBinding = require('./node-web-audio-api.win32-x64-msvc.node');
12
- } catch (e) {
13
- loadError = e;
14
- }
15
- break;
16
- case 'arm64':
17
- try {
18
- nativeBinding = require('./node-web-audio-api.win32-arm64-msvc.node');
19
- } catch (e) {
20
- loadError = e;
21
- }
22
- break;
23
- default:
24
- throw new Error(`Unsupported architecture on Windows: ${arch}`);
25
- }
26
- break;
27
- case 'darwin':
28
- switch (arch) {
29
- case 'x64':
30
- try {
31
- nativeBinding = require('./node-web-audio-api.darwin-x64.node');
32
- } catch (e) {
33
- loadError = e;
34
- }
35
- break;
36
- case 'arm64':
37
- try {
38
- nativeBinding = require('./node-web-audio-api.darwin-arm64.node');
39
- } catch (e) {
40
- loadError = e;
41
- }
42
- break;
43
- default:
44
- throw new Error(`Unsupported architecture on macOS: ${arch}`);
45
- }
46
- break;
47
- case 'linux':
48
- switch (arch) {
49
- case 'x64':
50
- try {
51
- nativeBinding = require('./node-web-audio-api.linux-x64-gnu.node');
52
- } catch (e) {
53
- loadError = e;
54
- }
55
- break;
56
- case 'arm64':
57
- try {
58
- nativeBinding = require('./node-web-audio-api.linux-arm64-gnu.node');
59
- } catch (e) {
60
- loadError = e;
61
- }
62
- break;
63
- case 'arm':
64
- try {
65
- nativeBinding = require('./node-web-audio-api.linux-arm-gnueabihf.node');
66
- } catch (e) {
67
- loadError = e;
68
- }
69
- break;
70
- default:
71
- throw new Error(`Unsupported architecture on Linux: ${arch}`);
72
- }
73
- break;
74
- default:
75
- throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`);
76
- }
23
+ // --------------------------------------------------------------------------
24
+ // Events
25
+ // --------------------------------------------------------------------------
26
+ jsExport.OfflineAudioCompletionEvent = require('./js/Events').OfflineAudioCompletionEvent;
27
+ jsExport.AudioProcessingEvent = require('./js/Events').AudioProcessingEvent;
28
+ jsExport.AudioRenderCapacityEvent = require('./js/Events').AudioRenderCapacityEvent;
29
+ // --------------------------------------------------------------------------
30
+ // Create Web Audio API facade
31
+ // --------------------------------------------------------------------------
32
+ jsExport.BaseAudioContext = require('./js/BaseAudioContext.js')(jsExport, nativeBinding);
33
+ jsExport.AudioContext = require('./js/AudioContext.js')(jsExport, nativeBinding);
34
+ jsExport.OfflineAudioContext = require('./js/OfflineAudioContext.js')(jsExport, nativeBinding);
77
35
 
78
- if (!nativeBinding) {
79
- if (loadError) {
80
- throw loadError;
81
- }
36
+ jsExport.ScriptProcessorNode = require('./js/ScriptProcessorNode.js')(jsExport, nativeBinding);
37
+ jsExport.AudioWorkletNode = require('./js/AudioWorkletNode.js')(jsExport, nativeBinding);
38
+ jsExport.AnalyserNode = require('./js/AnalyserNode.js')(jsExport, nativeBinding);
39
+ jsExport.AudioBufferSourceNode = require('./js/AudioBufferSourceNode.js')(jsExport, nativeBinding);
40
+ jsExport.BiquadFilterNode = require('./js/BiquadFilterNode.js')(jsExport, nativeBinding);
41
+ jsExport.ChannelMergerNode = require('./js/ChannelMergerNode.js')(jsExport, nativeBinding);
42
+ jsExport.ChannelSplitterNode = require('./js/ChannelSplitterNode.js')(jsExport, nativeBinding);
43
+ jsExport.ConstantSourceNode = require('./js/ConstantSourceNode.js')(jsExport, nativeBinding);
44
+ jsExport.ConvolverNode = require('./js/ConvolverNode.js')(jsExport, nativeBinding);
45
+ jsExport.DelayNode = require('./js/DelayNode.js')(jsExport, nativeBinding);
46
+ jsExport.DynamicsCompressorNode = require('./js/DynamicsCompressorNode.js')(jsExport, nativeBinding);
47
+ jsExport.GainNode = require('./js/GainNode.js')(jsExport, nativeBinding);
48
+ jsExport.IIRFilterNode = require('./js/IIRFilterNode.js')(jsExport, nativeBinding);
49
+ jsExport.MediaStreamAudioSourceNode = require('./js/MediaStreamAudioSourceNode.js')(jsExport, nativeBinding);
50
+ jsExport.OscillatorNode = require('./js/OscillatorNode.js')(jsExport, nativeBinding);
51
+ jsExport.PannerNode = require('./js/PannerNode.js')(jsExport, nativeBinding);
52
+ jsExport.StereoPannerNode = require('./js/StereoPannerNode.js')(jsExport, nativeBinding);
53
+ jsExport.WaveShaperNode = require('./js/WaveShaperNode.js')(jsExport, nativeBinding);
54
+
55
+ jsExport.AudioNode = require('./js/AudioNode.js');
56
+ jsExport.AudioScheduledSourceNode = require('./js/AudioScheduledSourceNode.js');
57
+ jsExport.AudioParam = require('./js/AudioParam.js');
58
+ jsExport.AudioDestinationNode = require('./js/AudioDestinationNode.js');
59
+ jsExport.AudioListener = require('./js/AudioListener.js');
60
+ jsExport.AudioWorklet = require('./js/AudioWorklet.js');
61
+ jsExport.AudioParamMap = require('./js/AudioParamMap.js');
62
+ jsExport.AudioRenderCapacity = require('./js/AudioRenderCapacity.js');
63
+
64
+ jsExport.PeriodicWave = require('./js/PeriodicWave.js')(jsExport, nativeBinding);
65
+ jsExport.AudioBuffer = require('./js/AudioBuffer.js')(jsExport, nativeBinding);
82
66
 
83
- throw new Error(`Failed to load native binding for OS: ${platform}, architecture: ${arch}`);
84
- }
67
+ // --------------------------------------------------------------------------
68
+ // Promisify MediaDevices API
69
+ // --------------------------------------------------------------------------
70
+ jsExport.mediaDevices = {};
85
71
 
86
- const monkeyPatch = require('./js/monkey-patch.js');
87
- nativeBinding = monkeyPatch(nativeBinding);
72
+ const enumerateDevicesSync = nativeBinding.mediaDevices.enumerateDevices;
73
+ jsExport.mediaDevices.enumerateDevices = async function enumerateDevices() {
74
+ const list = enumerateDevicesSync();
75
+ return Promise.resolve(list);
76
+ };
77
+
78
+ const getUserMediaSync = nativeBinding.mediaDevices.getUserMedia;
79
+ jsExport.mediaDevices.getUserMedia = async function getUserMedia(options) {
80
+ if (options === undefined) {
81
+ throw new TypeError('Failed to execute "getUserMedia" on "MediaDevices": audio must be requested');
82
+ }
88
83
 
89
- module.exports = nativeBinding;
84
+ const stream = getUserMediaSync(options);
85
+ return Promise.resolve(stream);
86
+ };
90
87
 
88
+ module.exports = jsExport;
package/index.mjs CHANGED
@@ -30,6 +30,7 @@ export const {
30
30
  // events
31
31
  OfflineAudioCompletionEvent,
32
32
  AudioProcessingEvent,
33
+ AudioRenderCapacityEvent,
33
34
 
34
35
  // manually written nodes
35
36
  BaseAudioContext,
@@ -41,11 +42,15 @@ export const {
41
42
  AudioParam,
42
43
  AudioDestinationNode,
43
44
  AudioListener,
45
+ AudioWorklet,
46
+ AudioParamMap,
47
+ AudioRenderCapacity,
44
48
 
45
49
  PeriodicWave,
46
50
  AudioBuffer,
47
51
  // generated nodes
48
52
  ScriptProcessorNode,
53
+ AudioWorkletNode,
49
54
  AnalyserNode,
50
55
  AudioBufferSourceNode,
51
56
  BiquadFilterNode,
@@ -11,6 +11,7 @@ const {
11
11
  kNapiObj,
12
12
  kOnStateChange,
13
13
  kOnSinkChange,
14
+ kWorkletRelease,
14
15
  } = require('./lib/symbols.js');
15
16
  const {
16
17
  propagateEvent,
@@ -22,6 +23,8 @@ module.exports = function(jsExport, nativeBinding) {
22
23
 
23
24
  class AudioContext extends jsExport.BaseAudioContext {
24
25
  #sinkId = '';
26
+ #renderCapacity = null;
27
+ #onsinkchange = null;
25
28
 
26
29
  constructor(options = {}) {
27
30
  if (typeof options !== 'object') {
@@ -51,18 +54,16 @@ module.exports = function(jsExport, nativeBinding) {
51
54
  }
52
55
 
53
56
  if (options.sinkId !== undefined) {
54
- const sinkId = options.sinkId;
55
-
56
57
  if (typeof options.sinkId === 'object') {
57
58
  // https://webaudio.github.io/web-audio-api/#enumdef-audiosinktype
58
59
  if (!('type' in options.sinkId) || options.sinkId.type !== 'none') {
59
- throw TypeError(`Failed to construct 'AudioContext': Failed to read the 'sinkId' property from AudioNodeOptions: Failed to read the 'type' property from 'AudioSinkOptions': The provided value (${sinkId.type}) is not a valid enum value of type AudioSinkType.`);
60
+ throw TypeError(`Failed to construct 'AudioContext': Failed to read the 'sinkId' property from AudioNodeOptions: Failed to read the 'type' property from 'AudioSinkOptions': The provided value (${options.sinkId.type}) is not a valid enum value of type AudioSinkType.`);
60
61
  }
61
62
 
62
63
  targetOptions.sinkId = 'none';
63
64
  } else {
64
- targetOptions.sinkId = conversions['DOMString'](sinkId, {
65
- context: `Failed to construct 'AudioContext': Failed to read the 'sinkId' property from AudioNodeOptions: Failed to read the 'type' property from 'AudioSinkOptions': The provided value (${sinkId})`,
65
+ targetOptions.sinkId = conversions['DOMString'](options.sinkId, {
66
+ context: `Failed to construct 'AudioContext': Failed to read the 'sinkId' property from AudioNodeOptions: Failed to read the 'type' property from 'AudioSinkOptions': The provided value (${options.sinkId})`,
66
67
  });
67
68
  }
68
69
  } else {
@@ -83,6 +84,10 @@ module.exports = function(jsExport, nativeBinding) {
83
84
  this.#sinkId = options.sinkId;
84
85
  }
85
86
 
87
+ this.#renderCapacity = new jsExport.AudioRenderCapacity({
88
+ [kNapiObj]: this[kNapiObj].renderCapacity,
89
+ });
90
+
86
91
  // Add function to Napi object to bridge from Rust events to JS EventTarget
87
92
  this[kNapiObj][kOnStateChange] = (err, rawEvent) => {
88
93
  if (typeof rawEvent !== 'object' && !('type' in rawEvent)) {
@@ -160,7 +165,7 @@ module.exports = function(jsExport, nativeBinding) {
160
165
  throw new TypeError('Invalid Invocation: Value of \'this\' must be of type \'AudioContext\'');
161
166
  }
162
167
 
163
- throw new Error(`AudioContext::renderCapacity is not yet implemented`);
168
+ return this.#renderCapacity;
164
169
  }
165
170
 
166
171
  get onsinkchange() {
@@ -168,7 +173,7 @@ module.exports = function(jsExport, nativeBinding) {
168
173
  throw new TypeError('Invalid Invocation: Value of \'this\' must be of type \'AudioContext\'');
169
174
  }
170
175
 
171
- return this._sinkchange || null;
176
+ return this.#onsinkchange;
172
177
  }
173
178
 
174
179
  set onsinkchange(value) {
@@ -177,7 +182,7 @@ module.exports = function(jsExport, nativeBinding) {
177
182
  }
178
183
 
179
184
  if (isFunction(value) || value === null) {
180
- this._sinkchange = value;
185
+ this.#onsinkchange = value;
181
186
  }
182
187
  }
183
188
 
@@ -210,6 +215,9 @@ module.exports = function(jsExport, nativeBinding) {
210
215
  throw new TypeError('Invalid Invocation: Value of \'this\' must be of type \'AudioContext\'');
211
216
  }
212
217
 
218
+ // Close audioWorklet first so that `run_audio_worklet_global_scope` exit first
219
+ // The other way around works too because of `recv_timeout` but cleaner this way
220
+ await this.audioWorklet[kWorkletRelease]();
213
221
  await this[kNapiObj].close();
214
222
  }
215
223
 
@@ -0,0 +1,88 @@
1
+ const {
2
+ kPrivateConstructor,
3
+ } = require('./lib/symbols.js');
4
+ const {
5
+ kEnumerableProperty,
6
+ } = require('./lib/utils.js');
7
+
8
+ class AudioParamMap {
9
+ #parameters = null;
10
+
11
+ constructor(options) {
12
+ if (
13
+ (typeof options !== 'object') ||
14
+ options[kPrivateConstructor] !== true
15
+ ) {
16
+ throw new TypeError('Illegal constructor');
17
+ }
18
+
19
+ this.#parameters = options.parameters;
20
+ }
21
+
22
+ get size() {
23
+ return this.#parameters.size;
24
+ }
25
+
26
+ entries() {
27
+ return this.#parameters.entries();
28
+ }
29
+
30
+ keys() {
31
+ return this.#parameters.keys();
32
+ }
33
+
34
+ values() {
35
+ return this.#parameters.values();
36
+ }
37
+
38
+ forEach(func) {
39
+ return this.#parameters.forEach(func);
40
+ }
41
+
42
+ get(name) {
43
+ return this.#parameters.get(name);
44
+ }
45
+
46
+ has(name) {
47
+ return this.#parameters.has(name);
48
+ }
49
+ }
50
+
51
+ Object.defineProperties(AudioParamMap, {
52
+ length: {
53
+ __proto__: null,
54
+ writable: false,
55
+ enumerable: false,
56
+ configurable: true,
57
+ value: 0,
58
+ },
59
+ });
60
+
61
+ Object.defineProperties(AudioParamMap.prototype, {
62
+ [Symbol.toStringTag]: {
63
+ __proto__: null,
64
+ writable: false,
65
+ enumerable: false,
66
+ configurable: true,
67
+ value: 'AudioParamMap',
68
+ },
69
+ [Symbol.iterator]: {
70
+ value: AudioParamMap.prototype.entries,
71
+ enumerable: false,
72
+ configurable: true,
73
+ writable: true,
74
+ },
75
+ size: {
76
+ __proto__: null,
77
+ enumerable: true,
78
+ configurable: true,
79
+ },
80
+ entries: kEnumerableProperty,
81
+ keys: kEnumerableProperty,
82
+ values: kEnumerableProperty,
83
+ forEach: kEnumerableProperty,
84
+ get: kEnumerableProperty,
85
+ has: kEnumerableProperty,
86
+ });
87
+
88
+ module.exports = AudioParamMap;
@@ -0,0 +1,117 @@
1
+ const conversions = require('webidl-conversions');
2
+
3
+ const {
4
+ kNapiObj,
5
+ kOnUpdate,
6
+ } = require('./lib/symbols.js');
7
+ const {
8
+ kEnumerableProperty,
9
+ } = require('./lib/utils.js');
10
+ const {
11
+ propagateEvent,
12
+ } = require('./lib/events.js');
13
+ const {
14
+ AudioRenderCapacityEvent,
15
+ } = require('./Events.js');
16
+
17
+ class AudioRenderCapacity extends EventTarget {
18
+ #onupdate = null;
19
+
20
+ constructor(options) {
21
+ // Make constructor "private"
22
+ if (
23
+ (typeof options !== 'object')
24
+ || !(kNapiObj in options)
25
+ || options[kNapiObj]['Symbol.toStringTag'] !== 'AudioRenderCapacity'
26
+ ) {
27
+ throw new TypeError('Illegal constructor');
28
+ }
29
+
30
+ super();
31
+
32
+ this[kNapiObj] = options[kNapiObj];
33
+
34
+ this[kNapiObj][kOnUpdate] = (err, rawEvent) => {
35
+ const event = new AudioRenderCapacityEvent('update', rawEvent);
36
+ propagateEvent(this, event);
37
+ };
38
+
39
+ this[kNapiObj].listen_to_events();
40
+ }
41
+
42
+ get onupdate() {
43
+ if (!(this instanceof AudioRenderCapacity)) {
44
+ throw new TypeError('Invalid Invocation: Value of \'this\' must be of type \'AudioRenderCapacity\'');
45
+ }
46
+
47
+ return this.#onupdate;
48
+ }
49
+
50
+ set onupdate(value) {
51
+ if (!(this instanceof AudioRenderCapacity)) {
52
+ throw new TypeError('Invalid Invocation: Value of \'this\' must be of type \'AudioRenderCapacity\'');
53
+ }
54
+
55
+ if (isFunction(value) || value === null) {
56
+ this.#onupdate = value;
57
+ }
58
+ }
59
+
60
+ start(options = null) {
61
+ if (!(this instanceof AudioRenderCapacity)) {
62
+ throw new TypeError(`Invalid Invocation: Value of 'this' must be of type 'AudioRenderCapacity'`);
63
+ }
64
+
65
+ let targetOptions = {};
66
+
67
+ if (typeof options === 'object' && options !== null) {
68
+ if (!('updateInterval' in options)) {
69
+ throw new TypeError(`Failed to execute 'start' on 'AudioRenderCapacity': Failed to read the 'updateInterval' property on 'AudioRenderCapacityOptions'`);
70
+ }
71
+
72
+ targetOptions.updateInterval = conversions['double'](options.updateInterval, {
73
+ context: `Failed to execute 'start' on 'AudioRenderCapacity': Failed to read the 'updateInterval' property on 'AudioRenderCapacityOptions': The provided value ()`
74
+ });
75
+ } else {
76
+ targetOptions.updateInterval = 1;
77
+ }
78
+
79
+ return this[kNapiObj].start(targetOptions);
80
+ }
81
+
82
+ stop() {
83
+ if (!(this instanceof AudioRenderCapacity)) {
84
+ throw new TypeError(`Invalid Invocation: Value of 'this' must be of type 'AudioRenderCapacity'`);
85
+ }
86
+
87
+ return this[kNapiObj].start();
88
+ }
89
+ }
90
+
91
+ Object.defineProperties(AudioRenderCapacity, {
92
+ length: {
93
+ __proto__: null,
94
+ writable: false,
95
+ enumerable: false,
96
+ configurable: true,
97
+ value: 0,
98
+ },
99
+ });
100
+
101
+ Object.defineProperties(AudioRenderCapacity.prototype, {
102
+ [Symbol.toStringTag]: {
103
+ __proto__: null,
104
+ writable: false,
105
+ enumerable: false,
106
+ configurable: true,
107
+ value: 'AudioRenderCapacity',
108
+ },
109
+
110
+ onupdate: kEnumerableProperty,
111
+ stop: kEnumerableProperty,
112
+ stop: kEnumerableProperty,
113
+ });
114
+
115
+ module.exports = AudioRenderCapacity;
116
+
117
+