1
0

testaudio.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. #include <stdlib.h>
  2. #ifdef __EMSCRIPTEN__
  3. #include <emscripten/emscripten.h>
  4. #endif
  5. #include <SDL3/SDL_test.h>
  6. #include <SDL3/SDL_test_common.h>
  7. #include <SDL3/SDL_main.h>
  8. #include "testutils.h"
  9. #define POOF_LIFETIME 250
  10. #define VISUALIZER_WIDTH 100
  11. #define VISUALIZER_HEIGHT 50
  12. typedef struct Texture
  13. {
  14. SDL_Texture *texture;
  15. float w;
  16. float h;
  17. } Texture;
  18. typedef enum ThingType
  19. {
  20. THING_NULL,
  21. THING_PHYSDEV,
  22. THING_PHYSDEV_CAPTURE,
  23. THING_LOGDEV,
  24. THING_LOGDEV_CAPTURE,
  25. THING_TRASHCAN,
  26. THING_STREAM,
  27. THING_POOF,
  28. THING_WAV
  29. } ThingType;
  30. typedef struct Thing Thing;
  31. struct Thing
  32. {
  33. ThingType what;
  34. union {
  35. struct {
  36. SDL_AudioDeviceID devid;
  37. SDL_bool iscapture;
  38. SDL_AudioSpec spec;
  39. char *name;
  40. } physdev;
  41. struct {
  42. SDL_AudioDeviceID devid;
  43. SDL_bool iscapture;
  44. SDL_AudioSpec spec;
  45. Thing *physdev;
  46. SDL_bool visualizer_enabled;
  47. SDL_bool visualizer_updated;
  48. SDL_Texture *visualizer;
  49. SDL_Mutex *postmix_lock;
  50. float *postmix_buffer;
  51. int postmix_buflen;
  52. int postmix_allocated;
  53. SDL_AudioSpec postmix_spec;
  54. SDL_AtomicInt postmix_updated;
  55. } logdev;
  56. struct {
  57. SDL_AudioSpec spec;
  58. Uint8 *buf;
  59. Uint32 buflen;
  60. } wav;
  61. struct {
  62. float startw;
  63. float starth;
  64. float centerx;
  65. float centery;
  66. } poof;
  67. struct {
  68. SDL_AudioStream *stream;
  69. int total_ticks;
  70. Uint64 next_level_update;
  71. Uint8 levels[5];
  72. } stream;
  73. } data;
  74. Thing *line_connected_to;
  75. char *titlebar;
  76. SDL_FRect rect;
  77. float z;
  78. Uint8 r, g, b, a;
  79. float progress;
  80. float scale;
  81. Uint64 createticks;
  82. Texture *texture;
  83. const ThingType *can_be_dropped_onto;
  84. void (*ontick)(Thing *thing, Uint64 now);
  85. void (*ondrag)(Thing *thing, int button, float x, float y);
  86. void (*ondrop)(Thing *thing, int button, float x, float y);
  87. void (*ondraw)(Thing *thing, SDL_Renderer *renderer);
  88. Thing *prev;
  89. Thing *next;
  90. };
  91. static Uint64 app_ready_ticks = 0;
  92. static int done = 0;
  93. static SDLTest_CommonState *state = NULL;
  94. static Thing *things = NULL;
  95. static char *current_titlebar = NULL;
  96. static Thing *mouseover_thing = NULL;
  97. static Thing *droppable_highlighted_thing = NULL;
  98. static Thing *dragging_thing = NULL;
  99. static int dragging_button = -1;
  100. static Texture *physdev_texture = NULL;
  101. static Texture *logdev_texture = NULL;
  102. static Texture *audio_texture = NULL;
  103. static Texture *trashcan_texture = NULL;
  104. static Texture *soundboard_texture = NULL;
  105. static Texture *soundboard_levels_texture = NULL;
  106. static void DestroyTexture(Texture *tex);
  107. static void DestroyThing(Thing *thing);
  108. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  109. static void Quit(int rc)
  110. {
  111. while (things != NULL) {
  112. DestroyThing(things); /* make sure all the audio devices are closed, etc. */
  113. }
  114. DestroyTexture(physdev_texture);
  115. DestroyTexture(logdev_texture);
  116. DestroyTexture(audio_texture);
  117. DestroyTexture(trashcan_texture);
  118. DestroyTexture(soundboard_texture);
  119. DestroyTexture(soundboard_levels_texture);
  120. SDLTest_CommonQuit(state);
  121. /* Let 'main()' return normally */
  122. if (rc != 0) {
  123. exit(rc);
  124. }
  125. }
  126. static char *xstrdup(const char *str)
  127. {
  128. char *ptr = SDL_strdup(str);
  129. if (!ptr) {
  130. SDL_Log("Out of memory!");
  131. Quit(1);
  132. }
  133. return ptr;
  134. }
  135. static void *xalloc(const size_t len)
  136. {
  137. void *ptr = SDL_calloc(1, len);
  138. if (!ptr) {
  139. SDL_Log("Out of memory!");
  140. Quit(1);
  141. }
  142. return ptr;
  143. }
  144. static void SetTitleBar(const char *fmt, ...)
  145. {
  146. char *newstr = NULL;
  147. va_list ap;
  148. va_start(ap, fmt);
  149. SDL_vasprintf(&newstr, fmt, ap);
  150. va_end(ap);
  151. if (newstr && (!current_titlebar || (SDL_strcmp(current_titlebar, newstr) != 0))) {
  152. SDL_SetWindowTitle(state->windows[0], newstr);
  153. SDL_free(current_titlebar);
  154. current_titlebar = newstr;
  155. } else {
  156. SDL_free(newstr);
  157. }
  158. }
  159. static void SetDefaultTitleBar(void)
  160. {
  161. SetTitleBar("testaudio: %s", SDL_GetCurrentAudioDriver());
  162. }
  163. static Thing *FindThingAtPoint(const float x, const float y)
  164. {
  165. const SDL_FPoint pt = { x, y };
  166. Thing *retval = NULL;
  167. Thing *i;
  168. for (i = things; i != NULL; i = i->next) {
  169. if ((i != dragging_thing) && SDL_PointInRectFloat(&pt, &i->rect)) {
  170. retval = i; /* keep going, though, because things drawn on top are later in the list. */
  171. }
  172. }
  173. return retval;
  174. }
  175. static Thing *UpdateMouseOver(const float x, const float y)
  176. {
  177. Thing *thing;
  178. if (dragging_thing) {
  179. thing = dragging_thing;
  180. } else {
  181. thing = FindThingAtPoint(x, y);
  182. }
  183. if (!thing) {
  184. SetDefaultTitleBar();
  185. } else if (thing->titlebar) {
  186. SetTitleBar("%s", thing->titlebar);
  187. }
  188. return thing;
  189. }
  190. static Thing *CreateThing(ThingType what, float x, float y, float z, float w, float h, Texture *texture, const char *titlebar)
  191. {
  192. Thing *last = NULL;
  193. Thing *i;
  194. Thing *thing;
  195. thing = (Thing *) xalloc(sizeof (Thing));
  196. if ((w < 0) || (h < 0)) {
  197. SDL_assert(texture != NULL);
  198. if (w < 0) {
  199. w = texture->w;
  200. }
  201. if (h < 0) {
  202. h = texture->h;
  203. }
  204. }
  205. thing->what = what;
  206. thing->rect.x = x;
  207. thing->rect.y = y;
  208. thing->rect.w = w;
  209. thing->rect.h = h;
  210. thing->z = z;
  211. thing->r = 255;
  212. thing->g = 255;
  213. thing->b = 255;
  214. thing->a = 255;
  215. thing->scale = 1.0f;
  216. thing->createticks = SDL_GetTicks();
  217. thing->texture = texture;
  218. thing->titlebar = titlebar ? xstrdup(titlebar) : NULL;
  219. /* insert in list by Z order (furthest from the "camera" first, so they get drawn over; negative Z is not drawn at all). */
  220. if (things == NULL) {
  221. things = thing;
  222. return thing;
  223. }
  224. for (i = things; i != NULL; i = i->next) {
  225. if (z > i->z) { /* insert here. */
  226. thing->next = i;
  227. thing->prev = i->prev;
  228. SDL_assert(i->prev == last);
  229. if (i->prev) {
  230. i->prev->next = thing;
  231. } else {
  232. SDL_assert(i == things);
  233. things = thing;
  234. }
  235. i->prev = thing;
  236. return thing;
  237. }
  238. last = i;
  239. }
  240. if (last) {
  241. last->next = thing;
  242. thing->prev = last;
  243. }
  244. return thing;
  245. }
  246. static void DestroyThing(Thing *thing)
  247. {
  248. if (!thing) {
  249. return;
  250. }
  251. if (mouseover_thing == thing) {
  252. mouseover_thing = NULL;
  253. }
  254. if (droppable_highlighted_thing == thing) {
  255. droppable_highlighted_thing = NULL;
  256. }
  257. if (dragging_thing == thing) {
  258. dragging_thing = NULL;
  259. }
  260. switch (thing->what) {
  261. case THING_POOF: break;
  262. case THING_NULL: break;
  263. case THING_TRASHCAN: break;
  264. case THING_LOGDEV:
  265. case THING_LOGDEV_CAPTURE:
  266. SDL_CloseAudioDevice(thing->data.logdev.devid);
  267. SDL_DestroyTexture(thing->data.logdev.visualizer);
  268. SDL_DestroyMutex(thing->data.logdev.postmix_lock);
  269. SDL_free(thing->data.logdev.postmix_buffer);
  270. break;
  271. case THING_PHYSDEV:
  272. case THING_PHYSDEV_CAPTURE:
  273. SDL_free(thing->data.physdev.name);
  274. break;
  275. case THING_WAV:
  276. SDL_free(thing->data.wav.buf);
  277. break;
  278. case THING_STREAM:
  279. SDL_DestroyAudioStream(thing->data.stream.stream);
  280. break;
  281. }
  282. if (thing->prev) {
  283. thing->prev->next = thing->next;
  284. } else {
  285. SDL_assert(thing == things);
  286. things = thing->next;
  287. }
  288. if (thing->next) {
  289. thing->next->prev = thing->prev;
  290. }
  291. SDL_free(thing->titlebar);
  292. SDL_free(thing);
  293. }
  294. static void DrawOneThing(SDL_Renderer *renderer, Thing *thing)
  295. {
  296. SDL_FRect dst;
  297. SDL_memcpy(&dst, &thing->rect, sizeof (SDL_FRect));
  298. if (thing->scale != 1.0f) {
  299. const float centerx = thing->rect.x + (thing->rect.w / 2);
  300. const float centery = thing->rect.y + (thing->rect.h / 2);
  301. SDL_assert(thing->texture != NULL);
  302. dst.w = thing->texture->w * thing->scale;
  303. dst.h = thing->texture->h * thing->scale;
  304. dst.x = centerx - (dst.w / 2);
  305. dst.y = centery - (dst.h / 2);
  306. }
  307. if (thing->texture) {
  308. if (droppable_highlighted_thing == thing) {
  309. SDL_SetRenderDrawColor(renderer, 255, 0, 255, 100);
  310. SDL_RenderFillRect(renderer, &dst);
  311. }
  312. SDL_SetRenderDrawColor(renderer, thing->r, thing->g, thing->b, thing->a);
  313. SDL_RenderTexture(renderer, thing->texture->texture, NULL, &dst);
  314. } else {
  315. SDL_SetRenderDrawColor(renderer, thing->r, thing->g, thing->b, thing->a);
  316. SDL_RenderFillRect(renderer, &dst);
  317. }
  318. if (thing->ondraw) {
  319. thing->ondraw(thing, renderer);
  320. }
  321. if (thing->progress > 0.0f) {
  322. SDL_FRect r = { thing->rect.x, thing->rect.y + (thing->rect.h + 2.0f), 0.0f, 10.0f };
  323. r.w = thing->rect.w * ((thing->progress > 1.0f) ? 1.0f : thing->progress);
  324. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 128);
  325. SDL_RenderFillRect(renderer, &r);
  326. }
  327. }
  328. static void DrawThings(SDL_Renderer *renderer)
  329. {
  330. Thing *i;
  331. /* draw connecting lines first, so they're behind everything else. */
  332. for (i = things; i && (i->z >= 0.0f); i = i->next) {
  333. Thing *dst = i->line_connected_to;
  334. if (dst) {
  335. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  336. 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));
  337. }
  338. }
  339. /* Draw the actual things. */
  340. for (i = things; i && (i->z >= 0.0f); i = i->next) {
  341. if (i != dragging_thing) {
  342. DrawOneThing(renderer, i);
  343. }
  344. }
  345. if (dragging_thing) {
  346. DrawOneThing(renderer, dragging_thing); /* draw last so it's always on top. */
  347. }
  348. }
  349. static void Draw(void)
  350. {
  351. SDL_Renderer *renderer = state->renderers[0];
  352. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  353. SDL_SetRenderDrawColor(renderer, 64, 0, 64, 255);
  354. SDL_RenderClear(renderer);
  355. DrawThings(renderer);
  356. SDL_RenderPresent(renderer);
  357. }
  358. static void RepositionRowOfThings(const ThingType what, const float y)
  359. {
  360. int total_things = 0;
  361. float texw = 0.0f;
  362. float texh = 0.0f;
  363. Thing *i;
  364. for (i = things; i != NULL; i = i->next) {
  365. if (i->what == what) {
  366. texw = i->rect.w;
  367. texh = i->rect.h;
  368. total_things++;
  369. }
  370. }
  371. if (total_things > 0) {
  372. int w, h;
  373. SDL_GetWindowSize(state->windows[0], &w, &h);
  374. const float spacing = w / ((float) total_things);
  375. float x = (spacing - texw) / 2.0f;
  376. for (i = things; i != NULL; i = i->next) {
  377. if (i->what == what) {
  378. i->rect.x = x;
  379. i->rect.y = (y >= 0.0f) ? y : ((h + y) - texh);
  380. x += spacing;
  381. }
  382. }
  383. }
  384. }
  385. static const char *AudioFmtToString(const SDL_AudioFormat fmt)
  386. {
  387. switch (fmt) {
  388. #define FMTCASE(x) case SDL_AUDIO_##x: return #x
  389. FMTCASE(U8);
  390. FMTCASE(S8);
  391. FMTCASE(S16LE);
  392. FMTCASE(S16BE);
  393. FMTCASE(S32LE);
  394. FMTCASE(S32BE);
  395. FMTCASE(F32LE);
  396. FMTCASE(F32BE);
  397. #undef FMTCASE
  398. }
  399. return "?";
  400. }
  401. static const char *AudioChansToStr(const int channels)
  402. {
  403. switch (channels) {
  404. case 1: return "mono";
  405. case 2: return "stereo";
  406. case 3: return "2.1";
  407. case 4: return "quad";
  408. case 5: return "4.1";
  409. case 6: return "5.1";
  410. case 7: return "6.1";
  411. case 8: return "7.1";
  412. default: break;
  413. }
  414. return "?";
  415. }
  416. static void PoofThing_ondrag(Thing *thing, int button, float x, float y)
  417. {
  418. dragging_thing = NULL; /* refuse to be dragged. */
  419. }
  420. static void PoofThing_ontick(Thing *thing, Uint64 now)
  421. {
  422. const int lifetime = POOF_LIFETIME;
  423. const int elasped = (int) (now - thing->createticks);
  424. if (elasped > lifetime) {
  425. DestroyThing(thing);
  426. } else {
  427. const float pct = ((float) elasped) / ((float) lifetime);
  428. thing->a = (Uint8) (int) (255.0f - (pct * 255.0f));
  429. thing->scale = 1.0f - pct; /* shrink to nothing! */
  430. }
  431. }
  432. static Thing *CreatePoofThing(Thing *poofing_thing)
  433. {
  434. const float centerx = poofing_thing->rect.x + (poofing_thing->rect.w / 2);
  435. const float centery = poofing_thing->rect.y + (poofing_thing->rect.h / 2);
  436. const float z = poofing_thing->z;
  437. 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);
  438. thing->data.poof.startw = poofing_thing->rect.w;
  439. thing->data.poof.starth = poofing_thing->rect.h;
  440. thing->data.poof.centerx = centerx;
  441. thing->data.poof.centery = centery;
  442. thing->ontick = PoofThing_ontick;
  443. thing->ondrag = PoofThing_ondrag;
  444. return thing;
  445. }
  446. static void DestroyThingInPoof(Thing *thing)
  447. {
  448. if (thing) {
  449. if (thing->what != THING_POOF) {
  450. CreatePoofThing(thing);
  451. }
  452. DestroyThing(thing);
  453. }
  454. }
  455. /* this poofs a thing and additionally poofs all things connected to the thing. */
  456. static void TrashThing(Thing *thing)
  457. {
  458. Thing *i, *next;
  459. for (i = things; i != NULL; i = next) {
  460. next = i->next;
  461. if (i->line_connected_to == thing) {
  462. TrashThing(i);
  463. next = things; /* start over in case this blew up the list. */
  464. }
  465. }
  466. DestroyThingInPoof(thing);
  467. }
  468. static void StreamThing_ontick(Thing *thing, Uint64 now)
  469. {
  470. if (!thing->line_connected_to) {
  471. return;
  472. }
  473. /* are we playing? See if we're done, or update state. */
  474. if (thing->line_connected_to->what == THING_LOGDEV) {
  475. const int available = SDL_GetAudioStreamAvailable(thing->data.stream.stream);
  476. SDL_AudioSpec spec;
  477. if (!available || (SDL_GetAudioStreamFormat(thing->data.stream.stream, NULL, &spec) < 0)) {
  478. DestroyThingInPoof(thing);
  479. } else {
  480. const int ticksleft = (int) ((((Uint64) (available / SDL_AUDIO_FRAMESIZE(spec))) * 1000) / spec.freq);
  481. const float pct = thing->data.stream.total_ticks ? (((float) (ticksleft)) / ((float) thing->data.stream.total_ticks)) : 0.0f;
  482. thing->progress = 1.0f - pct;
  483. }
  484. }
  485. if (thing->data.stream.next_level_update <= now) {
  486. Uint64 perf = SDL_GetPerformanceCounter();
  487. int i;
  488. for (i = 0; i < SDL_arraysize(thing->data.stream.levels); i++) {
  489. thing->data.stream.levels[i] = (Uint8) (perf % 6);
  490. perf >>= 3;
  491. }
  492. thing->data.stream.next_level_update += 150;
  493. }
  494. }
  495. static void StreamThing_ondrag(Thing *thing, int button, float x, float y)
  496. {
  497. if (button == SDL_BUTTON_RIGHT) { /* this is kinda hacky, but use this to disconnect from a playing source. */
  498. if (thing->line_connected_to) {
  499. SDL_UnbindAudioStream(thing->data.stream.stream); /* unbind from current device */
  500. thing->line_connected_to = NULL;
  501. }
  502. }
  503. }
  504. static void StreamThing_ondrop(Thing *thing, int button, float x, float y)
  505. {
  506. if (droppable_highlighted_thing) {
  507. if (droppable_highlighted_thing->what == THING_TRASHCAN) {
  508. TrashThing(thing);
  509. } else if (((droppable_highlighted_thing->what == THING_LOGDEV) || (droppable_highlighted_thing->what == THING_LOGDEV_CAPTURE)) && (droppable_highlighted_thing != thing->line_connected_to)) {
  510. /* connect to a logical device! */
  511. SDL_Log("Binding audio stream ('%s') to logical device %u", thing->titlebar, (unsigned int) droppable_highlighted_thing->data.logdev.devid);
  512. if (thing->line_connected_to) {
  513. const SDL_AudioSpec *spec = &droppable_highlighted_thing->data.logdev.spec;
  514. SDL_UnbindAudioStream(thing->data.stream.stream); /* unbind from current device */
  515. if (thing->line_connected_to->what == THING_LOGDEV_CAPTURE) {
  516. SDL_FlushAudioStream(thing->data.stream.stream);
  517. thing->data.stream.total_ticks = (int) ((((Uint64) (SDL_GetAudioStreamAvailable(thing->data.stream.stream) / SDL_AUDIO_FRAMESIZE(*spec))) * 1000) / spec->freq);
  518. }
  519. }
  520. SDL_BindAudioStream(droppable_highlighted_thing->data.logdev.devid, thing->data.stream.stream); /* bind to new device! */
  521. thing->progress = 0.0f; /* ontick will adjust this if we're on an output device.*/
  522. thing->data.stream.next_level_update = SDL_GetTicks() + 100;
  523. thing->line_connected_to = droppable_highlighted_thing;
  524. }
  525. }
  526. }
  527. static void StreamThing_ondraw(Thing *thing, SDL_Renderer *renderer)
  528. {
  529. if (thing->line_connected_to) { /* are we playing? Update progress bar, and bounce the levels a little. */
  530. static const float xlocs[5] = { 18, 39, 59, 79, 99 };
  531. static const float ylocs[5] = { 49, 39, 29, 19, 10 };
  532. const float blockw = soundboard_levels_texture->w;
  533. const float blockh = soundboard_levels_texture->h / 5.0f;
  534. int i, j;
  535. SDL_SetRenderDrawColor(renderer, thing->r, thing->g, thing->b, thing->a);
  536. for (i = 0; i < SDL_arraysize(thing->data.stream.levels); i++) {
  537. const int level = (int) thing->data.stream.levels[i];
  538. const float x = xlocs[i];
  539. for (j = 0; j < level; j++) {
  540. const SDL_FRect src = { 0, soundboard_levels_texture->h - ((j+1) * blockh), blockw, blockh };
  541. const SDL_FRect dst = { thing->rect.x + x, thing->rect.y + ylocs[j], blockw, blockh };
  542. SDL_RenderTexture(renderer, soundboard_levels_texture->texture, &src, &dst);
  543. }
  544. }
  545. }
  546. }
  547. static Thing *CreateStreamThing(const SDL_AudioSpec *spec, const Uint8 *buf, const Uint32 buflen, const char *fname, const float x, const float y)
  548. {
  549. static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_LOGDEV, THING_LOGDEV_CAPTURE, THING_NULL };
  550. Thing *thing = CreateThing(THING_STREAM, x, y, 0, -1, -1, soundboard_texture, fname);
  551. SDL_Log("Adding audio stream for %s", fname ? fname : "(null)");
  552. thing->data.stream.stream = SDL_CreateAudioStream(spec, spec);
  553. if (buf && buflen) {
  554. SDL_PutAudioStreamData(thing->data.stream.stream, buf, (int) buflen);
  555. SDL_FlushAudioStream(thing->data.stream.stream);
  556. thing->data.stream.total_ticks = (int) ((((Uint64) (SDL_GetAudioStreamAvailable(thing->data.stream.stream) / SDL_AUDIO_FRAMESIZE(*spec))) * 1000) / spec->freq);
  557. }
  558. thing->ontick = StreamThing_ontick;
  559. thing->ondrag = StreamThing_ondrag;
  560. thing->ondrop = StreamThing_ondrop;
  561. thing->ondraw = StreamThing_ondraw;
  562. thing->can_be_dropped_onto = can_be_dropped_onto;
  563. return thing;
  564. }
  565. static void WavThing_ondrag(Thing *thing, int button, float x, float y)
  566. {
  567. if (button == SDL_BUTTON_RIGHT) { /* drag out a new audio stream. */
  568. 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));
  569. }
  570. }
  571. static void WavThing_ondrop(Thing *thing, int button, float x, float y)
  572. {
  573. if (droppable_highlighted_thing) {
  574. if (droppable_highlighted_thing->what == THING_TRASHCAN) {
  575. TrashThing(thing);
  576. }
  577. }
  578. }
  579. static Thing *LoadWavThing(const char *fname, float x, float y)
  580. {
  581. Thing *thing = NULL;
  582. char *path;
  583. SDL_AudioSpec spec;
  584. Uint8 *buf = NULL;
  585. Uint32 buflen = 0;
  586. path = GetNearbyFilename(fname);
  587. if (path) {
  588. fname = path;
  589. }
  590. if (SDL_LoadWAV(fname, &spec, &buf, &buflen) == 0) {
  591. static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
  592. char *titlebar = NULL;
  593. const char *nodirs = SDL_strrchr(fname, '/');
  594. #ifdef __WINDOWS__
  595. const char *nodirs2 = SDL_strrchr(nodirs ? nodirs : fname, '\\');
  596. if (nodirs2) {
  597. nodirs = nodirs2;
  598. }
  599. #endif
  600. SDL_Log("Adding WAV file '%s'", fname);
  601. if (nodirs) {
  602. nodirs++;
  603. } else {
  604. nodirs = fname;
  605. }
  606. SDL_asprintf(&titlebar, "WAV file (\"%s\", %s, %s, %uHz)", nodirs, AudioFmtToString(spec.format), AudioChansToStr(spec.channels), (unsigned int) spec.freq);
  607. thing = CreateThing(THING_WAV, x - (audio_texture->w / 2), y - (audio_texture->h / 2), 5, -1, -1, audio_texture, titlebar);
  608. SDL_free(titlebar);
  609. SDL_memcpy(&thing->data.wav.spec, &spec, sizeof (SDL_AudioSpec));
  610. thing->data.wav.buf = buf;
  611. thing->data.wav.buflen = buflen;
  612. thing->can_be_dropped_onto = can_be_dropped_onto;
  613. thing->ondrag = WavThing_ondrag;
  614. thing->ondrop = WavThing_ondrop;
  615. }
  616. SDL_free(path);
  617. return thing;
  618. }
  619. static Thing *LoadStockWavThing(const char *fname)
  620. {
  621. char *path = GetNearbyFilename(fname);
  622. Thing *thing = LoadWavThing(path ? path : fname, 0.0f, 0.0f); /* will reposition in a moment. */
  623. SDL_free(path);
  624. return thing;
  625. }
  626. static void LoadStockWavThings(void)
  627. {
  628. LoadStockWavThing("sample.wav");
  629. RepositionRowOfThings(THING_WAV, -10.0f);
  630. }
  631. static void DestroyTexture(Texture *tex)
  632. {
  633. if (tex) {
  634. SDL_DestroyTexture(tex->texture);
  635. SDL_free(tex);
  636. }
  637. }
  638. static Texture *CreateTexture(const char *fname)
  639. {
  640. Texture *tex = (Texture *) xalloc(sizeof (Texture));
  641. int texw, texh;
  642. tex->texture = LoadTexture(state->renderers[0], fname, SDL_TRUE, &texw, &texh);
  643. if (!tex->texture) {
  644. SDL_Log("Failed to load '%s': %s", fname, SDL_GetError());
  645. SDL_free(tex);
  646. Quit(1);
  647. }
  648. SDL_SetTextureBlendMode(tex->texture, SDL_BLENDMODE_BLEND);
  649. tex->w = (float) texw;
  650. tex->h = (float) texh;
  651. return tex;
  652. }
  653. static Thing *CreateLogicalDeviceThing(Thing *parent, const SDL_AudioDeviceID which, const float x, const float y);
  654. static void DeviceThing_ondrag(Thing *thing, int button, float x, float y)
  655. {
  656. if ((button == SDL_BUTTON_MIDDLE) && (thing->what == THING_LOGDEV_CAPTURE)) { /* drag out a new stream. This is a UX mess. :/ */
  657. dragging_thing = CreateStreamThing(&thing->data.logdev.spec, NULL, 0, NULL, x, y);
  658. dragging_thing->data.stream.next_level_update = SDL_GetTicks() + 100;
  659. SDL_BindAudioStream(thing->data.logdev.devid, dragging_thing->data.stream.stream); /* bind to new device! */
  660. dragging_thing->line_connected_to = thing;
  661. } else if (button == SDL_BUTTON_RIGHT) { /* drag out a new logical device. */
  662. const SDL_AudioDeviceID which = ((thing->what == THING_LOGDEV) || (thing->what == THING_LOGDEV_CAPTURE)) ? thing->data.logdev.devid : thing->data.physdev.devid;
  663. const SDL_AudioDeviceID devid = SDL_OpenAudioDevice(which, NULL);
  664. dragging_thing = devid ? CreateLogicalDeviceThing(thing, devid, x - (thing->rect.w / 2), y - (thing->rect.h / 2)) : NULL;
  665. }
  666. }
  667. static void SetLogicalDeviceTitlebar(Thing *thing)
  668. {
  669. SDL_AudioSpec *spec = &thing->data.logdev.spec;
  670. SDL_GetAudioDeviceFormat(thing->data.logdev.devid, spec);
  671. SDL_free(thing->titlebar);
  672. SDL_asprintf(&thing->titlebar, "Logical device #%u (%s, %s, %s, %uHz)", (unsigned int) thing->data.logdev.devid, thing->data.logdev.iscapture ? "CAPTURE" : "OUTPUT", AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq);
  673. }
  674. static void LogicalDeviceThing_ondrop(Thing *thing, int button, float x, float y)
  675. {
  676. if (droppable_highlighted_thing) {
  677. if (droppable_highlighted_thing->what == THING_TRASHCAN) {
  678. TrashThing(thing);
  679. }
  680. }
  681. }
  682. static void SDLCALL PostmixCallback(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen)
  683. {
  684. Thing *thing = (Thing *) userdata;
  685. SDL_LockMutex(thing->data.logdev.postmix_lock);
  686. if (thing->data.logdev.postmix_allocated < buflen) {
  687. void *ptr = SDL_realloc(thing->data.logdev.postmix_buffer, buflen);
  688. if (!ptr) {
  689. SDL_UnlockMutex(thing->data.logdev.postmix_lock);
  690. return; /* oh well. */
  691. }
  692. thing->data.logdev.postmix_buffer = (float *) ptr;
  693. thing->data.logdev.postmix_allocated = buflen;
  694. }
  695. SDL_copyp(&thing->data.logdev.postmix_spec, spec);
  696. SDL_memcpy(thing->data.logdev.postmix_buffer, buffer, buflen);
  697. thing->data.logdev.postmix_buflen = buflen;
  698. SDL_AtomicSet(&thing->data.logdev.postmix_updated, 1);
  699. SDL_UnlockMutex(thing->data.logdev.postmix_lock);
  700. }
  701. static void UpdateVisualizer(SDL_Renderer *renderer, SDL_Texture *visualizer, const int channels, const float *buffer, const int buflen)
  702. {
  703. static const SDL_Color channel_colors[8] = {
  704. { 255, 255, 255, 255 },
  705. { 255, 0, 0, 255 },
  706. { 0, 255, 0, 255 },
  707. { 0, 0, 255, 255 },
  708. { 255, 255, 0, 255 },
  709. { 0, 255, 255, 255 },
  710. { 255, 0, 255, 255 },
  711. { 127, 127, 127, 255 }
  712. };
  713. SDL_SetRenderTarget(renderer, visualizer);
  714. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  715. SDL_RenderClear(renderer);
  716. if (buffer && buflen) {
  717. const int frames = (buflen / sizeof (float)) / channels;
  718. const int skip = frames / (VISUALIZER_WIDTH * 2);
  719. int i, j;
  720. for (i = channels - 1; i >= 0; i--) {
  721. const SDL_Color *color = &channel_colors[i % SDL_arraysize(channel_colors)];
  722. SDL_FPoint points[VISUALIZER_WIDTH + 2];
  723. float prevx = 0.0f;
  724. int pointidx = 1;
  725. points[0].x = 0.0f;
  726. points[0].y = VISUALIZER_HEIGHT * 0.5f;
  727. for (j = 0; j < (SDL_arraysize(points)-1); j++) {
  728. const float val = buffer[((j * skip) * channels) + i];
  729. const float x = prevx + 2;
  730. const float y = (VISUALIZER_HEIGHT * 0.5f) - (VISUALIZER_HEIGHT * (val * 0.5f));
  731. SDL_assert(pointidx < SDL_arraysize(points));
  732. points[pointidx].x = x;
  733. points[pointidx].y = y;
  734. pointidx++;
  735. prevx = x;
  736. }
  737. SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, 255);
  738. SDL_RenderLines(renderer, points, pointidx);
  739. }
  740. }
  741. SDL_SetRenderTarget(renderer, NULL);
  742. }
  743. static void LogicalDeviceThing_ontick(Thing *thing, Uint64 now)
  744. {
  745. const SDL_bool ismousedover = (thing == mouseover_thing) ? SDL_TRUE : SDL_FALSE;
  746. if (!thing->data.logdev.visualizer || !thing->data.logdev.postmix_lock) { /* need these to work, skip if they failed. */
  747. return;
  748. }
  749. if (thing->data.logdev.visualizer_enabled != ismousedover) {
  750. thing->data.logdev.visualizer_enabled = ismousedover;
  751. if (!ismousedover) {
  752. SDL_SetAudioPostmixCallback(thing->data.logdev.devid, NULL, NULL);
  753. } else {
  754. if (thing->data.logdev.postmix_buffer) {
  755. SDL_memset(thing->data.logdev.postmix_buffer, '\0', thing->data.logdev.postmix_buflen);
  756. }
  757. SDL_AtomicSet(&thing->data.logdev.postmix_updated, 1); /* so this will at least clear the texture later. */
  758. SDL_SetAudioPostmixCallback(thing->data.logdev.devid, PostmixCallback, thing);
  759. }
  760. }
  761. }
  762. static void LogicalDeviceThing_ondraw(Thing *thing, SDL_Renderer *renderer)
  763. {
  764. if (thing->data.logdev.visualizer_enabled) {
  765. SDL_FRect dst;
  766. dst.w = thing->rect.w;
  767. dst.h = thing->rect.h;
  768. dst.x = thing->rect.x + ((thing->rect.w - dst.w) / 2);
  769. dst.y = thing->rect.y + ((thing->rect.h - dst.h) / 2);
  770. if (SDL_AtomicGet(&thing->data.logdev.postmix_updated)) {
  771. float *buffer;
  772. int channels;
  773. int buflen;
  774. SDL_LockMutex(thing->data.logdev.postmix_lock);
  775. channels = thing->data.logdev.postmix_spec.channels;
  776. buflen = thing->data.logdev.postmix_buflen;
  777. buffer = (float *) SDL_malloc(thing->data.logdev.postmix_buflen);
  778. if (buffer) {
  779. SDL_memcpy(buffer, thing->data.logdev.postmix_buffer, thing->data.logdev.postmix_buflen);
  780. SDL_AtomicSet(&thing->data.logdev.postmix_updated, 0);
  781. }
  782. SDL_UnlockMutex(thing->data.logdev.postmix_lock);
  783. UpdateVisualizer(renderer, thing->data.logdev.visualizer, channels, buffer, buflen);
  784. SDL_free(buffer);
  785. }
  786. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 30);
  787. SDL_RenderTexture(renderer, thing->data.logdev.visualizer, NULL, &dst);
  788. }
  789. }
  790. static Thing *CreateLogicalDeviceThing(Thing *parent, const SDL_AudioDeviceID which, const float x, const float y)
  791. {
  792. static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
  793. Thing *physthing = ((parent->what == THING_LOGDEV) || (parent->what == THING_LOGDEV_CAPTURE)) ? parent->data.logdev.physdev : parent;
  794. const SDL_bool iscapture = physthing->data.physdev.iscapture;
  795. Thing *thing;
  796. SDL_Log("Adding logical audio device %u", (unsigned int) which);
  797. thing = CreateThing(iscapture ? THING_LOGDEV_CAPTURE : THING_LOGDEV, x, y, 5, -1, -1, logdev_texture, NULL);
  798. thing->data.logdev.devid = which;
  799. thing->data.logdev.iscapture = iscapture;
  800. thing->data.logdev.physdev = physthing;
  801. thing->data.logdev.visualizer = SDL_CreateTexture(state->renderers[0], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, VISUALIZER_WIDTH, VISUALIZER_HEIGHT);
  802. thing->data.logdev.postmix_lock = SDL_CreateMutex();
  803. if (thing->data.logdev.visualizer) {
  804. SDL_SetTextureBlendMode(thing->data.logdev.visualizer, SDL_BLENDMODE_BLEND);
  805. }
  806. thing->line_connected_to = physthing;
  807. thing->ontick = LogicalDeviceThing_ontick;
  808. thing->ondrag = DeviceThing_ondrag;
  809. thing->ondrop = LogicalDeviceThing_ondrop;
  810. thing->ondraw = LogicalDeviceThing_ondraw;
  811. thing->can_be_dropped_onto = can_be_dropped_onto;
  812. SetLogicalDeviceTitlebar(thing);
  813. return thing;
  814. }
  815. static void SetPhysicalDeviceTitlebar(Thing *thing)
  816. {
  817. SDL_AudioSpec *spec = &thing->data.physdev.spec;
  818. SDL_GetAudioDeviceFormat(thing->data.physdev.devid, spec);
  819. SDL_free(thing->titlebar);
  820. if (thing->data.physdev.devid == SDL_AUDIO_DEVICE_DEFAULT_CAPTURE) {
  821. SDL_asprintf(&thing->titlebar, "Default system device (CAPTURE, %s, %s, %uHz)", AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq);
  822. } else if (thing->data.physdev.devid == SDL_AUDIO_DEVICE_DEFAULT_OUTPUT) {
  823. SDL_asprintf(&thing->titlebar, "Default system device (OUTPUT, %s, %s, %uHz)", AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq);
  824. } else {
  825. SDL_asprintf(&thing->titlebar, "Physical device #%u (%s, \"%s\", %s, %s, %uHz)", (unsigned int) thing->data.physdev.devid, thing->data.physdev.iscapture ? "CAPTURE" : "OUTPUT", thing->data.physdev.name, AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq);
  826. }
  827. }
  828. static void PhysicalDeviceThing_ondrop(Thing *thing, int button, float x, float y)
  829. {
  830. if (droppable_highlighted_thing) {
  831. if (droppable_highlighted_thing->what == THING_TRASHCAN) {
  832. TrashThing(thing);
  833. }
  834. }
  835. }
  836. static void PhysicalDeviceThing_ontick(Thing *thing, Uint64 now)
  837. {
  838. const int lifetime = POOF_LIFETIME;
  839. const int elasped = (int) (now - thing->createticks);
  840. if (elasped > lifetime) {
  841. thing->scale = 1.0f;
  842. thing->a = 255;
  843. thing->ontick = NULL; /* no more ticking. */
  844. } else {
  845. const float pct = ((float) elasped) / ((float) lifetime);
  846. thing->a = (Uint8) (int) (pct * 255.0f);
  847. thing->scale = pct; /* grow to normal size */
  848. }
  849. }
  850. static Thing *CreatePhysicalDeviceThing(const SDL_AudioDeviceID which, const SDL_bool iscapture)
  851. {
  852. static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
  853. static float next_physdev_x = 0;
  854. Thing *thing;
  855. int winw, winh;
  856. SDL_GetWindowSize(state->windows[0], &winw, &winh);
  857. if (next_physdev_x > (winw-physdev_texture->w)) {
  858. next_physdev_x = 0;
  859. }
  860. SDL_Log("Adding physical audio device %u", (unsigned int) which);
  861. thing = CreateThing(iscapture ? THING_PHYSDEV_CAPTURE : THING_PHYSDEV, next_physdev_x, 170, 5, -1, -1, physdev_texture, NULL);
  862. thing->data.physdev.devid = which;
  863. thing->data.physdev.iscapture = iscapture;
  864. thing->data.physdev.name = SDL_GetAudioDeviceName(which);
  865. thing->ondrag = DeviceThing_ondrag;
  866. thing->ondrop = PhysicalDeviceThing_ondrop;
  867. thing->ontick = PhysicalDeviceThing_ontick;
  868. thing->can_be_dropped_onto = can_be_dropped_onto;
  869. SetPhysicalDeviceTitlebar(thing);
  870. if (SDL_GetTicks() <= (app_ready_ticks + 2000)) { /* assume this is the initial batch if it happens in the first two seconds. */
  871. RepositionRowOfThings(THING_PHYSDEV, 10.0f); /* don't rearrange them after the initial add. */
  872. RepositionRowOfThings(THING_PHYSDEV_CAPTURE, 170.0f); /* don't rearrange them after the initial add. */
  873. next_physdev_x = 0.0f;
  874. } else {
  875. next_physdev_x += physdev_texture->w * 1.5f;
  876. }
  877. return thing;
  878. }
  879. static Thing *CreateTrashcanThing(void)
  880. {
  881. int winw, winh;
  882. SDL_GetWindowSize(state->windows[0], &winw, &winh);
  883. return CreateThing(THING_TRASHCAN, winw - trashcan_texture->w, winh - trashcan_texture->h, 10, -1, -1, trashcan_texture, "Drag things here to remove them.");
  884. }
  885. static Thing *CreateDefaultPhysicalDevice(const SDL_bool iscapture)
  886. {
  887. return CreatePhysicalDeviceThing(iscapture ? SDL_AUDIO_DEVICE_DEFAULT_CAPTURE : SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, iscapture);
  888. }
  889. static void TickThings(void)
  890. {
  891. Thing *i;
  892. Thing *next;
  893. const Uint64 now = SDL_GetTicks();
  894. for (i = things; i != NULL; i = next) {
  895. next = i->next; /* in case this deletes itself. */
  896. if (i->ontick) {
  897. i->ontick(i, now);
  898. }
  899. }
  900. }
  901. static void WindowResized(const int newwinw, const int newwinh)
  902. {
  903. Thing *i;
  904. const float neww = (float) newwinw;
  905. const float newh = (float) newwinh;
  906. const float oldw = (float) state->window_w;
  907. const float oldh = (float) state->window_h;
  908. for (i = things; i != NULL; i = i->next) {
  909. const float halfw = i->rect.w / 2.0f;
  910. const float halfh = i->rect.h / 2.0f;
  911. const float x = (i->rect.x + halfw) / oldw;
  912. const float y = (i->rect.y + halfh) / oldh;
  913. i->rect.x = (x * neww) - halfw;
  914. i->rect.y = (y * newh) - halfh;
  915. }
  916. state->window_w = newwinw;
  917. state->window_h = newwinh;
  918. }
  919. static void Loop(void)
  920. {
  921. SDL_Event event;
  922. SDL_bool saw_event = SDL_FALSE;
  923. if (app_ready_ticks == 0) {
  924. app_ready_ticks = SDL_GetTicks();
  925. }
  926. while (SDL_PollEvent(&event)) {
  927. Thing *thing = NULL;
  928. saw_event = SDL_TRUE;
  929. switch (event.type) {
  930. case SDL_EVENT_MOUSE_MOTION:
  931. thing = UpdateMouseOver(event.motion.x, event.motion.y);
  932. if ((dragging_button == -1) && event.motion.state) {
  933. if (event.motion.state & SDL_BUTTON_LMASK) {
  934. dragging_button = SDL_BUTTON_LEFT;
  935. } else if (event.motion.state & SDL_BUTTON_RMASK) {
  936. dragging_button = SDL_BUTTON_RIGHT;
  937. } else if (event.motion.state & SDL_BUTTON_MMASK) {
  938. dragging_button = SDL_BUTTON_MIDDLE;
  939. }
  940. if (dragging_button != -1) {
  941. dragging_thing = thing;
  942. if (thing && thing->ondrag) {
  943. thing->ondrag(thing, dragging_button, event.motion.x, event.motion.y);
  944. }
  945. }
  946. }
  947. droppable_highlighted_thing = NULL;
  948. if (dragging_thing) {
  949. dragging_thing->rect.x = event.motion.x - (dragging_thing->rect.w / 2);
  950. dragging_thing->rect.y = event.motion.y - (dragging_thing->rect.h / 2);
  951. if (dragging_thing->can_be_dropped_onto) {
  952. thing = FindThingAtPoint(event.motion.x, event.motion.y);
  953. if (thing) {
  954. int i;
  955. for (i = 0; dragging_thing->can_be_dropped_onto[i]; i++) {
  956. if (dragging_thing->can_be_dropped_onto[i] == thing->what) {
  957. droppable_highlighted_thing = thing;
  958. break;
  959. }
  960. }
  961. }
  962. }
  963. }
  964. break;
  965. case SDL_EVENT_MOUSE_BUTTON_DOWN:
  966. thing = UpdateMouseOver(event.button.x, event.button.y);
  967. break;
  968. case SDL_EVENT_MOUSE_BUTTON_UP:
  969. if (dragging_button == event.button.button) {
  970. Thing *dropped_thing = dragging_thing;
  971. dragging_thing = NULL;
  972. dragging_button = -1;
  973. if (dropped_thing && dropped_thing->ondrop) {
  974. dropped_thing->ondrop(dropped_thing, event.button.button, event.button.x, event.button.y);
  975. }
  976. droppable_highlighted_thing = NULL;
  977. }
  978. thing = UpdateMouseOver(event.button.x, event.button.y);
  979. break;
  980. case SDL_EVENT_MOUSE_WHEEL:
  981. UpdateMouseOver(event.wheel.mouseX, event.wheel.mouseY);
  982. break;
  983. case SDL_EVENT_DROP_FILE:
  984. SDL_Log("Drop file! '%s'", event.drop.file);
  985. LoadWavThing(event.drop.file, event.drop.x, event.drop.y);
  986. /* SDLTest_CommonEvent will free the string, below. */
  987. break;
  988. case SDL_EVENT_WINDOW_RESIZED:
  989. WindowResized(event.window.data1, event.window.data2);
  990. break;
  991. case SDL_EVENT_AUDIO_DEVICE_ADDED:
  992. CreatePhysicalDeviceThing(event.adevice.which, event.adevice.iscapture);
  993. break;
  994. case SDL_EVENT_AUDIO_DEVICE_REMOVED: {
  995. const SDL_AudioDeviceID which = event.adevice.which;
  996. Thing *i, *next;
  997. SDL_Log("Removing audio device %u", (unsigned int) which);
  998. for (i = things; i != NULL; i = next) {
  999. next = i->next;
  1000. if (((i->what == THING_PHYSDEV) || (i->what == THING_PHYSDEV_CAPTURE)) && (i->data.physdev.devid == which)) {
  1001. TrashThing(i);
  1002. next = things; /* in case we mangled the list. */
  1003. } else if (((i->what == THING_LOGDEV) || (i->what == THING_LOGDEV_CAPTURE)) && (i->data.logdev.devid == which)) {
  1004. TrashThing(i);
  1005. next = things; /* in case we mangled the list. */
  1006. }
  1007. }
  1008. break;
  1009. }
  1010. default: break;
  1011. }
  1012. SDLTest_CommonEvent(state, &event, &done);
  1013. }
  1014. TickThings();
  1015. Draw();
  1016. if (!saw_event) {
  1017. SDL_Delay(10);
  1018. }
  1019. #ifdef __EMSCRIPTEN__
  1020. if (done) {
  1021. emscripten_cancel_main_loop();
  1022. }
  1023. #endif
  1024. }
  1025. int main(int argc, char *argv[])
  1026. {
  1027. int i;
  1028. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO | SDL_INIT_AUDIO);
  1029. if (state == NULL) {
  1030. Quit(1);
  1031. }
  1032. state->window_flags |= SDL_WINDOW_RESIZABLE;
  1033. for (i = 1; i < argc;) {
  1034. int consumed = SDLTest_CommonArg(state, i);
  1035. if (consumed == 0) {
  1036. consumed = -1;
  1037. /* add our own command lines here. */
  1038. }
  1039. if (consumed < 0) {
  1040. static const char *options[] = {
  1041. /* add our own command lines here. */
  1042. /*"[--blend none|blend|add|mod|mul|sub]",*/
  1043. NULL
  1044. };
  1045. SDLTest_CommonLogUsage(state, argv[0], options);
  1046. Quit(1);
  1047. }
  1048. i += consumed;
  1049. }
  1050. if (!SDLTest_CommonInit(state)) {
  1051. Quit(2);
  1052. }
  1053. if (state->audio_id) {
  1054. SDL_CloseAudioDevice(state->audio_id);
  1055. state->audio_id = 0;
  1056. }
  1057. SetDefaultTitleBar();
  1058. physdev_texture = CreateTexture("physaudiodev.bmp");
  1059. logdev_texture = CreateTexture("logaudiodev.bmp");
  1060. audio_texture = CreateTexture("audiofile.bmp");
  1061. trashcan_texture = CreateTexture("trashcan.bmp");
  1062. soundboard_texture = CreateTexture("soundboard.bmp");
  1063. soundboard_levels_texture = CreateTexture("soundboard_levels.bmp");
  1064. LoadStockWavThings();
  1065. CreateTrashcanThing();
  1066. CreateDefaultPhysicalDevice(SDL_FALSE);
  1067. CreateDefaultPhysicalDevice(SDL_TRUE);
  1068. #ifdef __EMSCRIPTEN__
  1069. emscripten_set_main_loop(Loop, 0, 1);
  1070. #else
  1071. while (!done) {
  1072. Loop();
  1073. }
  1074. #endif
  1075. Quit(0);
  1076. return 0;
  1077. }