node-web-audio-api 0.3.0 → 0.4.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 ADDED
@@ -0,0 +1,8 @@
1
+ ## v0.4.0
2
+
3
+ - Implement offline audio context
4
+ - Update web-audio-api-rs to v0.24.0
5
+ - Implement `audio_node.disconnect()`
6
+ - Properly support ESM
7
+ - Limit number of online contexts to 1 on Linux
8
+ - Force latencyHint to 'playback' if not manually set on RPi
package/README.md CHANGED
@@ -11,7 +11,7 @@ npm install [--save] node-web-audio-api
11
11
  ## Example
12
12
 
13
13
  ```js
14
- const { AudioContext, OscillatorNode, GainNode } = require('node-web-audio-api');
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
- }, 50);
33
+ }, 80);
34
34
  ```
35
35
 
36
- or using with EcmaScript module syntax
36
+ or using with old fashionned commonjs syntax
37
37
 
38
38
  ```js
39
- import wabaudioapi from 'node-web-audio-api';
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, this is subject to change in the future.
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 should provide a latency hint when building
55
- an audio context:
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' });
@@ -86,9 +86,14 @@ if (!nativeBinding) {
86
86
  throw new Error(`Failed to load native binding for OS: ${platform}, architecture: ${arch}`);
87
87
  }
88
88
 
89
- const { patchAudioContext, load } = require('./monkey-patch.js');
89
+ const {
90
+ patchAudioContext,
91
+ patchOfflineAudioContext,
92
+ load,
93
+ } = require('./monkey-patch.js');
90
94
 
91
95
  nativeBinding.AudioContext = patchAudioContext(nativeBinding.AudioContext);
96
+ nativeBinding.OfflineAudioContext = patchOfflineAudioContext(nativeBinding.OfflineAudioContext);
92
97
  nativeBinding.load = load;
93
98
 
94
99
  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,34 @@
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
- module.exports.patchAudioContext = function(NativeAudioContext) {
7
+ const { platform, arch } = process;
8
+ let contextId = 0;
9
+
10
+ function patchAudioContext(NativeAudioContext) {
10
11
  class AudioContext extends NativeAudioContext {
11
- constructor(...args) {
12
- super(...args);
12
+ constructor(options = {}) {
13
+
14
+ // special handling of options on linux, these are not spec compliant but are
15
+ // ment to be more user-friendly than what we have now (is subject to change)
16
+ if (platform === 'linux') {
17
+ // throw meaningfull error if several contexts are created on linux,
18
+ // because of alsa backend we currently use
19
+ if (contextId === 1) {
20
+ throw new Error(`[node-web-audio-api] node-web-audio-api currently uses alsa as backend, therefore only one context can be safely created`);
21
+ }
22
+
23
+ // fallback latencyHint to "playback" on RPi if not explicitely defined
24
+ if (arch === 'arm') {
25
+ if (!('latencyHint' in options)) {
26
+ options.latencyHint = 'playback';
27
+ }
28
+ }
29
+ }
30
+
31
+ super(options);
13
32
  // prevent garbage collection
14
33
  const processId = `__AudioContext_${contextId}`;
15
34
  process[processId] = this;
@@ -44,9 +63,9 @@ module.exports.patchAudioContext = function(NativeAudioContext) {
44
63
  }
45
64
 
46
65
  close() {
47
- delete process[this.__processId];
48
- clearTimeout(this.__keepAwakeId);
49
- return Promise.resolve(super.close());
66
+ delete process[this.__processId];
67
+ clearTimeout(this.__keepAwakeId);
68
+ return Promise.resolve(super.close());
50
69
  }
51
70
 
52
71
  decodeAudioData(audioData) {
@@ -66,6 +85,54 @@ module.exports.patchAudioContext = function(NativeAudioContext) {
66
85
  return AudioContext;
67
86
  }
68
87
 
88
+ function patchOfflineAudioContext(NativeOfflineAudioContext) {
89
+ class OfflineAudioContext extends NativeOfflineAudioContext {
90
+ constructor(...args) {
91
+ super(...args);
92
+ console.log(args);
93
+
94
+ // not sure this is usefull, to be tested
95
+ const keepAwakeId = setInterval(() => {}, 10000);
96
+ Object.defineProperty(this, '__keepAwakeId', {
97
+ value: keepAwakeId,
98
+ enumerable: false,
99
+ writable: true,
100
+ configurable: false,
101
+ });
102
+ }
103
+
104
+ // promisify sync APIs
105
+ startRendering() {
106
+ try {
107
+ const audioBuffer = super.startRendering();
108
+
109
+ clearTimeout(this.__keepAwakeId);
110
+ return Promise.resolve(audioBuffer);
111
+ } catch (err) {
112
+ return Promise.reject(err);
113
+ }
114
+ }
115
+
116
+ decodeAudioData(audioData) {
117
+ if (!isPlainObject(audioData) || !('path' in audioData)) {
118
+ throw new Error(`Invalid argument, please consider using the load helper`);
119
+ }
120
+
121
+ try {
122
+ const audioBuffer = super.decodeAudioData(audioData);
123
+ return Promise.resolve(audioBuffer);
124
+ } catch (err) {
125
+ return Promise.reject(err);
126
+ }
127
+ }
128
+ }
129
+
130
+ return OfflineAudioContext;
131
+ }
132
+
133
+ module.exports.patchAudioContext = patchAudioContext;
134
+ module.exports.patchOfflineAudioContext = patchOfflineAudioContext;
135
+
69
136
  // dumb method provided to mock an xhr call and mimick browser's API
70
137
  // see also `AudioContext.decodeAudioData`
71
138
  module.exports.load = function(path) {
Binary file
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "node-web-audio-api",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "author": "Benjamin Matuszewski",
5
5
  "description": "Node.js bindings for web-audio-api-rs using napi-rs",
6
- "main": "index.js",
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": [
package/simple-test.mjs CHANGED
@@ -1,7 +1,6 @@
1
- import webaudio from './index.js';
1
+ import { AudioContext } from './index.mjs';
2
2
 
3
- const audioContext = new webaudio.AudioContext();
4
- // process.audioContext = audioContext;
3
+ const audioContext = new AudioContext();
5
4
 
6
5
  setInterval(() => {
7
6
  const now = audioContext.currentTime;