@kmamal/sdl 0.11.13 → 1.0.0-2

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.
Files changed (86) hide show
  1. package/CHANGELOG.md +249 -0
  2. package/README.md +26 -2685
  3. package/binding.gyp +82 -65
  4. package/package.json +17 -11
  5. package/scripts/build-sdl.mjs +15 -7
  6. package/scripts/build.mjs +30 -15
  7. package/scripts/download-release.mjs +9 -6
  8. package/scripts/download-sdl.mjs +9 -6
  9. package/scripts/install.mjs +13 -5
  10. package/scripts/util/common.js +9 -1
  11. package/src/javascript/audio/audio-playback-stream.js +50 -0
  12. package/src/javascript/audio/audio-recording-stream.js +32 -0
  13. package/src/javascript/audio/{audio-instance.js → audio-stream.js} +35 -32
  14. package/src/javascript/audio/device.js +34 -6
  15. package/src/javascript/audio/format-helpers.js +26 -35
  16. package/src/javascript/audio/index.js +37 -26
  17. package/src/javascript/audio/prevent-exit.js +19 -9
  18. package/src/javascript/cleanup.js +19 -10
  19. package/src/javascript/enums.js +5 -0
  20. package/src/javascript/events/events-via-poll.js +25 -7
  21. package/src/javascript/events/index.js +274 -175
  22. package/src/javascript/gamepad/device.js +23 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +209 -0
  24. package/src/javascript/gamepad/index.js +51 -0
  25. package/src/javascript/globals.js +8 -5
  26. package/src/javascript/helpers.js +34 -7
  27. package/src/javascript/index.js +5 -3
  28. package/src/javascript/joystick/device.js +5 -10
  29. package/src/javascript/joystick/index.js +7 -7
  30. package/src/javascript/joystick/joystick-instance.js +79 -42
  31. package/src/javascript/joystick/shared.js +88 -0
  32. package/src/javascript/keyboard/index.js +6 -4
  33. package/src/javascript/keyboard/key-mapping.js +70 -28
  34. package/src/javascript/mouse/index.js +21 -10
  35. package/src/javascript/sensor/devices.js +17 -0
  36. package/src/javascript/sensor/index.js +2 -16
  37. package/src/javascript/sensor/sensor-instance.js +20 -6
  38. package/src/javascript/touch/devices.js +17 -0
  39. package/src/javascript/touch/index.js +3 -7
  40. package/src/javascript/video/format-helpers.js +99 -0
  41. package/src/javascript/video/index.js +37 -5
  42. package/src/javascript/video/window.js +280 -134
  43. package/src/native/audio.cpp +182 -58
  44. package/src/native/audio.h +10 -5
  45. package/src/native/clipboard.cpp +22 -5
  46. package/src/native/cocoa-window.h +1 -0
  47. package/src/native/cocoa-window.mm +1 -1
  48. package/src/native/enums.cpp +91 -58
  49. package/src/native/events.cpp +437 -284
  50. package/src/native/events.h +18 -12
  51. package/src/native/gamepad.cpp +185 -0
  52. package/src/native/gamepad.h +27 -0
  53. package/src/native/global.cpp +173 -167
  54. package/src/native/global.h +0 -2
  55. package/src/native/joystick.cpp +287 -225
  56. package/src/native/joystick.h +7 -6
  57. package/src/native/keyboard.cpp +28 -8
  58. package/src/native/keyboard.h +4 -2
  59. package/src/native/module.cpp +14 -8
  60. package/src/native/mouse.cpp +54 -34
  61. package/src/native/mouse.h +0 -1
  62. package/src/native/power.cpp +10 -4
  63. package/src/native/power.h +1 -1
  64. package/src/native/sensor.cpp +52 -52
  65. package/src/native/sensor.h +1 -1
  66. package/src/native/touch.cpp +45 -24
  67. package/src/native/touch.h +1 -1
  68. package/src/native/video.cpp +115 -75
  69. package/src/native/video.h +5 -2
  70. package/src/native/window.cpp +406 -318
  71. package/src/native/window.h +3 -1
  72. package/src/types/helpers.d.ts +44 -19
  73. package/src/types/index.d.ts +343 -255
  74. package/scripts/util/fetch.js +0 -85
  75. package/src/javascript/audio/audio-playback-instance.js +0 -23
  76. package/src/javascript/audio/audio-recording-instance.js +0 -17
  77. package/src/javascript/controller/controller-instance.js +0 -160
  78. package/src/javascript/controller/device.js +0 -26
  79. package/src/javascript/controller/index.js +0 -33
  80. package/src/javascript/events/reconcile-audio-devices.js +0 -16
  81. package/src/javascript/events/reconcile-displays.js +0 -17
  82. package/src/javascript/events/reconcile-joystick-and-controller-devices.js +0 -40
  83. package/src/javascript/events/reconcile.js +0 -61
  84. package/src/javascript/video/display.js +0 -4
  85. package/src/native/controller.cpp +0 -192
  86. package/src/native/controller.h +0 -26
package/binding.gyp CHANGED
@@ -1,67 +1,84 @@
1
1
  {
2
- 'targets': [{
3
- 'target_name': 'sdl',
4
- 'sources': [
5
- 'src/native/module.cpp',
6
- 'src/native/enums.cpp',
7
- 'src/native/global.cpp',
8
- 'src/native/events.cpp',
9
- 'src/native/video.cpp',
10
- 'src/native/window.cpp',
11
- 'src/native/keyboard.cpp',
12
- 'src/native/mouse.cpp',
13
- 'src/native/touch.cpp',
14
- 'src/native/joystick.cpp',
15
- 'src/native/controller.cpp',
16
- 'src/native/sensor.cpp',
17
- 'src/native/audio.cpp',
18
- 'src/native/clipboard.cpp',
19
- 'src/native/power.cpp',
20
- ],
21
- 'dependencies': [
22
- "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
23
- ],
24
- 'defines': [
25
- 'NAPI_VERSION=<(napi_build_version)',
26
- 'NODE_ADDON_API_DISABLE_DEPRECATED',
27
- ],
28
- 'cflags': [ '-Werror', '-Wall', '-Wextra' ],
29
- 'conditions': [
30
- ['OS == "linux"', {
31
- 'cflags': [ '-D_REENTRANT' ],
32
- 'cflags_cc': [ '-std=c++17' ],
33
- 'include_dirs': [ '$(SDL_INC)' ],
34
- 'libraries': [ '-L$(SDL_LIB)', '-lSDL2' ],
35
- 'link_settings': {
36
- 'libraries': [ "-Wl,-rpath,'$$ORIGIN'" ],
37
- },
38
- }],
39
- ['OS == "mac"', {
40
- 'sources': [
41
- 'src/native/cocoa-global.mm',
42
- 'src/native/cocoa-window.mm',
43
- ],
44
- 'cflags': [ '-D_THREAD_SAFE' ],
45
- 'xcode_settings': { 'OTHER_CFLAGS': [ '-std=c++17' ] },
46
- 'include_dirs': [
47
- '$(SDL_INC)',
48
- '/opt/X11/include',
49
- ],
50
- 'libraries': [ '-L$(SDL_LIB)', '-lSDL2' ],
51
- 'link_settings': {
52
- 'libraries': [ '-Wl,-rpath,@loader_path' ],
53
- },
54
- }],
55
- ['OS == "win"', {
56
- 'cflags': [ '-D_REENTRANT' ],
57
- 'msvs_settings': {
58
- 'VCCLCompilerTool': {
59
- 'AdditionalOptions': [ '-std:c++17' ],
60
- },
61
- },
62
- 'include_dirs': [ '<!(echo %SDL_INC%)' ],
63
- 'libraries': [ '-l<!(echo %SDL_LIB%)\\SDL2.lib' ],
64
- }],
65
- ],
66
- }],
2
+ 'targets': [
3
+ {
4
+ 'target_name':
5
+ 'sdl',
6
+ 'sources': [
7
+ 'src/native/module.cpp',
8
+ 'src/native/enums.cpp',
9
+ 'src/native/global.cpp',
10
+ 'src/native/events.cpp',
11
+ 'src/native/video.cpp',
12
+ 'src/native/window.cpp',
13
+ 'src/native/keyboard.cpp',
14
+ 'src/native/mouse.cpp',
15
+ 'src/native/touch.cpp',
16
+ 'src/native/joystick.cpp',
17
+ 'src/native/gamepad.cpp',
18
+ 'src/native/sensor.cpp',
19
+ 'src/native/audio.cpp',
20
+ 'src/native/clipboard.cpp',
21
+ 'src/native/power.cpp',
22
+ ],
23
+ 'dependencies': [
24
+ "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
25
+ ],
26
+ 'defines': [
27
+ # Node-API version of the oldest supported Node.js (v22.0.0)
28
+ 'NAPI_VERSION=9',
29
+ 'NODE_ADDON_API_DISABLE_DEPRECATED',
30
+ ],
31
+ 'cflags': ['-Werror', '-Wall', '-Wextra'],
32
+ 'conditions': [
33
+ [
34
+ 'OS == "linux" or OS == "freebsd" or OS == "openbsd" or OS == "netbsd"', {
35
+ 'cflags': ['-D_REENTRANT'],
36
+ 'cflags_cc': ['-std=c++17'],
37
+ 'include_dirs': ['$(SDL_INC)'],
38
+ 'libraries': ['-L$(SDL_LIB)', '-lSDL3'],
39
+ 'link_settings': {
40
+ 'libraries': ["-Wl,-rpath,'$$ORIGIN'"],
41
+ },
42
+ }
43
+ ],
44
+ [
45
+ 'OS == "mac"', {
46
+ 'sources': [
47
+ 'src/native/cocoa-global.mm',
48
+ 'src/native/cocoa-window.mm',
49
+ ],
50
+ 'defines': ['_THREAD_SAFE'],
51
+ 'xcode_settings': {
52
+ 'OTHER_CFLAGS': ['-std=c++17'],
53
+ 'WARNING_CFLAGS': ['-Wall', '-Wextra'],
54
+ 'GCC_TREAT_WARNINGS_AS_ERRORS': 'YES',
55
+ },
56
+ 'include_dirs': [
57
+ '$(SDL_INC)',
58
+ '/opt/X11/include',
59
+ ],
60
+ 'libraries': ['-L$(SDL_LIB)', '-lSDL3'],
61
+ 'link_settings': {
62
+ 'libraries': ['-Wl,-rpath,@loader_path'],
63
+ },
64
+ }
65
+ ],
66
+ [
67
+ 'OS == "win"', {
68
+ 'defines': ['_REENTRANT'],
69
+ 'msvs_settings': {
70
+ 'VCCLCompilerTool': {
71
+ 'AdditionalOptions': ['-std:c++17'],
72
+ 'WarningLevel': '4',
73
+ 'WarnAsError': 'true',
74
+ 'DisableSpecificWarnings': ['4127'],
75
+ },
76
+ },
77
+ 'include_dirs': ['<!(echo %SDL_INC%)'],
78
+ 'libraries': ['-l<!(echo %SDL_LIB%)\\SDL3.lib'],
79
+ }
80
+ ],
81
+ ],
82
+ }
83
+ ],
67
84
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
- "version": "0.11.13",
2
+ "version": "1.0.0-2",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
6
6
  "sdl",
7
- "sdl2",
7
+ "sdl3",
8
8
  "game",
9
9
  "gui",
10
10
  "desktop",
@@ -40,6 +40,9 @@
40
40
  "type": "git",
41
41
  "url": "git+ssh://git@github.com/kmamal/node-sdl.git"
42
42
  },
43
+ "engines": {
44
+ "node": ">=22"
45
+ },
43
46
  "main": "./src/javascript/index.js",
44
47
  "types": "./src/types/index.d.ts",
45
48
  "exports": {
@@ -63,21 +66,24 @@
63
66
  "release": "cd scripts && node release.mjs",
64
67
  "clean": "cd scripts && node clean.mjs"
65
68
  },
66
- "dependencies": {
67
- "tar": "^7.4.3"
68
- },
69
69
  "devDependencies": {
70
- "@kmamal/evdev": "^0.0.6",
71
- "@kmamal/testing": "^0.0.33",
72
- "@types/node": "^24.1.0",
73
- "node-addon-api": "^8.5.0",
74
- "node-gyp": "^11.2.0"
70
+ "@kmamal/evdev": "0.0.8",
71
+ "@kmamal/testing": "0.0.34",
72
+ "@types/node": "26.3.0",
73
+ "node-addon-api": "8.9.2",
74
+ "node-gyp": "13.0.2"
75
75
  },
76
76
  "config": {
77
77
  "sdl": {
78
78
  "owner": "kmamal",
79
79
  "repo": "build-sdl",
80
- "version": "2.32.8"
80
+ "version": "3.4.14"
81
81
  }
82
+ },
83
+ "allowScripts": {
84
+ "@kmamal/evdev": true
85
+ },
86
+ "dependencies": {
87
+ "modern-tar": "0.8.4"
82
88
  }
83
89
  }
@@ -1,24 +1,28 @@
1
1
  import Fs from 'node:fs'
2
2
  import Path from 'node:path'
3
- import { once } from 'node:events'
3
+ import Zlib from 'node:zlib'
4
+ import Stream from 'node:stream'
5
+ import { pathToFileURL } from 'node:url'
4
6
  import C from './util/common.js'
5
- import { fetch } from './util/fetch.js'
6
- import * as Tar from 'tar'
7
+ import { unpackTar } from 'modern-tar/fs'
7
8
 
8
9
  const url = `https://github.com/${C.sdl.owner}/${C.sdl.repo}/archive/refs/tags/v${C.sdl.version}.tar.gz`
9
10
 
10
11
  console.log("fetch", url)
11
12
  const response = await fetch(url)
13
+ if (!response.ok) { throw new Error(`bad status code ${response.status}`) }
12
14
 
13
15
  console.log("unpack to", C.dir.sdl)
14
16
  await Fs.promises.rm(C.dir.sdl, { recursive: true }).catch(() => {})
15
17
  await Fs.promises.mkdir(C.dir.sdl, { recursive: true })
16
- const tar = Tar.extract({ gzip: true, C: C.dir.sdl })
17
- response.stream().pipe(tar)
18
- await once(tar, 'finish')
18
+ await Stream.promises.pipeline(
19
+ Stream.Readable.fromWeb(response.body),
20
+ Zlib.createGunzip(),
21
+ unpackTar(C.dir.sdl),
22
+ )
19
23
 
20
24
  const sdlRoot = Path.join(C.dir.sdl, `${C.sdl.repo}-${C.sdl.version}`)
21
- await import(Path.join(sdlRoot, '/scripts/build.mjs'))
25
+ await import(pathToFileURL(Path.join(sdlRoot, 'scripts/build.mjs')).href)
22
26
  const sdlDist = Path.join(sdlRoot, 'dist')
23
27
  await Promise.all([
24
28
  Fs.promises.cp(
@@ -34,4 +38,8 @@ await Promise.all([
34
38
  verbatimSymlinks: true,
35
39
  },
36
40
  ),
41
+ Fs.promises.cp(
42
+ Path.join(sdlDist, 'LICENSE.txt'),
43
+ Path.join(C.dir.sdl, 'LICENSE.txt'),
44
+ ),
37
45
  ])
package/scripts/build.mjs CHANGED
@@ -11,9 +11,17 @@ await Promise.all([
11
11
  await Fs.promises.rm(dir, { recursive: true }).catch(() => {})
12
12
  }))
13
13
 
14
+ let SDL_INC = Path.join(C.dir.sdl, 'include')
15
+ let SDL_LIB = Path.join(C.dir.sdl, 'lib')
16
+ if (C.systemSdl) {
17
+ const pkgConfig = process.env.PKG_CONFIG || 'pkg-config'
18
+ const query = (variable) => execSync(`${pkgConfig} --variable=${variable} sdl3`, { encoding: 'utf8' }).trim()
19
+ SDL_INC = process.env.SDL_INC || query('includedir')
20
+ SDL_LIB = process.env.SDL_LIB || query('libdir')
21
+ console.log("system sdl in", SDL_INC, SDL_LIB)
22
+ }
23
+
14
24
  console.log("build in", C.dir.build)
15
- const SDL_INC = Path.join(C.dir.sdl, 'include')
16
- const SDL_LIB = Path.join(C.dir.sdl, 'lib')
17
25
 
18
26
  let archFlag = ''
19
27
  if (process.env.CROSS_COMPILE_ARCH) {
@@ -21,7 +29,7 @@ if (process.env.CROSS_COMPILE_ARCH) {
21
29
  }
22
30
 
23
31
  let parallelFlag = '-j max'
24
- if (process.env.NO_PARALLEL) {
32
+ if (C.noParallel) {
25
33
  parallelFlag = ''
26
34
  }
27
35
 
@@ -43,20 +51,27 @@ await Promise.all([
43
51
  Path.join(C.dir.build, 'Release/sdl.node'),
44
52
  Path.join(C.dir.dist, 'sdl.node'),
45
53
  ),
46
- (async () => {
47
- const libs = await Fs.promises.readdir(SDL_LIB)
48
- await Promise.all(libs.map(async (name) => {
49
- if (C.platform === 'win32' && name !== 'SDL2.dll') { return }
50
- await Fs.promises.cp(
51
- Path.join(SDL_LIB, name),
52
- Path.join(C.dir.dist, name),
53
- { verbatimSymlinks: true },
54
- )
55
- }))
56
- })(),
54
+ ...C.systemSdl ? [] : [
55
+ (async () => {
56
+ const libs = await Fs.promises.readdir(SDL_LIB)
57
+ await Promise.all(libs.map(async (name) => {
58
+ if (C.platform === 'win32' && name !== 'SDL3.dll') { return }
59
+ await Fs.promises.cp(
60
+ Path.join(SDL_LIB, name),
61
+ Path.join(C.dir.dist, name),
62
+ { verbatimSymlinks: true },
63
+ )
64
+ }))
65
+ })(),
66
+ // Include SDL's license
67
+ Fs.promises.cp(
68
+ Path.join(C.dir.sdl, 'LICENSE.txt'),
69
+ Path.join(C.dir.dist, 'LICENSE.SDL.txt'),
70
+ ),
71
+ ],
57
72
  ])
58
73
 
59
74
  // Strip binaries on linux
60
75
  if (C.platform === 'linux') {
61
- execSync(`strip -s ${Path.join(C.dir.dist, 'sdl.node')}`)
76
+ execSync(`strip -s "${Path.join(C.dir.dist, 'sdl.node')}"`)
62
77
  }
@@ -1,17 +1,20 @@
1
1
  import Fs from 'node:fs'
2
- import { once } from 'node:events'
2
+ import Zlib from 'node:zlib'
3
+ import Stream from 'node:stream'
3
4
  import C from './util/common.js'
4
- import { fetch } from './util/fetch.js'
5
- import * as Tar from 'tar'
5
+ import { unpackTar } from 'modern-tar/fs'
6
6
 
7
7
  const url = `https://github.com/${C.owner}/${C.repo}/releases/download/v${C.version}/${C.assetName}`
8
8
 
9
9
  console.log("fetch", url)
10
10
  const response = await fetch(url)
11
+ if (!response.ok) { throw new Error(`bad status code ${response.status}`) }
11
12
 
12
13
  console.log("unpack to", C.dir.dist)
13
14
  await Fs.promises.rm(C.dir.dist, { recursive: true }).catch(() => {})
14
15
  await Fs.promises.mkdir(C.dir.dist, { recursive: true })
15
- const tar = Tar.extract({ gzip: true, C: C.dir.dist })
16
- response.stream().pipe(tar)
17
- await once(tar, 'finish')
16
+ await Stream.promises.pipeline(
17
+ Stream.Readable.fromWeb(response.body),
18
+ Zlib.createGunzip(),
19
+ unpackTar(C.dir.dist),
20
+ )
@@ -1,21 +1,24 @@
1
1
  import Fs from 'node:fs'
2
- import { once } from 'node:events'
2
+ import Zlib from 'node:zlib'
3
+ import Stream from 'node:stream'
3
4
  import C from './util/common.js'
4
- import { fetch } from './util/fetch.js'
5
- import * as Tar from 'tar'
5
+ import { unpackTar } from 'modern-tar/fs'
6
6
 
7
7
  const url = `https://github.com/${C.sdl.owner}/${C.sdl.repo}/releases/download/v${C.sdl.version}/${C.sdl.assetName}`
8
8
 
9
9
  try {
10
10
  console.log("fetch", url)
11
11
  const response = await fetch(url)
12
+ if (!response.ok) { throw new Error(`bad status code ${response.status}`) }
12
13
 
13
14
  console.log("unpack to", C.dir.sdl)
14
15
  await Fs.promises.rm(C.dir.sdl, { recursive: true }).catch(() => {})
15
16
  await Fs.promises.mkdir(C.dir.sdl, { recursive: true })
16
- const tar = Tar.extract({ gzip: true, C: C.dir.sdl })
17
- response.stream().pipe(tar)
18
- await once(tar, 'finish')
17
+ await Stream.promises.pipeline(
18
+ Stream.Readable.fromWeb(response.body),
19
+ Zlib.createGunzip(),
20
+ unpackTar(C.dir.sdl),
21
+ )
19
22
  }
20
23
  catch (error) {
21
24
  console.log(error)
@@ -1,11 +1,19 @@
1
1
 
2
- if (!process.env.NODE_SDL_FROM_SOURCE) {
2
+ import C from './util/common.js'
3
+
4
+ if (C.systemSdl) {
5
+ console.log("build against the system sdl")
6
+ await import('./build.mjs')
7
+ process.exit(0)
8
+ }
9
+
10
+ if (!C.fromSource) {
3
11
  try {
4
12
  await import('./download-release.mjs')
5
13
  process.exit(0)
6
14
  }
7
- catch (_) {
8
- console.log("failed to download release")
15
+ catch (error) {
16
+ console.log("failed to download release:", error.cause?.message ?? error.message)
9
17
  }
10
18
  }
11
19
  else {
@@ -15,8 +23,8 @@ else {
15
23
  try {
16
24
  await import('./download-sdl.mjs')
17
25
  }
18
- catch (_) {
19
- console.log("failed to download sdl")
26
+ catch (error) {
27
+ console.log("failed to download sdl:", error.cause?.message ?? error.message)
20
28
  await import('./build-sdl.mjs')
21
29
  }
22
30
 
@@ -12,7 +12,12 @@ const pkgPath = Path.join(dir.root, 'package.json')
12
12
  const pkg = JSON.parse(Fs.readFileSync(pkgPath).toString())
13
13
  const version = pkg.version
14
14
  const isPrerelease = version.includes('-')
15
- const [ , owner, repo ] = pkg.repository.url.match(/([^/:]+)\/([^/]+).git$/u)
15
+ const [ , owner, repo ] = pkg.repository.url.match(/([^/:]+)\/([^/]+)\.git$/u)
16
+
17
+ const isSet = (value) => ![ undefined, '', '0', 'false' ].includes(value)
18
+ const fromSource = isSet(process.env.NODE_SDL_FROM_SOURCE)
19
+ const systemSdl = isSet(process.env.NODE_SDL_SYSTEM)
20
+ const noParallel = isSet(process.env.NO_PARALLEL)
16
21
 
17
22
  const { platform, arch } = process
18
23
  const targetArch = process.env.CROSS_COMPILE_ARCH || arch
@@ -23,6 +28,9 @@ sdl.assetName = `SDL-v${sdl.version}-${platform}-${targetArch}.tar.gz`
23
28
 
24
29
  module.exports = {
25
30
  dir,
31
+ fromSource,
32
+ systemSdl,
33
+ noParallel,
26
34
  version,
27
35
  isPrerelease,
28
36
  owner,
@@ -0,0 +1,50 @@
1
+ const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
+ const { AudioStream } = require('./audio-stream')
4
+ const { resetTimeout } = require('./prevent-exit')
5
+
6
+ class AudioPlaybackStream extends AudioStream {
7
+ constructor (device, options) {
8
+ super(false, device, options)
9
+ }
10
+
11
+ get queued () {
12
+ Globals.events.poll()
13
+ if (this._closed) { throw Object.assign(new Error("stream is closed"), { id: this._id }) }
14
+
15
+ return Bindings.audio_getQueued(this._id)
16
+ }
17
+
18
+ putData (buffer, bytes = null) {
19
+ if (this._closed) { throw Object.assign(new Error("stream is closed"), { id: this._id }) }
20
+
21
+ if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
22
+ bytes ??= buffer.length
23
+ if (!Number.isInteger(bytes)) { throw Object.assign(new Error("bytes must be an integer"), { bytes }) }
24
+ if (bytes < 0 || bytes > 2 ** 31 - 1) { throw Object.assign(new Error("bytes must be a non-negative 32-bit integer"), { 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 }) }
28
+
29
+ if (bytes === 0) { return }
30
+
31
+ Bindings.audio_putData(this._id, buffer, bytes)
32
+ }
33
+
34
+ clear () {
35
+ super.clear()
36
+ resetTimeout()
37
+ }
38
+
39
+ play (play = true) {
40
+ super.play(play)
41
+ if (!play) { resetTimeout() }
42
+ }
43
+
44
+ close () {
45
+ try { super.close() }
46
+ finally { resetTimeout() }
47
+ }
48
+ }
49
+
50
+ module.exports = { AudioPlaybackStream }
@@ -0,0 +1,32 @@
1
+ const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
+ const { AudioStream } = require('./audio-stream')
4
+
5
+ class AudioRecordingStream extends AudioStream {
6
+ constructor (device, options) {
7
+ super(true, device, options)
8
+ }
9
+
10
+ get available () {
11
+ Globals.events.poll()
12
+ if (this._closed) { throw Object.assign(new Error("stream is closed"), { id: this._id }) }
13
+
14
+ return Bindings.audio_getAvailable(this._id)
15
+ }
16
+
17
+ getData (buffer, bytes = null) {
18
+ if (this._closed) { throw Object.assign(new Error("stream is closed"), { id: this._id }) }
19
+
20
+ if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
21
+ bytes ??= buffer.length
22
+ if (!Number.isInteger(bytes)) { throw Object.assign(new Error("bytes must be an integer"), { bytes }) }
23
+ if (bytes < 0 || bytes > 2 ** 31 - 1) { throw Object.assign(new Error("bytes must be a non-negative 32-bit integer"), { bytes }) }
24
+ if (buffer.length < bytes) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, bytes }) }
25
+
26
+ if (bytes === 0) { return 0 }
27
+
28
+ return Bindings.audio_getData(this._id, buffer, bytes)
29
+ }
30
+ }
31
+
32
+ module.exports = { AudioRecordingStream }