testaudio.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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. int frames = 0;
  671. SDL_GetAudioDeviceFormat(thing->data.logdev.devid, spec, &frames);
  672. SDL_free(thing->titlebar);
  673. 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);
  674. }
  675. static void LogicalDeviceThing_ondrop(Thing *thing, int button, float x, float y)
  676. {
  677. if (droppable_highlighted_thing) {
  678. if (droppable_highlighted_thing->what == THING_TRASHCAN) {
  679. TrashThing(thing);
  680. }
  681. }
  682. }
  683. static void SDLCALL PostmixCallback(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen)
  684. {
  685. Thing *thing = (Thing *) userdata;
  686. SDL_LockMutex(thing->data.logdev.postmix_lock);
  687. if (thing->data.logdev.postmix_allocated < buflen) {
  688. void *ptr = SDL_realloc(thing->data.logdev.postmix_buffer, buflen);
  689. if (!ptr) {
  690. SDL_UnlockMutex(thing->data.logdev.postmix_lock);
  691. return; /* oh well. */
  692. }
  693. thing->data.logdev.postmix_buffer = (float *) ptr;
  694. thing->data.logdev.postmix_allocated = buflen;
  695. }
  696. SDL_copyp(&thing->data.logdev.postmix_spec, spec);
  697. SDL_memcpy(thing->data.logdev.postmix_buffer, buffer, buflen);
  698. thing->data.logdev.postmix_buflen = buflen;
  699. SDL_AtomicSet(&thing->data.logdev.postmix_updated, 1);
  700. SDL_UnlockMutex(thing->data.logdev.postmix_lock);
  701. }
  702. static void UpdateVisualizer(SDL_Renderer *renderer, SDL_Texture *visualizer, const int channels, const float *buffer, const int buflen)
  703. {
  704. static const SDL_Color channel_colors[8] = {
  705. { 255, 255, 255, 255 },
  706. { 255, 0, 0, 255 },
  707. { 0, 255, 0, 255 },
  708. { 0, 0, 255, 255 },
  709. { 255, 255, 0, 255 },
  710. { 0, 255, 255, 255 },
  711. { 255, 0, 255, 255 },
  712. { 127, 127, 127, 255 }
  713. };
  714. SDL_SetRenderTarget(renderer, visualizer);
  715. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  716. SDL_RenderClear(renderer);
  717. if (buffer && buflen) {
  718. const int frames = (buflen / sizeof (float)) / channels;
  719. const int skip = frames / (VISUALIZER_WIDTH * 2);
  720. int i, j;
  721. for (i = channels - 1; i >= 0; i--) {
  722. const SDL_Color *color = &channel_colors[i % SDL_arraysize(channel_colors)];
  723. SDL_FPoint points[VISUALIZER_WIDTH + 2];
  724. float prevx = 0.0f;
  725. int pointidx = 1;
  726. points[0].x = 0.0f;
  727. points[0].y = VISUALIZER_HEIGHT * 0.5f;
  728. for (j = 0; j < (SDL_arraysize(points)-1); j++) {
  729. const float val = buffer[((j * skip) * channels) + i];
  730. const float x = prevx + 2;
  731. const float y = (VISUALIZER_HEIGHT * 0.5f) - (VISUALIZER_HEIGHT * (val * 0.5f));
  732. SDL_assert(pointidx < SDL_arraysize(points));
  733. points[pointidx].x = x;
  734. points[pointidx].y = y;
  735. pointidx++;
  736. prevx = x;
  737. }
  738. SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, 255);
  739. SDL_RenderLines(renderer, points, pointidx);
  740. }
  741. }
  742. SDL_SetRenderTarget(renderer, NULL);
  743. }
  744. static void LogicalDeviceThing_ontick(Thing *thing, Uint64 now)
  745. {
  746. const SDL_bool ismousedover = (thing == mouseover_thing) ? SDL_TRUE : SDL_FALSE;
  747. if (!thing->data.logdev.visualizer || !thing->data.logdev.postmix_lock) { /* need these to work, skip if they failed. */
  748. return;
  749. }
  750. if (thing->data.logdev.visualizer_enabled != ismousedover) {
  751. thing->data.logdev.visualizer_enabled = ismousedover;
  752. if (!ismousedover) {
  753. SDL_SetAudioPostmixCallback(thing->data.logdev.devid, NULL, NULL);
  754. } else {
  755. if (thing->data.logdev.postmix_buffer) {
  756. SDL_memset(thing->data.logdev.postmix_buffer, '\0', thing->data.logdev.postmix_buflen);
  757. }
  758. SDL_AtomicSet(&thing->data.logdev.postmix_updated, 1); /* so this will at least clear the texture later. */
  759. SDL_SetAudioPostmixCallback(thing->data.logdev.devid, PostmixCallback, thing);
  760. }
  761. }
  762. }
  763. static void LogicalDeviceThing_ondraw(Thing *thing, SDL_Renderer *renderer)
  764. {
  765. if (thing->data.logdev.visualizer_enabled) {
  766. SDL_FRect dst;
  767. dst.w = thing->rect.w;
  768. dst.h = thing->rect.h;
  769. dst.x = thing->rect.x + ((thing->rect.w - dst.w) / 2);
  770. dst.y = thing->rect.y + ((thing->rect.h - dst.h) / 2);
  771. if (SDL_AtomicGet(&thing->data.logdev.postmix_updated)) {
  772. float *buffer;
  773. int channels;
  774. int buflen;
  775. SDL_LockMutex(thing->data.logdev.postmix_lock);
  776. channels = thing->data.logdev.postmix_spec.channels;
  777. buflen = thing->data.logdev.postmix_buflen;
  778. buffer = (float *) SDL_malloc(thing->data.logdev.postmix_buflen);
  779. if (buffer) {
  780. SDL_memcpy(buffer, thing->data.logdev.postmix_buffer, thing->data.logdev.postmix_buflen);
  781. SDL_AtomicSet(&thing->data.logdev.postmix_updated, 0);
  782. }
  783. SDL_UnlockMutex(thing->data.logdev.postmix_lock);
  784. UpdateVisualizer(renderer, thing->data.logdev.visualizer, channels, buffer, buflen);
  785. SDL_free(buffer);
  786. }
  787. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 30);
  788. SDL_RenderTexture(renderer, thing->data.logdev.visualizer, NULL, &dst);
  789. }
  790. }
  791. static Thing *CreateLogicalDeviceThing(Thing *parent, const SDL_AudioDeviceID which, const float x, const float y)
  792. {
  793. static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
  794. Thing *physthing = ((parent->what == THING_LOGDEV) || (parent->what == THING_LOGDEV_CAPTURE)) ? parent->data.logdev.physdev : parent;
  795. const SDL_bool iscapture = physthing->data.physdev.iscapture;
  796. Thing *thing;
  797. SDL_Log("Adding logical audio device %u", (unsigned int) which);
  798. thing = CreateThing(iscapture ? THING_LOGDEV_CAPTURE : THING_LOGDEV, x, y, 5, -1, -1, logdev_texture, NULL);
  799. thing->data.logdev.devid = which;
  800. thing->data.logdev.iscapture = iscapture;
  801. thing->data.logdev.physdev = physthing;
  802. thing->data.logdev.visualizer = SDL_CreateTexture(state->renderers[0], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, VISUALIZER_WIDTH, VISUALIZER_HEIGHT);
  803. thing->data.logdev.postmix_lock = SDL_CreateMutex();
  804. if (thing->data.logdev.visualizer) {
  805. SDL_SetTextureBlendMode(thing->data.logdev.visualizer, SDL_BLENDMODE_BLEND);
  806. }
  807. thing->line_connected_to = physthing;
  808. thing->ontick = LogicalDeviceThing_ontick;
  809. thing->ondrag = DeviceThing_ondrag;
  810. thing->ondrop = LogicalDeviceThing_ondrop;
  811. thing->ondraw = LogicalDeviceThing_ondraw;
  812. thing->can_be_dropped_onto = can_be_dropped_onto;
  813. SetLogicalDeviceTitlebar(thing);
  814. return thing;
  815. }
  816. static void SetPhysicalDeviceTitlebar(Thing *thing)
  817. {
  818. int frames = 0;
  819. SDL_AudioSpec *spec = &thing->data.physdev.spec;
  820. SDL_GetAudioDeviceFormat(thing->data.physdev.devid, spec, &frames);
  821. SDL_free(thing->titlebar);
  822. if (thing->data.physdev.devid == SDL_AUDIO_DEVICE_DEFAULT_CAPTURE) {
  823. SDL_asprintf(&thing->titlebar, "Default system device (CAPTURE, %s, %s, %uHz, %d frames)", AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq, frames);
  824. } else if (thing->data.physdev.devid == SDL_AUDIO_DEVICE_DEFAULT_OUTPUT) {
  825. SDL_asprintf(&thing->titlebar, "Default system device (OUTPUT, %s, %s, %uHz, %d frames)", AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq, frames);
  826. } else {
  827. 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);
  828. }
  829. }
  830. static void PhysicalDeviceThing_ondrop(Thing *thing, int button, float x, float y)
  831. {
  832. if (droppable_highlighted_thing) {
  833. if (droppable_highlighted_thing->what == THING_TRASHCAN) {
  834. TrashThing(thing);
  835. }
  836. }
  837. }
  838. static void PhysicalDeviceThing_ontick(Thing *thing, Uint64 now)
  839. {
  840. const int lifetime = POOF_LIFETIME;
  841. const int elasped = (int) (now - thing->createticks);
  842. if (elasped > lifetime) {
  843. thing->scale = 1.0f;
  844. thing->a = 255;
  845. thing->ontick = NULL; /* no more ticking. */
  846. } else {
  847. const float pct = ((float) elasped) / ((float) lifetime);
  848. thing->a = (Uint8) (int) (pct * 255.0f);
  849. thing->scale = pct; /* grow to normal size */
  850. }
  851. }
  852. static Thing *CreatePhysicalDeviceThing(const SDL_AudioDeviceID which, const SDL_bool iscapture)
  853. {
  854. static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
  855. static float next_physdev_x = 0;
  856. Thing *thing;
  857. int winw, winh;
  858. SDL_GetWindowSize(state->windows[0], &winw, &winh);
  859. if (next_physdev_x > (winw-physdev_texture->w)) {
  860. next_physdev_x = 0;
  861. }
  862. SDL_Log("Adding physical audio device %u", (unsigned int) which);
  863. thing = CreateThing(iscapture ? THING_PHYSDEV_CAPTURE : THING_PHYSDEV, next_physdev_x, 170, 5, -1, -1, physdev_texture, NULL);
  864. thing->data.physdev.devid = which;
  865. thing->data.physdev.iscapture = iscapture;
  866. thing->data.physdev.name = SDL_GetAudioDeviceName(which);
  867. thing->ondrag = DeviceThing_ondrag;
  868. thing->ondrop = PhysicalDeviceThing_ondrop;
  869. thing->ontick = PhysicalDeviceThing_ontick;
  870. thing->can_be_dropped_onto = can_be_dropped_onto;
  871. SetPhysicalDeviceTitlebar(thing);
  872. if (SDL_GetTicks() <= (app_ready_ticks + 2000)) { /* assume this is the initial batch if it happens in the first two seconds. */
  873. RepositionRowOfThings(THING_PHYSDEV, 10.0f); /* don't rearrange them after the initial add. */
  874. RepositionRowOfThings(THING_PHYSDEV_CAPTURE, 170.0f); /* don't rearrange them after the initial add. */
  875. next_physdev_x = 0.0f;
  876. } else {
  877. next_physdev_x += physdev_texture->w * 1.5f;
  878. }
  879. return thing;
  880. }
  881. static Thing *CreateTrashcanThing(void)
  882. {
  883. int winw, winh;
  884. SDL_GetWindowSize(state->windows[0], &winw, &winh);
  885. return CreateThing(THING_TRASHCAN, winw - trashcan_texture->w, winh - trashcan_texture->h, 10, -1, -1, trashcan_texture, "Drag things here to remove them.");
  886. }
  887. static Thing *CreateDefaultPhysicalDevice(const SDL_bool iscapture)
  888. {
  889. return CreatePhysicalDeviceThing(iscapture ? SDL_AUDIO_DEVICE_DEFAULT_CAPTURE : SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, iscapture);
  890. }
  891. static void TickThings(void)
  892. {
  893. Thing *i;
  894. Thing *next;
  895. const Uint64 now = SDL_GetTicks();
  896. for (i = things; i != NULL; i = next) {
  897. next = i->next; /* in case this deletes itself. */
  898. if (i->ontick) {
  899. i->ontick(i, now);
  900. }
  901. }
  902. }
  903. static void WindowResized(const int newwinw, const int newwinh)
  904. {
  905. Thing *i;
  906. const float neww = (float) newwinw;
  907. const float newh = (float) newwinh;
  908. const float oldw = (float) state->window_w;
  909. const float oldh = (float) state->window_h;
  910. for (i = things; i != NULL; i = i->next) {
  911. const float halfw = i->rect.w / 2.0f;
  912. const float halfh = i->rect.h / 2.0f;
  913. const float x = (i->rect.x + halfw) / oldw;
  914. const float y = (i->rect.y + halfh) / oldh;
  915. i->rect.x = (x * neww) - halfw;
  916. i->rect.y = (y * newh) - halfh;
  917. }
  918. state->window_w = newwinw;
  919. state->window_h = newwinh;
  920. }
  921. static void Loop(void)
  922. {
  923. SDL_Event event;
  924. SDL_bool saw_event = SDL_FALSE;
  925. if (app_ready_ticks == 0) {
  926. app_ready_ticks = SDL_GetTicks();
  927. }
  928. while (SDL_PollEvent(&event)) {
  929. Thing *thing = NULL;
  930. saw_event = SDL_TRUE;
  931. switch (event.type) {
  932. case SDL_EVENT_MOUSE_MOTION:
  933. thing = UpdateMouseOver(event.motion.x, event.motion.y);
  934. if ((dragging_button == -1) && event.motion.state) {
  935. if (event.motion.state & SDL_BUTTON_LMASK) {
  936. dragging_button = SDL_BUTTON_LEFT;
  937. } else if (event.motion.state & SDL_BUTTON_RMASK) {
  938. dragging_button = SDL_BUTTON_RIGHT;
  939. } else if (event.motion.state & SDL_BUTTON_MMASK) {
  940. dragging_button = SDL_BUTTON_MIDDLE;
  941. }
  942. if (dragging_button != -1) {
  943. dragging_thing = thing;
  944. if (thing && thing->ondrag) {
  945. thing->ondrag(thing, dragging_button, event.motion.x, event.motion.y);
  946. }
  947. }
  948. }
  949. droppable_highlighted_thing = NULL;
  950. if (dragging_thing) {
  951. dragging_thing->rect.x = event.motion.x - (dragging_thing->rect.w / 2);
  952. dragging_thing->rect.y = event.motion.y - (dragging_thing->rect.h / 2);
  953. if (dragging_thing->can_be_dropped_onto) {
  954. thing = FindThingAtPoint(event.motion.x, event.motion.y);
  955. if (thing) {
  956. int i;
  957. for (i = 0; dragging_thing->can_be_dropped_onto[i]; i++) {
  958. if (dragging_thing->can_be_dropped_onto[i] == thing->what) {
  959. droppable_highlighted_thing = thing;
  960. break;
  961. }
  962. }
  963. }
  964. }
  965. }
  966. break;
  967. case SDL_EVENT_MOUSE_BUTTON_DOWN:
  968. thing = UpdateMouseOver(event.button.x, event.button.y);
  969. break;
  970. case SDL_EVENT_MOUSE_BUTTON_UP:
  971. if (dragging_button == event.button.button) {
  972. Thing *dropped_thing = dragging_thing;
  973. dragging_thing = NULL;
  974. dragging_button = -1;
  975. if (dropped_thing && dropped_thing->ondrop) {
  976. dropped_thing->ondrop(dropped_thing, event.button.button, event.button.x, event.button.y);
  977. }
  978. droppable_highlighted_thing = NULL;
  979. }
  980. thing = UpdateMouseOver(event.button.x, event.button.y);
  981. break;
  982. case SDL_EVENT_MOUSE_WHEEL:
  983. UpdateMouseOver(event.wheel.mouseX, event.wheel.mouseY);
  984. break;
  985. case SDL_EVENT_DROP_FILE:
  986. SDL_Log("Drop file! '%s'", event.drop.file);
  987. LoadWavThing(event.drop.file, event.drop.x, event.drop.y);
  988. /* SDLTest_CommonEvent will free the string, below. */
  989. break;
  990. case SDL_EVENT_WINDOW_RESIZED:
  991. WindowResized(event.window.data1, event.window.data2);
  992. break;
  993. case SDL_EVENT_AUDIO_DEVICE_ADDED:
  994. CreatePhysicalDeviceThing(event.adevice.which, event.adevice.iscapture);
  995. break;
  996. case SDL_EVENT_AUDIO_DEVICE_REMOVED: {
  997. const SDL_AudioDeviceID which = event.adevice.which;
  998. Thing *i, *next;
  999. SDL_Log("Removing audio device %u", (unsigned int) which);
  1000. for (i = things; i != NULL; i = next) {
  1001. next = i->next;
  1002. if (((i->what == THING_PHYSDEV) || (i->what == THING_PHYSDEV_CAPTURE)) && (i->data.physdev.devid == which)) {
  1003. TrashThing(i);
  1004. next = things; /* in case we mangled the list. */
  1005. } else if (((i->what == THING_LOGDEV) || (i->what == THING_LOGDEV_CAPTURE)) && (i->data.logdev.devid == which)) {
  1006. TrashThing(i);
  1007. next = things; /* in case we mangled the list. */
  1008. }
  1009. }
  1010. break;
  1011. }
  1012. default: break;
  1013. }
  1014. SDLTest_CommonEvent(state, &event, &done);
  1015. }
  1016. TickThings();
  1017. Draw();
  1018. if (!saw_event) {
  1019. SDL_Delay(10);
  1020. }
  1021. #ifdef __EMSCRIPTEN__
  1022. if (done) {
  1023. emscripten_cancel_main_loop();
  1024. }
  1025. #endif
  1026. }
  1027. int main(int argc, char *argv[])
  1028. {
  1029. int i;
  1030. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO | SDL_INIT_AUDIO);
  1031. if (state == NULL) {
  1032. Quit(1);
  1033. }
  1034. state->window_flags |= SDL_WINDOW_RESIZABLE;
  1035. for (i = 1; i < argc;) {
  1036. int consumed = SDLTest_CommonArg(state, i);
  1037. if (consumed == 0) {
  1038. consumed = -1;
  1039. /* add our own command lines here. */
  1040. }
  1041. if (consumed < 0) {
  1042. static const char *options[] = {
  1043. /* add our own command lines here. */
  1044. /*"[--blend none|blend|add|mod|mul|sub]",*/
  1045. NULL
  1046. };
  1047. SDLTest_CommonLogUsage(state, argv[0], options);
  1048. Quit(1);
  1049. }
  1050. i += consumed;
  1051. }
  1052. if (!SDLTest_CommonInit(state)) {
  1053. Quit(2);
  1054. }
  1055. if (state->audio_id) {
  1056. SDL_CloseAudioDevice(state->audio_id);
  1057. state->audio_id = 0;
  1058. }
  1059. SetDefaultTitleBar();
  1060. physdev_texture = CreateTexture("physaudiodev.bmp");
  1061. logdev_texture = CreateTexture("logaudiodev.bmp");
  1062. audio_texture = CreateTexture("audiofile.bmp");
  1063. trashcan_texture = CreateTexture("trashcan.bmp");
  1064. soundboard_texture = CreateTexture("soundboard.bmp");
  1065. soundboard_levels_texture = CreateTexture("soundboard_levels.bmp");
  1066. LoadStockWavThings();
  1067. CreateTrashcanThing();
  1068. CreateDefaultPhysicalDevice(SDL_FALSE);
  1069. CreateDefaultPhysicalDevice(SDL_TRUE);
  1070. #ifdef __EMSCRIPTEN__
  1071. emscripten_set_main_loop(Loop, 0, 1);
  1072. #else
  1073. while (!done) {
  1074. Loop();
  1075. }
  1076. #endif
  1077. Quit(0);
  1078. return 0;
  1079. }