src/audio/windib/SDL_dibaudio.c
author Sam Lantinga
Mon, 06 Feb 2006 08:28:51 +0000
changeset 1330 450721ad5436
parent 1312 c9b51268668f
child 1336 3692456e7b0f
permissions -rw-r--r--
It's now possible to build SDL without any C runtime at all on Windows,
using Visual C++ 2005
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
/* Allow access to a raw mixing buffer */
slouken@0
    24
slouken@1330
    25
#include "SDL_windows.h"
slouken@0
    26
#include <mmsystem.h>
slouken@0
    27
slouken@0
    28
#include "SDL_audio.h"
slouken@0
    29
#include "SDL_mutex.h"
slouken@0
    30
#include "SDL_timer.h"
slouken@1330
    31
#include "SDL_stdlib.h"
slouken@1330
    32
#include "SDL_string.h"
slouken@0
    33
#include "SDL_audio_c.h"
slouken@0
    34
#include "SDL_dibaudio.h"
slouken@36
    35
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
    36
#include "win_ce_semaphore.h"
slouken@36
    37
#endif
slouken@0
    38
slouken@0
    39
slouken@0
    40
/* Audio driver functions */
slouken@0
    41
static int DIB_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
    42
static void DIB_ThreadInit(_THIS);
slouken@0
    43
static void DIB_WaitAudio(_THIS);
slouken@0
    44
static Uint8 *DIB_GetAudioBuf(_THIS);
slouken@0
    45
static void DIB_PlayAudio(_THIS);
slouken@0
    46
static void DIB_WaitDone(_THIS);
slouken@0
    47
static void DIB_CloseAudio(_THIS);
slouken@0
    48
slouken@0
    49
/* Audio driver bootstrap functions */
slouken@0
    50
slouken@0
    51
static int Audio_Available(void)
slouken@0
    52
{
slouken@0
    53
	return(1);
slouken@0
    54
}
slouken@0
    55
slouken@0
    56
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
    57
{
slouken@0
    58
	free(device->hidden);
slouken@0
    59
	free(device);
slouken@0
    60
}
slouken@0
    61
slouken@0
    62
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
    63
{
slouken@0
    64
	SDL_AudioDevice *this;
slouken@0
    65
slouken@0
    66
	/* Initialize all variables that we clean on shutdown */
slouken@0
    67
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
    68
	if ( this ) {
slouken@0
    69
		memset(this, 0, (sizeof *this));
slouken@0
    70
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
    71
				malloc((sizeof *this->hidden));
slouken@0
    72
	}
slouken@0
    73
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
    74
		SDL_OutOfMemory();
slouken@0
    75
		if ( this ) {
slouken@0
    76
			free(this);
slouken@0
    77
		}
slouken@0
    78
		return(0);
slouken@0
    79
	}
slouken@0
    80
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
    81
slouken@0
    82
	/* Set the function pointers */
slouken@0
    83
	this->OpenAudio = DIB_OpenAudio;
slouken@0
    84
	this->ThreadInit = DIB_ThreadInit;
slouken@0
    85
	this->WaitAudio = DIB_WaitAudio;
slouken@0
    86
	this->PlayAudio = DIB_PlayAudio;
slouken@0
    87
	this->GetAudioBuf = DIB_GetAudioBuf;
slouken@0
    88
	this->WaitDone = DIB_WaitDone;
slouken@0
    89
	this->CloseAudio = DIB_CloseAudio;
slouken@0
    90
slouken@0
    91
	this->free = Audio_DeleteDevice;
slouken@0
    92
slouken@0
    93
	return this;
slouken@0
    94
}
slouken@0
    95
slouken@0
    96
AudioBootStrap WAVEOUT_bootstrap = {
slouken@0
    97
	"waveout", "Win95/98/NT/2000 WaveOut",
slouken@0
    98
	Audio_Available, Audio_CreateDevice
slouken@0
    99
};
slouken@0
   100
slouken@0
   101
slouken@0
   102
/* The Win32 callback for filling the WAVE device */
slouken@0
   103
static void CALLBACK FillSound(HWAVEOUT hwo, UINT uMsg, DWORD dwInstance,
slouken@0
   104
						DWORD dwParam1, DWORD dwParam2)
slouken@0
   105
{
slouken@0
   106
	SDL_AudioDevice *this = (SDL_AudioDevice *)dwInstance;
slouken@0
   107
slouken@0
   108
	/* Only service "buffer done playing" messages */
slouken@0
   109
	if ( uMsg != WOM_DONE )
slouken@0
   110
		return;
slouken@0
   111
slouken@0
   112
	/* Signal that we are done playing a buffer */
slouken@36
   113
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
   114
	ReleaseSemaphoreCE(audio_sem, 1, NULL);
slouken@36
   115
#else
slouken@0
   116
	ReleaseSemaphore(audio_sem, 1, NULL);
slouken@36
   117
#endif
slouken@0
   118
}
slouken@0
   119
slouken@0
   120
static void SetMMerror(char *function, MMRESULT code)
slouken@0
   121
{
slouken@0
   122
	int len;
slouken@0
   123
	char errbuf[MAXERRORLENGTH];
slouken@36
   124
#ifdef _WIN32_WCE
slouken@36
   125
	wchar_t werrbuf[MAXERRORLENGTH];
slouken@36
   126
#endif
slouken@0
   127
slouken@1330
   128
	snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
slouken@0
   129
	len = strlen(errbuf);
slouken@36
   130
slouken@36
   131
#ifdef _WIN32_WCE
slouken@36
   132
	/* UNICODE version */
slouken@36
   133
	waveOutGetErrorText(code, werrbuf, MAXERRORLENGTH-len);
slouken@36
   134
	WideCharToMultiByte(CP_ACP,0,werrbuf,-1,errbuf+len,MAXERRORLENGTH-len,NULL,NULL);
slouken@36
   135
#else
slouken@0
   136
	waveOutGetErrorText(code, errbuf+len, MAXERRORLENGTH-len);
slouken@36
   137
#endif
slouken@36
   138
slouken@36
   139
	SDL_SetError("%s",errbuf);
slouken@0
   140
}
slouken@0
   141
slouken@0
   142
/* Set high priority for the audio thread */
slouken@0
   143
static void DIB_ThreadInit(_THIS)
slouken@0
   144
{
slouken@0
   145
	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
slouken@0
   146
}
slouken@0
   147
slouken@0
   148
void DIB_WaitAudio(_THIS)
slouken@0
   149
{
slouken@0
   150
	/* Wait for an audio chunk to finish */
slouken@36
   151
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
   152
	WaitForSemaphoreCE(audio_sem, INFINITE);
slouken@36
   153
#else
slouken@0
   154
	WaitForSingleObject(audio_sem, INFINITE);
slouken@36
   155
#endif
slouken@0
   156
}
slouken@0
   157
slouken@0
   158
Uint8 *DIB_GetAudioBuf(_THIS)
slouken@0
   159
{
slouken@0
   160
        Uint8 *retval;
slouken@0
   161
slouken@0
   162
	retval = (Uint8 *)(wavebuf[next_buffer].lpData);
slouken@0
   163
	return retval;
slouken@0
   164
}
slouken@0
   165
slouken@0
   166
void DIB_PlayAudio(_THIS)
slouken@0
   167
{
slouken@0
   168
	/* Queue it up */
slouken@0
   169
	waveOutWrite(sound, &wavebuf[next_buffer], sizeof(wavebuf[0]));
slouken@0
   170
	next_buffer = (next_buffer+1)%NUM_BUFFERS;
slouken@0
   171
}
slouken@0
   172
slouken@0
   173
void DIB_WaitDone(_THIS)
slouken@0
   174
{
slouken@0
   175
	int i, left;
slouken@0
   176
slouken@0
   177
	do {
slouken@0
   178
		left = NUM_BUFFERS;
slouken@0
   179
		for ( i=0; i<NUM_BUFFERS; ++i ) {
slouken@0
   180
			if ( wavebuf[i].dwFlags & WHDR_DONE ) {
slouken@0
   181
				--left;
slouken@0
   182
			}
slouken@0
   183
		}
slouken@0
   184
		if ( left > 0 ) {
slouken@0
   185
			SDL_Delay(100);
slouken@0
   186
		}
slouken@0
   187
	} while ( left > 0 );
slouken@0
   188
}
slouken@0
   189
slouken@0
   190
void DIB_CloseAudio(_THIS)
slouken@0
   191
{
slouken@0
   192
	int i;
slouken@0
   193
slouken@0
   194
	/* Close up audio */
slouken@0
   195
	if ( audio_sem ) {
slouken@36
   196
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
   197
		CloseSynchHandle(audio_sem);
slouken@36
   198
#else
slouken@0
   199
		CloseHandle(audio_sem);
slouken@36
   200
#endif
slouken@0
   201
	}
slouken@0
   202
	if ( sound ) {
slouken@0
   203
		waveOutClose(sound);
slouken@0
   204
	}
slouken@0
   205
slouken@0
   206
	/* Clean up mixing buffers */
slouken@0
   207
	for ( i=0; i<NUM_BUFFERS; ++i ) {
slouken@0
   208
		if ( wavebuf[i].dwUser != 0xFFFF ) {
slouken@0
   209
			waveOutUnprepareHeader(sound, &wavebuf[i],
slouken@0
   210
						sizeof(wavebuf[i]));
slouken@0
   211
			wavebuf[i].dwUser = 0xFFFF;
slouken@0
   212
		}
slouken@0
   213
	}
slouken@0
   214
	/* Free raw mixing buffer */
slouken@0
   215
	if ( mixbuf != NULL ) {
slouken@0
   216
		free(mixbuf);
slouken@0
   217
		mixbuf = NULL;
slouken@0
   218
	}
slouken@0
   219
}
slouken@0
   220
slouken@0
   221
int DIB_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   222
{
slouken@0
   223
	MMRESULT result;
slouken@0
   224
	int i;
slouken@0
   225
	WAVEFORMATEX waveformat;
slouken@0
   226
slouken@0
   227
	/* Initialize the wavebuf structures for closing */
slouken@0
   228
	sound = NULL;
slouken@0
   229
	audio_sem = NULL;
slouken@0
   230
	for ( i = 0; i < NUM_BUFFERS; ++i )
slouken@0
   231
		wavebuf[i].dwUser = 0xFFFF;
slouken@0
   232
	mixbuf = NULL;
slouken@0
   233
slouken@0
   234
	/* Set basic WAVE format parameters */
slouken@0
   235
	memset(&waveformat, 0, sizeof(waveformat));
slouken@0
   236
	waveformat.wFormatTag = WAVE_FORMAT_PCM;
slouken@0
   237
slouken@0
   238
	/* Determine the audio parameters from the AudioSpec */
slouken@0
   239
	switch ( spec->format & 0xFF ) {
slouken@0
   240
		case 8:
slouken@0
   241
			/* Unsigned 8 bit audio data */
slouken@0
   242
			spec->format = AUDIO_U8;
slouken@0
   243
			waveformat.wBitsPerSample = 8;
slouken@0
   244
			break;
slouken@0
   245
		case 16:
slouken@0
   246
			/* Signed 16 bit audio data */
slouken@0
   247
			spec->format = AUDIO_S16;
slouken@0
   248
			waveformat.wBitsPerSample = 16;
slouken@0
   249
			break;
slouken@0
   250
		default:
slouken@0
   251
			SDL_SetError("Unsupported audio format");
slouken@0
   252
			return(-1);
slouken@0
   253
	}
slouken@0
   254
	waveformat.nChannels = spec->channels;
slouken@0
   255
	waveformat.nSamplesPerSec = spec->freq;
slouken@0
   256
	waveformat.nBlockAlign =
slouken@0
   257
		waveformat.nChannels * (waveformat.wBitsPerSample/8);
slouken@0
   258
	waveformat.nAvgBytesPerSec = 
slouken@0
   259
		waveformat.nSamplesPerSec * waveformat.nBlockAlign;
slouken@0
   260
slouken@0
   261
	/* Check the buffer size -- minimum of 1/4 second (word aligned) */
slouken@0
   262
	if ( spec->samples < (spec->freq/4) )
slouken@0
   263
		spec->samples = ((spec->freq/4)+3)&~3;
slouken@0
   264
slouken@0
   265
	/* Update the fragment size as size in bytes */
slouken@0
   266
	SDL_CalculateAudioSpec(spec);
slouken@0
   267
slouken@0
   268
	/* Open the audio device */
slouken@0
   269
	result = waveOutOpen(&sound, WAVE_MAPPER, &waveformat,
slouken@0
   270
			(DWORD)FillSound, (DWORD)this, CALLBACK_FUNCTION);
slouken@0
   271
	if ( result != MMSYSERR_NOERROR ) {
slouken@0
   272
		SetMMerror("waveOutOpen()", result);
slouken@0
   273
		return(-1);
slouken@0
   274
	}
slouken@0
   275
slouken@0
   276
#ifdef SOUND_DEBUG
slouken@0
   277
	/* Check the sound device we retrieved */
slouken@0
   278
	{
slouken@0
   279
		WAVEOUTCAPS caps;
slouken@0
   280
slouken@0
   281
		result = waveOutGetDevCaps((UINT)sound, &caps, sizeof(caps));
slouken@0
   282
		if ( result != MMSYSERR_NOERROR ) {
slouken@0
   283
			SetMMerror("waveOutGetDevCaps()", result);
slouken@0
   284
			return(-1);
slouken@0
   285
		}
slouken@0
   286
		printf("Audio device: %s\n", caps.szPname);
slouken@0
   287
	}
slouken@0
   288
#endif
slouken@0
   289
slouken@0
   290
	/* Create the audio buffer semaphore */
slouken@36
   291
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
   292
	audio_sem = CreateSemaphoreCE(NULL, NUM_BUFFERS-1, NUM_BUFFERS, NULL);
slouken@36
   293
#else
slouken@0
   294
	audio_sem = CreateSemaphore(NULL, NUM_BUFFERS-1, NUM_BUFFERS, NULL);
slouken@36
   295
#endif
slouken@0
   296
	if ( audio_sem == NULL ) {
slouken@0
   297
		SDL_SetError("Couldn't create semaphore");
slouken@0
   298
		return(-1);
slouken@0
   299
	}
slouken@0
   300
slouken@0
   301
	/* Create the sound buffers */
slouken@0
   302
	mixbuf = (Uint8 *)malloc(NUM_BUFFERS*spec->size);
slouken@0
   303
	if ( mixbuf == NULL ) {
slouken@0
   304
		SDL_SetError("Out of memory");
slouken@0
   305
		return(-1);
slouken@0
   306
	}
slouken@0
   307
	for ( i = 0; i < NUM_BUFFERS; ++i ) {
slouken@0
   308
		memset(&wavebuf[i], 0, sizeof(wavebuf[i]));
slouken@0
   309
		wavebuf[i].lpData = (LPSTR) &mixbuf[i*spec->size];
slouken@0
   310
		wavebuf[i].dwBufferLength = spec->size;
slouken@0
   311
		wavebuf[i].dwFlags = WHDR_DONE;
slouken@0
   312
		result = waveOutPrepareHeader(sound, &wavebuf[i],
slouken@0
   313
							sizeof(wavebuf[i]));
slouken@0
   314
		if ( result != MMSYSERR_NOERROR ) {
slouken@0
   315
			SetMMerror("waveOutPrepareHeader()", result);
slouken@0
   316
			return(-1);
slouken@0
   317
		}
slouken@0
   318
	}
slouken@0
   319
slouken@0
   320
	/* Ready to go! */
slouken@0
   321
	next_buffer = 0;
slouken@0
   322
	return(0);
slouken@0
   323
}