2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* Allow access to a raw mixing buffer */
26 #define WIN32_LEAN_AND_MEAN
30 #include "SDL_timer.h"
31 #include "SDL_audio.h"
32 #include "../SDL_audio_c.h"
33 #include "SDL_dibaudio.h"
34 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
35 #include "win_ce_semaphore.h"
39 /* Audio driver functions */
40 static int DIB_OpenAudio(_THIS, SDL_AudioSpec *spec);
41 static void DIB_ThreadInit(_THIS);
42 static void DIB_WaitAudio(_THIS);
43 static Uint8 *DIB_GetAudioBuf(_THIS);
44 static void DIB_PlayAudio(_THIS);
45 static void DIB_WaitDone(_THIS);
46 static void DIB_CloseAudio(_THIS);
48 /* Audio driver bootstrap functions */
50 static int Audio_Available(void)
55 static void Audio_DeleteDevice(SDL_AudioDevice *device)
57 SDL_free(device->hidden);
61 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
63 SDL_AudioDevice *this;
65 /* Initialize all variables that we clean on shutdown */
66 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
68 SDL_memset(this, 0, (sizeof *this));
69 this->hidden = (struct SDL_PrivateAudioData *)
70 SDL_malloc((sizeof *this->hidden));
72 if ( (this == NULL) || (this->hidden == NULL) ) {
79 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
81 /* Set the function pointers */
82 this->OpenAudio = DIB_OpenAudio;
83 this->ThreadInit = DIB_ThreadInit;
84 this->WaitAudio = DIB_WaitAudio;
85 this->PlayAudio = DIB_PlayAudio;
86 this->GetAudioBuf = DIB_GetAudioBuf;
87 this->WaitDone = DIB_WaitDone;
88 this->CloseAudio = DIB_CloseAudio;
90 this->free = Audio_DeleteDevice;
95 AudioBootStrap WAVEOUT_bootstrap = {
96 "waveout", "Win95/98/NT/2000 WaveOut",
97 Audio_Available, Audio_CreateDevice
101 /* The Win32 callback for filling the WAVE device */
102 static void CALLBACK FillSound(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
103 DWORD dwParam1, DWORD dwParam2)
105 SDL_AudioDevice *this = (SDL_AudioDevice *)dwInstance;
107 /* Only service "buffer done playing" messages */
108 if ( uMsg != WOM_DONE )
111 /* Signal that we are done playing a buffer */
112 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
113 ReleaseSemaphoreCE(audio_sem, 1, NULL);
115 ReleaseSemaphore(audio_sem, 1, NULL);
119 static void SetMMerror(char *function, MMRESULT code)
122 char errbuf[MAXERRORLENGTH];
124 wchar_t werrbuf[MAXERRORLENGTH];
127 SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
128 len = SDL_strlen(errbuf);
131 /* UNICODE version */
132 waveOutGetErrorText(code, werrbuf, MAXERRORLENGTH-len);
133 WideCharToMultiByte(CP_ACP,0,werrbuf,-1,errbuf+len,MAXERRORLENGTH-len,NULL,NULL);
135 waveOutGetErrorText(code, errbuf+len, (UINT)(MAXERRORLENGTH-len));
138 SDL_SetError("%s",errbuf);
141 /* Set high priority for the audio thread */
142 static void DIB_ThreadInit(_THIS)
144 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
147 void DIB_WaitAudio(_THIS)
149 /* Wait for an audio chunk to finish */
150 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
151 WaitForSemaphoreCE(audio_sem, INFINITE);
153 WaitForSingleObject(audio_sem, INFINITE);
157 Uint8 *DIB_GetAudioBuf(_THIS)
161 retval = (Uint8 *)(wavebuf[next_buffer].lpData);
165 void DIB_PlayAudio(_THIS)
168 waveOutWrite(sound, &wavebuf[next_buffer], sizeof(wavebuf[0]));
169 next_buffer = (next_buffer+1)%NUM_BUFFERS;
172 void DIB_WaitDone(_THIS)
178 for ( i=0; i<NUM_BUFFERS; ++i ) {
179 if ( wavebuf[i].dwFlags & WHDR_DONE ) {
186 } while ( left > 0 );
189 void DIB_CloseAudio(_THIS)
195 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
196 CloseSynchHandle(audio_sem);
198 CloseHandle(audio_sem);
205 /* Clean up mixing buffers */
206 for ( i=0; i<NUM_BUFFERS; ++i ) {
207 if ( wavebuf[i].dwUser != 0xFFFF ) {
208 waveOutUnprepareHeader(sound, &wavebuf[i],
210 wavebuf[i].dwUser = 0xFFFF;
213 /* Free raw mixing buffer */
214 if ( mixbuf != NULL ) {
220 int DIB_OpenAudio(_THIS, SDL_AudioSpec *spec)
224 WAVEFORMATEX waveformat;
226 /* Initialize the wavebuf structures for closing */
229 for ( i = 0; i < NUM_BUFFERS; ++i )
230 wavebuf[i].dwUser = 0xFFFF;
233 /* Set basic WAVE format parameters */
234 SDL_memset(&waveformat, 0, sizeof(waveformat));
235 waveformat.wFormatTag = WAVE_FORMAT_PCM;
237 /* Determine the audio parameters from the AudioSpec */
238 switch ( spec->format & 0xFF ) {
240 /* Unsigned 8 bit audio data */
241 spec->format = AUDIO_U8;
242 waveformat.wBitsPerSample = 8;
245 /* Signed 16 bit audio data */
246 spec->format = AUDIO_S16;
247 waveformat.wBitsPerSample = 16;
250 SDL_SetError("Unsupported audio format");
253 waveformat.nChannels = spec->channels;
254 waveformat.nSamplesPerSec = spec->freq;
255 waveformat.nBlockAlign =
256 waveformat.nChannels * (waveformat.wBitsPerSample/8);
257 waveformat.nAvgBytesPerSec =
258 waveformat.nSamplesPerSec * waveformat.nBlockAlign;
260 /* Check the buffer size -- minimum of 1/4 second (word aligned) */
261 if ( spec->samples < (spec->freq/4) )
262 spec->samples = ((spec->freq/4)+3)&~3;
264 /* Update the fragment size as size in bytes */
265 SDL_CalculateAudioSpec(spec);
267 /* Open the audio device */
268 result = waveOutOpen(&sound, WAVE_MAPPER, &waveformat,
269 (DWORD_PTR)FillSound, (DWORD_PTR)this, CALLBACK_FUNCTION);
270 if ( result != MMSYSERR_NOERROR ) {
271 SetMMerror("waveOutOpen()", result);
276 /* Check the sound device we retrieved */
280 result = waveOutGetDevCaps((UINT)sound, &caps, sizeof(caps));
281 if ( result != MMSYSERR_NOERROR ) {
282 SetMMerror("waveOutGetDevCaps()", result);
285 printf("Audio device: %s\n", caps.szPname);
289 /* Create the audio buffer semaphore */
290 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
291 audio_sem = CreateSemaphoreCE(NULL, NUM_BUFFERS-1, NUM_BUFFERS, NULL);
293 audio_sem = CreateSemaphore(NULL, NUM_BUFFERS-1, NUM_BUFFERS, NULL);
295 if ( audio_sem == NULL ) {
296 SDL_SetError("Couldn't create semaphore");
300 /* Create the sound buffers */
301 mixbuf = (Uint8 *)SDL_malloc(NUM_BUFFERS*spec->size);
302 if ( mixbuf == NULL ) {
303 SDL_SetError("Out of memory");
306 for ( i = 0; i < NUM_BUFFERS; ++i ) {
307 SDL_memset(&wavebuf[i], 0, sizeof(wavebuf[i]));
308 wavebuf[i].lpData = (LPSTR) &mixbuf[i*spec->size];
309 wavebuf[i].dwBufferLength = spec->size;
310 wavebuf[i].dwFlags = WHDR_DONE;
311 result = waveOutPrepareHeader(sound, &wavebuf[i],
313 if ( result != MMSYSERR_NOERROR ) {
314 SetMMerror("waveOutPrepareHeader()", result);