testaudio.c 42 KB

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