Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
337 lines (289 loc) · 9.78 KB

SDL_dibaudio.c

File metadata and controls

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