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

Latest commit

 

History

History
267 lines (228 loc) · 7.78 KB

SDL_mmeaudio.c

File metadata and controls

267 lines (228 loc) · 7.78 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Jan 4, 2004
Jan 4, 2004
3
Copyright (C) 1997-2004 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
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
23
24
/* Tru64 UNIX MME support */
Feb 7, 2006
Feb 7, 2006
25
26
27
#include <mme_api.h>
#include "SDL_timer.h"
Feb 10, 2006
Feb 10, 2006
28
#include "SDL_audio.h"
Feb 16, 2006
Feb 16, 2006
29
#include "../SDL_audio_c.h"
30
31
32
33
#include "SDL_mmeaudio.h"
static BOOL inUse[NUM_BUFFERS];
Jul 10, 2006
Jul 10, 2006
34
static int
Oct 7, 2006
Oct 7, 2006
35
MME_Available(void)
Oct 7, 2006
Oct 7, 2006
37
return 1;
Jul 10, 2006
Jul 10, 2006
40
41
static void
SetMMerror(char *function, MMRESULT code)
42
43
44
45
{
int len;
char errbuf[MAXERRORLENGTH];
Feb 7, 2006
Feb 7, 2006
46
SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
Feb 7, 2006
Feb 7, 2006
47
len = SDL_strlen(errbuf);
Jul 10, 2006
Jul 10, 2006
48
49
waveOutGetErrorText(code, errbuf + len, MAXERRORLENGTH - len);
SDL_SetError("%s", errbuf);
Jul 10, 2006
Jul 10, 2006
52
static void CALLBACK
Oct 7, 2006
Oct 7, 2006
53
MME_Callback(HWAVEOUT hwo,
Jul 10, 2006
Jul 10, 2006
54
UINT uMsg, DWORD dwInstance, LPARAM dwParam1, LPARAM dwParam2)
55
56
57
{
WAVEHDR *wp = (WAVEHDR *) dwParam1;
Jul 10, 2006
Jul 10, 2006
58
59
if (uMsg == WOM_DONE)
inUse[wp->dwUser] = FALSE;
Jul 10, 2006
Jul 10, 2006
62
static int
Oct 7, 2006
Oct 7, 2006
63
MME_OpenDevice(_THIS, const char *devname, int iscapture)
Oct 7, 2006
Oct 7, 2006
65
int valid_format = 0;
66
MMRESULT result;
Oct 7, 2006
Oct 7, 2006
67
Uint8 *mixbuf = NULL;
Oct 7, 2006
Oct 7, 2006
70
71
72
73
74
75
76
77
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
SDL_malloc((sizeof *this->hidden));
if (this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
78
79
/* Set basic WAVE format parameters */
Oct 7, 2006
Oct 7, 2006
80
81
82
83
84
this->hidden->shm = mmeAllocMem(sizeof(*this->hidden->shm));
if (this->hidden->shm == NULL) {
MME_CloseDevice(this);
SDL_OutOfMemory();
return 0;
Oct 7, 2006
Oct 7, 2006
86
Oct 7, 2006
Oct 7, 2006
87
SDL_memset(this->hidden->shm, '\0', sizeof (*this->hidden->shm));
Oct 7, 2006
Oct 7, 2006
88
89
this->hidden->shm->sound = 0;
this->hidden->shm->wFmt.wf.wFormatTag = WAVE_FORMAT_PCM;
90
91
/* Determine the audio parameters from the AudioSpec */
Oct 7, 2006
Oct 7, 2006
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* 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) {
case AUDIO_U8:
case AUDIO_S16:
case AUDIO_S32:
break;
default:
valid_format = 0;
test_format = SDL_NextAudioFormat();
}
}
if (!valid_format) {
MME_CloseDevice(this);
Jul 10, 2006
Jul 10, 2006
109
SDL_SetError("Unsupported audio format");
Oct 7, 2006
Oct 7, 2006
110
return 0;
Oct 7, 2006
Oct 7, 2006
113
114
115
this->spec.format = test_format;
this->hidden->shm->wFmt.wBitsPerSample = SDL_AUDIO_BITSIZE(test_format);
Sep 1, 2006
Sep 1, 2006
116
/* !!! FIXME: Can this handle more than stereo? */
Oct 7, 2006
Oct 7, 2006
117
118
119
120
121
122
123
124
this->hidden->shm->wFmt.wf.nChannels = this->spec.channels;
this->hidden->shm->wFmt.wf.nSamplesPerSec = this->spec.freq;
this->hidden->shm->wFmt.wf.nBlockAlign =
this->hidden->shm->wFmt.wf.nChannels *
this->hidden->shm->wFmt.wBitsPerSample / 8;
this->hidden->shm->wFmt.wf.nAvgBytesPerSec =
this->hidden->shm->wFmt.wf.nSamplesPerSec *
this->hidden->shm->wFmt.wf.nBlockAlign;
125
126
/* Check the buffer size -- minimum of 1/4 second (word aligned) */
Oct 7, 2006
Oct 7, 2006
127
128
if (this->spec.samples < (this->spec.freq / 4))
this->spec.samples = ((this->spec.freq / 4) + 3) & ~3;
129
130
/* Update the fragment size as size in bytes */
Oct 7, 2006
Oct 7, 2006
131
SDL_CalculateAudioSpec(&this->spec);
132
133
/* Open the audio device */
Oct 7, 2006
Oct 7, 2006
134
result = waveOutOpen(&(this->hidden->shm->sound),
Jul 10, 2006
Jul 10, 2006
135
WAVE_MAPPER,
Oct 7, 2006
Oct 7, 2006
136
137
&(this->hidden->shm->wFmt.wf),
MME_Callback,
Jul 10, 2006
Jul 10, 2006
138
139
NULL, (CALLBACK_FUNCTION | WAVE_OPEN_SHAREABLE));
if (result != MMSYSERR_NOERROR) {
Oct 7, 2006
Oct 7, 2006
140
MME_CloseDevice(this);
Jul 10, 2006
Jul 10, 2006
141
SetMMerror("waveOutOpen()", result);
Oct 7, 2006
Oct 7, 2006
142
return 0;
143
144
145
}
/* Create the sound buffers */
Oct 7, 2006
Oct 7, 2006
146
mixbuf = (Uint8 *) mmeAllocBuffer(NUM_BUFFERS * (this->spec.size));
Jul 10, 2006
Jul 10, 2006
147
if (mixbuf == NULL) {
Oct 7, 2006
Oct 7, 2006
148
149
150
MME_CloseDevice(this);
SDL_OutOfMemory();
return 0;
Oct 7, 2006
Oct 7, 2006
152
this->hidden->mixbuf = mixbuf;
153
154
for (i = 0; i < NUM_BUFFERS; i++) {
Oct 7, 2006
Oct 7, 2006
155
156
157
158
159
160
161
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;
this->hidden->shm->wHdr[i].dwLoops = 0; /* loop control counter */
this->hidden->shm->wHdr[i].lpNext = NULL; /* reserved for driver */
this->hidden->shm->wHdr[i].reserved = 0;
Jul 10, 2006
Jul 10, 2006
162
inUse[i] = FALSE;
Oct 7, 2006
Oct 7, 2006
164
165
166
this->hidden->next_buffer = 0;
return 1;
Jul 10, 2006
Jul 10, 2006
169
static void
Oct 7, 2006
Oct 7, 2006
170
MME_WaitDevice(_THIS)
Oct 7, 2006
Oct 7, 2006
172
while (inUse[this->hidden->next_buffer]) {
Jul 10, 2006
Jul 10, 2006
173
174
mmeWaitForCallbacks();
mmeProcessCallbacks();
Dec 14, 2003
Dec 14, 2003
175
}
Jul 10, 2006
Jul 10, 2006
178
static Uint8 *
Oct 7, 2006
Oct 7, 2006
179
MME_GetDeviceBuf(_THIS)
Oct 7, 2006
Oct 7, 2006
181
182
183
void *retval = this->hidden->shm->wHdr[this->hidden->next_buffer].lpData;
inUse[this->hidden->next_buffer] = TRUE;
return (Uint8 *) retval;
Jul 10, 2006
Jul 10, 2006
186
static void
Oct 7, 2006
Oct 7, 2006
187
MME_PlayDevice(_THIS)
188
189
{
/* Queue it up */
Oct 7, 2006
Oct 7, 2006
190
191
192
193
waveOutWrite(this->hidden->shm->sound,
&(this->hidden->shm->wHdr[this->hidden->next_buffer]),
sizeof (WAVEHDR));
this->hidden->next_buffer = (this->hidden->next_buffer + 1) % NUM_BUFFERS;
Jul 10, 2006
Jul 10, 2006
196
197
static void
MME_WaitDone(_THIS)
198
199
200
201
{
MMRESULT result;
int i;
Oct 7, 2006
Oct 7, 2006
202
if (this->hidden->shm->sound) {
Jul 10, 2006
Jul 10, 2006
203
204
205
206
207
for (i = 0; i < NUM_BUFFERS; i++)
while (inUse[i]) {
mmeWaitForCallbacks();
mmeProcessCallbacks();
}
Oct 7, 2006
Oct 7, 2006
208
result = waveOutReset(this->hidden->shm->sound);
Jul 10, 2006
Jul 10, 2006
209
210
211
if (result != MMSYSERR_NOERROR)
SetMMerror("waveOutReset()", result);
mmeProcessCallbacks();
Jul 10, 2006
Jul 10, 2006
215
static void
Oct 7, 2006
Oct 7, 2006
216
MME_CloseDevice(_THIS)
Oct 7, 2006
Oct 7, 2006
218
219
if (this->hidden != NULL) {
MMRESULT result;
Oct 7, 2006
Oct 7, 2006
221
222
223
224
225
226
if (this->hidden->mixbuf) {
result = mmeFreeBuffer(this->hidden->mixbuf);
if (result != MMSYSERR_NOERROR)
SetMMerror("mmeFreeBuffer", result);
this->hidden->mixbuf = NULL;
}
Oct 7, 2006
Oct 7, 2006
228
229
230
231
232
233
234
235
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);
Jul 10, 2006
Jul 10, 2006
236
if (result != MMSYSERR_NOERROR)
Oct 7, 2006
Oct 7, 2006
237
238
SetMMerror("mmeFreeMem()", result);
this->hidden->shm = NULL;
Jul 10, 2006
Jul 10, 2006
239
}
Oct 7, 2006
Oct 7, 2006
240
241
242
SDL_free(this->hidden);
this->hidden = NULL;
Oct 7, 2006
Oct 7, 2006
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
static int
MME_Init(SDL_AudioDriverImpl *impl)
{
/* 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_Available, MME_Init, 0
};
Jul 10, 2006
Jul 10, 2006
267
/* vi: set ts=4 sw=4 expandtab: */