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.
- package/README.ja.md +10 -9
- package/README.ko.md +10 -9
- package/README.md +10 -9
- package/README.zh-CN.md +10 -9
- package/binding.gyp +23 -16
- package/dist/audio/effect-processor.js +9 -3
- package/dist/audio/input-sound-file.js +41 -15
- package/dist/audio/listener.js +33 -11
- package/dist/audio/music.js +41 -18
- package/dist/audio/output-sound-file.js +4 -2
- package/dist/audio/playback-device.js +11 -3
- package/dist/audio/sound-buffer-recorder.js +8 -5
- package/dist/audio/sound-buffer.js +40 -17
- package/dist/audio/sound-file-factory.js +21 -9
- package/dist/audio/sound-file-reader.js +21 -7
- package/dist/audio/sound-recorder.js +36 -15
- package/dist/audio/sound-source.js +117 -40
- package/dist/audio/sound-stream.js +14 -5
- package/dist/audio/sound.js +9 -4
- package/dist/audio/value.js +42 -16
- package/dist/graphics/blend-mode.js +2 -1
- package/dist/graphics/boolean-vector.d.ts +1 -2
- package/dist/graphics/boolean-vector.js +19 -9
- package/dist/graphics/color.js +2 -1
- package/dist/graphics/convex-shape.js +7 -3
- package/dist/graphics/font.js +52 -23
- package/dist/graphics/glsl-matrix.js +8 -3
- package/dist/graphics/glsl.js +47 -16
- package/dist/graphics/glyph.js +27 -10
- package/dist/graphics/image.js +30 -14
- package/dist/graphics/rect.js +15 -8
- package/dist/graphics/render-states.js +18 -9
- package/dist/graphics/render-target.js +49 -23
- package/dist/graphics/render-texture.js +24 -12
- package/dist/graphics/render-window.js +6 -3
- package/dist/graphics/shader.js +67 -32
- package/dist/graphics/shape.js +25 -13
- package/dist/graphics/sprite.js +19 -9
- package/dist/graphics/stencil-mode.js +10 -6
- package/dist/graphics/swap.js +2 -1
- package/dist/graphics/text.js +123 -48
- package/dist/graphics/texture.js +48 -24
- package/dist/graphics/transform.js +29 -13
- package/dist/graphics/transformable.js +2 -1
- package/dist/graphics/vertex-array.js +40 -18
- package/dist/graphics/vertex-buffer.js +39 -19
- package/dist/graphics/vertex.js +6 -3
- package/dist/graphics/view.js +30 -14
- package/dist/index.d.ts +4 -4
- package/dist/internal/audio-codec-registry.js +12 -6
- package/dist/internal/audio-input-stream.js +2 -1
- package/dist/internal/audio-music-pump.js +58 -24
- package/dist/internal/audio-recorder-session.js +17 -7
- package/dist/internal/audio-worker-session.js +28 -10
- package/dist/internal/audio-worker.js +40 -21
- package/dist/internal/glsl-value.d.ts +3 -1
- package/dist/internal/glsl-value.js +18 -8
- package/dist/internal/graphics-state.js +29 -15
- package/dist/internal/input-stream.d.ts +3 -1
- package/dist/internal/input-stream.js +16 -7
- package/dist/internal/numeric-properties.js +2 -1
- package/dist/internal/render-states.js +2 -1
- package/dist/internal/scalar.js +22 -13
- package/dist/internal/shaped-glyph.d.ts +3 -1
- package/dist/internal/shaped-glyph.js +12 -4
- package/dist/internal/struct-view.d.ts +3 -1
- package/dist/internal/struct-view.js +3 -1
- package/dist/internal/transformable.js +2 -1
- package/dist/network/common.js +23 -11
- package/dist/network/dns.js +25 -17
- package/dist/network/ftp.js +43 -18
- package/dist/network/http.js +126 -56
- package/dist/network/ip-address.js +50 -25
- package/dist/network/packet.js +63 -25
- package/dist/network/sftp.js +175 -74
- package/dist/network/socket-selector.js +28 -14
- package/dist/network/socket.js +71 -30
- package/dist/network/tcp-listener.js +42 -19
- package/dist/network/tcp-socket.js +111 -53
- package/dist/network/tls-material.js +2 -1
- package/dist/network/udp-socket.js +68 -33
- package/dist/system/angle.js +2 -1
- package/dist/system/clock.js +4 -2
- package/dist/system/err.js +5 -2
- package/dist/system/file-input-stream.js +8 -4
- package/dist/system/input-stream.js +6 -3
- package/dist/system/operators.d.ts +1 -1
- package/dist/system/operators.js +90 -50
- package/dist/system/sleep.js +4 -2
- package/dist/system/time.js +12 -6
- package/dist/system/vector2.js +6 -7
- package/dist/system/vector3.js +11 -6
- package/dist/window/clipboard.js +2 -1
- package/dist/window/context.js +10 -5
- package/dist/window/cursor.js +2 -1
- package/dist/window/event.js +9 -5
- package/dist/window/mouse.js +4 -2
- package/dist/window/video-mode.js +2 -1
- package/dist/window/window.js +19 -12
- package/native/addon.cc +25 -12
- package/native/audio-binding.cc +266 -126
- package/native/audio-codec-binding.cc +147 -77
- package/native/audio-device-binding.cc +76 -49
- package/native/audio-file-binding.cc +116 -67
- package/native/audio-recorder-binding.cc +147 -98
- package/native/audio-recorder.h +1 -1
- package/native/audio-state.h +53 -30
- package/native/audio-stream-binding.cc +148 -94
- package/native/audio-stream.h +5 -4
- package/native/audio-worker-binding.cc +163 -81
- package/native/audio-worker.h +35 -19
- package/native/batch-drawable.cc +29 -16
- package/native/batch-drawable.h +11 -8
- package/native/context-binding.cc +40 -27
- package/native/context-settings.h +15 -8
- package/native/deferred-delete.cc +9 -6
- package/native/deferred-delete.h +7 -8
- package/native/error-stream-binding.cc +42 -28
- package/native/font-binding.cc +89 -48
- package/native/graphics-binding.cc +122 -72
- package/native/graphics-state.h +50 -36
- package/native/image-binding.cc +38 -24
- package/native/image-state.h +26 -18
- package/native/input-binding.cc +42 -27
- package/native/render-window-binding.cc +38 -23
- package/native/shader-binding.cc +115 -50
- package/native/shape-binding.cc +94 -49
- package/native/shape-state.h +18 -9
- package/native/shaped-glyph-storage.h +9 -5
- package/native/stream-binding.cc +143 -117
- package/native/system-binding.cc +17 -12
- package/native/text-binding.cc +68 -35
- package/native/text-state.h +25 -18
- package/native/texture-binding.cc +42 -33
- package/native/texture-resource.h +20 -12
- package/native/time-value.h +3 -1
- package/native/transformable-binding.cc +12 -6
- package/native/transformable-state.h +6 -6
- package/native/value-binding.h +46 -28
- package/native/value-layout.h +10 -10
- package/native/values-binding.cc +29 -23
- package/native/vertex-binding.cc +18 -12
- package/native/vertex-buffer-binding.cc +38 -24
- package/native/vertex-storage.h +2 -2
- package/native/view-data.h +4 -3
- package/native/window-binding.cc +75 -45
- package/native/window-event.h +20 -11
- package/native/window-owner.h +15 -12
- package/package.json +44 -51
- package/src/audio/effect-processor.ts +9 -3
- package/src/audio/input-sound-file.ts +108 -36
- package/src/audio/listener.ts +39 -13
- package/src/audio/music.ts +112 -47
- package/src/audio/output-sound-file.ts +40 -11
- package/src/audio/playback-device.ts +23 -8
- package/src/audio/sound-buffer-recorder.ts +35 -13
- package/src/audio/sound-buffer.ts +166 -58
- package/src/audio/sound-file-factory.ts +127 -49
- package/src/audio/sound-file-reader.ts +49 -15
- package/src/audio/sound-recorder.ts +100 -34
- package/src/audio/sound-source.ts +142 -45
- package/src/audio/sound-stream.ts +15 -5
- package/src/audio/sound.ts +12 -4
- package/src/audio/value.ts +89 -26
- package/src/graphics/blend-mode.ts +3 -1
- package/src/graphics/boolean-vector.ts +56 -26
- package/src/graphics/color.ts +3 -1
- package/src/graphics/convex-shape.ts +10 -3
- package/src/graphics/font.ts +171 -50
- package/src/graphics/glsl-matrix.ts +9 -3
- package/src/graphics/glsl.ts +81 -35
- package/src/graphics/glyph.ts +67 -26
- package/src/graphics/image.ts +119 -45
- package/src/graphics/rect.ts +56 -24
- package/src/graphics/render-states.ts +31 -14
- package/src/graphics/render-target.ts +197 -81
- package/src/graphics/render-texture.ts +36 -12
- package/src/graphics/render-window.ts +9 -3
- package/src/graphics/shader.ts +186 -61
- package/src/graphics/shape.ts +93 -35
- package/src/graphics/sprite.ts +64 -25
- package/src/graphics/stencil-mode.ts +12 -6
- package/src/graphics/swap.ts +3 -2
- package/src/graphics/text.ts +229 -74
- package/src/graphics/texture.ts +180 -61
- package/src/graphics/transform.ts +132 -47
- package/src/graphics/transformable.ts +3 -1
- package/src/graphics/vertex-array.ts +87 -35
- package/src/graphics/vertex-buffer.ts +97 -33
- package/src/graphics/vertex.ts +9 -3
- package/src/graphics/view.ts +59 -21
- package/src/index.ts +101 -23
- package/src/internal/audio-codec-registry.ts +18 -6
- package/src/internal/audio-input-stream.ts +6 -2
- package/src/internal/audio-music-pump.ts +116 -41
- package/src/internal/audio-native.ts +249 -96
- package/src/internal/audio-recorder-session.ts +68 -24
- package/src/internal/audio-worker-session.ts +65 -21
- package/src/internal/audio-worker.ts +85 -41
- package/src/internal/glsl-value.ts +26 -11
- package/src/internal/graphics-state.ts +57 -20
- package/src/internal/input-stream.ts +23 -10
- package/src/internal/native.ts +252 -99
- package/src/internal/numeric-properties.ts +2 -1
- package/src/internal/render-states.ts +3 -1
- package/src/internal/scalar.ts +33 -14
- package/src/internal/shaped-glyph.ts +25 -12
- package/src/internal/struct-view.ts +40 -13
- package/src/internal/transformable.ts +3 -1
- package/src/network/common.ts +35 -11
- package/src/network/dns.ts +95 -38
- package/src/network/ftp.ts +129 -47
- package/src/network/http.ts +232 -77
- package/src/network/ip-address.ts +118 -48
- package/src/network/packet.ts +161 -54
- package/src/network/sftp.ts +498 -168
- package/src/network/socket-selector.ts +66 -26
- package/src/network/socket.ts +133 -46
- package/src/network/tcp-listener.ts +78 -29
- package/src/network/tcp-socket.ts +240 -95
- package/src/network/tls-material.ts +6 -2
- package/src/network/udp-socket.ts +147 -53
- package/src/system/angle.ts +3 -1
- package/src/system/clock.ts +12 -4
- package/src/system/err.ts +5 -2
- package/src/system/file-input-stream.ts +12 -4
- package/src/system/input-stream.ts +9 -3
- package/src/system/operators.ts +290 -115
- package/src/system/sleep.ts +6 -2
- package/src/system/time.ts +18 -6
- package/src/system/timeout-with-predicate.ts +19 -8
- package/src/system/vector2.ts +101 -49
- package/src/system/vector3.ts +85 -36
- package/src/window/clipboard.ts +3 -1
- package/src/window/context.ts +15 -5
- package/src/window/cursor.ts +3 -1
- package/src/window/event.ts +26 -12
- package/src/window/mouse.ts +6 -2
- package/src/window/video-mode.ts +3 -1
- package/src/window/window.ts +119 -37
- package/test/native/audio-probes.cc +74 -36
- package/test/native/deferred-delete-probes.cc +17 -11
- package/test/native/drawable-probes.cc +70 -36
- package/test/native/exception-probes.cc +14 -9
- package/test/native/image-probes.cc +20 -10
- package/test/native/matrix-probes.cc +8 -4
- package/test/native/network-oracle.cc +17 -11
- package/test/native/render-texture-probes.cc +33 -24
- package/test/native/shader-probes.cc +9 -9
- package/test/native/shape-probes.cc +37 -23
- package/test/native/sprite-probes.cc +41 -25
- package/test/native/system-probes.cc +58 -35
- package/test/native/text-probes.cc +77 -44
- package/test/native/texture-probes.cc +43 -26
- package/test/native/time-probes.cc +18 -12
- package/test/native/transform-probes.cc +15 -10
- package/test/native/transformable-probes.cc +23 -17
- package/test/native/value-oracle.h +9 -4
- package/test/native/value-probes.cc +31 -23
- package/test/native/vertex-array-probes.cc +22 -14
- package/test/native/vertex-buffer-probes.cc +28 -17
- package/test/native/view-probes.cc +29 -17
- package/test/native/window-probes.cc +66 -33
- package/tools/ensure-sfml.cjs +9 -0
- package/tools/generate-network-constants.cjs +11 -6
- package/tools/generate-window-constants.cjs +31 -12
- package/docs/README.ja.md +0 -30
- package/docs/README.ko.md +0 -30
- package/docs/README.md +0 -30
- package/docs/README.zh-CN.md +0 -30
- package/docs/api.ja.md +0 -84
- package/docs/api.ko.md +0 -84
- package/docs/api.md +0 -81
- package/docs/api.zh-CN.md +0 -81
- package/docs/audio.ja.md +0 -249
- package/docs/audio.ko.md +0 -251
- package/docs/audio.md +0 -252
- package/docs/audio.zh-CN.md +0 -252
- package/docs/concepts.ja.md +0 -130
- package/docs/concepts.ko.md +0 -130
- package/docs/concepts.md +0 -131
- package/docs/concepts.zh-CN.md +0 -131
- package/docs/development.ja.md +0 -144
- package/docs/development.ko.md +0 -144
- package/docs/development.md +0 -146
- package/docs/development.zh-CN.md +0 -146
- package/docs/getting-started.ja.md +0 -154
- package/docs/getting-started.ko.md +0 -154
- package/docs/getting-started.md +0 -156
- package/docs/getting-started.zh-CN.md +0 -156
- package/docs/graphics.ja.md +0 -256
- package/docs/graphics.ko.md +0 -254
- package/docs/graphics.md +0 -255
- package/docs/graphics.zh-CN.md +0 -257
- package/docs/network.ja.md +0 -214
- package/docs/network.ko.md +0 -216
- package/docs/network.md +0 -218
- package/docs/network.zh-CN.md +0 -218
- package/docs/system.ja.md +0 -169
- package/docs/system.ko.md +0 -171
- package/docs/system.md +0 -173
- package/docs/system.zh-CN.md +0 -173
- package/docs/window.ja.md +0 -138
- package/docs/window.ko.md +0 -140
- package/docs/window.md +0 -141
- package/docs/window.zh-CN.md +0 -141
- package/native/exception-boundary.h +0 -97
- package/native/numeric-fields.h +0 -96
- package/native/shared-handle.h +0 -107
- package/test/tsconfig.json +0 -8
- package/tools/configure-lsp.cjs +0 -23
- package/tools/lib_downloader.cjs +0 -92
- 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,
|
|
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
|
|
40
|
+
? context.session
|
|
41
|
+
: undefined;
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
/** 取得 super 默认实现对应的会话;拒绝过期或阶段不匹配的异步调用。 */
|
|
41
45
|
export function recorderCallbackSession(
|
|
42
|
-
recorder: AudioHandle<'recorder'>,
|
|
46
|
+
recorder: AudioHandle<'recorder'>,
|
|
47
|
+
phase: Phase,
|
|
43
48
|
): AudioRecorderSession {
|
|
44
49
|
const context = invocation.getStore();
|
|
45
|
-
if (
|
|
46
|
-
|
|
47
|
-
|
|
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)
|
|
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(
|
|
157
|
-
|
|
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 ??=
|
|
182
|
+
this.error ??=
|
|
183
|
+
error instanceof Error ? error : new Error(String(error));
|
|
163
184
|
}
|
|
164
185
|
|
|
165
186
|
/** 串行派发一个回调,并在异步完成后使其上下文失效。 */
|
|
166
|
-
private async invoke<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(
|
|
177
|
-
|
|
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) {
|
|
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)
|
|
225
|
+
if (!started) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
194
228
|
for (;;) {
|
|
195
229
|
const status = audioNative.pollAudioWorker(this.handle);
|
|
196
|
-
if (status === -3)
|
|
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)
|
|
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(
|
|
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(
|
|
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,
|
|
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 {
|
|
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(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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: {
|
|
28
|
-
|
|
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')
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (message.type === '
|
|
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(
|
|
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(
|
|
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(
|
|
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 {
|
|
105
|
+
get error(): Error | undefined {
|
|
106
|
+
return this.failure.error;
|
|
107
|
+
}
|
|
72
108
|
|
|
73
109
|
getStats(): AudioWorkerStats {
|
|
74
|
-
const fields = new BigUint64Array(
|
|
75
|
-
|
|
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)
|
|
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,
|
|
6
|
+
AudioWorkerContext,
|
|
7
|
+
SoundStreamCallbacks,
|
|
8
|
+
SoundRecorderCallbacks,
|
|
7
9
|
EffectProcessorCallbacks,
|
|
8
10
|
} from '../audio/worker';
|
|
9
11
|
|
|
10
|
-
type Callbacks = SoundStreamCallbacks &
|
|
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
|
-
|
|
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 =
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
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 =
|
|
46
|
-
|
|
47
|
-
? [ '
|
|
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 (
|
|
54
|
-
|
|
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)
|
|
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)
|
|
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 (
|
|
82
|
-
|
|
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 (
|
|
90
|
-
|
|
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]
|
|
141
|
+
fields[5]
|
|
142
|
+
? null
|
|
114
143
|
: input[fields[0]].subarray(fields[4], fields[4] + fields[2]),
|
|
115
|
-
fields[7],
|
|
116
|
-
fields[
|
|
144
|
+
fields[7],
|
|
145
|
+
output[fields[1]].subarray(0, fields[6] * channels),
|
|
146
|
+
fields[6],
|
|
147
|
+
channels,
|
|
148
|
+
);
|
|
117
149
|
if (result) {
|
|
118
|
-
if (
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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')
|
|
136
|
-
|
|
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()
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
-
/**
|
|
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,
|
|
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
|
-
|
|
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
|
-
|
|
47
|
-
components[i] !==
|
|
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 {
|
|
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) {
|
|
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 {
|
|
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) {
|
|
91
|
+
set y(value: boolean) {
|
|
92
|
+
this[glslStorage][1] = value ? 1 : 0;
|
|
93
|
+
}
|
|
79
94
|
}
|