Mercurial > sdl-ios-xcode
annotate src/audio/windib/SDL_dibaudio.c @ 1361:19418e4422cb
New configure-based build system. Still work in progress, but much improved
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 16 Feb 2006 10:11:48 +0000 |
parents | c71e05b4dc2e |
children | d910939febfa |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
36
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* Allow access to a raw mixing buffer */ | |
24 | |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
25 #include "SDL_windows.h" |
0 | 26 #include <mmsystem.h> |
27 | |
1358
c71e05b4dc2e
More header massaging... works great on Windows. ;-)
Sam Lantinga <slouken@libsdl.org>
parents:
1338
diff
changeset
|
28 #include "SDL_timer.h" |
0 | 29 #include "SDL_audio.h" |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
30 #include "../SDL_audio_c.h" |
0 | 31 #include "SDL_dibaudio.h" |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
32 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
33 #include "win_ce_semaphore.h" |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
34 #endif |
0 | 35 |
36 | |
37 /* Audio driver functions */ | |
38 static int DIB_OpenAudio(_THIS, SDL_AudioSpec *spec); | |
39 static void DIB_ThreadInit(_THIS); | |
40 static void DIB_WaitAudio(_THIS); | |
41 static Uint8 *DIB_GetAudioBuf(_THIS); | |
42 static void DIB_PlayAudio(_THIS); | |
43 static void DIB_WaitDone(_THIS); | |
44 static void DIB_CloseAudio(_THIS); | |
45 | |
46 /* Audio driver bootstrap functions */ | |
47 | |
48 static int Audio_Available(void) | |
49 { | |
50 return(1); | |
51 } | |
52 | |
53 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
54 { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
55 SDL_free(device->hidden); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
56 SDL_free(device); |
0 | 57 } |
58 | |
59 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
60 { | |
61 SDL_AudioDevice *this; | |
62 | |
63 /* Initialize all variables that we clean on shutdown */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
64 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice)); |
0 | 65 if ( this ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
66 SDL_memset(this, 0, (sizeof *this)); |
0 | 67 this->hidden = (struct SDL_PrivateAudioData *) |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
68 SDL_malloc((sizeof *this->hidden)); |
0 | 69 } |
70 if ( (this == NULL) || (this->hidden == NULL) ) { | |
71 SDL_OutOfMemory(); | |
72 if ( this ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
73 SDL_free(this); |
0 | 74 } |
75 return(0); | |
76 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
77 SDL_memset(this->hidden, 0, (sizeof *this->hidden)); |
0 | 78 |
79 /* Set the function pointers */ | |
80 this->OpenAudio = DIB_OpenAudio; | |
81 this->ThreadInit = DIB_ThreadInit; | |
82 this->WaitAudio = DIB_WaitAudio; | |
83 this->PlayAudio = DIB_PlayAudio; | |
84 this->GetAudioBuf = DIB_GetAudioBuf; | |
85 this->WaitDone = DIB_WaitDone; | |
86 this->CloseAudio = DIB_CloseAudio; | |
87 | |
88 this->free = Audio_DeleteDevice; | |
89 | |
90 return this; | |
91 } | |
92 | |
93 AudioBootStrap WAVEOUT_bootstrap = { | |
94 "waveout", "Win95/98/NT/2000 WaveOut", | |
95 Audio_Available, Audio_CreateDevice | |
96 }; | |
97 | |
98 | |
99 /* The Win32 callback for filling the WAVE device */ | |
100 static void CALLBACK FillSound(HWAVEOUT hwo, UINT uMsg, DWORD dwInstance, | |
101 DWORD dwParam1, DWORD dwParam2) | |
102 { | |
103 SDL_AudioDevice *this = (SDL_AudioDevice *)dwInstance; | |
104 | |
105 /* Only service "buffer done playing" messages */ | |
106 if ( uMsg != WOM_DONE ) | |
107 return; | |
108 | |
109 /* Signal that we are done playing a buffer */ | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
110 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
111 ReleaseSemaphoreCE(audio_sem, 1, NULL); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
112 #else |
0 | 113 ReleaseSemaphore(audio_sem, 1, NULL); |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
114 #endif |
0 | 115 } |
116 | |
117 static void SetMMerror(char *function, MMRESULT code) | |
118 { | |
119 int len; | |
120 char errbuf[MAXERRORLENGTH]; | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
121 #ifdef _WIN32_WCE |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
122 wchar_t werrbuf[MAXERRORLENGTH]; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
123 #endif |
0 | 124 |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
125 SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
126 len = SDL_strlen(errbuf); |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
127 |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
128 #ifdef _WIN32_WCE |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
129 /* UNICODE version */ |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
130 waveOutGetErrorText(code, werrbuf, MAXERRORLENGTH-len); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
131 WideCharToMultiByte(CP_ACP,0,werrbuf,-1,errbuf+len,MAXERRORLENGTH-len,NULL,NULL); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
132 #else |
0 | 133 waveOutGetErrorText(code, errbuf+len, MAXERRORLENGTH-len); |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
134 #endif |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
135 |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
136 SDL_SetError("%s",errbuf); |
0 | 137 } |
138 | |
139 /* Set high priority for the audio thread */ | |
140 static void DIB_ThreadInit(_THIS) | |
141 { | |
142 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST); | |
143 } | |
144 | |
145 void DIB_WaitAudio(_THIS) | |
146 { | |
147 /* Wait for an audio chunk to finish */ | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
148 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
149 WaitForSemaphoreCE(audio_sem, INFINITE); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
150 #else |
0 | 151 WaitForSingleObject(audio_sem, INFINITE); |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
152 #endif |
0 | 153 } |
154 | |
155 Uint8 *DIB_GetAudioBuf(_THIS) | |
156 { | |
157 Uint8 *retval; | |
158 | |
159 retval = (Uint8 *)(wavebuf[next_buffer].lpData); | |
160 return retval; | |
161 } | |
162 | |
163 void DIB_PlayAudio(_THIS) | |
164 { | |
165 /* Queue it up */ | |
166 waveOutWrite(sound, &wavebuf[next_buffer], sizeof(wavebuf[0])); | |
167 next_buffer = (next_buffer+1)%NUM_BUFFERS; | |
168 } | |
169 | |
170 void DIB_WaitDone(_THIS) | |
171 { | |
172 int i, left; | |
173 | |
174 do { | |
175 left = NUM_BUFFERS; | |
176 for ( i=0; i<NUM_BUFFERS; ++i ) { | |
177 if ( wavebuf[i].dwFlags & WHDR_DONE ) { | |
178 --left; | |
179 } | |
180 } | |
181 if ( left > 0 ) { | |
182 SDL_Delay(100); | |
183 } | |
184 } while ( left > 0 ); | |
185 } | |
186 | |
187 void DIB_CloseAudio(_THIS) | |
188 { | |
189 int i; | |
190 | |
191 /* Close up audio */ | |
192 if ( audio_sem ) { | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
193 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
194 CloseSynchHandle(audio_sem); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
195 #else |
0 | 196 CloseHandle(audio_sem); |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
197 #endif |
0 | 198 } |
199 if ( sound ) { | |
200 waveOutClose(sound); | |
201 } | |
202 | |
203 /* Clean up mixing buffers */ | |
204 for ( i=0; i<NUM_BUFFERS; ++i ) { | |
205 if ( wavebuf[i].dwUser != 0xFFFF ) { | |
206 waveOutUnprepareHeader(sound, &wavebuf[i], | |
207 sizeof(wavebuf[i])); | |
208 wavebuf[i].dwUser = 0xFFFF; | |
209 } | |
210 } | |
211 /* Free raw mixing buffer */ | |
212 if ( mixbuf != NULL ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
213 SDL_free(mixbuf); |
0 | 214 mixbuf = NULL; |
215 } | |
216 } | |
217 | |
218 int DIB_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
219 { | |
220 MMRESULT result; | |
221 int i; | |
222 WAVEFORMATEX waveformat; | |
223 | |
224 /* Initialize the wavebuf structures for closing */ | |
225 sound = NULL; | |
226 audio_sem = NULL; | |
227 for ( i = 0; i < NUM_BUFFERS; ++i ) | |
228 wavebuf[i].dwUser = 0xFFFF; | |
229 mixbuf = NULL; | |
230 | |
231 /* Set basic WAVE format parameters */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
232 SDL_memset(&waveformat, 0, sizeof(waveformat)); |
0 | 233 waveformat.wFormatTag = WAVE_FORMAT_PCM; |
234 | |
235 /* Determine the audio parameters from the AudioSpec */ | |
236 switch ( spec->format & 0xFF ) { | |
237 case 8: | |
238 /* Unsigned 8 bit audio data */ | |
239 spec->format = AUDIO_U8; | |
240 waveformat.wBitsPerSample = 8; | |
241 break; | |
242 case 16: | |
243 /* Signed 16 bit audio data */ | |
244 spec->format = AUDIO_S16; | |
245 waveformat.wBitsPerSample = 16; | |
246 break; | |
247 default: | |
248 SDL_SetError("Unsupported audio format"); | |
249 return(-1); | |
250 } | |
251 waveformat.nChannels = spec->channels; | |
252 waveformat.nSamplesPerSec = spec->freq; | |
253 waveformat.nBlockAlign = | |
254 waveformat.nChannels * (waveformat.wBitsPerSample/8); | |
255 waveformat.nAvgBytesPerSec = | |
256 waveformat.nSamplesPerSec * waveformat.nBlockAlign; | |
257 | |
258 /* Check the buffer size -- minimum of 1/4 second (word aligned) */ | |
259 if ( spec->samples < (spec->freq/4) ) | |
260 spec->samples = ((spec->freq/4)+3)&~3; | |
261 | |
262 /* Update the fragment size as size in bytes */ | |
263 SDL_CalculateAudioSpec(spec); | |
264 | |
265 /* Open the audio device */ | |
266 result = waveOutOpen(&sound, WAVE_MAPPER, &waveformat, | |
267 (DWORD)FillSound, (DWORD)this, CALLBACK_FUNCTION); | |
268 if ( result != MMSYSERR_NOERROR ) { | |
269 SetMMerror("waveOutOpen()", result); | |
270 return(-1); | |
271 } | |
272 | |
273 #ifdef SOUND_DEBUG | |
274 /* Check the sound device we retrieved */ | |
275 { | |
276 WAVEOUTCAPS caps; | |
277 | |
278 result = waveOutGetDevCaps((UINT)sound, &caps, sizeof(caps)); | |
279 if ( result != MMSYSERR_NOERROR ) { | |
280 SetMMerror("waveOutGetDevCaps()", result); | |
281 return(-1); | |
282 } | |
283 printf("Audio device: %s\n", caps.szPname); | |
284 } | |
285 #endif | |
286 | |
287 /* Create the audio buffer semaphore */ | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
288 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
289 audio_sem = CreateSemaphoreCE(NULL, NUM_BUFFERS-1, NUM_BUFFERS, NULL); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
290 #else |
0 | 291 audio_sem = CreateSemaphore(NULL, NUM_BUFFERS-1, NUM_BUFFERS, NULL); |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
292 #endif |
0 | 293 if ( audio_sem == NULL ) { |
294 SDL_SetError("Couldn't create semaphore"); | |
295 return(-1); | |
296 } | |
297 | |
298 /* Create the sound buffers */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
299 mixbuf = (Uint8 *)SDL_malloc(NUM_BUFFERS*spec->size); |
0 | 300 if ( mixbuf == NULL ) { |
301 SDL_SetError("Out of memory"); | |
302 return(-1); | |
303 } | |
304 for ( i = 0; i < NUM_BUFFERS; ++i ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
305 SDL_memset(&wavebuf[i], 0, sizeof(wavebuf[i])); |
0 | 306 wavebuf[i].lpData = (LPSTR) &mixbuf[i*spec->size]; |
307 wavebuf[i].dwBufferLength = spec->size; | |
308 wavebuf[i].dwFlags = WHDR_DONE; | |
309 result = waveOutPrepareHeader(sound, &wavebuf[i], | |
310 sizeof(wavebuf[i])); | |
311 if ( result != MMSYSERR_NOERROR ) { | |
312 SetMMerror("waveOutPrepareHeader()", result); | |
313 return(-1); | |
314 } | |
315 } | |
316 | |
317 /* Ready to go! */ | |
318 next_buffer = 0; | |
319 return(0); | |
320 } |