sndio.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <stdlib.h>
  8. #include <string.h>
  9. #include <sndio.h>
  10. #include "au.h"
  11. #include "adev.h"
  12. #include "common.h"
  13. enum {
  14. A_NULL,
  15. A_ENC,
  16. A_RATE,
  17. A_CHAN
  18. };
  19. struct adev {
  20. struct sio_hdl *sio;
  21. int needstart;
  22. };
  23. const struct a_lookup stab[] = {
  24. { "enc", A_ENC },
  25. { "encoding", A_ENC },
  26. { "rate", A_RATE },
  27. { "chan", A_CHAN },
  28. { "channel", A_CHAN },
  29. { "channels", A_CHAN },
  30. { NULL, A_NULL }
  31. };
  32. struct adev *
  33. ad_open(const char *dev, int mode)
  34. {
  35. struct adev *a;
  36. if ((a = malloc(sizeof(struct adev))) == NULL)
  37. return (NULL);
  38. if ((a->sio = sio_open(dev != NULL ? dev : SIO_DEVANY,
  39. (mode & A_WRITE) ? SIO_PLAY : SIO_REC, 0)) == NULL) {
  40. free(a);
  41. return (NULL);
  42. }
  43. a->needstart = 1;
  44. return (a);
  45. }
  46. struct adev *
  47. ad_fpopen(FILE *fp, int mode)
  48. {
  49. ERR(EOPNOTSUPP, NULL);
  50. }
  51. void
  52. ad_close(struct adev *a)
  53. {
  54. if (a == NULL)
  55. return;
  56. if (!a->needstart)
  57. (void)sio_stop(a->sio);
  58. sio_close(a->sio);
  59. free(a);
  60. }
  61. int
  62. ad_limit(struct adev *a)
  63. {
  64. #ifdef USE_PLEDGE
  65. return (pledge("audio stdio", NULL));
  66. #else
  67. ERR(ENOSYS, -1);
  68. #endif
  69. }
  70. int
  71. ad_get(struct adev *a, const char *s, ...)
  72. {
  73. ERR(EOPNOTSUPP, -1);
  74. }
  75. int
  76. ad_set(struct adev *a, const char *s, ...)
  77. {
  78. struct sio_par p;
  79. va_list ap;
  80. ssize_t r;
  81. int n;
  82. int v;
  83. if (a == NULL || s == NULL)
  84. ERR(EINVAL, -1);
  85. if (!a->needstart && !sio_stop(a->sio))
  86. ERR(EOPNOTSUPP, -1);
  87. a->needstart = 1;
  88. sio_initpar(&p);
  89. p.le = 0;
  90. p.sig = 1;
  91. p.msb = 1;
  92. va_start(ap, s);
  93. for (n = 0; (r = a_lookup(stab, s, &v)) > 0; n++, s += r)
  94. switch (v) {
  95. case A_ENC:
  96. switch (va_arg(ap, uint32_t)) {
  97. case AU_PCM8:
  98. p.bits = 8;
  99. break;
  100. case AU_PCM16:
  101. p.bits = 16;
  102. break;
  103. case AU_PCM24:
  104. p.bits = 24;
  105. break;
  106. case AU_PCM32:
  107. p.bits = 32;
  108. break;
  109. default:
  110. ERR(EINVAL, -1);
  111. }
  112. p.bps = SIO_BPS(p.bits);
  113. break;
  114. case A_RATE:
  115. p.rate = va_arg(ap, uint32_t);
  116. break;
  117. case A_CHAN:
  118. p.rchan = p.pchan = va_arg(ap, uint32_t);
  119. break;
  120. default:
  121. goto err;
  122. }
  123. if (r == 0) {
  124. if (!sio_setpar(a->sio, &p))
  125. goto err;
  126. va_end(ap);
  127. return (n);
  128. }
  129. err:
  130. va_end(ap);
  131. ERR(EINVAL, n);
  132. }
  133. ssize_t
  134. ad_write(struct adev *a, const void *b, size_t size)
  135. {
  136. size_t r;
  137. if (a == NULL || b == NULL)
  138. ERR(EFAULT, -1);
  139. if (a->needstart) {
  140. if (!sio_start(a->sio))
  141. ERR(EINVAL, -1);
  142. a->needstart = 0;
  143. }
  144. while ((r = sio_write(a->sio, b, size)) == 0 &&
  145. !sio_eof(a->sio))
  146. /* do nothing */;
  147. return (r);
  148. }
  149. ssize_t
  150. ad_read(struct adev *a, void *b, size_t size)
  151. {
  152. size_t r;
  153. if (a == NULL || b == NULL)
  154. ERR(EFAULT, -1);
  155. if (a->needstart) {
  156. if (!sio_start(a->sio))
  157. ERR(EINVAL, -1);
  158. a->needstart = 0;
  159. }
  160. while ((r = sio_read(a->sio, b, size)) == 0 && !sio_eof(a->sio))
  161. /* do nothing */;
  162. return (r);
  163. }