sfml.js 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (313) hide show
  1. package/README.ja.md +10 -9
  2. package/README.ko.md +10 -9
  3. package/README.md +10 -9
  4. package/README.zh-CN.md +10 -9
  5. package/binding.gyp +23 -16
  6. package/dist/audio/effect-processor.js +9 -3
  7. package/dist/audio/input-sound-file.js +41 -15
  8. package/dist/audio/listener.js +33 -11
  9. package/dist/audio/music.js +41 -18
  10. package/dist/audio/output-sound-file.js +4 -2
  11. package/dist/audio/playback-device.js +11 -3
  12. package/dist/audio/sound-buffer-recorder.js +8 -5
  13. package/dist/audio/sound-buffer.js +40 -17
  14. package/dist/audio/sound-file-factory.js +21 -9
  15. package/dist/audio/sound-file-reader.js +21 -7
  16. package/dist/audio/sound-recorder.js +36 -15
  17. package/dist/audio/sound-source.js +117 -40
  18. package/dist/audio/sound-stream.js +14 -5
  19. package/dist/audio/sound.js +9 -4
  20. package/dist/audio/value.js +42 -16
  21. package/dist/graphics/blend-mode.js +2 -1
  22. package/dist/graphics/boolean-vector.d.ts +1 -2
  23. package/dist/graphics/boolean-vector.js +19 -9
  24. package/dist/graphics/color.js +2 -1
  25. package/dist/graphics/convex-shape.js +7 -3
  26. package/dist/graphics/font.js +52 -23
  27. package/dist/graphics/glsl-matrix.js +8 -3
  28. package/dist/graphics/glsl.js +47 -16
  29. package/dist/graphics/glyph.js +27 -10
  30. package/dist/graphics/image.js +30 -14
  31. package/dist/graphics/rect.js +15 -8
  32. package/dist/graphics/render-states.js +18 -9
  33. package/dist/graphics/render-target.js +49 -23
  34. package/dist/graphics/render-texture.js +24 -12
  35. package/dist/graphics/render-window.js +6 -3
  36. package/dist/graphics/shader.js +67 -32
  37. package/dist/graphics/shape.js +25 -13
  38. package/dist/graphics/sprite.js +19 -9
  39. package/dist/graphics/stencil-mode.js +10 -6
  40. package/dist/graphics/swap.js +2 -1
  41. package/dist/graphics/text.js +123 -48
  42. package/dist/graphics/texture.js +48 -24
  43. package/dist/graphics/transform.js +29 -13
  44. package/dist/graphics/transformable.js +2 -1
  45. package/dist/graphics/vertex-array.js +40 -18
  46. package/dist/graphics/vertex-buffer.js +39 -19
  47. package/dist/graphics/vertex.js +6 -3
  48. package/dist/graphics/view.js +30 -14
  49. package/dist/index.d.ts +4 -4
  50. package/dist/internal/audio-codec-registry.js +12 -6
  51. package/dist/internal/audio-input-stream.js +2 -1
  52. package/dist/internal/audio-music-pump.js +58 -24
  53. package/dist/internal/audio-recorder-session.js +17 -7
  54. package/dist/internal/audio-worker-session.js +28 -10
  55. package/dist/internal/audio-worker.js +40 -21
  56. package/dist/internal/glsl-value.d.ts +3 -1
  57. package/dist/internal/glsl-value.js +18 -8
  58. package/dist/internal/graphics-state.js +29 -15
  59. package/dist/internal/input-stream.d.ts +3 -1
  60. package/dist/internal/input-stream.js +16 -7
  61. package/dist/internal/numeric-properties.js +2 -1
  62. package/dist/internal/render-states.js +2 -1
  63. package/dist/internal/scalar.js +22 -13
  64. package/dist/internal/shaped-glyph.d.ts +3 -1
  65. package/dist/internal/shaped-glyph.js +12 -4
  66. package/dist/internal/struct-view.d.ts +3 -1
  67. package/dist/internal/struct-view.js +3 -1
  68. package/dist/internal/transformable.js +2 -1
  69. package/dist/network/common.js +23 -11
  70. package/dist/network/dns.js +25 -17
  71. package/dist/network/ftp.js +43 -18
  72. package/dist/network/http.js +126 -56
  73. package/dist/network/ip-address.js +50 -25
  74. package/dist/network/packet.js +63 -25
  75. package/dist/network/sftp.js +175 -74
  76. package/dist/network/socket-selector.js +28 -14
  77. package/dist/network/socket.js +71 -30
  78. package/dist/network/tcp-listener.js +42 -19
  79. package/dist/network/tcp-socket.js +111 -53
  80. package/dist/network/tls-material.js +2 -1
  81. package/dist/network/udp-socket.js +68 -33
  82. package/dist/system/angle.js +2 -1
  83. package/dist/system/clock.js +4 -2
  84. package/dist/system/err.js +5 -2
  85. package/dist/system/file-input-stream.js +8 -4
  86. package/dist/system/input-stream.js +6 -3
  87. package/dist/system/operators.d.ts +1 -1
  88. package/dist/system/operators.js +90 -50
  89. package/dist/system/sleep.js +4 -2
  90. package/dist/system/time.js +12 -6
  91. package/dist/system/vector2.js +6 -7
  92. package/dist/system/vector3.js +11 -6
  93. package/dist/window/clipboard.js +2 -1
  94. package/dist/window/context.js +10 -5
  95. package/dist/window/cursor.js +2 -1
  96. package/dist/window/event.js +9 -5
  97. package/dist/window/mouse.js +4 -2
  98. package/dist/window/video-mode.js +2 -1
  99. package/dist/window/window.js +19 -12
  100. package/native/addon.cc +25 -12
  101. package/native/audio-binding.cc +266 -126
  102. package/native/audio-codec-binding.cc +147 -77
  103. package/native/audio-device-binding.cc +76 -49
  104. package/native/audio-file-binding.cc +116 -67
  105. package/native/audio-recorder-binding.cc +147 -98
  106. package/native/audio-recorder.h +1 -1
  107. package/native/audio-state.h +53 -30
  108. package/native/audio-stream-binding.cc +148 -94
  109. package/native/audio-stream.h +5 -4
  110. package/native/audio-worker-binding.cc +163 -81
  111. package/native/audio-worker.h +35 -19
  112. package/native/batch-drawable.cc +29 -16
  113. package/native/batch-drawable.h +11 -8
  114. package/native/context-binding.cc +40 -27
  115. package/native/context-settings.h +15 -8
  116. package/native/deferred-delete.cc +9 -6
  117. package/native/deferred-delete.h +7 -8
  118. package/native/error-stream-binding.cc +42 -28
  119. package/native/font-binding.cc +89 -48
  120. package/native/graphics-binding.cc +122 -72
  121. package/native/graphics-state.h +50 -36
  122. package/native/image-binding.cc +38 -24
  123. package/native/image-state.h +26 -18
  124. package/native/input-binding.cc +42 -27
  125. package/native/render-window-binding.cc +38 -23
  126. package/native/shader-binding.cc +115 -50
  127. package/native/shape-binding.cc +94 -49
  128. package/native/shape-state.h +18 -9
  129. package/native/shaped-glyph-storage.h +9 -5
  130. package/native/stream-binding.cc +143 -117
  131. package/native/system-binding.cc +17 -12
  132. package/native/text-binding.cc +68 -35
  133. package/native/text-state.h +25 -18
  134. package/native/texture-binding.cc +42 -33
  135. package/native/texture-resource.h +20 -12
  136. package/native/time-value.h +3 -1
  137. package/native/transformable-binding.cc +12 -6
  138. package/native/transformable-state.h +6 -6
  139. package/native/value-binding.h +46 -28
  140. package/native/value-layout.h +10 -10
  141. package/native/values-binding.cc +29 -23
  142. package/native/vertex-binding.cc +18 -12
  143. package/native/vertex-buffer-binding.cc +38 -24
  144. package/native/vertex-storage.h +2 -2
  145. package/native/view-data.h +4 -3
  146. package/native/window-binding.cc +75 -45
  147. package/native/window-event.h +20 -11
  148. package/native/window-owner.h +15 -12
  149. package/package.json +40 -48
  150. package/src/audio/effect-processor.ts +9 -3
  151. package/src/audio/input-sound-file.ts +108 -36
  152. package/src/audio/listener.ts +39 -13
  153. package/src/audio/music.ts +112 -47
  154. package/src/audio/output-sound-file.ts +40 -11
  155. package/src/audio/playback-device.ts +23 -8
  156. package/src/audio/sound-buffer-recorder.ts +35 -13
  157. package/src/audio/sound-buffer.ts +166 -58
  158. package/src/audio/sound-file-factory.ts +127 -49
  159. package/src/audio/sound-file-reader.ts +49 -15
  160. package/src/audio/sound-recorder.ts +100 -34
  161. package/src/audio/sound-source.ts +142 -45
  162. package/src/audio/sound-stream.ts +15 -5
  163. package/src/audio/sound.ts +12 -4
  164. package/src/audio/value.ts +89 -26
  165. package/src/graphics/blend-mode.ts +3 -1
  166. package/src/graphics/boolean-vector.ts +56 -26
  167. package/src/graphics/color.ts +3 -1
  168. package/src/graphics/convex-shape.ts +10 -3
  169. package/src/graphics/font.ts +171 -50
  170. package/src/graphics/glsl-matrix.ts +9 -3
  171. package/src/graphics/glsl.ts +81 -35
  172. package/src/graphics/glyph.ts +67 -26
  173. package/src/graphics/image.ts +119 -45
  174. package/src/graphics/rect.ts +56 -24
  175. package/src/graphics/render-states.ts +31 -14
  176. package/src/graphics/render-target.ts +197 -81
  177. package/src/graphics/render-texture.ts +36 -12
  178. package/src/graphics/render-window.ts +9 -3
  179. package/src/graphics/shader.ts +186 -61
  180. package/src/graphics/shape.ts +93 -35
  181. package/src/graphics/sprite.ts +64 -25
  182. package/src/graphics/stencil-mode.ts +12 -6
  183. package/src/graphics/swap.ts +3 -2
  184. package/src/graphics/text.ts +229 -74
  185. package/src/graphics/texture.ts +180 -61
  186. package/src/graphics/transform.ts +132 -47
  187. package/src/graphics/transformable.ts +3 -1
  188. package/src/graphics/vertex-array.ts +87 -35
  189. package/src/graphics/vertex-buffer.ts +97 -33
  190. package/src/graphics/vertex.ts +9 -3
  191. package/src/graphics/view.ts +59 -21
  192. package/src/index.ts +101 -23
  193. package/src/internal/audio-codec-registry.ts +18 -6
  194. package/src/internal/audio-input-stream.ts +6 -2
  195. package/src/internal/audio-music-pump.ts +116 -41
  196. package/src/internal/audio-native.ts +249 -96
  197. package/src/internal/audio-recorder-session.ts +68 -24
  198. package/src/internal/audio-worker-session.ts +65 -21
  199. package/src/internal/audio-worker.ts +85 -41
  200. package/src/internal/glsl-value.ts +26 -11
  201. package/src/internal/graphics-state.ts +57 -20
  202. package/src/internal/input-stream.ts +23 -10
  203. package/src/internal/native.ts +252 -99
  204. package/src/internal/numeric-properties.ts +2 -1
  205. package/src/internal/render-states.ts +3 -1
  206. package/src/internal/scalar.ts +33 -14
  207. package/src/internal/shaped-glyph.ts +25 -12
  208. package/src/internal/struct-view.ts +40 -13
  209. package/src/internal/transformable.ts +3 -1
  210. package/src/network/common.ts +35 -11
  211. package/src/network/dns.ts +95 -38
  212. package/src/network/ftp.ts +129 -47
  213. package/src/network/http.ts +232 -77
  214. package/src/network/ip-address.ts +118 -48
  215. package/src/network/packet.ts +161 -54
  216. package/src/network/sftp.ts +498 -168
  217. package/src/network/socket-selector.ts +66 -26
  218. package/src/network/socket.ts +133 -46
  219. package/src/network/tcp-listener.ts +78 -29
  220. package/src/network/tcp-socket.ts +240 -95
  221. package/src/network/tls-material.ts +6 -2
  222. package/src/network/udp-socket.ts +147 -53
  223. package/src/system/angle.ts +3 -1
  224. package/src/system/clock.ts +12 -4
  225. package/src/system/err.ts +5 -2
  226. package/src/system/file-input-stream.ts +12 -4
  227. package/src/system/input-stream.ts +9 -3
  228. package/src/system/operators.ts +290 -115
  229. package/src/system/sleep.ts +6 -2
  230. package/src/system/time.ts +18 -6
  231. package/src/system/timeout-with-predicate.ts +19 -8
  232. package/src/system/vector2.ts +101 -49
  233. package/src/system/vector3.ts +85 -36
  234. package/src/window/clipboard.ts +3 -1
  235. package/src/window/context.ts +15 -5
  236. package/src/window/cursor.ts +3 -1
  237. package/src/window/event.ts +26 -12
  238. package/src/window/mouse.ts +6 -2
  239. package/src/window/video-mode.ts +3 -1
  240. package/src/window/window.ts +119 -37
  241. package/test/native/audio-probes.cc +74 -36
  242. package/test/native/deferred-delete-probes.cc +17 -11
  243. package/test/native/drawable-probes.cc +70 -36
  244. package/test/native/exception-probes.cc +14 -9
  245. package/test/native/image-probes.cc +20 -10
  246. package/test/native/matrix-probes.cc +8 -4
  247. package/test/native/network-oracle.cc +17 -11
  248. package/test/native/render-texture-probes.cc +33 -24
  249. package/test/native/shader-probes.cc +9 -9
  250. package/test/native/shape-probes.cc +37 -23
  251. package/test/native/sprite-probes.cc +41 -25
  252. package/test/native/system-probes.cc +58 -35
  253. package/test/native/text-probes.cc +77 -44
  254. package/test/native/texture-probes.cc +43 -26
  255. package/test/native/time-probes.cc +18 -12
  256. package/test/native/transform-probes.cc +15 -10
  257. package/test/native/transformable-probes.cc +23 -17
  258. package/test/native/value-oracle.h +9 -4
  259. package/test/native/value-probes.cc +31 -23
  260. package/test/native/vertex-array-probes.cc +22 -14
  261. package/test/native/vertex-buffer-probes.cc +28 -17
  262. package/test/native/view-probes.cc +29 -17
  263. package/test/native/window-probes.cc +66 -33
  264. package/tools/ensure-sfml.cjs +9 -0
  265. package/tools/generate-network-constants.cjs +11 -6
  266. package/tools/generate-window-constants.cjs +31 -12
  267. package/docs/README.ja.md +0 -30
  268. package/docs/README.ko.md +0 -30
  269. package/docs/README.md +0 -30
  270. package/docs/README.zh-CN.md +0 -30
  271. package/docs/api.ja.md +0 -84
  272. package/docs/api.ko.md +0 -84
  273. package/docs/api.md +0 -81
  274. package/docs/api.zh-CN.md +0 -81
  275. package/docs/audio.ja.md +0 -249
  276. package/docs/audio.ko.md +0 -251
  277. package/docs/audio.md +0 -252
  278. package/docs/audio.zh-CN.md +0 -252
  279. package/docs/concepts.ja.md +0 -130
  280. package/docs/concepts.ko.md +0 -130
  281. package/docs/concepts.md +0 -131
  282. package/docs/concepts.zh-CN.md +0 -131
  283. package/docs/development.ja.md +0 -144
  284. package/docs/development.ko.md +0 -144
  285. package/docs/development.md +0 -146
  286. package/docs/development.zh-CN.md +0 -146
  287. package/docs/getting-started.ja.md +0 -154
  288. package/docs/getting-started.ko.md +0 -154
  289. package/docs/getting-started.md +0 -156
  290. package/docs/getting-started.zh-CN.md +0 -156
  291. package/docs/graphics.ja.md +0 -256
  292. package/docs/graphics.ko.md +0 -254
  293. package/docs/graphics.md +0 -255
  294. package/docs/graphics.zh-CN.md +0 -257
  295. package/docs/network.ja.md +0 -214
  296. package/docs/network.ko.md +0 -216
  297. package/docs/network.md +0 -218
  298. package/docs/network.zh-CN.md +0 -218
  299. package/docs/system.ja.md +0 -169
  300. package/docs/system.ko.md +0 -171
  301. package/docs/system.md +0 -173
  302. package/docs/system.zh-CN.md +0 -173
  303. package/docs/window.ja.md +0 -138
  304. package/docs/window.ko.md +0 -140
  305. package/docs/window.md +0 -141
  306. package/docs/window.zh-CN.md +0 -141
  307. package/native/exception-boundary.h +0 -97
  308. package/native/numeric-fields.h +0 -96
  309. package/native/shared-handle.h +0 -107
  310. package/test/tsconfig.json +0 -8
  311. package/tools/configure-lsp.cjs +0 -23
  312. package/tools/lib_downloader.cjs +0 -92
  313. package/tools/lib_downloader_urls.cjs +0 -23
@@ -24,18 +24,28 @@ struct Input {
24
24
  };
25
25
  using InputHandle = SharedHandle<Input>;
26
26
 
27
- void QueueInput(v8::Local<v8::Value> callback, const std::shared_ptr<InputFile>& file,
28
- Job::Action work, Job::Result result) {
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(callback, std::move(work), std::move(result),
31
- [stream = file->stream] { stream->Cancel(); });
32
- } else Queue(callback, std::move(work), std::move(result));
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, "buffer", MapShared(v8::Isolate::GetCurrent(), file,
38
- &file->metadata, sizeof(file->metadata)));
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)) return;
70
+ } else if (!ReadBytes(info[1], next->bytes)) {
71
+ return;
72
+ }
61
73
  auto ok = std::make_shared<bool>();
62
- QueueInput(info[2], next, [next, path, ok] {
63
- *ok = next->stream ? next->file.openFromStream(*next->stream)
64
- : next->bytes.backing
65
- ? next->file.openFromMemory(next->bytes.data, next->bytes.size)
66
- : next->file.openFromFile(Path(path));
67
- }, [input, next, ok]() -> v8::Local<v8::Value> {
68
- if (!*ok) return Nan::Undefined();
69
- next->open = true;
70
- next->Update();
71
- next->channelMap = next->file.getChannelMap();
72
- input->current = next;
73
- return MapInput(next);
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(info[2], file, [file, bytes, count] {
84
- *count = file->open
85
- ? file->file.read(static_cast<std::int16_t*>(bytes.data), bytes.size / 2)
86
- : 0;
87
- }, [file, count]() -> v8::Local<v8::Value> {
88
- if (file->open) file->Update();
89
- file->metadata.transferred = *count;
90
- return Nan::Undefined();
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
- ? static_cast<std::uint64_t>(info[1].As<v8::BigInt>()->Int64Value(&lossless))
105
- : info[1].As<v8::BigInt>()->Uint64Value(&lossless);
106
- if (!lossless) return Nan::ThrowRangeError("Offset is outside its scalar range");
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(info[3], file, [file, time, offset, signedOffset] {
109
- if (!file->open) return;
110
- if (time) file->file.seek(sf::microseconds(signedOffset));
111
- else file->file.seek(offset);
112
- }, [file]() -> v8::Local<v8::Value> {
113
- if (file->open) file->Update();
114
- return Nan::Undefined();
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(info[1], old, [old] {
125
- old->file.close();
126
- if (old->stream) old->stream->Cancel();
127
- old->stream.reset();
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)) return;
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(info[5], [output, path, rate, count, channels, ok] {
155
- *ok = output->file.openFromFile(Path(path), rate, count, channels);
156
- output->open = *ok;
157
- }, [ok]() -> v8::Local<v8::Value> { return Nan::New(*ok); });
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(info[2], [output, bytes] {
166
- if (!output->open) throw std::runtime_error("Audio output file is not open");
167
- output->file.write(static_cast<std::int16_t*>(bytes.data), bytes.size / 2);
168
- }, []() -> v8::Local<v8::Value> { return Nan::Undefined(); });
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(info[1], [output] { output->file.close(); output->open = false; },
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
- } // namespace sfmljs::audio
225
+ } // namespace sfmljs::audio
180
226
 
181
227
  NAN_MODULE_INIT(InitializeAudioFiles) {
182
- using namespace sfmljs::audio;
183
- sfmljs::SetMethod<CreateAudioInput>(target, "createAudioInput");
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, "createAudioOutput");
189
- sfmljs::SetMethod<OpenAudioOutput>(target, "openAudioOutput");
190
- sfmljs::SetMethod<WriteAudioOutput>(target, "writeAudioOutput");
191
- sfmljs::SetMethod<CloseAudioOutput>(target, "closeAudioOutput");
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 instead.
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, std::size_t count) override {
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()) return Nan::ThrowTypeError("Expected a recorder kind");
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, "values", MapShared(info.GetIsolate(), recorder,
28
- recorder->values.data(), sizeof(recorder->values)));
29
- Set(result, "channelMap", MapChannels(recorder->channels, *recorder->channels));
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)) return;
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 = direct ? nullptr
44
- : CreateWorkerState(1, frames, recorder->values[1], rate, slots);
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(info[4], [recorder, state, next, direct] {
48
- if (direct) *next = std::make_shared<sf::SoundBufferRecorder>();
49
- else *next = std::make_shared<WorkerRecorder>(state);
50
- (*next)->setChannelCount(recorder->values[1]);
51
- if ((*next)->getDevice() != recorder->device &&
52
- !(*next)->setDevice(recorder->device)) {
53
- throw std::runtime_error("Failed to select capture device");
54
- }
55
- }, [recorder, state, next, rate, callbacks]() -> v8::Local<v8::Value> {
56
- recorder->current = std::move(*next);
57
- recorder->worker = state;
58
- recorder->callbackRecorder = callbacks;
59
- recorder->values[0] = rate;
60
- auto result = Nan::New<v8::Object>();
61
- if (state) {
62
- Set(result, "handle", AudioWorkerHandle::Create(state));
63
- Set(result, "token", RegisterWorker(state));
64
- }
65
- return result;
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(info[1], [recorder, ok] {
76
- *ok = recorder->current->start(recorder->values[0]);
77
- }, [recorder, ok]() -> v8::Local<v8::Value> {
78
- recorder->active = *ok;
79
- recorder->values[2] = *ok;
80
- return Nan::New(*ok);
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(info[1], [recorder] {
89
- if (recorder->current) recorder->current->stop();
90
- }, [recorder]() -> v8::Local<v8::Value> {
91
- const bool wasActive = recorder->active;
92
- recorder->active = false;
93
- recorder->values[2] = 0;
94
- if (recorder->worker) {
95
- recorder->worker->running.store(false, std::memory_order_release);
96
- }
97
- if (!recorder->bufferRecorder || recorder->callbackRecorder || !wasActive ||
98
- !recorder->current) return Nan::Undefined();
99
- auto& buffer = static_cast<sf::SoundBufferRecorder&>(*recorder->current).getBuffer();
100
- auto data = MakeThreadOwned<BufferData>(recorder->current,
101
- const_cast<sf::SoundBuffer&>(buffer));
102
- return BufferDataHandle::Create(data);
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
- *recorder->channels);
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) return Nan::ThrowError("Recorder buffer is 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("Recorder samples must contain complete frames");
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(buffer->pcm.data(), buffer->pcm.size(),
142
- static_cast<unsigned>(buffer->channels.size()), buffer->rate,
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 return Nan::ThrowRangeError("Unknown recorder buffer command");
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(info[2], [recorder, name, ok] {
168
- if (recorder->current) {
169
- if (recorder->active) recorder->current->stop();
170
- *ok = recorder->current->setDevice(name);
171
- if (*ok && recorder->active) {
172
- *ok = recorder->current->start(recorder->values[0]);
173
- }
174
- }
175
- else {
176
- const auto devices = sf::SoundRecorder::getAvailableDevices();
177
- *ok = std::find(devices.begin(), devices.end(), name) != devices.end();
178
- }
179
- }, [recorder, name, ok]() -> v8::Local<v8::Value> {
180
- if (*ok) recorder->device = name;
181
- else {
182
- recorder->active = false;
183
- recorder->values[2] = 0;
184
- if (recorder->worker) {
185
- recorder->worker->running.store(false, std::memory_order_release);
186
- }
187
- }
188
- return Nan::New(*ok);
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) return Nan::ThrowRangeError("Capture supports mono or stereo");
198
- if (recorder->active) return Nan::ThrowError("Stop capture before changing channels");
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) *channels = {sf::SoundChannel::Mono};
202
- else *channels = {sf::SoundChannel::FrontLeft, sf::SoundChannel::FrontRight};
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(Nan::New(sf::SoundRecorder::getDefaultDevice()).ToLocalChecked());
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 return Nan::ThrowRangeError("Unknown capture device command");
260
+ } else {
261
+ return Nan::ThrowRangeError("Unknown capture device command");
262
+ }
222
263
  }
223
264
 
224
- } // namespace sfmljs::audio
265
+ } // namespace sfmljs::audio
225
266
 
226
267
  NAN_MODULE_INIT(InitializeAudioRecorders) {
227
- using namespace sfmljs::audio;
228
- sfmljs::SetMethod<CreateAudioRecorder>(target, "createAudioRecorder");
229
- sfmljs::SetMethod<PrepareAudioRecorder>(target, "prepareAudioRecorder");
230
- sfmljs::SetMethod<StartAudioRecorder>(target, "startAudioRecorder");
231
- sfmljs::SetMethod<StopAudioRecorder>(target, "stopAudioRecorder");
232
- sfmljs::SetMethod<CreateAudioRecorderBuffer>(target, "createAudioRecorderBuffer");
233
- sfmljs::SetMethod<AudioRecorderBuffer>(target, "audioRecorderBuffer");
234
- sfmljs::SetMethod<AudioRecorderDevice>(target, "audioRecorderDevice");
235
- sfmljs::SetMethod<AudioRecorderChannels>(target, "audioRecorderChannels");
236
- sfmljs::SetMethod<AudioCaptureDevices>(target, "audioCaptureDevices");
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
  }
@@ -36,4 +36,4 @@ struct RecorderBuffer {
36
36
  };
37
37
  using RecorderBufferHandle = SharedHandle<RecorderBuffer>;
38
38
 
39
- } // namespace sfmljs::audio
39
+ } // namespace sfmljs::audio