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 Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 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    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Allow access to a raw mixing buffer */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WIN32_LEAN_AND_MEAN
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <windows.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mmsystem.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_dibaudio.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "win_ce_semaphore.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DIB_OpenAudio(_THIS, SDL_AudioSpec *spec);
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_ThreadInit(_THIS);
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_WaitAudio(_THIS);
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *DIB_GetAudioBuf(_THIS);
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_PlayAudio(_THIS);
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_WaitDone(_THIS);
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_CloseAudio(_THIS);
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
48e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallint volatile dibaudio_thread_debug = 0;
49e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallint volatile dibaudio_cb_debug     = 0;
50e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallint volatile dibaudio_main_debug   = 0;
51e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void)
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(1);
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device)
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex)
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioDevice *this;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this ) {
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(this, 0, (sizeof *this));
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden = (struct SDL_PrivateAudioData *)
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *this->hidden));
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (this == NULL) || (this->hidden == NULL) ) {
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this ) {
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(this);
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->OpenAudio = DIB_OpenAudio;
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->ThreadInit = DIB_ThreadInit;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitAudio = DIB_WaitAudio;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->PlayAudio = DIB_PlayAudio;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->GetAudioBuf = DIB_GetAudioBuf;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitDone = DIB_WaitDone;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CloseAudio = DIB_CloseAudio;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->free = Audio_DeleteDevice;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return this;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap WAVEOUT_bootstrap = {
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"waveout", "Win95/98/NT/2000 WaveOut",
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Audio_Available, Audio_CreateDevice
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The Win32 callback for filling the WAVE device */
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void CALLBACK FillSound(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						DWORD dwParam1, DWORD dwParam2)
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioDevice *this = (SDL_AudioDevice *)dwInstance;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Only service "buffer done playing" messages */
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( uMsg != WOM_DONE )
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Signal that we are done playing a buffer */
116e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	dibaudio_cb_debug = 1;
117e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	EnterCriticalSection(&audio_cs);
118e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	dibaudio_cb_debug = 2;
119e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	SetEvent(audio_event);
120e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	dibaudio_cb_debug = 3;
121e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	LeaveCriticalSection(&audio_cs);
122e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	dibaudio_cb_debug = 4;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SetMMerror(char *function, MMRESULT code)
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t len;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char errbuf[MAXERRORLENGTH];
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	wchar_t werrbuf[MAXERRORLENGTH];
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	len = SDL_strlen(errbuf);
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* UNICODE version */
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveOutGetErrorText(code, werrbuf, MAXERRORLENGTH-len);
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WideCharToMultiByte(CP_ACP,0,werrbuf,-1,errbuf+len,MAXERRORLENGTH-len,NULL,NULL);
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveOutGetErrorText(code, errbuf+len, (UINT)(MAXERRORLENGTH-len));
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("%s",errbuf);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Set high priority for the audio thread */
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_ThreadInit(_THIS)
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DIB_WaitAudio(_THIS)
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Wait for an audio chunk to finish */
156e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	dibaudio_thread_debug = 1;
157e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	WaitForSingleObject(audio_event, INFINITE);
158e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	dibaudio_thread_debug = 2;
159e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	ResetEvent(audio_event);
160e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	dibaudio_thread_debug = 3;
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint8 *DIB_GetAudioBuf(_THIS)
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Uint8 *retval;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
167e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    dibaudio_thread_debug = 4;
168e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    EnterCriticalSection(&audio_cs);
169e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    dibaudio_thread_debug = 5;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = (Uint8 *)(wavebuf[next_buffer].lpData);
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DIB_PlayAudio(_THIS)
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Queue it up */
177e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	dibaudio_thread_debug = 6;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveOutWrite(sound, &wavebuf[next_buffer], sizeof(wavebuf[0]));
179e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	dibaudio_thread_debug = 7;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	next_buffer = (next_buffer+1)%NUM_BUFFERS;
181e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	LeaveCriticalSection(&audio_cs);
182e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	dibaudio_thread_debug = 8;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DIB_WaitDone(_THIS)
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
187e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	int i, left, tries = 5;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
189e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	dibaudio_thread_debug = 9;
190e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
191e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	/* give some time for the wave output to send the last buffer,
192e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	   but don't hang if one was cancelled
193e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	  */
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	do {
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		left = NUM_BUFFERS;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<NUM_BUFFERS; ++i ) {
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( wavebuf[i].dwFlags & WHDR_DONE ) {
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				--left;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
201e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		if ( left == 0 )
202e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall			break;
203e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
204e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		SDL_Delay(100);
205e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	} while ( --tries > 0 );
206e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
207e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	dibaudio_thread_debug = 10;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DIB_CloseAudio(_THIS)
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Close up audio */
215e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	if (audio_event != INVALID_HANDLE_VALUE ) {
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
217e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		CloseSynchHandle(audio_event);
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
219e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		CloseHandle(audio_event);
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( sound ) {
223e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		waveOutReset(sound);
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Clean up mixing buffers */
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<NUM_BUFFERS; ++i ) {
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( wavebuf[i].dwUser != 0xFFFF ) {
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			waveOutUnprepareHeader(sound, &wavebuf[i],
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						sizeof(wavebuf[i]));
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wavebuf[i].dwUser = 0xFFFF;
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Free raw mixing buffer */
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mixbuf != NULL ) {
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(mixbuf);
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mixbuf = NULL;
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
239e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
240e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	if ( sound )
241e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		waveOutClose(sound);
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DIB_OpenAudio(_THIS, SDL_AudioSpec *spec)
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MMRESULT result;
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WAVEFORMATEX waveformat;
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize the wavebuf structures for closing */
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sound = NULL;
252e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	InitializeCriticalSection(&audio_cs);
253e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	audio_event = INVALID_HANDLE_VALUE;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i = 0; i < NUM_BUFFERS; ++i )
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		wavebuf[i].dwUser = 0xFFFF;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixbuf = NULL;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set basic WAVE format parameters */
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(&waveformat, 0, sizeof(waveformat));
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveformat.wFormatTag = WAVE_FORMAT_PCM;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the audio parameters from the AudioSpec */
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch ( spec->format & 0xFF ) {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 8:
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Unsigned 8 bit audio data */
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			spec->format = AUDIO_U8;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			waveformat.wBitsPerSample = 8;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 16:
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Signed 16 bit audio data */
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			spec->format = AUDIO_S16;
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			waveformat.wBitsPerSample = 16;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Unsupported audio format");
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveformat.nChannels = spec->channels;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveformat.nSamplesPerSec = spec->freq;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveformat.nBlockAlign =
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		waveformat.nChannels * (waveformat.wBitsPerSample/8);
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveformat.nAvgBytesPerSec =
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		waveformat.nSamplesPerSec * waveformat.nBlockAlign;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check the buffer size -- minimum of 1/4 second (word aligned) */
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( spec->samples < (spec->freq/4) )
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		spec->samples = ((spec->freq/4)+3)&~3;
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Update the fragment size as size in bytes */
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CalculateAudioSpec(spec);
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Open the audio device */
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = waveOutOpen(&sound, WAVE_MAPPER, &waveformat,
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(DWORD_PTR)FillSound, (DWORD_PTR)this, CALLBACK_FUNCTION);
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != MMSYSERR_NOERROR ) {
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetMMerror("waveOutOpen()", result);
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SOUND_DEBUG
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check the sound device we retrieved */
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		WAVEOUTCAPS caps;
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = waveOutGetDevCaps((UINT)sound, &caps, sizeof(caps));
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( result != MMSYSERR_NOERROR ) {
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SetMMerror("waveOutGetDevCaps()", result);
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		printf("Audio device: %s\n", caps.szPname);
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the audio buffer semaphore */
315e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	audio_event = CreateEvent( NULL, TRUE, FALSE, NULL );
316e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	if (audio_event == NULL) {
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't create semaphore");
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the sound buffers */
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixbuf = (Uint8 *)SDL_malloc(NUM_BUFFERS*spec->size);
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mixbuf == NULL ) {
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Out of memory");
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i = 0; i < NUM_BUFFERS; ++i ) {
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(&wavebuf[i], 0, sizeof(wavebuf[i]));
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		wavebuf[i].lpData = (LPSTR) &mixbuf[i*spec->size];
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		wavebuf[i].dwBufferLength = spec->size;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		wavebuf[i].dwFlags = WHDR_DONE;
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = waveOutPrepareHeader(sound, &wavebuf[i],
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							sizeof(wavebuf[i]));
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( result != MMSYSERR_NOERROR ) {
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SetMMerror("waveOutPrepareHeader()", result);
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Ready to go! */
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	next_buffer = 0;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
344