@kmamal/sdl 1.0.0-5 → 1.0.0-8

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 (84) hide show
  1. package/CHANGELOG.md +12 -6
  2. package/binding.gyp +3 -0
  3. package/package.json +5 -12
  4. package/scripts/util/common.js +2 -2
  5. package/src/javascript/audio/audio-playback-stream.js +9 -10
  6. package/src/javascript/audio/audio-recording-stream.js +9 -8
  7. package/src/javascript/audio/audio-stream.js +100 -44
  8. package/src/javascript/audio/conversion.js +41 -0
  9. package/src/javascript/audio/devices.js +11 -0
  10. package/src/javascript/audio/events.js +65 -0
  11. package/src/javascript/audio/format-helpers.js +8 -4
  12. package/src/javascript/audio/index.js +25 -20
  13. package/src/javascript/audio/prevent-exit.js +12 -9
  14. package/src/javascript/audio/validate.js +9 -0
  15. package/src/javascript/cleanup.js +9 -6
  16. package/src/javascript/clipboard/index.js +8 -3
  17. package/src/javascript/device-instance.js +61 -0
  18. package/src/javascript/events/events-via-poll.js +33 -27
  19. package/src/javascript/events/index.js +75 -482
  20. package/src/javascript/events/try.js +22 -0
  21. package/src/javascript/gamepad/{device.js → devices.js} +4 -5
  22. package/src/javascript/gamepad/events.js +66 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +12 -160
  24. package/src/javascript/gamepad/index.js +15 -16
  25. package/src/javascript/globals.js +10 -1
  26. package/src/javascript/index.js +2 -5
  27. package/src/javascript/joystick/devices.js +43 -0
  28. package/src/javascript/joystick/events.js +129 -0
  29. package/src/javascript/joystick/index.js +9 -18
  30. package/src/javascript/joystick/joystick-device-instance.js +175 -0
  31. package/src/javascript/joystick/joystick-instance.js +12 -158
  32. package/src/javascript/keyboard/index.js +20 -10
  33. package/src/javascript/keyboard/key-mapping.js +46 -27
  34. package/src/javascript/mouse/index.js +36 -29
  35. package/src/javascript/sensor/events.js +21 -0
  36. package/src/javascript/sensor/index.js +13 -2
  37. package/src/javascript/sensor/sensor-instance.js +7 -47
  38. package/src/javascript/subsystems.js +56 -0
  39. package/src/javascript/touch/index.js +9 -4
  40. package/src/javascript/validate.js +114 -0
  41. package/src/javascript/video/conversion.js +63 -0
  42. package/src/javascript/video/events.js +179 -0
  43. package/src/javascript/video/format-helpers.js +10 -8
  44. package/src/javascript/video/index.js +28 -14
  45. package/src/javascript/video/validate.js +45 -0
  46. package/src/javascript/video/window.js +144 -161
  47. package/src/native/audio.cpp +457 -151
  48. package/src/native/audio.h +12 -6
  49. package/src/native/clipboard.cpp +8 -37
  50. package/src/native/enums.cpp +2 -0
  51. package/src/native/events.cpp +416 -354
  52. package/src/native/events.h +67 -65
  53. package/src/native/gamepad.cpp +86 -103
  54. package/src/native/gamepad.h +7 -8
  55. package/src/native/global.cpp +190 -85
  56. package/src/native/global.h +48 -2
  57. package/src/native/joystick.cpp +180 -274
  58. package/src/native/joystick.h +12 -6
  59. package/src/native/keyboard.cpp +60 -22
  60. package/src/native/keyboard.h +2 -1
  61. package/src/native/module.cpp +20 -3
  62. package/src/native/mouse.cpp +27 -58
  63. package/src/native/packed.cpp +106 -0
  64. package/src/native/packed.h +91 -0
  65. package/src/native/power.cpp +7 -1
  66. package/src/native/power.h +2 -2
  67. package/src/native/sensor.cpp +61 -69
  68. package/src/native/sensor.h +3 -3
  69. package/src/native/testing.cpp +131 -0
  70. package/src/native/testing.h +16 -0
  71. package/src/native/threads.cpp +300 -0
  72. package/src/native/threads.h +36 -0
  73. package/src/native/touch.cpp +32 -43
  74. package/src/native/touch.h +2 -1
  75. package/src/native/video.cpp +145 -95
  76. package/src/native/video.h +6 -3
  77. package/src/native/window.cpp +155 -218
  78. package/src/native/window.h +21 -20
  79. package/src/types/index.d.ts +141 -69
  80. package/src/javascript/audio/device.js +0 -37
  81. package/src/javascript/joystick/device.js +0 -13
  82. package/src/javascript/joystick/shared.js +0 -90
  83. package/src/javascript/worker.js +0 -88
  84. package/src/types/worker.d.ts +0 -71
@@ -2,21 +2,132 @@
2
2
  #include "global.h"
3
3
  #include <SDL3/SDL.h>
4
4
  #include <string>
5
- #include <sstream>
6
5
  #include <mutex>
6
+ #include <atomic>
7
+ #include <cstdint>
7
8
 
8
- std::map<bool, std::string> audio::device_types;
9
- std::map<SDL_AudioFormat, std::string> audio::formats;
9
+ std::map<bool, packed::Str> audio::device_types;
10
+ std::map<SDL_AudioFormat, packed::Str> audio::formats;
11
+
12
+ struct StreamState;
13
+
14
+ static void _notifyJs (Napi::Env env, Napi::Function callback, StreamState *state, std::nullptr_t *);
15
+
16
+ using NotifyFunction = Napi::TypedThreadSafeFunction<StreamState, std::nullptr_t, _notifyJs>;
17
+
18
+ struct StreamState {
19
+ std::atomic<bool> closed { false };
20
+ std::atomic<bool> notify { false };
21
+ std::atomic<bool> pending { false };
22
+ std::atomic<uint32_t> underruns { 0 };
23
+ std::atomic<uint32_t> missing { 0 };
24
+ std::atomic<uint32_t> available { 0 };
25
+ NotifyFunction tsfn;
26
+ };
10
27
 
11
28
  struct OpenStream {
12
29
  SDL_AudioStream *stream;
13
30
  napi_env env;
31
+ SDL_AudioDeviceID device_id;
32
+ bool is_recording;
33
+ StreamState *state;
14
34
  };
15
35
 
16
36
  static std::map<SDL_AudioDeviceID, OpenStream> open_streams;
17
37
 
18
- static SDL_AudioStream *
19
- _getStream (Napi::Env &env, SDL_AudioDeviceID audio_id)
38
+ static void
39
+ _notifyJs (Napi::Env env, Napi::Function callback, StreamState *state, std::nullptr_t *)
40
+ {
41
+ state->pending.exchange(false, std::memory_order_acq_rel);
42
+ if (env == nullptr || callback.IsEmpty()) { return; }
43
+ if (state->closed.load(std::memory_order_acquire)) { return; }
44
+ if (!state->notify.load(std::memory_order_acquire)) { return; }
45
+ uint32_t underruns = state->underruns.exchange(0, std::memory_order_relaxed);
46
+ uint32_t missing = state->missing.exchange(0, std::memory_order_relaxed);
47
+ uint32_t available = state->available.exchange(0, std::memory_order_relaxed);
48
+ callback.Call(env.Global(), {
49
+ Napi::Number::New(env, underruns),
50
+ Napi::Number::New(env, missing),
51
+ Napi::Number::New(env, available),
52
+ });
53
+ }
54
+
55
+ static void
56
+ _wake (StreamState *state)
57
+ {
58
+ if (!state->notify.load(std::memory_order_acquire)) { return; }
59
+ if (state->pending.exchange(true, std::memory_order_acq_rel)) { return; }
60
+ if (state->tsfn.NonBlockingCall() != napi_ok) {
61
+ state->pending.store(false, std::memory_order_release);
62
+ }
63
+ }
64
+
65
+ // _onGet and _onPut run on SDL's audio thread and must not take global::lock(): stream teardown holds it while SDL waits for this thread.
66
+ static void SDLCALL
67
+ _onGet (void *userdata, SDL_AudioStream *, int additional_amount, int)
68
+ {
69
+ StreamState *state = (StreamState *) userdata;
70
+
71
+ if (!state->notify.load(std::memory_order_acquire)) { return; }
72
+
73
+ if (additional_amount > 0) {
74
+ state->underruns.fetch_add(1, std::memory_order_relaxed);
75
+ state->missing.fetch_add((uint32_t) additional_amount, std::memory_order_relaxed);
76
+ }
77
+
78
+ _wake(state);
79
+ }
80
+
81
+ static void SDLCALL
82
+ _onPut (void *userdata, SDL_AudioStream *stream, int, int)
83
+ {
84
+ StreamState *state = (StreamState *) userdata;
85
+
86
+ if (!state->notify.load(std::memory_order_acquire)) { return; }
87
+
88
+ int available = SDL_GetAudioStreamAvailable(stream);
89
+ if (available < 0) { SDL_ClearError(); }
90
+ else { state->available.store((uint32_t) available, std::memory_order_relaxed); }
91
+
92
+ _wake(state);
93
+ }
94
+
95
+ static void
96
+ _unbind (OpenStream &entry)
97
+ {
98
+ if (entry.is_recording) { SDL_SetAudioStreamPutCallback(entry.stream, nullptr, nullptr); }
99
+ else { SDL_SetAudioStreamGetCallback(entry.stream, nullptr, nullptr); }
100
+ }
101
+
102
+ static void _destroy (SDL_AudioDeviceID audio_id, OpenStream &entry, bool same_env);
103
+
104
+ static void
105
+ _envCleanup (void *arg)
106
+ {
107
+ SDL_AudioDeviceID audio_id = (SDL_AudioDeviceID) (uintptr_t) arg;
108
+
109
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
110
+
111
+ auto it = open_streams.find(audio_id);
112
+ if (it == open_streams.end()) { return; }
113
+
114
+ _destroy(audio_id, it->second, false);
115
+ open_streams.erase(it);
116
+ }
117
+
118
+ static void
119
+ _destroy (SDL_AudioDeviceID audio_id, OpenStream &entry, bool same_env)
120
+ {
121
+ // The SDL callback is cleared before anything else.
122
+ _unbind(entry);
123
+ entry.state->closed.store(true, std::memory_order_release);
124
+ SDL_DestroyAudioStream(entry.stream);
125
+ if (same_env) { napi_remove_env_cleanup_hook(entry.env, _envCleanup, (void *) (uintptr_t) audio_id); }
126
+ entry.state->tsfn.Release();
127
+ }
128
+
129
+ static OpenStream &
130
+ _getEntry (Napi::Env &env, SDL_AudioDeviceID audio_id)
20
131
  {
21
132
  auto entry = open_streams.find(audio_id);
22
133
  if (entry == open_streams.end()) {
@@ -24,102 +135,161 @@ _getStream (Napi::Env &env, SDL_AudioDeviceID audio_id)
24
135
  message << "getAudioStream(" << audio_id << ") error: invalid audio stream id";
25
136
  throw Napi::Error::New(env, message.str());
26
137
  }
27
- return entry->second.stream;
138
+ return entry->second;
28
139
  }
29
140
 
30
- void
31
- audio::closeEnvStreams (Napi::Env env)
32
- {
33
- std::lock_guard<std::mutex> guard(global::lock());
34
-
35
- for (auto it = open_streams.begin(); it != open_streams.end();) {
36
- if (it->second.env == (napi_env) env) {
37
- SDL_DestroyAudioStream(it->second.stream);
38
- it = open_streams.erase(it);
39
- }
40
- else {
41
- it++;
42
- }
43
- }
141
+ static SDL_AudioStream *
142
+ _getStream (Napi::Env &env, SDL_AudioDeviceID audio_id)
143
+ {
144
+ return _getEntry(env, audio_id).stream;
44
145
  }
45
146
 
46
147
  void
47
148
  audio::closeAllStreams ()
48
149
  {
150
+ // Runs on the main thread and may touch other threads' streams, so it doesn't remove their cleanup hooks; those hooks find the stream gone and do nothing.
49
151
  for (auto &entry : open_streams) {
50
- SDL_DestroyAudioStream(entry.second.stream);
152
+ _destroy(entry.first, entry.second, false);
51
153
  }
52
154
  open_streams.clear();
53
155
  }
54
156
 
55
157
 
56
- static Napi::Value
57
- _packDevice (Napi::Env &env, SDL_AudioDeviceID id)
158
+ static void
159
+ _setFormat (packed::Value &target, SDL_AudioDeviceID id)
160
+ {
161
+ SDL_AudioSpec spec;
162
+ int sample_frames;
163
+ if (!SDL_GetAudioDeviceFormat(id, &spec, &sample_frames)) {
164
+ SDL_ClearError();
165
+ target.set("format", packed::Value());
166
+ target.set("channels", packed::Value());
167
+ target.set("frequency", packed::Value());
168
+ target.set("buffered", packed::Value());
169
+ return;
170
+ }
171
+
172
+ auto format_entry = audio::formats.find(spec.format);
173
+ packed::Value format;
174
+ if (format_entry != audio::formats.end()) { format = packed::Value::ref(format_entry->second); }
175
+
176
+ target.set("format", std::move(format));
177
+ target.set("channels", spec.channels);
178
+ target.set("frequency", spec.freq);
179
+ target.set("buffered", sample_frames);
180
+ }
181
+
182
+ static void
183
+ _setFormat (Napi::Object &target, SDL_AudioDeviceID id)
184
+ {
185
+ packed::Value format = packed::Value::object();
186
+ _setFormat(format, id);
187
+
188
+ Napi::Env env = target.Env();
189
+ for (const packed::Member &member : format.members()) {
190
+ target.Set(member.key, member.value.toNapi(env));
191
+ }
192
+ }
193
+
194
+ static SDL_AudioDeviceID
195
+ _defaultId (bool is_recording)
196
+ {
197
+ return is_recording ? SDL_AUDIO_DEVICE_DEFAULT_RECORDING : SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK;
198
+ }
199
+
200
+ packed::Value
201
+ audio::_getDevice (SDL_AudioDeviceID id)
58
202
  {
59
203
  const char *name = SDL_GetAudioDeviceName(id);
60
204
  if (name == nullptr) {
61
205
  SDL_ClearError();
62
- return env.Null();
206
+ return packed::Value();
63
207
  }
64
208
 
65
- Napi::Object device = Napi::Object::New(env);
66
- device.Set("id", id);
67
- device.Set("name", name);
209
+ packed::Value device = packed::Value::object();
210
+ device.set("id", id);
211
+ device.set("name", name);
212
+ _setFormat(device, id);
68
213
 
69
214
  return device;
70
215
  }
71
216
 
72
- Napi::Value
73
- audio::_getDevice(Napi::Env &env, SDL_AudioDeviceID id)
217
+ packed::Value
218
+ audio::_getFormat(SDL_AudioDeviceID id)
219
+ {
220
+ packed::Value result = packed::Value::object();
221
+ _setFormat(result, id);
222
+ return result;
223
+ }
224
+
225
+ packed::Value
226
+ audio::_getDefaultDevice(bool is_recording)
74
227
  {
75
- return _packDevice(env, id);
228
+ packed::Value device = packed::Value::object();
229
+ device.set("id", packed::Value());
230
+ device.set("name", packed::Value());
231
+ _setFormat(device, _defaultId(is_recording));
232
+ return device;
76
233
  }
77
234
 
78
- Napi::Array
79
- audio::_getDevices(Napi::Env &env, bool is_recording)
235
+ static Napi::Array
236
+ _getDevices (Napi::Env &env, bool is_recording)
80
237
  {
81
238
  int num_devices;
82
- SDL_AudioDeviceID *device_ids = is_recording
239
+ global::SdlArray<SDL_AudioDeviceID> device_ids(is_recording
83
240
  ? SDL_GetAudioRecordingDevices(&num_devices)
84
- : SDL_GetAudioPlaybackDevices(&num_devices);
85
- if (device_ids == nullptr) {
86
- std::ostringstream message;
87
- message << "SDL_GetAudio" << (is_recording ? "Recording" : "Playback") << "Devices() error: " << SDL_GetError();
88
- SDL_ClearError();
89
- throw Napi::Error::New(env, message.str());
241
+ : SDL_GetAudioPlaybackDevices(&num_devices));
242
+ if (!device_ids) {
243
+ throw global::sdlError(env, is_recording ? "SDL_GetAudioRecordingDevices" : "SDL_GetAudioPlaybackDevices");
90
244
  }
91
245
 
92
246
  Napi::Array devices = Napi::Array::New(env);
93
247
 
94
- try {
95
- int num_returned = 0;
96
- for (int i = 0; i < num_devices; i++) {
97
- Napi::Value device = _packDevice(env, device_ids[i]);
98
- if (device.IsNull()) { continue; }
99
- devices.Set(num_returned++, device);
100
- }
248
+ devices.Set((uint32_t) 0, audio::_getDefaultDevice(is_recording).toNapi(env));
249
+ int num_returned = 1;
250
+ for (int i = 0; i < num_devices; i++) {
251
+ packed::Value device = audio::_getDevice(device_ids[i]);
252
+ if (device.isNull()) { continue; }
253
+ devices.Set(num_returned++, device.toNapi(env));
101
254
  }
102
- catch (...) {
103
- SDL_free(device_ids);
104
- throw;
105
- }
106
- SDL_free(device_ids);
107
255
 
108
256
  return devices;
109
257
  }
110
258
 
259
+ Napi::Value
260
+ audio::getDrivers(const Napi::CallbackInfo &info)
261
+ {
262
+ Napi::Env env = info.Env();
263
+
264
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
265
+ global::assertReady(env);
266
+
267
+ Napi::Array all = Napi::Array::New(env);
268
+ int num_drivers = SDL_GetNumAudioDrivers();
269
+ for (int i = 0; i < num_drivers; i ++) {
270
+ all.Set(i, SDL_GetAudioDriver(i));
271
+ }
272
+
273
+ Napi::Object result = Napi::Object::New(env);
274
+ result.Set("all", all);
275
+ // Only NULL before the audio subsystem is initialized, which the JS
276
+ // assertInitialized() guard rules out, so this can never be NULL here.
277
+ result.Set("current", Napi::String::New(env, SDL_GetCurrentAudioDriver()));
278
+
279
+ return result;
280
+ }
111
281
 
112
282
  Napi::Value
113
283
  audio::getDevices(const Napi::CallbackInfo &info)
114
284
  {
115
285
  Napi::Env env = info.Env();
116
286
 
117
- std::lock_guard<std::mutex> guard(global::lock());
287
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
118
288
  global::assertReady(env);
119
289
 
120
290
  bool is_recording = info[0].As<Napi::Boolean>().Value();
121
291
 
122
- return audio::_getDevices(env, is_recording);
292
+ return _getDevices(env, is_recording);
123
293
  }
124
294
 
125
295
  Napi::Value
@@ -127,7 +297,7 @@ audio::open (const Napi::CallbackInfo &info)
127
297
  {
128
298
  Napi::Env env = info.Env();
129
299
 
130
- std::lock_guard<std::mutex> guard(global::lock());
300
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
131
301
  global::assertReady(env);
132
302
 
133
303
  bool has_id = !info[0].IsNull();
@@ -136,12 +306,12 @@ audio::open (const Napi::CallbackInfo &info)
136
306
  int format = info[3].As<Napi::Number>().Int32Value();
137
307
  int channels = info[4].As<Napi::Number>().Int32Value();
138
308
  int samples = info[5].As<Napi::Number>().Int32Value();
309
+ Napi::Function callback = info[6].As<Napi::Function>();
310
+ Napi::Object device = info[7].As<Napi::Object>();
139
311
 
140
312
  SDL_AudioDeviceID device_id = has_id
141
313
  ? info[0].As<Napi::Number>().Uint32Value()
142
- : is_recording
143
- ? SDL_AUDIO_DEVICE_DEFAULT_RECORDING
144
- : SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK;
314
+ : _defaultId(is_recording);
145
315
 
146
316
  SDL_AudioSpec desired;
147
317
  SDL_memset(&desired, 0, sizeof(desired));
@@ -150,62 +320,63 @@ audio::open (const Napi::CallbackInfo &info)
150
320
  desired.channels = channels;
151
321
 
152
322
  std::string samples_string = std::to_string(samples);
153
- SDL_SetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, samples_string.c_str());
323
+ if (!SDL_SetHintWithPriority(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, samples_string.c_str(), SDL_HINT_OVERRIDE)) {
324
+ throw global::sdlError(env, "SDL_SetHintWithPriority");
325
+ }
154
326
 
155
327
  SDL_AudioStream *stream = SDL_OpenAudioDeviceStream(device_id, &desired, nullptr, nullptr);
156
328
  SDL_ResetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES);
157
- if (stream == nullptr) {
158
- std::ostringstream message;
159
- message << "SDL_OpenAudioDeviceStream() error: " << SDL_GetError();
160
- SDL_ClearError();
161
- throw Napi::Error::New(env, message.str());
162
- }
329
+ if (stream == nullptr) { throw global::sdlError(env, "SDL_OpenAudioDeviceStream"); }
163
330
 
164
331
  SDL_AudioDeviceID audio_id = SDL_GetAudioStreamDevice(stream);
165
332
  if (audio_id == 0) {
166
- std::ostringstream message;
167
- message << "SDL_GetAudioStreamDevice() error: " << SDL_GetError();
168
- SDL_ClearError();
333
+ Napi::Error error = global::sdlError(env, "SDL_GetAudioStreamDevice");
169
334
  SDL_DestroyAudioStream(stream);
170
- throw Napi::Error::New(env, message.str());
335
+ throw error;
171
336
  }
172
337
 
173
- open_streams[audio_id] = { stream, env };
174
-
175
- return Napi::Number::New(env, audio_id);
176
- }
177
-
178
- Napi::Value
179
- audio::getDeviceFormat (const Napi::CallbackInfo &info)
180
- {
181
- Napi::Env env = info.Env();
182
-
183
- std::lock_guard<std::mutex> guard(global::lock());
184
- global::assertReady(env);
185
-
186
- int audio_id = info[0].As<Napi::Number>().Int32Value();
187
-
188
- SDL_AudioSpec spec;
189
- int sample_frames;
190
- if (!SDL_GetAudioDeviceFormat(audio_id, &spec, &sample_frames)) {
191
- std::ostringstream message;
192
- message << "SDL_GetAudioDeviceFormat(" << audio_id << ") error: " << SDL_GetError();
193
- SDL_ClearError();
194
- throw Napi::Error::New(env, message.str());
338
+ StreamState *state = new StreamState();
339
+ try {
340
+ // state is freed only by this finalizer, never directly, because wake-ups can still be queued after close.
341
+ state->tsfn = NotifyFunction::New(env, callback, "audioStreamNotify", 0, 1, state,
342
+ [](Napi::Env, void *, StreamState *state) { delete state; });
343
+ }
344
+ catch (...) {
345
+ delete state;
346
+ SDL_DestroyAudioStream(stream);
347
+ throw;
348
+ }
349
+ napi_status status = napi_unref_threadsafe_function(env, state->tsfn);
350
+ // Registered right after the thread-safe function so that on worker termination it runs before Node finalizes it (cleanup hooks run last-registered-first).
351
+ // Keying the hook by audio_id is safe: SDL never reuses audio device IDs (its instance counter only grows and isn't reset by SDL_Quit).
352
+ if (status == napi_ok) { status = napi_add_env_cleanup_hook(env, _envCleanup, (void *) (uintptr_t) audio_id); }
353
+ if (status != napi_ok) {
354
+ state->tsfn.Release();
355
+ SDL_DestroyAudioStream(stream);
356
+ NAPI_THROW_IF_FAILED(env, status, Napi::Value());
357
+ }
358
+ OpenStream &entry = open_streams[audio_id] = { stream, env, device_id, is_recording, state };
359
+
360
+ bool bound = is_recording
361
+ ? SDL_SetAudioStreamPutCallback(stream, _onPut, state)
362
+ : SDL_SetAudioStreamGetCallback(stream, _onGet, state);
363
+ if (!bound) {
364
+ Napi::Error error = global::sdlError(env, is_recording ? "SDL_SetAudioStreamPutCallback" : "SDL_SetAudioStreamGetCallback", audio_id);
365
+ _destroy(audio_id, entry, true);
366
+ open_streams.erase(audio_id);
367
+ throw error;
195
368
  }
196
369
 
197
- auto format_entry = audio::formats.find(spec.format);
198
- Napi::Value format = format_entry != audio::formats.end()
199
- ? Napi::String::New(env, format_entry->second)
200
- : env.Null();
201
-
202
- Napi::Object result = Napi::Object::New(env);
203
- result.Set("format", format);
204
- result.Set("channels", spec.channels);
205
- result.Set("frequency", spec.freq);
206
- result.Set("buffered", sample_frames);
370
+ try {
371
+ _setFormat(device, audio_id);
372
+ }
373
+ catch (...) {
374
+ _destroy(audio_id, entry, true);
375
+ open_streams.erase(audio_id);
376
+ throw;
377
+ }
207
378
 
208
- return result;
379
+ return Napi::Number::New(env, audio_id);
209
380
  }
210
381
 
211
382
  Napi::Value
@@ -213,10 +384,10 @@ audio::play (const Napi::CallbackInfo &info)
213
384
  {
214
385
  Napi::Env env = info.Env();
215
386
 
216
- std::lock_guard<std::mutex> guard(global::lock());
387
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
217
388
  global::assertReady(env);
218
389
 
219
- int audio_id = info[0].As<Napi::Number>().Int32Value();
390
+ SDL_AudioDeviceID audio_id = info[0].As<Napi::Number>().Uint32Value();
220
391
  bool should_play = info[1].As<Napi::Boolean>().Value();
221
392
 
222
393
  SDL_AudioStream *stream = _getStream(env, audio_id);
@@ -225,10 +396,7 @@ audio::play (const Napi::CallbackInfo &info)
225
396
  ? SDL_ResumeAudioStreamDevice(stream)
226
397
  : SDL_PauseAudioStreamDevice(stream);
227
398
  if (!success) {
228
- std::ostringstream message;
229
- message << (should_play ? "SDL_ResumeAudioStreamDevice(" : "SDL_PauseAudioStreamDevice(") << audio_id << ") error: " << SDL_GetError();
230
- SDL_ClearError();
231
- throw Napi::Error::New(env, message.str());
399
+ throw global::sdlError(env, should_play ? "SDL_ResumeAudioStreamDevice" : "SDL_PauseAudioStreamDevice", audio_id);
232
400
  }
233
401
 
234
402
  return env.Undefined();
@@ -239,43 +407,50 @@ audio::getQueued (const Napi::CallbackInfo &info)
239
407
  {
240
408
  Napi::Env env = info.Env();
241
409
 
242
- std::lock_guard<std::mutex> guard(global::lock());
410
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
243
411
  global::assertReady(env);
244
412
 
245
- int audio_id = info[0].As<Napi::Number>().Int32Value();
413
+ SDL_AudioDeviceID audio_id = info[0].As<Napi::Number>().Uint32Value();
246
414
 
247
415
  SDL_AudioStream *stream = _getStream(env, audio_id);
248
416
 
249
417
  int size = SDL_GetAudioStreamQueued(stream);
250
- if (size == -1) {
251
- std::ostringstream message;
252
- message << "SDL_GetAudioStreamQueued(" << audio_id << ") error: " << SDL_GetError();
253
- SDL_ClearError();
254
- throw Napi::Error::New(env, message.str());
255
- }
418
+ if (size == -1) { throw global::sdlError(env, "SDL_GetAudioStreamQueued", audio_id); }
256
419
 
257
420
  return Napi::Number::New(env, size);
258
421
  }
259
422
 
423
+ Napi::Value
424
+ audio::flush (const Napi::CallbackInfo &info)
425
+ {
426
+ Napi::Env env = info.Env();
427
+
428
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
429
+ global::assertReady(env);
430
+
431
+ SDL_AudioDeviceID audio_id = info[0].As<Napi::Number>().Uint32Value();
432
+
433
+ SDL_AudioStream *stream = _getStream(env, audio_id);
434
+
435
+ if (!SDL_FlushAudioStream(stream)) { throw global::sdlError(env, "SDL_FlushAudioStream", audio_id); }
436
+
437
+ return env.Undefined();
438
+ }
439
+
260
440
  Napi::Value
261
441
  audio::getAvailable (const Napi::CallbackInfo &info)
262
442
  {
263
443
  Napi::Env env = info.Env();
264
444
 
265
- std::lock_guard<std::mutex> guard(global::lock());
445
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
266
446
  global::assertReady(env);
267
447
 
268
- int audio_id = info[0].As<Napi::Number>().Int32Value();
448
+ SDL_AudioDeviceID audio_id = info[0].As<Napi::Number>().Uint32Value();
269
449
 
270
450
  SDL_AudioStream *stream = _getStream(env, audio_id);
271
451
 
272
452
  int size = SDL_GetAudioStreamAvailable(stream);
273
- if (size == -1) {
274
- std::ostringstream message;
275
- message << "SDL_GetAudioStreamAvailable(" << audio_id << ") error: " << SDL_GetError();
276
- SDL_ClearError();
277
- throw Napi::Error::New(env, message.str());
278
- }
453
+ if (size == -1) { throw global::sdlError(env, "SDL_GetAudioStreamAvailable", audio_id); }
279
454
 
280
455
  return Napi::Number::New(env, size);
281
456
  }
@@ -285,19 +460,14 @@ audio::clear (const Napi::CallbackInfo &info)
285
460
  {
286
461
  Napi::Env env = info.Env();
287
462
 
288
- std::lock_guard<std::mutex> guard(global::lock());
463
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
289
464
  global::assertReady(env);
290
465
 
291
- int audio_id = info[0].As<Napi::Number>().Int32Value();
466
+ SDL_AudioDeviceID audio_id = info[0].As<Napi::Number>().Uint32Value();
292
467
 
293
468
  SDL_AudioStream *stream = _getStream(env, audio_id);
294
469
 
295
- if (!SDL_ClearAudioStream(stream)) {
296
- std::ostringstream message;
297
- message << "SDL_ClearAudioStream(" << audio_id << ") error: " << SDL_GetError();
298
- SDL_ClearError();
299
- throw Napi::Error::New(env, message.str());
300
- }
470
+ if (!SDL_ClearAudioStream(stream)) { throw global::sdlError(env, "SDL_ClearAudioStream", audio_id); }
301
471
 
302
472
  return env.Undefined();
303
473
  }
@@ -307,21 +477,16 @@ audio::putData (const Napi::CallbackInfo &info)
307
477
  {
308
478
  Napi::Env env = info.Env();
309
479
 
310
- std::lock_guard<std::mutex> guard(global::lock());
480
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
311
481
  global::assertReady(env);
312
482
 
313
- int audio_id = info[0].As<Napi::Number>().Int32Value();
483
+ SDL_AudioDeviceID audio_id = info[0].As<Napi::Number>().Uint32Value();
314
484
  void *src = info[1].As<Napi::Buffer<char>>().Data();
315
485
  int size = info[2].As<Napi::Number>().Int32Value();
316
486
 
317
487
  SDL_AudioStream *stream = _getStream(env, audio_id);
318
488
 
319
- if (!SDL_PutAudioStreamData(stream, src, size)) {
320
- std::ostringstream message;
321
- message << "SDL_PutAudioStreamData(" << audio_id << ") error: " << SDL_GetError();
322
- SDL_ClearError();
323
- throw Napi::Error::New(env, message.str());
324
- }
489
+ if (!SDL_PutAudioStreamData(stream, src, size)) { throw global::sdlError(env, "SDL_PutAudioStreamData", audio_id); }
325
490
 
326
491
  return env.Undefined();
327
492
  }
@@ -331,22 +496,17 @@ audio::getData (const Napi::CallbackInfo &info)
331
496
  {
332
497
  Napi::Env env = info.Env();
333
498
 
334
- std::lock_guard<std::mutex> guard(global::lock());
499
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
335
500
  global::assertReady(env);
336
501
 
337
- int audio_id = info[0].As<Napi::Number>().Int32Value();
502
+ SDL_AudioDeviceID audio_id = info[0].As<Napi::Number>().Uint32Value();
338
503
  void *dst = info[1].As<Napi::Buffer<char>>().Data();
339
504
  int size = info[2].As<Napi::Number>().Int32Value();
340
505
 
341
506
  SDL_AudioStream *stream = _getStream(env, audio_id);
342
507
 
343
508
  int num = SDL_GetAudioStreamData(stream, dst, size);
344
- if (num == -1) {
345
- std::ostringstream message;
346
- message << "SDL_GetAudioStreamData(" << audio_id << ") error: " << SDL_GetError();
347
- SDL_ClearError();
348
- throw Napi::Error::New(env, message.str());
349
- }
509
+ if (num == -1) { throw global::sdlError(env, "SDL_GetAudioStreamData", audio_id); }
350
510
 
351
511
  return Napi::Number::New(env, num);
352
512
  }
@@ -356,15 +516,161 @@ audio::close (const Napi::CallbackInfo &info)
356
516
  {
357
517
  Napi::Env env = info.Env();
358
518
 
359
- std::lock_guard<std::mutex> guard(global::lock());
519
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
520
+ global::assertReady(env);
521
+
522
+ SDL_AudioDeviceID audio_id = info[0].As<Napi::Number>().Uint32Value();
523
+ Napi::Object device = info[1].As<Napi::Object>();
524
+
525
+ OpenStream &entry = _getEntry(env, audio_id);
526
+ SDL_AudioDeviceID device_id = entry.device_id;
527
+ _destroy(audio_id, entry, entry.env == (napi_env) env);
528
+ open_streams.erase(audio_id);
529
+
530
+ _setFormat(device, device_id);
531
+
532
+ return env.Undefined();
533
+ }
534
+
535
+ Napi::Value
536
+ audio::setNotify (const Napi::CallbackInfo &info)
537
+ {
538
+ Napi::Env env = info.Env();
539
+
540
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
360
541
  global::assertReady(env);
361
542
 
362
- int audio_id = info[0].As<Napi::Number>().Int32Value();
543
+ SDL_AudioDeviceID audio_id = info[0].As<Napi::Number>().Uint32Value();
544
+ bool notify = info[1].As<Napi::Boolean>().Value();
545
+
546
+ StreamState *state = _getEntry(env, audio_id).state;
547
+ if (notify) {
548
+ state->underruns.store(0, std::memory_order_relaxed);
549
+ state->missing.store(0, std::memory_order_relaxed);
550
+ state->available.store(0, std::memory_order_relaxed);
551
+ }
552
+ state->notify.store(notify, std::memory_order_release);
553
+
554
+ return env.Undefined();
555
+ }
556
+
557
+ Napi::Value
558
+ audio::setGain (const Napi::CallbackInfo &info)
559
+ {
560
+ Napi::Env env = info.Env();
561
+
562
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
563
+ global::assertReady(env);
564
+
565
+ SDL_AudioDeviceID audio_id = info[0].As<Napi::Number>().Uint32Value();
566
+ float gain = info[1].As<Napi::Number>().FloatValue();
363
567
 
364
568
  SDL_AudioStream *stream = _getStream(env, audio_id);
365
569
 
366
- SDL_DestroyAudioStream(stream);
367
- open_streams.erase(audio_id);
570
+ if (!SDL_SetAudioStreamGain(stream, gain)) { throw global::sdlError(env, "SDL_SetAudioStreamGain", audio_id); }
571
+
572
+ return env.Undefined();
573
+ }
574
+
575
+ Napi::Value
576
+ audio::setFrequencyRatio (const Napi::CallbackInfo &info)
577
+ {
578
+ Napi::Env env = info.Env();
579
+
580
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
581
+ global::assertReady(env);
582
+
583
+ SDL_AudioDeviceID audio_id = info[0].As<Napi::Number>().Uint32Value();
584
+ float ratio = info[1].As<Napi::Number>().FloatValue();
585
+
586
+ SDL_AudioStream *stream = _getStream(env, audio_id);
587
+
588
+ if (!SDL_SetAudioStreamFrequencyRatio(stream, ratio)) { throw global::sdlError(env, "SDL_SetAudioStreamFrequencyRatio", audio_id); }
368
589
 
369
590
  return env.Undefined();
370
591
  }
592
+
593
+ static void
594
+ _destroyStandaloneStream (SDL_AudioStream *stream)
595
+ {
596
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
597
+ SDL_DestroyAudioStream(stream);
598
+ }
599
+
600
+ static SDL_AudioStream *
601
+ _createStandaloneStream (Napi::Env &env, const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec)
602
+ {
603
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
604
+
605
+ SDL_AudioStream *stream = SDL_CreateAudioStream(src_spec, dst_spec);
606
+ if (stream == nullptr) { throw global::sdlError(env, "SDL_CreateAudioStream"); }
607
+
608
+ SDL_PropertiesID props = SDL_GetAudioStreamProperties(stream);
609
+ if (props == 0) {
610
+ Napi::Error error = global::sdlError(env, "SDL_GetAudioStreamProperties");
611
+ SDL_DestroyAudioStream(stream);
612
+ throw error;
613
+ }
614
+ if (!SDL_SetBooleanProperty(props, SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN, false)) {
615
+ Napi::Error error = global::sdlError(env, "SDL_SetBooleanProperty");
616
+ SDL_DestroyAudioStream(stream);
617
+ throw error;
618
+ }
619
+
620
+ return stream;
621
+ }
622
+
623
+ Napi::Value
624
+ audio::convert (const Napi::CallbackInfo &info)
625
+ {
626
+ Napi::Env env = info.Env();
627
+
628
+ SDL_AudioSpec src_spec;
629
+ src_spec.format = (SDL_AudioFormat) info[0].As<Napi::Number>().Int32Value();
630
+ src_spec.channels = info[1].As<Napi::Number>().Int32Value();
631
+ src_spec.freq = info[2].As<Napi::Number>().Int32Value();
632
+
633
+ Napi::Buffer<uint8_t> src = info[3].As<Napi::Buffer<uint8_t>>();
634
+ int src_size = info[4].As<Napi::Number>().Int32Value();
635
+
636
+ SDL_AudioSpec dst_spec;
637
+ dst_spec.format = (SDL_AudioFormat) info[5].As<Napi::Number>().Int32Value();
638
+ dst_spec.channels = info[6].As<Napi::Number>().Int32Value();
639
+ dst_spec.freq = info[7].As<Napi::Number>().Int32Value();
640
+
641
+ Napi::Buffer<uint8_t> dst = info[8].As<Napi::Buffer<uint8_t>>();
642
+
643
+ SDL_AudioStream *stream = _createStandaloneStream(env, &src_spec, &dst_spec);
644
+
645
+ if (!SDL_PutAudioStreamData(stream, src.Data(), src_size)) {
646
+ Napi::Error error = global::sdlError(env, "SDL_PutAudioStreamData");
647
+ _destroyStandaloneStream(stream);
648
+ throw error;
649
+ }
650
+
651
+ if (!SDL_FlushAudioStream(stream)) {
652
+ Napi::Error error = global::sdlError(env, "SDL_FlushAudioStream");
653
+ _destroyStandaloneStream(stream);
654
+ throw error;
655
+ }
656
+
657
+ int available = SDL_GetAudioStreamAvailable(stream);
658
+ if (available < 0) { available = 0; }
659
+
660
+ if (dst.Length() < (size_t) available) {
661
+ _destroyStandaloneStream(stream);
662
+ std::ostringstream message;
663
+ message << "dstData must hold at least " << available << " bytes";
664
+ throw Napi::Error::New(env, message.str());
665
+ }
666
+
667
+ int got = available > 0 ? SDL_GetAudioStreamData(stream, dst.Data(), available) : 0;
668
+ if (got < 0) {
669
+ Napi::Error error = global::sdlError(env, "SDL_GetAudioStreamData");
670
+ _destroyStandaloneStream(stream);
671
+ throw error;
672
+ }
673
+ _destroyStandaloneStream(stream);
674
+
675
+ return Napi::Number::New(env, got);
676
+ }