audio.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <limits.h>
  4. #include <stdarg.h>
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/audioio.h>
  13. #include <unistd.h>
  14. #include "au.h"
  15. #include "adev.h"
  16. #include "common.h"
  17. #define AUDIO "/dev/audio"
  18. enum {
  19. A_NULL,
  20. A_ENC,
  21. A_RATE,
  22. A_CHAN
  23. };
  24. struct adev {
  25. int audio;
  26. int mode;
  27. };
  28. static struct adev *ad_fdopen(int, int);
  29. static uint32_t audio_au_enc(const struct audio_prinfo *);
  30. static int au_audio_enc(struct audio_prinfo *, uint32_t);
  31. const struct a_lookup stab[] = {
  32. { "enc", A_ENC },
  33. { "encoding", A_ENC },
  34. { "rate", A_RATE },
  35. { "chan", A_CHAN },
  36. { "channel", A_CHAN },
  37. { "channels", A_CHAN },
  38. { NULL, A_NULL }
  39. };
  40. struct adev *
  41. ad_open(const char *dev, int mode)
  42. {
  43. char path[PATH_MAX];
  44. struct adev *a;
  45. int fmode = (mode & A_WRITE) ? O_WRONLY : O_RDONLY;
  46. int fd;
  47. if (dev == NULL)
  48. dev = AUDIO;
  49. if (snprintf(path, sizeof(path), "%s", dev)
  50. > sizeof(path))
  51. return (NULL);
  52. if ((fd = open(path, fmode)) >= 0)
  53. goto end;
  54. if (snprintf(path, sizeof(path), "/dev/%s", dev)
  55. > sizeof(path))
  56. return (NULL);
  57. if ((fd = open(path, fmode)) >= 0)
  58. goto end;
  59. return (NULL);
  60. end:
  61. if ((a = ad_fdopen(fd, mode)) == NULL)
  62. close(fd);
  63. return (a);
  64. }
  65. struct adev *
  66. ad_fpopen(FILE *fp, int mode)
  67. {
  68. if (fp == NULL)
  69. ERR(EINVAL, NULL);
  70. return (ad_fdopen(fileno(fp), mode));
  71. }
  72. struct adev *
  73. ad_fdopen(int fd, int mode)
  74. {
  75. struct adev *a;
  76. uint32_t e;
  77. if (fd < 0)
  78. ERR(EINVAL, NULL);
  79. if ((a = malloc(sizeof(struct adev))) == NULL)
  80. return (NULL);
  81. a->audio = fd;
  82. a->mode = mode;
  83. /* Set the audio(4) encoding to the closest au(7) encoding */
  84. if (ad_get(a, "enc", &e) != 1 ||
  85. ad_set(a, "enc", e) != 1) {
  86. ad_close(a);
  87. return (NULL);
  88. }
  89. return (a);
  90. }
  91. void
  92. ad_close(struct adev *a)
  93. {
  94. if (a == NULL)
  95. return;
  96. (void)ioctl(a->audio, AUDIO_DRAIN);
  97. if (a->audio >= 0)
  98. close(a->audio);
  99. free(a);
  100. }
  101. int
  102. ad_limit(struct adev *a)
  103. {
  104. ERR(ENOSYS, -1);
  105. }
  106. int
  107. ad_get(struct adev *a, const char *s, ...)
  108. {
  109. audio_info_t info;
  110. struct audio_prinfo *i;
  111. va_list ap;
  112. ssize_t r;
  113. int n;
  114. int v;
  115. if (a == NULL || s == NULL)
  116. ERR(EINVAL, -1);
  117. if (ioctl(a->audio, AUDIO_GETINFO, &info) != 0)
  118. return (-1);
  119. i = (a->mode & A_WRITE) ? &info.play : &info.record;
  120. va_start(ap, s);
  121. for (n = 0; (r = a_lookup(stab, s, &v)) > 0; n++, s += r)
  122. switch (v) {
  123. case A_ENC:
  124. *va_arg(ap, uint32_t *) = audio_au_enc(i);
  125. break;
  126. case A_RATE:
  127. *va_arg(ap, uint32_t *) = i->sample_rate;
  128. break;
  129. case A_CHAN:
  130. *va_arg(ap, uint32_t *) = i->channels;
  131. break;
  132. default:
  133. goto err;
  134. }
  135. if (r == 0) {
  136. va_end(ap);
  137. return (n);
  138. }
  139. err:
  140. va_end(ap);
  141. ERR(EINVAL, n);
  142. }
  143. int
  144. ad_set(struct adev *a, const char *s, ...)
  145. {
  146. audio_info_t info;
  147. struct audio_prinfo *i;
  148. va_list ap;
  149. ssize_t r;
  150. int n;
  151. int v;
  152. if (a == NULL || s == NULL)
  153. ERR(EINVAL, -1);
  154. if (ioctl(a->audio, AUDIO_GETINFO, &info) != 0)
  155. return (-1);
  156. i = (a->mode & A_WRITE) ? &info.play : &info.record;
  157. va_start(ap, s);
  158. for (n = 0; (r = a_lookup(stab, s, &v)) > 0; n++, s += r)
  159. switch (v) {
  160. case A_ENC:
  161. if (au_audio_enc(i, va_arg(ap, uint32_t)) != 0)
  162. goto err;
  163. break;
  164. case A_RATE:
  165. i->sample_rate = va_arg(ap, uint32_t);
  166. break;
  167. case A_CHAN:
  168. i->channels = va_arg(ap, uint32_t);
  169. break;
  170. default:
  171. goto err;
  172. }
  173. if (ioctl(a->audio, AUDIO_SETINFO, &info) != 0)
  174. goto err;
  175. if (r == 0) {
  176. va_end(ap);
  177. return (n);
  178. }
  179. err:
  180. va_end(ap);
  181. ERR(EINVAL, n);
  182. }
  183. ssize_t
  184. ad_read(struct adev *a, void *b, size_t s)
  185. {
  186. if (a == NULL || a->audio < 0 || b == NULL ||
  187. (a->mode & A_WRITE))
  188. ERR(EINVAL, -1);
  189. if (s == 0)
  190. return (0);
  191. return (read(a->audio, b, s));
  192. }
  193. ssize_t
  194. ad_write(struct adev *a, const void *b, size_t s)
  195. {
  196. if (a == NULL || a->audio < 0 || b == NULL ||
  197. !(a->mode & A_WRITE))
  198. ERR(EINVAL, -1);
  199. if (s == 0)
  200. return (0);
  201. return (write(a->audio, b, s));
  202. }
  203. uint32_t
  204. audio_au_enc(const struct audio_prinfo *i)
  205. {
  206. if (i == NULL)
  207. return (0);
  208. switch (i->encoding) {
  209. case AUDIO_ENCODING_ULAW:
  210. return (AU_ULAW);
  211. case AUDIO_ENCODING_ALAW:
  212. return (AU_ALAW);
  213. case AUDIO_ENCODING_SLINEAR:
  214. /* FALLTHROUGH */
  215. case AUDIO_ENCODING_SLINEAR_LE:
  216. /* FALLTHROUGH */
  217. case AUDIO_ENCODING_SLINEAR_BE:
  218. /* FALLTHROUGH */
  219. case AUDIO_ENCODING_ULINEAR:
  220. /* FALLTHROUGH */
  221. case AUDIO_ENCODING_ULINEAR_LE:
  222. /* FALLTHROUGH */
  223. case AUDIO_ENCODING_ULINEAR_BE:
  224. if (i->precision <= 8)
  225. return (AU_PCM8);
  226. else if (i->precision <= 16)
  227. return (AU_PCM16);
  228. else if (i->precision <= 24)
  229. return (AU_PCM24);
  230. else
  231. return (AU_PCM32);
  232. break;
  233. }
  234. return (0);
  235. }
  236. int
  237. au_audio_enc(struct audio_prinfo *i, uint32_t v)
  238. {
  239. if (i == NULL)
  240. return (-1);
  241. switch (v) {
  242. case AU_ULAW:
  243. i->encoding = AUDIO_ENCODING_ULAW;
  244. i->precision = 8;
  245. break;
  246. case AU_PCM8:
  247. case AU_PCM16:
  248. case AU_PCM24:
  249. case AU_PCM32:
  250. i->encoding = AUDIO_ENCODING_SLINEAR_BE;
  251. i->precision = au_encsize(v) * 8;
  252. break;
  253. case AU_ALAW:
  254. i->encoding = AUDIO_ENCODING_ALAW;
  255. i->precision = 8;
  256. break;
  257. default:
  258. return (-1);
  259. }
  260. return (0);
  261. }