testaudio.c 42 KB

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