Mercurial > sdl-ios-xcode
view src/audio/SDL_audio.c @ 4157:baf615f9f2a0 SDL-1.2
Date: Thu, 16 Oct 2008 20:27:34 +0400
From: "Ilya Kasnacheev" <ilya.kasnacheev@gmail.com>
To: sdl@lists.libsdl.org
Subject: [SDL] SDL for Windows CE: a few GAPI patches
Hi *!
I've just ported a POWDER roguelike ( http://www.zincland.com/powder/ ) to
Windows CE (PDAs, Windows Mobile/Pocket PC). To do that, I had to get libsdl
working. Thanks for the awesome project files, it built without a hitch.
Nevertheless, I've found quite a few bugs in Windows CE (GAPI) SDL
implementation, which I've solved and now present as a serie of patches.
I'll try carefully annotate them. Please annotate them so I can work
toward accepting
them into the main source tree since without them SDL isn't really working on
Windows CE (I wonder why nobody fixed them before, btw: why isn't SDL popular as
a way to develop Windows CE games? Where are no ports?)
These changes can't be considered flawless, but they can be considered working
because I've yet to hear complains about things I fixed and POWDER build for
Windows CE is now considered stable.
Note: my comments start with !!, delete them before applying.
diff -bru SDL-1.2.13/src/video/gapi/SDL_gapivideo.c
SDL-1.2.13-new/src/video/gapi/SDL_gapivideo.c
--- SDL-1.2.13/src/video/gapi/SDL_gapivideo.c 2007-12-31
07:48:00.000000000 +0300
+++ SDL-1.2.13-new/src/video/gapi/SDL_gapivideo.c 2008-10-16
20:02:11.000000000 +0400
@@ -643,6 +643,7 @@
}
gapi->userOrientation = SDL_ORIENTATION_UP;
+ gapi->systemOrientation = SDL_ORIENTATION_UP;
video->flags = SDL_FULLSCREEN; /* Clear flags, GAPI supports
fullscreen only */
/* GAPI or VGA? */
@@ -661,18 +662,21 @@
}
/* detect user landscape mode */
- if( (width > height) && (GetSystemMetrics(SM_CXSCREEN) <
GetSystemMetrics(SM_CYSCREEN)))
+ if( (width > height) && (gapi->gxProperties.cxWidth <
gapi->gxProperties.cyHeight))
gapi->userOrientation = SDL_ORIENTATION_RIGHT;
+ if(GetSystemMetrics(SM_CYSCREEN) < GetSystemMetrics(SM_CXSCREEN))
+ gapi->systemOrientation = SDL_ORIENTATION_RIGHT;
+
/* shall we apply hires fix? for example when we do not use
hires resource */
gapi->hiresFix = 0;
- if( gapi->userOrientation == SDL_ORIENTATION_RIGHT )
+ if( gapi->systemOrientation == gapi->userOrientation )
{
- if( (width > GetSystemMetrics(SM_CYSCREEN)) || (height
> GetSystemMetrics(SM_CXSCREEN)))
+ if( (width > GetSystemMetrics(SM_CXSCREEN)) || (height
> GetSystemMetrics(SM_CYSCREEN)))
gapi->hiresFix = 1;
} else
- if( (width > GetSystemMetrics(SM_CXSCREEN)) || (height
> GetSystemMetrics(SM_CYSCREEN)))
- if( !((width == GetSystemMetrics(SM_CYSCREEN))
&& (height == GetSystemMetrics(SM_CXSCREEN)))) // user portrait,
device landscape
+ if( (width > GetSystemMetrics(SM_CYSCREEN)) || (height
> GetSystemMetrics(SM_CXSCREEN)))
+// if( !((width == gapi->gxProperties.cyHeight)
&& (height == gapi->gxProperties.cxWidth))) // user portrait, device
landscape
gapi->hiresFix = 1;
switch( gapi->userOrientation )
!! It used to query system metrics which return dimensions according to screen
!! orientation, which can really be portrait, left landscape or right landscape.
!! This is presumably incorrect because we couldn't care less about user mode
!! dimensions - all we want are the GAPI framebuffer dimensions, which
only match
!! user dimensions in one of possible orientations.
!! There's a fair dose of cargo cult programming involved in this fix, but it
!! used to work only in one orientation (portrait for PDAs, where frame-buffer
!! have same orientation as user screen), and now it works on all orientations.
@@ -742,21 +746,30 @@
WIN_FlushMessageQueue();
/* Open GAPI display */
- if( !gapi->useVga && this->hidden->useGXOpenDisplay )
+ if( !gapi->useVga && this->hidden->useGXOpenDisplay &&
!this->hidden->alreadyGXOpened )
+ {
+ this->hidden->alreadyGXOpened = 1;
if( !gapi->gxFunc.GXOpenDisplay(SDL_Window, GX_FULLSCREEN) )
{
SDL_SetError("Couldn't initialize GAPI");
return(NULL);
}
+ }
#if REPORT_VIDEO_INFO
printf("Video properties:\n");
printf("display bpp: %d\n", gapi->gxProperties.cBPP);
printf("display width: %d\n", gapi->gxProperties.cxWidth);
printf("display height: %d\n", gapi->gxProperties.cyHeight);
+ printf("system display width: %d\n", GetSystemMetrics(SM_CXSCREEN));
+ printf("system display height: %d\n", GetSystemMetrics(SM_CYSCREEN));
printf("x pitch: %d\n", gapi->gxProperties.cbxPitch);
printf("y pitch: %d\n", gapi->gxProperties.cbyPitch);
printf("gapi flags: 0x%x\n", gapi->gxProperties.ffFormat);
+ printf("user orientation: %d\n", gapi->userOrientation);
+ printf("system orientation: %d\n", gapi->userOrientation);
+ printf("gapi orientation: %d\n", gapi->gapiOrientation);
+
if( !gapi->useVga && this->hidden->useGXOpenDisplay && gapi->needUpdate)
{
!! Previous version used to call gapi->gxFunc.GXOpenDisplay each time the video
!! mode would be changed. You shouldn't, because this call has a
meaning "Lock the
!! GAPI framebuffer, designate it as busy", so the second call will fail (it is
!! already locked/busy).
!! Testing might not find that because most programs set up the video mode only
!! once, but POWDER does this once in a while, so it crashed when in
320x240 mode
!! (640x480 mode doesn't use that code, it worked fine).
diff -bru SDL-1.2.13/src/video/gapi/SDL_gapivideo.h
SDL-1.2.13-new/src/video/gapi/SDL_gapivideo.h
--- SDL-1.2.13/src/video/gapi/SDL_gapivideo.h 2007-12-31
07:48:00.000000000 +0300
+++ SDL-1.2.13-new/src/video/gapi/SDL_gapivideo.h 2008-10-16
20:02:11.000000000 +0400
@@ -132,12 +132,17 @@
#define NUM_MODELISTS 4 /* 8, 16, 24, and 32 bits-per-pixel */
int SDL_nummodes[NUM_MODELISTS];
SDL_Rect **SDL_modelist[NUM_MODELISTS];
+ // The orientation of the video mode user wants to get
+ // Probably restricted to UP and RIGHT
enum SDL_ScreenOrientation userOrientation;
int invert;
char hiresFix; // using hires mode without defining hires resource
// --------------
int useGXOpenDisplay; /* use GXOpenDispplay */
+ int alreadyGXOpened;
int w, h;
+ // The orientation of GAPI framebuffer.
+ // Never changes on the same device.
enum SDL_ScreenOrientation gapiOrientation;
void *buffer; // may be 8, 16, 24, 32 bpp
@@ -153,6 +158,10 @@
int startOffset; // in bytes
int useVga;
int suspended; // do not pu anything into video memory
+ // The orientation of the system, as defined by SM_CXSCREEN
and SM_CYSCREEN
+ // User can change it by using 'screen layout' in system options
+ // Restricted to UP or RIGHT
+ enum SDL_ScreenOrientation systemOrientation;
};
!! This is a flag variable, see the previous comment
!! And yet another orientation: now we have to keep three of them in mind.
diff -bru SDL-1.2.13/src/video/wincommon/SDL_sysevents.c
SDL-1.2.13-new/src/video/wincommon/SDL_sysevents.c
--- SDL-1.2.13/src/video/wincommon/SDL_sysevents.c 2007-12-31
07:48:02.000000000 +0300
+++ SDL-1.2.13-new/src/video/wincommon/SDL_sysevents.c 2008-10-16
20:02:12.000000000 +0400
@@ -160,10 +160,22 @@
#endif */
}
break;
+ // FIXME: Older version used just SDL_VideoSurface->(w, h)
+ // w and h are "clipped" while x and y are "raw", which caused
+ // x in former and y in latter case to be clipped in a
wrong direction,
+ // thus offsetting the coordinate on 2 x clip pixels
+ // (like, 128 for 640 -> 512 clipping).
+ // We will now try to extract and use raw values.
+ // The way to do that RIGHT is do
(orientation-dependent) clipping before
+ // doing this transform, but it's hardly possible.
+
+ // SEE SDL_mouse.c /ClipOffset to understand these calculations.
case SDL_ORIENTATION_RIGHT:
if (!SDL_VideoSurface)
break;
- rotatedX = SDL_VideoSurface->w - *y;
+ rotatedX = (2 *
((SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/
+ SDL_VideoSurface->format->BytesPerPixel))
+ + SDL_VideoSurface->w - *y;
rotatedY = *x;
*x = rotatedX;
*y = rotatedY;
@@ -172,7 +184,8 @@
if (!SDL_VideoSurface)
break;
rotatedX = *y;
- rotatedY = SDL_VideoSurface->h - *x;
+ rotatedY = (2 *
(SDL_VideoSurface->offset/SDL_VideoSurface->pitch))
+ + SDL_VideoSurface->h - *x;
*x = rotatedX;
*y = rotatedY;
break;
!! That's the trickest part, hence the long comment.
!! GAPI would really support only 320x240 or 640x480 mode, if application
!! requested the different screen size (as POWDER did, wishing
256x192), then SDL
!! is going to grab the first mode that fits the requested, and pad the screen
!! with black bars (as they do with wide-screen films).
!! It would also get, say, 240x320 mode, and to turn it into 256x192 it would
!! need to rotate mouse clicks.
!! It worked, but one bug slipped through: it would receive mouse clicks
!! unpadded, then rotate them, and then pad the black bars. The
problem is: rotate
!! is done by GAPI driver while padding is done by SDL core. SDL core
doesn't know
!! anything about rotating, so it would pad one of dimensions incorrectly.
I understand that some of my claims (or code) might seem unbacked, but you can
always grab the POWDER binary, compile your own libsdl with one or more of
those fixes turned off, and see how weird it would misbehave. I can even supply
you with those custom builds of libsdl if you don't want to set up the build
environment for windows ce, you'll just need a PDA or a smartphone with it.
I plan to take care of SDL on Windows CE as long as I maintain the POWDER port.
POWDER is good for that because it:
Employs both padded (with centered image, black bars) and unpadded
(image occupies full screen) graphics; initializes video more than
once; uses both 320x240 and 640x480 video; uses both stylus and
buttons.
There's still a list of unresolved issues which I'm planning to fix:
1) Arrow buttons on PDA return weird scancodes compared to PC, this
caused the game to misbehave before I've fixed that. You can see it on
those diagrams:
http://wrar.name/upload/powder-htc.png
http://wrar.name/upload/powder-pda.png
2) SDL (or underlying windows) doesn't care to rotate arrow presses
when we're in a low-res GAPI mode, but it will rotate them in VGA mode
(because of different screen orientations, the same arrow buttons can
suddently mean different directions). Solution: we should stick to
GAPI user orientation (the orientation the program supposedly wants)
and rotate the keys on our own.
_______________________________________________
SDL mailing list
SDL@lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 07 Nov 2008 04:15:36 +0000 |
parents | 009d85e98922 |
children | a1b03ba2fcd0 |
line wrap: on
line source
/* SDL - Simple DirectMedia Layer Copyright (C) 1997-2006 Sam Lantinga This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Sam Lantinga slouken@libsdl.org */ #include "SDL_config.h" /* Allow access to a raw mixing buffer */ #include "SDL.h" #include "SDL_audio_c.h" #include "SDL_audiomem.h" #include "SDL_sysaudio.h" #ifdef __OS2__ /* We'll need the DosSetPriority() API! */ #define INCL_DOSPROCESS #include <os2.h> #endif /* Available audio drivers */ static AudioBootStrap *bootstrap[] = { #if SDL_AUDIO_DRIVER_BSD &BSD_AUDIO_bootstrap, #endif #if SDL_AUDIO_DRIVER_OSS &DSP_bootstrap, &DMA_bootstrap, #endif #if SDL_AUDIO_DRIVER_ALSA &ALSA_bootstrap, #endif #if SDL_AUDIO_DRIVER_PULSE &PULSE_bootstrap, #endif #if SDL_AUDIO_DRIVER_QNXNTO &QNXNTOAUDIO_bootstrap, #endif #if SDL_AUDIO_DRIVER_SUNAUDIO &SUNAUDIO_bootstrap, #endif #if SDL_AUDIO_DRIVER_DMEDIA &DMEDIA_bootstrap, #endif #if SDL_AUDIO_DRIVER_ARTS &ARTS_bootstrap, #endif #if SDL_AUDIO_DRIVER_ESD &ESD_bootstrap, #endif #if SDL_AUDIO_DRIVER_NAS &NAS_bootstrap, #endif #if SDL_AUDIO_DRIVER_DSOUND &DSOUND_bootstrap, #endif #if SDL_AUDIO_DRIVER_WAVEOUT &WAVEOUT_bootstrap, #endif #if SDL_AUDIO_DRIVER_PAUD &Paud_bootstrap, #endif #if SDL_AUDIO_DRIVER_BAUDIO &BAUDIO_bootstrap, #endif #if SDL_AUDIO_DRIVER_COREAUDIO &COREAUDIO_bootstrap, #endif #if SDL_AUDIO_DRIVER_SNDMGR &SNDMGR_bootstrap, #endif #if SDL_AUDIO_DRIVER_MINT &MINTAUDIO_GSXB_bootstrap, &MINTAUDIO_MCSN_bootstrap, &MINTAUDIO_STFA_bootstrap, &MINTAUDIO_XBIOS_bootstrap, &MINTAUDIO_DMA8_bootstrap, #endif #if SDL_AUDIO_DRIVER_DISK &DISKAUD_bootstrap, #endif #if SDL_AUDIO_DRIVER_DUMMY &DUMMYAUD_bootstrap, #endif #if SDL_AUDIO_DRIVER_DC &DCAUD_bootstrap, #endif #if SDL_AUDIO_DRIVER_NDS &NDSAUD_bootstrap, #endif #if SDL_AUDIO_DRIVER_MMEAUDIO &MMEAUDIO_bootstrap, #endif #if SDL_AUDIO_DRIVER_DART &DART_bootstrap, #endif #if SDL_AUDIO_DRIVER_EPOCAUDIO &EPOCAudio_bootstrap, #endif NULL }; SDL_AudioDevice *current_audio = NULL; /* Various local functions */ int SDL_AudioInit(const char *driver_name); void SDL_AudioQuit(void); /* The general mixing thread function */ int SDLCALL SDL_RunAudio(void *audiop) { SDL_AudioDevice *audio = (SDL_AudioDevice *)audiop; Uint8 *stream; int stream_len; void *udata; void (SDLCALL *fill)(void *userdata,Uint8 *stream, int len); int silence; /* Perform any thread setup */ if ( audio->ThreadInit ) { audio->ThreadInit(audio); } audio->threadid = SDL_ThreadID(); /* Set up the mixing function */ fill = audio->spec.callback; udata = audio->spec.userdata; if ( audio->convert.needed ) { if ( audio->convert.src_format == AUDIO_U8 ) { silence = 0x80; } else { silence = 0; } stream_len = audio->convert.len; } else { silence = audio->spec.silence; stream_len = audio->spec.size; } #ifdef __OS2__ /* Increase the priority of this thread to make sure that the audio will be continuous all the time! */ #ifdef USE_DOSSETPRIORITY if (SDL_getenv("SDL_USE_TIMECRITICAL_AUDIO")) { #ifdef DEBUG_BUILD printf("[SDL_RunAudio] : Setting priority to TimeCritical+0! (TID%d)\n", SDL_ThreadID()); #endif DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0); } else { #ifdef DEBUG_BUILD printf("[SDL_RunAudio] : Setting priority to ForegroundServer+0! (TID%d)\n", SDL_ThreadID()); #endif DosSetPriority(PRTYS_THREAD, PRTYC_FOREGROUNDSERVER, 0, 0); } #endif #endif /* Loop, filling the audio buffers */ while ( audio->enabled ) { /* Fill the current buffer with sound */ if ( audio->convert.needed ) { if ( audio->convert.buf ) { stream = audio->convert.buf; } else { continue; } } else { stream = audio->GetAudioBuf(audio); if ( stream == NULL ) { stream = audio->fake_stream; } } SDL_memset(stream, silence, stream_len); if ( ! audio->paused ) { SDL_mutexP(audio->mixer_lock); (*fill)(udata, stream, stream_len); SDL_mutexV(audio->mixer_lock); } /* Convert the audio if necessary */ if ( audio->convert.needed ) { SDL_ConvertAudio(&audio->convert); stream = audio->GetAudioBuf(audio); if ( stream == NULL ) { stream = audio->fake_stream; } SDL_memcpy(stream, audio->convert.buf, audio->convert.len_cvt); } /* Ready current buffer for play and change current buffer */ if ( stream != audio->fake_stream ) { audio->PlayAudio(audio); } /* Wait for an audio buffer to become available */ if ( stream == audio->fake_stream ) { SDL_Delay((audio->spec.samples*1000)/audio->spec.freq); } else { audio->WaitAudio(audio); } } /* Wait for the audio to drain.. */ if ( audio->WaitDone ) { audio->WaitDone(audio); } #ifdef __OS2__ #ifdef DEBUG_BUILD printf("[SDL_RunAudio] : Task exiting. (TID%d)\n", SDL_ThreadID()); #endif #endif return(0); } static void SDL_LockAudio_Default(SDL_AudioDevice *audio) { if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) { return; } SDL_mutexP(audio->mixer_lock); } static void SDL_UnlockAudio_Default(SDL_AudioDevice *audio) { if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) { return; } SDL_mutexV(audio->mixer_lock); } static Uint16 SDL_ParseAudioFormat(const char *string) { Uint16 format = 0; switch (*string) { case 'U': ++string; format |= 0x0000; break; case 'S': ++string; format |= 0x8000; break; default: return 0; } switch (SDL_atoi(string)) { case 8: string += 1; format |= 8; break; case 16: string += 2; format |= 16; if ( SDL_strcmp(string, "LSB") == 0 #if SDL_BYTEORDER == SDL_LIL_ENDIAN || SDL_strcmp(string, "SYS") == 0 #endif ) { format |= 0x0000; } if ( SDL_strcmp(string, "MSB") == 0 #if SDL_BYTEORDER == SDL_BIG_ENDIAN || SDL_strcmp(string, "SYS") == 0 #endif ) { format |= 0x1000; } break; default: return 0; } return format; } int SDL_AudioInit(const char *driver_name) { SDL_AudioDevice *audio; int i = 0, idx; /* Check to make sure we don't overwrite 'current_audio' */ if ( current_audio != NULL ) { SDL_AudioQuit(); } /* Select the proper audio driver */ audio = NULL; idx = 0; #if SDL_AUDIO_DRIVER_ESD if ( (driver_name == NULL) && (SDL_getenv("ESPEAKER") != NULL) ) { /* Ahem, we know that if ESPEAKER is set, user probably wants to use ESD, but don't start it if it's not already running. This probably isn't the place to do this, but... Shh! :) */ for ( i=0; bootstrap[i]; ++i ) { if ( SDL_strcasecmp(bootstrap[i]->name, "esd") == 0 ) { #ifdef HAVE_PUTENV const char *esd_no_spawn; /* Don't start ESD if it's not running */ esd_no_spawn = getenv("ESD_NO_SPAWN"); if ( esd_no_spawn == NULL ) { putenv("ESD_NO_SPAWN=1"); } #endif if ( bootstrap[i]->available() ) { audio = bootstrap[i]->create(0); break; } #ifdef HAVE_UNSETENV if ( esd_no_spawn == NULL ) { unsetenv("ESD_NO_SPAWN"); } #endif } } } #endif /* SDL_AUDIO_DRIVER_ESD */ if ( audio == NULL ) { if ( driver_name != NULL ) { #if 0 /* This will be replaced with a better driver selection API */ if ( SDL_strrchr(driver_name, ':') != NULL ) { idx = atoi(SDL_strrchr(driver_name, ':')+1); } #endif for ( i=0; bootstrap[i]; ++i ) { if (SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) { if ( bootstrap[i]->available() ) { audio=bootstrap[i]->create(idx); break; } } } } else { for ( i=0; bootstrap[i]; ++i ) { if ( bootstrap[i]->available() ) { audio = bootstrap[i]->create(idx); if ( audio != NULL ) { break; } } } } if ( audio == NULL ) { SDL_SetError("No available audio device"); #if 0 /* Don't fail SDL_Init() if audio isn't available. SDL_OpenAudio() will handle it at that point. *sigh* */ return(-1); #endif } } current_audio = audio; if ( current_audio ) { current_audio->name = bootstrap[i]->name; if ( !current_audio->LockAudio && !current_audio->UnlockAudio ) { current_audio->LockAudio = SDL_LockAudio_Default; current_audio->UnlockAudio = SDL_UnlockAudio_Default; } } return(0); } char *SDL_AudioDriverName(char *namebuf, int maxlen) { if ( current_audio != NULL ) { SDL_strlcpy(namebuf, current_audio->name, maxlen); return(namebuf); } return(NULL); } int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained) { SDL_AudioDevice *audio; const char *env; /* Start up the audio driver, if necessary */ if ( ! current_audio ) { if ( (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) || (current_audio == NULL) ) { return(-1); } } audio = current_audio; if (audio->opened) { SDL_SetError("Audio device is already opened"); return(-1); } /* Verify some parameters */ if ( desired->freq == 0 ) { env = SDL_getenv("SDL_AUDIO_FREQUENCY"); if ( env ) { desired->freq = SDL_atoi(env); } } if ( desired->freq == 0 ) { /* Pick some default audio frequency */ desired->freq = 22050; } if ( desired->format == 0 ) { env = SDL_getenv("SDL_AUDIO_FORMAT"); if ( env ) { desired->format = SDL_ParseAudioFormat(env); } } if ( desired->format == 0 ) { /* Pick some default audio format */ desired->format = AUDIO_S16; } if ( desired->channels == 0 ) { env = SDL_getenv("SDL_AUDIO_CHANNELS"); if ( env ) { desired->channels = (Uint8)SDL_atoi(env); } } if ( desired->channels == 0 ) { /* Pick a default number of channels */ desired->channels = 2; } switch ( desired->channels ) { case 1: /* Mono */ case 2: /* Stereo */ case 4: /* surround */ case 6: /* surround with center and lfe */ break; default: SDL_SetError("1 (mono) and 2 (stereo) channels supported"); return(-1); } if ( desired->samples == 0 ) { env = SDL_getenv("SDL_AUDIO_SAMPLES"); if ( env ) { desired->samples = (Uint16)SDL_atoi(env); } } if ( desired->samples == 0 ) { /* Pick a default of ~46 ms at desired frequency */ int samples = (desired->freq / 1000) * 46; int power2 = 1; while ( power2 < samples ) { power2 *= 2; } desired->samples = power2; } if ( desired->callback == NULL ) { SDL_SetError("SDL_OpenAudio() passed a NULL callback"); return(-1); } #if SDL_THREADS_DISABLED /* Uses interrupt driven audio, without thread */ #else /* Create a semaphore for locking the sound buffers */ audio->mixer_lock = SDL_CreateMutex(); if ( audio->mixer_lock == NULL ) { SDL_SetError("Couldn't create mixer lock"); SDL_CloseAudio(); return(-1); } #endif /* SDL_THREADS_DISABLED */ /* Calculate the silence and size of the audio specification */ SDL_CalculateAudioSpec(desired); /* Open the audio subsystem */ SDL_memcpy(&audio->spec, desired, sizeof(audio->spec)); audio->convert.needed = 0; audio->enabled = 1; audio->paused = 1; audio->opened = audio->OpenAudio(audio, &audio->spec)+1; if ( ! audio->opened ) { SDL_CloseAudio(); return(-1); } /* If the audio driver changes the buffer size, accept it */ if ( audio->spec.samples != desired->samples ) { desired->samples = audio->spec.samples; SDL_CalculateAudioSpec(desired); } /* Allocate a fake audio memory buffer */ audio->fake_stream = SDL_AllocAudioMem(audio->spec.size); if ( audio->fake_stream == NULL ) { SDL_CloseAudio(); SDL_OutOfMemory(); return(-1); } /* See if we need to do any conversion */ if ( obtained != NULL ) { SDL_memcpy(obtained, &audio->spec, sizeof(audio->spec)); } else if ( desired->freq != audio->spec.freq || desired->format != audio->spec.format || desired->channels != audio->spec.channels ) { /* Build an audio conversion block */ if ( SDL_BuildAudioCVT(&audio->convert, desired->format, desired->channels, desired->freq, audio->spec.format, audio->spec.channels, audio->spec.freq) < 0 ) { SDL_CloseAudio(); return(-1); } if ( audio->convert.needed ) { audio->convert.len = (int) ( ((double) desired->size) / audio->convert.len_ratio ); audio->convert.buf =(Uint8 *)SDL_AllocAudioMem( audio->convert.len*audio->convert.len_mult); if ( audio->convert.buf == NULL ) { SDL_CloseAudio(); SDL_OutOfMemory(); return(-1); } } } /* Start the audio thread if necessary */ switch (audio->opened) { case 1: /* Start the audio thread */ #if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC) && !defined(__SYMBIAN32__) #undef SDL_CreateThread audio->thread = SDL_CreateThread(SDL_RunAudio, audio, NULL, NULL); #else audio->thread = SDL_CreateThread(SDL_RunAudio, audio); #endif if ( audio->thread == NULL ) { SDL_CloseAudio(); SDL_SetError("Couldn't create audio thread"); return(-1); } break; default: /* The audio is now playing */ break; } return(0); } SDL_audiostatus SDL_GetAudioStatus(void) { SDL_AudioDevice *audio = current_audio; SDL_audiostatus status; status = SDL_AUDIO_STOPPED; if ( audio && audio->enabled ) { if ( audio->paused ) { status = SDL_AUDIO_PAUSED; } else { status = SDL_AUDIO_PLAYING; } } return(status); } void SDL_PauseAudio (int pause_on) { SDL_AudioDevice *audio = current_audio; if ( audio ) { audio->paused = pause_on; } } void SDL_LockAudio (void) { SDL_AudioDevice *audio = current_audio; /* Obtain a lock on the mixing buffers */ if ( audio && audio->LockAudio ) { audio->LockAudio(audio); } } void SDL_UnlockAudio (void) { SDL_AudioDevice *audio = current_audio; /* Release lock on the mixing buffers */ if ( audio && audio->UnlockAudio ) { audio->UnlockAudio(audio); } } void SDL_CloseAudio (void) { SDL_QuitSubSystem(SDL_INIT_AUDIO); } void SDL_AudioQuit(void) { SDL_AudioDevice *audio = current_audio; if ( audio ) { audio->enabled = 0; if ( audio->thread != NULL ) { SDL_WaitThread(audio->thread, NULL); } if ( audio->mixer_lock != NULL ) { SDL_DestroyMutex(audio->mixer_lock); } if ( audio->fake_stream != NULL ) { SDL_FreeAudioMem(audio->fake_stream); } if ( audio->convert.needed ) { SDL_FreeAudioMem(audio->convert.buf); } if ( audio->opened ) { audio->CloseAudio(audio); audio->opened = 0; } /* Free the driver data */ audio->free(audio); current_audio = NULL; } } #define NUM_FORMATS 6 static int format_idx; static int format_idx_sub; static Uint16 format_list[NUM_FORMATS][NUM_FORMATS] = { { AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB }, { AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB }, { AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8 }, { AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8 }, { AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U8, AUDIO_S8 }, { AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U8, AUDIO_S8 }, }; Uint16 SDL_FirstAudioFormat(Uint16 format) { for ( format_idx=0; format_idx < NUM_FORMATS; ++format_idx ) { if ( format_list[format_idx][0] == format ) { break; } } format_idx_sub = 0; return(SDL_NextAudioFormat()); } Uint16 SDL_NextAudioFormat(void) { if ( (format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS) ) { return(0); } return(format_list[format_idx][format_idx_sub++]); } void SDL_CalculateAudioSpec(SDL_AudioSpec *spec) { switch (spec->format) { case AUDIO_U8: spec->silence = 0x80; break; default: spec->silence = 0x00; break; } spec->size = (spec->format&0xFF)/8; spec->size *= spec->channels; spec->size *= spec->samples; }