audiodrv.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // --------------------------------------------------------------------------
  2. // ``sndio'' specific audio driver interface.
  3. // --------------------------------------------------------------------------
  4. #include "audiodrv.h"
  5. audioDriver::audioDriver()
  6. {
  7. hdl = NULL;
  8. }
  9. bool audioDriver::IsThere()
  10. {
  11. return 1;
  12. }
  13. bool audioDriver::Open(udword inFreq, int inPrecision, int inChannels,
  14. int inFragments, int inFragBase)
  15. {
  16. sio_par askpar, retpar;
  17. hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0);
  18. if (hdl == NULL)
  19. {
  20. errorString = "ERROR: Could not open audio device.";
  21. return false;
  22. }
  23. frequency = inFreq;
  24. channels = inChannels;
  25. precision = inPrecision;
  26. encoding = retpar.sig ? SIDEMU_SIGNED_PCM : SIDEMU_UNSIGNED_PCM;
  27. sio_initpar(&askpar);
  28. if (precision == SIDEMU_8BIT)
  29. {
  30. askpar.le = SIO_LE_NATIVE;
  31. askpar.bits = 16;
  32. askpar.sig = 1;
  33. } else {
  34. askpar.bits = 8;
  35. askpar.sig = 0;
  36. }
  37. askpar.pchan = inChannels == SIDEMU_MONO ? 1 : 2;
  38. askpar.rate = inFreq;
  39. askpar.round = 1 << inFragBase;
  40. askpar.appbufsz = inFragments * askpar.round;
  41. if (!sio_setpar(hdl, &askpar) || !sio_getpar(hdl, &retpar))
  42. {
  43. errorString = "ERROR: Could not set audio parameters.";
  44. goto bad_close;
  45. }
  46. if (retpar.bits != askpar.bits || retpar.sig != askpar.sig ||
  47. (retpar.bits > 8 && retpar.le != askpar.le) ||
  48. retpar.pchan != askpar.pchan || retpar.rate != askpar.rate)
  49. {
  50. errorString = "ERROR: Unsupported audio parameters.";
  51. goto bad_close;
  52. }
  53. blockSize = retpar.round;
  54. if (!sio_start(hdl))
  55. {
  56. errorString = "ERROR: Could not start audio device.";
  57. goto bad_close;
  58. }
  59. return true;
  60. bad_close:
  61. sio_close(hdl);
  62. hdl = NULL;
  63. return false;
  64. }
  65. void audioDriver::Close()
  66. {
  67. if (hdl != NULL)
  68. {
  69. sio_close(hdl);
  70. hdl = NULL;
  71. }
  72. }
  73. void audioDriver::Play(ubyte* pBuffer, int bufferSize)
  74. {
  75. if (hdl != NULL)
  76. sio_write(hdl, pBuffer, bufferSize);
  77. }
  78. bool audioDriver::Reset()
  79. {
  80. if (hdl != NULL) {
  81. sio_stop(hdl);
  82. sio_start(hdl);
  83. return true;
  84. }
  85. return false;
  86. }