testaudio.c 42 KB

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