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
|
@@ -24,18 +24,28 @@ struct Input {
|
|
|
24
24
|
};
|
|
25
25
|
using InputHandle = SharedHandle<Input>;
|
|
26
26
|
|
|
27
|
-
void QueueInput(v8::Local<v8::Value> callback,
|
|
28
|
-
|
|
27
|
+
void QueueInput(v8::Local<v8::Value> callback,
|
|
28
|
+
const std::shared_ptr<InputFile>& file,
|
|
29
|
+
Job::Action work,
|
|
30
|
+
Job::Result result) {
|
|
29
31
|
if (file->stream) {
|
|
30
|
-
QueueThread(
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
QueueThread(
|
|
33
|
+
callback, std::move(work), std::move(result), [stream = file->stream] {
|
|
34
|
+
stream->Cancel();
|
|
35
|
+
});
|
|
36
|
+
} else {
|
|
37
|
+
Queue(callback, std::move(work), std::move(result));
|
|
38
|
+
}
|
|
33
39
|
}
|
|
34
40
|
|
|
35
41
|
v8::Local<v8::Object> MapInput(const std::shared_ptr<InputFile>& file) {
|
|
36
42
|
auto result = Nan::New<v8::Object>();
|
|
37
|
-
Set(result,
|
|
38
|
-
|
|
43
|
+
Set(result,
|
|
44
|
+
"buffer",
|
|
45
|
+
MapShared(v8::Isolate::GetCurrent(),
|
|
46
|
+
file,
|
|
47
|
+
&file->metadata,
|
|
48
|
+
sizeof(file->metadata)));
|
|
39
49
|
Set(result, "channelMap", MapChannels(file, file->channelMap));
|
|
40
50
|
return result;
|
|
41
51
|
}
|
|
@@ -57,21 +67,27 @@ NAN_METHOD(OpenAudioInput) {
|
|
|
57
67
|
if (!String(info[1], path)) return;
|
|
58
68
|
} else if (auto* stream = StreamClientHandle::TryFrom(info[1])) {
|
|
59
69
|
next->stream = stream->owner();
|
|
60
|
-
} else if (!ReadBytes(info[1], next->bytes))
|
|
70
|
+
} else if (!ReadBytes(info[1], next->bytes)) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
61
73
|
auto ok = std::make_shared<bool>();
|
|
62
|
-
QueueInput(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
74
|
+
QueueInput(
|
|
75
|
+
info[2],
|
|
76
|
+
next,
|
|
77
|
+
[next, path, ok] {
|
|
78
|
+
*ok = next->stream ? next->file.openFromStream(*next->stream)
|
|
79
|
+
: next->bytes.backing ? next->file.openFromMemory(
|
|
80
|
+
next->bytes.data, next->bytes.size)
|
|
81
|
+
: next->file.openFromFile(Path(path));
|
|
82
|
+
},
|
|
83
|
+
[input, next, ok]() -> v8::Local<v8::Value> {
|
|
84
|
+
if (!*ok) return Nan::Undefined();
|
|
85
|
+
next->open = true;
|
|
86
|
+
next->Update();
|
|
87
|
+
next->channelMap = next->file.getChannelMap();
|
|
88
|
+
input->current = next;
|
|
89
|
+
return MapInput(next);
|
|
90
|
+
});
|
|
75
91
|
}
|
|
76
92
|
|
|
77
93
|
NAN_METHOD(ReadAudioInput) {
|
|
@@ -80,15 +96,20 @@ NAN_METHOD(ReadAudioInput) {
|
|
|
80
96
|
if (!handle || !ReadBytes(info[1], bytes, true)) return;
|
|
81
97
|
auto file = handle->owner()->current;
|
|
82
98
|
auto count = std::make_shared<std::uint64_t>();
|
|
83
|
-
QueueInput(
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
99
|
+
QueueInput(
|
|
100
|
+
info[2],
|
|
101
|
+
file,
|
|
102
|
+
[file, bytes, count] {
|
|
103
|
+
*count = file->open
|
|
104
|
+
? file->file.read(static_cast<std::int16_t*>(bytes.data),
|
|
105
|
+
bytes.size / 2)
|
|
106
|
+
: 0;
|
|
107
|
+
},
|
|
108
|
+
[file, count]() -> v8::Local<v8::Value> {
|
|
109
|
+
if (file->open) file->Update();
|
|
110
|
+
file->metadata.transferred = *count;
|
|
111
|
+
return Nan::Undefined();
|
|
112
|
+
});
|
|
92
113
|
}
|
|
93
114
|
|
|
94
115
|
NAN_METHOD(SeekAudioInput) {
|
|
@@ -101,18 +122,26 @@ NAN_METHOD(SeekAudioInput) {
|
|
|
101
122
|
bool lossless{};
|
|
102
123
|
const auto signedOffset = info[1].As<v8::BigInt>()->Int64Value();
|
|
103
124
|
const auto offset = time
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
125
|
+
? static_cast<std::uint64_t>(
|
|
126
|
+
info[1].As<v8::BigInt>()->Int64Value(&lossless))
|
|
127
|
+
: info[1].As<v8::BigInt>()->Uint64Value(&lossless);
|
|
128
|
+
if (!lossless)
|
|
129
|
+
return Nan::ThrowRangeError("Offset is outside its scalar range");
|
|
107
130
|
auto file = handle->owner()->current;
|
|
108
|
-
QueueInput(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
131
|
+
QueueInput(
|
|
132
|
+
info[3],
|
|
133
|
+
file,
|
|
134
|
+
[file, time, offset, signedOffset] {
|
|
135
|
+
if (!file->open) return;
|
|
136
|
+
if (time)
|
|
137
|
+
file->file.seek(sf::microseconds(signedOffset));
|
|
138
|
+
else
|
|
139
|
+
file->file.seek(offset);
|
|
140
|
+
},
|
|
141
|
+
[file]() -> v8::Local<v8::Value> {
|
|
142
|
+
if (file->open) file->Update();
|
|
143
|
+
return Nan::Undefined();
|
|
144
|
+
});
|
|
116
145
|
}
|
|
117
146
|
|
|
118
147
|
NAN_METHOD(CloseAudioInput) {
|
|
@@ -121,11 +150,14 @@ NAN_METHOD(CloseAudioInput) {
|
|
|
121
150
|
// Old views own old metadata; the file closes before releasing its bytes.
|
|
122
151
|
auto input = handle->owner();
|
|
123
152
|
auto old = input->current;
|
|
124
|
-
QueueInput(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
153
|
+
QueueInput(
|
|
154
|
+
info[1],
|
|
155
|
+
old,
|
|
156
|
+
[old] {
|
|
157
|
+
old->file.close();
|
|
158
|
+
if (old->stream) old->stream->Cancel();
|
|
159
|
+
old->stream.reset();
|
|
160
|
+
},
|
|
129
161
|
[input]() -> v8::Local<v8::Value> {
|
|
130
162
|
input->current = std::make_shared<InputFile>();
|
|
131
163
|
return MapInput(input->current);
|
|
@@ -148,13 +180,17 @@ NAN_METHOD(OpenAudioOutput) {
|
|
|
148
180
|
unsigned rate, count;
|
|
149
181
|
std::vector<sf::SoundChannel> channels;
|
|
150
182
|
if (!handle || !String(info[1], path) || !Unsigned(info[2], rate) ||
|
|
151
|
-
!Unsigned(info[3], count) || !Channels(info[4], channels))
|
|
183
|
+
!Unsigned(info[3], count) || !Channels(info[4], channels))
|
|
184
|
+
return;
|
|
152
185
|
auto output = handle->owner();
|
|
153
186
|
auto ok = std::make_shared<bool>();
|
|
154
|
-
Queue(
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
187
|
+
Queue(
|
|
188
|
+
info[5],
|
|
189
|
+
[output, path, rate, count, channels, ok] {
|
|
190
|
+
*ok = output->file.openFromFile(Path(path), rate, count, channels);
|
|
191
|
+
output->open = *ok;
|
|
192
|
+
},
|
|
193
|
+
[ok]() -> v8::Local<v8::Value> { return Nan::New(*ok); });
|
|
158
194
|
}
|
|
159
195
|
|
|
160
196
|
NAN_METHOD(WriteAudioOutput) {
|
|
@@ -162,31 +198,44 @@ NAN_METHOD(WriteAudioOutput) {
|
|
|
162
198
|
Bytes bytes;
|
|
163
199
|
if (!handle || !ReadBytes(info[1], bytes, true)) return;
|
|
164
200
|
auto output = handle->owner();
|
|
165
|
-
Queue(
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
201
|
+
Queue(
|
|
202
|
+
info[2],
|
|
203
|
+
[output, bytes] {
|
|
204
|
+
if (!output->open)
|
|
205
|
+
throw std::runtime_error("Audio output file is not open");
|
|
206
|
+
output->file.write(static_cast<std::int16_t*>(bytes.data),
|
|
207
|
+
bytes.size / 2);
|
|
208
|
+
},
|
|
209
|
+
[]() -> v8::Local<v8::Value> { return Nan::Undefined(); });
|
|
169
210
|
}
|
|
170
211
|
|
|
171
212
|
NAN_METHOD(CloseAudioOutput) {
|
|
172
213
|
auto* handle = OutputHandle::From(info[0]);
|
|
173
214
|
if (!handle) return;
|
|
174
215
|
auto output = handle->owner();
|
|
175
|
-
Queue(
|
|
216
|
+
Queue(
|
|
217
|
+
info[1],
|
|
218
|
+
[output] {
|
|
219
|
+
output->file.close();
|
|
220
|
+
output->open = false;
|
|
221
|
+
},
|
|
176
222
|
[]() -> v8::Local<v8::Value> { return Nan::Undefined(); });
|
|
177
223
|
}
|
|
178
224
|
|
|
179
|
-
}
|
|
225
|
+
} // namespace sfmljs::audio
|
|
180
226
|
|
|
181
227
|
NAN_MODULE_INIT(InitializeAudioFiles) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
sfmljs::SetMethod<OpenAudioInput>(target, "openAudioInput");
|
|
185
|
-
sfmljs::SetMethod<ReadAudioInput>(target, "readAudioInput");
|
|
186
|
-
sfmljs::SetMethod<SeekAudioInput>(target, "seekAudioInput");
|
|
187
|
-
sfmljs::SetMethod<CloseAudioInput>(target, "closeAudioInput");
|
|
188
|
-
sfmljs::SetMethod<CreateAudioOutput>(target,
|
|
189
|
-
|
|
190
|
-
sfmljs::SetMethod<
|
|
191
|
-
sfmljs::SetMethod<
|
|
228
|
+
sfmljs::SetMethod<sfmljs::audio::CreateAudioInput>(target,
|
|
229
|
+
"createAudioInput");
|
|
230
|
+
sfmljs::SetMethod<sfmljs::audio::OpenAudioInput>(target, "openAudioInput");
|
|
231
|
+
sfmljs::SetMethod<sfmljs::audio::ReadAudioInput>(target, "readAudioInput");
|
|
232
|
+
sfmljs::SetMethod<sfmljs::audio::SeekAudioInput>(target, "seekAudioInput");
|
|
233
|
+
sfmljs::SetMethod<sfmljs::audio::CloseAudioInput>(target, "closeAudioInput");
|
|
234
|
+
sfmljs::SetMethod<sfmljs::audio::CreateAudioOutput>(target,
|
|
235
|
+
"createAudioOutput");
|
|
236
|
+
sfmljs::SetMethod<sfmljs::audio::OpenAudioOutput>(target, "openAudioOutput");
|
|
237
|
+
sfmljs::SetMethod<sfmljs::audio::WriteAudioOutput>(target,
|
|
238
|
+
"writeAudioOutput");
|
|
239
|
+
sfmljs::SetMethod<sfmljs::audio::CloseAudioOutput>(target,
|
|
240
|
+
"closeAudioOutput");
|
|
192
241
|
}
|
|
@@ -3,14 +3,17 @@
|
|
|
3
3
|
namespace sfmljs::audio {
|
|
4
4
|
|
|
5
5
|
// SFML 3.1 calls onProcessSamples from the device callback. Returning false
|
|
6
|
-
// invokes ma_device_stop on that callback thread. Stop from an async job
|
|
6
|
+
// invokes ma_device_stop on that callback thread. Stop from an async job
|
|
7
|
+
// instead.
|
|
7
8
|
class WorkerRecorder final : public sf::SoundRecorder {
|
|
8
9
|
public:
|
|
9
10
|
explicit WorkerRecorder(std::shared_ptr<AudioWorkerState> state)
|
|
10
11
|
: state_(std::move(state)) {}
|
|
11
12
|
~WorkerRecorder() override { stop(); }
|
|
13
|
+
|
|
12
14
|
private:
|
|
13
|
-
bool onProcessSamples(const std::int16_t* samples,
|
|
15
|
+
bool onProcessSamples(const std::int16_t* samples,
|
|
16
|
+
std::size_t count) override {
|
|
14
17
|
if (state_) state_->Capture(samples, count);
|
|
15
18
|
return true;
|
|
16
19
|
}
|
|
@@ -18,15 +21,22 @@ class WorkerRecorder final : public sf::SoundRecorder {
|
|
|
18
21
|
};
|
|
19
22
|
|
|
20
23
|
NAN_METHOD(CreateAudioRecorder) {
|
|
21
|
-
if (!info[0]->IsBoolean())
|
|
24
|
+
if (!info[0]->IsBoolean())
|
|
25
|
+
return Nan::ThrowTypeError("Expected a recorder kind");
|
|
22
26
|
auto recorder = MakeThreadOwned<Recorder>();
|
|
23
27
|
recorder->bufferRecorder = info[0]->IsTrue();
|
|
24
28
|
recorder->device = sf::SoundRecorder::getDefaultDevice();
|
|
25
29
|
auto result = Nan::New<v8::Object>();
|
|
26
30
|
Set(result, "handle", RecorderHandle::Create(recorder));
|
|
27
|
-
Set(result,
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
Set(result,
|
|
32
|
+
"values",
|
|
33
|
+
MapShared(info.GetIsolate(),
|
|
34
|
+
recorder,
|
|
35
|
+
recorder->values.data(),
|
|
36
|
+
sizeof(recorder->values)));
|
|
37
|
+
Set(result,
|
|
38
|
+
"channelMap",
|
|
39
|
+
MapChannels(recorder->channels, *recorder->channels));
|
|
30
40
|
info.GetReturnValue().Set(result);
|
|
31
41
|
}
|
|
32
42
|
|
|
@@ -34,36 +44,43 @@ NAN_METHOD(PrepareAudioRecorder) {
|
|
|
34
44
|
auto* handle = RecorderHandle::From(info[0]);
|
|
35
45
|
unsigned rate, frames, slots;
|
|
36
46
|
if (!handle || !Unsigned(info[1], rate) || !Unsigned(info[2], frames) ||
|
|
37
|
-
!Unsigned(info[3], slots))
|
|
47
|
+
!Unsigned(info[3], slots))
|
|
48
|
+
return;
|
|
38
49
|
auto recorder = handle->owner();
|
|
39
50
|
if (recorder->active) return Nan::ThrowError("Recorder is already running");
|
|
40
51
|
if (rate == 0) return Nan::ThrowRangeError("Sample rate must be positive");
|
|
41
52
|
const bool callbacks = recorder->bufferRecorder && info[5]->IsTrue();
|
|
42
53
|
const bool direct = recorder->bufferRecorder && !callbacks;
|
|
43
|
-
auto state =
|
|
44
|
-
|
|
54
|
+
auto state =
|
|
55
|
+
direct ? nullptr
|
|
56
|
+
: CreateWorkerState(1, frames, recorder->values[1], rate, slots);
|
|
45
57
|
if (state) state->running.store(true, std::memory_order_release);
|
|
46
58
|
auto next = std::make_shared<std::shared_ptr<sf::SoundRecorder>>();
|
|
47
|
-
Queue(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
59
|
+
Queue(
|
|
60
|
+
info[4],
|
|
61
|
+
[recorder, state, next, direct] {
|
|
62
|
+
if (direct)
|
|
63
|
+
*next = std::make_shared<sf::SoundBufferRecorder>();
|
|
64
|
+
else
|
|
65
|
+
*next = std::make_shared<WorkerRecorder>(state);
|
|
66
|
+
(*next)->setChannelCount(recorder->values[1]);
|
|
67
|
+
if ((*next)->getDevice() != recorder->device &&
|
|
68
|
+
!(*next)->setDevice(recorder->device)) {
|
|
69
|
+
throw std::runtime_error("Failed to select capture device");
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
[recorder, state, next, rate, callbacks]() -> v8::Local<v8::Value> {
|
|
73
|
+
recorder->current = std::move(*next);
|
|
74
|
+
recorder->worker = state;
|
|
75
|
+
recorder->callbackRecorder = callbacks;
|
|
76
|
+
recorder->values[0] = rate;
|
|
77
|
+
auto result = Nan::New<v8::Object>();
|
|
78
|
+
if (state) {
|
|
79
|
+
Set(result, "handle", AudioWorkerHandle::Create(state));
|
|
80
|
+
Set(result, "token", RegisterWorker(state));
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
});
|
|
67
84
|
}
|
|
68
85
|
|
|
69
86
|
NAN_METHOD(StartAudioRecorder) {
|
|
@@ -72,35 +89,41 @@ NAN_METHOD(StartAudioRecorder) {
|
|
|
72
89
|
auto recorder = handle->owner();
|
|
73
90
|
if (!recorder->current) return Nan::ThrowError("Recorder is not prepared");
|
|
74
91
|
auto ok = std::make_shared<bool>();
|
|
75
|
-
Queue(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
92
|
+
Queue(
|
|
93
|
+
info[1],
|
|
94
|
+
[recorder, ok] { *ok = recorder->current->start(recorder->values[0]); },
|
|
95
|
+
[recorder, ok]() -> v8::Local<v8::Value> {
|
|
96
|
+
recorder->active = *ok;
|
|
97
|
+
recorder->values[2] = *ok;
|
|
98
|
+
return Nan::New(*ok);
|
|
99
|
+
});
|
|
82
100
|
}
|
|
83
101
|
|
|
84
102
|
NAN_METHOD(StopAudioRecorder) {
|
|
85
103
|
auto* handle = RecorderHandle::From(info[0]);
|
|
86
104
|
if (!handle) return;
|
|
87
105
|
auto recorder = handle->owner();
|
|
88
|
-
Queue(
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
106
|
+
Queue(
|
|
107
|
+
info[1],
|
|
108
|
+
[recorder] {
|
|
109
|
+
if (recorder->current) recorder->current->stop();
|
|
110
|
+
},
|
|
111
|
+
[recorder]() -> v8::Local<v8::Value> {
|
|
112
|
+
const bool wasActive = recorder->active;
|
|
113
|
+
recorder->active = false;
|
|
114
|
+
recorder->values[2] = 0;
|
|
115
|
+
if (recorder->worker) {
|
|
116
|
+
recorder->worker->running.store(false, std::memory_order_release);
|
|
117
|
+
}
|
|
118
|
+
if (!recorder->bufferRecorder || recorder->callbackRecorder ||
|
|
119
|
+
!wasActive || !recorder->current)
|
|
120
|
+
return Nan::Undefined();
|
|
121
|
+
auto& buffer = static_cast<sf::SoundBufferRecorder&>(*recorder->current)
|
|
122
|
+
.getBuffer();
|
|
123
|
+
auto data = MakeThreadOwned<BufferData>(
|
|
124
|
+
recorder->current, const_cast<sf::SoundBuffer&>(buffer));
|
|
125
|
+
return BufferDataHandle::Create(data);
|
|
126
|
+
});
|
|
104
127
|
}
|
|
105
128
|
|
|
106
129
|
NAN_METHOD(CreateAudioRecorderBuffer) {
|
|
@@ -108,7 +131,7 @@ NAN_METHOD(CreateAudioRecorderBuffer) {
|
|
|
108
131
|
if (!handle) return;
|
|
109
132
|
const auto recorder = handle->owner();
|
|
110
133
|
auto buffer = std::make_shared<RecorderBuffer>(recorder->values[0],
|
|
111
|
-
|
|
134
|
+
*recorder->channels);
|
|
112
135
|
info.GetReturnValue().Set(RecorderBufferHandle::Create(buffer));
|
|
113
136
|
}
|
|
114
137
|
|
|
@@ -124,10 +147,12 @@ NAN_METHOD(AudioRecorderBuffer) {
|
|
|
124
147
|
} else if (command == 1) {
|
|
125
148
|
Bytes samples;
|
|
126
149
|
if (!ReadBytes(info[2], samples, true)) return;
|
|
127
|
-
if (buffer->finalized)
|
|
150
|
+
if (buffer->finalized)
|
|
151
|
+
return Nan::ThrowError("Recorder buffer is finalized");
|
|
128
152
|
const auto count = samples.size / sizeof(std::int16_t);
|
|
129
153
|
if (count % buffer->channels.size()) {
|
|
130
|
-
return Nan::ThrowRangeError(
|
|
154
|
+
return Nan::ThrowRangeError(
|
|
155
|
+
"Recorder samples must contain complete frames");
|
|
131
156
|
}
|
|
132
157
|
if (count) {
|
|
133
158
|
const auto* data = static_cast<const std::int16_t*>(samples.data);
|
|
@@ -138,8 +163,11 @@ NAN_METHOD(AudioRecorderBuffer) {
|
|
|
138
163
|
if (!buffer->finalized) {
|
|
139
164
|
auto result = MakeThreadOwned<BufferData>();
|
|
140
165
|
if (!buffer->pcm.empty() &&
|
|
141
|
-
!result->buffer.loadFromSamples(
|
|
142
|
-
|
|
166
|
+
!result->buffer.loadFromSamples(
|
|
167
|
+
buffer->pcm.data(),
|
|
168
|
+
buffer->pcm.size(),
|
|
169
|
+
static_cast<unsigned>(buffer->channels.size()),
|
|
170
|
+
buffer->rate,
|
|
143
171
|
buffer->channels)) {
|
|
144
172
|
return Nan::ThrowError("Failed to finalize recorder buffer");
|
|
145
173
|
}
|
|
@@ -148,7 +176,9 @@ NAN_METHOD(AudioRecorderBuffer) {
|
|
|
148
176
|
buffer->finalized = true;
|
|
149
177
|
std::vector<std::int16_t>().swap(buffer->pcm);
|
|
150
178
|
}
|
|
151
|
-
} else
|
|
179
|
+
} else {
|
|
180
|
+
return Nan::ThrowRangeError("Unknown recorder buffer command");
|
|
181
|
+
}
|
|
152
182
|
info.GetReturnValue().Set(BufferDataHandle::Create(buffer->result));
|
|
153
183
|
}
|
|
154
184
|
|
|
@@ -164,29 +194,33 @@ NAN_METHOD(AudioRecorderDevice) {
|
|
|
164
194
|
if (!String(info[1], name)) return;
|
|
165
195
|
// Device replacement is serialized with capture start/stop in TypeScript.
|
|
166
196
|
auto ok = std::make_shared<bool>();
|
|
167
|
-
Queue(
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
recorder->
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
197
|
+
Queue(
|
|
198
|
+
info[2],
|
|
199
|
+
[recorder, name, ok] {
|
|
200
|
+
if (recorder->current) {
|
|
201
|
+
if (recorder->active) recorder->current->stop();
|
|
202
|
+
*ok = recorder->current->setDevice(name);
|
|
203
|
+
if (*ok && recorder->active) {
|
|
204
|
+
*ok = recorder->current->start(recorder->values[0]);
|
|
205
|
+
}
|
|
206
|
+
} else {
|
|
207
|
+
const auto devices = sf::SoundRecorder::getAvailableDevices();
|
|
208
|
+
*ok =
|
|
209
|
+
std::find(devices.begin(), devices.end(), name) != devices.end();
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
[recorder, name, ok]() -> v8::Local<v8::Value> {
|
|
213
|
+
if (*ok) {
|
|
214
|
+
recorder->device = name;
|
|
215
|
+
} else {
|
|
216
|
+
recorder->active = false;
|
|
217
|
+
recorder->values[2] = 0;
|
|
218
|
+
if (recorder->worker) {
|
|
219
|
+
recorder->worker->running.store(false, std::memory_order_release);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return Nan::New(*ok);
|
|
223
|
+
});
|
|
190
224
|
}
|
|
191
225
|
|
|
192
226
|
NAN_METHOD(AudioRecorderChannels) {
|
|
@@ -194,12 +228,16 @@ NAN_METHOD(AudioRecorderChannels) {
|
|
|
194
228
|
unsigned count;
|
|
195
229
|
if (!handle || !Unsigned(info[1], count)) return;
|
|
196
230
|
auto recorder = handle->owner();
|
|
197
|
-
if (count < 1 || count > 2)
|
|
198
|
-
|
|
231
|
+
if (count < 1 || count > 2)
|
|
232
|
+
return Nan::ThrowRangeError("Capture supports mono or stereo");
|
|
233
|
+
if (recorder->active)
|
|
234
|
+
return Nan::ThrowError("Stop capture before changing channels");
|
|
199
235
|
recorder->values[1] = count;
|
|
200
236
|
auto channels = std::make_shared<std::vector<sf::SoundChannel>>();
|
|
201
|
-
if (count == 1)
|
|
202
|
-
|
|
237
|
+
if (count == 1)
|
|
238
|
+
*channels = {sf::SoundChannel::Mono};
|
|
239
|
+
else
|
|
240
|
+
*channels = {sf::SoundChannel::FrontLeft, sf::SoundChannel::FrontRight};
|
|
203
241
|
recorder->channels = channels;
|
|
204
242
|
info.GetReturnValue().Set(MapChannels(channels, *channels));
|
|
205
243
|
}
|
|
@@ -215,23 +253,34 @@ NAN_METHOD(AudioCaptureDevices) {
|
|
|
215
253
|
}
|
|
216
254
|
info.GetReturnValue().Set(result);
|
|
217
255
|
} else if (command == 1) {
|
|
218
|
-
info.GetReturnValue().Set(
|
|
256
|
+
info.GetReturnValue().Set(
|
|
257
|
+
Nan::New(sf::SoundRecorder::getDefaultDevice()).ToLocalChecked());
|
|
219
258
|
} else if (command == 2) {
|
|
220
259
|
info.GetReturnValue().Set(sf::SoundRecorder::isAvailable());
|
|
221
|
-
} else
|
|
260
|
+
} else {
|
|
261
|
+
return Nan::ThrowRangeError("Unknown capture device command");
|
|
262
|
+
}
|
|
222
263
|
}
|
|
223
264
|
|
|
224
|
-
}
|
|
265
|
+
} // namespace sfmljs::audio
|
|
225
266
|
|
|
226
267
|
NAN_MODULE_INIT(InitializeAudioRecorders) {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
sfmljs::SetMethod<PrepareAudioRecorder>(
|
|
230
|
-
|
|
231
|
-
sfmljs::SetMethod<
|
|
232
|
-
|
|
233
|
-
sfmljs::SetMethod<
|
|
234
|
-
|
|
235
|
-
sfmljs::SetMethod<
|
|
236
|
-
|
|
268
|
+
sfmljs::SetMethod<sfmljs::audio::CreateAudioRecorder>(target,
|
|
269
|
+
"createAudioRecorder");
|
|
270
|
+
sfmljs::SetMethod<sfmljs::audio::PrepareAudioRecorder>(
|
|
271
|
+
target, "prepareAudioRecorder");
|
|
272
|
+
sfmljs::SetMethod<sfmljs::audio::StartAudioRecorder>(target,
|
|
273
|
+
"startAudioRecorder");
|
|
274
|
+
sfmljs::SetMethod<sfmljs::audio::StopAudioRecorder>(target,
|
|
275
|
+
"stopAudioRecorder");
|
|
276
|
+
sfmljs::SetMethod<sfmljs::audio::CreateAudioRecorderBuffer>(
|
|
277
|
+
target, "createAudioRecorderBuffer");
|
|
278
|
+
sfmljs::SetMethod<sfmljs::audio::AudioRecorderBuffer>(target,
|
|
279
|
+
"audioRecorderBuffer");
|
|
280
|
+
sfmljs::SetMethod<sfmljs::audio::AudioRecorderDevice>(target,
|
|
281
|
+
"audioRecorderDevice");
|
|
282
|
+
sfmljs::SetMethod<sfmljs::audio::AudioRecorderChannels>(
|
|
283
|
+
target, "audioRecorderChannels");
|
|
284
|
+
sfmljs::SetMethod<sfmljs::audio::AudioCaptureDevices>(target,
|
|
285
|
+
"audioCaptureDevices");
|
|
237
286
|
}
|
package/native/audio-recorder.h
CHANGED