@kmamal/sdl 1.0.0-5 → 1.0.0-9
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 +12 -6
- package/README.md +2 -2
- package/binding.gyp +21 -4
- package/package.json +5 -12
- package/scripts/build.mjs +3 -1
- package/scripts/install.mjs +2 -0
- package/scripts/util/common.js +3 -2
- package/src/javascript/audio/audio-playback-stream.js +9 -10
- package/src/javascript/audio/audio-recording-stream.js +9 -8
- package/src/javascript/audio/audio-stream.js +101 -44
- package/src/javascript/audio/conversion.js +41 -0
- package/src/javascript/audio/devices.js +11 -0
- package/src/javascript/audio/events.js +65 -0
- package/src/javascript/audio/format-helpers.js +8 -4
- package/src/javascript/audio/index.js +25 -20
- package/src/javascript/audio/prevent-exit.js +13 -10
- package/src/javascript/audio/validate.js +9 -0
- package/src/javascript/cleanup.js +9 -6
- package/src/javascript/clipboard/index.js +8 -3
- package/src/javascript/device-instance.js +61 -0
- package/src/javascript/events/events-via-poll.js +32 -28
- package/src/javascript/events/index.js +85 -479
- package/src/javascript/events/try.js +25 -0
- package/src/javascript/gamepad/{device.js → devices.js} +4 -5
- package/src/javascript/gamepad/events.js +66 -0
- package/src/javascript/gamepad/gamepad-instance.js +12 -160
- package/src/javascript/gamepad/index.js +15 -16
- package/src/javascript/globals.js +10 -1
- package/src/javascript/index.js +2 -5
- package/src/javascript/joystick/devices.js +43 -0
- package/src/javascript/joystick/events.js +129 -0
- package/src/javascript/joystick/index.js +9 -18
- package/src/javascript/joystick/joystick-device-instance.js +176 -0
- package/src/javascript/joystick/joystick-instance.js +12 -158
- package/src/javascript/keyboard/index.js +20 -10
- package/src/javascript/keyboard/key-mapping.js +46 -27
- package/src/javascript/mouse/index.js +36 -29
- package/src/javascript/sensor/events.js +21 -0
- package/src/javascript/sensor/index.js +13 -2
- package/src/javascript/sensor/sensor-instance.js +7 -47
- package/src/javascript/subsystems.js +56 -0
- package/src/javascript/touch/index.js +9 -4
- package/src/javascript/validate.js +119 -0
- package/src/javascript/video/conversion.js +63 -0
- package/src/javascript/video/events.js +179 -0
- package/src/javascript/video/format-helpers.js +10 -8
- package/src/javascript/video/index.js +28 -14
- package/src/javascript/video/validate.js +45 -0
- package/src/javascript/video/window.js +196 -167
- package/src/native/audio.cpp +457 -153
- package/src/native/audio.h +25 -19
- package/src/native/clipboard.cpp +8 -37
- package/src/native/clipboard.h +2 -2
- package/src/native/enums.cpp +16 -1
- package/src/native/enums.h +1 -1
- package/src/native/events.cpp +351 -357
- package/src/native/events.h +70 -68
- package/src/native/gamepad.cpp +147 -103
- package/src/native/gamepad.h +10 -11
- package/src/native/global.cpp +183 -340
- package/src/native/global.h +51 -5
- package/src/native/joystick.cpp +204 -275
- package/src/native/joystick.h +21 -15
- package/src/native/keyboard.cpp +63 -25
- package/src/native/keyboard.h +5 -4
- package/src/native/module.cpp +23 -3
- package/src/native/mouse.cpp +28 -59
- package/src/native/mouse.h +9 -9
- package/src/native/packed.cpp +99 -0
- package/src/native/packed.h +86 -0
- package/src/native/power.cpp +13 -1
- package/src/native/power.h +3 -3
- package/src/native/sensor.cpp +78 -69
- package/src/native/sensor.h +7 -7
- package/src/native/testing.cpp +148 -0
- package/src/native/testing.h +17 -0
- package/src/native/threads.cpp +301 -0
- package/src/native/threads.h +36 -0
- package/src/native/touch.cpp +38 -44
- package/src/native/touch.h +3 -2
- package/src/native/video.cpp +211 -98
- package/src/native/video.h +7 -4
- package/src/native/window.cpp +201 -219
- package/src/native/window.h +24 -20
- package/src/types/index.d.ts +157 -70
- package/src/javascript/audio/device.js +0 -37
- package/src/javascript/joystick/device.js +0 -13
- package/src/javascript/joystick/shared.js +0 -90
- package/src/javascript/worker.js +0 -88
- package/src/types/worker.d.ts +0 -71
package/CHANGELOG.md
CHANGED
|
@@ -7,18 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
-
## [v1.0.0] - 2026-
|
|
11
|
-
|
|
12
|
-
The library now builds against SDL3 (3.4.x) instead of SDL2. This release contains breaking changes, see the [migration guide](https://github.com/kmamal/node-sdl/tree/master/docs/migrating-to-sdl3.md) for a walkthrough.
|
|
10
|
+
## [v1.0.0] - 2026-11-08
|
|
13
11
|
|
|
14
12
|
### Changed
|
|
15
13
|
|
|
16
|
-
-
|
|
14
|
+
- The library now builds against SDL3 (3.4.x) instead of SDL2. This introduces many breaking changes, see the [migration guide](https://github.com/kmamal/node-sdl/tree/master/docs/migrating-to-sdl3.md) for a walkthrough.
|
|
15
|
+
- All SDL subsystems are now initialized lazily, on first use. The `sdl.info.initialized` flags are gone and instead every submodule now has an `isAvailable()` function that tries to initialize the subsystem and returns `true` or `false`. Functions, properties, and event listeners that need an SDL subsystem throw if that subsystem has failed to initialize.
|
|
16
|
+
- `sdl.info.drivers.video` and `sdl.info.drivers.audio` moved to `sdl.video.drivers` and `sdl.audio.drivers`.
|
|
17
|
+
- `@kmamal/sdl` can now be imported from worker threads and works exactly as in the main thread with the exception of the video subsystem (`sdl.video`, `sdl.keyboard`, `sdl.mouse`, `sdl.touch`, and `sdl.clipboard`) which can only be used on the main thread. `@kmamal/sdl/helpers` is no longer needed and has been removed.
|
|
18
|
+
- Exceptions thrown in event listeners are re-emitted as an `'error'` event, following the usual `EventEmitter` semantics: with no `'error'` listener they surface as an uncaught exception. They no longer stop event delivery or crash the process.
|
|
17
19
|
- Attaching a listener to a destroyed window or a closed instance or stream now throws, instead of silently never firing.
|
|
18
20
|
- Audio and video formats are reported using the platform-independent enums. The platform-dependent ones are only used as input.
|
|
19
21
|
- Character keys are reported as the unshifted character (`'ü'` instead of `'Ü'`), so `getKey(getScancode('ü'))` round-trips.
|
|
22
|
+
- Display events now report the display as `event.display` instead of `event.device`, and only include the display object itself. Read the changed props from the object.
|
|
23
|
+
- `sdl.keyboard.getState()` returns a `Uint8Array` of `0`/`1` values instead of an array of booleans.
|
|
20
24
|
- steamHandle is now reported as a BigInt.
|
|
21
|
-
- `@kmamal/sdl/helpers` is now `@kmamal/sdl/helpers`, and includes a lot of extra stuff.
|
|
22
25
|
|
|
23
26
|
### Added
|
|
24
27
|
|
|
@@ -29,10 +32,12 @@ The library now builds against SDL3 (3.4.x) instead of SDL2. This release contai
|
|
|
29
32
|
- Relative mouse mode for FPS-style camera controls.
|
|
30
33
|
- `mouseMove` and `ballMotion` events report relative movement through `dx` and `dy`.
|
|
31
34
|
- Window event `'fingerCancel'`, fired when the system cancels a touch.
|
|
35
|
+
- Windows emit `'enterFullscreen'` and `'leaveFullscreen'` events when they enter or leave fullscreen mode.
|
|
32
36
|
- Audio device objects report the `format`, `channels`, `frequency`, and `buffered` the device is running with while streams are open on them.
|
|
33
37
|
- Gamepad buttons `misc1` and `touchpad` are now reported instead of arriving with `button: null`.
|
|
34
38
|
- Pixel-format helpers similar to audio format helpers.
|
|
35
39
|
- More scancodes and keys
|
|
40
|
+
- Sensor `'update'` events carry the reading (`x`, `y`, `z`) and the sensor's `timestamp`, so readings queued between polls are no longer lost.
|
|
36
41
|
|
|
37
42
|
### Fixed
|
|
38
43
|
|
|
@@ -50,6 +55,7 @@ Windows:
|
|
|
50
55
|
- `setResizable()` and `setBorderless()` enforce the same mutual exclusivity as `createWindow()`.
|
|
51
56
|
- `window.destroy()` no longer crashes when called from a `beforeClose` listener, or from a `move` or `resize` listener while the window is being dragged.
|
|
52
57
|
- Removing all of a window's listeners no longer breaks event polling, lets the process exit while the window is open, or turns `destroy()` into a no-op.
|
|
58
|
+
- `window.render()` interprets `dstRect` in window coordinates, the same units as `width`/`height` and mouse events. On high-DPI displays it was previously in physical pixels, so a rectangle built from the window size covered only part of the window.
|
|
53
59
|
- `window.render()` throws instead of silently presenting stale contents when updating the texture fails, and no longer corrupts memory or leaks after a failed render or renderer switch.
|
|
54
60
|
- Windows recover from a lost graphics device instead of failing every `render()` from then on.
|
|
55
61
|
- `window.render()` validates image `stride` and buffer sizes, preventing out-of-bounds reads, including for planar and packed YUV formats with odd dimensions.
|
|
@@ -98,7 +104,7 @@ Sensors:
|
|
|
98
104
|
|
|
99
105
|
Audio:
|
|
100
106
|
|
|
101
|
-
- `putData()` and `getData()` accept empty buffers as no-ops
|
|
107
|
+
- `putData()` and `getData()` accept empty buffers as no-ops. `putData()` rejects a `bytes` count that isn't a whole number of sample frames with a validation error.
|
|
102
108
|
- `buffered` values larger than `32768` are rejected instead of silently truncated, and the buffer size the driver actually uses is reported on the stream's device.
|
|
103
109
|
- `playbackStream.queued` no longer reports negative values for queues over 2 GiB.
|
|
104
110
|
- `stream.playing` is `false` once the stream is closed.
|
package/README.md
CHANGED
|
@@ -16,11 +16,11 @@ SDL bindings for Node.js. Gives desktop applications access to features that Nod
|
|
|
16
16
|
- 🖱 Mouse
|
|
17
17
|
- 🕹 Joysticks
|
|
18
18
|
- 🎮 Gamepads
|
|
19
|
+
- 🧭 Sensors
|
|
19
20
|
- 🔈 Audio playback
|
|
20
21
|
- 🎤 Audio recording
|
|
21
22
|
- 📋 Clipboard manipulation
|
|
22
23
|
- 🔋 Battery status
|
|
23
|
-
- 🧭 Sensors
|
|
24
24
|
|
|
25
25
|
In addition, all these things can now be used without a browser:
|
|
26
26
|
|
|
@@ -28,7 +28,7 @@ In addition, all these things can now be used without a browser:
|
|
|
28
28
|
- __WebGL:__ [@kmamal/gl](https://github.com/kmamal/headless-gl#readme). A fork of [headless-gl](https://github.com/stackgl/headless-gl#readme) modified to render directly to SDL windows.
|
|
29
29
|
- __WebGPU:__ [@kmamal/gpu](https://github.com/kmamal/gpu#readme). A fork of [Google Dawn](https://dawn.googlesource.com/dawn/+/refs/heads/main/src/dawn/node/) modified to render directly to SDL windows.
|
|
30
30
|
- __Web Audio:__ [@kmamal/audio](https://github.com/kmamal/audio#readme). An implementation of the Web Audio API that uses `@kmamal/sdl` for audio playback and recording.
|
|
31
|
-
- __Three.js:__ [@kmamal/three](https://github.com/kmamal/three#readme). A fork of [Three.js](https://threejs.org/) ported to the desktop via
|
|
31
|
+
- __Three.js:__ [@kmamal/three](https://github.com/kmamal/three.js#readme). A fork of [Three.js](https://threejs.org/) ported to the desktop via the above libraries.
|
|
32
32
|
|
|
33
33
|
This package officially supports Linux (X11 and Wayland), Mac, and Windows.
|
|
34
34
|
It should work on any system that both SDL and Node.js support, but I haven't tried others.
|
package/binding.gyp
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
+
'variables': {
|
|
3
|
+
'werror%': 0,
|
|
4
|
+
},
|
|
2
5
|
'targets': [
|
|
3
6
|
{
|
|
4
7
|
'target_name':
|
|
@@ -19,18 +22,34 @@
|
|
|
19
22
|
'src/native/audio.cpp',
|
|
20
23
|
'src/native/clipboard.cpp',
|
|
21
24
|
'src/native/power.cpp',
|
|
25
|
+
'src/native/threads.cpp',
|
|
26
|
+
'src/native/packed.cpp',
|
|
27
|
+
'src/native/testing.cpp',
|
|
22
28
|
],
|
|
23
29
|
'dependencies': [
|
|
24
30
|
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
|
|
25
31
|
],
|
|
26
32
|
'defines': [
|
|
27
|
-
# Node-API
|
|
33
|
+
# Node-API 9, which every supported Node.js (v22.16.0 and later) has
|
|
28
34
|
'NAPI_VERSION=9',
|
|
29
35
|
'NODE_ADDON_API_DISABLE_DEPRECATED',
|
|
30
36
|
'NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS',
|
|
31
37
|
],
|
|
32
|
-
'cflags': ['-
|
|
38
|
+
'cflags': ['-Wall', '-Wextra'],
|
|
33
39
|
'conditions': [
|
|
40
|
+
[
|
|
41
|
+
'werror == 1', {
|
|
42
|
+
'cflags': ['-Werror'],
|
|
43
|
+
'xcode_settings': {
|
|
44
|
+
'GCC_TREAT_WARNINGS_AS_ERRORS': 'YES',
|
|
45
|
+
},
|
|
46
|
+
'msvs_settings': {
|
|
47
|
+
'VCCLCompilerTool': {
|
|
48
|
+
'WarnAsError': 'true',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
],
|
|
34
53
|
[
|
|
35
54
|
'OS == "linux" or OS == "freebsd" or OS == "openbsd" or OS == "netbsd"', {
|
|
36
55
|
'cflags': ['-D_REENTRANT'],
|
|
@@ -52,7 +71,6 @@
|
|
|
52
71
|
'xcode_settings': {
|
|
53
72
|
'OTHER_CFLAGS': ['-std=c++17'],
|
|
54
73
|
'WARNING_CFLAGS': ['-Wall', '-Wextra'],
|
|
55
|
-
'GCC_TREAT_WARNINGS_AS_ERRORS': 'YES',
|
|
56
74
|
},
|
|
57
75
|
'include_dirs': [
|
|
58
76
|
'$(SDL_INC)',
|
|
@@ -71,7 +89,6 @@
|
|
|
71
89
|
'VCCLCompilerTool': {
|
|
72
90
|
'AdditionalOptions': ['-std:c++17'],
|
|
73
91
|
'WarningLevel': '4',
|
|
74
|
-
'WarnAsError': 'true',
|
|
75
92
|
'DisableSpecificWarnings': ['4127'],
|
|
76
93
|
},
|
|
77
94
|
},
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.0.0-
|
|
2
|
+
"version": "1.0.0-9",
|
|
3
3
|
"name": "@kmamal/sdl",
|
|
4
4
|
"description": "SDL bindings for Node.js",
|
|
5
5
|
"keywords": [
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"url": "git+ssh://git@github.com/kmamal/node-sdl.git"
|
|
42
42
|
},
|
|
43
43
|
"engines": {
|
|
44
|
-
"node": ">=22"
|
|
44
|
+
"node": ">=22.16.0"
|
|
45
45
|
},
|
|
46
46
|
"main": "./src/javascript/index.js",
|
|
47
47
|
"types": "./src/types/index.d.ts",
|
|
@@ -49,15 +49,12 @@
|
|
|
49
49
|
".": {
|
|
50
50
|
"types": "./src/types/index.d.ts",
|
|
51
51
|
"default": "./src/javascript/index.js"
|
|
52
|
-
},
|
|
53
|
-
"./worker": {
|
|
54
|
-
"types": "./src/types/worker.d.ts",
|
|
55
|
-
"default": "./src/javascript/worker.js"
|
|
56
52
|
}
|
|
57
53
|
},
|
|
58
54
|
"gypfile": true,
|
|
59
55
|
"scripts": {
|
|
60
56
|
"test": "npx @kmamal/testing",
|
|
57
|
+
"test:stress": "npx @kmamal/testing 'tests/stress/*.stress.mjs'",
|
|
61
58
|
"install": "cd scripts && node install.mjs",
|
|
62
59
|
"download-release": "cd scripts && node download-release.mjs",
|
|
63
60
|
"download-sdl": "cd scripts && node download-sdl.mjs",
|
|
@@ -67,9 +64,8 @@
|
|
|
67
64
|
"clean": "cd scripts && node clean.mjs"
|
|
68
65
|
},
|
|
69
66
|
"devDependencies": {
|
|
70
|
-
"@kmamal/evdev": "0.0.8",
|
|
71
67
|
"@kmamal/testing": "0.0.34",
|
|
72
|
-
"@types/node": "26.6.
|
|
68
|
+
"@types/node": "26.6.6",
|
|
73
69
|
"node-addon-api": "8.9.2",
|
|
74
70
|
"node-gyp": "13.1.0"
|
|
75
71
|
},
|
|
@@ -77,12 +73,9 @@
|
|
|
77
73
|
"sdl": {
|
|
78
74
|
"owner": "kmamal",
|
|
79
75
|
"repo": "build-sdl",
|
|
80
|
-
"version": "3.4.
|
|
76
|
+
"version": "3.4.18"
|
|
81
77
|
}
|
|
82
78
|
},
|
|
83
|
-
"allowScripts": {
|
|
84
|
-
"@kmamal/evdev": true
|
|
85
|
-
},
|
|
86
79
|
"dependencies": {
|
|
87
80
|
"modern-tar": "0.8.6"
|
|
88
81
|
}
|
package/scripts/build.mjs
CHANGED
|
@@ -23,13 +23,15 @@ if (C.systemSdl) {
|
|
|
23
23
|
|
|
24
24
|
console.log("build in", C.dir.build)
|
|
25
25
|
|
|
26
|
+
const werror = C.isSet(process.env.NODE_SDL_WERROR ?? '1') ? 1 : 0
|
|
27
|
+
|
|
26
28
|
let archFlag = ''
|
|
27
29
|
if (process.env.CROSS_COMPILE_ARCH) {
|
|
28
30
|
archFlag = `--arch ${process.env.CROSS_COMPILE_ARCH}`
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
process.chdir(C.dir.root)
|
|
32
|
-
execSync(`npx -y node-gyp rebuild ${archFlag} -j max --verbose`, {
|
|
34
|
+
execSync(`npx -y node-gyp rebuild ${archFlag} -j max --verbose -- -Dwerror=${werror}`, {
|
|
33
35
|
stdio: 'inherit',
|
|
34
36
|
env: {
|
|
35
37
|
...process.env,
|
package/scripts/install.mjs
CHANGED
package/scripts/util/common.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const Fs = require('fs')
|
|
2
|
-
const Path = require('path')
|
|
1
|
+
const Fs = require('node:fs')
|
|
2
|
+
const Path = require('node:path')
|
|
3
3
|
|
|
4
4
|
const dir = {}
|
|
5
5
|
dir.root = Path.join(__dirname, '../..')
|
|
@@ -27,6 +27,7 @@ sdl.assetName = `SDL-v${sdl.version}-${platform}-${targetArch}.tar.gz`
|
|
|
27
27
|
|
|
28
28
|
module.exports = {
|
|
29
29
|
dir,
|
|
30
|
+
isSet,
|
|
30
31
|
fromSource,
|
|
31
32
|
systemSdl,
|
|
32
33
|
version,
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
const Globals = require('../globals')
|
|
2
2
|
const Bindings = require('../bindings')
|
|
3
3
|
const { AudioStream } = require('./audio-stream')
|
|
4
|
+
const { validateBytes } = require('./validate')
|
|
5
|
+
const { validateBuffer } = require('../validate')
|
|
4
6
|
const { resetTimeout } = require('./prevent-exit')
|
|
5
7
|
|
|
6
8
|
class AudioPlaybackStream extends AudioStream {
|
|
@@ -10,21 +12,18 @@ class AudioPlaybackStream extends AudioStream {
|
|
|
10
12
|
|
|
11
13
|
get queued () {
|
|
12
14
|
Globals.events.poll()
|
|
13
|
-
|
|
15
|
+
this._assertOpen()
|
|
14
16
|
|
|
15
17
|
return Bindings.audio_getQueued(this._id)
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
putData (buffer,
|
|
19
|
-
|
|
20
|
+
putData (buffer, _bytes) {
|
|
21
|
+
this._assertOpen()
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
bytes
|
|
23
|
-
|
|
24
|
-
if (bytes
|
|
25
|
-
if (buffer.length < bytes) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, bytes }) }
|
|
26
|
-
const frameSize = this._channels * this._bytesPerSample
|
|
27
|
-
if (bytes % frameSize !== 0) { throw Object.assign(new Error(`bytes must be a multiple of ${frameSize}`), { bytes }) }
|
|
23
|
+
validateBuffer('buffer', buffer)
|
|
24
|
+
const bytes = _bytes === undefined ? buffer.length : _bytes
|
|
25
|
+
validateBytes(buffer, bytes)
|
|
26
|
+
if (bytes % this._frameSize !== 0) { throw Object.assign(new Error(`bytes must be a multiple of ${this._frameSize}`), { bytes }) }
|
|
28
27
|
|
|
29
28
|
if (bytes === 0) { return }
|
|
30
29
|
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
const Globals = require('../globals')
|
|
2
2
|
const Bindings = require('../bindings')
|
|
3
3
|
const { AudioStream } = require('./audio-stream')
|
|
4
|
+
const { validateBytes } = require('./validate')
|
|
5
|
+
const { validateBuffer } = require('../validate')
|
|
4
6
|
|
|
5
7
|
class AudioRecordingStream extends AudioStream {
|
|
6
8
|
constructor (device, options) {
|
|
@@ -9,19 +11,18 @@ class AudioRecordingStream extends AudioStream {
|
|
|
9
11
|
|
|
10
12
|
get available () {
|
|
11
13
|
Globals.events.poll()
|
|
12
|
-
|
|
14
|
+
this._assertOpen()
|
|
13
15
|
|
|
14
16
|
return Bindings.audio_getAvailable(this._id)
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
getData (buffer,
|
|
18
|
-
|
|
19
|
+
getData (buffer, _bytes) {
|
|
20
|
+
this._assertOpen()
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
bytes
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (buffer.length < bytes) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, bytes }) }
|
|
22
|
+
validateBuffer('buffer', buffer)
|
|
23
|
+
const bytes = _bytes === undefined ? buffer.length : _bytes
|
|
24
|
+
validateBytes(buffer, bytes)
|
|
25
|
+
// Unlike putData(), a partial frame isn't rejected: SDL rounds the read down to whole frames and the return value reports what was read.
|
|
25
26
|
|
|
26
27
|
if (bytes === 0) { return 0 }
|
|
27
28
|
|
|
@@ -2,60 +2,97 @@ const Globals = require('../globals')
|
|
|
2
2
|
const Bindings = require('../bindings')
|
|
3
3
|
const Enums = require('../enums')
|
|
4
4
|
const { EventsViaPoll } = require('../events/events-via-poll')
|
|
5
|
-
const {
|
|
6
|
-
const {
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
const { tryEmit } = require('../events/try')
|
|
6
|
+
const { getFormatHelpers } = require('./format-helpers')
|
|
7
|
+
const {
|
|
8
|
+
validateBoolean,
|
|
9
|
+
validateObject,
|
|
10
|
+
validateBuffer,
|
|
11
|
+
validateNonNegativeFloat,
|
|
12
|
+
validateNumberRange,
|
|
13
|
+
validateIntegerRange,
|
|
14
|
+
validatePositiveInt32,
|
|
15
|
+
} = require('../validate')
|
|
16
|
+
|
|
17
|
+
const notifyEvents = { playback: 'drain', recording: 'readable' }
|
|
9
18
|
|
|
10
19
|
class AudioStream extends EventsViaPoll {
|
|
11
20
|
constructor (recording, device, options) {
|
|
12
|
-
|
|
21
|
+
const notifyEvent = notifyEvents[recording ? 'recording' : 'playback']
|
|
22
|
+
super([ 'close', notifyEvent ])
|
|
13
23
|
|
|
14
|
-
|
|
24
|
+
validateObject('options', options)
|
|
15
25
|
|
|
16
26
|
const {
|
|
17
27
|
channels = 1,
|
|
18
28
|
frequency = 48000,
|
|
19
29
|
format = 'f32',
|
|
20
30
|
buffered = 4096,
|
|
31
|
+
gain = 1,
|
|
32
|
+
frequencyRatio = 1,
|
|
21
33
|
} = options
|
|
22
34
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (buffered <= 0) { throw Object.assign(new Error("buffered must be positive"), { buffered }) }
|
|
30
|
-
if (buffered > 2 ** 15) { throw Object.assign(new Error("buffered must be at most 32768"), { buffered }) }
|
|
31
|
-
if (buffered !== 2 ** (32 - Math.clz32(buffered) - 1)) { throw Object.assign(new Error("buffered must be a power of 2"), { buffered }) }
|
|
35
|
+
validateIntegerRange('channels', channels, 1, 8)
|
|
36
|
+
validatePositiveInt32('frequency', frequency)
|
|
37
|
+
const helper = getFormatHelpers(format)
|
|
38
|
+
validatePositiveInt32('buffered', buffered)
|
|
39
|
+
validateNonNegativeFloat('gain', gain)
|
|
40
|
+
validateNumberRange('frequencyRatio', frequencyRatio, 0.01, 100)
|
|
32
41
|
|
|
33
42
|
const _format = Enums.audioFormat[format]
|
|
34
|
-
if (_format === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
|
|
35
43
|
|
|
36
|
-
const
|
|
44
|
+
const notify = (underruns, missing, available) => {
|
|
45
|
+
if (this._closed) { return }
|
|
46
|
+
const event = recording
|
|
47
|
+
? { type: notifyEvent, available }
|
|
48
|
+
: { type: notifyEvent, underruns, missing }
|
|
49
|
+
Globals.events.withoutPolling(() => { tryEmit(this, notifyEvent, event) })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const { id, format: deviceFormat } = Bindings.audio_open(device.id, recording, frequency, _format, channels, buffered, notify)
|
|
53
|
+
Object.assign(device, deviceFormat)
|
|
54
|
+
try {
|
|
55
|
+
if (gain !== 1) { Bindings.audio_setGain(id, gain) }
|
|
56
|
+
if (frequencyRatio !== 1) { Bindings.audio_setFrequencyRatio(id, frequencyRatio) }
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
try { Object.assign(device, Bindings.audio_close(id)) }
|
|
60
|
+
catch { }
|
|
61
|
+
throw error
|
|
62
|
+
}
|
|
37
63
|
|
|
38
64
|
this._id = id
|
|
39
65
|
this._recording = recording
|
|
40
|
-
|
|
66
|
+
this._frameSize = channels * helper.bytesPerSample
|
|
41
67
|
|
|
42
68
|
this._device = device
|
|
43
69
|
this._channels = channels
|
|
44
|
-
this._format = helper.name
|
|
45
70
|
this._frequency = frequency
|
|
46
71
|
|
|
47
72
|
this._playing = false
|
|
48
73
|
this._closed = false
|
|
74
|
+
this._gain = gain
|
|
75
|
+
this._frequencyRatio = frequencyRatio
|
|
49
76
|
|
|
50
|
-
this.
|
|
51
|
-
this._minSampleValue = helper.minSampleValue
|
|
52
|
-
this._maxSampleValue = helper.maxSampleValue
|
|
53
|
-
this._zeroSampleValue = helper.zeroSampleValue
|
|
54
|
-
this._reader = helper.reader
|
|
55
|
-
this._writer = helper.writer
|
|
77
|
+
this._helper = helper
|
|
56
78
|
|
|
57
79
|
Globals.audioStreams.set(this._id, this)
|
|
58
|
-
|
|
80
|
+
|
|
81
|
+
const notifyListeners = () => this.listenerCount(notifyEvent) + this.listenerCount('*')
|
|
82
|
+
this._addInternalListener('newListener', (type) => {
|
|
83
|
+
if ((type !== notifyEvent && type !== '*') || notifyListeners() !== 0) { return }
|
|
84
|
+
Bindings.audio_setNotify(this._id, true)
|
|
85
|
+
})
|
|
86
|
+
this._addInternalListener('removeListener', (type) => {
|
|
87
|
+
if ((type !== notifyEvent && type !== '*') || notifyListeners() !== 0 || this._closed) { return }
|
|
88
|
+
Bindings.audio_setNotify(this._id, false)
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
_makeClosedError () { return Object.assign(new Error("stream is closed"), { id: this._id }) }
|
|
93
|
+
|
|
94
|
+
_assertOpen () {
|
|
95
|
+
if (this._closed) { throw this._makeClosedError() }
|
|
59
96
|
}
|
|
60
97
|
|
|
61
98
|
get id () { return this._id }
|
|
@@ -64,27 +101,49 @@ class AudioStream extends EventsViaPoll {
|
|
|
64
101
|
get channels () { return this._channels }
|
|
65
102
|
get frequency () { return this._frequency }
|
|
66
103
|
|
|
67
|
-
get format () { return this.
|
|
68
|
-
get bytesPerSample () { return this.
|
|
69
|
-
get minSampleValue () { return this.
|
|
70
|
-
get maxSampleValue () { return this.
|
|
71
|
-
get zeroSampleValue () { return this.
|
|
104
|
+
get format () { return this._helper.name }
|
|
105
|
+
get bytesPerSample () { return this._helper.bytesPerSample }
|
|
106
|
+
get minSampleValue () { return this._helper.minSampleValue }
|
|
107
|
+
get maxSampleValue () { return this._helper.maxSampleValue }
|
|
108
|
+
get zeroSampleValue () { return this._helper.zeroSampleValue }
|
|
72
109
|
|
|
73
110
|
readSample (buffer, offset) {
|
|
74
|
-
|
|
75
|
-
return this.
|
|
111
|
+
validateBuffer('buffer', buffer)
|
|
112
|
+
return this._helper.reader.call(buffer, offset)
|
|
76
113
|
}
|
|
77
114
|
|
|
78
115
|
writeSample (buffer, value, offset) {
|
|
79
|
-
|
|
80
|
-
return this.
|
|
116
|
+
validateBuffer('buffer', buffer)
|
|
117
|
+
return this._helper.writer.call(buffer, value, offset)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
get gain () { return this._gain }
|
|
121
|
+
setGain (gain) {
|
|
122
|
+
this._assertOpen()
|
|
123
|
+
|
|
124
|
+
validateNonNegativeFloat('gain', gain)
|
|
125
|
+
|
|
126
|
+
Bindings.audio_setGain(this._id, gain)
|
|
127
|
+
|
|
128
|
+
this._gain = gain
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
get frequencyRatio () { return this._frequencyRatio }
|
|
132
|
+
setFrequencyRatio (frequencyRatio) {
|
|
133
|
+
this._assertOpen()
|
|
134
|
+
|
|
135
|
+
validateNumberRange('frequencyRatio', frequencyRatio, 0.01, 100)
|
|
136
|
+
|
|
137
|
+
Bindings.audio_setFrequencyRatio(this._id, frequencyRatio)
|
|
138
|
+
|
|
139
|
+
this._frequencyRatio = frequencyRatio
|
|
81
140
|
}
|
|
82
141
|
|
|
83
142
|
get playing () { return this._playing }
|
|
84
143
|
play (play = true) {
|
|
85
|
-
|
|
144
|
+
this._assertOpen()
|
|
86
145
|
|
|
87
|
-
|
|
146
|
+
validateBoolean('play', play)
|
|
88
147
|
|
|
89
148
|
Bindings.audio_play(this._id, play)
|
|
90
149
|
|
|
@@ -96,14 +155,14 @@ class AudioStream extends EventsViaPoll {
|
|
|
96
155
|
}
|
|
97
156
|
|
|
98
157
|
clear () {
|
|
99
|
-
|
|
158
|
+
this._assertOpen()
|
|
100
159
|
|
|
101
160
|
Bindings.audio_clear(this._id)
|
|
102
161
|
}
|
|
103
162
|
|
|
104
163
|
get closed () { return this._closed }
|
|
105
164
|
close () {
|
|
106
|
-
|
|
165
|
+
this._assertOpen()
|
|
107
166
|
|
|
108
167
|
this._closed = true
|
|
109
168
|
this._playing = false
|
|
@@ -111,13 +170,11 @@ class AudioStream extends EventsViaPoll {
|
|
|
111
170
|
Globals.audioStreams.delete(this._id)
|
|
112
171
|
|
|
113
172
|
// This call could throw if the device is gone
|
|
114
|
-
try { Bindings.audio_close(this._id) }
|
|
115
|
-
catch
|
|
116
|
-
|
|
117
|
-
updateDevice(this._device)
|
|
173
|
+
try { Object.assign(this._device, Bindings.audio_close(this._id)) }
|
|
174
|
+
catch { }
|
|
118
175
|
|
|
119
176
|
// We might be inside an event listener
|
|
120
|
-
this._retire(() =>
|
|
177
|
+
this._retire(() => this._makeClosedError())
|
|
121
178
|
|
|
122
179
|
Globals.events.withoutPolling(() => {
|
|
123
180
|
try { this.emit('close', { type: 'close' }) }
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const Bindings = require('../bindings')
|
|
2
|
+
const Enums = require('../enums')
|
|
3
|
+
const { getFormatHelpers } = require('./format-helpers')
|
|
4
|
+
const { INT32_MAX, validateIntegerRange, validatePositiveInt32, validateObject, validateBuffer } = require('../validate')
|
|
5
|
+
|
|
6
|
+
const checkSpec = (spec, name) => {
|
|
7
|
+
validateObject(name, spec)
|
|
8
|
+
const { format = 'f32', channels = 1, frequency = 48000 } = spec
|
|
9
|
+
const helpers = getFormatHelpers(format, `${name}.format`)
|
|
10
|
+
validateIntegerRange(`${name}.channels`, channels, 1, 8)
|
|
11
|
+
validatePositiveInt32(`${name}.frequency`, frequency)
|
|
12
|
+
return { format: Enums.audioFormat[format], channels, frequency, frameSize: channels * helpers.bytesPerSample }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const checkSource = (src, srcData) => {
|
|
16
|
+
if (srcData.length % src.frameSize !== 0) { throw Object.assign(new Error(`srcData length must be a multiple of ${src.frameSize}`), { length: srcData.length }) }
|
|
17
|
+
if (srcData.length > INT32_MAX) { throw Object.assign(new Error("srcData must be smaller than 2 GiB"), { length: srcData.length }) }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const convert = (srcSpec, srcData, dstSpec, dstData) => {
|
|
21
|
+
const src = checkSpec(srcSpec, 'srcSpec')
|
|
22
|
+
validateBuffer('srcData', srcData)
|
|
23
|
+
const dst = checkSpec(dstSpec, 'dstSpec')
|
|
24
|
+
checkSource(src, srcData)
|
|
25
|
+
|
|
26
|
+
const dstSize = Math.ceil(srcData.length / src.frameSize * dst.frequency / src.frequency) * dst.frameSize
|
|
27
|
+
if (dstSize > INT32_MAX) { throw Object.assign(new Error("converted data must be smaller than 2 GiB"), { size: dstSize }) }
|
|
28
|
+
|
|
29
|
+
if (dstData === undefined) {
|
|
30
|
+
if (srcData.length === 0) { return Buffer.alloc(0) }
|
|
31
|
+
const buffer = Buffer.alloc(dstSize)
|
|
32
|
+
const written = Bindings.audio_convert(src.format, src.channels, src.frequency, srcData, srcData.length, dst.format, dst.channels, dst.frequency, buffer)
|
|
33
|
+
return buffer.subarray(0, written)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
validateBuffer('dstData', dstData)
|
|
37
|
+
if (srcData.length === 0) { return 0 }
|
|
38
|
+
return Bindings.audio_convert(src.format, src.channels, src.frequency, srcData, srcData.length, dst.format, dst.channels, dst.frequency, dstData)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = { convert }
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const Globals = require('../globals')
|
|
2
|
+
const Bindings = require('../bindings')
|
|
3
|
+
|
|
4
|
+
const refreshDevices = () => {
|
|
5
|
+
Globals.audioDevices.playback = Bindings.audio_getDevices(false)
|
|
6
|
+
Globals.audioDevices.recording = Bindings.audio_getDevices(true)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
refreshDevices,
|
|
11
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const Globals = require('../globals')
|
|
2
|
+
const { audio: audioModule } = require('.')
|
|
3
|
+
const { tryEmit, tryCall } = require('../events/try')
|
|
4
|
+
|
|
5
|
+
const handleEvent = (event) => {
|
|
6
|
+
// Thread might not have initialized this subsystem
|
|
7
|
+
if (!Globals.initialized.audio) { return }
|
|
8
|
+
|
|
9
|
+
const { type, audioDeviceType, defaultDevice } = event
|
|
10
|
+
delete event.audioDeviceType
|
|
11
|
+
delete event.defaultDevice
|
|
12
|
+
|
|
13
|
+
const list = Globals.audioDevices[audioDeviceType]
|
|
14
|
+
const emitter = audioModule[audioDeviceType]
|
|
15
|
+
|
|
16
|
+
Object.assign(list[0], defaultDevice)
|
|
17
|
+
|
|
18
|
+
switch (type) {
|
|
19
|
+
case 'deviceAdd': {
|
|
20
|
+
const { device } = event
|
|
21
|
+
delete event.device
|
|
22
|
+
|
|
23
|
+
if (list.some((a) => a.id === device.id)) { return }
|
|
24
|
+
list.push(device)
|
|
25
|
+
event.device = device
|
|
26
|
+
} break
|
|
27
|
+
|
|
28
|
+
case 'deviceRemove': {
|
|
29
|
+
const { deviceId } = event
|
|
30
|
+
delete event.deviceId
|
|
31
|
+
|
|
32
|
+
const logical = Globals.audioStreams.get(deviceId)
|
|
33
|
+
if (logical) {
|
|
34
|
+
tryCall(() => { logical.close() })
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const index = list.findIndex((a) => a.id === deviceId)
|
|
39
|
+
if (index === -1) { return }
|
|
40
|
+
const device = list[index]
|
|
41
|
+
list.splice(index, 1)
|
|
42
|
+
|
|
43
|
+
for (const stream of [ ...Globals.audioStreams.values() ]) {
|
|
44
|
+
if (stream.device === device) {
|
|
45
|
+
tryCall(() => { stream.close() })
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
event.device = device
|
|
50
|
+
} break
|
|
51
|
+
|
|
52
|
+
case 'deviceFormatChange': {
|
|
53
|
+
const { deviceId, format } = event
|
|
54
|
+
const device = Globals.audioStreams.get(deviceId)?.device
|
|
55
|
+
?? list.find((a) => a.id === deviceId)
|
|
56
|
+
if (device) { Object.assign(device, format) }
|
|
57
|
+
} return
|
|
58
|
+
|
|
59
|
+
// No default
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
tryEmit(emitter, type, event)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
module.exports = { handleEvent }
|