testaudio.c 41 KB

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