This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_mmeaudio.c
260 lines (222 loc) · 7.58 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2009 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@libsdl.org
*/
22
#include "SDL_config.h"
23
24
/* Tru64 UNIX MME support */
25
26
27
#include <mme_api.h>
#include "SDL_timer.h"
28
#include "SDL_audio.h"
29
#include "../SDL_audio_c.h"
30
31
32
33
#include "SDL_mmeaudio.h"
static BOOL inUse[NUM_BUFFERS];
34
35
static void
SetMMerror(char *function, MMRESULT code)
36
37
38
39
{
int len;
char errbuf[MAXERRORLENGTH];
40
SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
41
len = SDL_strlen(errbuf);
42
43
waveOutGetErrorText(code, errbuf + len, MAXERRORLENGTH - len);
SDL_SetError("%s", errbuf);
44
45
}
46
static void CALLBACK
47
MME_Callback(HWAVEOUT hwo,
48
UINT uMsg, DWORD dwInstance, LPARAM dwParam1, LPARAM dwParam2)
49
50
51
{
WAVEHDR *wp = (WAVEHDR *) dwParam1;
52
53
if (uMsg == WOM_DONE)
inUse[wp->dwUser] = FALSE;
54
55
}
56
static int
57
MME_OpenDevice(_THIS, const char *devname, int iscapture)
58
{
59
int valid_format = 0;
60
MMRESULT result;
61
Uint8 *mixbuf = NULL;
62
63
int i;
64
65
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
66
SDL_malloc((sizeof *this->hidden));
67
68
69
70
71
if (this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
72
73
/* Set basic WAVE format parameters */
74
75
76
77
78
this->hidden->shm = mmeAllocMem(sizeof(*this->hidden->shm));
if (this->hidden->shm == NULL) {
MME_CloseDevice(this);
SDL_OutOfMemory();
return 0;
79
}
80
81
SDL_memset(this->hidden->shm, '\0', sizeof(*this->hidden->shm));
82
83
this->hidden->shm->sound = 0;
this->hidden->shm->wFmt.wf.wFormatTag = WAVE_FORMAT_PCM;
84
85
/* Determine the audio parameters from the AudioSpec */
86
87
88
89
90
/* Try for a closest match on audio format */
for (test_format = SDL_FirstAudioFormat(this->spec.format);
!valid_format && test_format;) {
valid_format = 1;
switch (test_format) {
91
92
93
94
95
96
97
case AUDIO_U8:
case AUDIO_S16:
case AUDIO_S32:
break;
default:
valid_format = 0;
test_format = SDL_NextAudioFormat();
98
99
100
101
102
}
}
if (!valid_format) {
MME_CloseDevice(this);
103
SDL_SetError("Unsupported audio format");
104
return 0;
105
106
}
107
108
109
this->spec.format = test_format;
this->hidden->shm->wFmt.wBitsPerSample = SDL_AUDIO_BITSIZE(test_format);
110
/* !!! FIXME: Can this handle more than stereo? */
111
112
113
this->hidden->shm->wFmt.wf.nChannels = this->spec.channels;
this->hidden->shm->wFmt.wf.nSamplesPerSec = this->spec.freq;
this->hidden->shm->wFmt.wf.nBlockAlign =
114
115
this->hidden->shm->wFmt.wf.nChannels *
this->hidden->shm->wFmt.wBitsPerSample / 8;
116
this->hidden->shm->wFmt.wf.nAvgBytesPerSec =
117
118
this->hidden->shm->wFmt.wf.nSamplesPerSec *
this->hidden->shm->wFmt.wf.nBlockAlign;
119
120
/* Check the buffer size -- minimum of 1/4 second (word aligned) */
121
122
if (this->spec.samples < (this->spec.freq / 4))
this->spec.samples = ((this->spec.freq / 4) + 3) & ~3;
123
124
/* Update the fragment size as size in bytes */
125
SDL_CalculateAudioSpec(&this->spec);
126
127
/* Open the audio device */
128
result = waveOutOpen(&(this->hidden->shm->sound),
129
WAVE_MAPPER,
130
131
&(this->hidden->shm->wFmt.wf),
MME_Callback,
132
133
NULL, (CALLBACK_FUNCTION | WAVE_OPEN_SHAREABLE));
if (result != MMSYSERR_NOERROR) {
134
MME_CloseDevice(this);
135
SetMMerror("waveOutOpen()", result);
136
return 0;
137
138
139
}
/* Create the sound buffers */
140
mixbuf = (Uint8 *) mmeAllocBuffer(NUM_BUFFERS * (this->spec.size));
141
if (mixbuf == NULL) {
142
143
144
MME_CloseDevice(this);
SDL_OutOfMemory();
return 0;
145
}
146
this->hidden->mixbuf = mixbuf;
147
148
for (i = 0; i < NUM_BUFFERS; i++) {
149
150
151
152
this->hidden->shm->wHdr[i].lpData = &mixbuf[i * (this->spec.size)];
this->hidden->shm->wHdr[i].dwBufferLength = this->spec.size;
this->hidden->shm->wHdr[i].dwFlags = 0;
this->hidden->shm->wHdr[i].dwUser = i;
153
154
this->hidden->shm->wHdr[i].dwLoops = 0; /* loop control counter */
this->hidden->shm->wHdr[i].lpNext = NULL; /* reserved for driver */
155
this->hidden->shm->wHdr[i].reserved = 0;
156
inUse[i] = FALSE;
157
}
158
159
160
this->hidden->next_buffer = 0;
return 1;
161
162
}
163
static void
164
MME_WaitDevice(_THIS)
165
{
166
while (inUse[this->hidden->next_buffer]) {
167
168
mmeWaitForCallbacks();
mmeProcessCallbacks();
169
}
170
171
}
172
static Uint8 *
173
MME_GetDeviceBuf(_THIS)
174
{
175
176
177
void *retval = this->hidden->shm->wHdr[this->hidden->next_buffer].lpData;
inUse[this->hidden->next_buffer] = TRUE;
return (Uint8 *) retval;
178
179
}
180
static void
181
MME_PlayDevice(_THIS)
182
183
{
/* Queue it up */
184
185
waveOutWrite(this->hidden->shm->sound,
&(this->hidden->shm->wHdr[this->hidden->next_buffer]),
186
sizeof(WAVEHDR));
187
this->hidden->next_buffer = (this->hidden->next_buffer + 1) % NUM_BUFFERS;
188
189
}
190
191
static void
MME_WaitDone(_THIS)
192
193
194
195
{
MMRESULT result;
int i;
196
if (this->hidden->shm->sound) {
197
198
199
200
201
for (i = 0; i < NUM_BUFFERS; i++)
while (inUse[i]) {
mmeWaitForCallbacks();
mmeProcessCallbacks();
}
202
result = waveOutReset(this->hidden->shm->sound);
203
204
205
if (result != MMSYSERR_NOERROR)
SetMMerror("waveOutReset()", result);
mmeProcessCallbacks();
206
207
208
}
}
209
static void
210
MME_CloseDevice(_THIS)
211
{
212
213
if (this->hidden != NULL) {
MMRESULT result;
214
215
216
217
218
219
220
if (this->hidden->mixbuf) {
result = mmeFreeBuffer(this->hidden->mixbuf);
if (result != MMSYSERR_NOERROR)
SetMMerror("mmeFreeBuffer", result);
this->hidden->mixbuf = NULL;
}
221
222
223
224
225
226
227
228
229
if (this->hidden->shm) {
if (this->hidden->shm->sound) {
result = waveOutClose(this->hidden->shm->sound);
if (result != MMSYSERR_NOERROR)
SetMMerror("waveOutClose()", result);
mmeProcessCallbacks();
}
result = mmeFreeMem(this->hidden->shm);
230
if (result != MMSYSERR_NOERROR)
231
232
SetMMerror("mmeFreeMem()", result);
this->hidden->shm = NULL;
233
}
234
235
236
SDL_free(this->hidden);
this->hidden = NULL;
237
238
239
}
}
240
static int
241
MME_Init(SDL_AudioDriverImpl * impl)
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
{
/* Set the function pointers */
impl->OpenDevice = MME_OpenDevice;
impl->WaitDevice = MME_WaitDevice;
impl->WaitDone = MME_WaitDone;
impl->PlayDevice = MME_PlayDevice;
impl->GetDeviceBuf = MME_GetDeviceBuf;
impl->CloseDevice = MME_CloseDevice;
impl->OnlyHasDefaultOutputDevice = 1;
return 1;
}
/* !!! FIXME: Windows "windib" driver is called waveout, too */
AudioBootStrap MMEAUDIO_bootstrap = {
"waveout", "Tru64 MME WaveOut", MME_Init, 0
};
260
/* vi: set ts=4 sw=4 expandtab: */