19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Library General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Library General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Library General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Tru64 UNIX MME support */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mme_api.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mmeaudio.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic BOOL inUse[NUM_BUFFERS];
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int MME_OpenAudio(_THIS, SDL_AudioSpec *spec);
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void MME_WaitAudio(_THIS);
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *MME_GetAudioBuf(_THIS);
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void MME_PlayAudio(_THIS);
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void MME_WaitDone(_THIS);
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void MME_CloseAudio(_THIS);
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void)
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(1);
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device)
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( device ) {
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( device->hidden ) {
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDL_free(device->hidden);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    device->hidden = NULL;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device = NULL;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex)
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_AudioDevice *this;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Initialize all variables that we clean on shutdown */
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this = SDL_malloc(sizeof(SDL_AudioDevice));
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this ) {
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this, 0, (sizeof *this));
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden = SDL_malloc((sizeof *this->hidden));
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( (this == NULL) || (this->hidden == NULL) ) {
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_OutOfMemory();
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this ) {
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDL_free(this);
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set the function pointers */
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->OpenAudio       =       MME_OpenAudio;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->WaitAudio       =       MME_WaitAudio;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->PlayAudio       =       MME_PlayAudio;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->GetAudioBuf     =     MME_GetAudioBuf;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->WaitDone        =        MME_WaitDone;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->CloseAudio      =      MME_CloseAudio;
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->free            =  Audio_DeleteDevice;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return this;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap MMEAUDIO_bootstrap = {
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    "waveout", "Tru64 MME WaveOut",
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Audio_Available, Audio_CreateDevice
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SetMMerror(char *function, MMRESULT code)
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int len;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char errbuf[MAXERRORLENGTH];
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    len = SDL_strlen(errbuf);
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    waveOutGetErrorText(code, errbuf+len, MAXERRORLENGTH-len);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("%s",errbuf);
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void CALLBACK MME_CALLBACK(HWAVEOUT hwo,
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				  UINT uMsg,
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				  DWORD dwInstance,
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				  LPARAM dwParam1,
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				  LPARAM dwParam2)
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WAVEHDR *wp = (WAVEHDR *) dwParam1;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( uMsg == WOM_DONE )
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	inUse[wp->dwUser] = FALSE;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int MME_OpenAudio(_THIS, SDL_AudioSpec *spec)
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MMRESULT result;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    mixbuf = NULL;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set basic WAVE format parameters */
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    shm = mmeAllocMem(sizeof(*shm));
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( shm == NULL ) {
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("Out of memory: shm");
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(-1);
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    shm->sound = 0;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    shm->wFmt.wf.wFormatTag = WAVE_FORMAT_PCM;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Determine the audio parameters from the AudioSpec */
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    switch ( spec->format & 0xFF ) {
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case 8:
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Unsigned 8 bit audio data */
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    spec->format = AUDIO_U8;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    shm->wFmt.wBitsPerSample = 8;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case 16:
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Signed 16 bit audio data */
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    spec->format = AUDIO_S16;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    shm->wFmt.wBitsPerSample = 16;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDL_SetError("Unsupported audio format");
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return(-1);
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    shm->wFmt.wf.nChannels = spec->channels;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    shm->wFmt.wf.nSamplesPerSec = spec->freq;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    shm->wFmt.wf.nBlockAlign =
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shm->wFmt.wf.nChannels * shm->wFmt.wBitsPerSample / 8;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    shm->wFmt.wf.nAvgBytesPerSec =
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shm->wFmt.wf.nSamplesPerSec * shm->wFmt.wf.nBlockAlign;
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Check the buffer size -- minimum of 1/4 second (word aligned) */
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( spec->samples < (spec->freq/4) )
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	spec->samples = ((spec->freq/4)+3)&~3;
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Update the fragment size as size in bytes */
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CalculateAudioSpec(spec);
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Open the audio device */
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    result = waveOutOpen(&(shm->sound),
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 WAVE_MAPPER,
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 &(shm->wFmt.wf),
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 MME_CALLBACK,
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 NULL,
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 (CALLBACK_FUNCTION|WAVE_OPEN_SHAREABLE));
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( result != MMSYSERR_NOERROR ) {
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SetMMerror("waveOutOpen()", result);
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return(-1);
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Create the sound buffers */
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    mixbuf = (Uint8 *)mmeAllocBuffer(NUM_BUFFERS * (spec->size));
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( mixbuf == NULL ) {
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("Out of memory: mixbuf");
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(-1);
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (i = 0; i < NUM_BUFFERS; i++) {
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shm->wHdr[i].lpData         = &mixbuf[i * (spec->size)];
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shm->wHdr[i].dwBufferLength = spec->size;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shm->wHdr[i].dwFlags        = 0;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shm->wHdr[i].dwUser         = i;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shm->wHdr[i].dwLoops        = 0;       /* loop control counter */
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shm->wHdr[i].lpNext         = NULL;    /* reserved for driver */
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shm->wHdr[i].reserved       = 0;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	inUse[i] = FALSE;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    next_buffer = 0;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void MME_WaitAudio(_THIS)
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( inUse[next_buffer] ) {
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mmeWaitForCallbacks();
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mmeProcessCallbacks();
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *MME_GetAudioBuf(_THIS)
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint8 *retval;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    inUse[next_buffer] = TRUE;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    retval = (Uint8 *)(shm->wHdr[next_buffer].lpData);
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return retval;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void MME_PlayAudio(_THIS)
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Queue it up */
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    waveOutWrite(shm->sound, &(shm->wHdr[next_buffer]), sizeof(WAVEHDR));
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    next_buffer = (next_buffer+1)%NUM_BUFFERS;
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void MME_WaitDone(_THIS)
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MMRESULT result;
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( shm->sound ) {
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i = 0; i < NUM_BUFFERS; i++)
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    while ( inUse[i] ) {
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mmeWaitForCallbacks();
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mmeProcessCallbacks();
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = waveOutReset(shm->sound);
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != MMSYSERR_NOERROR )
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SetMMerror("waveOutReset()", result);
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mmeProcessCallbacks();
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void MME_CloseAudio(_THIS)
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MMRESULT result;
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( mixbuf ) {
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = mmeFreeBuffer(mixbuf);
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (result != MMSYSERR_NOERROR )
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SetMMerror("mmeFreeBuffer", result);
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixbuf = NULL;
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( shm ) {
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( shm->sound ) {
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    result = waveOutClose(shm->sound);
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (result != MMSYSERR_NOERROR )
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetMMerror("waveOutClose()", result);
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    mmeProcessCallbacks();
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = mmeFreeMem(shm);
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (result != MMSYSERR_NOERROR )
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SetMMerror("mmeFreeMem()", result);
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shm = NULL;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
265