2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "SDL_config.h"
24 /* Tru64 UNIX MME support */
27 #include "SDL_timer.h"
28 #include "SDL_audio.h"
29 #include "../SDL_audio_c.h"
30 #include "SDL_mmeaudio.h"
32 static BOOL inUse[NUM_BUFFERS];
34 /* Audio driver functions */
35 static int MME_OpenAudio(_THIS, SDL_AudioSpec *spec);
36 static void MME_WaitAudio(_THIS);
37 static Uint8 *MME_GetAudioBuf(_THIS);
38 static void MME_PlayAudio(_THIS);
39 static void MME_WaitDone(_THIS);
40 static void MME_CloseAudio(_THIS);
42 /* Audio driver bootstrap functions */
43 static int Audio_Available(void)
48 static void Audio_DeleteDevice(SDL_AudioDevice *device)
51 if ( device->hidden ) {
52 SDL_free(device->hidden);
53 device->hidden = NULL;
60 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
62 SDL_AudioDevice *this;
64 /* Initialize all variables that we clean on shutdown */
65 this = SDL_malloc(sizeof(SDL_AudioDevice));
67 SDL_memset(this, 0, (sizeof *this));
68 this->hidden = SDL_malloc((sizeof *this->hidden));
70 if ( (this == NULL) || (this->hidden == NULL) ) {
77 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
78 /* Set the function pointers */
79 this->OpenAudio = MME_OpenAudio;
80 this->WaitAudio = MME_WaitAudio;
81 this->PlayAudio = MME_PlayAudio;
82 this->GetAudioBuf = MME_GetAudioBuf;
83 this->WaitDone = MME_WaitDone;
84 this->CloseAudio = MME_CloseAudio;
85 this->free = Audio_DeleteDevice;
90 AudioBootStrap MMEAUDIO_bootstrap = {
91 "waveout", "Tru64 MME WaveOut",
92 Audio_Available, Audio_CreateDevice
95 static void SetMMerror(char *function, MMRESULT code)
98 char errbuf[MAXERRORLENGTH];
100 SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
101 len = SDL_strlen(errbuf);
102 waveOutGetErrorText(code, errbuf+len, MAXERRORLENGTH-len);
103 SDL_SetError("%s",errbuf);
106 static void CALLBACK MME_CALLBACK(HWAVEOUT hwo,
112 WAVEHDR *wp = (WAVEHDR *) dwParam1;
114 if ( uMsg == WOM_DONE )
115 inUse[wp->dwUser] = FALSE;
118 static int MME_OpenAudio(_THIS, SDL_AudioSpec *spec)
125 /* Set basic WAVE format parameters */
126 shm = mmeAllocMem(sizeof(*shm));
128 SDL_SetError("Out of memory: shm");
132 shm->wFmt.wf.wFormatTag = WAVE_FORMAT_PCM;
134 /* Determine the audio parameters from the AudioSpec */
135 switch ( spec->format & 0xFF ) {
137 /* Unsigned 8 bit audio data */
138 spec->format = AUDIO_U8;
139 shm->wFmt.wBitsPerSample = 8;
142 /* Signed 16 bit audio data */
143 spec->format = AUDIO_S16;
144 shm->wFmt.wBitsPerSample = 16;
147 SDL_SetError("Unsupported audio format");
151 shm->wFmt.wf.nChannels = spec->channels;
152 shm->wFmt.wf.nSamplesPerSec = spec->freq;
153 shm->wFmt.wf.nBlockAlign =
154 shm->wFmt.wf.nChannels * shm->wFmt.wBitsPerSample / 8;
155 shm->wFmt.wf.nAvgBytesPerSec =
156 shm->wFmt.wf.nSamplesPerSec * shm->wFmt.wf.nBlockAlign;
158 /* Check the buffer size -- minimum of 1/4 second (word aligned) */
159 if ( spec->samples < (spec->freq/4) )
160 spec->samples = ((spec->freq/4)+3)&~3;
162 /* Update the fragment size as size in bytes */
163 SDL_CalculateAudioSpec(spec);
165 /* Open the audio device */
166 result = waveOutOpen(&(shm->sound),
171 (CALLBACK_FUNCTION|WAVE_OPEN_SHAREABLE));
172 if ( result != MMSYSERR_NOERROR ) {
173 SetMMerror("waveOutOpen()", result);
177 /* Create the sound buffers */
178 mixbuf = (Uint8 *)mmeAllocBuffer(NUM_BUFFERS * (spec->size));
179 if ( mixbuf == NULL ) {
180 SDL_SetError("Out of memory: mixbuf");
184 for (i = 0; i < NUM_BUFFERS; i++) {
185 shm->wHdr[i].lpData = &mixbuf[i * (spec->size)];
186 shm->wHdr[i].dwBufferLength = spec->size;
187 shm->wHdr[i].dwFlags = 0;
188 shm->wHdr[i].dwUser = i;
189 shm->wHdr[i].dwLoops = 0; /* loop control counter */
190 shm->wHdr[i].lpNext = NULL; /* reserved for driver */
191 shm->wHdr[i].reserved = 0;
198 static void MME_WaitAudio(_THIS)
200 while ( inUse[next_buffer] ) {
201 mmeWaitForCallbacks();
202 mmeProcessCallbacks();
206 static Uint8 *MME_GetAudioBuf(_THIS)
210 inUse[next_buffer] = TRUE;
211 retval = (Uint8 *)(shm->wHdr[next_buffer].lpData);
215 static void MME_PlayAudio(_THIS)
218 waveOutWrite(shm->sound, &(shm->wHdr[next_buffer]), sizeof(WAVEHDR));
219 next_buffer = (next_buffer+1)%NUM_BUFFERS;
222 static void MME_WaitDone(_THIS)
228 for (i = 0; i < NUM_BUFFERS; i++)
230 mmeWaitForCallbacks();
231 mmeProcessCallbacks();
233 result = waveOutReset(shm->sound);
234 if ( result != MMSYSERR_NOERROR )
235 SetMMerror("waveOutReset()", result);
236 mmeProcessCallbacks();
240 static void MME_CloseAudio(_THIS)
245 result = mmeFreeBuffer(mixbuf);
246 if (result != MMSYSERR_NOERROR )
247 SetMMerror("mmeFreeBuffer", result);
253 result = waveOutClose(shm->sound);
254 if (result != MMSYSERR_NOERROR )
255 SetMMerror("waveOutClose()", result);
256 mmeProcessCallbacks();
258 result = mmeFreeMem(shm);
259 if (result != MMSYSERR_NOERROR )
260 SetMMerror("mmeFreeMem()", result);