node-web-audio-api 0.2.1

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/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ Copyright (c) 2021-2022 IRCAM – Centre Pompidou (France, Paris)
2
+
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without modification,
6
+ are permitted provided that the following conditions are met:
7
+
8
+ * Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ * Redistributions in binary form must reproduce the above copyright notice, this
12
+ list of conditions and the following disclaimer in the documentation and/or
13
+ other materials provided with the distribution.
14
+
15
+ * Neither the name of the IRCAM nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # `node-web-audio-api`
2
+
3
+ > Nodejs bindings for [`orottier/web-audio-api-rs`](https://github.com/orottier/web-audio-api-rs/) using [`napi-rs`](https://github.com/napi-rs/napi-rs/)
4
+
5
+ ## Install
6
+
7
+ ```
8
+ npm install [--save] node-web-audio-api
9
+ ```
10
+
11
+ ## Example
12
+
13
+ ```js
14
+ import { AudioContext, OscillatorNode, GainNode } from 'node-web-audio-api';
15
+
16
+ const audioContext = new AudioContext();
17
+
18
+ setInterval(() => {
19
+ const now = audioContext.currentTime;
20
+
21
+ const env = new GainNode(audioContext);
22
+ env.connect(audioContext.destination);
23
+ env.gain.value = 0;
24
+ env.gain.setValueAtTime(0, now);
25
+ env.gain.linearRampToValueAtTime(1, now + 0.02);
26
+ env.gain.exponentialRampToValueAtTime(0.0001, now + 1);
27
+
28
+ const osc = new OscillatorNode(audioContext);
29
+ osc.frequency.value = 200 + Math.random() * 2800;
30
+ osc.connect(env);
31
+ osc.start(now);
32
+ osc.stop(now + 1);
33
+ }, 50);
34
+ ```
35
+
36
+
37
+
38
+ ## Build manually
39
+
40
+ If prebuilt binaries are not shippped for your platform, you will need to install
41
+ the rust toolchain and install and build the package from github.
42
+
43
+ 1. Install rust toolchain
44
+
45
+ ```
46
+ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
47
+ ```
48
+
49
+ 2. Install from github
50
+
51
+ ```
52
+ npm install --save git+https://github.com/b-ma/node-web-audio-api.git
53
+ ```
54
+
55
+ Note that the package will be built on your machine, so the install process might be a bit long
56
+
57
+ ## Roadmap
58
+
59
+ - Make a few nodes work properly with clean and predictable code
60
+ - Generate bindings from IDL [https://webaudio.github.io/web-audio-api/#idl-index](https://webaudio.github.io/web-audio-api/#idl-index)
61
+ - Publish on `npm` with binaries
62
+ - Implement prototype chain (?)
63
+ - Follow developments of `web-audio-api-rs`
64
+
65
+ ## License
66
+
67
+ This project is licensed under the [BSD-3-Clause license](./LICENSE).
package/index.js ADDED
@@ -0,0 +1,93 @@
1
+ const { existsSync, readFileSync } = require('fs');
2
+ const { join } = require('path');
3
+ const { patchAudioContext } = require('./monkey-patch.js');
4
+
5
+ const { platform, arch } = process;
6
+
7
+ let nativeBinding = null;
8
+ let loadError = null;
9
+
10
+ switch (platform) {
11
+ case 'win32':
12
+ switch (arch) {
13
+ case 'x64':
14
+ try {
15
+ nativeBinding = require('./node-web-audio-api.win32-x64-msvc.node');
16
+ } catch (e) {
17
+ loadError = e
18
+ }
19
+ break
20
+ case 'arm64':
21
+ try {
22
+ nativeBinding = require('./node-web-audio-api.win32-arm64-msvc.node');
23
+ } catch (e) {
24
+ loadError = e
25
+ }
26
+ break
27
+ default:
28
+ throw new Error(`Unsupported architecture on Windows: ${arch}`);
29
+ }
30
+ break
31
+ case 'darwin':
32
+ switch (arch) {
33
+ case 'x64':
34
+ try {
35
+ nativeBinding = require('./node-web-audio-api.darwin-x64.node');
36
+ } catch (e) {
37
+ loadError = e
38
+ }
39
+ break
40
+ case 'arm64':
41
+ try {
42
+ nativeBinding = require('./node-web-audio-api.darwin-arm64.node');
43
+ } catch (e) {
44
+ loadError = e
45
+ }
46
+ break
47
+ default:
48
+ throw new Error(`Unsupported architecture on macOS: ${arch}`);
49
+ }
50
+ break
51
+ case 'linux':
52
+ switch (arch) {
53
+ case 'x64':
54
+ try {
55
+ nativeBinding = require('./node-web-audio-api.linux-x64-gnu.node');
56
+ } catch (e) {
57
+ loadError = e
58
+ }
59
+ break
60
+ case 'arm64':
61
+ try {
62
+ nativeBinding = require('./node-web-audio-api.linux-arm64-gnu.node');
63
+ } catch (e) {
64
+ loadError = e
65
+ }
66
+ break
67
+ case 'arm':
68
+ try {
69
+ nativeBinding = require('./node-web-audio-api.linux-arm-gnueabihf.node');
70
+ } catch (e) {
71
+ loadError = e
72
+ }
73
+ break
74
+ default:
75
+ throw new Error(`Unsupported architecture on Linux: ${arch}`);
76
+ }
77
+ break
78
+ default:
79
+ throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`);
80
+ }
81
+
82
+ if (!nativeBinding) {
83
+ if (loadError) {
84
+ throw loadError;
85
+ }
86
+
87
+ throw new Error(`Failed to load native binding for OS: ${platform}, architecture: ${arch}`);
88
+ }
89
+
90
+ nativeBinding.AudioContext = patchAudioContext(nativeBinding.AudioContext);
91
+
92
+ module.exports = nativeBinding;
93
+
@@ -0,0 +1,38 @@
1
+
2
+ let contextId = 0;
3
+
4
+ module.exports.patchAudioContext = function(NativeAudioContext) {
5
+ class AudioContext extends NativeAudioContext {
6
+ constructor(...args) {
7
+ super(...args);
8
+ // prevent garbage collection
9
+ const processId = `__AudioContext_${contextId}`;
10
+ process[processId] = this;
11
+ this.__processId = processId;
12
+
13
+ contextId += 1;
14
+ // keep process awake
15
+ this.__keepAwakeId = setInterval(() => {}, 10000);
16
+ }
17
+
18
+ // @todo
19
+ // resume() {
20
+ // this.__keepAwakeId = setInterval(() => {}, 2000);
21
+ // return super.resume();
22
+ // }
23
+
24
+ // suspend() {
25
+ // // not sure to be confirmed
26
+ // clearTimeout(this.__keepAwakeId);
27
+ // return super.suspend();
28
+ // }
29
+
30
+ // close() {
31
+ // delete process[this.__processId];
32
+ // clearTimeout(this.__keepAwakeId);
33
+ // return super.close();
34
+ // }
35
+ }
36
+
37
+ return AudioContext;
38
+ }
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "node-web-audio-api",
3
+ "version": "0.2.1",
4
+ "author": "Benjamin Matuszewski",
5
+ "description": "Node.js bindings for web-audio-api-rs using napi-rs",
6
+ "main": "index.js",
7
+ "repository": "git@github.com:b-ma/node-web-audio-api.git",
8
+ "license": "BSD-3-Clause",
9
+ "keywords": [
10
+ "audio",
11
+ "web audio api",
12
+ "rust",
13
+ "n-api"
14
+ ],
15
+ "engines": {
16
+ "node": ">= 14"
17
+ },
18
+ "napi": {
19
+ "name": "node-web-audio-api"
20
+ },
21
+ "publishConfig": {
22
+ "registry": "https://registry.npmjs.org/",
23
+ "access": "public"
24
+ },
25
+ "scripts": {
26
+ "artifacts": "napi artifacts",
27
+ "build": "napi build --platform --release",
28
+ "build:debug": "napi build --platform",
29
+ "check": "cargo fmt && cargo clippy",
30
+ "generate": "node generator/index.js",
31
+ "preversion": "npm run generate"
32
+ },
33
+ "devDependencies": {
34
+ "chalk": "^5.0.1",
35
+ "dotenv": "^16.0.2",
36
+ "node-ssh": "^13.0.0",
37
+ "octokit": "^2.0.7",
38
+ "ping": "^0.4.2"
39
+ },
40
+ "dependencies": {
41
+ "@napi-rs/cli": "^2.10.0",
42
+ "@node-rs/helper": "^1.3.3"
43
+ }
44
+ }
@@ -0,0 +1,21 @@
1
+ const { AudioContext } = require('./index.js');
2
+
3
+ const audioContext = new AudioContext();
4
+ process.audioContext = audioContext;
5
+
6
+ setInterval(() => {
7
+ const now = audioContext.currentTime;
8
+
9
+ const env = audioContext.createGain();
10
+ env.connect(audioContext.destination);
11
+ env.gain.value = 0;
12
+ env.gain.setValueAtTime(0, now);
13
+ env.gain.linearRampToValueAtTime(0.1, now + 0.02);
14
+ env.gain.exponentialRampToValueAtTime(0.0001, now + 1);
15
+
16
+ const osc = audioContext.createOscillator();
17
+ osc.frequency.value = 200 + Math.random() * 2800;
18
+ osc.connect(env);
19
+ osc.start(now);
20
+ osc.stop(now + 1);
21
+ }, 100);
@@ -0,0 +1,21 @@
1
+ import webaudio from './index.js';
2
+
3
+ const audioContext = new webaudio.AudioContext();
4
+ // process.audioContext = audioContext;
5
+
6
+ setInterval(() => {
7
+ const now = audioContext.currentTime;
8
+
9
+ const env = audioContext.createGain();
10
+ env.connect(audioContext.destination);
11
+ env.gain.value = 0;
12
+ env.gain.setValueAtTime(0, now);
13
+ env.gain.linearRampToValueAtTime(0.1, now + 0.02);
14
+ env.gain.exponentialRampToValueAtTime(0.0001, now + 1);
15
+
16
+ const osc = audioContext.createOscillator();
17
+ osc.frequency.value = 200 + Math.random() * 2800;
18
+ osc.connect(env);
19
+ osc.start(now);
20
+ osc.stop(now + 1);
21
+ }, 100);