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