sdl.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #include <errno.h>
  2. #include <limits.h>
  3. #include <stdarg.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <SDL.h>
  9. #include <SDL_audio.h>
  10. #include <SDL_events.h>
  11. #include <SDL_mutex.h>
  12. #include "au.h"
  13. #include "adev.h"
  14. #include "common.h"
  15. #ifndef SDL_BSIZE
  16. #define SDL_BSIZE 0x4000
  17. #endif
  18. enum {
  19. A_NULL,
  20. A_ENC,
  21. A_RATE,
  22. A_CHAN
  23. };
  24. struct adev {
  25. uint8_t buffer[SDL_BSIZE];
  26. SDL_AudioSpec spec;
  27. SDL_AudioDeviceID dev;
  28. SDL_mutex *mtx;
  29. SDL_cond *cond;
  30. SDL_cond *done;
  31. const char *device;
  32. size_t bcount;
  33. size_t waiting;
  34. Uint32 etype;
  35. uint32_t enc;
  36. int mode;
  37. int needopen;
  38. int isdone;
  39. };
  40. static int correct_audio(SDL_AudioSpec *, void *, size_t);
  41. static void sdl_callback(void *, uint8_t *, int);
  42. const struct a_lookup stab[] = {
  43. { "enc", A_ENC },
  44. { "encoding", A_ENC },
  45. { "rate", A_RATE },
  46. { "chan", A_CHAN },
  47. { "channel", A_CHAN },
  48. { "channels", A_CHAN },
  49. { NULL, A_NULL }
  50. };
  51. struct adev *
  52. ad_open(const char *dev, int mode)
  53. {
  54. struct adev *a;
  55. if (SDL_Init(SDL_INIT_AUDIO) != 0)
  56. return (NULL);
  57. if ((a = malloc(sizeof(struct adev))) == NULL) {
  58. SDL_Quit();
  59. return (NULL);
  60. }
  61. a->dev = 0;
  62. a->mtx = NULL;
  63. a->cond = NULL;
  64. a->done = NULL;
  65. memset(&a->spec, 0, sizeof(SDL_AudioSpec));
  66. a->spec.size = sizeof(a->buffer);
  67. a->spec.callback = &sdl_callback;
  68. a->spec.userdata = a;
  69. a->bcount = 0;
  70. a->waiting = 0;
  71. a->needopen = 1;
  72. a->isdone = 0;
  73. a->dev = 0;
  74. a->device = dev;
  75. a->enc = 0;
  76. a->mode = mode == A_READ;
  77. if ((a->etype = SDL_RegisterEvents(1)) == (Uint32)-1)
  78. goto err;
  79. if ((a->mtx = SDL_CreateMutex()) == NULL ||
  80. (a->cond = SDL_CreateCond()) == NULL ||
  81. (a->done = SDL_CreateCond()) == NULL)
  82. goto err;
  83. return (a);
  84. err:
  85. ad_close(a);
  86. return (NULL);
  87. }
  88. struct adev *
  89. ad_fpopen(FILE *fp, int mode)
  90. {
  91. ERR(EOPNOTSUPP, NULL);
  92. }
  93. void
  94. ad_close(struct adev *a)
  95. {
  96. if (a == NULL)
  97. return;
  98. a->isdone = 1;
  99. if (a->mtx != NULL)
  100. (void)SDL_LockMutex(a->mtx);
  101. while (a->waiting > 0) {
  102. (void)SDL_CondSignal(a->cond);
  103. (void)SDL_CondWait(a->done, a->mtx);
  104. }
  105. if (a->mtx != NULL)
  106. SDL_UnlockMutex(a->mtx);
  107. if (a->dev > 0)
  108. SDL_CloseAudioDevice(a->dev);
  109. if (a->mtx != NULL)
  110. SDL_DestroyMutex(a->mtx);
  111. if (a->cond != NULL)
  112. SDL_DestroyCond(a->cond);
  113. SDL_Quit();
  114. free(a);
  115. }
  116. int
  117. ad_limit(struct adev *a)
  118. {
  119. ERR(ENOSYS, -1);
  120. }
  121. int
  122. ad_get(struct adev *a, const char *s, ...)
  123. {
  124. va_list ap;
  125. ssize_t r;
  126. int n;
  127. int v;
  128. if (a == NULL || s == NULL)
  129. ERR(EINVAL, -1);
  130. (void)SDL_LockMutex(a->mtx);
  131. va_start(ap, s);
  132. for (n = 0; (r = a_lookup(stab, s, &v)) > 0; n++, s += r)
  133. switch (v) {
  134. case A_ENC:
  135. *va_arg(ap, uint32_t *) = a->enc;
  136. break;
  137. case A_RATE:
  138. *va_arg(ap, uint32_t *) = a->spec.freq;
  139. break;
  140. case A_CHAN:
  141. *va_arg(ap, uint32_t *) = a->spec.channels;
  142. break;
  143. default:
  144. goto err;
  145. }
  146. if (r == 0) {
  147. (void)SDL_UnlockMutex(a->mtx);
  148. va_end(ap);
  149. return (n);
  150. }
  151. err:
  152. (void)SDL_UnlockMutex(a->mtx);
  153. va_end(ap);
  154. ERR(EINVAL, n);
  155. }
  156. int
  157. ad_set(struct adev *a, const char *s, ...)
  158. {
  159. va_list ap;
  160. ssize_t r;
  161. int n;
  162. int v;
  163. if (a == NULL || s == NULL)
  164. ERR(EINVAL, -1);
  165. (void)SDL_LockMutex(a->mtx);
  166. if (a->dev > 0) {
  167. SDL_CloseAudioDevice(a->dev);
  168. a->dev = 0;
  169. }
  170. a->needopen = 1;
  171. va_start(ap, s);
  172. for (n = 0; (r = a_lookup(stab, s, &v)) > 0; n++, s += r)
  173. switch (v) {
  174. case A_ENC:
  175. switch ((a->enc = va_arg(ap, uint32_t))) {
  176. case AU_ULAW:
  177. a->enc = AU_PCM16;
  178. a->spec.format = AUDIO_S16MSB;
  179. goto err;
  180. case AU_PCM8:
  181. a->spec.format = AUDIO_S8;
  182. break;
  183. case AU_PCM16:
  184. a->spec.format = AUDIO_S16MSB;
  185. break;
  186. case AU_PCM24:
  187. a->enc = AU_PCM32;
  188. a->spec.format = AUDIO_S32MSB;
  189. goto err;
  190. case AU_PCM32:
  191. a->spec.format = AUDIO_S32MSB;
  192. break;
  193. case AU_FLOAT32:
  194. a->spec.format = AUDIO_F32MSB;
  195. break;
  196. case AU_FLOAT64:
  197. a->enc = AU_FLOAT32;
  198. a->spec.format = AUDIO_F32MSB;
  199. goto err;
  200. case AU_ALAW:
  201. a->enc = AU_PCM16;
  202. a->spec.format = AUDIO_S16MSB;
  203. goto err;
  204. default:
  205. goto err;
  206. }
  207. break;
  208. case A_RATE:
  209. a->spec.freq = va_arg(ap, uint32_t);
  210. break;
  211. case A_CHAN:
  212. a->spec.channels = va_arg(ap, uint32_t);
  213. break;
  214. default:
  215. goto err;
  216. }
  217. if (r == 0) {
  218. (void)SDL_UnlockMutex(a->mtx);
  219. va_end(ap);
  220. return (n);
  221. }
  222. err:
  223. (void)SDL_UnlockMutex(a->mtx);
  224. va_end(ap);
  225. ERR(EINVAL, n);
  226. }
  227. ssize_t
  228. ad_read(struct adev *a, void *b, size_t s)
  229. {
  230. SDL_Event e;
  231. size_t i;
  232. size_t n;
  233. if (a == NULL || b == NULL || !a->mode)
  234. ERR(EINVAL, -1);
  235. if (s > UINT32_MAX)
  236. ERR(ERANGE, -1);
  237. if (s == 0)
  238. return (0);
  239. (void)SDL_LockMutex(a->mtx);
  240. if (a->needopen) {
  241. if ((a->dev = SDL_OpenAudioDevice(a->device, a->mode,
  242. &a->spec, NULL, 0)) < 2)
  243. goto err;
  244. SDL_PauseAudioDevice(a->dev, 0);
  245. a->needopen = 0;
  246. }
  247. for (i = 0; i < s && !a->isdone;) {
  248. /* Wait for the buffer to have data */
  249. while (a->bcount == 0 && !a->isdone) {
  250. if (SDL_CondSignal(a->cond) != 0)
  251. goto err;
  252. (void)SDL_UnlockMutex(a->mtx);
  253. if (SDL_WaitEvent(&e) != 1)
  254. goto err;
  255. (void)SDL_LockMutex(a->mtx);
  256. if (e.type == SDL_AUDIODEVICEREMOVED &&
  257. e.adevice.which == a->dev)
  258. goto ret;
  259. if (e.type != a->etype)
  260. continue;
  261. }
  262. n = MIN(s - i, a->bcount);
  263. if (correct_audio(&a->spec, a->buffer, n) != 0)
  264. goto err;
  265. (void)memcpy((char *)b + i, a->buffer, n);
  266. if (a->bcount > n)
  267. (void)memmove(a->buffer, a->buffer + n,
  268. a->bcount - n);
  269. a->bcount -= n;
  270. i += n;
  271. }
  272. ret:
  273. (void)SDL_UnlockMutex(a->mtx);
  274. return (i);
  275. err:
  276. (void)SDL_UnlockMutex(a->mtx);
  277. return (-1);
  278. }
  279. ssize_t
  280. ad_write(struct adev *a, const void *b, size_t s)
  281. {
  282. SDL_Event e;
  283. size_t i;
  284. size_t n;
  285. if (a == NULL || b == NULL || a->mode)
  286. ERR(EINVAL, -1);
  287. if (s > UINT32_MAX)
  288. ERR(ERANGE, -1);
  289. if (s == 0)
  290. return (0);
  291. (void)SDL_LockMutex(a->mtx);
  292. if (a->needopen) {
  293. if ((a->dev = SDL_OpenAudioDevice(a->device, a->mode,
  294. &a->spec, NULL, 0)) < 2)
  295. goto err;
  296. SDL_PauseAudioDevice(a->dev, 0);
  297. a->needopen = 0;
  298. }
  299. for (i = 0; i < s && !a->isdone;) {
  300. /* Wait for the buffer to have free space */
  301. while (a->bcount >= sizeof(a->buffer) && !a->isdone) {
  302. if (SDL_CondSignal(a->cond) != 0)
  303. goto err;
  304. (void)SDL_UnlockMutex(a->mtx);
  305. if (SDL_WaitEvent(&e) != 1)
  306. goto err;
  307. (void)SDL_LockMutex(a->mtx);
  308. if (e.type == SDL_AUDIODEVICEREMOVED &&
  309. e.adevice.which == a->dev)
  310. goto ret;
  311. if (e.type != a->etype)
  312. continue;
  313. }
  314. n = MIN(s - i, sizeof(a->buffer) - a->bcount);
  315. (void)memcpy(a->buffer + a->bcount, (char *)b + i, n);
  316. if (correct_audio(&a->spec,
  317. a->buffer + a->bcount, n) != 0)
  318. goto err;
  319. i += n;
  320. a->bcount += n;
  321. }
  322. ret:
  323. (void)SDL_UnlockMutex(a->mtx);
  324. return (i);
  325. err:
  326. (void)SDL_UnlockMutex(a->mtx);
  327. return (-1);
  328. }
  329. void
  330. sdl_callback(void *ctx, uint8_t *b, int s)
  331. {
  332. struct adev *a = ctx;
  333. SDL_Event e;
  334. int i;
  335. int n;
  336. if (ctx == NULL || b == NULL || s <= 0)
  337. return;
  338. if (a->isdone)
  339. return;
  340. if (SDL_LockMutex(a->mtx) != 0) {
  341. a->isdone = 1;
  342. return;
  343. }
  344. e.type = a->etype;
  345. if (a->mode) { /* read */
  346. for (i = 0; i < s && !a->isdone;) {
  347. /* Wait for the buffer to have free space */
  348. a->waiting++;
  349. while (a->bcount >= sizeof(a->buffer) &&
  350. !a->isdone)
  351. if (SDL_PushEvent(&e) != 1 ||
  352. SDL_CondWait(a->cond, a->mtx) != 0)
  353. a->isdone = 1;
  354. a->waiting--;
  355. if (a->bcount < sizeof(a->buffer)) {
  356. n = MIN(sizeof(a->buffer) - a->bcount,
  357. s - i);
  358. (void)memcpy(a->buffer + a->bcount,
  359. b + i, n);
  360. i += n;
  361. a->bcount += n;
  362. }
  363. }
  364. if (a->bcount >= sizeof(a->buffer) && !a->isdone)
  365. (void)SDL_PushEvent(&e);
  366. } else { /* write */
  367. for (i = 0; i < s && !a->isdone;) {
  368. /* Wait for the buffer to have data */
  369. a->waiting++;
  370. while (a->bcount == 0 && !a->isdone)
  371. if (SDL_PushEvent(&e) != 1 ||
  372. SDL_CondWait(a->cond, a->mtx) != 0)
  373. a->isdone = 1;
  374. a->waiting--;
  375. n = MIN(s - i, a->bcount);
  376. (void)memcpy(b + i, a->buffer, n);
  377. if (a->bcount > s - i)
  378. (void)memmove(a->buffer, a->buffer + n,
  379. a->bcount - n);
  380. i += n;
  381. a->bcount -= n;
  382. }
  383. if (a->bcount == 0 && !a->isdone)
  384. (void)SDL_PushEvent(&e);
  385. }
  386. if (a->isdone) {
  387. (void)SDL_PushEvent(&e);
  388. (void)SDL_CondSignal(a->done);
  389. }
  390. (void)SDL_UnlockMutex(a->mtx);
  391. }
  392. int
  393. correct_audio(SDL_AudioSpec *spec, void *b, size_t s)
  394. {
  395. uint8_t *u;
  396. size_t i;
  397. unsigned bs;
  398. if (spec == NULL || b == NULL ||
  399. (bs = SDL_AUDIO_BITSIZE(spec->format)) > 32)
  400. ERR(EINVAL, -1);
  401. if (bs == 0)
  402. return (0);
  403. if (SDL_AUDIO_ISLITTLEENDIAN(spec->format))
  404. au_encbswap(AU_PCM8 + (bs - 1) / 8, b, s);
  405. if (SDL_AUDIO_ISUNSIGNED(spec->format))
  406. for (u = b, i = 0, bs = (bs + 7) / 8; i < s; i += bs)
  407. u[i] ^= 0x80;
  408. return (0);
  409. }