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
package/native/audio-state.h
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
|
+
#include <array>
|
|
4
|
+
#include <functional>
|
|
5
|
+
#include <span> // NOLINT(build/include_order): cpplint lacks C++20 headers
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
3
9
|
#include "deferred-delete.h"
|
|
4
10
|
#include "exception-boundary.h"
|
|
5
11
|
#include "numeric-fields.h"
|
|
6
|
-
|
|
7
12
|
#include <SFML/Audio.hpp>
|
|
8
13
|
#include <SFML/System/MemoryInputStream.hpp>
|
|
9
14
|
|
|
10
|
-
#include <array>
|
|
11
|
-
#include <functional>
|
|
12
|
-
#include <span>
|
|
13
|
-
#include <string>
|
|
14
|
-
#include <vector>
|
|
15
|
-
|
|
16
15
|
namespace sfmljs::audio {
|
|
17
16
|
|
|
18
17
|
using Info = Nan::FunctionCallbackInfo<v8::Value>;
|
|
19
18
|
|
|
20
|
-
inline void Set(v8::Local<v8::Object> object,
|
|
19
|
+
inline void Set(v8::Local<v8::Object> object,
|
|
20
|
+
const char* name,
|
|
21
21
|
v8::Local<v8::Value> value) {
|
|
22
22
|
Nan::Set(object, Nan::New(name).ToLocalChecked(), value).FromJust();
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
inline bool Unsigned(v8::Local<v8::Value> value,
|
|
25
|
+
inline bool Unsigned(v8::Local<v8::Value> value,
|
|
26
|
+
unsigned& result) { // NOLINT(runtime/references)
|
|
26
27
|
if (!value->IsUint32()) {
|
|
27
28
|
Nan::ThrowRangeError("Expected an unsigned 32-bit integer");
|
|
28
29
|
return false;
|
|
@@ -31,7 +32,8 @@ inline bool Unsigned(v8::Local<v8::Value> value, unsigned& result) {
|
|
|
31
32
|
return true;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
inline bool String(v8::Local<v8::Value> value,
|
|
35
|
+
inline bool String(v8::Local<v8::Value> value,
|
|
36
|
+
std::string& result) { // NOLINT(runtime/references)
|
|
35
37
|
if (!value->IsString()) {
|
|
36
38
|
Nan::ThrowTypeError("Expected a string");
|
|
37
39
|
return false;
|
|
@@ -56,10 +58,12 @@ struct Bytes {
|
|
|
56
58
|
std::size_t size{};
|
|
57
59
|
};
|
|
58
60
|
|
|
59
|
-
inline bool ReadBytes(v8::Local<v8::Value> value,
|
|
61
|
+
inline bool ReadBytes(v8::Local<v8::Value> value,
|
|
62
|
+
Bytes& result, // NOLINT(runtime/references)
|
|
60
63
|
bool samples = false) {
|
|
61
64
|
if (samples ? !value->IsInt16Array() : !value->IsUint8Array()) {
|
|
62
|
-
Nan::ThrowTypeError(samples ? "Expected an Int16Array"
|
|
65
|
+
Nan::ThrowTypeError(samples ? "Expected an Int16Array"
|
|
66
|
+
: "Expected a Uint8Array");
|
|
63
67
|
return false;
|
|
64
68
|
}
|
|
65
69
|
auto view = value.As<v8::ArrayBufferView>();
|
|
@@ -73,13 +77,14 @@ inline bool ReadBytes(v8::Local<v8::Value> value, Bytes& result,
|
|
|
73
77
|
Nan::ThrowTypeError("Expected attached, fixed, non-shared storage");
|
|
74
78
|
return false;
|
|
75
79
|
}
|
|
76
|
-
result = {backing,
|
|
80
|
+
result = {backing,
|
|
81
|
+
static_cast<std::byte*>(backing->Data()) + view->ByteOffset(),
|
|
77
82
|
view->ByteLength()};
|
|
78
83
|
return true;
|
|
79
84
|
}
|
|
80
85
|
|
|
81
86
|
inline bool Channels(v8::Local<v8::Value> value,
|
|
82
|
-
std::vector<sf::SoundChannel>& result) {
|
|
87
|
+
std::vector<sf::SoundChannel>& result) { // NOLINT
|
|
83
88
|
if (!value->IsInt32Array()) {
|
|
84
89
|
Nan::ThrowTypeError("Expected an Int32Array channel map");
|
|
85
90
|
return false;
|
|
@@ -91,7 +96,8 @@ inline bool Channels(v8::Local<v8::Value> value,
|
|
|
91
96
|
}
|
|
92
97
|
Nan::TypedArrayContents<std::int32_t> data(value);
|
|
93
98
|
for (std::size_t i = 0; i < view->Length(); ++i) {
|
|
94
|
-
if ((*data)[i] < 0 ||
|
|
99
|
+
if ((*data)[i] < 0 ||
|
|
100
|
+
(*data)[i] > static_cast<int>(sf::SoundChannel::TopBackCenter)) {
|
|
95
101
|
Nan::ThrowRangeError("Unknown sound channel");
|
|
96
102
|
return false;
|
|
97
103
|
}
|
|
@@ -101,12 +107,15 @@ inline bool Channels(v8::Local<v8::Value> value,
|
|
|
101
107
|
}
|
|
102
108
|
|
|
103
109
|
template <typename Owner>
|
|
104
|
-
v8::Local<v8::ArrayBuffer> MapChannels(
|
|
105
|
-
|
|
110
|
+
v8::Local<v8::ArrayBuffer> MapChannels(
|
|
111
|
+
const std::shared_ptr<Owner>& owner,
|
|
112
|
+
const std::vector<sf::SoundChannel>& map) {
|
|
106
113
|
static_assert(sizeof(sf::SoundChannel) == 4);
|
|
107
114
|
return map.empty() ? v8::ArrayBuffer::New(v8::Isolate::GetCurrent(), 0)
|
|
108
|
-
|
|
109
|
-
|
|
115
|
+
: MapShared(v8::Isolate::GetCurrent(),
|
|
116
|
+
owner,
|
|
117
|
+
const_cast<sf::SoundChannel*>(map.data()),
|
|
118
|
+
map.size() * sizeof(map[0]));
|
|
110
119
|
}
|
|
111
120
|
|
|
112
121
|
// Derived facts and async results only. SFML-owned scalar members map directly
|
|
@@ -147,7 +156,7 @@ struct Buffer {
|
|
|
147
156
|
// Control/result storage is confined to the owning JS thread. The SFML calls
|
|
148
157
|
// implement the engine's conversions and synchronization, not naked writes.
|
|
149
158
|
struct Source {
|
|
150
|
-
unsigned kind{1};
|
|
159
|
+
unsigned kind{1}; // 0 Sound, 1 Music, 2 custom SoundStream
|
|
151
160
|
std::shared_ptr<Buffer> buffer;
|
|
152
161
|
std::shared_ptr<BufferData> samples;
|
|
153
162
|
Bytes encoded;
|
|
@@ -177,10 +186,18 @@ using SourceHandle = SharedHandle<Source>;
|
|
|
177
186
|
inline v8::Local<v8::Object> MapSource(const std::shared_ptr<Source>& source) {
|
|
178
187
|
auto result = Nan::New<v8::Object>();
|
|
179
188
|
Set(result, "handle", SourceHandle::Create(source));
|
|
180
|
-
Set(result,
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
189
|
+
Set(result,
|
|
190
|
+
"values",
|
|
191
|
+
MapShared(v8::Isolate::GetCurrent(),
|
|
192
|
+
source,
|
|
193
|
+
source->values.data(),
|
|
194
|
+
sizeof(source->values)));
|
|
195
|
+
Set(result,
|
|
196
|
+
"times",
|
|
197
|
+
MapShared(v8::Isolate::GetCurrent(),
|
|
198
|
+
source,
|
|
199
|
+
source->times.data(),
|
|
200
|
+
sizeof(source->times)));
|
|
184
201
|
return result;
|
|
185
202
|
}
|
|
186
203
|
|
|
@@ -189,7 +206,8 @@ class Job final : public sfmljs::AsyncWorker {
|
|
|
189
206
|
using Action = std::function<void()>;
|
|
190
207
|
using Result = std::function<v8::Local<v8::Value>()>;
|
|
191
208
|
Job(Nan::Callback* callback, Action work, Result result)
|
|
192
|
-
: AsyncWorker(callback, "sfml:Audio"),
|
|
209
|
+
: AsyncWorker(callback, "sfml:Audio"),
|
|
210
|
+
work_(std::move(work)),
|
|
193
211
|
result_(std::move(result)) {}
|
|
194
212
|
void ExecuteWork() override { work_(); }
|
|
195
213
|
void CompleteWork() override {
|
|
@@ -197,16 +215,20 @@ class Job final : public sfmljs::AsyncWorker {
|
|
|
197
215
|
v8::Local<v8::Value> args[] = {Nan::Null(), result_()};
|
|
198
216
|
callback->Call(2, args, async_resource);
|
|
199
217
|
}
|
|
218
|
+
|
|
200
219
|
private:
|
|
201
220
|
Action work_;
|
|
202
221
|
Result result_;
|
|
203
222
|
};
|
|
204
223
|
|
|
205
|
-
inline void Queue(v8::Local<v8::Value> callback,
|
|
224
|
+
inline void Queue(v8::Local<v8::Value> callback,
|
|
225
|
+
Job::Action work,
|
|
206
226
|
Job::Result result) {
|
|
207
|
-
if (!callback->IsFunction())
|
|
227
|
+
if (!callback->IsFunction())
|
|
228
|
+
return Nan::ThrowTypeError("Expected a callback");
|
|
208
229
|
auto cb = std::make_unique<Nan::Callback>(callback.As<v8::Function>());
|
|
209
|
-
auto job =
|
|
230
|
+
auto job =
|
|
231
|
+
std::make_unique<Job>(cb.get(), std::move(work), std::move(result));
|
|
210
232
|
cb.release();
|
|
211
233
|
Nan::AsyncQueueWorker(job.release());
|
|
212
234
|
}
|
|
@@ -214,7 +236,8 @@ inline void Queue(v8::Local<v8::Value> callback, Job::Action work,
|
|
|
214
236
|
v8::Local<v8::Object> MapBuffer(const std::shared_ptr<BufferData>& data);
|
|
215
237
|
void CommitBuffer(const std::shared_ptr<Buffer>& buffer,
|
|
216
238
|
const std::shared_ptr<BufferData>& data);
|
|
217
|
-
void CopySourceSettings(sf::SoundSource& to,
|
|
239
|
+
void CopySourceSettings(sf::SoundSource& to, // NOLINT(runtime/references)
|
|
240
|
+
const sf::SoundSource& from);
|
|
218
241
|
void InstallSourceEffect(const std::shared_ptr<Source>& source);
|
|
219
242
|
|
|
220
|
-
}
|
|
243
|
+
} // namespace sfmljs::audio
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
#include "audio-stream.h"
|
|
2
|
-
|
|
3
1
|
#include <atomic>
|
|
4
|
-
#include <condition_variable>
|
|
2
|
+
#include <condition_variable> // NOLINT(build/c++11)
|
|
5
3
|
#include <cstring>
|
|
6
|
-
#include <thread>
|
|
4
|
+
#include <thread> // NOLINT(build/c++11)
|
|
5
|
+
|
|
6
|
+
#include "audio-stream.h"
|
|
7
7
|
|
|
8
8
|
namespace sfmljs::audio {
|
|
9
9
|
namespace {
|
|
@@ -28,58 +28,80 @@ NAN_METHOD(CompleteStreamRequest) {
|
|
|
28
28
|
auto request = handle->owner();
|
|
29
29
|
std::lock_guard lock(request->mutex);
|
|
30
30
|
if (request->complete) return;
|
|
31
|
-
GuardExceptions(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
31
|
+
GuardExceptions(
|
|
32
|
+
[&] {
|
|
33
|
+
if (!info[0]->IsNullOrUndefined()) {
|
|
34
|
+
Nan::Utf8String error(info[0]);
|
|
35
|
+
request->error.assign(*error, error.length());
|
|
36
|
+
if (request->error.empty())
|
|
37
|
+
request->error = "Audio InputStream failed";
|
|
38
|
+
} else if (!info[1]->IsUndefined()) {
|
|
39
|
+
bool lossless{};
|
|
40
|
+
const auto value =
|
|
41
|
+
info[1]->IsBigInt()
|
|
42
|
+
? info[1].As<v8::BigInt>()->Uint64Value(&lossless)
|
|
43
|
+
: 0;
|
|
44
|
+
if (!lossless || value > std::numeric_limits<std::size_t>::max() ||
|
|
45
|
+
(request->operation == 0 && value > request->bytes.size())) {
|
|
46
|
+
request->error =
|
|
47
|
+
"Stream result must be a non-negative bigint within range";
|
|
48
|
+
} else {
|
|
49
|
+
request->result = static_cast<std::size_t>(value);
|
|
50
|
+
if (request->operation == 0) {
|
|
51
|
+
request->snapshot.assign(request->bytes.begin(),
|
|
52
|
+
request->bytes.begin() + value);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
47
55
|
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
}, [&](const char* error) { request->error = error; });
|
|
56
|
+
},
|
|
57
|
+
[&](const char* error) { request->error = error; });
|
|
51
58
|
request->complete = true;
|
|
52
59
|
request->ready.notify_one();
|
|
53
60
|
}
|
|
54
61
|
|
|
55
62
|
class ThreadJob {
|
|
56
63
|
public:
|
|
57
|
-
ThreadJob(v8::Local<v8::Function> callback,
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
ThreadJob(v8::Local<v8::Function> callback,
|
|
65
|
+
Job::Action work,
|
|
66
|
+
Job::Result result,
|
|
67
|
+
Job::Action cancel)
|
|
68
|
+
: callback_(callback),
|
|
69
|
+
resource_("sfml:AudioStream"),
|
|
70
|
+
work_(std::move(work)),
|
|
71
|
+
result_(std::move(result)),
|
|
72
|
+
cancel_(std::move(cancel)) {}
|
|
61
73
|
void Start() {
|
|
62
|
-
const int status =
|
|
63
|
-
|
|
74
|
+
const int status =
|
|
75
|
+
uv_async_init(node::GetCurrentEventLoop(v8::Isolate::GetCurrent()),
|
|
76
|
+
&async_,
|
|
77
|
+
[](uv_async_t* handle) {
|
|
78
|
+
static_cast<ThreadJob*>(handle->data)->Finish();
|
|
79
|
+
});
|
|
64
80
|
if (status) throw std::runtime_error(uv_strerror(status));
|
|
65
81
|
async_.data = this;
|
|
66
|
-
cleanup_ = node::AddEnvironmentCleanupHook(
|
|
82
|
+
cleanup_ = node::AddEnvironmentCleanupHook(
|
|
83
|
+
v8::Isolate::GetCurrent(),
|
|
67
84
|
[](void* pointer, void (*done)(void*), void* argument) {
|
|
68
85
|
auto* job = static_cast<ThreadJob*>(pointer);
|
|
69
86
|
job->cleanupDone_ = done;
|
|
70
87
|
job->cleanupArgument_ = argument;
|
|
71
88
|
if (job->cancel_) job->cancel_();
|
|
72
|
-
},
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
89
|
+
},
|
|
90
|
+
this);
|
|
91
|
+
GuardExceptions(
|
|
92
|
+
[&] {
|
|
93
|
+
thread_ = std::thread([this] {
|
|
94
|
+
GuardExceptions(work_,
|
|
95
|
+
[this](const char* error) { error_ = error; });
|
|
96
|
+
uv_async_send(&async_);
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
[this](const char* error) {
|
|
100
|
+
error_ = error;
|
|
101
|
+
uv_async_send(&async_);
|
|
102
|
+
});
|
|
82
103
|
}
|
|
104
|
+
|
|
83
105
|
private:
|
|
84
106
|
void Finish() {
|
|
85
107
|
if (thread_.joinable()) thread_.join();
|
|
@@ -92,7 +114,8 @@ class ThreadJob {
|
|
|
92
114
|
[this](const char* error) { error_ = error; });
|
|
93
115
|
}
|
|
94
116
|
v8::Local<v8::Value> error = error_.empty()
|
|
95
|
-
|
|
117
|
+
? v8::Local<v8::Value>(Nan::Null())
|
|
118
|
+
: Error(error_.c_str());
|
|
96
119
|
v8::Local<v8::Value> args[] = {error, value};
|
|
97
120
|
callback_.Call(2, args, &resource_);
|
|
98
121
|
}
|
|
@@ -113,16 +136,21 @@ class ThreadJob {
|
|
|
113
136
|
Job::Action cancel_;
|
|
114
137
|
std::string error_;
|
|
115
138
|
node::AsyncCleanupHookHandle cleanup_;
|
|
116
|
-
void (*cleanupDone_)(void*)
|
|
139
|
+
void (*cleanupDone_)(void*) = nullptr;
|
|
117
140
|
void* cleanupArgument_{};
|
|
118
141
|
};
|
|
119
|
-
}
|
|
142
|
+
} // namespace
|
|
120
143
|
|
|
121
|
-
void QueueThread(v8::Local<v8::Value> callback,
|
|
122
|
-
Job::
|
|
123
|
-
|
|
144
|
+
void QueueThread(v8::Local<v8::Value> callback,
|
|
145
|
+
Job::Action work,
|
|
146
|
+
Job::Result result,
|
|
147
|
+
Job::Action cancel) {
|
|
148
|
+
if (!callback->IsFunction())
|
|
149
|
+
return Nan::ThrowTypeError("Expected a callback");
|
|
124
150
|
auto job = std::make_unique<ThreadJob>(callback.As<v8::Function>(),
|
|
125
|
-
|
|
151
|
+
std::move(work),
|
|
152
|
+
std::move(result),
|
|
153
|
+
std::move(cancel));
|
|
126
154
|
job->Start();
|
|
127
155
|
job.release();
|
|
128
156
|
}
|
|
@@ -148,8 +176,8 @@ struct StreamClient::Bridge : std::enable_shared_from_this<Bridge> {
|
|
|
148
176
|
}
|
|
149
177
|
void Start() {
|
|
150
178
|
isolate = v8::Isolate::GetCurrent();
|
|
151
|
-
const int status = uv_async_init(
|
|
152
|
-
[](uv_async_t* handle) {
|
|
179
|
+
const int status = uv_async_init(
|
|
180
|
+
node::GetCurrentEventLoop(isolate), &async, [](uv_async_t* handle) {
|
|
153
181
|
auto self = *static_cast<std::shared_ptr<Bridge>*>(handle->data);
|
|
154
182
|
self->Dispatch();
|
|
155
183
|
});
|
|
@@ -175,7 +203,8 @@ struct StreamClient::Bridge : std::enable_shared_from_this<Bridge> {
|
|
|
175
203
|
if (closing) return;
|
|
176
204
|
closing = true;
|
|
177
205
|
}
|
|
178
|
-
if (!cleanup)
|
|
206
|
+
if (!cleanup)
|
|
207
|
+
node::RemoveEnvironmentCleanupHook(isolate, Cleanup, async.data);
|
|
179
208
|
dispatch.Reset();
|
|
180
209
|
resource.reset();
|
|
181
210
|
uv_close(reinterpret_cast<uv_handle_t*>(&async), [](uv_handle_t* handle) {
|
|
@@ -187,7 +216,8 @@ struct StreamClient::Bridge : std::enable_shared_from_this<Bridge> {
|
|
|
187
216
|
next->operation = operation;
|
|
188
217
|
next->position = value;
|
|
189
218
|
if (operation == 0) {
|
|
190
|
-
if (value > node::Buffer::kMaxLength)
|
|
219
|
+
if (value > node::Buffer::kMaxLength)
|
|
220
|
+
throw std::length_error("Stream read too large");
|
|
191
221
|
next->bytes.resize(value);
|
|
192
222
|
}
|
|
193
223
|
{
|
|
@@ -209,49 +239,67 @@ struct StreamClient::Bridge : std::enable_shared_from_this<Bridge> {
|
|
|
209
239
|
cancel = cancelled;
|
|
210
240
|
next = request;
|
|
211
241
|
}
|
|
212
|
-
if (cancel) {
|
|
242
|
+
if (cancel) {
|
|
243
|
+
Close();
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
213
246
|
if (!next || next->dispatched) return;
|
|
214
247
|
next->dispatched = true;
|
|
215
248
|
Nan::HandleScope scope;
|
|
216
|
-
GuardExceptions(
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
249
|
+
GuardExceptions(
|
|
250
|
+
[&] {
|
|
251
|
+
auto complete = Nan::GetFunction(Nan::New<v8::FunctionTemplate>(
|
|
252
|
+
CompleteStreamRequest,
|
|
253
|
+
RequestHandle::Create(next)))
|
|
254
|
+
.ToLocalChecked();
|
|
255
|
+
v8::Local<v8::Value> data = Nan::Undefined();
|
|
256
|
+
if (next->operation == 0) {
|
|
257
|
+
auto buffer = next->bytes.empty() ? v8::ArrayBuffer::New(isolate, 0)
|
|
258
|
+
: MapShared(isolate,
|
|
259
|
+
next,
|
|
260
|
+
next->bytes.data(),
|
|
261
|
+
next->bytes.size());
|
|
262
|
+
data = v8::Uint8Array::New(buffer, 0, next->bytes.size());
|
|
263
|
+
}
|
|
264
|
+
v8::Local<v8::Value> args[] = {
|
|
265
|
+
Nan::New(next->operation),
|
|
266
|
+
data,
|
|
267
|
+
v8::BigInt::NewFromUnsigned(isolate, next->position),
|
|
268
|
+
complete,
|
|
269
|
+
};
|
|
270
|
+
Nan::TryCatch caught;
|
|
271
|
+
dispatch.Call(4, args, resource.get());
|
|
272
|
+
if (caught.HasCaught()) {
|
|
273
|
+
std::lock_guard lock(next->mutex);
|
|
274
|
+
if (!next->complete) {
|
|
275
|
+
Nan::Utf8String error(caught.Exception());
|
|
276
|
+
next->error.assign(*error, error.length());
|
|
277
|
+
next->complete = true;
|
|
278
|
+
next->ready.notify_one();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
[&](const char* error) {
|
|
283
|
+
std::lock_guard lock(next->mutex);
|
|
284
|
+
if (!next->complete) {
|
|
285
|
+
next->error = error;
|
|
286
|
+
next->complete = true;
|
|
287
|
+
next->ready.notify_one();
|
|
288
|
+
}
|
|
289
|
+
});
|
|
248
290
|
}
|
|
249
291
|
};
|
|
250
292
|
|
|
251
293
|
StreamClient::StreamClient(v8::Local<v8::Function> dispatch)
|
|
252
|
-
: bridge(std::make_shared<Bridge>(dispatch)) {
|
|
253
|
-
|
|
254
|
-
|
|
294
|
+
: bridge(std::make_shared<Bridge>(dispatch)) {
|
|
295
|
+
bridge->Start();
|
|
296
|
+
}
|
|
297
|
+
StreamClient::~StreamClient() {
|
|
298
|
+
bridge->Cancel();
|
|
299
|
+
}
|
|
300
|
+
void StreamClient::Cancel() {
|
|
301
|
+
bridge->Cancel();
|
|
302
|
+
}
|
|
255
303
|
std::optional<std::size_t> StreamClient::read(void* data, std::size_t size) {
|
|
256
304
|
auto request = bridge->Exchange(0, size);
|
|
257
305
|
if (request->result && *request->result) {
|
|
@@ -262,11 +310,16 @@ std::optional<std::size_t> StreamClient::read(void* data, std::size_t size) {
|
|
|
262
310
|
std::optional<std::size_t> StreamClient::seek(std::size_t position) {
|
|
263
311
|
return bridge->Exchange(1, position)->result;
|
|
264
312
|
}
|
|
265
|
-
std::optional<std::size_t> StreamClient::tell() {
|
|
266
|
-
|
|
313
|
+
std::optional<std::size_t> StreamClient::tell() {
|
|
314
|
+
return bridge->Exchange(2)->result;
|
|
315
|
+
}
|
|
316
|
+
std::optional<std::size_t> StreamClient::getSize() {
|
|
317
|
+
return bridge->Exchange(3)->result;
|
|
318
|
+
}
|
|
267
319
|
|
|
268
320
|
NAN_METHOD(CreateAudioInputStream) {
|
|
269
|
-
if (!info[0]->IsFunction())
|
|
321
|
+
if (!info[0]->IsFunction())
|
|
322
|
+
return Nan::ThrowTypeError("Expected stream dispatch");
|
|
270
323
|
info.GetReturnValue().Set(StreamClientHandle::Create(
|
|
271
324
|
std::make_shared<StreamClient>(info[0].As<v8::Function>())));
|
|
272
325
|
}
|
|
@@ -275,10 +328,11 @@ NAN_METHOD(CloseAudioInputStream) {
|
|
|
275
328
|
if (handle) handle->owner()->Cancel();
|
|
276
329
|
}
|
|
277
330
|
|
|
278
|
-
}
|
|
331
|
+
} // namespace sfmljs::audio
|
|
279
332
|
|
|
280
333
|
NAN_MODULE_INIT(InitializeAudioStreams) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
sfmljs::SetMethod<CloseAudioInputStream>(
|
|
334
|
+
sfmljs::SetMethod<sfmljs::audio::CreateAudioInputStream>(
|
|
335
|
+
target, "createAudioInputStream");
|
|
336
|
+
sfmljs::SetMethod<sfmljs::audio::CloseAudioInputStream>(
|
|
337
|
+
target, "closeAudioInputStream");
|
|
284
338
|
}
|
package/native/audio-stream.h
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
3
|
#include "audio-state.h"
|
|
4
|
-
|
|
5
4
|
#include <SFML/System/InputStream.hpp>
|
|
6
5
|
|
|
7
6
|
namespace sfmljs::audio {
|
|
@@ -22,7 +21,9 @@ struct StreamClient : sf::InputStream {
|
|
|
22
21
|
|
|
23
22
|
using StreamClientHandle = SharedHandle<StreamClient>;
|
|
24
23
|
|
|
25
|
-
void QueueThread(v8::Local<v8::Value> callback,
|
|
26
|
-
Job::
|
|
24
|
+
void QueueThread(v8::Local<v8::Value> callback,
|
|
25
|
+
Job::Action work,
|
|
26
|
+
Job::Result result,
|
|
27
|
+
Job::Action cancel = {});
|
|
27
28
|
|
|
28
|
-
}
|
|
29
|
+
} // namespace sfmljs::audio
|