1
0

testaudio.c 35 KB

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