This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_dibaudio.c
329 lines (281 loc) · 9.62 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2011 Sam Lantinga
4
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.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
23
24
25
/* Allow access to a raw mixing buffer */
26
#include "../../core/windows/SDL_windows.h"
27
28
29
#include <mmsystem.h>
#include "SDL_timer.h"
30
#include "SDL_audio.h"
31
#include "../SDL_audio_c.h"
32
#include "SDL_dibaudio.h"
33
34
35
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
#include "win_ce_semaphore.h"
#endif
36
37
38
39
40
41
42
43
#if defined(_WIN32_WCE)
#define WINDOWS_OS_NAME "Windows CE/PocketPC"
#elif defined(WIN64)
#define WINDOWS_OS_NAME "Win64"
#else
#define WINDOWS_OS_NAME "Win32"
#endif
44
45
/* The Win32 callback for filling the WAVE device */
46
47
48
static void CALLBACK
FillSound(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
DWORD dwParam1, DWORD dwParam2)
49
{
50
SDL_AudioDevice *this = (SDL_AudioDevice *) dwInstance;
51
52
53
54
/* Only service "buffer done playing" messages */
if (uMsg != WOM_DONE)
return;
55
56
/* Signal that we are done playing a buffer */
57
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
58
ReleaseSemaphoreCE(this->hidden->audio_sem, 1, NULL);
59
#else
60
ReleaseSemaphore(this->hidden->audio_sem, 1, NULL);
61
#endif
62
63
}
64
65
static void
SetMMerror(char *function, MMRESULT code)
66
{
67
68
69
size_t len;
char errbuf[MAXERRORLENGTH];
wchar_t werrbuf[MAXERRORLENGTH];
70
71
72
SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
len = SDL_strlen(errbuf);
73
74
75
76
waveOutGetErrorText(code, werrbuf, MAXERRORLENGTH - len);
WideCharToMultiByte(CP_ACP, 0, werrbuf, -1, errbuf + len,
MAXERRORLENGTH - len, NULL, NULL);
77
78
SDL_SetError("%s", errbuf);
79
80
81
}
/* Set high priority for the audio thread */
82
static void
83
WINWAVEOUT_ThreadInit(_THIS)
84
{
85
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
86
87
}
88
void
89
WINWAVEOUT_WaitDevice(_THIS)
90
{
91
/* Wait for an audio chunk to finish */
92
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
93
WaitForSemaphoreCE(this->hidden->audio_sem, INFINITE);
94
#else
95
WaitForSingleObject(this->hidden->audio_sem, INFINITE);
96
#endif
97
98
}
99
Uint8 *
100
WINWAVEOUT_GetDeviceBuf(_THIS)
101
{
102
103
return (Uint8 *) (this->hidden->
wavebuf[this->hidden->next_buffer].lpData);
104
105
}
106
void
107
WINWAVEOUT_PlayDevice(_THIS)
108
{
109
/* Queue it up */
110
111
waveOutWrite(this->hidden->sound,
&this->hidden->wavebuf[this->hidden->next_buffer],
112
sizeof(this->hidden->wavebuf[0]));
113
this->hidden->next_buffer = (this->hidden->next_buffer + 1) % NUM_BUFFERS;
114
115
}
116
void
117
WINWAVEOUT_WaitDone(_THIS)
118
{
119
120
121
122
123
int i, left;
do {
left = NUM_BUFFERS;
for (i = 0; i < NUM_BUFFERS; ++i) {
124
if (this->hidden->wavebuf[i].dwFlags & WHDR_DONE) {
125
126
127
128
129
130
--left;
}
}
if (left > 0) {
SDL_Delay(100);
}
131
} while (left > 0);
132
133
}
134
void
135
WINWAVEOUT_CloseDevice(_THIS)
136
{
137
/* Close up audio */
138
139
140
141
if (this->hidden != NULL) {
int i;
if (this->hidden->audio_sem) {
142
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
143
CloseSynchHandle(this->hidden->audio_sem);
144
#else
145
CloseHandle(this->hidden->audio_sem);
146
#endif
147
148
this->hidden->audio_sem = 0;
}
149
150
151
152
if (this->hidden->sound) {
waveOutClose(this->hidden->sound);
this->hidden->sound = 0;
153
}
154
155
156
157
158
159
/* Clean up mixing buffers */
for (i = 0; i < NUM_BUFFERS; ++i) {
if (this->hidden->wavebuf[i].dwUser != 0xFFFF) {
waveOutUnprepareHeader(this->hidden->sound,
&this->hidden->wavebuf[i],
160
sizeof(this->hidden->wavebuf[i]));
161
162
163
164
165
166
167
168
169
170
171
172
this->hidden->wavebuf[i].dwUser = 0xFFFF;
}
}
if (this->hidden->mixbuf != NULL) {
/* Free raw mixing buffer */
SDL_free(this->hidden->mixbuf);
this->hidden->mixbuf = NULL;
}
SDL_free(this->hidden);
this->hidden = NULL;
173
}
174
175
}
176
int
177
WINWAVEOUT_OpenDevice(_THIS, const char *devname, int iscapture)
178
{
179
180
SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
int valid_datatype = 0;
181
182
MMRESULT result;
WAVEFORMATEX waveformat;
183
184
185
186
int i;
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
187
SDL_malloc((sizeof *this->hidden));
188
189
190
191
192
if (this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
193
194
195
/* Initialize the wavebuf structures for closing */
for (i = 0; i < NUM_BUFFERS; ++i)
196
197
198
199
this->hidden->wavebuf[i].dwUser = 0xFFFF;
while ((!valid_datatype) && (test_format)) {
valid_datatype = 1;
200
this->spec.format = test_format;
201
switch (test_format) {
202
203
204
205
206
207
208
209
210
case AUDIO_U8:
case AUDIO_S16:
case AUDIO_S32:
break; /* valid. */
default:
valid_datatype = 0;
test_format = SDL_NextAudioFormat();
break;
211
212
213
214
215
216
217
218
}
}
if (!valid_datatype) {
WINWAVEOUT_CloseDevice(this);
SDL_SetError("Unsupported audio format");
return 0;
}
219
220
/* Set basic WAVE format parameters */
221
SDL_memset(&waveformat, '\0', sizeof(waveformat));
222
waveformat.wFormatTag = WAVE_FORMAT_PCM;
223
waveformat.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
224
225
if (this->spec.channels > 2)
226
this->spec.channels = 2; /* !!! FIXME: is this right? */
227
228
229
waveformat.nChannels = this->spec.channels;
waveformat.nSamplesPerSec = this->spec.freq;
230
231
232
233
234
235
waveformat.nBlockAlign =
waveformat.nChannels * (waveformat.wBitsPerSample / 8);
waveformat.nAvgBytesPerSec =
waveformat.nSamplesPerSec * waveformat.nBlockAlign;
/* Check the buffer size -- minimum of 1/4 second (word aligned) */
236
237
if (this->spec.samples < (this->spec.freq / 4))
this->spec.samples = ((this->spec.freq / 4) + 3) & ~3;
238
239
/* Update the fragment size as size in bytes */
240
SDL_CalculateAudioSpec(&this->spec);
241
242
/* Open the audio device */
243
result = waveOutOpen(&this->hidden->sound, WAVE_MAPPER, &waveformat,
244
245
246
(DWORD_PTR) FillSound, (DWORD_PTR) this,
CALLBACK_FUNCTION);
if (result != MMSYSERR_NOERROR) {
247
WINWAVEOUT_CloseDevice(this);
248
SetMMerror("waveOutOpen()", result);
249
return 0;
250
}
251
#ifdef SOUND_DEBUG
252
253
254
255
/* Check the sound device we retrieved */
{
WAVEOUTCAPS caps;
256
257
result = waveOutGetDevCaps((UINT) this->hidden->sound,
&caps, sizeof(caps));
258
if (result != MMSYSERR_NOERROR) {
259
WINWAVEOUT_CloseDevice(this);
260
SetMMerror("waveOutGetDevCaps()", result);
261
return 0;
262
263
264
}
printf("Audio device: %s\n", caps.szPname);
}
265
266
#endif
267
/* Create the audio buffer semaphore */
268
this->hidden->audio_sem =
269
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
270
CreateSemaphoreCE(NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL);
271
#else
272
CreateSemaphore(NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL);
273
#endif
274
275
if (this->hidden->audio_sem == NULL) {
WINWAVEOUT_CloseDevice(this);
276
SDL_SetError("Couldn't create semaphore");
277
return 0;
278
279
280
}
/* Create the sound buffers */
281
282
this->hidden->mixbuf =
(Uint8 *) SDL_malloc(NUM_BUFFERS * this->spec.size);
283
if (this->hidden->mixbuf == NULL) {
284
285
286
WINWAVEOUT_CloseDevice(this);
SDL_OutOfMemory();
return 0;
287
288
}
for (i = 0; i < NUM_BUFFERS; ++i) {
289
SDL_memset(&this->hidden->wavebuf[i], '\0',
290
sizeof(this->hidden->wavebuf[i]));
291
292
293
this->hidden->wavebuf[i].dwBufferLength = this->spec.size;
this->hidden->wavebuf[i].dwFlags = WHDR_DONE;
this->hidden->wavebuf[i].lpData =
294
(LPSTR) & this->hidden->mixbuf[i * this->spec.size];
295
296
result = waveOutPrepareHeader(this->hidden->sound,
&this->hidden->wavebuf[i],
297
sizeof(this->hidden->wavebuf[i]));
298
if (result != MMSYSERR_NOERROR) {
299
WINWAVEOUT_CloseDevice(this);
300
SetMMerror("waveOutPrepareHeader()", result);
301
return 0;
302
303
304
}
}
305
return 1; /* Ready to go! */
306
}
307
308
309
static int
310
WINWAVEOUT_Init(SDL_AudioDriverImpl * impl)
311
312
313
314
315
316
317
318
319
{
/* Set the function pointers */
impl->OpenDevice = WINWAVEOUT_OpenDevice;
impl->ThreadInit = WINWAVEOUT_ThreadInit;
impl->PlayDevice = WINWAVEOUT_PlayDevice;
impl->WaitDevice = WINWAVEOUT_WaitDevice;
impl->WaitDone = WINWAVEOUT_WaitDone;
impl->GetDeviceBuf = WINWAVEOUT_GetDeviceBuf;
impl->CloseDevice = WINWAVEOUT_CloseDevice;
320
impl->OnlyHasDefaultOutputDevice = 1; /* !!! FIXME: Is this true? */
321
322
return 1; /* this audio target is available. */
323
324
325
326
327
328
}
AudioBootStrap WINWAVEOUT_bootstrap = {
"waveout", WINDOWS_OS_NAME " WaveOut", WINWAVEOUT_Init, 0
};
329
/* vi: set ts=4 sw=4 expandtab: */