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
|
@@ -10,11 +10,15 @@ using ReaderInfoHandle = SharedHandle<ReaderInfo>;
|
|
|
10
10
|
v8::Local<v8::Object> MapReaderInfo(const std::shared_ptr<ReaderInfo>& owner) {
|
|
11
11
|
auto result = Nan::New<v8::Object>();
|
|
12
12
|
Set(result, "handle", ReaderInfoHandle::Create(owner));
|
|
13
|
-
Set(result,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
Set(result,
|
|
14
|
+
"info",
|
|
15
|
+
NumericFields()
|
|
16
|
+
.Add("sampleCount", owner->info.sampleCount)
|
|
17
|
+
.Add("channelCount", owner->info.channelCount)
|
|
18
|
+
.Add("sampleRate", owner->info.sampleRate)
|
|
19
|
+
.Map(owner));
|
|
20
|
+
auto map =
|
|
21
|
+
std::make_shared<std::vector<sf::SoundChannel>>(owner->info.channelMap);
|
|
18
22
|
Set(result, "channelMap", MapChannels(map, *map));
|
|
19
23
|
return result;
|
|
20
24
|
}
|
|
@@ -22,12 +26,14 @@ v8::Local<v8::Object> MapReaderInfo(const std::shared_ptr<ReaderInfo>& owner) {
|
|
|
22
26
|
NAN_METHOD(CreateAudioReaderInfo) {
|
|
23
27
|
info.GetReturnValue().Set(MapReaderInfo(std::make_shared<ReaderInfo>()));
|
|
24
28
|
}
|
|
29
|
+
|
|
25
30
|
NAN_METHOD(SetAudioReaderChannels) {
|
|
26
31
|
auto* handle = ReaderInfoHandle::From(info[0]);
|
|
27
32
|
std::vector<sf::SoundChannel> map;
|
|
28
33
|
if (!handle || !Channels(info[1], map)) return;
|
|
29
34
|
handle->owner()->info.channelMap = map;
|
|
30
|
-
auto channels =
|
|
35
|
+
auto channels =
|
|
36
|
+
std::make_shared<std::vector<sf::SoundChannel>>(std::move(map));
|
|
31
37
|
info.GetReturnValue().Set(MapChannels(channels, *channels));
|
|
32
38
|
}
|
|
33
39
|
|
|
@@ -46,19 +52,28 @@ NAN_METHOD(CreateAudioReader) {
|
|
|
46
52
|
if (!String(info[0], path)) return;
|
|
47
53
|
} else if (auto* handle = StreamClientHandle::TryFrom(info[0])) {
|
|
48
54
|
stream = handle->owner();
|
|
49
|
-
} else if (!ReadBytes(info[0], bytes))
|
|
55
|
+
} else if (!ReadBytes(info[0], bytes)) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
50
58
|
auto work = [reader, bytes, path, stream] {
|
|
51
|
-
if (stream)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
if (stream) {
|
|
60
|
+
reader->reader = sf::SoundFileFactory::createReaderFromStream(*stream);
|
|
61
|
+
} else if (bytes.backing) {
|
|
62
|
+
reader->reader =
|
|
63
|
+
sf::SoundFileFactory::createReaderFromMemory(bytes.data, bytes.size);
|
|
64
|
+
} else {
|
|
65
|
+
reader->reader =
|
|
66
|
+
sf::SoundFileFactory::createReaderFromFilename(Path(path));
|
|
67
|
+
}
|
|
55
68
|
};
|
|
56
69
|
auto result = [reader]() -> v8::Local<v8::Value> {
|
|
57
70
|
if (!reader->reader) return Nan::Undefined();
|
|
58
71
|
return ReaderHandle::Create(reader);
|
|
59
72
|
};
|
|
60
|
-
if (stream)
|
|
61
|
-
|
|
73
|
+
if (stream)
|
|
74
|
+
QueueThread(info[1], work, result, [stream] { stream->Cancel(); });
|
|
75
|
+
else
|
|
76
|
+
Queue(info[1], work, result);
|
|
62
77
|
}
|
|
63
78
|
|
|
64
79
|
NAN_METHOD(OpenAudioReader) {
|
|
@@ -69,15 +84,17 @@ NAN_METHOD(OpenAudioReader) {
|
|
|
69
84
|
if (!reader->reader) return Nan::ThrowError("Reader is closed");
|
|
70
85
|
auto stream = input->owner();
|
|
71
86
|
auto result = std::make_shared<std::optional<sf::SoundFileReader::Info>>();
|
|
72
|
-
QueueThread(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
87
|
+
QueueThread(
|
|
88
|
+
info[2],
|
|
89
|
+
[reader, stream, result] { *result = reader->reader->open(*stream); },
|
|
90
|
+
[reader, stream, result]() -> v8::Local<v8::Value> {
|
|
91
|
+
if (!*result) return Nan::Undefined();
|
|
92
|
+
reader->stream = stream;
|
|
93
|
+
auto value = std::make_shared<ReaderInfo>();
|
|
94
|
+
value->info = std::move(**result);
|
|
95
|
+
return MapReaderInfo(value);
|
|
96
|
+
},
|
|
97
|
+
[stream] { stream->Cancel(); });
|
|
81
98
|
}
|
|
82
99
|
|
|
83
100
|
NAN_METHOD(ReadAudioReader) {
|
|
@@ -85,24 +102,35 @@ NAN_METHOD(ReadAudioReader) {
|
|
|
85
102
|
Bytes bytes;
|
|
86
103
|
if (!handle || !ReadBytes(info[1], bytes, true)) return;
|
|
87
104
|
auto reader = handle->owner();
|
|
88
|
-
if (!reader->reader || !reader->stream)
|
|
105
|
+
if (!reader->reader || !reader->stream)
|
|
106
|
+
return Nan::ThrowError("Reader is not open");
|
|
89
107
|
auto count = std::make_shared<std::uint64_t>();
|
|
90
|
-
QueueThread(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
108
|
+
QueueThread(
|
|
109
|
+
info[2],
|
|
110
|
+
[reader, bytes, count] {
|
|
111
|
+
*count = reader->reader->read(static_cast<std::int16_t*>(bytes.data),
|
|
112
|
+
bytes.size / 2);
|
|
113
|
+
},
|
|
114
|
+
[count]() -> v8::Local<v8::Value> {
|
|
115
|
+
return v8::BigInt::NewFromUnsigned(v8::Isolate::GetCurrent(), *count);
|
|
116
|
+
},
|
|
117
|
+
[stream = reader->stream] { stream->Cancel(); });
|
|
95
118
|
}
|
|
96
119
|
|
|
97
120
|
NAN_METHOD(SeekAudioReader) {
|
|
98
121
|
auto* handle = ReaderHandle::From(info[0]);
|
|
99
122
|
if (!handle) return;
|
|
100
123
|
bool lossless{};
|
|
101
|
-
const auto offset = info[1]->IsBigInt()
|
|
124
|
+
const auto offset = info[1]->IsBigInt()
|
|
125
|
+
? info[1].As<v8::BigInt>()->Uint64Value(&lossless)
|
|
126
|
+
: 0;
|
|
102
127
|
if (!lossless) return Nan::ThrowRangeError("Expected uint64 sample offset");
|
|
103
128
|
auto reader = handle->owner();
|
|
104
|
-
if (!reader->reader || !reader->stream)
|
|
105
|
-
|
|
129
|
+
if (!reader->reader || !reader->stream)
|
|
130
|
+
return Nan::ThrowError("Reader is not open");
|
|
131
|
+
QueueThread(
|
|
132
|
+
info[2],
|
|
133
|
+
[reader, offset] { reader->reader->seek(offset); },
|
|
106
134
|
[]() -> v8::Local<v8::Value> { return Nan::Undefined(); },
|
|
107
135
|
[stream = reader->stream] { stream->Cancel(); });
|
|
108
136
|
}
|
|
@@ -111,14 +139,19 @@ NAN_METHOD(CloseAudioReader) {
|
|
|
111
139
|
auto* handle = ReaderHandle::From(info[0]);
|
|
112
140
|
if (!handle) return;
|
|
113
141
|
auto reader = handle->owner();
|
|
114
|
-
Queue(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
142
|
+
Queue(
|
|
143
|
+
info[1],
|
|
144
|
+
[reader] {
|
|
145
|
+
reader->reader.reset();
|
|
146
|
+
if (reader->stream) reader->stream->Cancel();
|
|
147
|
+
reader->stream.reset();
|
|
148
|
+
},
|
|
149
|
+
[]() -> v8::Local<v8::Value> { return Nan::Undefined(); });
|
|
119
150
|
}
|
|
120
151
|
|
|
121
|
-
struct Writer {
|
|
152
|
+
struct Writer {
|
|
153
|
+
std::unique_ptr<sf::SoundFileWriter> writer;
|
|
154
|
+
};
|
|
122
155
|
using WriterHandle = SharedHandle<Writer>;
|
|
123
156
|
NAN_METHOD(CreateAudioWriter) {
|
|
124
157
|
std::string path;
|
|
@@ -133,13 +166,17 @@ NAN_METHOD(OpenAudioWriter) {
|
|
|
133
166
|
unsigned rate, count;
|
|
134
167
|
std::vector<sf::SoundChannel> map;
|
|
135
168
|
if (!handle || !String(info[1], path) || !Unsigned(info[2], rate) ||
|
|
136
|
-
!Unsigned(info[3], count) || !Channels(info[4], map))
|
|
169
|
+
!Unsigned(info[3], count) || !Channels(info[4], map))
|
|
170
|
+
return;
|
|
137
171
|
auto writer = handle->owner();
|
|
138
172
|
if (!writer->writer) return Nan::ThrowError("Writer is closed");
|
|
139
173
|
auto result = std::make_shared<bool>();
|
|
140
|
-
Queue(
|
|
141
|
-
|
|
142
|
-
|
|
174
|
+
Queue(
|
|
175
|
+
info[5],
|
|
176
|
+
[writer, path, rate, count, map, result] {
|
|
177
|
+
*result = writer->writer->open(Path(path), rate, count, map);
|
|
178
|
+
},
|
|
179
|
+
[result]() -> v8::Local<v8::Value> { return Nan::New(*result); });
|
|
143
180
|
}
|
|
144
181
|
NAN_METHOD(WriteAudioWriter) {
|
|
145
182
|
auto* handle = WriterHandle::From(info[0]);
|
|
@@ -147,16 +184,22 @@ NAN_METHOD(WriteAudioWriter) {
|
|
|
147
184
|
if (!handle || !ReadBytes(info[1], samples, true)) return;
|
|
148
185
|
auto writer = handle->owner();
|
|
149
186
|
if (!writer->writer) return Nan::ThrowError("Writer is closed");
|
|
150
|
-
Queue(
|
|
151
|
-
|
|
152
|
-
|
|
187
|
+
Queue(
|
|
188
|
+
info[2],
|
|
189
|
+
[writer, samples] {
|
|
190
|
+
writer->writer->write(static_cast<std::int16_t*>(samples.data),
|
|
191
|
+
samples.size / 2);
|
|
192
|
+
},
|
|
193
|
+
[]() -> v8::Local<v8::Value> { return Nan::Undefined(); });
|
|
153
194
|
}
|
|
154
195
|
NAN_METHOD(CloseAudioWriter) {
|
|
155
196
|
auto* handle = WriterHandle::From(info[0]);
|
|
156
197
|
if (!handle) return;
|
|
157
198
|
auto writer = handle->owner();
|
|
158
|
-
Queue(
|
|
159
|
-
|
|
199
|
+
Queue(
|
|
200
|
+
info[1],
|
|
201
|
+
[writer] { writer->writer.reset(); },
|
|
202
|
+
[]() -> v8::Local<v8::Value> { return Nan::Undefined(); });
|
|
160
203
|
}
|
|
161
204
|
|
|
162
205
|
struct FileMetadata {
|
|
@@ -164,7 +207,9 @@ struct FileMetadata {
|
|
|
164
207
|
std::vector<sf::SoundChannel> channels;
|
|
165
208
|
void UpdateTime() {
|
|
166
209
|
value.timeOffset = sf::seconds(static_cast<float>(value.offset) /
|
|
167
|
-
|
|
210
|
+
static_cast<float>(value.channels) /
|
|
211
|
+
static_cast<float>(value.rate))
|
|
212
|
+
.asMicroseconds();
|
|
168
213
|
}
|
|
169
214
|
};
|
|
170
215
|
using FileMetadataHandle = SharedHandle<FileMetadata>;
|
|
@@ -182,10 +227,14 @@ NAN_METHOD(CreateAudioFileMetadata) {
|
|
|
182
227
|
file->value.rate = metadata.sampleRate;
|
|
183
228
|
file->channels = metadata.channelMap;
|
|
184
229
|
file->value.duration = sf::seconds(static_cast<float>(metadata.sampleCount) /
|
|
185
|
-
|
|
230
|
+
static_cast<float>(metadata.channelCount) /
|
|
231
|
+
static_cast<float>(metadata.sampleRate))
|
|
232
|
+
.asMicroseconds();
|
|
186
233
|
auto result = Nan::New<v8::Object>();
|
|
187
234
|
Set(result, "handle", FileMetadataHandle::Create(file));
|
|
188
|
-
Set(result,
|
|
235
|
+
Set(result,
|
|
236
|
+
"buffer",
|
|
237
|
+
MapShared(info.GetIsolate(), file, &file->value, sizeof(file->value)));
|
|
189
238
|
Set(result, "channelMap", MapChannels(file, file->channels));
|
|
190
239
|
info.GetReturnValue().Set(result);
|
|
191
240
|
}
|
|
@@ -195,22 +244,33 @@ NAN_METHOD(UpdateAudioFileMetadata) {
|
|
|
195
244
|
if (!handle || !Unsigned(info[1], operation)) return;
|
|
196
245
|
auto& file = *handle->owner();
|
|
197
246
|
bool lossless{};
|
|
198
|
-
const auto offset = info[2]->IsBigInt()
|
|
247
|
+
const auto offset = info[2]->IsBigInt()
|
|
248
|
+
? info[2].As<v8::BigInt>()->Uint64Value(&lossless)
|
|
249
|
+
: 0;
|
|
199
250
|
if (!lossless && operation != 2 && operation != 3) {
|
|
200
251
|
return Nan::ThrowRangeError("Expected uint64 sample count");
|
|
201
252
|
}
|
|
202
|
-
if (operation == 0) {
|
|
253
|
+
if (operation == 0) { // read completed
|
|
203
254
|
file.value.offset += offset;
|
|
204
255
|
file.value.transferred = offset;
|
|
205
|
-
} else if (operation == 1) {
|
|
206
|
-
file.value.transferred = std::min(
|
|
207
|
-
|
|
208
|
-
|
|
256
|
+
} else if (operation == 1) { // prepare sample seek
|
|
257
|
+
file.value.transferred = std::min(
|
|
258
|
+
offset / file.value.channels * file.value.channels, file.value.count);
|
|
259
|
+
} else if (operation ==
|
|
260
|
+
2) { // prepare Time seek, preserving SFML float arithmetic
|
|
261
|
+
if (!info[2]->IsBigInt())
|
|
262
|
+
return Nan::ThrowTypeError("Expected microseconds");
|
|
209
263
|
const auto time = sf::microseconds(info[2].As<v8::BigInt>()->Int64Value());
|
|
210
|
-
file.value.transferred =
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
264
|
+
file.value.transferred =
|
|
265
|
+
std::min(static_cast<std::uint64_t>(
|
|
266
|
+
time.asSeconds() * static_cast<float>(file.value.rate)) *
|
|
267
|
+
file.value.channels,
|
|
268
|
+
file.value.count);
|
|
269
|
+
} else if (operation == 3) {
|
|
270
|
+
file.value.offset = file.value.transferred;
|
|
271
|
+
} else {
|
|
272
|
+
return Nan::ThrowRangeError("Unknown metadata operation");
|
|
273
|
+
}
|
|
214
274
|
file.UpdateTime();
|
|
215
275
|
}
|
|
216
276
|
|
|
@@ -221,26 +281,36 @@ NAN_METHOD(AllocateAudioSamples) {
|
|
|
221
281
|
return Nan::ThrowRangeError("Audio sample buffer is too large");
|
|
222
282
|
}
|
|
223
283
|
auto data = std::make_shared<std::vector<std::int16_t>>(count);
|
|
224
|
-
info.GetReturnValue().Set(
|
|
225
|
-
|
|
284
|
+
info.GetReturnValue().Set(
|
|
285
|
+
count ? MapShared(info.GetIsolate(), data, data->data(), count * 2)
|
|
286
|
+
: v8::ArrayBuffer::New(info.GetIsolate(), 0));
|
|
226
287
|
}
|
|
227
288
|
|
|
228
|
-
}
|
|
289
|
+
} // namespace sfmljs::audio
|
|
229
290
|
|
|
230
291
|
NAN_MODULE_INIT(InitializeAudioCodecs) {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
sfmljs::SetMethod<SetAudioReaderChannels>(
|
|
234
|
-
|
|
235
|
-
sfmljs::SetMethod<
|
|
236
|
-
|
|
237
|
-
sfmljs::SetMethod<
|
|
238
|
-
sfmljs::SetMethod<
|
|
239
|
-
sfmljs::SetMethod<
|
|
240
|
-
sfmljs::SetMethod<
|
|
241
|
-
|
|
242
|
-
sfmljs::SetMethod<
|
|
243
|
-
|
|
244
|
-
sfmljs::SetMethod<
|
|
245
|
-
sfmljs::SetMethod<
|
|
292
|
+
sfmljs::SetMethod<sfmljs::audio::CreateAudioReaderInfo>(
|
|
293
|
+
target, "createAudioReaderInfo");
|
|
294
|
+
sfmljs::SetMethod<sfmljs::audio::SetAudioReaderChannels>(
|
|
295
|
+
target, "setAudioReaderChannels");
|
|
296
|
+
sfmljs::SetMethod<sfmljs::audio::CreateAudioReader>(target,
|
|
297
|
+
"createAudioReader");
|
|
298
|
+
sfmljs::SetMethod<sfmljs::audio::OpenAudioReader>(target, "openAudioReader");
|
|
299
|
+
sfmljs::SetMethod<sfmljs::audio::ReadAudioReader>(target, "readAudioReader");
|
|
300
|
+
sfmljs::SetMethod<sfmljs::audio::SeekAudioReader>(target, "seekAudioReader");
|
|
301
|
+
sfmljs::SetMethod<sfmljs::audio::CloseAudioReader>(target,
|
|
302
|
+
"closeAudioReader");
|
|
303
|
+
sfmljs::SetMethod<sfmljs::audio::CreateAudioWriter>(target,
|
|
304
|
+
"createAudioWriter");
|
|
305
|
+
sfmljs::SetMethod<sfmljs::audio::OpenAudioWriter>(target, "openAudioWriter");
|
|
306
|
+
sfmljs::SetMethod<sfmljs::audio::WriteAudioWriter>(target,
|
|
307
|
+
"writeAudioWriter");
|
|
308
|
+
sfmljs::SetMethod<sfmljs::audio::CloseAudioWriter>(target,
|
|
309
|
+
"closeAudioWriter");
|
|
310
|
+
sfmljs::SetMethod<sfmljs::audio::CreateAudioFileMetadata>(
|
|
311
|
+
target, "createAudioFileMetadata");
|
|
312
|
+
sfmljs::SetMethod<sfmljs::audio::UpdateAudioFileMetadata>(
|
|
313
|
+
target, "updateAudioFileMetadata");
|
|
314
|
+
sfmljs::SetMethod<sfmljs::audio::AllocateAudioSamples>(
|
|
315
|
+
target, "allocateAudioSamples");
|
|
246
316
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
#include "audio-state.h"
|
|
2
|
-
|
|
3
1
|
#include <deque>
|
|
4
|
-
#include <mutex>
|
|
2
|
+
#include <mutex> // NOLINT(build/c++11)
|
|
3
|
+
|
|
4
|
+
#include "audio-state.h"
|
|
5
5
|
|
|
6
6
|
namespace sfmljs::audio {
|
|
7
7
|
|
|
@@ -11,7 +11,9 @@ NAN_METHOD(CreateAudioDeviceState) {
|
|
|
11
11
|
auto data = std::make_shared<DeviceData>();
|
|
12
12
|
auto result = Nan::New<v8::Object>();
|
|
13
13
|
Set(result, "handle", SharedHandle<DeviceData>::Create(data));
|
|
14
|
-
Set(result,
|
|
14
|
+
Set(result,
|
|
15
|
+
"buffer",
|
|
16
|
+
MapShared(info.GetIsolate(), data, data->data(), sizeof(*data)));
|
|
15
17
|
info.GetReturnValue().Set(result);
|
|
16
18
|
}
|
|
17
19
|
|
|
@@ -30,7 +32,10 @@ NAN_METHOD(AudioDevice) {
|
|
|
30
32
|
auto& data = *handle->owner();
|
|
31
33
|
std::string name;
|
|
32
34
|
switch (command) {
|
|
33
|
-
case 0:
|
|
35
|
+
case 0:
|
|
36
|
+
info.GetReturnValue().Set(
|
|
37
|
+
Strings(sf::PlaybackDevice::getAvailableDevices()));
|
|
38
|
+
break;
|
|
34
39
|
case 1:
|
|
35
40
|
if (const auto value = sf::PlaybackDevice::getDefaultDevice()) {
|
|
36
41
|
info.GetReturnValue().Set(Nan::New(*value).ToLocalChecked());
|
|
@@ -40,8 +45,12 @@ NAN_METHOD(AudioDevice) {
|
|
|
40
45
|
if (!String(info[2], name)) return;
|
|
41
46
|
data[3] = sf::PlaybackDevice::setDevice(name);
|
|
42
47
|
break;
|
|
43
|
-
case 3:
|
|
44
|
-
|
|
48
|
+
case 3:
|
|
49
|
+
data[3] = sf::PlaybackDevice::setDeviceToDefault();
|
|
50
|
+
break;
|
|
51
|
+
case 4:
|
|
52
|
+
data[3] = sf::PlaybackDevice::setDeviceToNull();
|
|
53
|
+
break;
|
|
45
54
|
case 5:
|
|
46
55
|
if (const auto value = sf::PlaybackDevice::getDevice()) {
|
|
47
56
|
info.GetReturnValue().Set(Nan::New(*value).ToLocalChecked());
|
|
@@ -49,11 +58,15 @@ NAN_METHOD(AudioDevice) {
|
|
|
49
58
|
break;
|
|
50
59
|
case 6: {
|
|
51
60
|
const auto value = sf::PlaybackDevice::getDeviceSampleRate();
|
|
52
|
-
data[0] = value.value_or(0);
|
|
61
|
+
data[0] = value.value_or(0);
|
|
62
|
+
data[1] = value.has_value();
|
|
53
63
|
break;
|
|
54
64
|
}
|
|
55
|
-
case 7:
|
|
56
|
-
|
|
65
|
+
case 7:
|
|
66
|
+
data[2] = sf::PlaybackDevice::isDefaultDevice();
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
return Nan::ThrowRangeError("Unknown playback device command");
|
|
57
70
|
}
|
|
58
71
|
}
|
|
59
72
|
|
|
@@ -78,9 +91,10 @@ void NotificationCleanup(void* pointer) {
|
|
|
78
91
|
}
|
|
79
92
|
sink->callback.Reset();
|
|
80
93
|
sink->resource.reset();
|
|
81
|
-
uv_close(
|
|
82
|
-
|
|
83
|
-
|
|
94
|
+
uv_close(
|
|
95
|
+
reinterpret_cast<uv_handle_t*>(&sink->async), [](uv_handle_t* value) {
|
|
96
|
+
delete static_cast<std::shared_ptr<NotificationSink>*>(value->data);
|
|
97
|
+
});
|
|
84
98
|
}
|
|
85
99
|
|
|
86
100
|
NAN_METHOD(AudioDeviceNotifications) {
|
|
@@ -102,36 +116,46 @@ NAN_METHOD(AudioDeviceNotifications) {
|
|
|
102
116
|
} else {
|
|
103
117
|
sink = std::make_shared<NotificationSink>();
|
|
104
118
|
sink->resource = std::make_unique<Nan::AsyncResource>("sfml:AudioDevice");
|
|
105
|
-
const int status = uv_async_init(
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
sink->pending.clear();
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
events.swap(sink->pending);
|
|
116
|
-
}
|
|
117
|
-
Nan::HandleScope scope;
|
|
118
|
-
for (const auto event : events) {
|
|
119
|
+
const int status = uv_async_init(
|
|
120
|
+
node::GetCurrentEventLoop(info.GetIsolate()),
|
|
121
|
+
&sink->async,
|
|
122
|
+
[](uv_async_t* async) {
|
|
123
|
+
auto sink =
|
|
124
|
+
*static_cast<std::shared_ptr<NotificationSink>*>(async->data);
|
|
125
|
+
std::deque<unsigned> events;
|
|
119
126
|
{
|
|
120
127
|
std::lock_guard lock(notificationMutex);
|
|
121
|
-
if (notificationTarget.lock() != sink)
|
|
128
|
+
if (sink->closing || notificationTarget.lock() != sink) {
|
|
129
|
+
sink->pending.clear();
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
events.swap(sink->pending);
|
|
122
133
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
134
|
+
Nan::HandleScope scope;
|
|
135
|
+
for (const auto event : events) {
|
|
136
|
+
{
|
|
137
|
+
std::lock_guard lock(notificationMutex);
|
|
138
|
+
if (notificationTarget.lock() != sink) break;
|
|
139
|
+
}
|
|
140
|
+
v8::Local<v8::Value> args[] = {Nan::New(event)};
|
|
141
|
+
Nan::TryCatch caught;
|
|
142
|
+
sink->resource->runInAsyncScope(Nan::GetCurrentContext()->Global(),
|
|
143
|
+
Nan::New(sink->callback),
|
|
144
|
+
1,
|
|
145
|
+
args);
|
|
146
|
+
if (caught.HasCaught()) {
|
|
147
|
+
Nan::FatalException(caught);
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
130
152
|
if (status != 0) return sfmljs::ThrowError(uv_strerror(status));
|
|
131
153
|
sink->async.data = new std::shared_ptr<NotificationSink>(sink);
|
|
132
154
|
uv_unref(reinterpret_cast<uv_handle_t*>(&sink->async));
|
|
133
|
-
node::AddEnvironmentCleanupHook(
|
|
134
|
-
|
|
155
|
+
node::AddEnvironmentCleanupHook(
|
|
156
|
+
info.GetIsolate(), NotificationCleanup, sink->async.data);
|
|
157
|
+
Nan::SetPrivate(global, key, SharedHandle<NotificationSink>::Create(sink))
|
|
158
|
+
.FromJust();
|
|
135
159
|
}
|
|
136
160
|
sink->callback.Reset(info[0].As<v8::Function>());
|
|
137
161
|
{
|
|
@@ -140,21 +164,24 @@ NAN_METHOD(AudioDeviceNotifications) {
|
|
|
140
164
|
notificationTarget = sink;
|
|
141
165
|
}
|
|
142
166
|
std::call_once(notificationInstalled, [] {
|
|
143
|
-
sf::PlaybackDevice::setNotificationCallback(
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
167
|
+
sf::PlaybackDevice::setNotificationCallback(
|
|
168
|
+
[](sf::PlaybackDevice::Notification event) {
|
|
169
|
+
std::lock_guard lock(notificationMutex);
|
|
170
|
+
if (auto target = notificationTarget.lock();
|
|
171
|
+
target && !target->closing) {
|
|
172
|
+
target->pending.push_back(static_cast<unsigned>(event));
|
|
173
|
+
uv_async_send(&target->async);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
150
176
|
});
|
|
151
177
|
}
|
|
152
178
|
|
|
153
|
-
}
|
|
179
|
+
} // namespace sfmljs::audio
|
|
154
180
|
|
|
155
181
|
NAN_MODULE_INIT(InitializeAudioDevices) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
sfmljs::SetMethod<AudioDevice>(target, "audioDevice");
|
|
159
|
-
sfmljs::SetMethod<AudioDeviceNotifications>(
|
|
182
|
+
sfmljs::SetMethod<sfmljs::audio::CreateAudioDeviceState>(
|
|
183
|
+
target, "createAudioDeviceState");
|
|
184
|
+
sfmljs::SetMethod<sfmljs::audio::AudioDevice>(target, "audioDevice");
|
|
185
|
+
sfmljs::SetMethod<sfmljs::audio::AudioDeviceNotifications>(
|
|
186
|
+
target, "audioDeviceNotifications");
|
|
160
187
|
}
|