audio.cpp 2.5 KB

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