patch-morseplayer_c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. $OpenBSD: patch-morseplayer_c,v 1.2 2012/07/04 14:39:08 naddy Exp $
  2. --- morseplayer.c.orig Tue Jan 10 20:25:45 2006
  3. +++ morseplayer.c Wed Jul 4 16:36:40 2012
  4. @@ -32,9 +32,6 @@
  5. * April 1990 (http://www.arrl.org/files/infoserv/tech/code-std.txt)
  6. */
  7. -#include <sys/types.h>
  8. -#include <sys/ioctl.h>
  9. -#include <sys/audioio.h>
  10. #include <sys/queue.h>
  11. #include <sys/poll.h>
  12. #include <string.h>
  13. @@ -42,12 +39,13 @@
  14. #include <err.h>
  15. #include <unistd.h>
  16. #include <math.h>
  17. +#include <sndio.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <sysexits.h>
  21. #include <errno.h>
  22. #include <ctype.h>
  23. -#include <limits.h>
  24. +#include <float.h>
  25. #ifndef BYTE_ORDER
  26. #error "no byte order defined"
  27. @@ -72,6 +70,7 @@ struct play_head {
  28. };
  29. struct s_params {
  30. + struct sio_hdl *hdl; /* sndio handle */
  31. u_int sp_rate; /* sample rate */
  32. double sp_hz; /* audio frequency */
  33. u_int sp_ditlen; /* dit length */
  34. @@ -81,7 +80,6 @@ struct s_params {
  35. u_int sp_blocksize; /* audio block size */
  36. u_int sp_channels; /* number of channels */
  37. u_int sp_precision; /* sample precision (bits) */
  38. - int sp_fd; /* audio file descriptor */
  39. int sp_seenspace; /* seen a space character? */
  40. };
  41. @@ -353,16 +351,16 @@ feed_audio(struct s_params *pars)
  42. struct play_list *e;
  43. if (SIMPLEQ_EMPTY(&playhead.l)) {
  44. - r = write(pars->sp_fd, quietBlock.buf, res);
  45. - if (r == -1)
  46. + r = sio_write(pars->hdl, quietBlock.buf, res);
  47. + if (r == 0)
  48. err(1, "write");
  49. break;
  50. }
  51. e = SIMPLEQ_FIRST(&playhead.l);
  52. if (e->pl_res > res) {
  53. - r = write(pars->sp_fd, e->pl_ptr, res);
  54. - if (r == -1)
  55. + r = sio_write(pars->hdl, e->pl_ptr, res);
  56. + if (r == 0)
  57. err(1, "write");
  58. e->pl_ptr += res;
  59. e->pl_res -= res;
  60. @@ -370,8 +368,8 @@ feed_audio(struct s_params *pars)
  61. break;
  62. }
  63. - r = write(pars->sp_fd, e->pl_ptr, e->pl_res);
  64. - if (r == -1)
  65. + r = sio_write(pars->hdl, e->pl_ptr, e->pl_res);
  66. + if (r == 0)
  67. err(1, "write");
  68. playhead.nsamps -= e->pl_res;
  69. res -= e->pl_res;
  70. @@ -386,22 +384,18 @@ int
  71. main_loop(struct s_params *pars)
  72. {
  73. struct pollfd fds[2];
  74. - ssize_t r;
  75. + nfds_t nfds;
  76. int iseof = 0;
  77. - /*
  78. - * first, play one block of silence... /dev/audio
  79. - * isn't poll-able until it's been kicked.
  80. - */
  81. - r = write(pars->sp_fd, quietBlock.buf, quietBlock.len);
  82. - if (r == -1)
  83. - err(1, "write");
  84. -
  85. - fds[0].fd = pars->sp_fd;
  86. - fds[0].events = POLLOUT;
  87. + nfds = sio_nfds(pars->hdl);
  88. + if (nfds != 1)
  89. + errx(1, "too many sndio file handles");
  90. fds[1].events = POLLIN;
  91. for (;;) {
  92. + nfds = sio_pollfd(pars->hdl, &fds[0], POLLOUT);
  93. + if (nfds == 0)
  94. + fds[0].fd = -1;
  95. if (playhead.nsamps < pars->sp_sampthresh && !iseof)
  96. fds[1].fd = fileno(stdin);
  97. else
  98. @@ -416,12 +410,10 @@ main_loop(struct s_params *pars)
  99. iseof = 1;
  100. }
  101. - if (fds[0].revents & POLLOUT) {
  102. + if (sio_revents(pars->hdl, &fds[0]) & POLLOUT) {
  103. /* feed the audio device */
  104. feed_audio(pars);
  105. if (iseof && SIMPLEQ_EMPTY(&playhead.l)) {
  106. - if (ioctl(fds[0].fd, AUDIO_DRAIN, NULL) == -1)
  107. - err(1, "audio_drain");
  108. break;
  109. }
  110. }
  111. @@ -726,8 +718,8 @@ int
  112. main(int argc, char *argv[])
  113. {
  114. struct s_params pars;
  115. - int f, c;
  116. - audio_info_t ai;
  117. + int c;
  118. + struct sio_par par;
  119. float cwpm = -1.0, owpm = -1.0, pitch = -1.0;
  120. char *afname = NULL;
  121. @@ -765,7 +757,7 @@ main(int argc, char *argv[])
  122. break;
  123. case '?':
  124. default:
  125. - fprintf(stderr, "%s [-d /dev/audio] [-c cwpm] "
  126. + fprintf(stderr, "%s [-d <sndio device>] [-c cwpm] "
  127. "[-w owpm] [-f freq] [-D]\n", argv[0]);
  128. return (1);
  129. }
  130. @@ -798,49 +790,36 @@ main(int argc, char *argv[])
  131. overallwpm = owpm;
  132. charwpm = cwpm;
  133. - if (afname == NULL)
  134. - afname = "/dev/audio";
  135. + pars.hdl = sio_open(afname, SIO_PLAY, 0);
  136. + if (pars.hdl == NULL)
  137. + errx(1, "Could not open sndio device");
  138. - f = open(afname, O_WRONLY, 0);
  139. - if (f == -1)
  140. - err(1, "open %s", afname);
  141. + sio_initpar(&par);
  142. + par.rate = 22050;
  143. + par.sig = 0;
  144. + par.bits = 8;
  145. + par.pchan = 1;
  146. + if (!sio_setpar(pars.hdl, &par))
  147. + errx(1, "sio_setpar failed");
  148. + if (!sio_getpar(pars.hdl, &par))
  149. + errx(1, "sio_getpar failed");
  150. - AUDIO_INITINFO(&ai);
  151. - /* ai.play.sample_rate = 22050; */
  152. - ai.play.encoding = AUDIO_ENCODING_SLINEAR;
  153. - ai.play.precision = 8;
  154. - ai.play.channels = 1;
  155. - if (ioctl(f, AUDIO_SETINFO, &ai) == -1) {
  156. -#if BYTE_ORDER == LITTLE_ENDIAN
  157. - ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
  158. -#elif BYTE_ORDER == BIG_ENDIAN
  159. - ai.play.encoding = AUDIO_ENCODING_SLINEAR_BE;
  160. -#else
  161. -# error "oh please, get the pdp outta here."
  162. -#endif
  163. - ai.play.precision = 16;
  164. - ai.play.channels = 2;
  165. - if (ioctl(f, AUDIO_SETINFO, &ai) == -1) {
  166. - err(1, "setinfo");
  167. - }
  168. - }
  169. -
  170. - if (ioctl(f, AUDIO_GETINFO, &ai) == -1)
  171. - err(1, "getinfo");
  172. - pars.sp_rate = ai.play.sample_rate;
  173. + pars.sp_rate = par.rate;
  174. pars.sp_hz = pitch;
  175. - pars.sp_sampthresh = ai.blocksize * ai.hiwat;
  176. - pars.sp_blocksize = ai.blocksize;
  177. + pars.sp_sampthresh = par.appbufsz * par.bps;
  178. + pars.sp_blocksize = par.round * par.bps;
  179. pars.sp_seenspace = 0;
  180. - pars.sp_fd = f;
  181. - pars.sp_channels = ai.play.channels;
  182. - pars.sp_precision = ai.play.precision;
  183. + pars.sp_channels = par.pchan;
  184. + pars.sp_precision = par.bits;
  185. if (diagmode > 0) {
  186. check_chars();
  187. test_times(&pars);
  188. return (0);
  189. }
  190. +
  191. + if (!sio_start(pars.hdl))
  192. + errx(1, "could not start sndio");
  193. playlist_init();
  194. init_sounds();