@kmamal/sdl 1.0.0-5 → 1.0.0-9

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