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

Latest commit

 

History

History
329 lines (281 loc) · 9.62 KB

SDL_dibaudio.c

File metadata and controls

329 lines (281 loc) · 9.62 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 12, 2011
Feb 12, 2011
3
Copyright (C) 1997-2011 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 */
Jan 25, 2011
Jan 25, 2011
26
#include "../../core/windows/SDL_windows.h"
Apr 26, 2001
Apr 26, 2001
27
28
29
#include <mmsystem.h>
#include "SDL_timer.h"
Feb 10, 2006
Feb 10, 2006
30
#include "SDL_audio.h"
Feb 16, 2006
Feb 16, 2006
31
#include "../SDL_audio_c.h"
Apr 26, 2001
Apr 26, 2001
32
#include "SDL_dibaudio.h"
May 23, 2001
May 23, 2001
33
34
35
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
#include "win_ce_semaphore.h"
#endif
Apr 26, 2001
Apr 26, 2001
36
Oct 17, 2006
Oct 17, 2006
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
Apr 26, 2001
Apr 26, 2001
44
45
/* The Win32 callback for filling the WAVE device */
Jul 10, 2006
Jul 10, 2006
46
47
48
static void CALLBACK
FillSound(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
DWORD dwParam1, DWORD dwParam2)
Apr 26, 2001
Apr 26, 2001
49
{
Jul 10, 2006
Jul 10, 2006
50
SDL_AudioDevice *this = (SDL_AudioDevice *) dwInstance;
Apr 26, 2001
Apr 26, 2001
51
Jul 10, 2006
Jul 10, 2006
52
53
54
/* Only service "buffer done playing" messages */
if (uMsg != WOM_DONE)
return;
Apr 26, 2001
Apr 26, 2001
55
Jul 10, 2006
Jul 10, 2006
56
/* Signal that we are done playing a buffer */
May 23, 2001
May 23, 2001
57
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
Oct 17, 2006
Oct 17, 2006
58
ReleaseSemaphoreCE(this->hidden->audio_sem, 1, NULL);
May 23, 2001
May 23, 2001
59
#else
Oct 17, 2006
Oct 17, 2006
60
ReleaseSemaphore(this->hidden->audio_sem, 1, NULL);
May 23, 2001
May 23, 2001
61
#endif
Apr 26, 2001
Apr 26, 2001
62
63
}
Jul 10, 2006
Jul 10, 2006
64
65
static void
SetMMerror(char *function, MMRESULT code)
Apr 26, 2001
Apr 26, 2001
66
{
Jul 10, 2006
Jul 10, 2006
67
68
69
size_t len;
char errbuf[MAXERRORLENGTH];
wchar_t werrbuf[MAXERRORLENGTH];
Apr 26, 2001
Apr 26, 2001
70
Jul 10, 2006
Jul 10, 2006
71
72
SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
len = SDL_strlen(errbuf);
May 23, 2001
May 23, 2001
73
Jul 10, 2006
Jul 10, 2006
74
75
76
waveOutGetErrorText(code, werrbuf, MAXERRORLENGTH - len);
WideCharToMultiByte(CP_ACP, 0, werrbuf, -1, errbuf + len,
MAXERRORLENGTH - len, NULL, NULL);
May 23, 2001
May 23, 2001
77
Jul 10, 2006
Jul 10, 2006
78
SDL_SetError("%s", errbuf);
Apr 26, 2001
Apr 26, 2001
79
80
81
}
/* Set high priority for the audio thread */
Jul 10, 2006
Jul 10, 2006
82
static void
Oct 17, 2006
Oct 17, 2006
83
WINWAVEOUT_ThreadInit(_THIS)
Apr 26, 2001
Apr 26, 2001
84
{
Jul 10, 2006
Jul 10, 2006
85
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
Apr 26, 2001
Apr 26, 2001
86
87
}
Jul 10, 2006
Jul 10, 2006
88
void
Oct 17, 2006
Oct 17, 2006
89
WINWAVEOUT_WaitDevice(_THIS)
Apr 26, 2001
Apr 26, 2001
90
{
Jul 10, 2006
Jul 10, 2006
91
/* Wait for an audio chunk to finish */
May 23, 2001
May 23, 2001
92
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
Oct 17, 2006
Oct 17, 2006
93
WaitForSemaphoreCE(this->hidden->audio_sem, INFINITE);
May 23, 2001
May 23, 2001
94
#else
Oct 17, 2006
Oct 17, 2006
95
WaitForSingleObject(this->hidden->audio_sem, INFINITE);
May 23, 2001
May 23, 2001
96
#endif
Apr 26, 2001
Apr 26, 2001
97
98
}
Jul 10, 2006
Jul 10, 2006
99
Uint8 *
Oct 17, 2006
Oct 17, 2006
100
WINWAVEOUT_GetDeviceBuf(_THIS)
Apr 26, 2001
Apr 26, 2001
101
{
Jan 10, 2009
Jan 10, 2009
102
103
return (Uint8 *) (this->hidden->
wavebuf[this->hidden->next_buffer].lpData);
Apr 26, 2001
Apr 26, 2001
104
105
}
Jul 10, 2006
Jul 10, 2006
106
void
Oct 17, 2006
Oct 17, 2006
107
WINWAVEOUT_PlayDevice(_THIS)
Apr 26, 2001
Apr 26, 2001
108
{
Jul 10, 2006
Jul 10, 2006
109
/* Queue it up */
Oct 17, 2006
Oct 17, 2006
110
111
waveOutWrite(this->hidden->sound,
&this->hidden->wavebuf[this->hidden->next_buffer],
Oct 28, 2006
Oct 28, 2006
112
sizeof(this->hidden->wavebuf[0]));
Oct 17, 2006
Oct 17, 2006
113
this->hidden->next_buffer = (this->hidden->next_buffer + 1) % NUM_BUFFERS;
Apr 26, 2001
Apr 26, 2001
114
115
}
Jul 10, 2006
Jul 10, 2006
116
void
Oct 17, 2006
Oct 17, 2006
117
WINWAVEOUT_WaitDone(_THIS)
Apr 26, 2001
Apr 26, 2001
118
{
Jul 10, 2006
Jul 10, 2006
119
120
121
122
123
int i, left;
do {
left = NUM_BUFFERS;
for (i = 0; i < NUM_BUFFERS; ++i) {
Oct 17, 2006
Oct 17, 2006
124
if (this->hidden->wavebuf[i].dwFlags & WHDR_DONE) {
Jul 10, 2006
Jul 10, 2006
125
126
127
128
129
130
--left;
}
}
if (left > 0) {
SDL_Delay(100);
}
Aug 27, 2008
Aug 27, 2008
131
} while (left > 0);
Apr 26, 2001
Apr 26, 2001
132
133
}
Jul 10, 2006
Jul 10, 2006
134
void
Oct 17, 2006
Oct 17, 2006
135
WINWAVEOUT_CloseDevice(_THIS)
Apr 26, 2001
Apr 26, 2001
136
{
Jul 10, 2006
Jul 10, 2006
137
/* Close up audio */
Oct 17, 2006
Oct 17, 2006
138
139
140
141
if (this->hidden != NULL) {
int i;
if (this->hidden->audio_sem) {
May 23, 2001
May 23, 2001
142
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
Oct 17, 2006
Oct 17, 2006
143
CloseSynchHandle(this->hidden->audio_sem);
May 23, 2001
May 23, 2001
144
#else
Oct 17, 2006
Oct 17, 2006
145
CloseHandle(this->hidden->audio_sem);
May 23, 2001
May 23, 2001
146
#endif
Oct 17, 2006
Oct 17, 2006
147
148
this->hidden->audio_sem = 0;
}
Jul 10, 2006
Jul 10, 2006
149
Oct 17, 2006
Oct 17, 2006
150
151
152
if (this->hidden->sound) {
waveOutClose(this->hidden->sound);
this->hidden->sound = 0;
Jul 10, 2006
Jul 10, 2006
153
}
Oct 17, 2006
Oct 17, 2006
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],
Oct 28, 2006
Oct 28, 2006
160
sizeof(this->hidden->wavebuf[i]));
Oct 17, 2006
Oct 17, 2006
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;
Jul 10, 2006
Jul 10, 2006
173
}
Apr 26, 2001
Apr 26, 2001
174
175
}
Jul 10, 2006
Jul 10, 2006
176
int
Oct 17, 2006
Oct 17, 2006
177
WINWAVEOUT_OpenDevice(_THIS, const char *devname, int iscapture)
Apr 26, 2001
Apr 26, 2001
178
{
Oct 17, 2006
Oct 17, 2006
179
180
SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
int valid_datatype = 0;
Jul 10, 2006
Jul 10, 2006
181
182
MMRESULT result;
WAVEFORMATEX waveformat;
Oct 17, 2006
Oct 17, 2006
183
184
185
186
int i;
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
Oct 28, 2006
Oct 28, 2006
187
SDL_malloc((sizeof *this->hidden));
Oct 17, 2006
Oct 17, 2006
188
189
190
191
192
if (this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
Jul 10, 2006
Jul 10, 2006
193
194
195
/* Initialize the wavebuf structures for closing */
for (i = 0; i < NUM_BUFFERS; ++i)
Oct 17, 2006
Oct 17, 2006
196
197
198
199
this->hidden->wavebuf[i].dwUser = 0xFFFF;
while ((!valid_datatype) && (test_format)) {
valid_datatype = 1;
Oct 18, 2006
Oct 18, 2006
200
this->spec.format = test_format;
Oct 17, 2006
Oct 17, 2006
201
switch (test_format) {
Oct 28, 2006
Oct 28, 2006
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;
Oct 17, 2006
Oct 17, 2006
211
212
213
214
215
216
217
218
}
}
if (!valid_datatype) {
WINWAVEOUT_CloseDevice(this);
SDL_SetError("Unsupported audio format");
return 0;
}
Jul 10, 2006
Jul 10, 2006
219
220
/* Set basic WAVE format parameters */
Oct 28, 2006
Oct 28, 2006
221
SDL_memset(&waveformat, '\0', sizeof(waveformat));
Jul 10, 2006
Jul 10, 2006
222
waveformat.wFormatTag = WAVE_FORMAT_PCM;
Oct 17, 2006
Oct 17, 2006
223
waveformat.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
Jul 10, 2006
Jul 10, 2006
224
Oct 17, 2006
Oct 17, 2006
225
if (this->spec.channels > 2)
Oct 28, 2006
Oct 28, 2006
226
this->spec.channels = 2; /* !!! FIXME: is this right? */
Oct 17, 2006
Oct 17, 2006
227
228
229
waveformat.nChannels = this->spec.channels;
waveformat.nSamplesPerSec = this->spec.freq;
Jul 10, 2006
Jul 10, 2006
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) */
Oct 17, 2006
Oct 17, 2006
236
237
if (this->spec.samples < (this->spec.freq / 4))
this->spec.samples = ((this->spec.freq / 4) + 3) & ~3;
Jul 10, 2006
Jul 10, 2006
238
239
/* Update the fragment size as size in bytes */
Oct 17, 2006
Oct 17, 2006
240
SDL_CalculateAudioSpec(&this->spec);
Jul 10, 2006
Jul 10, 2006
241
242
/* Open the audio device */
Oct 17, 2006
Oct 17, 2006
243
result = waveOutOpen(&this->hidden->sound, WAVE_MAPPER, &waveformat,
Jul 10, 2006
Jul 10, 2006
244
245
246
(DWORD_PTR) FillSound, (DWORD_PTR) this,
CALLBACK_FUNCTION);
if (result != MMSYSERR_NOERROR) {
Oct 17, 2006
Oct 17, 2006
247
WINWAVEOUT_CloseDevice(this);
Jul 10, 2006
Jul 10, 2006
248
SetMMerror("waveOutOpen()", result);
Oct 17, 2006
Oct 17, 2006
249
return 0;
Jul 10, 2006
Jul 10, 2006
250
}
Apr 26, 2001
Apr 26, 2001
251
#ifdef SOUND_DEBUG
Jul 10, 2006
Jul 10, 2006
252
253
254
255
/* Check the sound device we retrieved */
{
WAVEOUTCAPS caps;
Oct 17, 2006
Oct 17, 2006
256
257
result = waveOutGetDevCaps((UINT) this->hidden->sound,
&caps, sizeof(caps));
Jul 10, 2006
Jul 10, 2006
258
if (result != MMSYSERR_NOERROR) {
Oct 17, 2006
Oct 17, 2006
259
WINWAVEOUT_CloseDevice(this);
Jul 10, 2006
Jul 10, 2006
260
SetMMerror("waveOutGetDevCaps()", result);
Oct 17, 2006
Oct 17, 2006
261
return 0;
Jul 10, 2006
Jul 10, 2006
262
263
264
}
printf("Audio device: %s\n", caps.szPname);
}
Apr 26, 2001
Apr 26, 2001
265
266
#endif
Jul 10, 2006
Jul 10, 2006
267
/* Create the audio buffer semaphore */
Oct 28, 2006
Oct 28, 2006
268
this->hidden->audio_sem =
May 23, 2001
May 23, 2001
269
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
Oct 28, 2006
Oct 28, 2006
270
CreateSemaphoreCE(NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL);
May 23, 2001
May 23, 2001
271
#else
Oct 28, 2006
Oct 28, 2006
272
CreateSemaphore(NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL);
May 23, 2001
May 23, 2001
273
#endif
Oct 17, 2006
Oct 17, 2006
274
275
if (this->hidden->audio_sem == NULL) {
WINWAVEOUT_CloseDevice(this);
Jul 10, 2006
Jul 10, 2006
276
SDL_SetError("Couldn't create semaphore");
Oct 17, 2006
Oct 17, 2006
277
return 0;
Jul 10, 2006
Jul 10, 2006
278
279
280
}
/* Create the sound buffers */
Oct 28, 2006
Oct 28, 2006
281
282
this->hidden->mixbuf =
(Uint8 *) SDL_malloc(NUM_BUFFERS * this->spec.size);
Oct 18, 2006
Oct 18, 2006
283
if (this->hidden->mixbuf == NULL) {
Oct 17, 2006
Oct 17, 2006
284
285
286
WINWAVEOUT_CloseDevice(this);
SDL_OutOfMemory();
return 0;
Jul 10, 2006
Jul 10, 2006
287
288
}
for (i = 0; i < NUM_BUFFERS; ++i) {
Oct 17, 2006
Oct 17, 2006
289
SDL_memset(&this->hidden->wavebuf[i], '\0',
Oct 28, 2006
Oct 28, 2006
290
sizeof(this->hidden->wavebuf[i]));
Oct 17, 2006
Oct 17, 2006
291
292
293
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
294
(LPSTR) & this->hidden->mixbuf[i * this->spec.size];
Oct 17, 2006
Oct 17, 2006
295
296
result = waveOutPrepareHeader(this->hidden->sound,
&this->hidden->wavebuf[i],
Oct 28, 2006
Oct 28, 2006
297
sizeof(this->hidden->wavebuf[i]));
Jul 10, 2006
Jul 10, 2006
298
if (result != MMSYSERR_NOERROR) {
Oct 17, 2006
Oct 17, 2006
299
WINWAVEOUT_CloseDevice(this);
Jul 10, 2006
Jul 10, 2006
300
SetMMerror("waveOutPrepareHeader()", result);
Oct 17, 2006
Oct 17, 2006
301
return 0;
Jul 10, 2006
Jul 10, 2006
302
303
304
}
}
Oct 28, 2006
Oct 28, 2006
305
return 1; /* Ready to go! */
Apr 26, 2001
Apr 26, 2001
306
}
Jul 10, 2006
Jul 10, 2006
307
Oct 17, 2006
Oct 17, 2006
308
309
static int
Oct 28, 2006
Oct 28, 2006
310
WINWAVEOUT_Init(SDL_AudioDriverImpl * impl)
Oct 17, 2006
Oct 17, 2006
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;
Oct 28, 2006
Oct 28, 2006
320
impl->OnlyHasDefaultOutputDevice = 1; /* !!! FIXME: Is this true? */
Oct 17, 2006
Oct 17, 2006
321
Jan 26, 2010
Jan 26, 2010
322
return 1; /* this audio target is available. */
Oct 17, 2006
Oct 17, 2006
323
324
325
326
327
328
}
AudioBootStrap WINWAVEOUT_bootstrap = {
"waveout", WINDOWS_OS_NAME " WaveOut", WINWAVEOUT_Init, 0
};
Jul 10, 2006
Jul 10, 2006
329
/* vi: set ts=4 sw=4 expandtab: */