summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testaudio.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
committer3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
commit6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch)
tree34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/test/testaudio.c
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testaudio.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testaudio.c1274
1 files changed, 1274 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testaudio.c b/src/contrib/SDL-3.2.20/test/testaudio.c
new file mode 100644
index 0000000..8abb3b2
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testaudio.c
@@ -0,0 +1,1274 @@
1/*
2 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11*/
12
13#define SDL_MAIN_USE_CALLBACKS 1
14#include <SDL3/SDL_test.h>
15#include <SDL3/SDL_test_common.h>
16#include <SDL3/SDL_main.h>
17#include "testutils.h"
18
19#define POOF_LIFETIME 250
20#define VISUALIZER_WIDTH 100
21#define VISUALIZER_HEIGHT 50
22
23typedef struct Texture
24{
25 SDL_Texture *texture;
26 float w;
27 float h;
28} Texture;
29
30typedef enum ThingType
31{
32 THING_NULL,
33 THING_PHYSDEV,
34 THING_PHYSDEV_RECORDING,
35 THING_LOGDEV,
36 THING_LOGDEV_RECORDING,
37 THING_TRASHCAN,
38 THING_STREAM,
39 THING_POOF,
40 THING_WAV
41} ThingType;
42
43
44typedef struct Thing Thing;
45
46struct Thing
47{
48 ThingType what;
49
50 union {
51 struct {
52 SDL_AudioDeviceID devid;
53 bool recording;
54 SDL_AudioSpec spec;
55 char *name;
56 } physdev;
57 struct {
58 SDL_AudioDeviceID devid;
59 bool recording;
60 SDL_AudioSpec spec;
61 Thing *physdev;
62 bool visualizer_enabled;
63 bool visualizer_updated;
64 SDL_Texture *visualizer;
65 SDL_Mutex *postmix_lock;
66 float *postmix_buffer;
67 int postmix_buflen;
68 int postmix_allocated;
69 SDL_AudioSpec postmix_spec;
70 SDL_AtomicInt postmix_updated;
71 } logdev;
72 struct {
73 SDL_AudioSpec spec;
74 Uint8 *buf;
75 Uint32 buflen;
76 } wav;
77 struct {
78 float startw;
79 float starth;
80 float centerx;
81 float centery;
82 } poof;
83 struct {
84 SDL_AudioStream *stream;
85 int total_bytes;
86 Uint64 next_level_update;
87 Uint8 levels[5];
88 } stream;
89 } data;
90
91 Thing *line_connected_to;
92 char *titlebar;
93 SDL_FRect rect;
94 float z;
95 Uint8 r, g, b, a;
96 float progress;
97 float meter;
98 float scale;
99 Uint64 createticks;
100 Texture *texture;
101 const ThingType *can_be_dropped_onto;
102
103 void (*ontick)(Thing *thing, Uint64 now);
104 void (*ondrag)(Thing *thing, int button, float x, float y);
105 void (*ondrop)(Thing *thing, int button, float x, float y);
106 void (*ondraw)(Thing *thing, SDL_Renderer *renderer);
107 void (*onmousewheel)(Thing *thing, float y);
108
109 Thing *prev;
110 Thing *next;
111};
112
113
114static Uint64 app_ready_ticks = 0;
115static SDLTest_CommonState *state = NULL;
116
117static Thing *things = NULL;
118
119static Thing *mouseover_thing = NULL;
120static Thing *droppable_highlighted_thing = NULL;
121static Thing *dragging_thing = NULL;
122static int dragging_button = -1;
123static int dragging_button_real = -1;
124static bool ctrl_held = false;
125static bool alt_held = false;
126
127static Texture *physdev_texture = NULL;
128static Texture *logdev_texture = NULL;
129static Texture *audio_texture = NULL;
130static Texture *trashcan_texture = NULL;
131static Texture *soundboard_texture = NULL;
132static Texture *soundboard_levels_texture = NULL;
133
134
135static void SetTitleBar(const char *fmt, ...)
136{
137 char *title = NULL;
138 va_list ap;
139 va_start(ap, fmt);
140 SDL_vasprintf(&title, fmt, ap);
141 va_end(ap);
142
143 SDL_SetWindowTitle(state->windows[0], title);
144 SDL_free(title);
145}
146
147static void SetDefaultTitleBar(void)
148{
149 SetTitleBar("testaudio: %s", SDL_GetCurrentAudioDriver());
150}
151
152static Thing *FindThingAtPoint(const float x, const float y)
153{
154 const SDL_FPoint pt = { x, y };
155 Thing *result = NULL;
156 Thing *i;
157 for (i = things; i; i = i->next) {
158 if ((i != dragging_thing) && SDL_PointInRectFloat(&pt, &i->rect)) {
159 result = i; /* keep going, though, because things drawn on top are later in the list. */
160 }
161 }
162 return result;
163}
164
165static Thing *UpdateMouseOver(const float x, const float y)
166{
167 Thing *thing;
168
169 if (dragging_thing) {
170 thing = dragging_thing;
171 } else {
172 thing = FindThingAtPoint(x, y);
173 }
174
175 mouseover_thing = thing;
176
177 if (!thing) {
178 SetDefaultTitleBar();
179 } else if (thing->titlebar) {
180 SetTitleBar("%s", thing->titlebar);
181 }
182
183 return thing;
184}
185
186static Thing *CreateThing(ThingType what, float x, float y, float z, float w, float h, Texture *texture, const char *titlebar)
187{
188 Thing *last = NULL;
189 Thing *i;
190 Thing *thing;
191
192 thing = (Thing *) SDL_calloc(1, sizeof (Thing));
193 if (!thing) {
194 SDL_Log("Out of memory!");
195 return NULL;
196 }
197
198 if ((w < 0) || (h < 0)) {
199 SDL_assert(texture != NULL);
200 if (w < 0) {
201 w = texture->w;
202 }
203 if (h < 0) {
204 h = texture->h;
205 }
206 }
207
208 thing->what = what;
209 thing->rect.x = x;
210 thing->rect.y = y;
211 thing->rect.w = w;
212 thing->rect.h = h;
213 thing->z = z;
214 thing->r = 255;
215 thing->g = 255;
216 thing->b = 255;
217 thing->a = 255;
218 thing->scale = 1.0f;
219 thing->createticks = SDL_GetTicks();
220 thing->texture = texture;
221 thing->titlebar = titlebar ? SDL_strdup(titlebar) : NULL; /* if allocation fails, oh well. */
222
223 /* insert in list by Z order (furthest from the "camera" first, so they get drawn over; negative Z is not drawn at all). */
224 if (!things) {
225 things = thing;
226 return thing;
227 }
228
229 for (i = things; i; i = i->next) {
230 if (z > i->z) { /* insert here. */
231 thing->next = i;
232 thing->prev = i->prev;
233
234 SDL_assert(i->prev == last);
235
236 if (i->prev) {
237 i->prev->next = thing;
238 } else {
239 SDL_assert(i == things);
240 things = thing;
241 }
242 i->prev = thing;
243 return thing;
244 }
245 last = i;
246 }
247
248 if (last) {
249 last->next = thing;
250 thing->prev = last;
251 }
252 return thing;
253}
254
255static void DestroyThing(Thing *thing)
256{
257 if (!thing) {
258 return;
259 }
260
261 if (mouseover_thing == thing) {
262 mouseover_thing = NULL;
263 }
264
265 if (droppable_highlighted_thing == thing) {
266 droppable_highlighted_thing = NULL;
267 }
268
269 if (dragging_thing == thing) {
270 dragging_thing = NULL;
271 }
272
273 switch (thing->what) {
274 case THING_POOF: break;
275 case THING_NULL: break;
276 case THING_TRASHCAN: break;
277
278 case THING_LOGDEV:
279 case THING_LOGDEV_RECORDING:
280 SDL_CloseAudioDevice(thing->data.logdev.devid);
281 if (state->renderers[0] != NULL) {
282 SDL_DestroyTexture(thing->data.logdev.visualizer);
283 }
284 SDL_DestroyMutex(thing->data.logdev.postmix_lock);
285 SDL_free(thing->data.logdev.postmix_buffer);
286 break;
287
288 case THING_PHYSDEV:
289 case THING_PHYSDEV_RECORDING:
290 SDL_free(thing->data.physdev.name);
291 break;
292
293 case THING_WAV:
294 SDL_free(thing->data.wav.buf);
295 break;
296
297 case THING_STREAM:
298 SDL_DestroyAudioStream(thing->data.stream.stream);
299 break;
300 }
301
302 if (thing->prev) {
303 SDL_assert(thing != things);
304 thing->prev->next = thing->next;
305 } else {
306 SDL_assert(thing == things);
307 things = thing->next;
308 }
309
310 if (thing->next) {
311 thing->next->prev = thing->prev;
312 }
313
314 SDL_free(thing->titlebar);
315 SDL_free(thing);
316}
317
318static void DrawOneThing(SDL_Renderer *renderer, Thing *thing)
319{
320 SDL_FRect dst;
321 SDL_memcpy(&dst, &thing->rect, sizeof (SDL_FRect));
322 if (thing->scale != 1.0f) {
323 const float centerx = thing->rect.x + (thing->rect.w / 2);
324 const float centery = thing->rect.y + (thing->rect.h / 2);
325 const int w = thing->texture ? (int) thing->texture->w : 128;
326 const int h = thing->texture ? (int) thing->texture->h : 128;
327 dst.w = w * thing->scale;
328 dst.h = h * thing->scale;
329 dst.x = centerx - (dst.w / 2);
330 dst.y = centery - (dst.h / 2);
331 }
332
333 if (thing->texture) {
334 if (droppable_highlighted_thing == thing) {
335 SDL_SetRenderDrawColor(renderer, 255, 0, 255, 100);
336 SDL_RenderFillRect(renderer, &dst);
337 }
338 SDL_SetRenderDrawColor(renderer, thing->r, thing->g, thing->b, thing->a);
339 SDL_RenderTexture(renderer, thing->texture->texture, NULL, &dst);
340 } else {
341 SDL_SetRenderDrawColor(renderer, thing->r, thing->g, thing->b, thing->a);
342 SDL_RenderFillRect(renderer, &dst);
343 }
344
345 if (thing->ondraw) {
346 thing->ondraw(thing, renderer);
347 }
348
349 if (thing->progress > 0.0f) {
350 SDL_FRect r = { thing->rect.x, thing->rect.y + (thing->rect.h + 2.0f), 0.0f, 10.0f };
351 r.w = thing->rect.w * ((thing->progress > 1.0f) ? 1.0f : thing->progress);
352 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 128);
353 SDL_RenderFillRect(renderer, &r);
354 }
355
356 if (thing->meter > 0.0f) {
357 SDL_FRect r;
358 r.w = 10.0f;
359 r.h = thing->rect.h * ((thing->meter > 1.0f) ? 1.0f : thing->meter);
360 r.x = thing->rect.x + thing->rect.w + 2.0f;
361 r.y = (thing->rect.y + thing->rect.h) - r.h;
362 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 128);
363 SDL_RenderFillRect(renderer, &r);
364 }
365}
366
367static void DrawThings(SDL_Renderer *renderer)
368{
369 Thing *i;
370
371 /* draw connecting lines first, so they're behind everything else. */
372 for (i = things; i && (i->z >= 0.0f); i = i->next) {
373 Thing *dst = i->line_connected_to;
374 if (dst) {
375 SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
376 SDL_RenderLine(renderer, i->rect.x + (i->rect.w / 2), i->rect.y + (i->rect.h / 2), dst->rect.x + (dst->rect.w / 2), dst->rect.y + (dst->rect.h / 2));
377 }
378 }
379
380 /* Draw the actual things. */
381 for (i = things; i && (i->z >= 0.0f); i = i->next) {
382 if (i != dragging_thing) {
383 DrawOneThing(renderer, i);
384 }
385 }
386
387 if (dragging_thing) {
388 DrawOneThing(renderer, dragging_thing); /* draw last so it's always on top. */
389 }
390}
391
392static void Draw(void)
393{
394 SDL_Renderer *renderer = state->renderers[0];
395 if (renderer) { /* might be NULL if we're shutting down. */
396 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
397 SDL_SetRenderDrawColor(renderer, 64, 0, 64, 255);
398 SDL_RenderClear(renderer);
399 DrawThings(renderer);
400 SDL_RenderPresent(renderer);
401 }
402}
403
404static void RepositionRowOfThings(const ThingType what, const float y)
405{
406 int total_things = 0;
407 float texw = 0.0f;
408 float texh = 0.0f;
409 Thing *i;
410
411 for (i = things; i; i = i->next) {
412 if (i->what == what) {
413 texw = i->rect.w;
414 texh = i->rect.h;
415 total_things++;
416 }
417 }
418
419 if (total_things > 0) {
420 int w, h;
421 SDL_GetWindowSize(state->windows[0], &w, &h);
422 const float spacing = w / ((float) total_things);
423 float x = (spacing - texw) / 2.0f;
424 for (i = things; i; i = i->next) {
425 if (i->what == what) {
426 i->rect.x = x;
427 i->rect.y = (y >= 0.0f) ? y : ((h + y) - texh);
428 x += spacing;
429 }
430 }
431 }
432}
433
434static const char *AudioChansToStr(const int channels)
435{
436 switch (channels) {
437 case 1: return "mono";
438 case 2: return "stereo";
439 case 3: return "2.1";
440 case 4: return "quad";
441 case 5: return "4.1";
442 case 6: return "5.1";
443 case 7: return "6.1";
444 case 8: return "7.1";
445 default: break;
446 }
447 return "?";
448}
449
450static void PoofThing_ondrag(Thing *thing, int button, float x, float y)
451{
452 dragging_thing = NULL; /* refuse to be dragged. */
453}
454
455static void PoofThing_ontick(Thing *thing, Uint64 now)
456{
457 const int lifetime = POOF_LIFETIME;
458 const int elapsed = (int) (now - thing->createticks);
459 if (elapsed > lifetime) {
460 DestroyThing(thing);
461 } else {
462 const float pct = ((float) elapsed) / ((float) lifetime);
463 thing->a = (Uint8) (int) (255.0f - (pct * 255.0f));
464 thing->scale = 1.0f - pct; /* shrink to nothing! */
465 }
466}
467
468static Thing *CreatePoofThing(Thing *poofing_thing)
469{
470 const float centerx = poofing_thing->rect.x + (poofing_thing->rect.w / 2);
471 const float centery = poofing_thing->rect.y + (poofing_thing->rect.h / 2);
472 const float z = poofing_thing->z;
473 Thing *thing = CreateThing(THING_POOF, poofing_thing->rect.x, poofing_thing->rect.y, z, poofing_thing->rect.w, poofing_thing->rect.h, poofing_thing->texture, NULL);
474 if (thing) {
475 thing->data.poof.startw = poofing_thing->rect.w;
476 thing->data.poof.starth = poofing_thing->rect.h;
477 thing->data.poof.centerx = centerx;
478 thing->data.poof.centery = centery;
479 thing->ontick = PoofThing_ontick;
480 thing->ondrag = PoofThing_ondrag;
481 }
482 return thing;
483}
484
485static void DestroyThingInPoof(Thing *thing)
486{
487 if (thing) {
488 if (thing->what != THING_POOF) {
489 CreatePoofThing(thing);
490 }
491 DestroyThing(thing);
492 }
493}
494
495/* this poofs a thing and additionally poofs all things connected to the thing. */
496static void TrashThing(Thing *thing)
497{
498 Thing *i, *next;
499 for (i = things; i; i = next) {
500 next = i->next;
501 if (i->line_connected_to == thing) {
502 TrashThing(i);
503 next = things; /* start over in case this blew up the list. */
504 }
505 }
506 DestroyThingInPoof(thing);
507}
508
509static void StreamThing_ontick(Thing *thing, Uint64 now)
510{
511 if (!thing->line_connected_to) {
512 return;
513 }
514
515 /* are we playing? See if we're done, or update state. */
516 if (thing->line_connected_to->what == THING_LOGDEV) {
517 const int available = SDL_GetAudioStreamAvailable(thing->data.stream.stream);
518 if (!available) {
519 DestroyThingInPoof(thing);
520 return;
521 } else {
522 thing->progress = 1.0f - (thing->data.stream.total_bytes ? (((float) (available)) / ((float) thing->data.stream.total_bytes)) : 0.0f);
523 }
524 }
525
526 if (thing->data.stream.next_level_update <= now) {
527 Uint64 perf = SDL_GetPerformanceCounter();
528 int i;
529 for (i = 0; i < SDL_arraysize(thing->data.stream.levels); i++) {
530 thing->data.stream.levels[i] = (Uint8) (perf % 6);
531 perf >>= 3;
532 }
533 thing->data.stream.next_level_update += 150;
534 }
535}
536
537static void StreamThing_ondrag(Thing *thing, int button, float x, float y)
538{
539 if (button == SDL_BUTTON_RIGHT) { /* this is kinda hacky, but use this to disconnect from a playing source. */
540 if (thing->line_connected_to) {
541 SDL_UnbindAudioStream(thing->data.stream.stream); /* unbind from current device */
542 if (thing->line_connected_to->what == THING_LOGDEV_RECORDING) {
543 SDL_FlushAudioStream(thing->data.stream.stream);
544 }
545 thing->line_connected_to = NULL;
546 }
547 }
548}
549
550static void StreamThing_ondrop(Thing *thing, int button, float x, float y)
551{
552 if (droppable_highlighted_thing) {
553 if (droppable_highlighted_thing->what == THING_TRASHCAN) {
554 TrashThing(thing);
555 } else if (((droppable_highlighted_thing->what == THING_LOGDEV) || (droppable_highlighted_thing->what == THING_LOGDEV_RECORDING)) && (droppable_highlighted_thing != thing->line_connected_to)) {
556 /* connect to a logical device! */
557 SDL_Log("Binding audio stream ('%s') to logical device %u", thing->titlebar, (unsigned int) droppable_highlighted_thing->data.logdev.devid);
558 if (thing->line_connected_to) {
559 SDL_UnbindAudioStream(thing->data.stream.stream); /* unbind from current device */
560 if (thing->line_connected_to->what == THING_LOGDEV_RECORDING) {
561 SDL_FlushAudioStream(thing->data.stream.stream);
562 }
563 }
564
565 SDL_BindAudioStream(droppable_highlighted_thing->data.logdev.devid, thing->data.stream.stream); /* bind to new device! */
566 thing->data.stream.total_bytes = SDL_GetAudioStreamAvailable(thing->data.stream.stream);
567 thing->progress = 0.0f; /* ontick will adjust this if we're on a playback device.*/
568 thing->data.stream.next_level_update = SDL_GetTicks() + 100;
569 thing->line_connected_to = droppable_highlighted_thing;
570 }
571 }
572}
573
574static void StreamThing_onmousewheel(Thing *thing, float y)
575{
576 thing->meter += y * 0.01f;
577 thing->meter = SDL_clamp(thing->meter, 0.0f, 1.0f);
578 SDL_SetAudioStreamGain(thing->data.stream.stream, thing->meter);
579}
580
581static void StreamThing_ondraw(Thing *thing, SDL_Renderer *renderer)
582{
583 if (thing->line_connected_to) { /* are we playing? Update progress bar, and bounce the levels a little. */
584 static const float xlocs[5] = { 18, 39, 59, 79, 99 };
585 static const float ylocs[5] = { 49, 39, 29, 19, 10 };
586 const float blockw = soundboard_levels_texture->w;
587 const float blockh = soundboard_levels_texture->h / 5.0f;
588 int i, j;
589 SDL_SetRenderDrawColor(renderer, thing->r, thing->g, thing->b, thing->a);
590 for (i = 0; i < SDL_arraysize(thing->data.stream.levels); i++) {
591 const int level = (int) thing->data.stream.levels[i];
592 const float x = xlocs[i];
593 for (j = 0; j < level; j++) {
594 const SDL_FRect src = { 0, soundboard_levels_texture->h - ((j+1) * blockh), blockw, blockh };
595 const SDL_FRect dst = { thing->rect.x + x, thing->rect.y + ylocs[j], blockw, blockh };
596 SDL_RenderTexture(renderer, soundboard_levels_texture->texture, &src, &dst);
597 }
598 }
599 }
600}
601
602static Thing *CreateStreamThing(const SDL_AudioSpec *spec, const Uint8 *buf, const Uint32 buflen, const char *fname, const float x, const float y)
603{
604 static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_LOGDEV, THING_LOGDEV_RECORDING, THING_NULL };
605 Thing *thing = CreateThing(THING_STREAM, x, y, 0, -1, -1, soundboard_texture, fname);
606 if (thing) {
607 SDL_Log("Adding audio stream for %s", fname ? fname : "(null)");
608 thing->data.stream.stream = SDL_CreateAudioStream(spec, spec);
609 if (buf && buflen) {
610 SDL_PutAudioStreamData(thing->data.stream.stream, buf, (int) buflen);
611 SDL_FlushAudioStream(thing->data.stream.stream);
612 thing->data.stream.total_bytes = SDL_GetAudioStreamAvailable(thing->data.stream.stream);
613 }
614 thing->ontick = StreamThing_ontick;
615 thing->ondrag = StreamThing_ondrag;
616 thing->ondrop = StreamThing_ondrop;
617 thing->ondraw = StreamThing_ondraw;
618 thing->onmousewheel = StreamThing_onmousewheel;
619 thing->can_be_dropped_onto = can_be_dropped_onto;
620 thing->meter = 1.0f; /* gain. */
621 }
622 return thing;
623}
624
625static void WavThing_ondrag(Thing *thing, int button, float x, float y)
626{
627 if (button == SDL_BUTTON_RIGHT) { /* drag out a new audio stream. */
628 dragging_thing = CreateStreamThing(&thing->data.wav.spec, thing->data.wav.buf, thing->data.wav.buflen, thing->titlebar, x - (thing->rect.w / 2), y - (thing->rect.h / 2));
629 }
630}
631
632static void WavThing_ondrop(Thing *thing, int button, float x, float y)
633{
634 if (droppable_highlighted_thing) {
635 if (droppable_highlighted_thing->what == THING_TRASHCAN) {
636 TrashThing(thing);
637 }
638 }
639}
640
641static Thing *LoadWavThing(const char *fname, float x, float y)
642{
643 Thing *thing = NULL;
644 char *path;
645 SDL_AudioSpec spec;
646 Uint8 *buf = NULL;
647 Uint32 buflen = 0;
648
649 path = GetNearbyFilename(fname);
650 if (path) {
651 fname = path;
652 }
653
654 if (SDL_LoadWAV(fname, &spec, &buf, &buflen)) {
655 static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
656 char *titlebar = NULL;
657 const char *nodirs = SDL_strrchr(fname, '/');
658 #ifdef SDL_PLATFORM_WINDOWS
659 const char *nodirs2 = SDL_strrchr(nodirs ? nodirs : fname, '\\');
660 if (nodirs2) {
661 nodirs = nodirs2;
662 }
663 #endif
664
665 SDL_Log("Adding WAV file '%s'", fname);
666
667 if (nodirs) {
668 nodirs++;
669 } else {
670 nodirs = fname;
671 }
672
673 SDL_asprintf(&titlebar, "WAV file (\"%s\", %s, %s, %uHz)", nodirs, SDL_GetAudioFormatName(spec.format), AudioChansToStr(spec.channels), (unsigned int) spec.freq);
674 thing = CreateThing(THING_WAV, x - (audio_texture->w / 2), y - (audio_texture->h / 2), 5, -1, -1, audio_texture, titlebar);
675 if (thing) {
676 SDL_free(titlebar);
677 SDL_memcpy(&thing->data.wav.spec, &spec, sizeof (SDL_AudioSpec));
678 thing->data.wav.buf = buf;
679 thing->data.wav.buflen = buflen;
680 thing->can_be_dropped_onto = can_be_dropped_onto;
681 thing->ondrag = WavThing_ondrag;
682 thing->ondrop = WavThing_ondrop;
683 }
684 }
685
686 SDL_free(path);
687
688 return thing;
689}
690
691static Thing *LoadStockWavThing(const char *fname)
692{
693 char *path = GetNearbyFilename(fname);
694 Thing *thing = LoadWavThing(path ? path : fname, 0.0f, 0.0f); /* will reposition in a moment. */
695 SDL_free(path);
696 return thing;
697}
698
699static void LoadStockWavThings(void)
700{
701 LoadStockWavThing("sample.wav");
702 RepositionRowOfThings(THING_WAV, -10.0f);
703}
704
705static void DestroyTexture(Texture *tex)
706{
707 if (tex) {
708 if (state->renderers[0] != NULL) { /* if the renderer went away, this pointer is already bogus. */
709 SDL_DestroyTexture(tex->texture);
710 }
711 SDL_free(tex);
712 }
713}
714
715static Texture *CreateTexture(const char *fname)
716{
717 Texture *tex = (Texture *) SDL_calloc(1, sizeof (Texture));
718 if (!tex) {
719 SDL_Log("Out of memory!");
720 } else {
721 int texw, texh;
722 tex->texture = LoadTexture(state->renderers[0], fname, true, &texw, &texh);
723 if (!tex->texture) {
724 SDL_Log("Failed to load '%s': %s", fname, SDL_GetError());
725 SDL_free(tex);
726 return NULL;
727 }
728 SDL_SetTextureBlendMode(tex->texture, SDL_BLENDMODE_BLEND);
729 tex->w = (float) texw;
730 tex->h = (float) texh;
731 }
732 return tex;
733}
734
735static Thing *CreateLogicalDeviceThing(Thing *parent, SDL_AudioDeviceID which, float x, float y);
736
737static void DeviceThing_ondrag(Thing *thing, int button, float x, float y)
738{
739 if ((button == SDL_BUTTON_MIDDLE) && (thing->what == THING_LOGDEV_RECORDING)) { /* drag out a new stream. This is a UX mess. :/ */
740 dragging_thing = CreateStreamThing(&thing->data.logdev.spec, NULL, 0, NULL, x, y);
741 if (dragging_thing) {
742 dragging_thing->data.stream.next_level_update = SDL_GetTicks() + 100;
743 SDL_BindAudioStream(thing->data.logdev.devid, dragging_thing->data.stream.stream); /* bind to new device! */
744 dragging_thing->line_connected_to = thing;
745 }
746 } else if (button == SDL_BUTTON_RIGHT) { /* drag out a new logical device. */
747 const SDL_AudioDeviceID which = ((thing->what == THING_LOGDEV) || (thing->what == THING_LOGDEV_RECORDING)) ? thing->data.logdev.devid : thing->data.physdev.devid;
748 const SDL_AudioDeviceID devid = SDL_OpenAudioDevice(which, NULL);
749 dragging_thing = devid ? CreateLogicalDeviceThing(thing, devid, x - (thing->rect.w / 2), y - (thing->rect.h / 2)) : NULL;
750 }
751}
752
753static void SetLogicalDeviceTitlebar(Thing *thing)
754{
755 SDL_AudioSpec *spec = &thing->data.logdev.spec;
756 int frames = 0;
757 SDL_GetAudioDeviceFormat(thing->data.logdev.devid, spec, &frames);
758 SDL_free(thing->titlebar);
759 SDL_asprintf(&thing->titlebar, "Logical device #%u (%s, %s, %s, %uHz, %d frames)", (unsigned int) thing->data.logdev.devid, thing->data.logdev.recording ? "RECORDING" : "PLAYBACK", SDL_GetAudioFormatName(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq, frames);
760}
761
762static void LogicalDeviceThing_ondrop(Thing *thing, int button, float x, float y)
763{
764 if (droppable_highlighted_thing) {
765 if (droppable_highlighted_thing->what == THING_TRASHCAN) {
766 TrashThing(thing);
767 }
768 }
769}
770
771static void SDLCALL PostmixCallback(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen)
772{
773 Thing *thing = (Thing *) userdata;
774
775 SDL_LockMutex(thing->data.logdev.postmix_lock);
776
777 if (thing->data.logdev.postmix_allocated < buflen) {
778 void *ptr = SDL_realloc(thing->data.logdev.postmix_buffer, buflen);
779 if (!ptr) {
780 SDL_UnlockMutex(thing->data.logdev.postmix_lock);
781 return; /* oh well. */
782 }
783 thing->data.logdev.postmix_buffer = (float *) ptr;
784 thing->data.logdev.postmix_allocated = buflen;
785 }
786
787 SDL_copyp(&thing->data.logdev.postmix_spec, spec);
788 SDL_memcpy(thing->data.logdev.postmix_buffer, buffer, buflen);
789 thing->data.logdev.postmix_buflen = buflen;
790 SDL_SetAtomicInt(&thing->data.logdev.postmix_updated, 1);
791
792 SDL_UnlockMutex(thing->data.logdev.postmix_lock);
793}
794
795static void UpdateVisualizer(SDL_Renderer *renderer, SDL_Texture *visualizer, const int channels, const float *buffer, const int buflen)
796{
797 static const SDL_Color channel_colors[8] = {
798 { 255, 255, 255, 255 },
799 { 255, 0, 0, 255 },
800 { 0, 255, 0, 255 },
801 { 0, 0, 255, 255 },
802 { 255, 255, 0, 255 },
803 { 0, 255, 255, 255 },
804 { 255, 0, 255, 255 },
805 { 127, 127, 127, 255 }
806 };
807
808 SDL_SetRenderTarget(renderer, visualizer);
809 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
810 SDL_RenderClear(renderer);
811
812 if (buffer && buflen) {
813 const int frames = (buflen / sizeof (float)) / channels;
814 const int skip = frames / (VISUALIZER_WIDTH * 2);
815 int i, j;
816
817 for (i = channels - 1; i >= 0; i--) {
818 const SDL_Color *color = &channel_colors[i % SDL_arraysize(channel_colors)];
819 SDL_FPoint points[VISUALIZER_WIDTH + 2];
820 float prevx = 0.0f;
821 int pointidx = 1;
822
823 points[0].x = 0.0f;
824 points[0].y = VISUALIZER_HEIGHT * 0.5f;
825
826 for (j = 0; j < (SDL_arraysize(points)-1); j++) {
827 const float val = buffer[((j * skip) * channels) + i];
828 const float x = prevx + 2;
829 const float y = (VISUALIZER_HEIGHT * 0.5f) - (VISUALIZER_HEIGHT * (val * 0.5f));
830 SDL_assert(pointidx < SDL_arraysize(points));
831 points[pointidx].x = x;
832 points[pointidx].y = y;
833 pointidx++;
834 prevx = x;
835 }
836
837 SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, 255);
838 SDL_RenderLines(renderer, points, pointidx);
839 }
840 }
841
842 SDL_SetRenderTarget(renderer, NULL);
843}
844
845static void LogicalDeviceThing_ontick(Thing *thing, Uint64 now)
846{
847 const bool ismousedover = (thing == mouseover_thing);
848
849 if (!thing->data.logdev.visualizer || !thing->data.logdev.postmix_lock) { /* need these to work, skip if they failed. */
850 return;
851 }
852
853 if (thing->data.logdev.visualizer_enabled != ismousedover) {
854 thing->data.logdev.visualizer_enabled = ismousedover;
855 if (!ismousedover) {
856 SDL_SetAudioPostmixCallback(thing->data.logdev.devid, NULL, NULL);
857 } else {
858 if (thing->data.logdev.postmix_buffer) {
859 SDL_memset(thing->data.logdev.postmix_buffer, '\0', thing->data.logdev.postmix_buflen);
860 }
861 SDL_SetAtomicInt(&thing->data.logdev.postmix_updated, 1); /* so this will at least clear the texture later. */
862 SDL_SetAudioPostmixCallback(thing->data.logdev.devid, PostmixCallback, thing);
863 }
864 }
865}
866
867static void LogicalDeviceThing_ondraw(Thing *thing, SDL_Renderer *renderer)
868{
869 if (thing->data.logdev.visualizer_enabled) {
870 SDL_FRect dst;
871 dst.w = thing->rect.w;
872 dst.h = thing->rect.h;
873 dst.x = thing->rect.x + ((thing->rect.w - dst.w) / 2);
874 dst.y = thing->rect.y + ((thing->rect.h - dst.h) / 2);
875
876 if (SDL_GetAtomicInt(&thing->data.logdev.postmix_updated)) {
877 float *buffer;
878 int channels;
879 int buflen;
880
881 SDL_LockMutex(thing->data.logdev.postmix_lock);
882 channels = thing->data.logdev.postmix_spec.channels;
883 buflen = thing->data.logdev.postmix_buflen;
884 buffer = (float *) SDL_malloc(thing->data.logdev.postmix_buflen);
885 if (buffer) {
886 SDL_memcpy(buffer, thing->data.logdev.postmix_buffer, thing->data.logdev.postmix_buflen);
887 SDL_SetAtomicInt(&thing->data.logdev.postmix_updated, 0);
888 }
889 SDL_UnlockMutex(thing->data.logdev.postmix_lock);
890
891 UpdateVisualizer(renderer, thing->data.logdev.visualizer, channels, buffer, buflen);
892 SDL_free(buffer);
893 }
894
895 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 30);
896 SDL_RenderTexture(renderer, thing->data.logdev.visualizer, NULL, &dst);
897 }
898}
899
900static void LogicalDeviceThing_onmousewheel(Thing *thing, float y)
901{
902 thing->meter += y * 0.01f;
903 thing->meter = SDL_clamp(thing->meter, 0.0f, 1.0f);
904 SDL_SetAudioDeviceGain(thing->data.logdev.devid, thing->meter);
905}
906
907static Thing *CreateLogicalDeviceThing(Thing *parent, SDL_AudioDeviceID which, float x, float y)
908{
909 static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
910 Thing *physthing = ((parent->what == THING_LOGDEV) || (parent->what == THING_LOGDEV_RECORDING)) ? parent->data.logdev.physdev : parent;
911 const bool recording = physthing->data.physdev.recording;
912 Thing *thing;
913
914 SDL_Log("Adding logical audio device %u", (unsigned int) which);
915 thing = CreateThing(recording ? THING_LOGDEV_RECORDING : THING_LOGDEV, x, y, 5, -1, -1, logdev_texture, NULL);
916 if (thing) {
917 thing->data.logdev.devid = which;
918 thing->data.logdev.recording = recording;
919 thing->data.logdev.physdev = physthing;
920 thing->data.logdev.visualizer = SDL_CreateTexture(state->renderers[0], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, VISUALIZER_WIDTH, VISUALIZER_HEIGHT);
921 thing->data.logdev.postmix_lock = SDL_CreateMutex();
922 if (thing->data.logdev.visualizer) {
923 SDL_SetTextureBlendMode(thing->data.logdev.visualizer, SDL_BLENDMODE_BLEND);
924 }
925 thing->line_connected_to = physthing;
926 thing->meter = 1.0f;
927 thing->ontick = LogicalDeviceThing_ontick;
928 thing->ondrag = DeviceThing_ondrag;
929 thing->ondrop = LogicalDeviceThing_ondrop;
930 thing->ondraw = LogicalDeviceThing_ondraw;
931 thing->onmousewheel = LogicalDeviceThing_onmousewheel;
932 thing->can_be_dropped_onto = can_be_dropped_onto;
933
934 SetLogicalDeviceTitlebar(thing);
935 }
936 return thing;
937}
938
939static void SetPhysicalDeviceTitlebar(Thing *thing)
940{
941 int frames = 0;
942 SDL_AudioSpec *spec = &thing->data.physdev.spec;
943 SDL_GetAudioDeviceFormat(thing->data.physdev.devid, spec, &frames);
944 SDL_free(thing->titlebar);
945 if (thing->data.physdev.devid == SDL_AUDIO_DEVICE_DEFAULT_RECORDING) {
946 SDL_asprintf(&thing->titlebar, "Default system device (RECORDING, %s, %s, %uHz, %d frames)", SDL_GetAudioFormatName(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq, frames);
947 } else if (thing->data.physdev.devid == SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK) {
948 SDL_asprintf(&thing->titlebar, "Default system device (PLAYBACK, %s, %s, %uHz, %d frames)", SDL_GetAudioFormatName(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq, frames);
949 } else {
950 SDL_asprintf(&thing->titlebar, "Physical device #%u (%s, \"%s\", %s, %s, %uHz, %d frames)", (unsigned int) thing->data.physdev.devid, thing->data.physdev.recording ? "RECORDING" : "PLAYBACK", thing->data.physdev.name, SDL_GetAudioFormatName(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq, frames);
951 }
952}
953
954static void PhysicalDeviceThing_ondrop(Thing *thing, int button, float x, float y)
955{
956 if (droppable_highlighted_thing) {
957 if (droppable_highlighted_thing->what == THING_TRASHCAN) {
958 TrashThing(thing);
959 }
960 }
961}
962
963static void PhysicalDeviceThing_ontick(Thing *thing, Uint64 now)
964{
965 const int lifetime = POOF_LIFETIME;
966 const int elapsed = (int) (now - thing->createticks);
967 if (elapsed > lifetime) {
968 thing->scale = 1.0f;
969 thing->a = 255;
970 thing->ontick = NULL; /* no more ticking. */
971 } else {
972 const float pct = ((float) elapsed) / ((float) lifetime);
973 thing->a = (Uint8) (int) (pct * 255.0f);
974 thing->scale = pct; /* grow to normal size */
975 }
976}
977
978
979static Thing *CreatePhysicalDeviceThing(const SDL_AudioDeviceID which, const bool recording)
980{
981 static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
982 static float next_physdev_x = 0;
983 Thing *thing;
984 int winw, winh;
985
986 SDL_GetWindowSize(state->windows[0], &winw, &winh);
987 if (next_physdev_x > (winw-physdev_texture->w)) {
988 next_physdev_x = 0;
989 }
990
991 SDL_Log("Adding physical audio device %u", (unsigned int) which);
992 thing = CreateThing(recording ? THING_PHYSDEV_RECORDING : THING_PHYSDEV, next_physdev_x, 170, 5, -1, -1, physdev_texture, NULL);
993 if (thing) {
994 const char *name = SDL_GetAudioDeviceName(which);
995 thing->data.physdev.devid = which;
996 thing->data.physdev.recording = recording;
997 thing->data.physdev.name = name ? SDL_strdup(name) : NULL;
998 thing->ondrag = DeviceThing_ondrag;
999 thing->ondrop = PhysicalDeviceThing_ondrop;
1000 thing->ontick = PhysicalDeviceThing_ontick;
1001 thing->can_be_dropped_onto = can_be_dropped_onto;
1002
1003 SetPhysicalDeviceTitlebar(thing);
1004 if (SDL_GetTicks() <= (app_ready_ticks + 2000)) { /* assume this is the initial batch if it happens in the first two seconds. */
1005 RepositionRowOfThings(THING_PHYSDEV, 10.0f); /* don't rearrange them after the initial add. */
1006 RepositionRowOfThings(THING_PHYSDEV_RECORDING, 170.0f); /* don't rearrange them after the initial add. */
1007 next_physdev_x = 0.0f;
1008 } else {
1009 next_physdev_x += physdev_texture->w * 1.5f;
1010 }
1011 }
1012
1013 return thing;
1014}
1015
1016static Thing *CreateTrashcanThing(void)
1017{
1018 int winw, winh;
1019 SDL_GetWindowSize(state->windows[0], &winw, &winh);
1020 return CreateThing(THING_TRASHCAN, winw - trashcan_texture->w, winh - trashcan_texture->h, 10, -1, -1, trashcan_texture, "Drag things here to remove them.");
1021}
1022
1023static Thing *CreateDefaultPhysicalDevice(const bool recording)
1024{
1025 return CreatePhysicalDeviceThing(recording ? SDL_AUDIO_DEVICE_DEFAULT_RECORDING : SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, recording);
1026}
1027
1028static void TickThings(void)
1029{
1030 Thing *i;
1031 Thing *next;
1032 const Uint64 now = SDL_GetTicks();
1033 for (i = things; i; i = next) {
1034 next = i->next; /* in case this deletes itself. */
1035 if (i->ontick) {
1036 i->ontick(i, now);
1037 }
1038 }
1039}
1040
1041static void WindowResized(const int newwinw, const int newwinh)
1042{
1043 Thing *i;
1044 const float neww = (float) newwinw;
1045 const float newh = (float) newwinh;
1046 const float oldw = (float) state->window_w;
1047 const float oldh = (float) state->window_h;
1048 for (i = things; i; i = i->next) {
1049 const float halfw = i->rect.w / 2.0f;
1050 const float halfh = i->rect.h / 2.0f;
1051 const float x = (i->rect.x + halfw) / oldw;
1052 const float y = (i->rect.y + halfh) / oldh;
1053 i->rect.x = (x * neww) - halfw;
1054 i->rect.y = (y * newh) - halfh;
1055 }
1056 state->window_w = newwinw;
1057 state->window_h = newwinh;
1058}
1059
1060SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
1061{
1062 int i;
1063
1064 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO | SDL_INIT_AUDIO);
1065 if (!state) {
1066 return SDL_APP_FAILURE;
1067 }
1068
1069 state->window_flags |= SDL_WINDOW_RESIZABLE;
1070
1071 for (i = 1; i < argc;) {
1072 int consumed = SDLTest_CommonArg(state, i);
1073 if (consumed == 0) {
1074 consumed = -1;
1075 /* add our own command lines here. */
1076 }
1077 if (consumed < 0) {
1078 static const char *options[] = {
1079 /* add our own command lines here. */
1080 /*"[--blend none|blend|add|mod|mul|sub]",*/
1081 NULL
1082 };
1083 SDLTest_CommonLogUsage(state, argv[0], options);
1084 return SDL_APP_FAILURE;
1085 }
1086 i += consumed;
1087 }
1088
1089 if (!SDLTest_CommonInit(state)) {
1090 return SDL_APP_FAILURE;
1091 }
1092
1093 if (state->audio_id) {
1094 SDL_CloseAudioDevice(state->audio_id);
1095 state->audio_id = 0;
1096 }
1097
1098 SetDefaultTitleBar();
1099
1100 if ((physdev_texture = CreateTexture("physaudiodev.bmp")) == NULL) { return SDL_APP_FAILURE; }
1101 if ((logdev_texture = CreateTexture("logaudiodev.bmp")) == NULL) { return SDL_APP_FAILURE; }
1102 if ((audio_texture = CreateTexture("audiofile.bmp")) == NULL) { return SDL_APP_FAILURE; }
1103 if ((trashcan_texture = CreateTexture("trashcan.bmp")) == NULL) { return SDL_APP_FAILURE; }
1104 if ((soundboard_texture = CreateTexture("soundboard.bmp")) == NULL) { return SDL_APP_FAILURE; }
1105 if ((soundboard_levels_texture = CreateTexture("soundboard_levels.bmp")) == NULL) { return SDL_APP_FAILURE; }
1106
1107 LoadStockWavThings();
1108 CreateTrashcanThing();
1109 CreateDefaultPhysicalDevice(false);
1110 CreateDefaultPhysicalDevice(true);
1111
1112 return SDL_APP_CONTINUE;
1113}
1114
1115
1116static bool saw_event = false;
1117
1118SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
1119{
1120 Thing *thing = NULL;
1121
1122 saw_event = true;
1123 SDL_ConvertEventToRenderCoordinates(SDL_GetRenderer(SDL_GetWindowFromEvent(event)), event);
1124
1125 switch (event->type) {
1126 case SDL_EVENT_MOUSE_MOTION:
1127 thing = UpdateMouseOver(event->motion.x, event->motion.y);
1128 if ((dragging_button == -1) && event->motion.state) {
1129 if (event->motion.state & SDL_BUTTON_LMASK) {
1130 /* for people that don't have all three buttons... */
1131 if (ctrl_held) {
1132 dragging_button = SDL_BUTTON_RIGHT;
1133 } else if (alt_held) {
1134 dragging_button = SDL_BUTTON_MIDDLE;
1135 } else {
1136 dragging_button = SDL_BUTTON_LEFT;
1137 }
1138 dragging_button_real = SDL_BUTTON_LEFT;
1139 } else if (event->motion.state & SDL_BUTTON_RMASK) {
1140 dragging_button = SDL_BUTTON_RIGHT;
1141 dragging_button_real = SDL_BUTTON_RIGHT;
1142 } else if (event->motion.state & SDL_BUTTON_MMASK) {
1143 dragging_button = SDL_BUTTON_MIDDLE;
1144 dragging_button_real = SDL_BUTTON_MIDDLE;
1145 }
1146
1147 if (dragging_button != -1) {
1148 dragging_thing = thing;
1149 if (thing && thing->ondrag) {
1150 thing->ondrag(thing, dragging_button, event->motion.x, event->motion.y);
1151 }
1152 }
1153 }
1154
1155 droppable_highlighted_thing = NULL;
1156 if (dragging_thing) {
1157 dragging_thing->rect.x = event->motion.x - (dragging_thing->rect.w / 2);
1158 dragging_thing->rect.y = event->motion.y - (dragging_thing->rect.h / 2);
1159 if (dragging_thing->can_be_dropped_onto) {
1160 thing = FindThingAtPoint(event->motion.x, event->motion.y);
1161 if (thing) {
1162 int i;
1163 for (i = 0; dragging_thing->can_be_dropped_onto[i]; i++) {
1164 if (dragging_thing->can_be_dropped_onto[i] == thing->what) {
1165 droppable_highlighted_thing = thing;
1166 break;
1167 }
1168 }
1169 }
1170 }
1171 }
1172 break;
1173
1174 case SDL_EVENT_MOUSE_BUTTON_DOWN:
1175 thing = UpdateMouseOver(event->button.x, event->button.y);
1176 break;
1177
1178 case SDL_EVENT_MOUSE_BUTTON_UP:
1179 if (dragging_button_real == event->button.button) {
1180 Thing *dropped_thing = dragging_thing;
1181 dragging_thing = NULL;
1182 dragging_button = -1;
1183 dragging_button_real = -1;
1184 if (dropped_thing && dropped_thing->ondrop) {
1185 dropped_thing->ondrop(dropped_thing, event->button.button, event->button.x, event->button.y);
1186 }
1187 droppable_highlighted_thing = NULL;
1188 }
1189 thing = UpdateMouseOver(event->button.x, event->button.y);
1190 break;
1191
1192 case SDL_EVENT_MOUSE_WHEEL:
1193 thing = UpdateMouseOver(event->wheel.mouse_x, event->wheel.mouse_y);
1194 if (thing && thing->onmousewheel) {
1195 thing->onmousewheel(thing, event->wheel.y * ((event->wheel.direction == SDL_MOUSEWHEEL_FLIPPED) ? -1.0f : 1.0f));
1196 }
1197 break;
1198
1199 case SDL_EVENT_KEY_DOWN:
1200 case SDL_EVENT_KEY_UP:
1201 ctrl_held = ((event->key.mod & SDL_KMOD_CTRL) != 0);
1202 alt_held = ((event->key.mod & SDL_KMOD_ALT) != 0);
1203 break;
1204
1205 case SDL_EVENT_DROP_FILE:
1206 SDL_Log("Drop file! '%s'", event->drop.data);
1207 LoadWavThing(event->drop.data, event->drop.x, event->drop.y);
1208 /* SDL frees event->drop.data for you when you use SDL_AppEvent(). */
1209 break;
1210
1211 case SDL_EVENT_WINDOW_RESIZED:
1212 WindowResized(event->window.data1, event->window.data2);
1213 break;
1214
1215 case SDL_EVENT_AUDIO_DEVICE_ADDED:
1216 CreatePhysicalDeviceThing(event->adevice.which, event->adevice.recording);
1217 break;
1218
1219 case SDL_EVENT_AUDIO_DEVICE_REMOVED: {
1220 const SDL_AudioDeviceID which = event->adevice.which;
1221 Thing *i, *next;
1222 SDL_Log("Removing audio device %u", (unsigned int) which);
1223 for (i = things; i; i = next) {
1224 next = i->next;
1225 if (((i->what == THING_PHYSDEV) || (i->what == THING_PHYSDEV_RECORDING)) && (i->data.physdev.devid == which)) {
1226 TrashThing(i);
1227 next = things; /* in case we mangled the list. */
1228 } else if (((i->what == THING_LOGDEV) || (i->what == THING_LOGDEV_RECORDING)) && (i->data.logdev.devid == which)) {
1229 TrashThing(i);
1230 next = things; /* in case we mangled the list. */
1231 }
1232 }
1233 break;
1234 }
1235
1236 default: break;
1237 }
1238
1239 return SDLTest_CommonEventMainCallbacks(state, event);
1240}
1241
1242SDL_AppResult SDL_AppIterate(void *appstate)
1243{
1244 if (app_ready_ticks == 0) {
1245 app_ready_ticks = SDL_GetTicks();
1246 }
1247
1248 TickThings();
1249 Draw();
1250
1251 if (saw_event) {
1252 saw_event = false; /* reset this so we know when SDL_AppEvent() runs again */
1253 } else {
1254 SDL_Delay(10);
1255 }
1256
1257 return SDL_APP_CONTINUE;
1258}
1259
1260void SDL_AppQuit(void *appstate, SDL_AppResult result)
1261{
1262 while (things) {
1263 DestroyThing(things); /* make sure all the audio devices are closed, etc. */
1264 }
1265
1266 DestroyTexture(physdev_texture);
1267 DestroyTexture(logdev_texture);
1268 DestroyTexture(audio_texture);
1269 DestroyTexture(trashcan_texture);
1270 DestroyTexture(soundboard_texture);
1271 DestroyTexture(soundboard_levels_texture);
1272 SDLTest_CommonQuit(state);
1273}
1274