sfml.js 1.0.0 → 1.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.
Files changed (313) hide show
  1. package/README.ja.md +10 -9
  2. package/README.ko.md +10 -9
  3. package/README.md +10 -9
  4. package/README.zh-CN.md +10 -9
  5. package/binding.gyp +23 -16
  6. package/dist/audio/effect-processor.js +9 -3
  7. package/dist/audio/input-sound-file.js +41 -15
  8. package/dist/audio/listener.js +33 -11
  9. package/dist/audio/music.js +41 -18
  10. package/dist/audio/output-sound-file.js +4 -2
  11. package/dist/audio/playback-device.js +11 -3
  12. package/dist/audio/sound-buffer-recorder.js +8 -5
  13. package/dist/audio/sound-buffer.js +40 -17
  14. package/dist/audio/sound-file-factory.js +21 -9
  15. package/dist/audio/sound-file-reader.js +21 -7
  16. package/dist/audio/sound-recorder.js +36 -15
  17. package/dist/audio/sound-source.js +117 -40
  18. package/dist/audio/sound-stream.js +14 -5
  19. package/dist/audio/sound.js +9 -4
  20. package/dist/audio/value.js +42 -16
  21. package/dist/graphics/blend-mode.js +2 -1
  22. package/dist/graphics/boolean-vector.d.ts +1 -2
  23. package/dist/graphics/boolean-vector.js +19 -9
  24. package/dist/graphics/color.js +2 -1
  25. package/dist/graphics/convex-shape.js +7 -3
  26. package/dist/graphics/font.js +52 -23
  27. package/dist/graphics/glsl-matrix.js +8 -3
  28. package/dist/graphics/glsl.js +47 -16
  29. package/dist/graphics/glyph.js +27 -10
  30. package/dist/graphics/image.js +30 -14
  31. package/dist/graphics/rect.js +15 -8
  32. package/dist/graphics/render-states.js +18 -9
  33. package/dist/graphics/render-target.js +49 -23
  34. package/dist/graphics/render-texture.js +24 -12
  35. package/dist/graphics/render-window.js +6 -3
  36. package/dist/graphics/shader.js +67 -32
  37. package/dist/graphics/shape.js +25 -13
  38. package/dist/graphics/sprite.js +19 -9
  39. package/dist/graphics/stencil-mode.js +10 -6
  40. package/dist/graphics/swap.js +2 -1
  41. package/dist/graphics/text.js +123 -48
  42. package/dist/graphics/texture.js +48 -24
  43. package/dist/graphics/transform.js +29 -13
  44. package/dist/graphics/transformable.js +2 -1
  45. package/dist/graphics/vertex-array.js +40 -18
  46. package/dist/graphics/vertex-buffer.js +39 -19
  47. package/dist/graphics/vertex.js +6 -3
  48. package/dist/graphics/view.js +30 -14
  49. package/dist/index.d.ts +4 -4
  50. package/dist/internal/audio-codec-registry.js +12 -6
  51. package/dist/internal/audio-input-stream.js +2 -1
  52. package/dist/internal/audio-music-pump.js +58 -24
  53. package/dist/internal/audio-recorder-session.js +17 -7
  54. package/dist/internal/audio-worker-session.js +28 -10
  55. package/dist/internal/audio-worker.js +40 -21
  56. package/dist/internal/glsl-value.d.ts +3 -1
  57. package/dist/internal/glsl-value.js +18 -8
  58. package/dist/internal/graphics-state.js +29 -15
  59. package/dist/internal/input-stream.d.ts +3 -1
  60. package/dist/internal/input-stream.js +16 -7
  61. package/dist/internal/numeric-properties.js +2 -1
  62. package/dist/internal/render-states.js +2 -1
  63. package/dist/internal/scalar.js +22 -13
  64. package/dist/internal/shaped-glyph.d.ts +3 -1
  65. package/dist/internal/shaped-glyph.js +12 -4
  66. package/dist/internal/struct-view.d.ts +3 -1
  67. package/dist/internal/struct-view.js +3 -1
  68. package/dist/internal/transformable.js +2 -1
  69. package/dist/network/common.js +23 -11
  70. package/dist/network/dns.js +25 -17
  71. package/dist/network/ftp.js +43 -18
  72. package/dist/network/http.js +126 -56
  73. package/dist/network/ip-address.js +50 -25
  74. package/dist/network/packet.js +63 -25
  75. package/dist/network/sftp.js +175 -74
  76. package/dist/network/socket-selector.js +28 -14
  77. package/dist/network/socket.js +71 -30
  78. package/dist/network/tcp-listener.js +42 -19
  79. package/dist/network/tcp-socket.js +111 -53
  80. package/dist/network/tls-material.js +2 -1
  81. package/dist/network/udp-socket.js +68 -33
  82. package/dist/system/angle.js +2 -1
  83. package/dist/system/clock.js +4 -2
  84. package/dist/system/err.js +5 -2
  85. package/dist/system/file-input-stream.js +8 -4
  86. package/dist/system/input-stream.js +6 -3
  87. package/dist/system/operators.d.ts +1 -1
  88. package/dist/system/operators.js +90 -50
  89. package/dist/system/sleep.js +4 -2
  90. package/dist/system/time.js +12 -6
  91. package/dist/system/vector2.js +6 -7
  92. package/dist/system/vector3.js +11 -6
  93. package/dist/window/clipboard.js +2 -1
  94. package/dist/window/context.js +10 -5
  95. package/dist/window/cursor.js +2 -1
  96. package/dist/window/event.js +9 -5
  97. package/dist/window/mouse.js +4 -2
  98. package/dist/window/video-mode.js +2 -1
  99. package/dist/window/window.js +19 -12
  100. package/native/addon.cc +25 -12
  101. package/native/audio-binding.cc +266 -126
  102. package/native/audio-codec-binding.cc +147 -77
  103. package/native/audio-device-binding.cc +76 -49
  104. package/native/audio-file-binding.cc +116 -67
  105. package/native/audio-recorder-binding.cc +147 -98
  106. package/native/audio-recorder.h +1 -1
  107. package/native/audio-state.h +53 -30
  108. package/native/audio-stream-binding.cc +148 -94
  109. package/native/audio-stream.h +5 -4
  110. package/native/audio-worker-binding.cc +163 -81
  111. package/native/audio-worker.h +35 -19
  112. package/native/batch-drawable.cc +29 -16
  113. package/native/batch-drawable.h +11 -8
  114. package/native/context-binding.cc +40 -27
  115. package/native/context-settings.h +15 -8
  116. package/native/deferred-delete.cc +9 -6
  117. package/native/deferred-delete.h +7 -8
  118. package/native/error-stream-binding.cc +42 -28
  119. package/native/font-binding.cc +89 -48
  120. package/native/graphics-binding.cc +122 -72
  121. package/native/graphics-state.h +50 -36
  122. package/native/image-binding.cc +38 -24
  123. package/native/image-state.h +26 -18
  124. package/native/input-binding.cc +42 -27
  125. package/native/render-window-binding.cc +38 -23
  126. package/native/shader-binding.cc +115 -50
  127. package/native/shape-binding.cc +94 -49
  128. package/native/shape-state.h +18 -9
  129. package/native/shaped-glyph-storage.h +9 -5
  130. package/native/stream-binding.cc +143 -117
  131. package/native/system-binding.cc +17 -12
  132. package/native/text-binding.cc +68 -35
  133. package/native/text-state.h +25 -18
  134. package/native/texture-binding.cc +42 -33
  135. package/native/texture-resource.h +20 -12
  136. package/native/time-value.h +3 -1
  137. package/native/transformable-binding.cc +12 -6
  138. package/native/transformable-state.h +6 -6
  139. package/native/value-binding.h +46 -28
  140. package/native/value-layout.h +10 -10
  141. package/native/values-binding.cc +29 -23
  142. package/native/vertex-binding.cc +18 -12
  143. package/native/vertex-buffer-binding.cc +38 -24
  144. package/native/vertex-storage.h +2 -2
  145. package/native/view-data.h +4 -3
  146. package/native/window-binding.cc +75 -45
  147. package/native/window-event.h +20 -11
  148. package/native/window-owner.h +15 -12
  149. package/package.json +44 -51
  150. package/src/audio/effect-processor.ts +9 -3
  151. package/src/audio/input-sound-file.ts +108 -36
  152. package/src/audio/listener.ts +39 -13
  153. package/src/audio/music.ts +112 -47
  154. package/src/audio/output-sound-file.ts +40 -11
  155. package/src/audio/playback-device.ts +23 -8
  156. package/src/audio/sound-buffer-recorder.ts +35 -13
  157. package/src/audio/sound-buffer.ts +166 -58
  158. package/src/audio/sound-file-factory.ts +127 -49
  159. package/src/audio/sound-file-reader.ts +49 -15
  160. package/src/audio/sound-recorder.ts +100 -34
  161. package/src/audio/sound-source.ts +142 -45
  162. package/src/audio/sound-stream.ts +15 -5
  163. package/src/audio/sound.ts +12 -4
  164. package/src/audio/value.ts +89 -26
  165. package/src/graphics/blend-mode.ts +3 -1
  166. package/src/graphics/boolean-vector.ts +56 -26
  167. package/src/graphics/color.ts +3 -1
  168. package/src/graphics/convex-shape.ts +10 -3
  169. package/src/graphics/font.ts +171 -50
  170. package/src/graphics/glsl-matrix.ts +9 -3
  171. package/src/graphics/glsl.ts +81 -35
  172. package/src/graphics/glyph.ts +67 -26
  173. package/src/graphics/image.ts +119 -45
  174. package/src/graphics/rect.ts +56 -24
  175. package/src/graphics/render-states.ts +31 -14
  176. package/src/graphics/render-target.ts +197 -81
  177. package/src/graphics/render-texture.ts +36 -12
  178. package/src/graphics/render-window.ts +9 -3
  179. package/src/graphics/shader.ts +186 -61
  180. package/src/graphics/shape.ts +93 -35
  181. package/src/graphics/sprite.ts +64 -25
  182. package/src/graphics/stencil-mode.ts +12 -6
  183. package/src/graphics/swap.ts +3 -2
  184. package/src/graphics/text.ts +229 -74
  185. package/src/graphics/texture.ts +180 -61
  186. package/src/graphics/transform.ts +132 -47
  187. package/src/graphics/transformable.ts +3 -1
  188. package/src/graphics/vertex-array.ts +87 -35
  189. package/src/graphics/vertex-buffer.ts +97 -33
  190. package/src/graphics/vertex.ts +9 -3
  191. package/src/graphics/view.ts +59 -21
  192. package/src/index.ts +101 -23
  193. package/src/internal/audio-codec-registry.ts +18 -6
  194. package/src/internal/audio-input-stream.ts +6 -2
  195. package/src/internal/audio-music-pump.ts +116 -41
  196. package/src/internal/audio-native.ts +249 -96
  197. package/src/internal/audio-recorder-session.ts +68 -24
  198. package/src/internal/audio-worker-session.ts +65 -21
  199. package/src/internal/audio-worker.ts +85 -41
  200. package/src/internal/glsl-value.ts +26 -11
  201. package/src/internal/graphics-state.ts +57 -20
  202. package/src/internal/input-stream.ts +23 -10
  203. package/src/internal/native.ts +252 -99
  204. package/src/internal/numeric-properties.ts +2 -1
  205. package/src/internal/render-states.ts +3 -1
  206. package/src/internal/scalar.ts +33 -14
  207. package/src/internal/shaped-glyph.ts +25 -12
  208. package/src/internal/struct-view.ts +40 -13
  209. package/src/internal/transformable.ts +3 -1
  210. package/src/network/common.ts +35 -11
  211. package/src/network/dns.ts +95 -38
  212. package/src/network/ftp.ts +129 -47
  213. package/src/network/http.ts +232 -77
  214. package/src/network/ip-address.ts +118 -48
  215. package/src/network/packet.ts +161 -54
  216. package/src/network/sftp.ts +498 -168
  217. package/src/network/socket-selector.ts +66 -26
  218. package/src/network/socket.ts +133 -46
  219. package/src/network/tcp-listener.ts +78 -29
  220. package/src/network/tcp-socket.ts +240 -95
  221. package/src/network/tls-material.ts +6 -2
  222. package/src/network/udp-socket.ts +147 -53
  223. package/src/system/angle.ts +3 -1
  224. package/src/system/clock.ts +12 -4
  225. package/src/system/err.ts +5 -2
  226. package/src/system/file-input-stream.ts +12 -4
  227. package/src/system/input-stream.ts +9 -3
  228. package/src/system/operators.ts +290 -115
  229. package/src/system/sleep.ts +6 -2
  230. package/src/system/time.ts +18 -6
  231. package/src/system/timeout-with-predicate.ts +19 -8
  232. package/src/system/vector2.ts +101 -49
  233. package/src/system/vector3.ts +85 -36
  234. package/src/window/clipboard.ts +3 -1
  235. package/src/window/context.ts +15 -5
  236. package/src/window/cursor.ts +3 -1
  237. package/src/window/event.ts +26 -12
  238. package/src/window/mouse.ts +6 -2
  239. package/src/window/video-mode.ts +3 -1
  240. package/src/window/window.ts +119 -37
  241. package/test/native/audio-probes.cc +74 -36
  242. package/test/native/deferred-delete-probes.cc +17 -11
  243. package/test/native/drawable-probes.cc +70 -36
  244. package/test/native/exception-probes.cc +14 -9
  245. package/test/native/image-probes.cc +20 -10
  246. package/test/native/matrix-probes.cc +8 -4
  247. package/test/native/network-oracle.cc +17 -11
  248. package/test/native/render-texture-probes.cc +33 -24
  249. package/test/native/shader-probes.cc +9 -9
  250. package/test/native/shape-probes.cc +37 -23
  251. package/test/native/sprite-probes.cc +41 -25
  252. package/test/native/system-probes.cc +58 -35
  253. package/test/native/text-probes.cc +77 -44
  254. package/test/native/texture-probes.cc +43 -26
  255. package/test/native/time-probes.cc +18 -12
  256. package/test/native/transform-probes.cc +15 -10
  257. package/test/native/transformable-probes.cc +23 -17
  258. package/test/native/value-oracle.h +9 -4
  259. package/test/native/value-probes.cc +31 -23
  260. package/test/native/vertex-array-probes.cc +22 -14
  261. package/test/native/vertex-buffer-probes.cc +28 -17
  262. package/test/native/view-probes.cc +29 -17
  263. package/test/native/window-probes.cc +66 -33
  264. package/tools/ensure-sfml.cjs +9 -0
  265. package/tools/generate-network-constants.cjs +11 -6
  266. package/tools/generate-window-constants.cjs +31 -12
  267. package/docs/README.ja.md +0 -30
  268. package/docs/README.ko.md +0 -30
  269. package/docs/README.md +0 -30
  270. package/docs/README.zh-CN.md +0 -30
  271. package/docs/api.ja.md +0 -84
  272. package/docs/api.ko.md +0 -84
  273. package/docs/api.md +0 -81
  274. package/docs/api.zh-CN.md +0 -81
  275. package/docs/audio.ja.md +0 -249
  276. package/docs/audio.ko.md +0 -251
  277. package/docs/audio.md +0 -252
  278. package/docs/audio.zh-CN.md +0 -252
  279. package/docs/concepts.ja.md +0 -130
  280. package/docs/concepts.ko.md +0 -130
  281. package/docs/concepts.md +0 -131
  282. package/docs/concepts.zh-CN.md +0 -131
  283. package/docs/development.ja.md +0 -144
  284. package/docs/development.ko.md +0 -144
  285. package/docs/development.md +0 -146
  286. package/docs/development.zh-CN.md +0 -146
  287. package/docs/getting-started.ja.md +0 -154
  288. package/docs/getting-started.ko.md +0 -154
  289. package/docs/getting-started.md +0 -156
  290. package/docs/getting-started.zh-CN.md +0 -156
  291. package/docs/graphics.ja.md +0 -256
  292. package/docs/graphics.ko.md +0 -254
  293. package/docs/graphics.md +0 -255
  294. package/docs/graphics.zh-CN.md +0 -257
  295. package/docs/network.ja.md +0 -214
  296. package/docs/network.ko.md +0 -216
  297. package/docs/network.md +0 -218
  298. package/docs/network.zh-CN.md +0 -218
  299. package/docs/system.ja.md +0 -169
  300. package/docs/system.ko.md +0 -171
  301. package/docs/system.md +0 -173
  302. package/docs/system.zh-CN.md +0 -173
  303. package/docs/window.ja.md +0 -138
  304. package/docs/window.ko.md +0 -140
  305. package/docs/window.md +0 -141
  306. package/docs/window.zh-CN.md +0 -141
  307. package/native/exception-boundary.h +0 -97
  308. package/native/numeric-fields.h +0 -96
  309. package/native/shared-handle.h +0 -107
  310. package/test/tsconfig.json +0 -8
  311. package/tools/configure-lsp.cjs +0 -23
  312. package/tools/lib_downloader.cjs +0 -92
  313. package/tools/lib_downloader_urls.cjs +0 -23
@@ -1,6 +1,9 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import {
3
- AudioQueue, audioAsync, audioNative, type AudioHandle,
3
+ AudioQueue,
4
+ audioAsync,
5
+ audioNative,
6
+ type AudioHandle,
4
7
  } from './audio-native';
5
8
  import type { AudioWorkerStats } from '../audio/worker';
6
9
 
@@ -34,17 +37,24 @@ export function currentRecorderSession(
34
37
  ): AudioRecorderSession | undefined {
35
38
  const context = invocation.getStore();
36
39
  return context?.active && context.session.recorder === recorder
37
- ? context.session : undefined;
40
+ ? context.session
41
+ : undefined;
38
42
  }
39
43
 
40
44
  /** 取得 super 默认实现对应的会话;拒绝过期或阶段不匹配的异步调用。 */
41
45
  export function recorderCallbackSession(
42
- recorder: AudioHandle<'recorder'>, phase: Phase,
46
+ recorder: AudioHandle<'recorder'>,
47
+ phase: Phase,
43
48
  ): AudioRecorderSession {
44
49
  const context = invocation.getStore();
45
- if (!context?.active || context.phase !== phase ||
46
- context.session.recorder !== recorder) {
47
- throw new Error(`${phase} must run inside its active Recorder callback`);
50
+ if (
51
+ !context?.active ||
52
+ context.phase !== phase ||
53
+ context.session.recorder !== recorder
54
+ ) {
55
+ throw new Error(
56
+ `${phase} must run inside its active Recorder callback`,
57
+ );
48
58
  }
49
59
  return context.session;
50
60
  }
@@ -98,7 +108,8 @@ export class AudioRecorderSession {
98
108
  rejectReady = reject;
99
109
  });
100
110
  this.done = Promise.resolve().then(() =>
101
- this.run(resolveReady, rejectReady));
111
+ this.run(resolveReady, rejectReady),
112
+ );
102
113
  }
103
114
 
104
115
  /** 重置 Native 聚合和空 SoundBuffer,仅供 super.onStart 使用。 */
@@ -130,7 +141,8 @@ export class AudioRecorderSession {
130
141
  if (!this.captureStop) {
131
142
  this.captureStop = this.devices.run(async () => {
132
143
  await audioAsync(callback =>
133
- audioNative.stopAudioRecorder(this.recorder, callback));
144
+ audioNative.stopAudioRecorder(this.recorder, callback),
145
+ );
134
146
  });
135
147
  void this.captureStop.catch(error => this.fail(error));
136
148
  }
@@ -142,7 +154,9 @@ export class AudioRecorderSession {
142
154
  this.draining = true;
143
155
  this.timer?.ref();
144
156
  await this.done;
145
- if (this.error) throw this.error;
157
+ if (this.error) {
158
+ throw this.error;
159
+ }
146
160
  }
147
161
 
148
162
  /** 停止设备并等待本会话完整结束。 */
@@ -153,17 +167,27 @@ export class AudioRecorderSession {
153
167
 
154
168
  /** 返回 Native 队列统计,processed 的单位为交错样本。 */
155
169
  getStats(): AudioWorkerStats {
156
- const fields = new BigUint64Array(audioNative.audioWorkerStats(this.handle));
157
- return { underruns: fields[0], overruns: fields[1], processed: fields[2] };
170
+ const fields = new BigUint64Array(
171
+ audioNative.audioWorkerStats(this.handle),
172
+ );
173
+ return {
174
+ underruns: fields[0],
175
+ overruns: fields[1],
176
+ processed: fields[2],
177
+ };
158
178
  }
159
179
 
160
180
  /** 保留首个错误,避免清理异常覆盖原始回调错误。 */
161
181
  private fail(error: unknown): void {
162
- this.error ??= error instanceof Error ? error : new Error(String(error));
182
+ this.error ??=
183
+ error instanceof Error ? error : new Error(String(error));
163
184
  }
164
185
 
165
186
  /** 串行派发一个回调,并在异步完成后使其上下文失效。 */
166
- private async invoke<T>(phase: Phase, callback: () => T): Promise<Awaited<T>> {
187
+ private async invoke<T>(
188
+ phase: Phase,
189
+ callback: () => T,
190
+ ): Promise<Awaited<T>> {
167
191
  const context: Invocation = { session: this, phase, active: true };
168
192
  try {
169
193
  return await invocation.run(context, callback);
@@ -173,13 +197,19 @@ export class AudioRecorderSession {
173
197
  }
174
198
 
175
199
  /** 驱动启动、PCM 消费和唯一的停止回调,所有样本仍由 Native 持有。 */
176
- private async run(resolveReady: (value: boolean) => void,
177
- rejectReady: (error: Error) => void): Promise<void> {
200
+ private async run(
201
+ resolveReady: (value: boolean) => void,
202
+ rejectReady: (error: Error) => void,
203
+ ): Promise<void> {
178
204
  try {
179
205
  // 已取消的启动仍经 finally 派发 onStop,但不启动设备或派发 onStart。
180
- if (this.stopping) { resolveReady(false); return; }
206
+ if (this.stopping) {
207
+ resolveReady(false);
208
+ return;
209
+ }
181
210
  const accepted = await this.invoke('onStart', () =>
182
- this.callbacks.onStart());
211
+ this.callbacks.onStart(),
212
+ );
183
213
  if (typeof accepted !== 'boolean') {
184
214
  throw new TypeError('Recorder onStart must return a boolean');
185
215
  }
@@ -187,28 +217,42 @@ export class AudioRecorderSession {
187
217
  if (accepted && !this.stopping) {
188
218
  started = await this.devices.run(() =>
189
219
  audioAsync<boolean>(callback =>
190
- audioNative.startAudioRecorder(this.recorder, callback)));
220
+ audioNative.startAudioRecorder(this.recorder, callback),
221
+ ),
222
+ );
191
223
  }
192
224
  resolveReady(started && !this.stopping);
193
- if (!started) return;
225
+ if (!started) {
226
+ return;
227
+ }
194
228
  for (;;) {
195
229
  const status = audioNative.pollAudioWorker(this.handle);
196
- if (status === -3) break;
230
+ if (status === -3) {
231
+ break;
232
+ }
197
233
  if (status === -1) {
198
234
  await new Promise<void>(resolve => {
199
235
  this.timer = setTimeout(resolve, 1);
200
- if (!this.draining) this.timer.unref();
236
+ if (!this.draining) {
237
+ this.timer.unref();
238
+ }
201
239
  });
202
240
  this.timer = undefined;
203
241
  continue;
204
242
  }
205
243
  let more = false;
206
244
  try {
207
- const samples = this.input[this.fields[0]].subarray(0, this.fields[2]);
245
+ const samples = this.input[this.fields[0]].subarray(
246
+ 0,
247
+ this.fields[2],
248
+ );
208
249
  more = await this.invoke('onProcessSamples', () =>
209
- this.callbacks.onProcessSamples(samples));
250
+ this.callbacks.onProcessSamples(samples),
251
+ );
210
252
  if (typeof more !== 'boolean') {
211
- throw new TypeError('Recorder onProcessSamples must return a boolean');
253
+ throw new TypeError(
254
+ 'Recorder onProcessSamples must return a boolean',
255
+ );
212
256
  }
213
257
  } finally {
214
258
  audioNative.commitAudioWorker(this.handle, more === true);
@@ -3,14 +3,20 @@ import { resolve, join } from 'node:path';
3
3
  import { pathToFileURL } from 'node:url';
4
4
  import { audioNative, type AudioHandle } from './audio-native';
5
5
  import type {
6
- AudioWorkerOptions, AudioWorkerContext, AudioWorkerStats,
6
+ AudioWorkerOptions,
7
+ AudioWorkerContext,
8
+ AudioWorkerStats,
7
9
  } from '../audio/worker';
8
10
  import { SFMLError } from '../system/error';
9
11
 
10
12
  const cleanup = new FinalizationRegistry<AudioHandle<'worker'>>(handle =>
11
- audioNative.closeAudioWorker(handle));
13
+ audioNative.closeAudioWorker(handle),
14
+ );
12
15
 
13
- export interface WorkerMessage { type: string; error?: string }
16
+ export interface WorkerMessage {
17
+ type: string;
18
+ error?: string;
19
+ }
14
20
 
15
21
  export class AudioWorkerSession {
16
22
  readonly ready: Promise<boolean>;
@@ -18,26 +24,49 @@ export class AudioWorkerSession {
18
24
  private readonly worker: Worker;
19
25
  private readonly failure: { error?: Error } = {};
20
26
 
21
- constructor(readonly handle: AudioHandle<'worker'>, token: bigint,
22
- kind: 'stream' | 'effect' | 'recorder', options: AudioWorkerOptions,
23
- context: AudioWorkerContext, onStop?: () => void) {
24
- const module = options.module instanceof URL ? options.module.href
25
- : pathToFileURL(resolve(options.module)).href;
27
+ constructor(
28
+ readonly handle: AudioHandle<'worker'>,
29
+ token: bigint,
30
+ kind: 'stream' | 'effect' | 'recorder',
31
+ options: AudioWorkerOptions,
32
+ context: AudioWorkerContext,
33
+ onStop?: () => void,
34
+ ) {
35
+ const module =
36
+ options.module instanceof URL
37
+ ? options.module.href
38
+ : pathToFileURL(resolve(options.module)).href;
26
39
  const worker = new Worker(join(__dirname, 'audio-worker.js'), {
27
- workerData: { token, kind, module, exportName: options.exportName,
28
- data: options.data, context },
40
+ workerData: {
41
+ token,
42
+ kind,
43
+ module,
44
+ exportName: options.exportName,
45
+ data: options.data,
46
+ context,
47
+ },
29
48
  });
30
49
  this.worker = worker;
31
50
  const failure = this.failure;
32
51
  let completed = false;
33
52
  this.ready = new Promise<boolean>((resolveReady, reject) => {
34
53
  worker.on('message', (message: WorkerMessage) => {
35
- if (message.type === 'closed') completed = true;
36
- if (message.type === 'ready') resolveReady(true);
37
- if (message.type === 'aborted') resolveReady(false);
38
- if (message.type === 'stop') onStop?.();
54
+ if (message.type === 'closed') {
55
+ completed = true;
56
+ }
57
+ if (message.type === 'ready') {
58
+ resolveReady(true);
59
+ }
60
+ if (message.type === 'aborted') {
61
+ resolveReady(false);
62
+ }
63
+ if (message.type === 'stop') {
64
+ onStop?.();
65
+ }
39
66
  if (message.type === 'error') {
40
- failure.error = new SFMLError(message.error ?? 'Audio worker failed');
67
+ failure.error = new SFMLError(
68
+ message.error ?? 'Audio worker failed',
69
+ );
41
70
  audioNative.closeAudioWorker(handle);
42
71
  reject(failure.error);
43
72
  onStop?.();
@@ -52,7 +81,9 @@ export class AudioWorkerSession {
52
81
  worker.once('exit', code => {
53
82
  audioNative.closeAudioWorker(handle);
54
83
  if (!completed && !failure.error) {
55
- failure.error = new SFMLError(`Audio worker exited early (${code})`);
84
+ failure.error = new SFMLError(
85
+ `Audio worker exited early (${code})`,
86
+ );
56
87
  reject(failure.error);
57
88
  onStop?.();
58
89
  } else {
@@ -61,18 +92,29 @@ export class AudioWorkerSession {
61
92
  });
62
93
  });
63
94
  // Keep initialization alive even if ready has not yet been awaited.
64
- void this.ready.then(() => worker.unref(), () => worker.unref());
95
+ void this.ready.then(
96
+ () => worker.unref(),
97
+ () => worker.unref(),
98
+ );
65
99
  this.done = new Promise(resolveDone => {
66
100
  worker.once('exit', () => resolveDone());
67
101
  });
68
102
  cleanup.register(this, handle, this);
69
103
  }
70
104
 
71
- get error(): Error | undefined { return this.failure.error; }
105
+ get error(): Error | undefined {
106
+ return this.failure.error;
107
+ }
72
108
 
73
109
  getStats(): AudioWorkerStats {
74
- const fields = new BigUint64Array(audioNative.audioWorkerStats(this.handle));
75
- return { underruns: fields[0], overruns: fields[1], processed: fields[2] };
110
+ const fields = new BigUint64Array(
111
+ audioNative.audioWorkerStats(this.handle),
112
+ );
113
+ return {
114
+ underruns: fields[0],
115
+ overruns: fields[1],
116
+ processed: fields[2],
117
+ };
76
118
  }
77
119
 
78
120
  async close(): Promise<void> {
@@ -84,6 +126,8 @@ export class AudioWorkerSession {
84
126
  async drain(): Promise<void> {
85
127
  this.worker.ref();
86
128
  await this.done;
87
- if (this.error) throw this.error;
129
+ if (this.error) {
130
+ throw this.error;
131
+ }
88
132
  }
89
133
  }
@@ -3,11 +3,14 @@ import { setTimeout as delay } from 'node:timers/promises';
3
3
  import { audioNative } from './audio-native';
4
4
  import { Time } from '../system/time';
5
5
  import type {
6
- AudioWorkerContext, SoundStreamCallbacks, SoundRecorderCallbacks,
6
+ AudioWorkerContext,
7
+ SoundStreamCallbacks,
8
+ SoundRecorderCallbacks,
7
9
  EffectProcessorCallbacks,
8
10
  } from '../audio/worker';
9
11
 
10
- type Callbacks = SoundStreamCallbacks & SoundRecorderCallbacks &
12
+ type Callbacks = SoundStreamCallbacks &
13
+ SoundRecorderCallbacks &
11
14
  EffectProcessorCallbacks;
12
15
  interface Setup {
13
16
  token: bigint;
@@ -19,8 +22,9 @@ interface Setup {
19
22
  }
20
23
  // Preserve native ESM import when this package is compiled to CommonJS.
21
24
  // eslint-disable-next-line no-new-func
22
- const importModule = new Function('url', 'return import(url)') as
23
- (url: string) => Promise<Record<string, unknown>>;
25
+ const importModule = new Function('url', 'return import(url)') as (
26
+ url: string,
27
+ ) => Promise<Record<string, unknown>>;
24
28
 
25
29
  async function run(): Promise<void> {
26
30
  const setup = workerData as Setup;
@@ -28,12 +32,18 @@ async function run(): Promise<void> {
28
32
  const handle = mapping.handle;
29
33
  const fields = new Int32Array(mapping.fields);
30
34
  const time = new BigInt64Array(mapping.time);
31
- const pcm = setup.kind === 'effect' ? []
32
- : mapping.input.map(buffer => new Int16Array(buffer));
33
- const input = setup.kind === 'effect'
34
- ? mapping.input.map(buffer => new Float32Array(buffer)) : [];
35
- const output = setup.kind === 'effect'
36
- ? mapping.output.map(buffer => new Float32Array(buffer)) : [];
35
+ const pcm =
36
+ setup.kind === 'effect'
37
+ ? []
38
+ : mapping.input.map(buffer => new Int16Array(buffer));
39
+ const input =
40
+ setup.kind === 'effect'
41
+ ? mapping.input.map(buffer => new Float32Array(buffer))
42
+ : [];
43
+ const output =
44
+ setup.kind === 'effect'
45
+ ? mapping.output.map(buffer => new Float32Array(buffer))
46
+ : [];
37
47
  let callbacks: Partial<Callbacks> = {};
38
48
  try {
39
49
  const module = await importModule(setup.module);
@@ -41,26 +51,36 @@ async function run(): Promise<void> {
41
51
  if (typeof factory !== 'function') {
42
52
  throw new TypeError('Audio worker export must be a factory');
43
53
  }
44
- callbacks = await factory(setup.context, setup.data) as Callbacks;
45
- const required = setup.kind === 'stream'
46
- ? [ 'onGetData', 'onSeek' ] : setup.kind === 'recorder'
47
- ? [ 'onProcessSamples' ] : [ 'process' ];
54
+ callbacks = (await factory(setup.context, setup.data)) as Callbacks;
55
+ const required =
56
+ setup.kind === 'stream'
57
+ ? [ 'onGetData', 'onSeek' ]
58
+ : setup.kind === 'recorder'
59
+ ? [ 'onProcessSamples' ]
60
+ : [ 'process' ];
48
61
  for (const name of required) {
49
62
  if (typeof callbacks[name as keyof Callbacks] !== 'function') {
50
63
  throw new TypeError(`Audio worker requires ${name}()`);
51
64
  }
52
65
  }
53
- if (setup.kind === 'recorder' && callbacks.onStart &&
54
- !await callbacks.onStart()) {
66
+ if (
67
+ setup.kind === 'recorder' &&
68
+ callbacks.onStart &&
69
+ !(await callbacks.onStart())
70
+ ) {
55
71
  parentPort!.postMessage({ type: 'aborted' });
56
72
  return;
57
73
  }
58
74
  let ready = setup.kind !== 'stream';
59
- if (ready) parentPort!.postMessage({ type: 'ready' });
75
+ if (ready) {
76
+ parentPort!.postMessage({ type: 'ready' });
77
+ }
60
78
  let primed = 0;
61
79
  for (;;) {
62
80
  const operation = audioNative.pollAudioWorker(handle);
63
- if (operation === -3) break;
81
+ if (operation === -3) {
82
+ break;
83
+ }
64
84
  if (operation === -2) {
65
85
  await callbacks.onSeek!(new Time(time[0]));
66
86
  continue;
@@ -78,16 +98,23 @@ async function run(): Promise<void> {
78
98
  if (chunk.samples !== pcm[fields[0]]) {
79
99
  throw new TypeError('Fill the provided native sample buffer');
80
100
  }
81
- if (!Number.isInteger(chunk.sampleCount) || chunk.sampleCount < 0 ||
82
- chunk.sampleCount > chunk.samples.length) {
101
+ if (
102
+ !Number.isInteger(chunk.sampleCount) ||
103
+ chunk.sampleCount < 0 ||
104
+ chunk.sampleCount > chunk.samples.length
105
+ ) {
83
106
  throw new RangeError('Invalid stream sample count');
84
107
  }
85
108
  fields[2] = chunk.sampleCount;
86
109
  fields[3] = +more;
87
110
  if (!more) {
88
111
  const loop = callbacks.onLoop ? await callbacks.onLoop() : 0n;
89
- if (loop !== undefined && (typeof loop !== 'bigint' || loop < 0n ||
90
- loop > 0x7fffffffffffffffn)) {
112
+ if (
113
+ loop !== undefined &&
114
+ (typeof loop !== 'bigint' ||
115
+ loop < 0n ||
116
+ loop > 0x7fffffffffffffffn)
117
+ ) {
91
118
  throw new RangeError('Invalid loop sample offset');
92
119
  }
93
120
  time[1] = loop ?? -1n;
@@ -99,7 +126,8 @@ async function run(): Promise<void> {
99
126
  }
100
127
  } else if (setup.kind === 'recorder') {
101
128
  const more = await callbacks.onProcessSamples!(
102
- pcm[fields[0]].subarray(0, fields[2]));
129
+ pcm[fields[0]].subarray(0, fields[2]),
130
+ );
103
131
  if (typeof more !== 'boolean') {
104
132
  throw new TypeError('onProcessSamples must return a boolean');
105
133
  }
@@ -110,17 +138,26 @@ async function run(): Promise<void> {
110
138
  } else {
111
139
  const channels = fields[3];
112
140
  const result = await callbacks.process!(
113
- fields[5] ? null
141
+ fields[5]
142
+ ? null
114
143
  : input[fields[0]].subarray(fields[4], fields[4] + fields[2]),
115
- fields[7], output[fields[1]].subarray(0, fields[6] * channels),
116
- fields[6], channels);
144
+ fields[7],
145
+ output[fields[1]].subarray(0, fields[6] * channels),
146
+ fields[6],
147
+ channels,
148
+ );
117
149
  if (result) {
118
- if (!Number.isInteger(result.inputFrameCount) ||
119
- !Number.isInteger(result.outputFrameCount) ||
120
- result.inputFrameCount < 0 || result.inputFrameCount > fields[7] ||
121
- result.outputFrameCount < 0 ||
122
- result.outputFrameCount > fields[6]) {
123
- throw new RangeError('Effect frame counts exceed the leased block');
150
+ if (
151
+ !Number.isInteger(result.inputFrameCount) ||
152
+ !Number.isInteger(result.outputFrameCount) ||
153
+ result.inputFrameCount < 0 ||
154
+ result.inputFrameCount > fields[7] ||
155
+ result.outputFrameCount < 0 ||
156
+ result.outputFrameCount > fields[6]
157
+ ) {
158
+ throw new RangeError(
159
+ 'Effect frame counts exceed the leased block',
160
+ );
124
161
  }
125
162
  fields[6] = result.outputFrameCount;
126
163
  fields[7] = result.inputFrameCount;
@@ -132,15 +169,22 @@ async function run(): Promise<void> {
132
169
  }
133
170
  } finally {
134
171
  audioNative.closeAudioWorker(handle);
135
- if (setup.kind === 'recorder') await callbacks.onStop?.();
136
- else await callbacks.onClose?.();
172
+ if (setup.kind === 'recorder') {
173
+ await callbacks.onStop?.();
174
+ } else {
175
+ await callbacks.onClose?.();
176
+ }
137
177
  }
138
178
  }
139
179
 
140
- void run().catch((error: unknown) => {
141
- parentPort!.postMessage({ type: 'error',
142
- error: error instanceof Error ? error.stack : String(error) });
143
- }).finally(() => {
144
- parentPort!.postMessage({ type: 'closed' });
145
- parentPort!.close();
146
- });
180
+ void run()
181
+ .catch((error: unknown) => {
182
+ parentPort!.postMessage({
183
+ type: 'error',
184
+ error: error instanceof Error ? error.stack : String(error),
185
+ });
186
+ })
187
+ .finally(() => {
188
+ parentPort!.postMessage({ type: 'closed' });
189
+ parentPort!.close();
190
+ });
@@ -5,13 +5,16 @@ import { mutableValue } from './value-storage';
5
5
  export const glslStorage: unique symbol = Symbol('Glsl.storage');
6
6
  export const glslNative: unique symbol = Symbol('Glsl.native');
7
7
 
8
- /** Internal storage only; concrete GLSL types define their own component API. */
8
+ /**
9
+ * Internal storage only; concrete GLSL types define their own component API.
10
+ */
9
11
  export abstract class GlslValue<K extends string, A extends ValueArray> {
10
12
  readonly [mutableValue] = true;
11
13
  private readonly state: LazyValue<K, A>;
12
14
 
13
15
  protected constructor(
14
- values: A, wrap: (buffer: ArrayBuffer) => NativeValueMapping<K>,
16
+ values: A,
17
+ wrap: (buffer: ArrayBuffer) => NativeValueMapping<K>,
15
18
  ) {
16
19
  this.state = new LazyValue(values, wrap);
17
20
  }
@@ -25,8 +28,10 @@ export abstract class GlslValue<K extends string, A extends ValueArray> {
25
28
  }
26
29
  }
27
30
 
28
- export abstract class BooleanComponents<K extends string>
29
- extends GlslValue<K, Uint8Array> {
31
+ export abstract class BooleanComponents<K extends string> extends GlslValue<
32
+ K,
33
+ Uint8Array
34
+ > {
30
35
  /**
31
36
  * Boolean scalar type with components stored as 0/1 bytes.
32
37
  * @zh 布尔标量类型,底层分量使用 0/1 字节。
@@ -42,9 +47,11 @@ export abstract class BooleanComponents<K extends string>
42
47
  super(new Uint8Array(components.length), wrap);
43
48
  for (let i = 0; i < components.length; i++) {
44
49
  // C++ conversion treats NaN as nonzero, unlike JavaScript Boolean(NaN).
45
- this[glslStorage][i] =
46
- Number(components[i] !== 0 && components[i] !== 0n &&
47
- components[i] !== false);
50
+ this[glslStorage][i] = Number(
51
+ components[i] !== 0 &&
52
+ components[i] !== 0n &&
53
+ components[i] !== false,
54
+ );
48
55
  }
49
56
  }
50
57
 
@@ -54,26 +61,34 @@ export abstract class BooleanComponents<K extends string>
54
61
  * @ja X の真偽値成分。
55
62
  * @ko X 불리언 성분.
56
63
  */
57
- get x(): boolean { return this[glslStorage][0] !== 0; }
64
+ get x(): boolean {
65
+ return this[glslStorage][0] !== 0;
66
+ }
58
67
  /**
59
68
  * Set the X boolean component.
60
69
  * @zh 设置 X 布尔分量。
61
70
  * @ja X の真偽値成分を設定します。
62
71
  * @ko X 불리언 성분을 설정합니다.
63
72
  */
64
- set x(value: boolean) { this[glslStorage][0] = value ? 1 : 0; }
73
+ set x(value: boolean) {
74
+ this[glslStorage][0] = value ? 1 : 0;
75
+ }
65
76
  /**
66
77
  * Y boolean component.
67
78
  * @zh Y 布尔分量。
68
79
  * @ja Y の真偽値成分。
69
80
  * @ko Y 불리언 성분.
70
81
  */
71
- get y(): boolean { return this[glslStorage][1] !== 0; }
82
+ get y(): boolean {
83
+ return this[glslStorage][1] !== 0;
84
+ }
72
85
  /**
73
86
  * Set the Y boolean component.
74
87
  * @zh 设置 Y 布尔分量。
75
88
  * @ja Y の真偽値成分を設定します。
76
89
  * @ko Y 불리언 성분을 설정합니다.
77
90
  */
78
- set y(value: boolean) { this[glslStorage][1] = value ? 1 : 0; }
91
+ set y(value: boolean) {
92
+ this[glslStorage][1] = value ? 1 : 0;
93
+ }
79
94
  }