1
0

AUDIO.Cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <windows.h>
  2. #include "Audio.h"
  3. #include "main.h"
  4. #include "api__in_cdda.h"
  5. static int start_clock,paused;
  6. static HWAVEIN hWaveIn;
  7. static short data_latest[576*2];
  8. static short data_buffers[2][576*2];
  9. static WAVEHDR wave_hdrs[2];
  10. static HANDLE hThread;
  11. static DWORD WINAPI Thread(LPVOID _a);
  12. int a_v = -666, a_p = 0, initted = 0;
  13. int audioInit(/*int sample*/)
  14. {
  15. WAVEFORMATEX wft = {0};
  16. DWORD threadid;
  17. start_clock=GetTickCount();
  18. paused=0;
  19. // if (!sample) return initted=0;
  20. wft.wFormatTag = WAVE_FORMAT_PCM;
  21. wft.nChannels = 2;
  22. wft.nSamplesPerSec = 44100;
  23. wft.nBlockAlign = 2*2;
  24. wft.nAvgBytesPerSec = wft.nSamplesPerSec*wft.nBlockAlign;
  25. wft.wBitsPerSample = 16;
  26. wft.cbSize = 0;
  27. if (waveInOpen(&hWaveIn,WAVE_MAPPER,&wft,0,0,0) != MMSYSERR_NOERROR)
  28. {
  29. return 1;
  30. }
  31. for (int x = 0; x < 2; x ++)
  32. {
  33. memset(&wave_hdrs[x],0,sizeof(wave_hdrs[x]));
  34. wave_hdrs[x].lpData = (char *) data_buffers[x];
  35. wave_hdrs[x].dwBufferLength = 576*2*sizeof(short);
  36. waveInPrepareHeader(hWaveIn,&wave_hdrs[x],sizeof(wave_hdrs[0]));
  37. waveInAddBuffer(hWaveIn,&wave_hdrs[x],sizeof(wave_hdrs[0]));
  38. }
  39. initted=1;
  40. done=0;
  41. waveInStart(hWaveIn);
  42. hThread = CreateThread(NULL,0,Thread,(LPVOID)&done,0,&threadid);
  43. SetThreadPriority(hThread, (int)AGAVE_API_CONFIG->GetInt(playbackConfigGroupGUID, L"priority", THREAD_PRIORITY_HIGHEST));
  44. return 0;
  45. }
  46. void audioPause(int s)
  47. {
  48. if (s)
  49. {
  50. if (!paused)
  51. {
  52. paused=1;
  53. if (initted) waveInStop(hWaveIn);
  54. start_clock = GetTickCount()-start_clock;
  55. }
  56. }
  57. else
  58. {
  59. if (paused)
  60. {
  61. if (initted) waveInStart(hWaveIn);
  62. start_clock=GetTickCount()-start_clock;
  63. paused=0;
  64. }
  65. }
  66. }
  67. void audioQuit()
  68. {
  69. if (!initted) return;
  70. done=1;
  71. WaitForSingleObject(hThread,INFINITE);
  72. waveInStop(hWaveIn);
  73. waveInReset(hWaveIn);
  74. while (waveInClose(hWaveIn) == WAVERR_STILLPLAYING) Sleep(10);
  75. CloseHandle(hThread);
  76. }
  77. int audioGetPos()
  78. {
  79. if (paused) return start_clock;
  80. return GetTickCount()-start_clock;
  81. }
  82. void audioSetPos(int ms)
  83. {
  84. start_clock = GetTickCount()-ms;
  85. }
  86. static DWORD WINAPI Thread(LPVOID _a)
  87. {
  88. volatile int *a = (volatile int *)_a;
  89. int w;
  90. while (!*a)
  91. {
  92. Sleep(576000/44100);
  93. if (wave_hdrs[0].dwFlags & WHDR_DONE) w=0;
  94. else if (wave_hdrs[1].dwFlags & WHDR_DONE) w=1;
  95. else continue;
  96. memcpy(data_latest,wave_hdrs[w].lpData,576*2*sizeof(short));
  97. wave_hdrs[w].dwFlags=WHDR_PREPARED;
  98. waveInAddBuffer(hWaveIn,&wave_hdrs[w],sizeof(wave_hdrs[0]));
  99. // memset(data_latest,0,576*2*sizeof(short));
  100. line.VSAAddPCMData(data_latest,2,16,0);
  101. line.SAAddPCMData(data_latest,2,16,0);
  102. }
  103. return 0;
  104. }