1
0

waveout.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. simple waveout class.
  3. usage:
  4. create the object
  5. write PCM data using WriteData(); WriteData() returns number of bytes successfully written; CanWrite() returns amout of bytes you can write at given point of time
  6. ForcePlay after writing last data block to ensure that all your PCM data gets queued to waveout
  7. wait for GetLatency() to become 0 to ensure that playback is finished
  8. delete the object
  9. */
  10. class WaveOutConfig
  11. {
  12. friend class WaveOut;
  13. private:
  14. UINT sr,nch,bps,buf_ms,dev,prebuf;
  15. WCHAR* error;
  16. void SetError(const WCHAR* x);
  17. bool use_volume,use_altvol,resetvol;
  18. public:
  19. WaveOutConfig()
  20. {
  21. error=0;
  22. sr=44100;
  23. nch=2;
  24. bps=16;
  25. buf_ms=2000;
  26. dev=0;
  27. prebuf=200;
  28. use_volume=1;
  29. use_altvol=0;
  30. resetvol=0;
  31. }
  32. void SetPCM(UINT _sr,UINT _nch,UINT _bps) {sr=_sr;nch=_nch;bps=_bps;}
  33. void SetBuffer(UINT _buf,UINT _prebuf) {buf_ms=_buf;prebuf=_prebuf;}
  34. void SetDevice(UINT d) {dev=d;}
  35. void SetVolumeSetup(bool enabled,bool alt,bool _reset) {use_volume = enabled;use_altvol=alt;resetvol=_reset;}
  36. ~WaveOutConfig()
  37. {
  38. if (error) LocalFree(error);
  39. }
  40. //call these after attempting to create WaveOut
  41. const WCHAR* GetError() {return error;}
  42. };
  43. class WaveOut{
  44. public:
  45. static WaveOut * Create(WaveOutConfig * cfg);
  46. ~WaveOut();
  47. int WriteData(const void * ptr,UINT siz);
  48. int GetLatency(void);
  49. void Flush();
  50. void SetVolume(int v);
  51. void SetPan(int p);
  52. void Pause(int s);
  53. void ForcePlay();
  54. int CanWrite();
  55. int IsPaused() {return paused?1:0;}
  56. UINT GetMaxLatency() {return MulDiv(buf_size,1000,fmt_mul);}
  57. bool PrintState(char * z);
  58. //for gapless mode
  59. void SetCloseOnStop(bool b) {closeonstop=b;}
  60. bool IsClosed();
  61. private:
  62. WaveOut();
  63. typedef struct tagHEADER
  64. {
  65. tagHEADER * next;
  66. WAVEHDR hdr;
  67. } HEADER;
  68. HEADER * hdr_alloc();
  69. void hdr_free(HEADER * h);
  70. static void hdr_free_list(HEADER * h);
  71. HWAVEOUT hWo;
  72. HANDLE hThread;
  73. HANDLE hEvent;
  74. CRITICAL_SECTION sync;
  75. HEADER *hdrs,*hdrs_free;
  76. UINT fmt_bps,fmt_nch,fmt_sr,fmt_mul,fmt_align;//,fmt_chan;
  77. char * buffer;
  78. UINT buf_size,buf_size_used;
  79. UINT data_written,write_ptr;
  80. UINT minblock,maxblock,avgblock;
  81. DWORD last_time;
  82. DWORD p_time;
  83. UINT prebuf;
  84. UINT n_playing;
  85. UINT cur_block;
  86. bool paused,needplay;
  87. bool die;
  88. bool use_volume,use_altvol,use_resetvol;
  89. bool newpause;//,needflush;
  90. bool closeonstop;
  91. int myvol,mypan;
  92. DWORD orgvol;
  93. void killwaveout();
  94. void flush();
  95. void update_vol();
  96. void advance_block();
  97. void reset_shit();
  98. void thread();
  99. void init();
  100. int open(WaveOutConfig * cfg);
  101. void do_altvol(char * ptr,UINT s);
  102. void do_altvol_i(char * ptr,UINT max,UINT start,UINT d,int vol);
  103. static DWORD WINAPI ThreadProc(WaveOut * p);
  104. };