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

Latest commit

 

History

History
338 lines (290 loc) · 9.74 KB

SDL_dibaudio.c

File metadata and controls

338 lines (290 loc) · 9.74 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 1, 2006
Feb 1, 2006
3
Copyright (C) 1997-2006 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
{
Oct 28, 2006
Oct 28, 2006
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
139
140
--left;
}
}
if (left > 0) {
SDL_Delay(100);
}
}
while (left > 0);
Apr 26, 2001
Apr 26, 2001
141
142
}
Jul 10, 2006
Jul 10, 2006
143
void
Oct 17, 2006
Oct 17, 2006
144
WINWAVEOUT_CloseDevice(_THIS)
Apr 26, 2001
Apr 26, 2001
145
{
Jul 10, 2006
Jul 10, 2006
146
/* Close up audio */
Oct 17, 2006
Oct 17, 2006
147
148
149
150
if (this->hidden != NULL) {
int i;
if (this->hidden->audio_sem) {
May 23, 2001
May 23, 2001
151
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
Oct 17, 2006
Oct 17, 2006
152
CloseSynchHandle(this->hidden->audio_sem);
May 23, 2001
May 23, 2001
153
#else
Oct 17, 2006
Oct 17, 2006
154
CloseHandle(this->hidden->audio_sem);
May 23, 2001
May 23, 2001
155
#endif
Oct 17, 2006
Oct 17, 2006
156
157
this->hidden->audio_sem = 0;
}
Jul 10, 2006
Jul 10, 2006
158
Oct 17, 2006
Oct 17, 2006
159
160
161
if (this->hidden->sound) {
waveOutClose(this->hidden->sound);
this->hidden->sound = 0;
Jul 10, 2006
Jul 10, 2006
162
}
Oct 17, 2006
Oct 17, 2006
163
164
165
166
167
168
/* 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
169
sizeof(this->hidden->wavebuf[i]));
Oct 17, 2006
Oct 17, 2006
170
171
172
173
174
175
176
177
178
179
180
181
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
182
}
Apr 26, 2001
Apr 26, 2001
183
184
}
Jul 10, 2006
Jul 10, 2006
185
int
Oct 17, 2006
Oct 17, 2006
186
WINWAVEOUT_OpenDevice(_THIS, const char *devname, int iscapture)
Apr 26, 2001
Apr 26, 2001
187
{
Oct 17, 2006
Oct 17, 2006
188
189
SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
int valid_datatype = 0;
Jul 10, 2006
Jul 10, 2006
190
191
MMRESULT result;
WAVEFORMATEX waveformat;
Oct 17, 2006
Oct 17, 2006
192
193
194
195
int i;
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
Oct 28, 2006
Oct 28, 2006
196
SDL_malloc((sizeof *this->hidden));
Oct 17, 2006
Oct 17, 2006
197
198
199
200
201
if (this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
Jul 10, 2006
Jul 10, 2006
202
203
204
/* Initialize the wavebuf structures for closing */
for (i = 0; i < NUM_BUFFERS; ++i)
Oct 17, 2006
Oct 17, 2006
205
206
207
208
this->hidden->wavebuf[i].dwUser = 0xFFFF;
while ((!valid_datatype) && (test_format)) {
valid_datatype = 1;
Oct 18, 2006
Oct 18, 2006
209
this->spec.format = test_format;
Oct 17, 2006
Oct 17, 2006
210
switch (test_format) {
Oct 28, 2006
Oct 28, 2006
211
212
213
214
215
216
217
218
219
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
220
221
222
223
224
225
226
227
}
}
if (!valid_datatype) {
WINWAVEOUT_CloseDevice(this);
SDL_SetError("Unsupported audio format");
return 0;
}
Jul 10, 2006
Jul 10, 2006
228
229
/* Set basic WAVE format parameters */
Oct 28, 2006
Oct 28, 2006
230
SDL_memset(&waveformat, '\0', sizeof(waveformat));
Jul 10, 2006
Jul 10, 2006
231
waveformat.wFormatTag = WAVE_FORMAT_PCM;
Oct 17, 2006
Oct 17, 2006
232
waveformat.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
Jul 10, 2006
Jul 10, 2006
233
Oct 17, 2006
Oct 17, 2006
234
if (this->spec.channels > 2)
Oct 28, 2006
Oct 28, 2006
235
this->spec.channels = 2; /* !!! FIXME: is this right? */
Oct 17, 2006
Oct 17, 2006
236
237
238
waveformat.nChannels = this->spec.channels;
waveformat.nSamplesPerSec = this->spec.freq;
Jul 10, 2006
Jul 10, 2006
239
240
241
242
243
244
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
245
246
if (this->spec.samples < (this->spec.freq / 4))
this->spec.samples = ((this->spec.freq / 4) + 3) & ~3;
Jul 10, 2006
Jul 10, 2006
247
248
/* Update the fragment size as size in bytes */
Oct 17, 2006
Oct 17, 2006
249
SDL_CalculateAudioSpec(&this->spec);
Jul 10, 2006
Jul 10, 2006
250
251
/* Open the audio device */
Oct 17, 2006
Oct 17, 2006
252
result = waveOutOpen(&this->hidden->sound, WAVE_MAPPER, &waveformat,
Jul 10, 2006
Jul 10, 2006
253
254
255
(DWORD_PTR) FillSound, (DWORD_PTR) this,
CALLBACK_FUNCTION);
if (result != MMSYSERR_NOERROR) {
Oct 17, 2006
Oct 17, 2006
256
WINWAVEOUT_CloseDevice(this);
Jul 10, 2006
Jul 10, 2006
257
SetMMerror("waveOutOpen()", result);
Oct 17, 2006
Oct 17, 2006
258
return 0;
Jul 10, 2006
Jul 10, 2006
259
}
Apr 26, 2001
Apr 26, 2001
260
#ifdef SOUND_DEBUG
Jul 10, 2006
Jul 10, 2006
261
262
263
264
/* Check the sound device we retrieved */
{
WAVEOUTCAPS caps;
Oct 17, 2006
Oct 17, 2006
265
266
result = waveOutGetDevCaps((UINT) this->hidden->sound,
&caps, sizeof(caps));
Jul 10, 2006
Jul 10, 2006
267
if (result != MMSYSERR_NOERROR) {
Oct 17, 2006
Oct 17, 2006
268
WINWAVEOUT_CloseDevice(this);
Jul 10, 2006
Jul 10, 2006
269
SetMMerror("waveOutGetDevCaps()", result);
Oct 17, 2006
Oct 17, 2006
270
return 0;
Jul 10, 2006
Jul 10, 2006
271
272
273
}
printf("Audio device: %s\n", caps.szPname);
}
Apr 26, 2001
Apr 26, 2001
274
275
#endif
Jul 10, 2006
Jul 10, 2006
276
/* Create the audio buffer semaphore */
Oct 28, 2006
Oct 28, 2006
277
this->hidden->audio_sem =
May 23, 2001
May 23, 2001
278
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
Oct 28, 2006
Oct 28, 2006
279
CreateSemaphoreCE(NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL);
May 23, 2001
May 23, 2001
280
#else
Oct 28, 2006
Oct 28, 2006
281
CreateSemaphore(NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL);
May 23, 2001
May 23, 2001
282
#endif
Oct 17, 2006
Oct 17, 2006
283
284
if (this->hidden->audio_sem == NULL) {
WINWAVEOUT_CloseDevice(this);
Jul 10, 2006
Jul 10, 2006
285
SDL_SetError("Couldn't create semaphore");
Oct 17, 2006
Oct 17, 2006
286
return 0;
Jul 10, 2006
Jul 10, 2006
287
288
289
}
/* Create the sound buffers */
Oct 28, 2006
Oct 28, 2006
290
291
this->hidden->mixbuf =
(Uint8 *) SDL_malloc(NUM_BUFFERS * this->spec.size);
Oct 18, 2006
Oct 18, 2006
292
if (this->hidden->mixbuf == NULL) {
Oct 17, 2006
Oct 17, 2006
293
294
295
WINWAVEOUT_CloseDevice(this);
SDL_OutOfMemory();
return 0;
Jul 10, 2006
Jul 10, 2006
296
297
}
for (i = 0; i < NUM_BUFFERS; ++i) {
Oct 17, 2006
Oct 17, 2006
298
SDL_memset(&this->hidden->wavebuf[i], '\0',
Oct 28, 2006
Oct 28, 2006
299
sizeof(this->hidden->wavebuf[i]));
Oct 17, 2006
Oct 17, 2006
300
301
302
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
303
(LPSTR) & this->hidden->mixbuf[i * this->spec.size];
Oct 17, 2006
Oct 17, 2006
304
305
result = waveOutPrepareHeader(this->hidden->sound,
&this->hidden->wavebuf[i],
Oct 28, 2006
Oct 28, 2006
306
sizeof(this->hidden->wavebuf[i]));
Jul 10, 2006
Jul 10, 2006
307
if (result != MMSYSERR_NOERROR) {
Oct 17, 2006
Oct 17, 2006
308
WINWAVEOUT_CloseDevice(this);
Jul 10, 2006
Jul 10, 2006
309
SetMMerror("waveOutPrepareHeader()", result);
Oct 17, 2006
Oct 17, 2006
310
return 0;
Jul 10, 2006
Jul 10, 2006
311
312
313
}
}
Oct 28, 2006
Oct 28, 2006
314
return 1; /* Ready to go! */
Apr 26, 2001
Apr 26, 2001
315
}
Jul 10, 2006
Jul 10, 2006
316
Oct 17, 2006
Oct 17, 2006
317
318
static int
Oct 28, 2006
Oct 28, 2006
319
WINWAVEOUT_Init(SDL_AudioDriverImpl * impl)
Oct 17, 2006
Oct 17, 2006
320
321
322
323
324
325
326
327
328
{
/* 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
329
impl->OnlyHasDefaultOutputDevice = 1; /* !!! FIXME: Is this true? */
Oct 17, 2006
Oct 17, 2006
330
331
332
333
334
335
336
337
return 1;
}
AudioBootStrap WINWAVEOUT_bootstrap = {
"waveout", WINDOWS_OS_NAME " WaveOut", WINWAVEOUT_Init, 0
};
Jul 10, 2006
Jul 10, 2006
338
/* vi: set ts=4 sw=4 expandtab: */