drv_win.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:5k
源码类别:

Windows CE

开发平台:

C/C++

  1. /* MikMod sound library
  2. (c) 1998, 1999, 2000 Miodrag Vallat and others - see file AUTHORS for
  3. complete list.
  4. This library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of
  7. the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA.
  16. */
  17. /*==============================================================================
  18.   $Id: drv_win.c,v 1.2 2004/01/31 22:39:40 raph Exp $
  19.   Driver for output on win32 platforms using the multimedia API
  20. ==============================================================================*/
  21. /*
  22. Written by Bjornar Henden <bhenden@online.no>
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "mikmod_internals.h"
  28. #ifdef DRV_WIN
  29. #include <windows.h>
  30. #pragma comment(lib,"winmm.lib")
  31. #define NUMBUFFERS 6 /* number of buffers */
  32. #define BUFFERSIZE 120 /* buffer size in milliseconds */
  33. HWAVEOUT hwaveout;
  34. WAVEHDR header[NUMBUFFERS];
  35. LPSTR buffer[NUMBUFFERS]; /* pointers to buffers */
  36. WORD buffersout; /* number of buffers playing/about to be played */
  37. WORD nextbuffer; /* next buffer to be mixed */
  38. ULONG buffersize; /* buffer size in bytes */
  39. /* converts Windows error to libmikmod error */
  40. static int WIN_GetError(MMRESULT mmr)
  41. {
  42. switch (mmr) {
  43. case MMSYSERR_INVALHANDLE:
  44. return MMERR_WINMM_HANDLE;
  45. case MMSYSERR_NODRIVER:
  46. return MMERR_OPENING_AUDIO;
  47. case MMSYSERR_NOMEM:
  48. return MMERR_OUT_OF_MEMORY;
  49. case MMSYSERR_ALLOCATED:
  50. return MMERR_WINMM_ALLOCATED;
  51. case MMSYSERR_BADDEVICEID:
  52. return MMERR_WINMM_DEVICEID;
  53. case WAVERR_BADFORMAT:
  54. return MMERR_WINMM_FORMAT;
  55. default:
  56. return MMERR_WINMM_UNKNOWN; /* should hopefully not happen */
  57. }
  58. }
  59. static BOOL WIN_IsThere(void)
  60. {
  61. return waveOutGetNumDevs()>0?1:0;
  62. }
  63. static void CALLBACK WIN_CallBack(HWAVEOUT hwo,UINT uMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2)
  64. {
  65. if (uMsg==WOM_DONE) --buffersout;
  66. }
  67.   
  68. static BOOL WIN_Init(void)
  69. {
  70. WAVEFORMATEX wfe;
  71. WORD samplesize;
  72. MMRESULT mmr;
  73. int n;
  74. samplesize=1;
  75. if (md_mode&DMODE_STEREO) samplesize<<=1;
  76. if (md_mode&DMODE_16BITS) samplesize<<=1;
  77. wfe.wFormatTag=WAVE_FORMAT_PCM;
  78. wfe.nChannels=md_mode&DMODE_STEREO?2:1;
  79. wfe.nSamplesPerSec=md_mixfreq;
  80. wfe.nAvgBytesPerSec=md_mixfreq*samplesize;
  81. wfe.nBlockAlign=samplesize;
  82. wfe.wBitsPerSample=md_mode&DMODE_16BITS?16:8;
  83. wfe.cbSize=sizeof(wfe);
  84. mmr=waveOutOpen(&hwaveout,WAVE_MAPPER,&wfe,(DWORD)WIN_CallBack,0,CALLBACK_FUNCTION);
  85. if (mmr!=MMSYSERR_NOERROR) {
  86. _mm_errno=WIN_GetError(mmr);
  87. return 1;
  88. }
  89. buffersize=md_mixfreq*samplesize*BUFFERSIZE/1000;
  90. for (n=0;n<NUMBUFFERS;n++) {
  91. buffer[n]=_mm_malloc(buffersize);
  92. header[n].lpData=buffer[n];
  93. header[n].dwBufferLength=buffersize;
  94. mmr=waveOutPrepareHeader(hwaveout,&header[n],sizeof(WAVEHDR));
  95. if (!buffer[n]||mmr!=MMSYSERR_NOERROR) {
  96. if (!buffer[n])
  97. _mm_errno=MMERR_OUT_OF_MEMORY;
  98. else
  99. _mm_errno=WIN_GetError(mmr);
  100. return 1;
  101. }
  102. }
  103. md_mode|=DMODE_SOFT_MUSIC|DMODE_SOFT_SNDFX;
  104. buffersout=nextbuffer=0;
  105. return VC_Init();
  106. }
  107. static void WIN_Exit(void)
  108. {
  109. int n;
  110. VC_Exit();
  111. if (hwaveout) {
  112. for (n=0;n<NUMBUFFERS;n++) {
  113. if (header[n].dwFlags&WHDR_PREPARED)
  114. waveOutUnprepareHeader(hwaveout,&header[n],sizeof(WAVEHDR));
  115. _mm_free(buffer[n]);
  116. }
  117. while (waveOutClose(hwaveout)==WAVERR_STILLPLAYING) Sleep(10);
  118. hwaveout=NULL;
  119. }
  120. }
  121. static void WIN_Update(void)
  122. {
  123. ULONG done;
  124. while (buffersout<NUMBUFFERS) {
  125. done=VC_WriteBytes(buffer[nextbuffer],buffersize);
  126. if (!done) break;
  127. header[nextbuffer].dwBufferLength=done;
  128. waveOutWrite(hwaveout,&header[nextbuffer],sizeof(WAVEHDR));
  129. if (++nextbuffer>=NUMBUFFERS) nextbuffer%=NUMBUFFERS;
  130. ++buffersout;
  131. }
  132. }
  133. static void WIN_PlayStop(void)
  134. {
  135. if (hwaveout) waveOutReset(hwaveout);
  136. VC_PlayStop();
  137. }
  138. MIKMODAPI MDRIVER drv_win={
  139. NULL,
  140. "Windows waveform-audio",
  141. "Windows waveform-audio driver v0.1",
  142. 0,255,
  143. "winmm",
  144. NULL,
  145. NULL,
  146. WIN_IsThere,
  147. VC_SampleLoad,
  148. VC_SampleUnload,
  149. VC_SampleSpace,
  150. VC_SampleLength,
  151. WIN_Init,
  152. WIN_Exit,
  153. NULL,
  154. VC_SetNumVoices,
  155. VC_PlayStart,
  156. WIN_PlayStop,
  157. WIN_Update,
  158. NULL,
  159. VC_VoiceSetVolume,
  160. VC_VoiceGetVolume,
  161. VC_VoiceSetFrequency,
  162. VC_VoiceGetFrequency,
  163. VC_VoiceSetPanning,
  164. VC_VoiceGetPanning,
  165. VC_VoicePlay,
  166. VC_VoiceStop,
  167. VC_VoiceStopped,
  168. VC_VoiceGetPosition,
  169. VC_VoiceRealVolume
  170. };
  171. #else
  172. MISSING(drv_win);
  173. #endif
  174. /* ex:set ts=4: */