patch-src_audio_c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. $OpenBSD: patch-src_audio_c,v 1.2 2014/11/04 10:58:21 sthen Exp $
  2. --- src/audio.c.orig Thu May 15 12:59:25 2014
  3. +++ src/audio.c Mon Nov 3 21:02:56 2014
  4. @@ -32,13 +32,48 @@
  5. extern char sc_device[];
  6. +#ifdef HAVE_SNDIO
  7. +struct sio_hdl *ahdl;
  8. +#else
  9. int afd;
  10. +#endif
  11. #define ABUFSIZE 4000
  12. void init_audio()
  13. {
  14. +#ifdef HAVE_SNDIO
  15. + struct sio_par par;
  16. +
  17. + if ((ahdl = sio_open(SIO_DEVANY, SIO_REC, 0)) == NULL)
  18. + {
  19. + fprintf(stderr, "Failed to open audio device\n");
  20. + exit(1);
  21. + }
  22. + sio_initpar(&par);
  23. + par.bits = 8;
  24. + par.sig = 0;
  25. + par.rchan = 1;
  26. + par.rate = 8000;
  27. + if (!sio_setpar(ahdl, &par) || !sio_getpar(ahdl, &par))
  28. + {
  29. + fprintf(stderr, "Couldn't set audio parameters\n");
  30. + exit(1);
  31. + }
  32. +
  33. + if (par.bits != 8 || par.sig || par.rchan != 1 || par.rate != 8000)
  34. + {
  35. + fprintf(stderr, "Unsupported audio parameters\n");
  36. + exit(1);
  37. + }
  38. +
  39. + if (!sio_start(ahdl))
  40. + {
  41. + fprintf(stderr, "Couldn't start audio device\n");
  42. + exit(1);
  43. + }
  44. +#else
  45. char afile[40]; /* Audio device name */
  46. int sndfmt; /* Encoding of audio from */
  47. int channels; /* Number of channels to record */
  48. @@ -71,12 +106,16 @@ void init_audio()
  49. perror("8000 sps");
  50. exit(errno);
  51. }
  52. +#endif
  53. }
  54. int close_audio()
  55. {
  56. -
  57. +#ifdef HAVE_SNDIO
  58. + sio_close (ahdl);
  59. +#else
  60. close(afd);
  61. +#endif
  62. return (0);
  63. }
  64. @@ -132,6 +171,23 @@ int rescale(int testvalue)
  65. return (testvalue);
  66. }
  67. +#ifdef HAVE_SNDIO
  68. +void read_abuf(unsigned char *p) {
  69. + size_t todo, n;
  70. +
  71. + todo = ABUFSIZE;
  72. + while (todo > 0) {
  73. + n = sio_read(ahdl, p, todo);
  74. + if (n == 0) {
  75. + fprintf(stderr, "Failed to read from audio device\n");
  76. + exit(1);
  77. + }
  78. + p += n;
  79. + todo -= n;
  80. + }
  81. +}
  82. +#endif
  83. +
  84. float get_audio_sample(void)
  85. {
  86. @@ -144,10 +200,14 @@ float get_audio_sample(void)
  87. int i, k;
  88. for (k = 0; k < 4; k++) {
  89. +#ifdef HAVE_SNDIO
  90. + read_abuf(abuf);
  91. +#else
  92. if ((rc = read(afd, abuf, ABUFSIZE)) == -1) {
  93. perror("audio read");
  94. exit(errno);
  95. }
  96. +#endif
  97. for (i = 0; i < rc; i++) // calculate average
  98. {