From b4af184139e307f4f854599e4ba330788fcf7475 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 21 May 2013 21:21:23 -0700 Subject: [PATCH] Cleaned up whitespace for the 2.0.0 release --- SDL_mixer.h | 94 +- VisualC/resource.h | 30 +- Xcode/playmus/SDLMain.m | 50 +- Xcode/playwave/SDLMain.m | 50 +- dynamic_flac.c | 252 ++-- dynamic_flac.h | 58 +- dynamic_fluidsynth.c | 68 +- dynamic_fluidsynth.h | 40 +- dynamic_mod.c | 470 +++---- dynamic_mod.h | 58 +- dynamic_mp3.c | 244 ++-- dynamic_mp3.h | 28 +- dynamic_ogg.c | 152 +-- dynamic_ogg.h | 20 +- effect_position.c | 876 ++++++------ effect_stereoreverse.c | 2 +- effects_internal.c | 2 +- fluidsynth.c | 250 ++-- fluidsynth.h | 6 +- load_aiff.c | 376 +++--- load_aiff.h | 2 +- load_flac.c | 482 +++---- load_flac.h | 4 +- load_ogg.c | 18 +- load_voc.c | 74 +- mixer.c | 2080 ++++++++++++++--------------- music_cmd.c | 302 ++--- music_cmd.h | 6 +- music_flac.c | 942 ++++++------- music_flac.h | 50 +- music_mad.c | 290 ++-- music_mad.h | 4 +- music_mod.c | 430 +++--- music_modplug.c | 354 ++--- music_modplug.h | 4 +- music_ogg.c | 238 ++-- music_ogg.h | 18 +- native_midi/native_midi_common.c | 606 ++++----- native_midi/native_midi_common.h | 34 +- native_midi/native_midi_haiku.cpp | 28 +- native_midi/native_midi_mac.c | 1054 +++++++-------- native_midi/native_midi_macosx.c | 2 +- native_midi/native_midi_win32.c | 106 +- playmus.c | 338 ++--- playwave.c | 584 ++++---- wavestream.c | 746 +++++------ wavestream.h | 10 +- 47 files changed, 5964 insertions(+), 5968 deletions(-) diff --git a/SDL_mixer.h b/SDL_mixer.h index 4f998a38..4634ed8c 100644 --- a/SDL_mixer.h +++ b/SDL_mixer.h @@ -38,25 +38,25 @@ extern "C" { /* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL */ -#define SDL_MIXER_MAJOR_VERSION 2 -#define SDL_MIXER_MINOR_VERSION 0 +#define SDL_MIXER_MAJOR_VERSION 2 +#define SDL_MIXER_MINOR_VERSION 0 #define SDL_MIXER_PATCHLEVEL 0 /* This macro can be used to fill a version structure with the compile-time * version of the SDL_mixer library. */ -#define SDL_MIXER_VERSION(X) \ -{ \ - (X)->major = SDL_MIXER_MAJOR_VERSION; \ - (X)->minor = SDL_MIXER_MINOR_VERSION; \ - (X)->patch = SDL_MIXER_PATCHLEVEL; \ +#define SDL_MIXER_VERSION(X) \ +{ \ + (X)->major = SDL_MIXER_MAJOR_VERSION; \ + (X)->minor = SDL_MIXER_MINOR_VERSION; \ + (X)->patch = SDL_MIXER_PATCHLEVEL; \ } /* Backwards compatibility */ -#define MIX_MAJOR_VERSION SDL_MIXER_MAJOR_VERSION -#define MIX_MINOR_VERSION SDL_MIXER_MINOR_VERSION -#define MIX_PATCHLEVEL SDL_MIXER_PATCHLEVEL -#define MIX_VERSION(X) SDL_MIXER_VERSION(X) +#define MIX_MAJOR_VERSION SDL_MIXER_MAJOR_VERSION +#define MIX_MINOR_VERSION SDL_MIXER_MINOR_VERSION +#define MIX_PATCHLEVEL SDL_MIXER_PATCHLEVEL +#define MIX_VERSION(X) SDL_MIXER_VERSION(X) /* This function gets the version of the dynamically linked SDL_mixer library. it should NOT be used to fill a version structure, instead you should @@ -85,53 +85,52 @@ extern DECLSPEC void SDLCALL Mix_Quit(void); /* The default mixer has 8 simultaneous mixing channels */ #ifndef MIX_CHANNELS -#define MIX_CHANNELS 8 +#define MIX_CHANNELS 8 #endif /* Good default values for a PC soundcard */ -#define MIX_DEFAULT_FREQUENCY 22050 +#define MIX_DEFAULT_FREQUENCY 22050 #if SDL_BYTEORDER == SDL_LIL_ENDIAN -#define MIX_DEFAULT_FORMAT AUDIO_S16LSB +#define MIX_DEFAULT_FORMAT AUDIO_S16LSB #else -#define MIX_DEFAULT_FORMAT AUDIO_S16MSB +#define MIX_DEFAULT_FORMAT AUDIO_S16MSB #endif -#define MIX_DEFAULT_CHANNELS 2 -#define MIX_MAX_VOLUME 128 /* Volume of a chunk */ +#define MIX_DEFAULT_CHANNELS 2 +#define MIX_MAX_VOLUME 128 /* Volume of a chunk */ /* The internal format for an audio chunk */ typedef struct Mix_Chunk { - int allocated; - Uint8 *abuf; - Uint32 alen; - Uint8 volume; /* Per-sample volume, 0-128 */ + int allocated; + Uint8 *abuf; + Uint32 alen; + Uint8 volume; /* Per-sample volume, 0-128 */ } Mix_Chunk; /* The different fading types supported */ typedef enum { - MIX_NO_FADING, - MIX_FADING_OUT, - MIX_FADING_IN + MIX_NO_FADING, + MIX_FADING_OUT, + MIX_FADING_IN } Mix_Fading; typedef enum { - MUS_NONE, - MUS_CMD, - MUS_WAV, - MUS_MOD, - MUS_MID, - MUS_OGG, - MUS_MP3, - MUS_MP3_MAD, - MUS_FLAC, - MUS_MODPLUG + MUS_NONE, + MUS_CMD, + MUS_WAV, + MUS_MOD, + MUS_MID, + MUS_OGG, + MUS_MP3, + MUS_MP3_MAD, + MUS_FLAC, + MUS_MODPLUG } Mix_MusicType; /* The internal format for a music chunk interpreted via mikmod */ typedef struct _Mix_Music Mix_Music; /* Open the mixer with a certain audio format */ -extern DECLSPEC int SDLCALL Mix_OpenAudio(int frequency, Uint16 format, int channels, - int chunksize); +extern DECLSPEC int SDLCALL Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize); /* Dynamically change the number of channels managed by the mixer. If decreasing the number of channels, the upper channels are @@ -147,7 +146,7 @@ extern DECLSPEC int SDLCALL Mix_QuerySpec(int *frequency,Uint16 *format,int *cha /* Load a wave file or a music (.mod .s3m .it .xm) file */ extern DECLSPEC Mix_Chunk * SDLCALL Mix_LoadWAV_RW(SDL_RWops *src, int freesrc); -#define Mix_LoadWAV(file) Mix_LoadWAV_RW(SDL_RWFromFile(file, "rb"), 1) +#define Mix_LoadWAV(file) Mix_LoadWAV_RW(SDL_RWFromFile(file, "rb"), 1) extern DECLSPEC Mix_Music * SDLCALL Mix_LoadMUS(const char *file); /* Load a music file from an SDL_RWop object (Ogg and MikMod specific currently) @@ -200,14 +199,12 @@ extern DECLSPEC Mix_MusicType SDLCALL Mix_GetMusicType(const Mix_Music *music); This can be used to provide real-time visual display of the audio stream or add a custom mixer filter for the stream data. */ -extern DECLSPEC void SDLCALL Mix_SetPostMix(void (*mix_func) - (void *udata, Uint8 *stream, int len), void *arg); +extern DECLSPEC void SDLCALL Mix_SetPostMix(void (*mix_func)(void *udata, Uint8 *stream, int len), void *arg); /* Add your own music player or additional mixer function. If 'mix_func' is NULL, the default music player is re-enabled. */ -extern DECLSPEC void SDLCALL Mix_HookMusic(void (*mix_func) - (void *udata, Uint8 *stream, int len), void *arg); +extern DECLSPEC void SDLCALL Mix_HookMusic(void (*mix_func)(void *udata, Uint8 *stream, int len), void *arg); /* Add your own callback when the music has finished playing. This callback is only called if the music finishes naturally. @@ -219,9 +216,9 @@ extern DECLSPEC void * SDLCALL Mix_GetMusicHookData(void); /* * Add your own callback when a channel has finished playing. NULL - * to disable callback. The callback may be called from the mixer's audio + * to disable callback. The callback may be called from the mixer's audio * callback or it could be called as a result of Mix_HaltChannel(), etc. - * do not call SDL_LockAudio() from this callback; you will either be + * do not call SDL_LockAudio() from this callback; you will either be * inside the audio callback, or SDL_mixer will explicitly lock the audio * before calling your callback. */ @@ -302,15 +299,14 @@ typedef void (*Mix_EffectDone_t)(int chan, void *udata); * * After all these effects have finished processing, the callback registered * through Mix_SetPostMix() runs, and then the stream goes to the audio - * device. + * device. * * DO NOT EVER call SDL_LockAudio() from your callback function! * * returns zero if error (no such channel), nonzero if added. * Error messages can be retrieved from Mix_GetError(). */ -extern DECLSPEC int SDLCALL Mix_RegisterEffect(int chan, Mix_EffectFunc_t f, - Mix_EffectDone_t d, void *arg); +extern DECLSPEC int SDLCALL Mix_RegisterEffect(int chan, Mix_EffectFunc_t f, Mix_EffectDone_t d, void *arg); /* You may not need to call this explicitly, unless you need to stop an @@ -590,7 +586,7 @@ extern DECLSPEC int SDLCALL Mix_PausedMusic(void); /* Set the current position in the music stream. This returns 0 if successful, or -1 if it failed or isn't implemented. This function is only implemented for MOD music formats (set pattern - order number) and for OGG, FLAC, MP3_MAD, and MODPLUG music (set + order number) and for OGG, FLAC, MP3_MAD, and MODPLUG music (set position in seconds), at the moment. */ extern DECLSPEC int SDLCALL Mix_SetMusicPosition(double position); @@ -622,8 +618,8 @@ extern DECLSPEC Mix_Chunk * SDLCALL Mix_GetChunk(int channel); extern DECLSPEC void SDLCALL Mix_CloseAudio(void); /* We'll use SDL for reporting errors */ -#define Mix_SetError SDL_SetError -#define Mix_GetError SDL_GetError +#define Mix_SetError SDL_SetError +#define Mix_GetError SDL_GetError /* Ends C function definitions when using C++ */ #ifdef __cplusplus diff --git a/VisualC/resource.h b/VisualC/resource.h index ddc9d929..7c822cff 100755 --- a/VisualC/resource.h +++ b/VisualC/resource.h @@ -1,15 +1,15 @@ -//{{NO_DEPENDENCIES}} -// Microsoft Developer Studio generated include file. -// Used by Version.rc -// - -// Next default values for new objects -// -#ifdef APSTUDIO_INVOKED -#ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 101 -#define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1000 -#define _APS_NEXT_SYMED_VALUE 101 -#endif -#endif +//{{NO_DEPENDENCIES}} +// Microsoft Developer Studio generated include file. +// Used by Version.rc +// + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1000 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git a/Xcode/playmus/SDLMain.m b/Xcode/playmus/SDLMain.m index be9e3523..6a2247b1 100644 --- a/Xcode/playmus/SDLMain.m +++ b/Xcode/playmus/SDLMain.m @@ -18,21 +18,21 @@ - (void)setAppleMenu:(NSMenu *)menu; @end /* Use this flag to determine whether we use SDLMain.nib or not */ -#define SDL_USE_NIB_FILE 0 +#define SDL_USE_NIB_FILE 0 /* Use this flag to determine whether we use CPS (docking) or not */ -#define SDL_USE_CPS 1 +#define SDL_USE_CPS 1 #ifdef SDL_USE_CPS /* Portions of CPS.h */ typedef struct CPSProcessSerNum { - UInt32 lo; - UInt32 hi; + UInt32 lo; + UInt32 hi; } CPSProcessSerNum; -extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn); -extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5); -extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn); +extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn); +extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5); +extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn); #endif /* SDL_USE_CPS */ @@ -50,7 +50,7 @@ - (void)setAppleMenu:(NSMenu *)menu; dict = (const NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle()); if (dict) appName = [dict objectForKey: @"CFBundleName"]; - + if (![appName length]) appName = [[NSProcessInfo processInfo] processName]; @@ -131,10 +131,10 @@ static void setApplicationMenu(void) NSMenuItem *menuItem; NSString *title; NSString *appName; - + appName = getApplicationName(); appleMenu = [[NSMenu alloc] initWithTitle:@""]; - + /* Add menu items */ title = [@"About " stringByAppendingString:appName]; [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; @@ -154,7 +154,7 @@ static void setApplicationMenu(void) title = [@"Quit " stringByAppendingString:appName]; [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"]; - + /* Put menu into the menubar */ menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""]; [menuItem setSubmenu:appleMenu]; @@ -176,17 +176,17 @@ static void setupWindowMenu(void) NSMenuItem *menuItem; windowMenu = [[NSMenu alloc] initWithTitle:@"Window"]; - + /* "Minimize" item */ menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"]; [windowMenu addItem:menuItem]; [menuItem release]; - + /* Put menu into the menubar */ windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""]; [windowMenuItem setSubmenu:windowMenu]; [[NSApp mainMenu] addItem:windowMenuItem]; - + /* Tell the application object that this is now the window menu */ [NSApp setWindowsMenu:windowMenu]; @@ -198,12 +198,12 @@ static void setupWindowMenu(void) /* Replacement for NSApplicationMain */ static void CustomApplicationMain (int argc, char **argv) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - SDLMain *sdlMain; + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + SDLMain *sdlMain; /* Ensure the application object is initialised */ [SDLApplication sharedApplication]; - + #ifdef SDL_USE_CPS { CPSProcessSerNum PSN; @@ -223,10 +223,10 @@ static void CustomApplicationMain (int argc, char **argv) /* Create SDLMain and make it the app delegate */ sdlMain = [[SDLMain alloc] init]; [NSApp setDelegate:sdlMain]; - + /* Start the main event loop */ [NSApp run]; - + [sdlMain release]; [pool release]; } @@ -319,27 +319,27 @@ - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString bufferSize = selfLen + aStringLen - aRange.length; buffer = (unichar *)NSAllocateMemoryPages(bufferSize*sizeof(unichar)); - + /* Get first part into buffer */ localRange.location = 0; localRange.length = aRange.location; [self getCharacters:buffer range:localRange]; - + /* Get middle part into buffer */ localRange.location = 0; localRange.length = aStringLen; [aString getCharacters:(buffer+aRange.location) range:localRange]; - + /* Get last part into buffer */ localRange.location = aRange.location + aRange.length; localRange.length = selfLen - localRange.location; [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange]; - + /* Build output string */ result = [NSString stringWithCharacters:buffer length:bufferSize]; - + NSDeallocateMemoryPages(buffer, bufferSize); - + return result; } diff --git a/Xcode/playwave/SDLMain.m b/Xcode/playwave/SDLMain.m index be9e3523..6a2247b1 100644 --- a/Xcode/playwave/SDLMain.m +++ b/Xcode/playwave/SDLMain.m @@ -18,21 +18,21 @@ - (void)setAppleMenu:(NSMenu *)menu; @end /* Use this flag to determine whether we use SDLMain.nib or not */ -#define SDL_USE_NIB_FILE 0 +#define SDL_USE_NIB_FILE 0 /* Use this flag to determine whether we use CPS (docking) or not */ -#define SDL_USE_CPS 1 +#define SDL_USE_CPS 1 #ifdef SDL_USE_CPS /* Portions of CPS.h */ typedef struct CPSProcessSerNum { - UInt32 lo; - UInt32 hi; + UInt32 lo; + UInt32 hi; } CPSProcessSerNum; -extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn); -extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5); -extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn); +extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn); +extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5); +extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn); #endif /* SDL_USE_CPS */ @@ -50,7 +50,7 @@ - (void)setAppleMenu:(NSMenu *)menu; dict = (const NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle()); if (dict) appName = [dict objectForKey: @"CFBundleName"]; - + if (![appName length]) appName = [[NSProcessInfo processInfo] processName]; @@ -131,10 +131,10 @@ static void setApplicationMenu(void) NSMenuItem *menuItem; NSString *title; NSString *appName; - + appName = getApplicationName(); appleMenu = [[NSMenu alloc] initWithTitle:@""]; - + /* Add menu items */ title = [@"About " stringByAppendingString:appName]; [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; @@ -154,7 +154,7 @@ static void setApplicationMenu(void) title = [@"Quit " stringByAppendingString:appName]; [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"]; - + /* Put menu into the menubar */ menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""]; [menuItem setSubmenu:appleMenu]; @@ -176,17 +176,17 @@ static void setupWindowMenu(void) NSMenuItem *menuItem; windowMenu = [[NSMenu alloc] initWithTitle:@"Window"]; - + /* "Minimize" item */ menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"]; [windowMenu addItem:menuItem]; [menuItem release]; - + /* Put menu into the menubar */ windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""]; [windowMenuItem setSubmenu:windowMenu]; [[NSApp mainMenu] addItem:windowMenuItem]; - + /* Tell the application object that this is now the window menu */ [NSApp setWindowsMenu:windowMenu]; @@ -198,12 +198,12 @@ static void setupWindowMenu(void) /* Replacement for NSApplicationMain */ static void CustomApplicationMain (int argc, char **argv) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - SDLMain *sdlMain; + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + SDLMain *sdlMain; /* Ensure the application object is initialised */ [SDLApplication sharedApplication]; - + #ifdef SDL_USE_CPS { CPSProcessSerNum PSN; @@ -223,10 +223,10 @@ static void CustomApplicationMain (int argc, char **argv) /* Create SDLMain and make it the app delegate */ sdlMain = [[SDLMain alloc] init]; [NSApp setDelegate:sdlMain]; - + /* Start the main event loop */ [NSApp run]; - + [sdlMain release]; [pool release]; } @@ -319,27 +319,27 @@ - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString bufferSize = selfLen + aStringLen - aRange.length; buffer = (unichar *)NSAllocateMemoryPages(bufferSize*sizeof(unichar)); - + /* Get first part into buffer */ localRange.location = 0; localRange.length = aRange.location; [self getCharacters:buffer range:localRange]; - + /* Get middle part into buffer */ localRange.location = 0; localRange.length = aStringLen; [aString getCharacters:(buffer+aRange.location) range:localRange]; - + /* Get last part into buffer */ localRange.location = aRange.location + aRange.length; localRange.length = selfLen - localRange.location; [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange]; - + /* Build output string */ result = [NSString stringWithCharacters:buffer length:bufferSize]; - + NSDeallocateMemoryPages(buffer, bufferSize); - + return result; } diff --git a/dynamic_flac.c b/dynamic_flac.c index 2ebdeaf8..50f0d7c5 100644 --- a/dynamic_flac.c +++ b/dynamic_flac.c @@ -19,7 +19,7 @@ 3. This notice may not be removed or altered from any source distribution. Implementation of the dynamic loading functionality for libFLAC. - ~ Austen Dicken (admin@cvpcs.org) + ~ Austen Dicken (admin@cvpcs.org) */ #ifdef FLAC_MUSIC @@ -29,148 +29,148 @@ #include "dynamic_flac.h" flac_loader flac = { - 0, NULL + 0, NULL }; #ifdef FLAC_DYNAMIC int Mix_InitFLAC() { - if ( flac.loaded == 0 ) { - flac.handle = SDL_LoadObject(FLAC_DYNAMIC); - if ( flac.handle == NULL ) { - return -1; - } - flac.FLAC__stream_decoder_new = - (FLAC__StreamDecoder *(*)()) - SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_new"); - if ( flac.FLAC__stream_decoder_new == NULL ) { - SDL_UnloadObject(flac.handle); - return -1; - } - flac.FLAC__stream_decoder_delete = - (void (*)(FLAC__StreamDecoder *)) - SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_delete"); - if ( flac.FLAC__stream_decoder_delete == NULL ) { - SDL_UnloadObject(flac.handle); - return -1; - } - flac.FLAC__stream_decoder_init_stream = - (FLAC__StreamDecoderInitStatus (*)( - FLAC__StreamDecoder *, - FLAC__StreamDecoderReadCallback, - FLAC__StreamDecoderSeekCallback, - FLAC__StreamDecoderTellCallback, - FLAC__StreamDecoderLengthCallback, - FLAC__StreamDecoderEofCallback, - FLAC__StreamDecoderWriteCallback, - FLAC__StreamDecoderMetadataCallback, - FLAC__StreamDecoderErrorCallback, - void *)) - SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_init_stream"); - if ( flac.FLAC__stream_decoder_init_stream == NULL ) { - SDL_UnloadObject(flac.handle); - return -1; - } - flac.FLAC__stream_decoder_finish = - (FLAC__bool (*)(FLAC__StreamDecoder *)) - SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_finish"); - if ( flac.FLAC__stream_decoder_finish == NULL ) { - SDL_UnloadObject(flac.handle); - return -1; - } - flac.FLAC__stream_decoder_flush = - (FLAC__bool (*)(FLAC__StreamDecoder *)) - SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_flush"); - if ( flac.FLAC__stream_decoder_flush == NULL ) { - SDL_UnloadObject(flac.handle); - return -1; - } - flac.FLAC__stream_decoder_process_single = - (FLAC__bool (*)(FLAC__StreamDecoder *)) - SDL_LoadFunction(flac.handle, - "FLAC__stream_decoder_process_single"); - if ( flac.FLAC__stream_decoder_process_single == NULL ) { - SDL_UnloadObject(flac.handle); - return -1; - } - flac.FLAC__stream_decoder_process_until_end_of_metadata = - (FLAC__bool (*)(FLAC__StreamDecoder *)) - SDL_LoadFunction(flac.handle, - "FLAC__stream_decoder_process_until_end_of_metadata"); - if ( flac.FLAC__stream_decoder_process_until_end_of_metadata == NULL ) { - SDL_UnloadObject(flac.handle); - return -1; - } - flac.FLAC__stream_decoder_process_until_end_of_stream = - (FLAC__bool (*)(FLAC__StreamDecoder *)) - SDL_LoadFunction(flac.handle, - "FLAC__stream_decoder_process_until_end_of_stream"); - if ( flac.FLAC__stream_decoder_process_until_end_of_stream == NULL ) { - SDL_UnloadObject(flac.handle); - return -1; - } - flac.FLAC__stream_decoder_seek_absolute = - (FLAC__bool (*)(FLAC__StreamDecoder *, FLAC__uint64)) - SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_seek_absolute"); - if ( flac.FLAC__stream_decoder_seek_absolute == NULL ) { - SDL_UnloadObject(flac.handle); - return -1; - } - flac.FLAC__stream_decoder_get_state = - (FLAC__StreamDecoderState (*)(const FLAC__StreamDecoder *decoder)) - SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_get_state"); - if ( flac.FLAC__stream_decoder_get_state == NULL ) { - SDL_UnloadObject(flac.handle); - return -1; - } - } - ++flac.loaded; + if ( flac.loaded == 0 ) { + flac.handle = SDL_LoadObject(FLAC_DYNAMIC); + if ( flac.handle == NULL ) { + return -1; + } + flac.FLAC__stream_decoder_new = + (FLAC__StreamDecoder *(*)()) + SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_new"); + if ( flac.FLAC__stream_decoder_new == NULL ) { + SDL_UnloadObject(flac.handle); + return -1; + } + flac.FLAC__stream_decoder_delete = + (void (*)(FLAC__StreamDecoder *)) + SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_delete"); + if ( flac.FLAC__stream_decoder_delete == NULL ) { + SDL_UnloadObject(flac.handle); + return -1; + } + flac.FLAC__stream_decoder_init_stream = + (FLAC__StreamDecoderInitStatus (*)( + FLAC__StreamDecoder *, + FLAC__StreamDecoderReadCallback, + FLAC__StreamDecoderSeekCallback, + FLAC__StreamDecoderTellCallback, + FLAC__StreamDecoderLengthCallback, + FLAC__StreamDecoderEofCallback, + FLAC__StreamDecoderWriteCallback, + FLAC__StreamDecoderMetadataCallback, + FLAC__StreamDecoderErrorCallback, + void *)) + SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_init_stream"); + if ( flac.FLAC__stream_decoder_init_stream == NULL ) { + SDL_UnloadObject(flac.handle); + return -1; + } + flac.FLAC__stream_decoder_finish = + (FLAC__bool (*)(FLAC__StreamDecoder *)) + SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_finish"); + if ( flac.FLAC__stream_decoder_finish == NULL ) { + SDL_UnloadObject(flac.handle); + return -1; + } + flac.FLAC__stream_decoder_flush = + (FLAC__bool (*)(FLAC__StreamDecoder *)) + SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_flush"); + if ( flac.FLAC__stream_decoder_flush == NULL ) { + SDL_UnloadObject(flac.handle); + return -1; + } + flac.FLAC__stream_decoder_process_single = + (FLAC__bool (*)(FLAC__StreamDecoder *)) + SDL_LoadFunction(flac.handle, + "FLAC__stream_decoder_process_single"); + if ( flac.FLAC__stream_decoder_process_single == NULL ) { + SDL_UnloadObject(flac.handle); + return -1; + } + flac.FLAC__stream_decoder_process_until_end_of_metadata = + (FLAC__bool (*)(FLAC__StreamDecoder *)) + SDL_LoadFunction(flac.handle, + "FLAC__stream_decoder_process_until_end_of_metadata"); + if ( flac.FLAC__stream_decoder_process_until_end_of_metadata == NULL ) { + SDL_UnloadObject(flac.handle); + return -1; + } + flac.FLAC__stream_decoder_process_until_end_of_stream = + (FLAC__bool (*)(FLAC__StreamDecoder *)) + SDL_LoadFunction(flac.handle, + "FLAC__stream_decoder_process_until_end_of_stream"); + if ( flac.FLAC__stream_decoder_process_until_end_of_stream == NULL ) { + SDL_UnloadObject(flac.handle); + return -1; + } + flac.FLAC__stream_decoder_seek_absolute = + (FLAC__bool (*)(FLAC__StreamDecoder *, FLAC__uint64)) + SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_seek_absolute"); + if ( flac.FLAC__stream_decoder_seek_absolute == NULL ) { + SDL_UnloadObject(flac.handle); + return -1; + } + flac.FLAC__stream_decoder_get_state = + (FLAC__StreamDecoderState (*)(const FLAC__StreamDecoder *decoder)) + SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_get_state"); + if ( flac.FLAC__stream_decoder_get_state == NULL ) { + SDL_UnloadObject(flac.handle); + return -1; + } + } + ++flac.loaded; - return 0; + return 0; } void Mix_QuitFLAC() { - if ( flac.loaded == 0 ) { - return; - } - if ( flac.loaded == 1 ) { - SDL_UnloadObject(flac.handle); - } - --flac.loaded; + if ( flac.loaded == 0 ) { + return; + } + if ( flac.loaded == 1 ) { + SDL_UnloadObject(flac.handle); + } + --flac.loaded; } #else int Mix_InitFLAC() { - if ( flac.loaded == 0 ) { - flac.FLAC__stream_decoder_new = FLAC__stream_decoder_new; - flac.FLAC__stream_decoder_delete = FLAC__stream_decoder_delete; - flac.FLAC__stream_decoder_init_stream = - FLAC__stream_decoder_init_stream; - flac.FLAC__stream_decoder_finish = FLAC__stream_decoder_finish; - flac.FLAC__stream_decoder_flush = FLAC__stream_decoder_flush; - flac.FLAC__stream_decoder_process_single = - FLAC__stream_decoder_process_single; - flac.FLAC__stream_decoder_process_until_end_of_metadata = - FLAC__stream_decoder_process_until_end_of_metadata; - flac.FLAC__stream_decoder_process_until_end_of_stream = - FLAC__stream_decoder_process_until_end_of_stream; - flac.FLAC__stream_decoder_seek_absolute = - FLAC__stream_decoder_seek_absolute; - flac.FLAC__stream_decoder_get_state = - FLAC__stream_decoder_get_state; - } - ++flac.loaded; + if ( flac.loaded == 0 ) { + flac.FLAC__stream_decoder_new = FLAC__stream_decoder_new; + flac.FLAC__stream_decoder_delete = FLAC__stream_decoder_delete; + flac.FLAC__stream_decoder_init_stream = + FLAC__stream_decoder_init_stream; + flac.FLAC__stream_decoder_finish = FLAC__stream_decoder_finish; + flac.FLAC__stream_decoder_flush = FLAC__stream_decoder_flush; + flac.FLAC__stream_decoder_process_single = + FLAC__stream_decoder_process_single; + flac.FLAC__stream_decoder_process_until_end_of_metadata = + FLAC__stream_decoder_process_until_end_of_metadata; + flac.FLAC__stream_decoder_process_until_end_of_stream = + FLAC__stream_decoder_process_until_end_of_stream; + flac.FLAC__stream_decoder_seek_absolute = + FLAC__stream_decoder_seek_absolute; + flac.FLAC__stream_decoder_get_state = + FLAC__stream_decoder_get_state; + } + ++flac.loaded; - return 0; + return 0; } void Mix_QuitFLAC() { - if ( flac.loaded == 0 ) { - return; - } - if ( flac.loaded == 1 ) { - } - --flac.loaded; + if ( flac.loaded == 0 ) { + return; + } + if ( flac.loaded == 1 ) { + } + --flac.loaded; } #endif /* FLAC_DYNAMIC */ diff --git a/dynamic_flac.h b/dynamic_flac.h index ab7bf4c7..10a40463 100644 --- a/dynamic_flac.h +++ b/dynamic_flac.h @@ -20,7 +20,7 @@ The following file defines all of the functions/objects used to dynamically link to the libFLAC library. - ~ Austen Dicken (admin@cvpcs.org) + ~ Austen Dicken (admin@cvpcs.org) */ #ifdef FLAC_MUSIC @@ -28,34 +28,34 @@ #include typedef struct { - int loaded; - void *handle; - FLAC__StreamDecoder *(*FLAC__stream_decoder_new)(); - void (*FLAC__stream_decoder_delete)(FLAC__StreamDecoder *decoder); - FLAC__StreamDecoderInitStatus (*FLAC__stream_decoder_init_stream)( - FLAC__StreamDecoder *decoder, - FLAC__StreamDecoderReadCallback read_callback, - FLAC__StreamDecoderSeekCallback seek_callback, - FLAC__StreamDecoderTellCallback tell_callback, - FLAC__StreamDecoderLengthCallback length_callback, - FLAC__StreamDecoderEofCallback eof_callback, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data); - FLAC__bool (*FLAC__stream_decoder_finish)(FLAC__StreamDecoder *decoder); - FLAC__bool (*FLAC__stream_decoder_flush)(FLAC__StreamDecoder *decoder); - FLAC__bool (*FLAC__stream_decoder_process_single)( - FLAC__StreamDecoder *decoder); - FLAC__bool (*FLAC__stream_decoder_process_until_end_of_metadata)( - FLAC__StreamDecoder *decoder); - FLAC__bool (*FLAC__stream_decoder_process_until_end_of_stream)( - FLAC__StreamDecoder *decoder); - FLAC__bool (*FLAC__stream_decoder_seek_absolute)( - FLAC__StreamDecoder *decoder, - FLAC__uint64 sample); - FLAC__StreamDecoderState (*FLAC__stream_decoder_get_state)( - const FLAC__StreamDecoder *decoder); + int loaded; + void *handle; + FLAC__StreamDecoder *(*FLAC__stream_decoder_new)(); + void (*FLAC__stream_decoder_delete)(FLAC__StreamDecoder *decoder); + FLAC__StreamDecoderInitStatus (*FLAC__stream_decoder_init_stream)( + FLAC__StreamDecoder *decoder, + FLAC__StreamDecoderReadCallback read_callback, + FLAC__StreamDecoderSeekCallback seek_callback, + FLAC__StreamDecoderTellCallback tell_callback, + FLAC__StreamDecoderLengthCallback length_callback, + FLAC__StreamDecoderEofCallback eof_callback, + FLAC__StreamDecoderWriteCallback write_callback, + FLAC__StreamDecoderMetadataCallback metadata_callback, + FLAC__StreamDecoderErrorCallback error_callback, + void *client_data); + FLAC__bool (*FLAC__stream_decoder_finish)(FLAC__StreamDecoder *decoder); + FLAC__bool (*FLAC__stream_decoder_flush)(FLAC__StreamDecoder *decoder); + FLAC__bool (*FLAC__stream_decoder_process_single)( + FLAC__StreamDecoder *decoder); + FLAC__bool (*FLAC__stream_decoder_process_until_end_of_metadata)( + FLAC__StreamDecoder *decoder); + FLAC__bool (*FLAC__stream_decoder_process_until_end_of_stream)( + FLAC__StreamDecoder *decoder); + FLAC__bool (*FLAC__stream_decoder_seek_absolute)( + FLAC__StreamDecoder *decoder, + FLAC__uint64 sample); + FLAC__StreamDecoderState (*FLAC__stream_decoder_get_state)( + const FLAC__StreamDecoder *decoder); } flac_loader; extern flac_loader flac; diff --git a/dynamic_fluidsynth.c b/dynamic_fluidsynth.c index 613ccb1f..fe0f8418 100644 --- a/dynamic_fluidsynth.c +++ b/dynamic_fluidsynth.c @@ -28,60 +28,60 @@ #include "dynamic_fluidsynth.h" fluidsynth_loader fluidsynth = { - 0, NULL + 0, NULL }; #ifdef FLUIDSYNTH_DYNAMIC #define FLUIDSYNTH_LOADER(FUNC, SIG) \ - fluidsynth.FUNC = (SIG) SDL_LoadFunction(fluidsynth.handle, #FUNC); \ - if (fluidsynth.FUNC == NULL) { SDL_UnloadObject(fluidsynth.handle); return -1; } + fluidsynth.FUNC = (SIG) SDL_LoadFunction(fluidsynth.handle, #FUNC); \ + if (fluidsynth.FUNC == NULL) { SDL_UnloadObject(fluidsynth.handle); return -1; } #else #define FLUIDSYNTH_LOADER(FUNC, SIG) \ - fluidsynth.FUNC = FUNC; + fluidsynth.FUNC = FUNC; #endif int Mix_InitFluidSynth() { - if ( fluidsynth.loaded == 0 ) { + if ( fluidsynth.loaded == 0 ) { #ifdef FLUIDSYNTH_DYNAMIC - fluidsynth.handle = SDL_LoadObject(FLUIDSYNTH_DYNAMIC); - if ( fluidsynth.handle == NULL ) return -1; + fluidsynth.handle = SDL_LoadObject(FLUIDSYNTH_DYNAMIC); + if ( fluidsynth.handle == NULL ) return -1; #endif - FLUIDSYNTH_LOADER(delete_fluid_player, int (*)(fluid_player_t*)); - FLUIDSYNTH_LOADER(delete_fluid_settings, void (*)(fluid_settings_t*)); - FLUIDSYNTH_LOADER(delete_fluid_synth, int (*)(fluid_synth_t*)); - FLUIDSYNTH_LOADER(fluid_player_add, int (*)(fluid_player_t*, const char*)); - FLUIDSYNTH_LOADER(fluid_player_add_mem, int (*)(fluid_player_t*, const void*, size_t)); - FLUIDSYNTH_LOADER(fluid_player_get_status, int (*)(fluid_player_t*)); - FLUIDSYNTH_LOADER(fluid_player_play, int (*)(fluid_player_t*)); - FLUIDSYNTH_LOADER(fluid_player_set_loop, int (*)(fluid_player_t*, int)); - FLUIDSYNTH_LOADER(fluid_player_stop, int (*)(fluid_player_t*)); - FLUIDSYNTH_LOADER(fluid_settings_setnum, int (*)(fluid_settings_t*, const char*, double)); - FLUIDSYNTH_LOADER(fluid_synth_get_settings, fluid_settings_t* (*)(fluid_synth_t*)); - FLUIDSYNTH_LOADER(fluid_synth_set_gain, void (*)(fluid_synth_t*, float)); - FLUIDSYNTH_LOADER(fluid_synth_sfload, int(*)(fluid_synth_t*, const char*, int)); - FLUIDSYNTH_LOADER(fluid_synth_write_s16, int(*)(fluid_synth_t*, int, void*, int, int, void*, int, int)); - FLUIDSYNTH_LOADER(new_fluid_player, fluid_player_t* (*)(fluid_synth_t*)); - FLUIDSYNTH_LOADER(new_fluid_settings, fluid_settings_t* (*)(void)); - FLUIDSYNTH_LOADER(new_fluid_synth, fluid_synth_t* (*)(fluid_settings_t*)); - } - ++fluidsynth.loaded; + FLUIDSYNTH_LOADER(delete_fluid_player, int (*)(fluid_player_t*)); + FLUIDSYNTH_LOADER(delete_fluid_settings, void (*)(fluid_settings_t*)); + FLUIDSYNTH_LOADER(delete_fluid_synth, int (*)(fluid_synth_t*)); + FLUIDSYNTH_LOADER(fluid_player_add, int (*)(fluid_player_t*, const char*)); + FLUIDSYNTH_LOADER(fluid_player_add_mem, int (*)(fluid_player_t*, const void*, size_t)); + FLUIDSYNTH_LOADER(fluid_player_get_status, int (*)(fluid_player_t*)); + FLUIDSYNTH_LOADER(fluid_player_play, int (*)(fluid_player_t*)); + FLUIDSYNTH_LOADER(fluid_player_set_loop, int (*)(fluid_player_t*, int)); + FLUIDSYNTH_LOADER(fluid_player_stop, int (*)(fluid_player_t*)); + FLUIDSYNTH_LOADER(fluid_settings_setnum, int (*)(fluid_settings_t*, const char*, double)); + FLUIDSYNTH_LOADER(fluid_synth_get_settings, fluid_settings_t* (*)(fluid_synth_t*)); + FLUIDSYNTH_LOADER(fluid_synth_set_gain, void (*)(fluid_synth_t*, float)); + FLUIDSYNTH_LOADER(fluid_synth_sfload, int(*)(fluid_synth_t*, const char*, int)); + FLUIDSYNTH_LOADER(fluid_synth_write_s16, int(*)(fluid_synth_t*, int, void*, int, int, void*, int, int)); + FLUIDSYNTH_LOADER(new_fluid_player, fluid_player_t* (*)(fluid_synth_t*)); + FLUIDSYNTH_LOADER(new_fluid_settings, fluid_settings_t* (*)(void)); + FLUIDSYNTH_LOADER(new_fluid_synth, fluid_synth_t* (*)(fluid_settings_t*)); + } + ++fluidsynth.loaded; - return 0; + return 0; } void Mix_QuitFluidSynth() { - if ( fluidsynth.loaded == 0 ) { - return; - } - if ( fluidsynth.loaded == 1 ) { + if ( fluidsynth.loaded == 0 ) { + return; + } + if ( fluidsynth.loaded == 1 ) { #ifdef FLUIDSYNTH_DYNAMIC - SDL_UnloadObject(fluidsynth.handle); + SDL_UnloadObject(fluidsynth.handle); #endif - } - --fluidsynth.loaded; + } + --fluidsynth.loaded; } #endif /* USE_FLUIDSYNTH_MIDI */ diff --git a/dynamic_fluidsynth.h b/dynamic_fluidsynth.h index 36f4a030..455b4193 100644 --- a/dynamic_fluidsynth.h +++ b/dynamic_fluidsynth.h @@ -27,26 +27,26 @@ #include typedef struct { - int loaded; - void *handle; - - int (*delete_fluid_player)(fluid_player_t*); - void (*delete_fluid_settings)(fluid_settings_t*); - int (*delete_fluid_synth)(fluid_synth_t*); - int (*fluid_player_add)(fluid_player_t*, const char*); - int (*fluid_player_add_mem)(fluid_player_t*, const void*, size_t); - int (*fluid_player_get_status)(fluid_player_t*); - int (*fluid_player_play)(fluid_player_t*); - int (*fluid_player_set_loop)(fluid_player_t*, int); - int (*fluid_player_stop)(fluid_player_t*); - int (*fluid_settings_setnum)(fluid_settings_t*, const char*, double); - fluid_settings_t* (*fluid_synth_get_settings)(fluid_synth_t*); - void (*fluid_synth_set_gain)(fluid_synth_t*, float); - int (*fluid_synth_sfload)(fluid_synth_t*, const char*, int); - int (*fluid_synth_write_s16)(fluid_synth_t*, int, void*, int, int, void*, int, int); - fluid_player_t* (*new_fluid_player)(fluid_synth_t*); - fluid_settings_t* (*new_fluid_settings)(void); - fluid_synth_t* (*new_fluid_synth)(fluid_settings_t*); + int loaded; + void *handle; + + int (*delete_fluid_player)(fluid_player_t*); + void (*delete_fluid_settings)(fluid_settings_t*); + int (*delete_fluid_synth)(fluid_synth_t*); + int (*fluid_player_add)(fluid_player_t*, const char*); + int (*fluid_player_add_mem)(fluid_player_t*, const void*, size_t); + int (*fluid_player_get_status)(fluid_player_t*); + int (*fluid_player_play)(fluid_player_t*); + int (*fluid_player_set_loop)(fluid_player_t*, int); + int (*fluid_player_stop)(fluid_player_t*); + int (*fluid_settings_setnum)(fluid_settings_t*, const char*, double); + fluid_settings_t* (*fluid_synth_get_settings)(fluid_synth_t*); + void (*fluid_synth_set_gain)(fluid_synth_t*, float); + int (*fluid_synth_sfload)(fluid_synth_t*, const char*, int); + int (*fluid_synth_write_s16)(fluid_synth_t*, int, void*, int, int, void*, int, int); + fluid_player_t* (*new_fluid_player)(fluid_synth_t*); + fluid_settings_t* (*new_fluid_settings)(void); + fluid_synth_t* (*new_fluid_synth)(fluid_settings_t*); } fluidsynth_loader; extern fluidsynth_loader fluidsynth; diff --git a/dynamic_mod.c b/dynamic_mod.c index 52bca3fc..0862f007 100644 --- a/dynamic_mod.c +++ b/dynamic_mod.c @@ -26,261 +26,261 @@ #include "dynamic_mod.h" mikmod_loader mikmod = { - 0, NULL + 0, NULL }; #ifdef MOD_DYNAMIC int Mix_InitMOD() { - if ( mikmod.loaded == 0 ) { - mikmod.handle = SDL_LoadObject(MOD_DYNAMIC); - if ( mikmod.handle == NULL ) { - return -1; - } - mikmod.MikMod_Exit = - (void (*)(void)) - SDL_LoadFunction(mikmod.handle, "MikMod_Exit"); - if ( mikmod.MikMod_Exit == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.MikMod_InfoDriver = - (CHAR* (*)(void)) - SDL_LoadFunction(mikmod.handle, "MikMod_InfoDriver"); - if ( mikmod.MikMod_InfoDriver == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.MikMod_InfoLoader = - (CHAR* (*)(void)) - SDL_LoadFunction(mikmod.handle, "MikMod_InfoLoader"); - if ( mikmod.MikMod_InfoLoader == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.MikMod_Init = - (BOOL (*)(CHAR*)) - SDL_LoadFunction(mikmod.handle, "MikMod_Init"); - if ( mikmod.MikMod_Init == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.MikMod_RegisterAllLoaders = - (void (*)(void)) - SDL_LoadFunction(mikmod.handle, "MikMod_RegisterAllLoaders"); - if ( mikmod.MikMod_RegisterAllLoaders == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.MikMod_RegisterDriver = - (void (*)(struct MDRIVER*)) - SDL_LoadFunction(mikmod.handle, "MikMod_RegisterDriver"); - if ( mikmod.MikMod_RegisterDriver == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.MikMod_errno = - (int*) - SDL_LoadFunction(mikmod.handle, "MikMod_errno"); - if ( mikmod.MikMod_errno == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.MikMod_strerror = - (char* (*)(int)) - SDL_LoadFunction(mikmod.handle, "MikMod_strerror"); - if ( mikmod.MikMod_strerror == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.MikMod_free = - (void (*)(void*)) - SDL_LoadFunction(mikmod.handle, "MikMod_free"); - if ( mikmod.MikMod_free == NULL ) { - /* libmikmod 3.1 and earlier doesn't have it */ - mikmod.MikMod_free = free; - } - mikmod.Player_Active = - (BOOL (*)(void)) - SDL_LoadFunction(mikmod.handle, "Player_Active"); - if ( mikmod.Player_Active == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.Player_Free = - (void (*)(MODULE*)) - SDL_LoadFunction(mikmod.handle, "Player_Free"); - if ( mikmod.Player_Free == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.Player_LoadGeneric = - (MODULE* (*)(MREADER*,int,BOOL)) - SDL_LoadFunction(mikmod.handle, "Player_LoadGeneric"); - if ( mikmod.Player_LoadGeneric == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.Player_SetPosition = - (void (*)(UWORD)) - SDL_LoadFunction(mikmod.handle, "Player_SetPosition"); - if ( mikmod.Player_SetPosition == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.Player_SetVolume = - (void (*)(SWORD)) - SDL_LoadFunction(mikmod.handle, "Player_SetVolume"); - if ( mikmod.Player_SetVolume == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.Player_Start = - (void (*)(MODULE*)) - SDL_LoadFunction(mikmod.handle, "Player_Start"); - if ( mikmod.Player_Start == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.Player_Stop = - (void (*)(void)) - SDL_LoadFunction(mikmod.handle, "Player_Stop"); - if ( mikmod.Player_Stop == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.VC_WriteBytes = - (ULONG (*)(SBYTE*,ULONG)) - SDL_LoadFunction(mikmod.handle, "VC_WriteBytes"); - if ( mikmod.VC_WriteBytes == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.drv_nos = - (MDRIVER*) - SDL_LoadFunction(mikmod.handle, "drv_nos"); - if ( mikmod.drv_nos == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.md_device = - (UWORD*) - SDL_LoadFunction(mikmod.handle, "md_device"); - if ( mikmod.md_device == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.md_mixfreq = - (UWORD*) - SDL_LoadFunction(mikmod.handle, "md_mixfreq"); - if ( mikmod.md_mixfreq == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.md_mode = - (UWORD*) - SDL_LoadFunction(mikmod.handle, "md_mode"); - if ( mikmod.md_mode == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.md_musicvolume = - (UBYTE*) - SDL_LoadFunction(mikmod.handle, "md_musicvolume"); - if ( mikmod.md_musicvolume == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.md_pansep = - (UBYTE*) - SDL_LoadFunction(mikmod.handle, "md_pansep"); - if ( mikmod.md_pansep == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.md_reverb = - (UBYTE*) - SDL_LoadFunction(mikmod.handle, "md_reverb"); - if ( mikmod.md_reverb == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.md_sndfxvolume = - (UBYTE*) - SDL_LoadFunction(mikmod.handle, "md_sndfxvolume"); - if ( mikmod.md_sndfxvolume == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - mikmod.md_volume = - (UBYTE*) - SDL_LoadFunction(mikmod.handle, "md_volume"); - if ( mikmod.md_volume == NULL ) { - SDL_UnloadObject(mikmod.handle); - return -1; - } - } - ++mikmod.loaded; + if ( mikmod.loaded == 0 ) { + mikmod.handle = SDL_LoadObject(MOD_DYNAMIC); + if ( mikmod.handle == NULL ) { + return -1; + } + mikmod.MikMod_Exit = + (void (*)(void)) + SDL_LoadFunction(mikmod.handle, "MikMod_Exit"); + if ( mikmod.MikMod_Exit == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.MikMod_InfoDriver = + (CHAR* (*)(void)) + SDL_LoadFunction(mikmod.handle, "MikMod_InfoDriver"); + if ( mikmod.MikMod_InfoDriver == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.MikMod_InfoLoader = + (CHAR* (*)(void)) + SDL_LoadFunction(mikmod.handle, "MikMod_InfoLoader"); + if ( mikmod.MikMod_InfoLoader == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.MikMod_Init = + (BOOL (*)(CHAR*)) + SDL_LoadFunction(mikmod.handle, "MikMod_Init"); + if ( mikmod.MikMod_Init == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.MikMod_RegisterAllLoaders = + (void (*)(void)) + SDL_LoadFunction(mikmod.handle, "MikMod_RegisterAllLoaders"); + if ( mikmod.MikMod_RegisterAllLoaders == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.MikMod_RegisterDriver = + (void (*)(struct MDRIVER*)) + SDL_LoadFunction(mikmod.handle, "MikMod_RegisterDriver"); + if ( mikmod.MikMod_RegisterDriver == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.MikMod_errno = + (int*) + SDL_LoadFunction(mikmod.handle, "MikMod_errno"); + if ( mikmod.MikMod_errno == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.MikMod_strerror = + (char* (*)(int)) + SDL_LoadFunction(mikmod.handle, "MikMod_strerror"); + if ( mikmod.MikMod_strerror == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.MikMod_free = + (void (*)(void*)) + SDL_LoadFunction(mikmod.handle, "MikMod_free"); + if ( mikmod.MikMod_free == NULL ) { + /* libmikmod 3.1 and earlier doesn't have it */ + mikmod.MikMod_free = free; + } + mikmod.Player_Active = + (BOOL (*)(void)) + SDL_LoadFunction(mikmod.handle, "Player_Active"); + if ( mikmod.Player_Active == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.Player_Free = + (void (*)(MODULE*)) + SDL_LoadFunction(mikmod.handle, "Player_Free"); + if ( mikmod.Player_Free == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.Player_LoadGeneric = + (MODULE* (*)(MREADER*,int,BOOL)) + SDL_LoadFunction(mikmod.handle, "Player_LoadGeneric"); + if ( mikmod.Player_LoadGeneric == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.Player_SetPosition = + (void (*)(UWORD)) + SDL_LoadFunction(mikmod.handle, "Player_SetPosition"); + if ( mikmod.Player_SetPosition == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.Player_SetVolume = + (void (*)(SWORD)) + SDL_LoadFunction(mikmod.handle, "Player_SetVolume"); + if ( mikmod.Player_SetVolume == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.Player_Start = + (void (*)(MODULE*)) + SDL_LoadFunction(mikmod.handle, "Player_Start"); + if ( mikmod.Player_Start == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.Player_Stop = + (void (*)(void)) + SDL_LoadFunction(mikmod.handle, "Player_Stop"); + if ( mikmod.Player_Stop == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.VC_WriteBytes = + (ULONG (*)(SBYTE*,ULONG)) + SDL_LoadFunction(mikmod.handle, "VC_WriteBytes"); + if ( mikmod.VC_WriteBytes == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.drv_nos = + (MDRIVER*) + SDL_LoadFunction(mikmod.handle, "drv_nos"); + if ( mikmod.drv_nos == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.md_device = + (UWORD*) + SDL_LoadFunction(mikmod.handle, "md_device"); + if ( mikmod.md_device == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.md_mixfreq = + (UWORD*) + SDL_LoadFunction(mikmod.handle, "md_mixfreq"); + if ( mikmod.md_mixfreq == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.md_mode = + (UWORD*) + SDL_LoadFunction(mikmod.handle, "md_mode"); + if ( mikmod.md_mode == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.md_musicvolume = + (UBYTE*) + SDL_LoadFunction(mikmod.handle, "md_musicvolume"); + if ( mikmod.md_musicvolume == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.md_pansep = + (UBYTE*) + SDL_LoadFunction(mikmod.handle, "md_pansep"); + if ( mikmod.md_pansep == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.md_reverb = + (UBYTE*) + SDL_LoadFunction(mikmod.handle, "md_reverb"); + if ( mikmod.md_reverb == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.md_sndfxvolume = + (UBYTE*) + SDL_LoadFunction(mikmod.handle, "md_sndfxvolume"); + if ( mikmod.md_sndfxvolume == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + mikmod.md_volume = + (UBYTE*) + SDL_LoadFunction(mikmod.handle, "md_volume"); + if ( mikmod.md_volume == NULL ) { + SDL_UnloadObject(mikmod.handle); + return -1; + } + } + ++mikmod.loaded; - return 0; + return 0; } void Mix_QuitMOD() { - if ( mikmod.loaded == 0 ) { - return; - } - if ( mikmod.loaded == 1 ) { - SDL_UnloadObject(mikmod.handle); - } - --mikmod.loaded; + if ( mikmod.loaded == 0 ) { + return; + } + if ( mikmod.loaded == 1 ) { + SDL_UnloadObject(mikmod.handle); + } + --mikmod.loaded; } #else int Mix_InitMOD() { - if ( mikmod.loaded == 0 ) { - mikmod.MikMod_Exit = MikMod_Exit; - mikmod.MikMod_InfoDriver = MikMod_InfoDriver; - mikmod.MikMod_InfoLoader = MikMod_InfoLoader; - mikmod.MikMod_Init = MikMod_Init; - mikmod.MikMod_RegisterAllLoaders = MikMod_RegisterAllLoaders; - mikmod.MikMod_RegisterDriver = MikMod_RegisterDriver; - mikmod.MikMod_errno = &MikMod_errno; - mikmod.MikMod_strerror = MikMod_strerror; + if ( mikmod.loaded == 0 ) { + mikmod.MikMod_Exit = MikMod_Exit; + mikmod.MikMod_InfoDriver = MikMod_InfoDriver; + mikmod.MikMod_InfoLoader = MikMod_InfoLoader; + mikmod.MikMod_Init = MikMod_Init; + mikmod.MikMod_RegisterAllLoaders = MikMod_RegisterAllLoaders; + mikmod.MikMod_RegisterDriver = MikMod_RegisterDriver; + mikmod.MikMod_errno = &MikMod_errno; + mikmod.MikMod_strerror = MikMod_strerror; #if LIBMIKMOD_VERSION < ((3<<16)|(2<<8)) - mikmod.MikMod_free = free; + mikmod.MikMod_free = free; #else - mikmod.MikMod_free = MikMod_free; + mikmod.MikMod_free = MikMod_free; #endif - mikmod.Player_Active = Player_Active; - mikmod.Player_Free = Player_Free; - mikmod.Player_LoadGeneric = Player_LoadGeneric; - mikmod.Player_SetPosition = Player_SetPosition; - mikmod.Player_SetVolume = Player_SetVolume; - mikmod.Player_Start = Player_Start; - mikmod.Player_Stop = Player_Stop; - mikmod.VC_WriteBytes = VC_WriteBytes; - mikmod.drv_nos = &drv_nos; - mikmod.md_device = &md_device; - mikmod.md_mixfreq = &md_mixfreq; - mikmod.md_mode = &md_mode; - mikmod.md_musicvolume = &md_musicvolume; - mikmod.md_pansep = &md_pansep; - mikmod.md_reverb = &md_reverb; - mikmod.md_sndfxvolume = &md_sndfxvolume; - mikmod.md_volume = &md_volume; - } - ++mikmod.loaded; + mikmod.Player_Active = Player_Active; + mikmod.Player_Free = Player_Free; + mikmod.Player_LoadGeneric = Player_LoadGeneric; + mikmod.Player_SetPosition = Player_SetPosition; + mikmod.Player_SetVolume = Player_SetVolume; + mikmod.Player_Start = Player_Start; + mikmod.Player_Stop = Player_Stop; + mikmod.VC_WriteBytes = VC_WriteBytes; + mikmod.drv_nos = &drv_nos; + mikmod.md_device = &md_device; + mikmod.md_mixfreq = &md_mixfreq; + mikmod.md_mode = &md_mode; + mikmod.md_musicvolume = &md_musicvolume; + mikmod.md_pansep = &md_pansep; + mikmod.md_reverb = &md_reverb; + mikmod.md_sndfxvolume = &md_sndfxvolume; + mikmod.md_volume = &md_volume; + } + ++mikmod.loaded; - return 0; + return 0; } void Mix_QuitMOD() { - if ( mikmod.loaded == 0 ) { - return; - } - if ( mikmod.loaded == 1 ) { - } - --mikmod.loaded; + if ( mikmod.loaded == 0 ) { + return; + } + if ( mikmod.loaded == 1 ) { + } + --mikmod.loaded; } #endif /* MOD_DYNAMIC */ diff --git a/dynamic_mod.h b/dynamic_mod.h index 96e63aaa..945d3cf4 100644 --- a/dynamic_mod.h +++ b/dynamic_mod.h @@ -24,35 +24,35 @@ #include "mikmod.h" typedef struct { - int loaded; - void *handle; - - void (*MikMod_Exit)(void); - CHAR* (*MikMod_InfoDriver)(void); - CHAR* (*MikMod_InfoLoader)(void); - BOOL (*MikMod_Init)(CHAR*); - void (*MikMod_RegisterAllLoaders)(void); - void (*MikMod_RegisterDriver)(struct MDRIVER*); - int* MikMod_errno; - char* (*MikMod_strerror)(int); - void (*MikMod_free)(void*); - BOOL (*Player_Active)(void); - void (*Player_Free)(MODULE*); - MODULE* (*Player_LoadGeneric)(MREADER*,int,BOOL); - void (*Player_SetPosition)(UWORD); - void (*Player_SetVolume)(SWORD); - void (*Player_Start)(MODULE*); - void (*Player_Stop)(void); - ULONG (*VC_WriteBytes)(SBYTE*,ULONG); - struct MDRIVER* drv_nos; - UWORD* md_device; - UWORD* md_mixfreq; - UWORD* md_mode; - UBYTE* md_musicvolume; - UBYTE* md_pansep; - UBYTE* md_reverb; - UBYTE* md_sndfxvolume; - UBYTE* md_volume; + int loaded; + void *handle; + + void (*MikMod_Exit)(void); + CHAR* (*MikMod_InfoDriver)(void); + CHAR* (*MikMod_InfoLoader)(void); + BOOL (*MikMod_Init)(CHAR*); + void (*MikMod_RegisterAllLoaders)(void); + void (*MikMod_RegisterDriver)(struct MDRIVER*); + int* MikMod_errno; + char* (*MikMod_strerror)(int); + void (*MikMod_free)(void*); + BOOL (*Player_Active)(void); + void (*Player_Free)(MODULE*); + MODULE* (*Player_LoadGeneric)(MREADER*,int,BOOL); + void (*Player_SetPosition)(UWORD); + void (*Player_SetVolume)(SWORD); + void (*Player_Start)(MODULE*); + void (*Player_Stop)(void); + ULONG (*VC_WriteBytes)(SBYTE*,ULONG); + struct MDRIVER* drv_nos; + UWORD* md_device; + UWORD* md_mixfreq; + UWORD* md_mode; + UBYTE* md_musicvolume; + UBYTE* md_pansep; + UBYTE* md_reverb; + UBYTE* md_sndfxvolume; + UBYTE* md_volume; } mikmod_loader; extern mikmod_loader mikmod; diff --git a/dynamic_mp3.c b/dynamic_mp3.c index ae38eb4c..c5842f1c 100644 --- a/dynamic_mp3.c +++ b/dynamic_mp3.c @@ -26,145 +26,145 @@ #include "dynamic_mp3.h" smpeg_loader smpeg = { - 0, NULL + 0, NULL }; #ifdef MP3_DYNAMIC int Mix_InitMP3() { - if ( smpeg.loaded == 0 ) { - smpeg.handle = SDL_LoadObject(MP3_DYNAMIC); - if ( smpeg.handle == NULL ) { - return -1; - } - smpeg.SMPEG_actualSpec = - (void (*)( SMPEG *, SDL_AudioSpec * )) - SDL_LoadFunction(smpeg.handle, "SMPEG_actualSpec"); - if ( smpeg.SMPEG_actualSpec == NULL ) { - SDL_UnloadObject(smpeg.handle); - return -1; - } - smpeg.SMPEG_delete = - (void (*)( SMPEG* )) - SDL_LoadFunction(smpeg.handle, "SMPEG_delete"); - if ( smpeg.SMPEG_delete == NULL ) { - SDL_UnloadObject(smpeg.handle); - return -1; - } - smpeg.SMPEG_enableaudio = - (void (*)( SMPEG*, int )) - SDL_LoadFunction(smpeg.handle, "SMPEG_enableaudio"); - if ( smpeg.SMPEG_enableaudio == NULL ) { - SDL_UnloadObject(smpeg.handle); - return -1; - } - smpeg.SMPEG_enablevideo = - (void (*)( SMPEG*, int )) - SDL_LoadFunction(smpeg.handle, "SMPEG_enablevideo"); - if ( smpeg.SMPEG_enablevideo == NULL ) { - SDL_UnloadObject(smpeg.handle); - return -1; - } - smpeg.SMPEG_new_rwops = - (SMPEG* (*)(SDL_RWops *, SMPEG_Info*, int)) - SDL_LoadFunction(smpeg.handle, "SMPEG_new_rwops"); - if ( smpeg.SMPEG_new_rwops == NULL ) { - SDL_UnloadObject(smpeg.handle); - return -1; - } - smpeg.SMPEG_play = - (void (*)( SMPEG* )) - SDL_LoadFunction(smpeg.handle, "SMPEG_play"); - if ( smpeg.SMPEG_play == NULL ) { - SDL_UnloadObject(smpeg.handle); - return -1; - } - smpeg.SMPEG_playAudio = - (int (*)( SMPEG *, Uint8 *, int )) - SDL_LoadFunction(smpeg.handle, "SMPEG_playAudio"); - if ( smpeg.SMPEG_playAudio == NULL ) { - SDL_UnloadObject(smpeg.handle); - return -1; - } - smpeg.SMPEG_rewind = - (void (*)( SMPEG* )) - SDL_LoadFunction(smpeg.handle, "SMPEG_rewind"); - if ( smpeg.SMPEG_rewind == NULL ) { - SDL_UnloadObject(smpeg.handle); - return -1; - } - smpeg.SMPEG_setvolume = - (void (*)( SMPEG*, int )) - SDL_LoadFunction(smpeg.handle, "SMPEG_setvolume"); - if ( smpeg.SMPEG_setvolume == NULL ) { - SDL_UnloadObject(smpeg.handle); - return -1; - } - smpeg.SMPEG_skip = - (void (*)( SMPEG*, float )) - SDL_LoadFunction(smpeg.handle, "SMPEG_skip"); - if ( smpeg.SMPEG_skip == NULL ) { - SDL_UnloadObject(smpeg.handle); - return -1; - } - smpeg.SMPEG_status = - (SMPEGstatus (*)( SMPEG* )) - SDL_LoadFunction(smpeg.handle, "SMPEG_status"); - if ( smpeg.SMPEG_status == NULL ) { - SDL_UnloadObject(smpeg.handle); - return -1; - } - smpeg.SMPEG_stop = - (void (*)( SMPEG* )) - SDL_LoadFunction(smpeg.handle, "SMPEG_stop"); - if ( smpeg.SMPEG_stop == NULL ) { - SDL_UnloadObject(smpeg.handle); - return -1; - } - } - ++smpeg.loaded; + if ( smpeg.loaded == 0 ) { + smpeg.handle = SDL_LoadObject(MP3_DYNAMIC); + if ( smpeg.handle == NULL ) { + return -1; + } + smpeg.SMPEG_actualSpec = + (void (*)( SMPEG *, SDL_AudioSpec * )) + SDL_LoadFunction(smpeg.handle, "SMPEG_actualSpec"); + if ( smpeg.SMPEG_actualSpec == NULL ) { + SDL_UnloadObject(smpeg.handle); + return -1; + } + smpeg.SMPEG_delete = + (void (*)( SMPEG* )) + SDL_LoadFunction(smpeg.handle, "SMPEG_delete"); + if ( smpeg.SMPEG_delete == NULL ) { + SDL_UnloadObject(smpeg.handle); + return -1; + } + smpeg.SMPEG_enableaudio = + (void (*)( SMPEG*, int )) + SDL_LoadFunction(smpeg.handle, "SMPEG_enableaudio"); + if ( smpeg.SMPEG_enableaudio == NULL ) { + SDL_UnloadObject(smpeg.handle); + return -1; + } + smpeg.SMPEG_enablevideo = + (void (*)( SMPEG*, int )) + SDL_LoadFunction(smpeg.handle, "SMPEG_enablevideo"); + if ( smpeg.SMPEG_enablevideo == NULL ) { + SDL_UnloadObject(smpeg.handle); + return -1; + } + smpeg.SMPEG_new_rwops = + (SMPEG* (*)(SDL_RWops *, SMPEG_Info*, int)) + SDL_LoadFunction(smpeg.handle, "SMPEG_new_rwops"); + if ( smpeg.SMPEG_new_rwops == NULL ) { + SDL_UnloadObject(smpeg.handle); + return -1; + } + smpeg.SMPEG_play = + (void (*)( SMPEG* )) + SDL_LoadFunction(smpeg.handle, "SMPEG_play"); + if ( smpeg.SMPEG_play == NULL ) { + SDL_UnloadObject(smpeg.handle); + return -1; + } + smpeg.SMPEG_playAudio = + (int (*)( SMPEG *, Uint8 *, int )) + SDL_LoadFunction(smpeg.handle, "SMPEG_playAudio"); + if ( smpeg.SMPEG_playAudio == NULL ) { + SDL_UnloadObject(smpeg.handle); + return -1; + } + smpeg.SMPEG_rewind = + (void (*)( SMPEG* )) + SDL_LoadFunction(smpeg.handle, "SMPEG_rewind"); + if ( smpeg.SMPEG_rewind == NULL ) { + SDL_UnloadObject(smpeg.handle); + return -1; + } + smpeg.SMPEG_setvolume = + (void (*)( SMPEG*, int )) + SDL_LoadFunction(smpeg.handle, "SMPEG_setvolume"); + if ( smpeg.SMPEG_setvolume == NULL ) { + SDL_UnloadObject(smpeg.handle); + return -1; + } + smpeg.SMPEG_skip = + (void (*)( SMPEG*, float )) + SDL_LoadFunction(smpeg.handle, "SMPEG_skip"); + if ( smpeg.SMPEG_skip == NULL ) { + SDL_UnloadObject(smpeg.handle); + return -1; + } + smpeg.SMPEG_status = + (SMPEGstatus (*)( SMPEG* )) + SDL_LoadFunction(smpeg.handle, "SMPEG_status"); + if ( smpeg.SMPEG_status == NULL ) { + SDL_UnloadObject(smpeg.handle); + return -1; + } + smpeg.SMPEG_stop = + (void (*)( SMPEG* )) + SDL_LoadFunction(smpeg.handle, "SMPEG_stop"); + if ( smpeg.SMPEG_stop == NULL ) { + SDL_UnloadObject(smpeg.handle); + return -1; + } + } + ++smpeg.loaded; - return 0; + return 0; } void Mix_QuitMP3() { - if ( smpeg.loaded == 0 ) { - return; - } - if ( smpeg.loaded == 1 ) { - SDL_UnloadObject(smpeg.handle); - } - --smpeg.loaded; + if ( smpeg.loaded == 0 ) { + return; + } + if ( smpeg.loaded == 1 ) { + SDL_UnloadObject(smpeg.handle); + } + --smpeg.loaded; } #else int Mix_InitMP3() { - if ( smpeg.loaded == 0 ) { - smpeg.SMPEG_actualSpec = SMPEG_actualSpec; - smpeg.SMPEG_delete = SMPEG_delete; - smpeg.SMPEG_enableaudio = SMPEG_enableaudio; - smpeg.SMPEG_enablevideo = SMPEG_enablevideo; - smpeg.SMPEG_new_rwops = SMPEG_new_rwops; - smpeg.SMPEG_play = SMPEG_play; - smpeg.SMPEG_playAudio = SMPEG_playAudio; - smpeg.SMPEG_rewind = SMPEG_rewind; - smpeg.SMPEG_setvolume = SMPEG_setvolume; - smpeg.SMPEG_skip = SMPEG_skip; - smpeg.SMPEG_status = SMPEG_status; - smpeg.SMPEG_stop = SMPEG_stop; - } - ++smpeg.loaded; + if ( smpeg.loaded == 0 ) { + smpeg.SMPEG_actualSpec = SMPEG_actualSpec; + smpeg.SMPEG_delete = SMPEG_delete; + smpeg.SMPEG_enableaudio = SMPEG_enableaudio; + smpeg.SMPEG_enablevideo = SMPEG_enablevideo; + smpeg.SMPEG_new_rwops = SMPEG_new_rwops; + smpeg.SMPEG_play = SMPEG_play; + smpeg.SMPEG_playAudio = SMPEG_playAudio; + smpeg.SMPEG_rewind = SMPEG_rewind; + smpeg.SMPEG_setvolume = SMPEG_setvolume; + smpeg.SMPEG_skip = SMPEG_skip; + smpeg.SMPEG_status = SMPEG_status; + smpeg.SMPEG_stop = SMPEG_stop; + } + ++smpeg.loaded; - return 0; + return 0; } void Mix_QuitMP3() { - if ( smpeg.loaded == 0 ) { - return; - } - if ( smpeg.loaded == 1 ) { - } - --smpeg.loaded; + if ( smpeg.loaded == 0 ) { + return; + } + if ( smpeg.loaded == 1 ) { + } + --smpeg.loaded; } #endif /* MP3_DYNAMIC */ diff --git a/dynamic_mp3.h b/dynamic_mp3.h index ad24a117..1ed611de 100644 --- a/dynamic_mp3.h +++ b/dynamic_mp3.h @@ -23,20 +23,20 @@ #include "smpeg.h" typedef struct { - int loaded; - void *handle; - void (*SMPEG_actualSpec)( SMPEG *mpeg, SDL_AudioSpec *spec ); - void (*SMPEG_delete)( SMPEG* mpeg ); - void (*SMPEG_enableaudio)( SMPEG* mpeg, int enable ); - void (*SMPEG_enablevideo)( SMPEG* mpeg, int enable ); - SMPEG* (*SMPEG_new_rwops)(SDL_RWops *src, SMPEG_Info* info, int sdl_audio); - void (*SMPEG_play)( SMPEG* mpeg ); - int (*SMPEG_playAudio)( SMPEG *mpeg, Uint8 *stream, int len ); - void (*SMPEG_rewind)( SMPEG* mpeg ); - void (*SMPEG_setvolume)( SMPEG* mpeg, int volume ); - void (*SMPEG_skip)( SMPEG* mpeg, float seconds ); - SMPEGstatus (*SMPEG_status)( SMPEG* mpeg ); - void (*SMPEG_stop)( SMPEG* mpeg ); + int loaded; + void *handle; + void (*SMPEG_actualSpec)( SMPEG *mpeg, SDL_AudioSpec *spec ); + void (*SMPEG_delete)( SMPEG* mpeg ); + void (*SMPEG_enableaudio)( SMPEG* mpeg, int enable ); + void (*SMPEG_enablevideo)( SMPEG* mpeg, int enable ); + SMPEG* (*SMPEG_new_rwops)(SDL_RWops *src, SMPEG_Info* info, int sdl_audio); + void (*SMPEG_play)( SMPEG* mpeg ); + int (*SMPEG_playAudio)( SMPEG *mpeg, Uint8 *stream, int len ); + void (*SMPEG_rewind)( SMPEG* mpeg ); + void (*SMPEG_setvolume)( SMPEG* mpeg, int volume ); + void (*SMPEG_skip)( SMPEG* mpeg, float seconds ); + SMPEGstatus (*SMPEG_status)( SMPEG* mpeg ); + void (*SMPEG_stop)( SMPEG* mpeg ); } smpeg_loader; extern smpeg_loader smpeg; diff --git a/dynamic_ogg.c b/dynamic_ogg.c index 7522f454..eac58baf 100644 --- a/dynamic_ogg.c +++ b/dynamic_ogg.c @@ -26,105 +26,105 @@ #include "dynamic_ogg.h" vorbis_loader vorbis = { - 0, NULL + 0, NULL }; #ifdef OGG_DYNAMIC int Mix_InitOgg() { - if ( vorbis.loaded == 0 ) { - vorbis.handle = SDL_LoadObject(OGG_DYNAMIC); - if ( vorbis.handle == NULL ) { - return -1; - } - vorbis.ov_clear = - (int (*)(OggVorbis_File *)) - SDL_LoadFunction(vorbis.handle, "ov_clear"); - if ( vorbis.ov_clear == NULL ) { - SDL_UnloadObject(vorbis.handle); - return -1; - } - vorbis.ov_info = - (vorbis_info *(*)(OggVorbis_File *,int)) - SDL_LoadFunction(vorbis.handle, "ov_info"); - if ( vorbis.ov_info == NULL ) { - SDL_UnloadObject(vorbis.handle); - return -1; - } - vorbis.ov_open_callbacks = - (int (*)(void *, OggVorbis_File *, char *, long, ov_callbacks)) - SDL_LoadFunction(vorbis.handle, "ov_open_callbacks"); - if ( vorbis.ov_open_callbacks == NULL ) { - SDL_UnloadObject(vorbis.handle); - return -1; - } - vorbis.ov_pcm_total = - (ogg_int64_t (*)(OggVorbis_File *,int)) - SDL_LoadFunction(vorbis.handle, "ov_pcm_total"); - if ( vorbis.ov_pcm_total == NULL ) { - SDL_UnloadObject(vorbis.handle); - return -1; - } - vorbis.ov_read = + if ( vorbis.loaded == 0 ) { + vorbis.handle = SDL_LoadObject(OGG_DYNAMIC); + if ( vorbis.handle == NULL ) { + return -1; + } + vorbis.ov_clear = + (int (*)(OggVorbis_File *)) + SDL_LoadFunction(vorbis.handle, "ov_clear"); + if ( vorbis.ov_clear == NULL ) { + SDL_UnloadObject(vorbis.handle); + return -1; + } + vorbis.ov_info = + (vorbis_info *(*)(OggVorbis_File *,int)) + SDL_LoadFunction(vorbis.handle, "ov_info"); + if ( vorbis.ov_info == NULL ) { + SDL_UnloadObject(vorbis.handle); + return -1; + } + vorbis.ov_open_callbacks = + (int (*)(void *, OggVorbis_File *, char *, long, ov_callbacks)) + SDL_LoadFunction(vorbis.handle, "ov_open_callbacks"); + if ( vorbis.ov_open_callbacks == NULL ) { + SDL_UnloadObject(vorbis.handle); + return -1; + } + vorbis.ov_pcm_total = + (ogg_int64_t (*)(OggVorbis_File *,int)) + SDL_LoadFunction(vorbis.handle, "ov_pcm_total"); + if ( vorbis.ov_pcm_total == NULL ) { + SDL_UnloadObject(vorbis.handle); + return -1; + } + vorbis.ov_read = #ifdef OGG_USE_TREMOR - (long (*)(OggVorbis_File *,char *,int,int *)) + (long (*)(OggVorbis_File *,char *,int,int *)) #else - (long (*)(OggVorbis_File *,char *,int,int,int,int,int *)) + (long (*)(OggVorbis_File *,char *,int,int,int,int,int *)) #endif - SDL_LoadFunction(vorbis.handle, "ov_read"); - if ( vorbis.ov_read == NULL ) { - SDL_UnloadObject(vorbis.handle); - return -1; - } - vorbis.ov_time_seek = + SDL_LoadFunction(vorbis.handle, "ov_read"); + if ( vorbis.ov_read == NULL ) { + SDL_UnloadObject(vorbis.handle); + return -1; + } + vorbis.ov_time_seek = #ifdef OGG_USE_TREMOR - (long (*)(OggVorbis_File *,ogg_int64_t)) + (long (*)(OggVorbis_File *,ogg_int64_t)) #else - (int (*)(OggVorbis_File *,double)) + (int (*)(OggVorbis_File *,double)) #endif - SDL_LoadFunction(vorbis.handle, "ov_time_seek"); - if ( vorbis.ov_time_seek == NULL ) { - SDL_UnloadObject(vorbis.handle); - return -1; - } - } - ++vorbis.loaded; + SDL_LoadFunction(vorbis.handle, "ov_time_seek"); + if ( vorbis.ov_time_seek == NULL ) { + SDL_UnloadObject(vorbis.handle); + return -1; + } + } + ++vorbis.loaded; - return 0; + return 0; } void Mix_QuitOgg() { - if ( vorbis.loaded == 0 ) { - return; - } - if ( vorbis.loaded == 1 ) { - SDL_UnloadObject(vorbis.handle); - } - --vorbis.loaded; + if ( vorbis.loaded == 0 ) { + return; + } + if ( vorbis.loaded == 1 ) { + SDL_UnloadObject(vorbis.handle); + } + --vorbis.loaded; } #else int Mix_InitOgg() { - if ( vorbis.loaded == 0 ) { - vorbis.ov_clear = ov_clear; - vorbis.ov_info = ov_info; - vorbis.ov_open_callbacks = ov_open_callbacks; - vorbis.ov_pcm_total = ov_pcm_total; - vorbis.ov_read = ov_read; - vorbis.ov_time_seek = ov_time_seek; - } - ++vorbis.loaded; + if ( vorbis.loaded == 0 ) { + vorbis.ov_clear = ov_clear; + vorbis.ov_info = ov_info; + vorbis.ov_open_callbacks = ov_open_callbacks; + vorbis.ov_pcm_total = ov_pcm_total; + vorbis.ov_read = ov_read; + vorbis.ov_time_seek = ov_time_seek; + } + ++vorbis.loaded; - return 0; + return 0; } void Mix_QuitOgg() { - if ( vorbis.loaded == 0 ) { - return; - } - if ( vorbis.loaded == 1 ) { - } - --vorbis.loaded; + if ( vorbis.loaded == 0 ) { + return; + } + if ( vorbis.loaded == 1 ) { + } + --vorbis.loaded; } #endif /* OGG_DYNAMIC */ diff --git a/dynamic_ogg.h b/dynamic_ogg.h index b87ff551..4d8133d7 100644 --- a/dynamic_ogg.h +++ b/dynamic_ogg.h @@ -27,21 +27,21 @@ #endif typedef struct { - int loaded; - void *handle; - int (*ov_clear)(OggVorbis_File *vf); - vorbis_info *(*ov_info)(OggVorbis_File *vf,int link); - int (*ov_open_callbacks)(void *datasource, OggVorbis_File *vf, const char *initial, long ibytes, ov_callbacks callbacks); - ogg_int64_t (*ov_pcm_total)(OggVorbis_File *vf,int i); + int loaded; + void *handle; + int (*ov_clear)(OggVorbis_File *vf); + vorbis_info *(*ov_info)(OggVorbis_File *vf,int link); + int (*ov_open_callbacks)(void *datasource, OggVorbis_File *vf, const char *initial, long ibytes, ov_callbacks callbacks); + ogg_int64_t (*ov_pcm_total)(OggVorbis_File *vf,int i); #ifdef OGG_USE_TREMOR - long (*ov_read)(OggVorbis_File *vf,char *buffer,int length, int *bitstream); + long (*ov_read)(OggVorbis_File *vf,char *buffer,int length, int *bitstream); #else - long (*ov_read)(OggVorbis_File *vf,char *buffer,int length, int bigendianp,int word,int sgned,int *bitstream); + long (*ov_read)(OggVorbis_File *vf,char *buffer,int length, int bigendianp,int word,int sgned,int *bitstream); #endif #ifdef OGG_USE_TREMOR - int (*ov_time_seek)(OggVorbis_File *vf,ogg_int64_t pos); + int (*ov_time_seek)(OggVorbis_File *vf,ogg_int64_t pos); #else - int (*ov_time_seek)(OggVorbis_File *vf,double pos); + int (*ov_time_seek)(OggVorbis_File *vf,double pos); #endif } vorbis_loader; diff --git a/effect_position.c b/effect_position.c index 2c232c72..bb998ecf 100644 --- a/effect_position.c +++ b/effect_position.c @@ -42,7 +42,7 @@ #include struct timeval tv1; struct timeval tv2; - + gettimeofday(&tv1, NULL); ... do your thing here ... @@ -134,19 +134,19 @@ static void _Eff_position_u8(int chan, void *stream, int len, void *udata) if (args->room_angle == 180) for (i = 0; i < len; i += sizeof (Uint8) * 2) { /* must adjust the sample so that 0 is the center */ - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_f) * args->distance_f) + 128); ptr++; } else for (i = 0; i < len; i += sizeof (Uint8) * 2) { /* must adjust the sample so that 0 is the center */ - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_f) * args->distance_f) + 128); ptr++; } @@ -171,64 +171,64 @@ static void _Eff_position_u8_c4(int chan, void *stream, int len, void *udata) if (args->room_angle == 0) for (i = 0; i < len; i += sizeof (Uint8) * 6) { /* must adjust the sample so that 0 is the center */ - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_rear_f) * args->distance_f) + 128); ptr++; } else if (args->room_angle == 90) for (i = 0; i < len; i += sizeof (Uint8) * 6) { /* must adjust the sample so that 0 is the center */ - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_rear_f) * args->distance_f) + 128); ptr++; } else if (args->room_angle == 180) for (i = 0; i < len; i += sizeof (Uint8) * 6) { /* must adjust the sample so that 0 is the center */ - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_f) * args->distance_f) + 128); ptr++; } else if (args->room_angle == 270) for (i = 0; i < len; i += sizeof (Uint8) * 6) { /* must adjust the sample so that 0 is the center */ - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_f) * args->distance_f) + 128); ptr++; } @@ -255,94 +255,94 @@ static void _Eff_position_u8_c6(int chan, void *stream, int len, void *udata) if (args->room_angle == 0) for (i = 0; i < len; i += sizeof (Uint8) * 6) { /* must adjust the sample so that 0 is the center */ - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->center_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->lfe_f) * args->distance_f) + 128); ptr++; } else if (args->room_angle == 90) for (i = 0; i < len; i += sizeof (Uint8) * 6) { /* must adjust the sample so that 0 is the center */ - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_rear_f) * args->distance_f/2) + 128) - + (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + + (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_f) * args->distance_f/2) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->lfe_f) * args->distance_f) + 128); ptr++; } else if (args->room_angle == 180) for (i = 0; i < len; i += sizeof (Uint8) * 6) { /* must adjust the sample so that 0 is the center */ - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_rear_f) * args->distance_f/2) + 128) - + (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + + (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_rear_f) * args->distance_f/2) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->lfe_f) * args->distance_f) + 128); ptr++; } else if (args->room_angle == 270) for (i = 0; i < len; i += sizeof (Uint8) * 6) { /* must adjust the sample so that 0 is the center */ - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_rear_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->right_f) * args->distance_f) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_f) * args->distance_f/2) + 128) - + (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + + (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->left_rear_f) * args->distance_f/2) + 128); ptr++; - *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) * args->lfe_f) * args->distance_f) + 128); ptr++; } @@ -368,9 +368,9 @@ static void _Eff_position_table_u8(int chan, void *stream, int len, void *udata) Uint8 *d = ((Uint8 *) _Eff_volume_table) + (256 * args->distance_u8); if (args->room_angle == 180) { - Uint8 *temp = l; - l = r; - r = temp; + Uint8 *temp = l; + l = r; + r = temp; } /* * if there's only a mono channnel, then l[] and r[] are always @@ -462,25 +462,25 @@ static void _Eff_position_s8_c4(int chan, void *stream, int len, void *udata) *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++; - break; + break; case 90: *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++; - break; + break; case 180: *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f); ptr++; - break; + break; case 270: *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++; - break; + break; } } } @@ -510,7 +510,7 @@ static void _Eff_position_s8_c6(int chan, void *stream, int len, void *udata) *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->center_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->lfe_f) * args->distance_f); ptr++; - break; + break; case 90: *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++; @@ -519,7 +519,7 @@ static void _Eff_position_s8_c6(int chan, void *stream, int len, void *udata) *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f / 2) + (Sint8)((((float) *ptr) * args->right_f) * args->distance_f / 2); ptr++; *ptr = (Sint8)((((float) *ptr) * args->lfe_f) * args->distance_f); ptr++; - break; + break; case 180: *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++; @@ -528,7 +528,7 @@ static void _Eff_position_s8_c6(int chan, void *stream, int len, void *udata) *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f / 2) + (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f / 2); ptr++; *ptr = (Sint8)((((float) *ptr) * args->lfe_f) * args->distance_f); ptr++; - break; + break; case 270: *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++; *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f); ptr++; @@ -537,7 +537,7 @@ static void _Eff_position_s8_c6(int chan, void *stream, int len, void *udata) *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f / 2) + (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f / 2); ptr++; *ptr = (Sint8)((((float) *ptr) * args->lfe_f) * args->distance_f); ptr++; - break; + break; } } } @@ -562,9 +562,9 @@ static void _Eff_position_table_s8(int chan, void *stream, int len, void *udata) Sint8 *d = ((Sint8 *) _Eff_volume_table) + (256 * args->distance_u8); if (args->room_angle == 180) { - Sint8 *temp = l; - l = r; - r = temp; + Sint8 *temp = l; + l = r; + r = temp; } @@ -610,20 +610,20 @@ static void _Eff_position_u16lsb(int chan, void *stream, int len, void *udata) for (i = 0; i < len; i += sizeof (Uint16) * 2) { Sint16 sampl = (Sint16) (SDL_SwapLE16(*(ptr+0)) - 32768); Sint16 sampr = (Sint16) (SDL_SwapLE16(*(ptr+1)) - 32768); - + Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f) * args->distance_f) + 32768); Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f) * args->distance_f) + 32768); - if (args->room_angle == 180) { - *(ptr++) = (Uint16) SDL_SwapLE16(swapr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapl); - } - else { - *(ptr++) = (Uint16) SDL_SwapLE16(swapl); - *(ptr++) = (Uint16) SDL_SwapLE16(swapr); - } + if (args->room_angle == 180) { + *(ptr++) = (Uint16) SDL_SwapLE16(swapr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapl); + } + else { + *(ptr++) = (Uint16) SDL_SwapLE16(swapl); + *(ptr++) = (Uint16) SDL_SwapLE16(swapr); + } } } static void _Eff_position_u16lsb_c4(int chan, void *stream, int len, void *udata) @@ -637,7 +637,7 @@ static void _Eff_position_u16lsb_c4(int chan, void *stream, int len, void *udata Sint16 sampr = (Sint16) (SDL_SwapLE16(*(ptr+1)) - 32768); Sint16 samplr = (Sint16) (SDL_SwapLE16(*(ptr+2)) - 32768); Sint16 samprr = (Sint16) (SDL_SwapLE16(*(ptr+3)) - 32768); - + Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f) * args->distance_f) + 32768); Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f) @@ -647,32 +647,32 @@ static void _Eff_position_u16lsb_c4(int chan, void *stream, int len, void *udata Uint16 swaprr = (Uint16) ((Sint16) (((float) samprr * args->right_rear_f) * args->distance_f) + 32768); - switch (args->room_angle) { - case 0: - *(ptr++) = (Uint16) SDL_SwapLE16(swapl); - *(ptr++) = (Uint16) SDL_SwapLE16(swapr); - *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); - break; - case 90: - *(ptr++) = (Uint16) SDL_SwapLE16(swapr); - *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapl); - *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); - break; - case 180: - *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapl); - break; - case 270: - *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapl); - *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapr); - break; - } + switch (args->room_angle) { + case 0: + *(ptr++) = (Uint16) SDL_SwapLE16(swapl); + *(ptr++) = (Uint16) SDL_SwapLE16(swapr); + *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); + break; + case 90: + *(ptr++) = (Uint16) SDL_SwapLE16(swapr); + *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapl); + *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); + break; + case 180: + *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapl); + break; + case 270: + *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapl); + *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapr); + break; + } } } static void _Eff_position_u16lsb_c6(int chan, void *stream, int len, void *udata) @@ -702,40 +702,40 @@ static void _Eff_position_u16lsb_c6(int chan, void *stream, int len, void *udata Uint16 swapwf = (Uint16) ((Sint16) (((float) sampwf * args->lfe_f) * args->distance_f) + 32768); - switch (args->room_angle) { - case 0: - *(ptr++) = (Uint16) SDL_SwapLE16(swapl); - *(ptr++) = (Uint16) SDL_SwapLE16(swapr); - *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapce); - *(ptr++) = (Uint16) SDL_SwapLE16(swapwf); - break; - case 90: - *(ptr++) = (Uint16) SDL_SwapLE16(swapr); - *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapl); - *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapr)/2 + (Uint16) SDL_SwapLE16(swaprr)/2; - *(ptr++) = (Uint16) SDL_SwapLE16(swapwf); - break; - case 180: - *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapl); - *(ptr++) = (Uint16) SDL_SwapLE16(swaprr)/2 + (Uint16) SDL_SwapLE16(swaplr)/2; - *(ptr++) = (Uint16) SDL_SwapLE16(swapwf); - break; - case 270: - *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapl); - *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapr); - *(ptr++) = (Uint16) SDL_SwapLE16(swapl)/2 + (Uint16) SDL_SwapLE16(swaplr)/2; - *(ptr++) = (Uint16) SDL_SwapLE16(swapwf); - break; - } + switch (args->room_angle) { + case 0: + *(ptr++) = (Uint16) SDL_SwapLE16(swapl); + *(ptr++) = (Uint16) SDL_SwapLE16(swapr); + *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapce); + *(ptr++) = (Uint16) SDL_SwapLE16(swapwf); + break; + case 90: + *(ptr++) = (Uint16) SDL_SwapLE16(swapr); + *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapl); + *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapr)/2 + (Uint16) SDL_SwapLE16(swaprr)/2; + *(ptr++) = (Uint16) SDL_SwapLE16(swapwf); + break; + case 180: + *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapl); + *(ptr++) = (Uint16) SDL_SwapLE16(swaprr)/2 + (Uint16) SDL_SwapLE16(swaplr)/2; + *(ptr++) = (Uint16) SDL_SwapLE16(swapwf); + break; + case 270: + *(ptr++) = (Uint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapl); + *(ptr++) = (Uint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapr); + *(ptr++) = (Uint16) SDL_SwapLE16(swapl)/2 + (Uint16) SDL_SwapLE16(swaplr)/2; + *(ptr++) = (Uint16) SDL_SwapLE16(swapwf); + break; + } } } @@ -748,8 +748,8 @@ static void _Eff_position_s16lsb(int chan, void *stream, int len, void *udata) #if 0 if (len % (sizeof(Sint16) * 2)) { - fprintf(stderr,"Not an even number of frames! len=%d\n", len); - return; + fprintf(stderr,"Not an even number of frames! len=%d\n", len); + return; } #endif @@ -758,14 +758,14 @@ static void _Eff_position_s16lsb(int chan, void *stream, int len, void *udata) args->left_f) * args->distance_f); Sint16 swapr = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+1))) * args->right_f) * args->distance_f); - if (args->room_angle == 180) { - *(ptr++) = (Sint16) SDL_SwapLE16(swapr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapl); - } - else { - *(ptr++) = (Sint16) SDL_SwapLE16(swapl); - *(ptr++) = (Sint16) SDL_SwapLE16(swapr); - } + if (args->room_angle == 180) { + *(ptr++) = (Sint16) SDL_SwapLE16(swapr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapl); + } + else { + *(ptr++) = (Sint16) SDL_SwapLE16(swapl); + *(ptr++) = (Sint16) SDL_SwapLE16(swapr); + } } } static void _Eff_position_s16lsb_c4(int chan, void *stream, int len, void *udata) @@ -784,32 +784,32 @@ static void _Eff_position_s16lsb_c4(int chan, void *stream, int len, void *udata args->left_rear_f) * args->distance_f); Sint16 swaprr = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+2))) * args->right_rear_f) * args->distance_f); - switch (args->room_angle) { - case 0: - *(ptr++) = (Sint16) SDL_SwapLE16(swapl); - *(ptr++) = (Sint16) SDL_SwapLE16(swapr); - *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); - break; - case 90: - *(ptr++) = (Sint16) SDL_SwapLE16(swapr); - *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapl); - *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); - break; - case 180: - *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapl); - break; - case 270: - *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapl); - *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapr); - break; - } + switch (args->room_angle) { + case 0: + *(ptr++) = (Sint16) SDL_SwapLE16(swapl); + *(ptr++) = (Sint16) SDL_SwapLE16(swapr); + *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); + break; + case 90: + *(ptr++) = (Sint16) SDL_SwapLE16(swapr); + *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapl); + *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); + break; + case 180: + *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapl); + break; + case 270: + *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapl); + *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapr); + break; + } } } @@ -833,40 +833,40 @@ static void _Eff_position_s16lsb_c6(int chan, void *stream, int len, void *udata args->center_f) * args->distance_f); Sint16 swapwf = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+5))) * args->lfe_f) * args->distance_f); - switch (args->room_angle) { - case 0: - *(ptr++) = (Sint16) SDL_SwapLE16(swapl); - *(ptr++) = (Sint16) SDL_SwapLE16(swapr); - *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapce); - *(ptr++) = (Sint16) SDL_SwapLE16(swapwf); - break; - case 90: - *(ptr++) = (Sint16) SDL_SwapLE16(swapr); - *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapl); - *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapr)/2 + (Sint16) SDL_SwapLE16(swaprr)/2; - *(ptr++) = (Sint16) SDL_SwapLE16(swapwf); - break; - case 180: - *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapl); - *(ptr++) = (Sint16) SDL_SwapLE16(swaprr)/2 + (Sint16) SDL_SwapLE16(swaplr)/2; - *(ptr++) = (Sint16) SDL_SwapLE16(swapwf); - break; - case 270: - *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapl); - *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapr); - *(ptr++) = (Sint16) SDL_SwapLE16(swapl)/2 + (Sint16) SDL_SwapLE16(swaplr)/2; - *(ptr++) = (Sint16) SDL_SwapLE16(swapwf); - break; - } + switch (args->room_angle) { + case 0: + *(ptr++) = (Sint16) SDL_SwapLE16(swapl); + *(ptr++) = (Sint16) SDL_SwapLE16(swapr); + *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapce); + *(ptr++) = (Sint16) SDL_SwapLE16(swapwf); + break; + case 90: + *(ptr++) = (Sint16) SDL_SwapLE16(swapr); + *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapl); + *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapr)/2 + (Sint16) SDL_SwapLE16(swaprr)/2; + *(ptr++) = (Sint16) SDL_SwapLE16(swapwf); + break; + case 180: + *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapl); + *(ptr++) = (Sint16) SDL_SwapLE16(swaprr)/2 + (Sint16) SDL_SwapLE16(swaplr)/2; + *(ptr++) = (Sint16) SDL_SwapLE16(swapwf); + break; + case 270: + *(ptr++) = (Sint16) SDL_SwapLE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapl); + *(ptr++) = (Sint16) SDL_SwapLE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapr); + *(ptr++) = (Sint16) SDL_SwapLE16(swapl)/2 + (Sint16) SDL_SwapLE16(swaplr)/2; + *(ptr++) = (Sint16) SDL_SwapLE16(swapwf); + break; + } } } @@ -880,20 +880,20 @@ static void _Eff_position_u16msb(int chan, void *stream, int len, void *udata) for (i = 0; i < len; i += sizeof (Sint16) * 2) { Sint16 sampl = (Sint16) (SDL_SwapBE16(*(ptr+0)) - 32768); Sint16 sampr = (Sint16) (SDL_SwapBE16(*(ptr+1)) - 32768); - + Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f) * args->distance_f) + 32768); Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f) * args->distance_f) + 32768); - if (args->room_angle == 180) { - *(ptr++) = (Uint16) SDL_SwapBE16(swapr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapl); - } - else { - *(ptr++) = (Uint16) SDL_SwapBE16(swapl); - *(ptr++) = (Uint16) SDL_SwapBE16(swapr); - } + if (args->room_angle == 180) { + *(ptr++) = (Uint16) SDL_SwapBE16(swapr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapl); + } + else { + *(ptr++) = (Uint16) SDL_SwapBE16(swapl); + *(ptr++) = (Uint16) SDL_SwapBE16(swapr); + } } } static void _Eff_position_u16msb_c4(int chan, void *stream, int len, void *udata) @@ -908,7 +908,7 @@ static void _Eff_position_u16msb_c4(int chan, void *stream, int len, void *udata Sint16 sampr = (Sint16) (SDL_SwapBE16(*(ptr+1)) - 32768); Sint16 samplr = (Sint16) (SDL_SwapBE16(*(ptr+2)) - 32768); Sint16 samprr = (Sint16) (SDL_SwapBE16(*(ptr+3)) - 32768); - + Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f) * args->distance_f) + 32768); Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f) @@ -918,32 +918,32 @@ static void _Eff_position_u16msb_c4(int chan, void *stream, int len, void *udata Uint16 swaprr = (Uint16) ((Sint16) (((float) samprr * args->right_rear_f) * args->distance_f) + 32768); - switch (args->room_angle) { - case 0: - *(ptr++) = (Uint16) SDL_SwapBE16(swapl); - *(ptr++) = (Uint16) SDL_SwapBE16(swapr); - *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); - break; - case 90: - *(ptr++) = (Uint16) SDL_SwapBE16(swapr); - *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapl); - *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); - break; - case 180: - *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapl); - break; - case 270: - *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapl); - *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapr); - break; - } + switch (args->room_angle) { + case 0: + *(ptr++) = (Uint16) SDL_SwapBE16(swapl); + *(ptr++) = (Uint16) SDL_SwapBE16(swapr); + *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); + break; + case 90: + *(ptr++) = (Uint16) SDL_SwapBE16(swapr); + *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapl); + *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); + break; + case 180: + *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapl); + break; + case 270: + *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapl); + *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapr); + break; + } } } static void _Eff_position_u16msb_c6(int chan, void *stream, int len, void *udata) @@ -960,7 +960,7 @@ static void _Eff_position_u16msb_c6(int chan, void *stream, int len, void *udata Sint16 samprr = (Sint16) (SDL_SwapBE16(*(ptr+3)) - 32768); Sint16 sampce = (Sint16) (SDL_SwapBE16(*(ptr+4)) - 32768); Sint16 sampwf = (Sint16) (SDL_SwapBE16(*(ptr+5)) - 32768); - + Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f) * args->distance_f) + 32768); Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f) @@ -974,40 +974,40 @@ static void _Eff_position_u16msb_c6(int chan, void *stream, int len, void *udata Uint16 swapwf = (Uint16) ((Sint16) (((float) sampwf * args->lfe_f) * args->distance_f) + 32768); - switch (args->room_angle) { - case 0: - *(ptr++) = (Uint16) SDL_SwapBE16(swapl); - *(ptr++) = (Uint16) SDL_SwapBE16(swapr); - *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapce); - *(ptr++) = (Uint16) SDL_SwapBE16(swapwf); - break; - case 90: - *(ptr++) = (Uint16) SDL_SwapBE16(swapr); - *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapl); - *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapr)/2 + (Uint16) SDL_SwapBE16(swaprr)/2; - *(ptr++) = (Uint16) SDL_SwapBE16(swapwf); - break; - case 180: - *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapl); - *(ptr++) = (Uint16) SDL_SwapBE16(swaprr)/2 + (Uint16) SDL_SwapBE16(swaplr)/2; - *(ptr++) = (Uint16) SDL_SwapBE16(swapwf); - break; - case 270: - *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapl); - *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapr); - *(ptr++) = (Uint16) SDL_SwapBE16(swapl)/2 + (Uint16) SDL_SwapBE16(swaplr)/2; - *(ptr++) = (Uint16) SDL_SwapBE16(swapwf); - break; - } + switch (args->room_angle) { + case 0: + *(ptr++) = (Uint16) SDL_SwapBE16(swapl); + *(ptr++) = (Uint16) SDL_SwapBE16(swapr); + *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapce); + *(ptr++) = (Uint16) SDL_SwapBE16(swapwf); + break; + case 90: + *(ptr++) = (Uint16) SDL_SwapBE16(swapr); + *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapl); + *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapr)/2 + (Uint16) SDL_SwapBE16(swaprr)/2; + *(ptr++) = (Uint16) SDL_SwapBE16(swapwf); + break; + case 180: + *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapl); + *(ptr++) = (Uint16) SDL_SwapBE16(swaprr)/2 + (Uint16) SDL_SwapBE16(swaplr)/2; + *(ptr++) = (Uint16) SDL_SwapBE16(swapwf); + break; + case 270: + *(ptr++) = (Uint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapl); + *(ptr++) = (Uint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapr); + *(ptr++) = (Uint16) SDL_SwapBE16(swapl)/2 + (Uint16) SDL_SwapBE16(swaplr)/2; + *(ptr++) = (Uint16) SDL_SwapBE16(swapwf); + break; + } } } @@ -1043,32 +1043,32 @@ static void _Eff_position_s16msb_c4(int chan, void *stream, int len, void *udata args->left_rear_f) * args->distance_f); Sint16 swaprr = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+3))) * args->right_rear_f) * args->distance_f); - switch (args->room_angle) { - case 0: - *(ptr++) = (Sint16) SDL_SwapBE16(swapl); - *(ptr++) = (Sint16) SDL_SwapBE16(swapr); - *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); - break; - case 90: - *(ptr++) = (Sint16) SDL_SwapBE16(swapr); - *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapl); - *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); - break; - case 180: - *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapl); - break; - case 270: - *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapl); - *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapr); - break; - } + switch (args->room_angle) { + case 0: + *(ptr++) = (Sint16) SDL_SwapBE16(swapl); + *(ptr++) = (Sint16) SDL_SwapBE16(swapr); + *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); + break; + case 90: + *(ptr++) = (Sint16) SDL_SwapBE16(swapr); + *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapl); + *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); + break; + case 180: + *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapl); + break; + case 270: + *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapl); + *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapr); + break; + } } } static void _Eff_position_s16msb_c6(int chan, void *stream, int len, void *udata) @@ -1092,40 +1092,40 @@ static void _Eff_position_s16msb_c6(int chan, void *stream, int len, void *udata Sint16 swapwf = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+5))) * args->lfe_f) * args->distance_f); - switch (args->room_angle) { - case 0: - *(ptr++) = (Sint16) SDL_SwapBE16(swapl); - *(ptr++) = (Sint16) SDL_SwapBE16(swapr); - *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapce); - *(ptr++) = (Sint16) SDL_SwapBE16(swapwf); - break; - case 90: - *(ptr++) = (Sint16) SDL_SwapBE16(swapr); - *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapl); - *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapr)/2 + (Sint16) SDL_SwapBE16(swaprr)/2; - *(ptr++) = (Sint16) SDL_SwapBE16(swapwf); - break; - case 180: - *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapl); - *(ptr++) = (Sint16) SDL_SwapBE16(swaprr)/2 + (Sint16) SDL_SwapBE16(swaplr)/2; - *(ptr++) = (Sint16) SDL_SwapBE16(swapwf); - break; - case 270: - *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapl); - *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapr); - *(ptr++) = (Sint16) SDL_SwapBE16(swapl)/2 + (Sint16) SDL_SwapBE16(swaplr)/2; - *(ptr++) = (Sint16) SDL_SwapBE16(swapwf); - break; - } + switch (args->room_angle) { + case 0: + *(ptr++) = (Sint16) SDL_SwapBE16(swapl); + *(ptr++) = (Sint16) SDL_SwapBE16(swapr); + *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapce); + *(ptr++) = (Sint16) SDL_SwapBE16(swapwf); + break; + case 90: + *(ptr++) = (Sint16) SDL_SwapBE16(swapr); + *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapl); + *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapr)/2 + (Sint16) SDL_SwapBE16(swaprr)/2; + *(ptr++) = (Sint16) SDL_SwapBE16(swapwf); + break; + case 180: + *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapl); + *(ptr++) = (Sint16) SDL_SwapBE16(swaprr)/2 + (Sint16) SDL_SwapBE16(swaplr)/2; + *(ptr++) = (Sint16) SDL_SwapBE16(swapwf); + break; + case 270: + *(ptr++) = (Sint16) SDL_SwapBE16(swaplr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapl); + *(ptr++) = (Sint16) SDL_SwapBE16(swaprr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapr); + *(ptr++) = (Sint16) SDL_SwapBE16(swapl)/2 + (Sint16) SDL_SwapBE16(swaplr)/2; + *(ptr++) = (Sint16) SDL_SwapBE16(swapwf); + break; + } } } @@ -1192,95 +1192,95 @@ static Mix_EffectFunc_t get_position_effect_func(Uint16 format, int channels) switch (format) { case AUDIO_U8: - switch (channels) { - case 1: - case 2: - f = (_Eff_build_volume_table_u8()) ? _Eff_position_table_u8 : - _Eff_position_u8; - break; - case 4: + switch (channels) { + case 1: + case 2: + f = (_Eff_build_volume_table_u8()) ? _Eff_position_table_u8 : + _Eff_position_u8; + break; + case 4: f = _Eff_position_u8_c4; - break; - case 6: + break; + case 6: f = _Eff_position_u8_c6; - break; - } + break; + } break; case AUDIO_S8: - switch (channels) { - case 1: - case 2: - f = (_Eff_build_volume_table_s8()) ? _Eff_position_table_s8 : - _Eff_position_s8; - break; - case 4: + switch (channels) { + case 1: + case 2: + f = (_Eff_build_volume_table_s8()) ? _Eff_position_table_s8 : + _Eff_position_s8; + break; + case 4: f = _Eff_position_s8_c4; - break; - case 6: + break; + case 6: f = _Eff_position_s8_c6; - break; - } + break; + } break; case AUDIO_U16LSB: - switch (channels) { - case 1: - case 2: - f = _Eff_position_u16lsb; - break; - case 4: - f = _Eff_position_u16lsb_c4; - break; - case 6: - f = _Eff_position_u16lsb_c6; - break; - } + switch (channels) { + case 1: + case 2: + f = _Eff_position_u16lsb; + break; + case 4: + f = _Eff_position_u16lsb_c4; + break; + case 6: + f = _Eff_position_u16lsb_c6; + break; + } break; case AUDIO_S16LSB: - switch (channels) { - case 1: - case 2: - f = _Eff_position_s16lsb; - break; - case 4: - f = _Eff_position_s16lsb_c4; - break; - case 6: - f = _Eff_position_s16lsb_c6; - break; - } + switch (channels) { + case 1: + case 2: + f = _Eff_position_s16lsb; + break; + case 4: + f = _Eff_position_s16lsb_c4; + break; + case 6: + f = _Eff_position_s16lsb_c6; + break; + } break; case AUDIO_U16MSB: - switch (channels) { - case 1: - case 2: - f = _Eff_position_u16msb; - break; - case 4: - f = _Eff_position_u16msb_c4; - break; - case 6: - f = _Eff_position_u16msb_c6; - break; - } + switch (channels) { + case 1: + case 2: + f = _Eff_position_u16msb; + break; + case 4: + f = _Eff_position_u16msb_c4; + break; + case 6: + f = _Eff_position_u16msb_c6; + break; + } break; case AUDIO_S16MSB: - switch (channels) { - case 1: - case 2: - f = _Eff_position_s16msb; - break; - case 4: - f = _Eff_position_s16msb_c4; - break; - case 6: - f = _Eff_position_s16msb_c6; - break; - } + switch (channels) { + case 1: + case 2: + f = _Eff_position_s16msb; + break; + case 4: + f = _Eff_position_s16msb_c4; + break; + case 6: + f = _Eff_position_s16msb_c6; + break; + } break; default: @@ -1388,34 +1388,34 @@ static void set_amplitudes(int channels, int angle, int room_angle) if (center < 0) center = 0; if (center > 255) center = 255; if (room_angle == 90) { - speaker_amplitude[0] = (Uint8)left_rear; - speaker_amplitude[1] = (Uint8)left; - speaker_amplitude[2] = (Uint8)right_rear; - speaker_amplitude[3] = (Uint8)right; + speaker_amplitude[0] = (Uint8)left_rear; + speaker_amplitude[1] = (Uint8)left; + speaker_amplitude[2] = (Uint8)right_rear; + speaker_amplitude[3] = (Uint8)right; } else if (room_angle == 180) { - if (channels == 2) { - speaker_amplitude[0] = (Uint8)right; - speaker_amplitude[1] = (Uint8)left; - } - else { - speaker_amplitude[0] = (Uint8)right_rear; - speaker_amplitude[1] = (Uint8)left_rear; - speaker_amplitude[2] = (Uint8)right; - speaker_amplitude[3] = (Uint8)left; - } + if (channels == 2) { + speaker_amplitude[0] = (Uint8)right; + speaker_amplitude[1] = (Uint8)left; + } + else { + speaker_amplitude[0] = (Uint8)right_rear; + speaker_amplitude[1] = (Uint8)left_rear; + speaker_amplitude[2] = (Uint8)right; + speaker_amplitude[3] = (Uint8)left; + } } else if (room_angle == 270) { - speaker_amplitude[0] = (Uint8)right; - speaker_amplitude[1] = (Uint8)right_rear; - speaker_amplitude[2] = (Uint8)left; - speaker_amplitude[3] = (Uint8)left_rear; + speaker_amplitude[0] = (Uint8)right; + speaker_amplitude[1] = (Uint8)right_rear; + speaker_amplitude[2] = (Uint8)left; + speaker_amplitude[3] = (Uint8)left_rear; } else { - speaker_amplitude[0] = (Uint8)left; - speaker_amplitude[1] = (Uint8)right; - speaker_amplitude[2] = (Uint8)left_rear; - speaker_amplitude[3] = (Uint8)right_rear; + speaker_amplitude[0] = (Uint8)left; + speaker_amplitude[1] = (Uint8)right; + speaker_amplitude[2] = (Uint8)left_rear; + speaker_amplitude[3] = (Uint8)right_rear; } speaker_amplitude[4] = (Uint8)center; speaker_amplitude[5] = 255; @@ -1438,13 +1438,13 @@ int Mix_SetPanning(int channel, Uint8 left, Uint8 right) if (channels > 2) { /* left = right = 255 => angle = 0, to unregister effect as when channels = 2 */ - /* left = 255 => angle = -90; left = 0 => angle = +89 */ + /* left = 255 => angle = -90; left = 0 => angle = +89 */ int angle = 0; if ((left != 255) || (right != 255)) { - angle = (int)left; - angle = 127 - angle; - angle = -angle; - angle = angle * 90 / 128; /* Make it larger for more effect? */ + angle = (int)left; + angle = 127 - angle; + angle = -angle; + angle = angle * 90 / 128; /* Make it larger for more effect? */ } return( Mix_SetPosition(channel, angle, 0) ); } @@ -1571,18 +1571,18 @@ int Mix_SetPosition(int channel, Sint16 angle, Uint8 distance) if (channels == 2) { - if (angle > 180) - room_angle = 180; /* exchange left and right channels */ - else room_angle = 0; + if (angle > 180) + room_angle = 180; /* exchange left and right channels */ + else room_angle = 0; } if (channels == 4 || channels == 6) { - if (angle > 315) room_angle = 0; - else if (angle > 225) room_angle = 270; - else if (angle > 135) room_angle = 180; - else if (angle > 45) room_angle = 90; - else room_angle = 0; + if (angle > 315) room_angle = 0; + else if (angle > 225) room_angle = 270; + else if (angle > 135) room_angle = 180; + else if (angle > 45) room_angle = 90; + else room_angle = 0; } diff --git a/effect_stereoreverse.c b/effect_stereoreverse.c index 44f6f784..5763aea9 100644 --- a/effect_stereoreverse.c +++ b/effect_stereoreverse.c @@ -40,7 +40,7 @@ #include struct timeval tv1; struct timeval tv2; - + gettimeofday(&tv1, NULL); ... do your thing here ... diff --git a/effects_internal.c b/effects_internal.c index ffc81044..62d59b22 100644 --- a/effects_internal.c +++ b/effects_internal.c @@ -78,7 +78,7 @@ void *_Eff_build_volume_table_u8(void) _Eff_volume_table = (void *) rc; for (volume = 0; volume < 256; volume++) { for (sample = -128; sample < 128; sample ++) { - *rc = (Uint8)(((float) sample) * ((float) volume / 255.0)) + *rc = (Uint8)(((float) sample) * ((float) volume / 255.0)) + 128; rc++; } diff --git a/fluidsynth.c b/fluidsynth.c index d8ae1a2e..24c60ab5 100644 --- a/fluidsynth.c +++ b/fluidsynth.c @@ -36,187 +36,187 @@ static int freq; int fluidsynth_check_soundfont(const char *path, void *data) { - FILE *file = fopen(path, "r"); - - if (file) { - fclose(file); - return 1; - } else { - Mix_SetError("Failed to access the SoundFont %s", path); - return 0; - } + FILE *file = fopen(path, "r"); + + if (file) { + fclose(file); + return 1; + } else { + Mix_SetError("Failed to access the SoundFont %s", path); + return 0; + } } int fluidsynth_load_soundfont(const char *path, void *data) { - /* If this fails, it's too late to try Timidity so pray that at least one works. */ - fluidsynth.fluid_synth_sfload((fluid_synth_t*) data, path, 1); - return 1; + /* If this fails, it's too late to try Timidity so pray that at least one works. */ + fluidsynth.fluid_synth_sfload((fluid_synth_t*) data, path, 1); + return 1; } int fluidsynth_init(SDL_AudioSpec *mixer) { - if (!Mix_EachSoundFont(fluidsynth_check_soundfont, NULL)) - return -1; + if (!Mix_EachSoundFont(fluidsynth_check_soundfont, NULL)) + return -1; - format = mixer->format; - channels = mixer->channels; - freq = mixer->freq; + format = mixer->format; + channels = mixer->channels; + freq = mixer->freq; - return 0; + return 0; } static FluidSynthMidiSong *fluidsynth_loadsong_common(int (*function)(FluidSynthMidiSong*, void*), void *data) { - FluidSynthMidiSong *song; - fluid_settings_t *settings = NULL; - - if (!Mix_Init(MIX_INIT_FLUIDSYNTH)) { - return NULL; - } - - if ((song = SDL_malloc(sizeof(FluidSynthMidiSong)))) { - memset(song, 0, sizeof(FluidSynthMidiSong)); - - if (SDL_BuildAudioCVT(&song->convert, AUDIO_S16, 2, freq, format, channels, freq) >= 0) { - if ((settings = fluidsynth.new_fluid_settings())) { - fluidsynth.fluid_settings_setnum(settings, "synth.sample-rate", (double) freq); - - if ((song->synth = fluidsynth.new_fluid_synth(settings))) { - if (Mix_EachSoundFont(fluidsynth_load_soundfont, (void*) song->synth)) { - if ((song->player = fluidsynth.new_fluid_player(song->synth))) { - if (function(song, data)) return song; - fluidsynth.delete_fluid_player(song->player); - } else { - Mix_SetError("Failed to create FluidSynth player"); - } - } - fluidsynth.delete_fluid_synth(song->synth); - } else { - Mix_SetError("Failed to create FluidSynth synthesizer"); - } - fluidsynth.delete_fluid_settings(settings); - } else { - Mix_SetError("Failed to create FluidSynth settings"); - } - } else { - Mix_SetError("Failed to set up audio conversion"); - } - SDL_free(song); - } else { - Mix_SetError("Insufficient memory for song"); - } - return NULL; + FluidSynthMidiSong *song; + fluid_settings_t *settings = NULL; + + if (!Mix_Init(MIX_INIT_FLUIDSYNTH)) { + return NULL; + } + + if ((song = SDL_malloc(sizeof(FluidSynthMidiSong)))) { + memset(song, 0, sizeof(FluidSynthMidiSong)); + + if (SDL_BuildAudioCVT(&song->convert, AUDIO_S16, 2, freq, format, channels, freq) >= 0) { + if ((settings = fluidsynth.new_fluid_settings())) { + fluidsynth.fluid_settings_setnum(settings, "synth.sample-rate", (double) freq); + + if ((song->synth = fluidsynth.new_fluid_synth(settings))) { + if (Mix_EachSoundFont(fluidsynth_load_soundfont, (void*) song->synth)) { + if ((song->player = fluidsynth.new_fluid_player(song->synth))) { + if (function(song, data)) return song; + fluidsynth.delete_fluid_player(song->player); + } else { + Mix_SetError("Failed to create FluidSynth player"); + } + } + fluidsynth.delete_fluid_synth(song->synth); + } else { + Mix_SetError("Failed to create FluidSynth synthesizer"); + } + fluidsynth.delete_fluid_settings(settings); + } else { + Mix_SetError("Failed to create FluidSynth settings"); + } + } else { + Mix_SetError("Failed to set up audio conversion"); + } + SDL_free(song); + } else { + Mix_SetError("Insufficient memory for song"); + } + return NULL; } static int fluidsynth_loadsong_RW_internal(FluidSynthMidiSong *song, void *data) { - off_t offset; - size_t size; - char *buffer; - SDL_RWops *rw = (SDL_RWops*) data; - - offset = SDL_RWtell(rw); - SDL_RWseek(rw, 0, RW_SEEK_END); - size = SDL_RWtell(rw) - offset; - SDL_RWseek(rw, offset, RW_SEEK_SET); - - if ((buffer = (char*) SDL_malloc(size))) { - if(SDL_RWread(rw, buffer, size, 1) == 1) { - if (fluidsynth.fluid_player_add_mem(song->player, buffer, size) == FLUID_OK) { - return 1; - } else { - Mix_SetError("FluidSynth failed to load in-memory song"); - } - } else { - Mix_SetError("Failed to read in-memory song"); - } - SDL_free(buffer); - } else { - Mix_SetError("Insufficient memory for song"); - } - return 0; + off_t offset; + size_t size; + char *buffer; + SDL_RWops *rw = (SDL_RWops*) data; + + offset = SDL_RWtell(rw); + SDL_RWseek(rw, 0, RW_SEEK_END); + size = SDL_RWtell(rw) - offset; + SDL_RWseek(rw, offset, RW_SEEK_SET); + + if ((buffer = (char*) SDL_malloc(size))) { + if(SDL_RWread(rw, buffer, size, 1) == 1) { + if (fluidsynth.fluid_player_add_mem(song->player, buffer, size) == FLUID_OK) { + return 1; + } else { + Mix_SetError("FluidSynth failed to load in-memory song"); + } + } else { + Mix_SetError("Failed to read in-memory song"); + } + SDL_free(buffer); + } else { + Mix_SetError("Insufficient memory for song"); + } + return 0; } FluidSynthMidiSong *fluidsynth_loadsong_RW(SDL_RWops *rw, int freerw) { - FluidSynthMidiSong *song; + FluidSynthMidiSong *song; - song = fluidsynth_loadsong_common(fluidsynth_loadsong_RW_internal, (void*) rw); - if (freerw) { - SDL_RWclose(rw); - } - return song; + song = fluidsynth_loadsong_common(fluidsynth_loadsong_RW_internal, (void*) rw); + if (freerw) { + SDL_RWclose(rw); + } + return song; } void fluidsynth_freesong(FluidSynthMidiSong *song) { - if (!song) return; - fluidsynth.delete_fluid_player(song->player); - fluidsynth.delete_fluid_settings(fluidsynth.fluid_synth_get_settings(song->synth)); - fluidsynth.delete_fluid_synth(song->synth); - SDL_free(song); + if (!song) return; + fluidsynth.delete_fluid_player(song->player); + fluidsynth.delete_fluid_settings(fluidsynth.fluid_synth_get_settings(song->synth)); + fluidsynth.delete_fluid_synth(song->synth); + SDL_free(song); } void fluidsynth_start(FluidSynthMidiSong *song) { - fluidsynth.fluid_player_set_loop(song->player, 1); - fluidsynth.fluid_player_play(song->player); + fluidsynth.fluid_player_set_loop(song->player, 1); + fluidsynth.fluid_player_play(song->player); } void fluidsynth_stop(FluidSynthMidiSong *song) { - fluidsynth.fluid_player_stop(song->player); + fluidsynth.fluid_player_stop(song->player); } int fluidsynth_active(FluidSynthMidiSong *song) { - return fluidsynth.fluid_player_get_status(song->player) == FLUID_PLAYER_PLAYING ? 1 : 0; + return fluidsynth.fluid_player_get_status(song->player) == FLUID_PLAYER_PLAYING ? 1 : 0; } void fluidsynth_setvolume(FluidSynthMidiSong *song, int volume) { - /* FluidSynth's default is 0.2. Make 1.2 the maximum. */ - fluidsynth.fluid_synth_set_gain(song->synth, (float) (volume * 1.2 / MIX_MAX_VOLUME)); + /* FluidSynth's default is 0.2. Make 1.2 the maximum. */ + fluidsynth.fluid_synth_set_gain(song->synth, (float) (volume * 1.2 / MIX_MAX_VOLUME)); } int fluidsynth_playsome(FluidSynthMidiSong *song, void *dest, int dest_len) { - int result = -1; - int frames = dest_len / channels / ((format & 0xFF) / 8); - int src_len = frames * 4; /* 16-bit stereo */ - void *src = dest; + int result = -1; + int frames = dest_len / channels / ((format & 0xFF) / 8); + int src_len = frames * 4; /* 16-bit stereo */ + void *src = dest; - if (dest_len < src_len) { - if (!(src = SDL_malloc(src_len))) { - Mix_SetError("Insufficient memory for audio conversion"); - return result; - } - } + if (dest_len < src_len) { + if (!(src = SDL_malloc(src_len))) { + Mix_SetError("Insufficient memory for audio conversion"); + return result; + } + } - if (fluidsynth.fluid_synth_write_s16(song->synth, frames, src, 0, 2, src, 1, 2) != FLUID_OK) { - Mix_SetError("Error generating FluidSynth audio"); - goto finish; - } + if (fluidsynth.fluid_synth_write_s16(song->synth, frames, src, 0, 2, src, 1, 2) != FLUID_OK) { + Mix_SetError("Error generating FluidSynth audio"); + goto finish; + } - song->convert.buf = src; - song->convert.len = src_len; + song->convert.buf = src; + song->convert.len = src_len; - if (SDL_ConvertAudio(&song->convert) < 0) { - Mix_SetError("Error during audio conversion"); - goto finish; - } + if (SDL_ConvertAudio(&song->convert) < 0) { + Mix_SetError("Error during audio conversion"); + goto finish; + } - if (src != dest) - memcpy(dest, src, dest_len); + if (src != dest) + memcpy(dest, src, dest_len); - result = 0; + result = 0; finish: - if (src != dest) - SDL_free(src); + if (src != dest) + SDL_free(src); - return result; + return result; } #endif /* USE_FLUIDSYNTH_MIDI */ diff --git a/fluidsynth.h b/fluidsynth.h index 171a5171..dfccd1c7 100644 --- a/fluidsynth.h +++ b/fluidsynth.h @@ -32,9 +32,9 @@ #include typedef struct { - SDL_AudioCVT convert; - fluid_synth_t *synth; - fluid_player_t* player; + SDL_AudioCVT convert; + fluid_synth_t *synth; + fluid_player_t* player; } FluidSynthMidiSong; int fluidsynth_init(SDL_AudioSpec *mixer); diff --git a/load_aiff.c b/load_aiff.c index d3bca96e..ede8c8d0 100644 --- a/load_aiff.c +++ b/load_aiff.c @@ -40,15 +40,15 @@ /*********************************************/ /* Define values for AIFF (IFF audio) format */ /*********************************************/ -#define FORM 0x4d524f46 /* "FORM" */ +#define FORM 0x4d524f46 /* "FORM" */ -#define AIFF 0x46464941 /* "AIFF" */ -#define SSND 0x444e5353 /* "SSND" */ -#define COMM 0x4d4d4f43 /* "COMM" */ +#define AIFF 0x46464941 /* "AIFF" */ +#define SSND 0x444e5353 /* "SSND" */ +#define COMM 0x4d4d4f43 /* "COMM" */ -#define _8SVX 0x58565338 /* "8SVX" */ -#define VHDR 0x52444856 /* "VHDR" */ -#define BODY 0x59444F42 /* "BODY" */ +#define _8SVX 0x58565338 /* "8SVX" */ +#define VHDR 0x52444856 /* "VHDR" */ +#define BODY 0x59444F42 /* "BODY" */ /* This function was taken from libsndfile. I don't pretend to fully * understand it. @@ -56,195 +56,195 @@ static Uint32 SANE_to_Uint32 (Uint8 *sanebuf) { - /* Is the frequency outside of what we can represent with Uint32? */ - if ( (sanebuf[0] & 0x80) || (sanebuf[0] <= 0x3F) || (sanebuf[0] > 0x40) - || (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C) ) - return 0; + /* Is the frequency outside of what we can represent with Uint32? */ + if ( (sanebuf[0] & 0x80) || (sanebuf[0] <= 0x3F) || (sanebuf[0] > 0x40) + || (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C) ) + return 0; - return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7) - | (sanebuf[5] >> 1)) >> (29 - sanebuf[1]); + return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7) + | (sanebuf[5] >> 1)) >> (29 - sanebuf[1]); } /* This function is based on SDL_LoadWAV_RW(). */ SDL_AudioSpec *Mix_LoadAIFF_RW (SDL_RWops *src, int freesrc, - SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len) + SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len) { - int was_error; - int found_SSND; - int found_COMM; - int found_VHDR; - int found_BODY; - long start = 0; - - Uint32 chunk_type; - Uint32 chunk_length; - long next_chunk; - - /* AIFF magic header */ - Uint32 FORMchunk; - Uint32 AIFFmagic; - - /* SSND chunk */ - Uint32 offset; - Uint32 blocksize; - - /* COMM format chunk */ - Uint16 channels = 0; - Uint32 numsamples = 0; - Uint16 samplesize = 0; - Uint8 sane_freq[10]; - Uint32 frequency = 0; - - /* Make sure we are passed a valid data source */ - was_error = 0; - if ( src == NULL ) { - was_error = 1; - goto done; - } - - FORMchunk = SDL_ReadLE32(src); - chunk_length = SDL_ReadBE32(src); - if ( chunk_length == AIFF ) { /* The FORMchunk has already been read */ - AIFFmagic = chunk_length; - chunk_length = FORMchunk; - FORMchunk = FORM; - } else { - AIFFmagic = SDL_ReadLE32(src); - } - if ( (FORMchunk != FORM) || ( (AIFFmagic != AIFF) && (AIFFmagic != _8SVX) ) ) { - SDL_SetError("Unrecognized file type (not AIFF nor 8SVX)"); - was_error = 1; - goto done; - } - - /* TODO: Better santity-checking. */ - - found_SSND = 0; - found_COMM = 0; - found_VHDR = 0; - found_BODY = 0; - - do { - chunk_type = SDL_ReadLE32(src); - chunk_length = SDL_ReadBE32(src); - next_chunk = SDL_RWtell(src) + chunk_length; - /* Paranoia to avoid infinite loops */ - if (chunk_length == 0) - break; - - switch (chunk_type) { - case SSND: - found_SSND = 1; - offset = SDL_ReadBE32(src); - blocksize = SDL_ReadBE32(src); - start = SDL_RWtell(src) + offset; - break; - - case COMM: - found_COMM = 1; - channels = SDL_ReadBE16(src); - numsamples = SDL_ReadBE32(src); - samplesize = SDL_ReadBE16(src); - SDL_RWread(src, sane_freq, sizeof(sane_freq), 1); - frequency = SANE_to_Uint32(sane_freq); - if (frequency == 0) { - SDL_SetError("Bad AIFF sample frequency"); - was_error = 1; - goto done; - } - break; - - case VHDR: - found_VHDR = 1; - SDL_ReadBE32(src); - SDL_ReadBE32(src); - SDL_ReadBE32(src); - frequency = SDL_ReadBE16(src); - channels = 1; - samplesize = 8; - break; - - case BODY: - found_BODY = 1; - numsamples = chunk_length; - start = SDL_RWtell(src); - break; - - default: - break; - } - /* a 0 pad byte can be stored for any odd-length chunk */ - if (chunk_length&1) - next_chunk++; - } while ( ( ( (AIFFmagic == AIFF) && ( !found_SSND || !found_COMM ) ) - || ( (AIFFmagic == _8SVX ) && ( !found_VHDR || !found_BODY ) ) ) - && SDL_RWseek(src, next_chunk, RW_SEEK_SET) != 1 ); - - if ( (AIFFmagic == AIFF) && !found_SSND ) { - SDL_SetError("Bad AIFF (no SSND chunk)"); - was_error = 1; - goto done; - } - - if ( (AIFFmagic == AIFF) && !found_COMM ) { - SDL_SetError("Bad AIFF (no COMM chunk)"); - was_error = 1; - goto done; - } - - if ( (AIFFmagic == _8SVX) && !found_VHDR ) { - SDL_SetError("Bad 8SVX (no VHDR chunk)"); - was_error = 1; - goto done; - } - - if ( (AIFFmagic == _8SVX) && !found_BODY ) { - SDL_SetError("Bad 8SVX (no BODY chunk)"); - was_error = 1; - goto done; - } - - /* Decode the audio data format */ - memset(spec, 0, sizeof(*spec)); - spec->freq = frequency; - switch (samplesize) { - case 8: - spec->format = AUDIO_S8; - break; - case 16: - spec->format = AUDIO_S16MSB; - break; - default: - SDL_SetError("Unsupported AIFF samplesize"); - was_error = 1; - goto done; - } - spec->channels = (Uint8) channels; - spec->samples = 4096; /* Good default buffer size */ - - *audio_len = channels * numsamples * (samplesize / 8); - *audio_buf = (Uint8 *)SDL_malloc(*audio_len); - if ( *audio_buf == NULL ) { - SDL_SetError("Out of memory"); - return(NULL); - } - SDL_RWseek(src, start, RW_SEEK_SET); - if ( SDL_RWread(src, *audio_buf, *audio_len, 1) != 1 ) { - SDL_SetError("Unable to read audio data"); - return(NULL); - } - - /* Don't return a buffer that isn't a multiple of samplesize */ - *audio_len &= ~((samplesize / 8) - 1); + int was_error; + int found_SSND; + int found_COMM; + int found_VHDR; + int found_BODY; + long start = 0; + + Uint32 chunk_type; + Uint32 chunk_length; + long next_chunk; + + /* AIFF magic header */ + Uint32 FORMchunk; + Uint32 AIFFmagic; + + /* SSND chunk */ + Uint32 offset; + Uint32 blocksize; + + /* COMM format chunk */ + Uint16 channels = 0; + Uint32 numsamples = 0; + Uint16 samplesize = 0; + Uint8 sane_freq[10]; + Uint32 frequency = 0; + + /* Make sure we are passed a valid data source */ + was_error = 0; + if ( src == NULL ) { + was_error = 1; + goto done; + } + + FORMchunk = SDL_ReadLE32(src); + chunk_length = SDL_ReadBE32(src); + if ( chunk_length == AIFF ) { /* The FORMchunk has already been read */ + AIFFmagic = chunk_length; + chunk_length = FORMchunk; + FORMchunk = FORM; + } else { + AIFFmagic = SDL_ReadLE32(src); + } + if ( (FORMchunk != FORM) || ( (AIFFmagic != AIFF) && (AIFFmagic != _8SVX) ) ) { + SDL_SetError("Unrecognized file type (not AIFF nor 8SVX)"); + was_error = 1; + goto done; + } + + /* TODO: Better santity-checking. */ + + found_SSND = 0; + found_COMM = 0; + found_VHDR = 0; + found_BODY = 0; + + do { + chunk_type = SDL_ReadLE32(src); + chunk_length = SDL_ReadBE32(src); + next_chunk = SDL_RWtell(src) + chunk_length; + /* Paranoia to avoid infinite loops */ + if (chunk_length == 0) + break; + + switch (chunk_type) { + case SSND: + found_SSND = 1; + offset = SDL_ReadBE32(src); + blocksize = SDL_ReadBE32(src); + start = SDL_RWtell(src) + offset; + break; + + case COMM: + found_COMM = 1; + channels = SDL_ReadBE16(src); + numsamples = SDL_ReadBE32(src); + samplesize = SDL_ReadBE16(src); + SDL_RWread(src, sane_freq, sizeof(sane_freq), 1); + frequency = SANE_to_Uint32(sane_freq); + if (frequency == 0) { + SDL_SetError("Bad AIFF sample frequency"); + was_error = 1; + goto done; + } + break; + + case VHDR: + found_VHDR = 1; + SDL_ReadBE32(src); + SDL_ReadBE32(src); + SDL_ReadBE32(src); + frequency = SDL_ReadBE16(src); + channels = 1; + samplesize = 8; + break; + + case BODY: + found_BODY = 1; + numsamples = chunk_length; + start = SDL_RWtell(src); + break; + + default: + break; + } + /* a 0 pad byte can be stored for any odd-length chunk */ + if (chunk_length&1) + next_chunk++; + } while ( ( ( (AIFFmagic == AIFF) && ( !found_SSND || !found_COMM ) ) + || ( (AIFFmagic == _8SVX ) && ( !found_VHDR || !found_BODY ) ) ) + && SDL_RWseek(src, next_chunk, RW_SEEK_SET) != 1 ); + + if ( (AIFFmagic == AIFF) && !found_SSND ) { + SDL_SetError("Bad AIFF (no SSND chunk)"); + was_error = 1; + goto done; + } + + if ( (AIFFmagic == AIFF) && !found_COMM ) { + SDL_SetError("Bad AIFF (no COMM chunk)"); + was_error = 1; + goto done; + } + + if ( (AIFFmagic == _8SVX) && !found_VHDR ) { + SDL_SetError("Bad 8SVX (no VHDR chunk)"); + was_error = 1; + goto done; + } + + if ( (AIFFmagic == _8SVX) && !found_BODY ) { + SDL_SetError("Bad 8SVX (no BODY chunk)"); + was_error = 1; + goto done; + } + + /* Decode the audio data format */ + memset(spec, 0, sizeof(*spec)); + spec->freq = frequency; + switch (samplesize) { + case 8: + spec->format = AUDIO_S8; + break; + case 16: + spec->format = AUDIO_S16MSB; + break; + default: + SDL_SetError("Unsupported AIFF samplesize"); + was_error = 1; + goto done; + } + spec->channels = (Uint8) channels; + spec->samples = 4096; /* Good default buffer size */ + + *audio_len = channels * numsamples * (samplesize / 8); + *audio_buf = (Uint8 *)SDL_malloc(*audio_len); + if ( *audio_buf == NULL ) { + SDL_SetError("Out of memory"); + return(NULL); + } + SDL_RWseek(src, start, RW_SEEK_SET); + if ( SDL_RWread(src, *audio_buf, *audio_len, 1) != 1 ) { + SDL_SetError("Unable to read audio data"); + return(NULL); + } + + /* Don't return a buffer that isn't a multiple of samplesize */ + *audio_len &= ~((samplesize / 8) - 1); done: - if ( freesrc && src ) { - SDL_RWclose(src); - } - if ( was_error ) { - spec = NULL; - } - return(spec); + if ( freesrc && src ) { + SDL_RWclose(src); + } + if ( was_error ) { + spec = NULL; + } + return(spec); } diff --git a/load_aiff.h b/load_aiff.h index ed55d364..3dd85a4b 100644 --- a/load_aiff.h +++ b/load_aiff.h @@ -28,4 +28,4 @@ /* Don't call this directly; use Mix_LoadWAV_RW() for now. */ SDL_AudioSpec *Mix_LoadAIFF_RW (SDL_RWops *src, int freesrc, - SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len); + SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len); diff --git a/load_flac.c b/load_flac.c index a9a93f37..e8bfbaa0 100644 --- a/load_flac.c +++ b/load_flac.c @@ -19,7 +19,7 @@ 3. This notice may not be removed or altered from any source distribution. This is the source needed to decode a FLAC into a waveform. - ~ Austen Dicken (admin@cvpcs.org). + ~ Austen Dicken (admin@cvpcs.org). */ #ifdef FLAC_MUSIC @@ -39,299 +39,299 @@ #include typedef struct { - SDL_RWops* sdl_src; - SDL_AudioSpec* sdl_spec; - Uint8** sdl_audio_buf; - Uint32* sdl_audio_len; - int sdl_audio_read; - FLAC__uint64 flac_total_samples; - unsigned flac_bps; + SDL_RWops* sdl_src; + SDL_AudioSpec* sdl_spec; + Uint8** sdl_audio_buf; + Uint32* sdl_audio_len; + int sdl_audio_read; + FLAC__uint64 flac_total_samples; + unsigned flac_bps; } FLAC_SDL_Data; static FLAC__StreamDecoderReadStatus flac_read_load_cb( - const FLAC__StreamDecoder *decoder, - FLAC__byte buffer[], - size_t *bytes, - void *client_data) + const FLAC__StreamDecoder *decoder, + FLAC__byte buffer[], + size_t *bytes, + void *client_data) { - // make sure there is something to be reading - if (*bytes > 0) { - FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; - - *bytes = SDL_RWread (data->sdl_src, buffer, sizeof (FLAC__byte), - *bytes); - - if (*bytes == 0) { // error or no data was read (EOF) - return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; - } - else { // data was read, continue - return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; - } - } - else { - return FLAC__STREAM_DECODER_READ_STATUS_ABORT; - } + // make sure there is something to be reading + if (*bytes > 0) { + FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; + + *bytes = SDL_RWread (data->sdl_src, buffer, sizeof (FLAC__byte), + *bytes); + + if (*bytes == 0) { // error or no data was read (EOF) + return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; + } + else { // data was read, continue + return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; + } + } + else { + return FLAC__STREAM_DECODER_READ_STATUS_ABORT; + } } static FLAC__StreamDecoderSeekStatus flac_seek_load_cb( - const FLAC__StreamDecoder *decoder, - FLAC__uint64 absolute_byte_offset, - void *client_data) + const FLAC__StreamDecoder *decoder, + FLAC__uint64 absolute_byte_offset, + void *client_data) { - FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; - - if (SDL_RWseek (data->sdl_src, absolute_byte_offset, RW_SEEK_SET) < 0) { - return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; - } - else { - return FLAC__STREAM_DECODER_SEEK_STATUS_OK; - } + FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; + + if (SDL_RWseek (data->sdl_src, absolute_byte_offset, RW_SEEK_SET) < 0) { + return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; + } + else { + return FLAC__STREAM_DECODER_SEEK_STATUS_OK; + } } static FLAC__StreamDecoderTellStatus flac_tell_load_cb( - const FLAC__StreamDecoder *decoder, - FLAC__uint64 *absolute_byte_offset, - void *client_data) + const FLAC__StreamDecoder *decoder, + FLAC__uint64 *absolute_byte_offset, + void *client_data) { - FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; + FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; - int pos = SDL_RWtell (data->sdl_src); + int pos = SDL_RWtell (data->sdl_src); - if (pos < 0) { - return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; - } - else { - *absolute_byte_offset = (FLAC__uint64)pos; - return FLAC__STREAM_DECODER_TELL_STATUS_OK; - } + if (pos < 0) { + return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; + } + else { + *absolute_byte_offset = (FLAC__uint64)pos; + return FLAC__STREAM_DECODER_TELL_STATUS_OK; + } } static FLAC__StreamDecoderLengthStatus flac_length_load_cb( - const FLAC__StreamDecoder *decoder, - FLAC__uint64 *stream_length, - void *client_data) + const FLAC__StreamDecoder *decoder, + FLAC__uint64 *stream_length, + void *client_data) { - FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; - - int pos = SDL_RWtell (data->sdl_src); - int length = SDL_RWseek (data->sdl_src, 0, RW_SEEK_END); - - if (SDL_RWseek (data->sdl_src, pos, RW_SEEK_SET) != pos || length < 0) { - /* there was an error attempting to return the stream to the original - * position, or the length was invalid. */ - return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; - } - else { - *stream_length = (FLAC__uint64)length; - return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; - } + FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; + + int pos = SDL_RWtell (data->sdl_src); + int length = SDL_RWseek (data->sdl_src, 0, RW_SEEK_END); + + if (SDL_RWseek (data->sdl_src, pos, RW_SEEK_SET) != pos || length < 0) { + /* there was an error attempting to return the stream to the original + * position, or the length was invalid. */ + return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; + } + else { + *stream_length = (FLAC__uint64)length; + return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; + } } static FLAC__bool flac_eof_load_cb(const FLAC__StreamDecoder *decoder, - void *client_data) + void *client_data) { - FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; - - int pos = SDL_RWtell (data->sdl_src); - int end = SDL_RWseek (data->sdl_src, 0, RW_SEEK_END); - - // was the original position equal to the end (a.k.a. the seek didn't move)? - if (pos == end) { - // must be EOF - return true; - } - else { - // not EOF, return to the original position - SDL_RWseek (data->sdl_src, pos, RW_SEEK_SET); - - return false; - } + FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; + + int pos = SDL_RWtell (data->sdl_src); + int end = SDL_RWseek (data->sdl_src, 0, RW_SEEK_END); + + // was the original position equal to the end (a.k.a. the seek didn't move)? + if (pos == end) { + // must be EOF + return true; + } + else { + // not EOF, return to the original position + SDL_RWseek (data->sdl_src, pos, RW_SEEK_SET); + + return false; + } } static FLAC__StreamDecoderWriteStatus flac_write_load_cb( - const FLAC__StreamDecoder *decoder, - const FLAC__Frame *frame, - const FLAC__int32 *const buffer[], - void *client_data) + const FLAC__StreamDecoder *decoder, + const FLAC__Frame *frame, + const FLAC__int32 *const buffer[], + void *client_data) { - FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; - size_t i; - Uint8 *buf; - - if (data->flac_total_samples == 0) { - SDL_SetError ("Given FLAC file does not specify its sample count."); - return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; - } - - if (data->sdl_spec->channels != 2 || data->flac_bps != 16) { - SDL_SetError ("Current FLAC support is only for 16 bit Stereo files."); - return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; - } - - // check if it is the first audio frame so we can initialize the output - // buffer - if (frame->header.number.sample_number == 0) { - *(data->sdl_audio_len) = data->sdl_spec->size; - data->sdl_audio_read = 0; - *(data->sdl_audio_buf) = SDL_malloc (*(data->sdl_audio_len)); - - if (*(data->sdl_audio_buf) == NULL) { - SDL_SetError - ("Unable to allocate memory to store the FLAC stream."); - return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; - } - } - - buf = *(data->sdl_audio_buf); - - for (i = 0; i < frame->header.blocksize; i++) { - FLAC__int16 i16; - FLAC__uint16 ui16; - - i16 = (FLAC__int16)buffer[0][i]; - ui16 = (FLAC__uint16)i16; - - *(buf + (data->sdl_audio_read++)) = (char)(ui16); - *(buf + (data->sdl_audio_read++)) = (char)(ui16 >> 8); - - i16 = (FLAC__int16)buffer[1][i]; - ui16 = (FLAC__uint16)i16; - - *(buf + (data->sdl_audio_read++)) = (char)(ui16); - *(buf + (data->sdl_audio_read++)) = (char)(ui16 >> 8); - } - - return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; + FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; + size_t i; + Uint8 *buf; + + if (data->flac_total_samples == 0) { + SDL_SetError ("Given FLAC file does not specify its sample count."); + return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; + } + + if (data->sdl_spec->channels != 2 || data->flac_bps != 16) { + SDL_SetError ("Current FLAC support is only for 16 bit Stereo files."); + return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; + } + + // check if it is the first audio frame so we can initialize the output + // buffer + if (frame->header.number.sample_number == 0) { + *(data->sdl_audio_len) = data->sdl_spec->size; + data->sdl_audio_read = 0; + *(data->sdl_audio_buf) = SDL_malloc (*(data->sdl_audio_len)); + + if (*(data->sdl_audio_buf) == NULL) { + SDL_SetError + ("Unable to allocate memory to store the FLAC stream."); + return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; + } + } + + buf = *(data->sdl_audio_buf); + + for (i = 0; i < frame->header.blocksize; i++) { + FLAC__int16 i16; + FLAC__uint16 ui16; + + i16 = (FLAC__int16)buffer[0][i]; + ui16 = (FLAC__uint16)i16; + + *(buf + (data->sdl_audio_read++)) = (char)(ui16); + *(buf + (data->sdl_audio_read++)) = (char)(ui16 >> 8); + + i16 = (FLAC__int16)buffer[1][i]; + ui16 = (FLAC__uint16)i16; + + *(buf + (data->sdl_audio_read++)) = (char)(ui16); + *(buf + (data->sdl_audio_read++)) = (char)(ui16 >> 8); + } + + return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; } static void flac_metadata_load_cb( - const FLAC__StreamDecoder *decoder, - const FLAC__StreamMetadata *metadata, - void *client_data) + const FLAC__StreamDecoder *decoder, + const FLAC__StreamMetadata *metadata, + void *client_data) { - FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; - FLAC__uint64 total_samples; - unsigned bps; - - if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) { - // save the metadata right now for use later on - *(data->sdl_audio_buf) = NULL; - *(data->sdl_audio_len) = 0; - memset (data->sdl_spec, '\0', sizeof (SDL_AudioSpec)); - - data->sdl_spec->format = AUDIO_S16; - data->sdl_spec->freq = (int)(metadata->data.stream_info.sample_rate); - data->sdl_spec->channels = (Uint8)(metadata->data.stream_info.channels); - data->sdl_spec->samples = 8192; /* buffer size */ - - total_samples = metadata->data.stream_info.total_samples; - bps = metadata->data.stream_info.bits_per_sample; - - data->sdl_spec->size = total_samples * data->sdl_spec->channels * - (bps / 8); - data->flac_total_samples = total_samples; - data->flac_bps = bps; - } + FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data; + FLAC__uint64 total_samples; + unsigned bps; + + if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) { + // save the metadata right now for use later on + *(data->sdl_audio_buf) = NULL; + *(data->sdl_audio_len) = 0; + memset (data->sdl_spec, '\0', sizeof (SDL_AudioSpec)); + + data->sdl_spec->format = AUDIO_S16; + data->sdl_spec->freq = (int)(metadata->data.stream_info.sample_rate); + data->sdl_spec->channels = (Uint8)(metadata->data.stream_info.channels); + data->sdl_spec->samples = 8192; /* buffer size */ + + total_samples = metadata->data.stream_info.total_samples; + bps = metadata->data.stream_info.bits_per_sample; + + data->sdl_spec->size = total_samples * data->sdl_spec->channels * + (bps / 8); + data->flac_total_samples = total_samples; + data->flac_bps = bps; + } } static void flac_error_load_cb( - const FLAC__StreamDecoder *decoder, - FLAC__StreamDecoderErrorStatus status, - void *client_data) + const FLAC__StreamDecoder *decoder, + FLAC__StreamDecoderErrorStatus status, + void *client_data) { - // print an SDL error based on the error status - switch (status) { - case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC: - SDL_SetError ("Error processing the FLAC file [LOST_SYNC]."); - break; - case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER: - SDL_SetError ("Error processing the FLAC file [BAD_HEADER]."); - break; - case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH: - SDL_SetError ("Error processing the FLAC file [CRC_MISMATCH]."); - break; - case FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM: - SDL_SetError ("Error processing the FLAC file [UNPARSEABLE]."); - break; - default: - SDL_SetError ("Error processing the FLAC file [UNKNOWN]."); - break; - } + // print an SDL error based on the error status + switch (status) { + case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC: + SDL_SetError ("Error processing the FLAC file [LOST_SYNC]."); + break; + case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER: + SDL_SetError ("Error processing the FLAC file [BAD_HEADER]."); + break; + case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH: + SDL_SetError ("Error processing the FLAC file [CRC_MISMATCH]."); + break; + case FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM: + SDL_SetError ("Error processing the FLAC file [UNPARSEABLE]."); + break; + default: + SDL_SetError ("Error processing the FLAC file [UNKNOWN]."); + break; + } } /* don't call this directly; use Mix_LoadWAV_RW() for now. */ SDL_AudioSpec *Mix_LoadFLAC_RW (SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len) { - FLAC__StreamDecoder *decoder = 0; - FLAC__StreamDecoderInitStatus init_status; - int was_error = 1; - int was_init = 0; - Uint32 samplesize; + FLAC__StreamDecoder *decoder = 0; + FLAC__StreamDecoderInitStatus init_status; + int was_error = 1; + int was_init = 0; + Uint32 samplesize; - // create the client data passing information - FLAC_SDL_Data* client_data; - client_data = (FLAC_SDL_Data *)SDL_malloc (sizeof (FLAC_SDL_Data)); + // create the client data passing information + FLAC_SDL_Data* client_data; + client_data = (FLAC_SDL_Data *)SDL_malloc (sizeof (FLAC_SDL_Data)); - if ((!src) || (!audio_buf) || (!audio_len)) /* sanity checks. */ - goto done; + if ((!src) || (!audio_buf) || (!audio_len)) /* sanity checks. */ + goto done; - if (!Mix_Init(MIX_INIT_FLAC)) - goto done; + if (!Mix_Init(MIX_INIT_FLAC)) + goto done; - if ((decoder = flac.FLAC__stream_decoder_new ()) == NULL) { - SDL_SetError ("Unable to allocate FLAC decoder."); - goto done; - } + if ((decoder = flac.FLAC__stream_decoder_new ()) == NULL) { + SDL_SetError ("Unable to allocate FLAC decoder."); + goto done; + } - init_status = flac.FLAC__stream_decoder_init_stream (decoder, - flac_read_load_cb, flac_seek_load_cb, - flac_tell_load_cb, flac_length_load_cb, - flac_eof_load_cb, flac_write_load_cb, - flac_metadata_load_cb, flac_error_load_cb, - client_data); + init_status = flac.FLAC__stream_decoder_init_stream (decoder, + flac_read_load_cb, flac_seek_load_cb, + flac_tell_load_cb, flac_length_load_cb, + flac_eof_load_cb, flac_write_load_cb, + flac_metadata_load_cb, flac_error_load_cb, + client_data); - if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) { - SDL_SetError ("Unable to initialize FLAC stream decoder."); - goto done; - } + if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) { + SDL_SetError ("Unable to initialize FLAC stream decoder."); + goto done; + } - was_init = 1; + was_init = 1; - client_data->sdl_src = src; - client_data->sdl_spec = spec; - client_data->sdl_audio_buf = audio_buf; - client_data->sdl_audio_len = audio_len; + client_data->sdl_src = src; + client_data->sdl_spec = spec; + client_data->sdl_audio_buf = audio_buf; + client_data->sdl_audio_len = audio_len; - if (!flac.FLAC__stream_decoder_process_until_end_of_stream (decoder)) { - SDL_SetError ("Unable to process FLAC file."); - goto done; - } + if (!flac.FLAC__stream_decoder_process_until_end_of_stream (decoder)) { + SDL_SetError ("Unable to process FLAC file."); + goto done; + } - was_error = 0; + was_error = 0; - /* Don't return a buffer that isn't a multiple of samplesize */ - samplesize = ((spec->format & 0xFF) / 8) * spec->channels; - *audio_len &= ~(samplesize - 1); + /* Don't return a buffer that isn't a multiple of samplesize */ + samplesize = ((spec->format & 0xFF) / 8) * spec->channels; + *audio_len &= ~(samplesize - 1); done: - if (was_init && decoder) { - flac.FLAC__stream_decoder_finish (decoder); - } - - if (decoder) { - flac.FLAC__stream_decoder_delete (decoder); - } - - if (src) { - if (freesrc) - SDL_RWclose (src); - else - SDL_RWseek (src, 0, RW_SEEK_SET); - } - - if (was_error) - spec = NULL; + if (was_init && decoder) { + flac.FLAC__stream_decoder_finish (decoder); + } + + if (decoder) { + flac.FLAC__stream_decoder_delete (decoder); + } + + if (src) { + if (freesrc) + SDL_RWclose (src); + else + SDL_RWseek (src, 0, RW_SEEK_SET); + } + + if (was_error) + spec = NULL; return spec; } diff --git a/load_flac.h b/load_flac.h index 1b8fb51c..f71f31eb 100644 --- a/load_flac.h +++ b/load_flac.h @@ -19,7 +19,7 @@ 3. This notice may not be removed or altered from any source distribution. This is the source needed to decode a FLAC into a waveform. - ~ Austen Dicken (admin@cvpcs.org). + ~ Austen Dicken (admin@cvpcs.org). */ /* $Id: $ */ @@ -27,5 +27,5 @@ #ifdef FLAC_MUSIC /* Don't call this directly; use Mix_LoadWAV_RW() for now. */ SDL_AudioSpec *Mix_LoadFLAC_RW (SDL_RWops *src, int freesrc, - SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len); + SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len); #endif diff --git a/load_ogg.c b/load_ogg.c index 38305201..2cca3584 100644 --- a/load_ogg.c +++ b/load_ogg.c @@ -78,7 +78,7 @@ SDL_AudioSpec *Mix_LoadOGG_RW (SDL_RWops *src, int freesrc, int read, to_read; int must_close = 1; int was_error = 1; - + if ( (!src) || (!audio_buf) || (!audio_len) ) /* sanity checks. */ goto done; @@ -88,7 +88,7 @@ SDL_AudioSpec *Mix_LoadOGG_RW (SDL_RWops *src, int freesrc, callbacks.read_func = sdl_read_func; callbacks.seek_func = sdl_seek_func; callbacks.tell_func = sdl_tell_func; - callbacks.close_func = freesrc ? + callbacks.close_func = freesrc ? sdl_close_func_freesrc : sdl_close_func_nofreesrc; if (vorbis.ov_open_callbacks(src, &vf, NULL, 0, callbacks) != 0) @@ -98,9 +98,9 @@ SDL_AudioSpec *Mix_LoadOGG_RW (SDL_RWops *src, int freesrc, } must_close = 0; - + info = vorbis.ov_info(&vf, -1); - + *audio_buf = NULL; *audio_len = 0; memset(spec, '\0', sizeof (SDL_AudioSpec)); @@ -109,7 +109,7 @@ SDL_AudioSpec *Mix_LoadOGG_RW (SDL_RWops *src, int freesrc, spec->channels = info->channels; spec->freq = info->rate; spec->samples = 4096; /* buffer size */ - + samples = (long)vorbis.ov_pcm_total(&vf, -1); *audio_len = spec->size = samples * spec->channels * 2; @@ -121,17 +121,17 @@ SDL_AudioSpec *Mix_LoadOGG_RW (SDL_RWops *src, int freesrc, to_read = *audio_len; #ifdef OGG_USE_TREMOR for (read = vorbis.ov_read(&vf, (char *)buf, to_read, &bitstream); - read > 0; - read = vorbis.ov_read(&vf, (char *)buf, to_read, &bitstream)) + read > 0; + read = vorbis.ov_read(&vf, (char *)buf, to_read, &bitstream)) #else for (read = vorbis.ov_read(&vf, (char *)buf, to_read, 0/*LE*/, 2/*16bit*/, 1/*signed*/, &bitstream); read > 0; read = vorbis.ov_read(&vf, (char *)buf, to_read, 0, 2, 1, &bitstream)) -#endif +#endif { if (read == OV_HOLE || read == OV_EBADLINK) break; /* error */ - + to_read -= read; buf += read; } diff --git a/load_voc.c b/load_voc.c index b1679021..c6390aa0 100644 --- a/load_voc.c +++ b/load_voc.c @@ -44,48 +44,48 @@ /* Private data for VOC file */ typedef struct vocstuff { - Uint32 rest; /* bytes remaining in current block */ - Uint32 rate; /* rate code (byte) of this chunk */ - int silent; /* sound or silence? */ - Uint32 srate; /* rate code (byte) of silence */ - Uint32 blockseek; /* start of current output block */ - Uint32 samples; /* number of samples output */ - Uint32 size; /* word length of data */ - Uint8 channels; /* number of sound channels */ - int has_extended; /* Has an extended block been read? */ + Uint32 rest; /* bytes remaining in current block */ + Uint32 rate; /* rate code (byte) of this chunk */ + int silent; /* sound or silence? */ + Uint32 srate; /* rate code (byte) of silence */ + Uint32 blockseek; /* start of current output block */ + Uint32 samples; /* number of samples output */ + Uint32 size; /* word length of data */ + Uint8 channels; /* number of sound channels */ + int has_extended; /* Has an extended block been read? */ } vs_t; -/* Size field */ +/* Size field */ /* SJB: note that the 1st 3 are sometimes used as sizeof(type) */ -#define ST_SIZE_BYTE 1 -#define ST_SIZE_8BIT 1 -#define ST_SIZE_WORD 2 -#define ST_SIZE_16BIT 2 -#define ST_SIZE_DWORD 4 -#define ST_SIZE_32BIT 4 -#define ST_SIZE_FLOAT 5 -#define ST_SIZE_DOUBLE 6 -#define ST_SIZE_IEEE 7 /* IEEE 80-bit floats. */ +#define ST_SIZE_BYTE 1 +#define ST_SIZE_8BIT 1 +#define ST_SIZE_WORD 2 +#define ST_SIZE_16BIT 2 +#define ST_SIZE_DWORD 4 +#define ST_SIZE_32BIT 4 +#define ST_SIZE_FLOAT 5 +#define ST_SIZE_DOUBLE 6 +#define ST_SIZE_IEEE 7 /* IEEE 80-bit floats. */ /* Style field */ -#define ST_ENCODING_UNSIGNED 1 /* unsigned linear: Sound Blaster */ -#define ST_ENCODING_SIGN2 2 /* signed linear 2's comp: Mac */ -#define ST_ENCODING_ULAW 3 /* U-law signed logs: US telephony, SPARC */ -#define ST_ENCODING_ALAW 4 /* A-law signed logs: non-US telephony */ -#define ST_ENCODING_ADPCM 5 /* Compressed PCM */ -#define ST_ENCODING_IMA_ADPCM 6 /* Compressed PCM */ -#define ST_ENCODING_GSM 7 /* GSM 6.10 33-byte frame lossy compression */ - -#define VOC_TERM 0 -#define VOC_DATA 1 -#define VOC_CONT 2 -#define VOC_SILENCE 3 -#define VOC_MARKER 4 -#define VOC_TEXT 5 -#define VOC_LOOP 6 -#define VOC_LOOPEND 7 +#define ST_ENCODING_UNSIGNED 1 /* unsigned linear: Sound Blaster */ +#define ST_ENCODING_SIGN2 2 /* signed linear 2's comp: Mac */ +#define ST_ENCODING_ULAW 3 /* U-law signed logs: US telephony, SPARC */ +#define ST_ENCODING_ALAW 4 /* A-law signed logs: non-US telephony */ +#define ST_ENCODING_ADPCM 5 /* Compressed PCM */ +#define ST_ENCODING_IMA_ADPCM 6 /* Compressed PCM */ +#define ST_ENCODING_GSM 7 /* GSM 6.10 33-byte frame lossy compression */ + +#define VOC_TERM 0 +#define VOC_DATA 1 +#define VOC_CONT 2 +#define VOC_SILENCE 3 +#define VOC_MARKER 4 +#define VOC_TEXT 5 +#define VOC_LOOP 6 +#define VOC_LOOPEND 7 #define VOC_EXTENDED 8 -#define VOC_DATA_16 9 +#define VOC_DATA_16 9 static int voc_check_header(SDL_RWops *src) @@ -140,7 +140,7 @@ static int voc_get_block(SDL_RWops *src, vs_t *v, SDL_AudioSpec *spec) if (SDL_RWread(src, bits24, sizeof (bits24), 1) != 1) return 1; /* assume that's the end of the file. */ - + /* Size is an 24-bit value. Ugh. */ sblen = ( (bits24[0]) | (bits24[1] << 8) | (bits24[2] << 16) ); diff --git a/mixer.c b/mixer.c index 46d78c3c..6b01dd2c 100644 --- a/mixer.c +++ b/mixer.c @@ -43,40 +43,40 @@ #include "effects_internal.h" /* Magic numbers for various audio file formats */ -#define RIFF 0x46464952 /* "RIFF" */ -#define WAVE 0x45564157 /* "WAVE" */ -#define FORM 0x4d524f46 /* "FORM" */ -#define OGGS 0x5367674f /* "OggS" */ -#define CREA 0x61657243 /* "Crea" */ -#define FLAC 0x43614C66 /* "fLaC" */ +#define RIFF 0x46464952 /* "RIFF" */ +#define WAVE 0x45564157 /* "WAVE" */ +#define FORM 0x4d524f46 /* "FORM" */ +#define OGGS 0x5367674f /* "OggS" */ +#define CREA 0x61657243 /* "Crea" */ +#define FLAC 0x43614C66 /* "fLaC" */ static int audio_opened = 0; static SDL_AudioSpec mixer; typedef struct _Mix_effectinfo { - Mix_EffectFunc_t callback; - Mix_EffectDone_t done_callback; - void *udata; - struct _Mix_effectinfo *next; + Mix_EffectFunc_t callback; + Mix_EffectDone_t done_callback; + void *udata; + struct _Mix_effectinfo *next; } effect_info; static struct _Mix_Channel { - Mix_Chunk *chunk; - int playing; - int paused; - Uint8 *samples; - int volume; - int looping; - int tag; - Uint32 expire; - Uint32 start_time; - Mix_Fading fading; - int fade_volume; - int fade_volume_reset; - Uint32 fade_length; - Uint32 ticks_fade; - effect_info *effects; + Mix_Chunk *chunk; + int playing; + int paused; + Uint8 *samples; + int volume; + int looping; + int tag; + Uint32 expire; + Uint32 start_time; + Mix_Fading fading; + int fade_volume; + int fade_volume_reset; + Uint32 fade_length; + Uint32 ticks_fade; + effect_info *effects; } *mix_channel = NULL; static effect_info *posteffects = NULL; @@ -113,124 +113,124 @@ extern char* soundfont_paths; int Mix_GetNumChunkDecoders(void) { - return(num_decoders); + return(num_decoders); } const char *Mix_GetChunkDecoder(int index) { - if ((index < 0) || (index >= num_decoders)) { - return NULL; - } - return(chunk_decoders[index]); + if ((index < 0) || (index >= num_decoders)) { + return NULL; + } + return(chunk_decoders[index]); } static void add_chunk_decoder(const char *decoder) { - void *ptr = SDL_realloc(chunk_decoders, (num_decoders + 1) * sizeof (const char **)); - if (ptr == NULL) { - return; /* oh well, go on without it. */ - } - chunk_decoders = (const char **) ptr; - chunk_decoders[num_decoders++] = decoder; + void *ptr = SDL_realloc(chunk_decoders, (num_decoders + 1) * sizeof (const char **)); + if (ptr == NULL) { + return; /* oh well, go on without it. */ + } + chunk_decoders = (const char **) ptr; + chunk_decoders[num_decoders++] = decoder; } /* rcg06192001 get linked library's version. */ const SDL_version *Mix_Linked_Version(void) { - static SDL_version linked_version; - SDL_MIXER_VERSION(&linked_version); - return(&linked_version); + static SDL_version linked_version; + SDL_MIXER_VERSION(&linked_version); + return(&linked_version); } static int initialized = 0; int Mix_Init(int flags) { - int result = 0; + int result = 0; - if (flags & MIX_INIT_FLUIDSYNTH) { + if (flags & MIX_INIT_FLUIDSYNTH) { #ifdef USE_FLUIDSYNTH_MIDI - if ((initialized & MIX_INIT_FLUIDSYNTH) || Mix_InitFluidSynth() == 0) { - result |= MIX_INIT_FLUIDSYNTH; - } + if ((initialized & MIX_INIT_FLUIDSYNTH) || Mix_InitFluidSynth() == 0) { + result |= MIX_INIT_FLUIDSYNTH; + } #else - Mix_SetError("Mixer not built with FluidSynth support"); + Mix_SetError("Mixer not built with FluidSynth support"); #endif - } - if (flags & MIX_INIT_FLAC) { + } + if (flags & MIX_INIT_FLAC) { #ifdef FLAC_MUSIC - if ((initialized & MIX_INIT_FLAC) || Mix_InitFLAC() == 0) { - result |= MIX_INIT_FLAC; - } + if ((initialized & MIX_INIT_FLAC) || Mix_InitFLAC() == 0) { + result |= MIX_INIT_FLAC; + } #else - Mix_SetError("Mixer not built with FLAC support"); + Mix_SetError("Mixer not built with FLAC support"); #endif - } - if (flags & MIX_INIT_MOD) { + } + if (flags & MIX_INIT_MOD) { #ifdef MOD_MUSIC - if ((initialized & MIX_INIT_MOD) || Mix_InitMOD() == 0) { - result |= MIX_INIT_MOD; - } + if ((initialized & MIX_INIT_MOD) || Mix_InitMOD() == 0) { + result |= MIX_INIT_MOD; + } #else - Mix_SetError("Mixer not built with MOD support"); + Mix_SetError("Mixer not built with MOD support"); #endif - } - if (flags & MIX_INIT_MP3) { + } + if (flags & MIX_INIT_MP3) { #ifdef MP3_MUSIC - if ((initialized & MIX_INIT_MP3) || Mix_InitMP3() == 0) { - result |= MIX_INIT_MP3; - } + if ((initialized & MIX_INIT_MP3) || Mix_InitMP3() == 0) { + result |= MIX_INIT_MP3; + } #else - Mix_SetError("Mixer not built with MP3 support"); + Mix_SetError("Mixer not built with MP3 support"); #endif - } - if (flags & MIX_INIT_OGG) { + } + if (flags & MIX_INIT_OGG) { #ifdef OGG_MUSIC - if ((initialized & MIX_INIT_OGG) || Mix_InitOgg() == 0) { - result |= MIX_INIT_OGG; - } + if ((initialized & MIX_INIT_OGG) || Mix_InitOgg() == 0) { + result |= MIX_INIT_OGG; + } #else - Mix_SetError("Mixer not built with Ogg Vorbis support"); + Mix_SetError("Mixer not built with Ogg Vorbis support"); #endif - } - initialized |= result; + } + initialized |= result; - return (result); + return (result); } void Mix_Quit() { #ifdef USE_FLUIDSYNTH_MIDI - if (initialized & MIX_INIT_FLUIDSYNTH) { - Mix_QuitFluidSynth(); - } + if (initialized & MIX_INIT_FLUIDSYNTH) { + Mix_QuitFluidSynth(); + } #endif #ifdef FLAC_MUSIC - if (initialized & MIX_INIT_FLAC) { - Mix_QuitFLAC(); - } + if (initialized & MIX_INIT_FLAC) { + Mix_QuitFLAC(); + } #endif #ifdef MOD_MUSIC - if (initialized & MIX_INIT_MOD) { - Mix_QuitMOD(); - } + if (initialized & MIX_INIT_MOD) { + Mix_QuitMOD(); + } #endif #ifdef MP3_MUSIC - if (initialized & MIX_INIT_MP3) { - Mix_QuitMP3(); - } + if (initialized & MIX_INIT_MP3) { + Mix_QuitMP3(); + } #endif #ifdef OGG_MUSIC - if (initialized & MIX_INIT_OGG) { - Mix_QuitOgg(); - } + if (initialized & MIX_INIT_OGG) { + Mix_QuitOgg(); + } #endif #ifdef MID_MUSIC - if (soundfont_paths) { - SDL_free(soundfont_paths); - } + if (soundfont_paths) { + SDL_free(soundfont_paths); + } #endif - initialized = 0; + initialized = 0; } static int _Mix_remove_all_effects(int channel, effect_info **e); @@ -242,163 +242,163 @@ static int _Mix_remove_all_effects(int channel, effect_info **e); */ static void _Mix_channel_done_playing(int channel) { - if (channel_done_callback) { - channel_done_callback(channel); - } - - /* - * Call internal function directly, to avoid locking audio from - * inside audio callback. - */ - _Mix_remove_all_effects(channel, &mix_channel[channel].effects); + if (channel_done_callback) { + channel_done_callback(channel); + } + + /* + * Call internal function directly, to avoid locking audio from + * inside audio callback. + */ + _Mix_remove_all_effects(channel, &mix_channel[channel].effects); } static void *Mix_DoEffects(int chan, void *snd, int len) { - int posteffect = (chan == MIX_CHANNEL_POST); - effect_info *e = ((posteffect) ? posteffects : mix_channel[chan].effects); - void *buf = snd; - - if (e != NULL) { /* are there any registered effects? */ - /* if this is the postmix, we can just overwrite the original. */ - if (!posteffect) { - buf = SDL_malloc(len); - if (buf == NULL) { - return(snd); - } - memcpy(buf, snd, len); - } - - for (; e != NULL; e = e->next) { - if (e->callback != NULL) { - e->callback(chan, buf, len, e->udata); - } - } - } - - /* be sure to SDL_free() the return value if != snd ... */ - return(buf); + int posteffect = (chan == MIX_CHANNEL_POST); + effect_info *e = ((posteffect) ? posteffects : mix_channel[chan].effects); + void *buf = snd; + + if (e != NULL) { /* are there any registered effects? */ + /* if this is the postmix, we can just overwrite the original. */ + if (!posteffect) { + buf = SDL_malloc(len); + if (buf == NULL) { + return(snd); + } + memcpy(buf, snd, len); + } + + for (; e != NULL; e = e->next) { + if (e->callback != NULL) { + e->callback(chan, buf, len, e->udata); + } + } + } + + /* be sure to SDL_free() the return value if != snd ... */ + return(buf); } /* Mixing function */ static void mix_channels(void *udata, Uint8 *stream, int len) { - Uint8 *mix_input; - int i, mixable, volume = SDL_MIX_MAXVOLUME; - Uint32 sdl_ticks; + Uint8 *mix_input; + int i, mixable, volume = SDL_MIX_MAXVOLUME; + Uint32 sdl_ticks; #if SDL_VERSION_ATLEAST(1, 3, 0) - /* Need to initialize the stream in SDL 1.3+ */ - memset(stream, mixer.silence, len); + /* Need to initialize the stream in SDL 1.3+ */ + memset(stream, mixer.silence, len); #endif - /* Mix the music (must be done before the channels are added) */ - if ( music_active || (mix_music != music_mixer) ) { - mix_music(music_data, stream, len); - } - - /* Mix any playing channels... */ - sdl_ticks = SDL_GetTicks(); - for ( i=0; i 0 && mix_channel[i].expire < sdl_ticks ) { - /* Expiration delay for that channel is reached */ - mix_channel[i].playing = 0; - mix_channel[i].looping = 0; - mix_channel[i].fading = MIX_NO_FADING; - mix_channel[i].expire = 0; - _Mix_channel_done_playing(i); - } else if ( mix_channel[i].fading != MIX_NO_FADING ) { - Uint32 ticks = sdl_ticks - mix_channel[i].ticks_fade; - if( ticks > mix_channel[i].fade_length ) { - Mix_Volume(i, mix_channel[i].fade_volume_reset); /* Restore the volume */ - if( mix_channel[i].fading == MIX_FADING_OUT ) { - mix_channel[i].playing = 0; - mix_channel[i].looping = 0; - mix_channel[i].expire = 0; - _Mix_channel_done_playing(i); - } - mix_channel[i].fading = MIX_NO_FADING; - } else { - if( mix_channel[i].fading == MIX_FADING_OUT ) { - Mix_Volume(i, (mix_channel[i].fade_volume * (mix_channel[i].fade_length-ticks)) - / mix_channel[i].fade_length ); - } else { - Mix_Volume(i, (mix_channel[i].fade_volume * ticks) / mix_channel[i].fade_length ); - } - } - } - if ( mix_channel[i].playing > 0 ) { - int index = 0; - int remaining = len; - while (mix_channel[i].playing > 0 && index < len) { - remaining = len - index; - volume = (mix_channel[i].volume*mix_channel[i].chunk->volume) / MIX_MAX_VOLUME; - mixable = mix_channel[i].playing; - if ( mixable > remaining ) { - mixable = remaining; - } - - mix_input = Mix_DoEffects(i, mix_channel[i].samples, mixable); - SDL_MixAudio(stream+index,mix_input,mixable,volume); - if (mix_input != mix_channel[i].samples) - SDL_free(mix_input); - - mix_channel[i].samples += mixable; - mix_channel[i].playing -= mixable; - index += mixable; - - /* rcg06072001 Alert app if channel is done playing. */ - if (!mix_channel[i].playing && !mix_channel[i].looping) { - _Mix_channel_done_playing(i); - } - } - - /* If looping the sample and we are at its end, make sure - we will still return a full buffer */ - while ( mix_channel[i].looping && index < len ) { - int alen = mix_channel[i].chunk->alen; - remaining = len - index; - if (remaining > alen) { - remaining = alen; - } - - mix_input = Mix_DoEffects(i, mix_channel[i].chunk->abuf, remaining); - SDL_MixAudio(stream+index, mix_input, remaining, volume); - if (mix_input != mix_channel[i].chunk->abuf) - SDL_free(mix_input); - - --mix_channel[i].looping; - mix_channel[i].samples = mix_channel[i].chunk->abuf + remaining; - mix_channel[i].playing = mix_channel[i].chunk->alen - remaining; - index += remaining; - } - if ( ! mix_channel[i].playing && mix_channel[i].looping ) { - --mix_channel[i].looping; - mix_channel[i].samples = mix_channel[i].chunk->abuf; - mix_channel[i].playing = mix_channel[i].chunk->alen; - } - } - } - } - - /* rcg06122001 run posteffects... */ - Mix_DoEffects(MIX_CHANNEL_POST, stream, len); - - if ( mix_postmix ) { - mix_postmix(mix_postmix_data, stream, len); - } + /* Mix the music (must be done before the channels are added) */ + if ( music_active || (mix_music != music_mixer) ) { + mix_music(music_data, stream, len); + } + + /* Mix any playing channels... */ + sdl_ticks = SDL_GetTicks(); + for ( i=0; i 0 && mix_channel[i].expire < sdl_ticks ) { + /* Expiration delay for that channel is reached */ + mix_channel[i].playing = 0; + mix_channel[i].looping = 0; + mix_channel[i].fading = MIX_NO_FADING; + mix_channel[i].expire = 0; + _Mix_channel_done_playing(i); + } else if ( mix_channel[i].fading != MIX_NO_FADING ) { + Uint32 ticks = sdl_ticks - mix_channel[i].ticks_fade; + if( ticks > mix_channel[i].fade_length ) { + Mix_Volume(i, mix_channel[i].fade_volume_reset); /* Restore the volume */ + if( mix_channel[i].fading == MIX_FADING_OUT ) { + mix_channel[i].playing = 0; + mix_channel[i].looping = 0; + mix_channel[i].expire = 0; + _Mix_channel_done_playing(i); + } + mix_channel[i].fading = MIX_NO_FADING; + } else { + if( mix_channel[i].fading == MIX_FADING_OUT ) { + Mix_Volume(i, (mix_channel[i].fade_volume * (mix_channel[i].fade_length-ticks)) + / mix_channel[i].fade_length ); + } else { + Mix_Volume(i, (mix_channel[i].fade_volume * ticks) / mix_channel[i].fade_length ); + } + } + } + if ( mix_channel[i].playing > 0 ) { + int index = 0; + int remaining = len; + while (mix_channel[i].playing > 0 && index < len) { + remaining = len - index; + volume = (mix_channel[i].volume*mix_channel[i].chunk->volume) / MIX_MAX_VOLUME; + mixable = mix_channel[i].playing; + if ( mixable > remaining ) { + mixable = remaining; + } + + mix_input = Mix_DoEffects(i, mix_channel[i].samples, mixable); + SDL_MixAudio(stream+index,mix_input,mixable,volume); + if (mix_input != mix_channel[i].samples) + SDL_free(mix_input); + + mix_channel[i].samples += mixable; + mix_channel[i].playing -= mixable; + index += mixable; + + /* rcg06072001 Alert app if channel is done playing. */ + if (!mix_channel[i].playing && !mix_channel[i].looping) { + _Mix_channel_done_playing(i); + } + } + + /* If looping the sample and we are at its end, make sure + we will still return a full buffer */ + while ( mix_channel[i].looping && index < len ) { + int alen = mix_channel[i].chunk->alen; + remaining = len - index; + if (remaining > alen) { + remaining = alen; + } + + mix_input = Mix_DoEffects(i, mix_channel[i].chunk->abuf, remaining); + SDL_MixAudio(stream+index, mix_input, remaining, volume); + if (mix_input != mix_channel[i].chunk->abuf) + SDL_free(mix_input); + + --mix_channel[i].looping; + mix_channel[i].samples = mix_channel[i].chunk->abuf + remaining; + mix_channel[i].playing = mix_channel[i].chunk->alen - remaining; + index += remaining; + } + if ( ! mix_channel[i].playing && mix_channel[i].looping ) { + --mix_channel[i].looping; + mix_channel[i].samples = mix_channel[i].chunk->abuf; + mix_channel[i].playing = mix_channel[i].chunk->alen; + } + } + } + } + + /* rcg06122001 run posteffects... */ + Mix_DoEffects(MIX_CHANNEL_POST, stream, len); + + if ( mix_postmix ) { + mix_postmix(mix_postmix_data, stream, len); + } } #if 0 static void PrintFormat(char *title, SDL_AudioSpec *fmt) { - printf("%s: %d bit %s audio (%s) at %u Hz\n", title, (fmt->format&0xFF), - (fmt->format&0x8000) ? "signed" : "unsigned", - (fmt->channels > 2) ? "surround" : - (fmt->channels > 1) ? "stereo" : "mono", fmt->freq); + printf("%s: %d bit %s audio (%s) at %u Hz\n", title, (fmt->format&0xFF), + (fmt->format&0x8000) ? "signed" : "unsigned", + (fmt->channels > 2) ? "surround" : + (fmt->channels > 1) ? "stereo" : "mono", fmt->freq); } #endif @@ -406,77 +406,77 @@ static void PrintFormat(char *title, SDL_AudioSpec *fmt) /* Open the mixer with a certain desired audio format */ int Mix_OpenAudio(int frequency, Uint16 format, int nchannels, int chunksize) { - int i; - SDL_AudioSpec desired; - - /* If the mixer is already opened, increment open count */ - if ( audio_opened ) { - if ( format == mixer.format && nchannels == mixer.channels ) { - ++audio_opened; - return(0); - } - while ( audio_opened ) { - Mix_CloseAudio(); - } - } - - /* Set the desired format and frequency */ - desired.freq = frequency; - desired.format = format; - desired.channels = nchannels; - desired.samples = chunksize; - desired.callback = mix_channels; - desired.userdata = NULL; - - /* Accept nearly any audio format */ - if ( SDL_OpenAudio(&desired, &mixer) < 0 ) { - return(-1); - } + int i; + SDL_AudioSpec desired; + + /* If the mixer is already opened, increment open count */ + if ( audio_opened ) { + if ( format == mixer.format && nchannels == mixer.channels ) { + ++audio_opened; + return(0); + } + while ( audio_opened ) { + Mix_CloseAudio(); + } + } + + /* Set the desired format and frequency */ + desired.freq = frequency; + desired.format = format; + desired.channels = nchannels; + desired.samples = chunksize; + desired.callback = mix_channels; + desired.userdata = NULL; + + /* Accept nearly any audio format */ + if ( SDL_OpenAudio(&desired, &mixer) < 0 ) { + return(-1); + } #if 0 - PrintFormat("Audio device", &mixer); + PrintFormat("Audio device", &mixer); #endif - /* Initialize the music players */ - if ( open_music(&mixer) < 0 ) { - SDL_CloseAudio(); - return(-1); - } - - num_channels = MIX_CHANNELS; - mix_channel = (struct _Mix_Channel *) SDL_malloc(num_channels * sizeof(struct _Mix_Channel)); - - /* Clear out the audio channels */ - for ( i=0; i num_channels ) { - /* Initialize the new channels */ - int i; - for(i=num_channels; i < numchans; i++) { - mix_channel[i].chunk = NULL; - mix_channel[i].playing = 0; - mix_channel[i].looping = 0; - mix_channel[i].volume = SDL_MIX_MAXVOLUME; - mix_channel[i].fade_volume = SDL_MIX_MAXVOLUME; - mix_channel[i].fade_volume_reset = SDL_MIX_MAXVOLUME; - mix_channel[i].fading = MIX_NO_FADING; - mix_channel[i].tag = -1; - mix_channel[i].expire = 0; - mix_channel[i].effects = NULL; - mix_channel[i].paused = 0; - } - } - num_channels = numchans; - SDL_UnlockAudio(); - return(num_channels); + if ( numchans<0 || numchans==num_channels ) + return(num_channels); + + if ( numchans < num_channels ) { + /* Stop the affected channels */ + int i; + for(i=numchans; i < num_channels; i++) { + Mix_UnregisterAllEffects(i); + Mix_HaltChannel(i); + } + } + SDL_LockAudio(); + mix_channel = (struct _Mix_Channel *) SDL_realloc(mix_channel, numchans * sizeof(struct _Mix_Channel)); + if ( numchans > num_channels ) { + /* Initialize the new channels */ + int i; + for(i=num_channels; i < numchans; i++) { + mix_channel[i].chunk = NULL; + mix_channel[i].playing = 0; + mix_channel[i].looping = 0; + mix_channel[i].volume = SDL_MIX_MAXVOLUME; + mix_channel[i].fade_volume = SDL_MIX_MAXVOLUME; + mix_channel[i].fade_volume_reset = SDL_MIX_MAXVOLUME; + mix_channel[i].fading = MIX_NO_FADING; + mix_channel[i].tag = -1; + mix_channel[i].expire = 0; + mix_channel[i].effects = NULL; + mix_channel[i].paused = 0; + } + } + num_channels = numchans; + SDL_UnlockAudio(); + return(num_channels); } /* Return the actual mixer parameters */ int Mix_QuerySpec(int *frequency, Uint16 *format, int *channels) { - if ( audio_opened ) { - if ( frequency ) { - *frequency = mixer.freq; - } - if ( format ) { - *format = mixer.format; - } - if ( channels ) { - *channels = mixer.channels; - } - } - return(audio_opened); + if ( audio_opened ) { + if ( frequency ) { + *frequency = mixer.freq; + } + if ( format ) { + *format = mixer.format; + } + if ( channels ) { + *channels = mixer.channels; + } + } + return(audio_opened); } @@ -546,213 +546,213 @@ int Mix_QuerySpec(int *frequency, Uint16 *format, int *channels) /* Load a wave file */ Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc) { - Uint32 magic; - Mix_Chunk *chunk; - SDL_AudioSpec wavespec, *loaded; - SDL_AudioCVT wavecvt; - int samplesize; - - /* rcg06012001 Make sure src is valid */ - if ( ! src ) { - SDL_SetError("Mix_LoadWAV_RW with NULL src"); - return(NULL); - } - - /* Make sure audio has been opened */ - if ( ! audio_opened ) { - SDL_SetError("Audio device hasn't been opened"); - if ( freesrc && src ) { - SDL_RWclose(src); - } - return(NULL); - } - - /* Allocate the chunk memory */ - chunk = (Mix_Chunk *)SDL_malloc(sizeof(Mix_Chunk)); - if ( chunk == NULL ) { - SDL_SetError("Out of memory"); - if ( freesrc ) { - SDL_RWclose(src); - } - return(NULL); - } - - /* Find out what kind of audio file this is */ - magic = SDL_ReadLE32(src); - /* Seek backwards for compatibility with older loaders */ - SDL_RWseek(src, -(int)sizeof(Uint32), RW_SEEK_CUR); - - switch (magic) { - case WAVE: - case RIFF: - loaded = SDL_LoadWAV_RW(src, freesrc, &wavespec, - (Uint8 **)&chunk->abuf, &chunk->alen); - break; - case FORM: - loaded = Mix_LoadAIFF_RW(src, freesrc, &wavespec, - (Uint8 **)&chunk->abuf, &chunk->alen); - break; + Uint32 magic; + Mix_Chunk *chunk; + SDL_AudioSpec wavespec, *loaded; + SDL_AudioCVT wavecvt; + int samplesize; + + /* rcg06012001 Make sure src is valid */ + if ( ! src ) { + SDL_SetError("Mix_LoadWAV_RW with NULL src"); + return(NULL); + } + + /* Make sure audio has been opened */ + if ( ! audio_opened ) { + SDL_SetError("Audio device hasn't been opened"); + if ( freesrc && src ) { + SDL_RWclose(src); + } + return(NULL); + } + + /* Allocate the chunk memory */ + chunk = (Mix_Chunk *)SDL_malloc(sizeof(Mix_Chunk)); + if ( chunk == NULL ) { + SDL_SetError("Out of memory"); + if ( freesrc ) { + SDL_RWclose(src); + } + return(NULL); + } + + /* Find out what kind of audio file this is */ + magic = SDL_ReadLE32(src); + /* Seek backwards for compatibility with older loaders */ + SDL_RWseek(src, -(int)sizeof(Uint32), RW_SEEK_CUR); + + switch (magic) { + case WAVE: + case RIFF: + loaded = SDL_LoadWAV_RW(src, freesrc, &wavespec, + (Uint8 **)&chunk->abuf, &chunk->alen); + break; + case FORM: + loaded = Mix_LoadAIFF_RW(src, freesrc, &wavespec, + (Uint8 **)&chunk->abuf, &chunk->alen); + break; #ifdef OGG_MUSIC - case OGGS: - loaded = Mix_LoadOGG_RW(src, freesrc, &wavespec, - (Uint8 **)&chunk->abuf, &chunk->alen); - break; + case OGGS: + loaded = Mix_LoadOGG_RW(src, freesrc, &wavespec, + (Uint8 **)&chunk->abuf, &chunk->alen); + break; #endif #ifdef FLAC_MUSIC - case FLAC: - loaded = Mix_LoadFLAC_RW(src, freesrc, &wavespec, - (Uint8 **)&chunk->abuf, &chunk->alen); - break; + case FLAC: + loaded = Mix_LoadFLAC_RW(src, freesrc, &wavespec, + (Uint8 **)&chunk->abuf, &chunk->alen); + break; #endif - case CREA: - loaded = Mix_LoadVOC_RW(src, freesrc, &wavespec, - (Uint8 **)&chunk->abuf, &chunk->alen); - break; - default: - SDL_SetError("Unrecognized sound file type"); - if ( freesrc ) { - SDL_RWclose(src); - } - loaded = NULL; - break; - } - if ( !loaded ) { - /* The individual loaders have closed src if needed */ - SDL_free(chunk); - return(NULL); - } + case CREA: + loaded = Mix_LoadVOC_RW(src, freesrc, &wavespec, + (Uint8 **)&chunk->abuf, &chunk->alen); + break; + default: + SDL_SetError("Unrecognized sound file type"); + if ( freesrc ) { + SDL_RWclose(src); + } + loaded = NULL; + break; + } + if ( !loaded ) { + /* The individual loaders have closed src if needed */ + SDL_free(chunk); + return(NULL); + } #if 0 - PrintFormat("Audio device", &mixer); - PrintFormat("-- Wave file", &wavespec); + PrintFormat("Audio device", &mixer); + PrintFormat("-- Wave file", &wavespec); #endif - /* Build the audio converter and create conversion buffers */ - if ( wavespec.format != mixer.format || - wavespec.channels != mixer.channels || - wavespec.freq != mixer.freq ) { - if ( SDL_BuildAudioCVT(&wavecvt, - wavespec.format, wavespec.channels, wavespec.freq, - mixer.format, mixer.channels, mixer.freq) < 0 ) { - SDL_free(chunk->abuf); - SDL_free(chunk); - return(NULL); - } - samplesize = ((wavespec.format & 0xFF)/8)*wavespec.channels; - wavecvt.len = chunk->alen & ~(samplesize-1); - wavecvt.buf = (Uint8 *)SDL_calloc(1, wavecvt.len*wavecvt.len_mult); - if ( wavecvt.buf == NULL ) { - SDL_SetError("Out of memory"); - SDL_free(chunk->abuf); - SDL_free(chunk); - return(NULL); - } - memcpy(wavecvt.buf, chunk->abuf, chunk->alen); - SDL_free(chunk->abuf); - - /* Run the audio converter */ - if ( SDL_ConvertAudio(&wavecvt) < 0 ) { - SDL_free(wavecvt.buf); - SDL_free(chunk); - return(NULL); - } - - chunk->abuf = wavecvt.buf; - chunk->alen = wavecvt.len_cvt; - } - - chunk->allocated = 1; - chunk->volume = MIX_MAX_VOLUME; - - return(chunk); + /* Build the audio converter and create conversion buffers */ + if ( wavespec.format != mixer.format || + wavespec.channels != mixer.channels || + wavespec.freq != mixer.freq ) { + if ( SDL_BuildAudioCVT(&wavecvt, + wavespec.format, wavespec.channels, wavespec.freq, + mixer.format, mixer.channels, mixer.freq) < 0 ) { + SDL_free(chunk->abuf); + SDL_free(chunk); + return(NULL); + } + samplesize = ((wavespec.format & 0xFF)/8)*wavespec.channels; + wavecvt.len = chunk->alen & ~(samplesize-1); + wavecvt.buf = (Uint8 *)SDL_calloc(1, wavecvt.len*wavecvt.len_mult); + if ( wavecvt.buf == NULL ) { + SDL_SetError("Out of memory"); + SDL_free(chunk->abuf); + SDL_free(chunk); + return(NULL); + } + memcpy(wavecvt.buf, chunk->abuf, chunk->alen); + SDL_free(chunk->abuf); + + /* Run the audio converter */ + if ( SDL_ConvertAudio(&wavecvt) < 0 ) { + SDL_free(wavecvt.buf); + SDL_free(chunk); + return(NULL); + } + + chunk->abuf = wavecvt.buf; + chunk->alen = wavecvt.len_cvt; + } + + chunk->allocated = 1; + chunk->volume = MIX_MAX_VOLUME; + + return(chunk); } /* Load a wave file of the mixer format from a memory buffer */ Mix_Chunk *Mix_QuickLoad_WAV(Uint8 *mem) { - Mix_Chunk *chunk; - Uint8 magic[4]; - - /* Make sure audio has been opened */ - if ( ! audio_opened ) { - SDL_SetError("Audio device hasn't been opened"); - return(NULL); - } - - /* Allocate the chunk memory */ - chunk = (Mix_Chunk *)SDL_calloc(1,sizeof(Mix_Chunk)); - if ( chunk == NULL ) { - SDL_SetError("Out of memory"); - return(NULL); - } - - /* Essentially just skip to the audio data (no error checking - fast) */ - chunk->allocated = 0; - mem += 12; /* WAV header */ - do { - memcpy(magic, mem, 4); - mem += 4; - chunk->alen = ((mem[3]<<24)|(mem[2]<<16)|(mem[1]<<8)|(mem[0])); - mem += 4; - chunk->abuf = mem; - mem += chunk->alen; - } while ( memcmp(magic, "data", 4) != 0 ); - chunk->volume = MIX_MAX_VOLUME; - - return(chunk); + Mix_Chunk *chunk; + Uint8 magic[4]; + + /* Make sure audio has been opened */ + if ( ! audio_opened ) { + SDL_SetError("Audio device hasn't been opened"); + return(NULL); + } + + /* Allocate the chunk memory */ + chunk = (Mix_Chunk *)SDL_calloc(1,sizeof(Mix_Chunk)); + if ( chunk == NULL ) { + SDL_SetError("Out of memory"); + return(NULL); + } + + /* Essentially just skip to the audio data (no error checking - fast) */ + chunk->allocated = 0; + mem += 12; /* WAV header */ + do { + memcpy(magic, mem, 4); + mem += 4; + chunk->alen = ((mem[3]<<24)|(mem[2]<<16)|(mem[1]<<8)|(mem[0])); + mem += 4; + chunk->abuf = mem; + mem += chunk->alen; + } while ( memcmp(magic, "data", 4) != 0 ); + chunk->volume = MIX_MAX_VOLUME; + + return(chunk); } /* Load raw audio data of the mixer format from a memory buffer */ Mix_Chunk *Mix_QuickLoad_RAW(Uint8 *mem, Uint32 len) { - Mix_Chunk *chunk; - - /* Make sure audio has been opened */ - if ( ! audio_opened ) { - SDL_SetError("Audio device hasn't been opened"); - return(NULL); - } - - /* Allocate the chunk memory */ - chunk = (Mix_Chunk *)SDL_malloc(sizeof(Mix_Chunk)); - if ( chunk == NULL ) { - SDL_SetError("Out of memory"); - return(NULL); - } - - /* Essentially just point at the audio data (no error checking - fast) */ - chunk->allocated = 0; - chunk->alen = len; - chunk->abuf = mem; - chunk->volume = MIX_MAX_VOLUME; - - return(chunk); + Mix_Chunk *chunk; + + /* Make sure audio has been opened */ + if ( ! audio_opened ) { + SDL_SetError("Audio device hasn't been opened"); + return(NULL); + } + + /* Allocate the chunk memory */ + chunk = (Mix_Chunk *)SDL_malloc(sizeof(Mix_Chunk)); + if ( chunk == NULL ) { + SDL_SetError("Out of memory"); + return(NULL); + } + + /* Essentially just point at the audio data (no error checking - fast) */ + chunk->allocated = 0; + chunk->alen = len; + chunk->abuf = mem; + chunk->volume = MIX_MAX_VOLUME; + + return(chunk); } /* Free an audio chunk previously loaded */ void Mix_FreeChunk(Mix_Chunk *chunk) { - int i; - - /* Caution -- if the chunk is playing, the mixer will crash */ - if ( chunk ) { - /* Guarantee that this chunk isn't playing */ - SDL_LockAudio(); - if ( mix_channel ) { - for ( i=0; iallocated ) { - SDL_free(chunk->abuf); - } - SDL_free(chunk); - } + int i; + + /* Caution -- if the chunk is playing, the mixer will crash */ + if ( chunk ) { + /* Guarantee that this chunk isn't playing */ + SDL_LockAudio(); + if ( mix_channel ) { + for ( i=0; iallocated ) { + SDL_free(chunk->abuf); + } + SDL_free(chunk); + } } /* Set a function that is called after all mixing is performed. @@ -762,10 +762,10 @@ void Mix_FreeChunk(Mix_Chunk *chunk) void Mix_SetPostMix(void (*mix_func) (void *udata, Uint8 *stream, int len), void *arg) { - SDL_LockAudio(); - mix_postmix_data = arg; - mix_postmix = mix_func; - SDL_UnlockAudio(); + SDL_LockAudio(); + mix_postmix_data = arg; + mix_postmix = mix_func; + SDL_UnlockAudio(); } /* Add your own music player or mixer function. @@ -774,27 +774,27 @@ void Mix_SetPostMix(void (*mix_func) void Mix_HookMusic(void (*mix_func)(void *udata, Uint8 *stream, int len), void *arg) { - SDL_LockAudio(); - if ( mix_func != NULL ) { - music_data = arg; - mix_music = mix_func; - } else { - music_data = NULL; - mix_music = music_mixer; - } - SDL_UnlockAudio(); + SDL_LockAudio(); + if ( mix_func != NULL ) { + music_data = arg; + mix_music = mix_func; + } else { + music_data = NULL; + mix_music = music_mixer; + } + SDL_UnlockAudio(); } void *Mix_GetMusicHookData(void) { - return(music_data); + return(music_data); } void Mix_ChannelFinished(void (*channel_finished)(int channel)) { - SDL_LockAudio(); - channel_done_callback = channel_finished; - SDL_UnlockAudio(); + SDL_LockAudio(); + channel_done_callback = channel_finished; + SDL_UnlockAudio(); } @@ -804,20 +804,20 @@ void Mix_ChannelFinished(void (*channel_finished)(int channel)) */ int Mix_ReserveChannels(int num) { - if (num > num_channels) - num = num_channels; - reserved_channels = num; - return num; + if (num > num_channels) + num = num_channels; + reserved_channels = num; + return num; } static int checkchunkintegral(Mix_Chunk *chunk) { - int frame_width = 1; + int frame_width = 1; - if ((mixer.format & 0xFF) == 16) frame_width = 2; - frame_width *= mixer.channels; - while (chunk->alen % frame_width) chunk->alen--; - return chunk->alen; + if ((mixer.format & 0xFF) == 16) frame_width = 2; + frame_width *= mixer.channels; + while (chunk->alen % frame_width) chunk->alen--; + return chunk->alen; } /* Play an audio chunk on a specific channel. @@ -828,259 +828,259 @@ static int checkchunkintegral(Mix_Chunk *chunk) */ int Mix_PlayChannelTimed(int which, Mix_Chunk *chunk, int loops, int ticks) { - int i; - - /* Don't play null pointers :-) */ - if ( chunk == NULL ) { - Mix_SetError("Tried to play a NULL chunk"); - return(-1); - } - if ( !checkchunkintegral(chunk)) { - Mix_SetError("Tried to play a chunk with a bad frame"); - return(-1); - } - - /* Lock the mixer while modifying the playing channels */ - SDL_LockAudio(); - { - /* If which is -1, play on the first free channel */ - if ( which == -1 ) { - for ( i=reserved_channels; i= 0 && which < num_channels ) { - Uint32 sdl_ticks = SDL_GetTicks(); - if (Mix_Playing(which)) - _Mix_channel_done_playing(which); - mix_channel[which].samples = chunk->abuf; - mix_channel[which].playing = chunk->alen; - mix_channel[which].looping = loops; - mix_channel[which].chunk = chunk; - mix_channel[which].paused = 0; - mix_channel[which].fading = MIX_NO_FADING; - mix_channel[which].start_time = sdl_ticks; - mix_channel[which].expire = (ticks>0) ? (sdl_ticks + ticks) : 0; - } - } - SDL_UnlockAudio(); - - /* Return the channel on which the sound is being played */ - return(which); + int i; + + /* Don't play null pointers :-) */ + if ( chunk == NULL ) { + Mix_SetError("Tried to play a NULL chunk"); + return(-1); + } + if ( !checkchunkintegral(chunk)) { + Mix_SetError("Tried to play a chunk with a bad frame"); + return(-1); + } + + /* Lock the mixer while modifying the playing channels */ + SDL_LockAudio(); + { + /* If which is -1, play on the first free channel */ + if ( which == -1 ) { + for ( i=reserved_channels; i= 0 && which < num_channels ) { + Uint32 sdl_ticks = SDL_GetTicks(); + if (Mix_Playing(which)) + _Mix_channel_done_playing(which); + mix_channel[which].samples = chunk->abuf; + mix_channel[which].playing = chunk->alen; + mix_channel[which].looping = loops; + mix_channel[which].chunk = chunk; + mix_channel[which].paused = 0; + mix_channel[which].fading = MIX_NO_FADING; + mix_channel[which].start_time = sdl_ticks; + mix_channel[which].expire = (ticks>0) ? (sdl_ticks + ticks) : 0; + } + } + SDL_UnlockAudio(); + + /* Return the channel on which the sound is being played */ + return(which); } /* Change the expiration delay for a channel */ int Mix_ExpireChannel(int which, int ticks) { - int status = 0; - - if ( which == -1 ) { - int i; - for ( i=0; i < num_channels; ++ i ) { - status += Mix_ExpireChannel(i, ticks); - } - } else if ( which < num_channels ) { - SDL_LockAudio(); - mix_channel[which].expire = (ticks>0) ? (SDL_GetTicks() + ticks) : 0; - SDL_UnlockAudio(); - ++ status; - } - return(status); + int status = 0; + + if ( which == -1 ) { + int i; + for ( i=0; i < num_channels; ++ i ) { + status += Mix_ExpireChannel(i, ticks); + } + } else if ( which < num_channels ) { + SDL_LockAudio(); + mix_channel[which].expire = (ticks>0) ? (SDL_GetTicks() + ticks) : 0; + SDL_UnlockAudio(); + ++ status; + } + return(status); } /* Fade in a sound on a channel, over ms milliseconds */ int Mix_FadeInChannelTimed(int which, Mix_Chunk *chunk, int loops, int ms, int ticks) { - int i; - - /* Don't play null pointers :-) */ - if ( chunk == NULL ) { - return(-1); - } - if ( !checkchunkintegral(chunk)) { - Mix_SetError("Tried to play a chunk with a bad frame"); - return(-1); - } - - /* Lock the mixer while modifying the playing channels */ - SDL_LockAudio(); - { - /* If which is -1, play on the first free channel */ - if ( which == -1 ) { - for ( i=reserved_channels; i= 0 && which < num_channels ) { - Uint32 sdl_ticks = SDL_GetTicks(); - if (Mix_Playing(which)) - _Mix_channel_done_playing(which); - mix_channel[which].samples = chunk->abuf; - mix_channel[which].playing = chunk->alen; - mix_channel[which].looping = loops; - mix_channel[which].chunk = chunk; - mix_channel[which].paused = 0; - mix_channel[which].fading = MIX_FADING_IN; - mix_channel[which].fade_volume = mix_channel[which].volume; - mix_channel[which].fade_volume_reset = mix_channel[which].volume; - mix_channel[which].volume = 0; - mix_channel[which].fade_length = (Uint32)ms; - mix_channel[which].start_time = mix_channel[which].ticks_fade = sdl_ticks; - mix_channel[which].expire = (ticks > 0) ? (sdl_ticks+ticks) : 0; - } - } - SDL_UnlockAudio(); - - /* Return the channel on which the sound is being played */ - return(which); + int i; + + /* Don't play null pointers :-) */ + if ( chunk == NULL ) { + return(-1); + } + if ( !checkchunkintegral(chunk)) { + Mix_SetError("Tried to play a chunk with a bad frame"); + return(-1); + } + + /* Lock the mixer while modifying the playing channels */ + SDL_LockAudio(); + { + /* If which is -1, play on the first free channel */ + if ( which == -1 ) { + for ( i=reserved_channels; i= 0 && which < num_channels ) { + Uint32 sdl_ticks = SDL_GetTicks(); + if (Mix_Playing(which)) + _Mix_channel_done_playing(which); + mix_channel[which].samples = chunk->abuf; + mix_channel[which].playing = chunk->alen; + mix_channel[which].looping = loops; + mix_channel[which].chunk = chunk; + mix_channel[which].paused = 0; + mix_channel[which].fading = MIX_FADING_IN; + mix_channel[which].fade_volume = mix_channel[which].volume; + mix_channel[which].fade_volume_reset = mix_channel[which].volume; + mix_channel[which].volume = 0; + mix_channel[which].fade_length = (Uint32)ms; + mix_channel[which].start_time = mix_channel[which].ticks_fade = sdl_ticks; + mix_channel[which].expire = (ticks > 0) ? (sdl_ticks+ticks) : 0; + } + } + SDL_UnlockAudio(); + + /* Return the channel on which the sound is being played */ + return(which); } /* Set volume of a particular channel */ int Mix_Volume(int which, int volume) { - int i; - int prev_volume = 0; - - if ( which == -1 ) { - for ( i=0; i= 0 ) { - if ( volume > SDL_MIX_MAXVOLUME ) { - volume = SDL_MIX_MAXVOLUME; - } - mix_channel[which].volume = volume; - } - } - return(prev_volume); + int i; + int prev_volume = 0; + + if ( which == -1 ) { + for ( i=0; i= 0 ) { + if ( volume > SDL_MIX_MAXVOLUME ) { + volume = SDL_MIX_MAXVOLUME; + } + mix_channel[which].volume = volume; + } + } + return(prev_volume); } /* Set volume of a particular chunk */ int Mix_VolumeChunk(Mix_Chunk *chunk, int volume) { - int prev_volume; - - prev_volume = chunk->volume; - if ( volume >= 0 ) { - if ( volume > MIX_MAX_VOLUME ) { - volume = MIX_MAX_VOLUME; - } - chunk->volume = volume; - } - return(prev_volume); + int prev_volume; + + prev_volume = chunk->volume; + if ( volume >= 0 ) { + if ( volume > MIX_MAX_VOLUME ) { + volume = MIX_MAX_VOLUME; + } + chunk->volume = volume; + } + return(prev_volume); } /* Halt playing of a particular channel */ int Mix_HaltChannel(int which) { - int i; - - if ( which == -1 ) { - for ( i=0; i 0) && - (mix_channel[which].fading != MIX_FADING_OUT) ) { - mix_channel[which].fade_volume = mix_channel[which].volume; - mix_channel[which].fading = MIX_FADING_OUT; - mix_channel[which].fade_length = ms; - mix_channel[which].ticks_fade = SDL_GetTicks(); - - /* only change fade_volume_reset if we're not fading. */ - if (mix_channel[which].fading == MIX_NO_FADING) { - mix_channel[which].fade_volume_reset = mix_channel[which].volume; - } - ++status; - } - SDL_UnlockAudio(); - } - } - return(status); + int status; + + status = 0; + if ( audio_opened ) { + if ( which == -1 ) { + int i; + + for ( i=0; i 0) && + (mix_channel[which].fading != MIX_FADING_OUT) ) { + mix_channel[which].fade_volume = mix_channel[which].volume; + mix_channel[which].fading = MIX_FADING_OUT; + mix_channel[which].fade_length = ms; + mix_channel[which].ticks_fade = SDL_GetTicks(); + + /* only change fade_volume_reset if we're not fading. */ + if (mix_channel[which].fading == MIX_NO_FADING) { + mix_channel[which].fade_volume_reset = mix_channel[which].volume; + } + ++status; + } + SDL_UnlockAudio(); + } + } + return(status); } /* Halt playing of a particular group of channels */ int Mix_FadeOutGroup(int tag, int ms) { - int i; - int status = 0; - for ( i=0; i= num_channels ) { - return MIX_NO_FADING; - } - return mix_channel[which].fading; + if ( which < 0 || which >= num_channels ) { + return MIX_NO_FADING; + } + return mix_channel[which].fading; } /* Check the status of a specific channel. @@ -1088,206 +1088,206 @@ Mix_Fading Mix_FadingChannel(int which) */ int Mix_Playing(int which) { - int status; - - status = 0; - if ( which == -1 ) { - int i; - - for ( i=0; i 0) || - (mix_channel[i].looping > 0)) - { - ++status; - } - } - } else if ( which < num_channels ) { - if ( (mix_channel[which].playing > 0) || - (mix_channel[which].looping > 0) ) - { - ++status; - } - } - return(status); + int status; + + status = 0; + if ( which == -1 ) { + int i; + + for ( i=0; i 0) || + (mix_channel[i].looping > 0)) + { + ++status; + } + } + } else if ( which < num_channels ) { + if ( (mix_channel[which].playing > 0) || + (mix_channel[which].looping > 0) ) + { + ++status; + } + } + return(status); } /* rcg06072001 Get the chunk associated with a channel. */ Mix_Chunk *Mix_GetChunk(int channel) { - Mix_Chunk *retval = NULL; + Mix_Chunk *retval = NULL; - if ((channel >= 0) && (channel < num_channels)) { - retval = mix_channel[channel].chunk; - } + if ((channel >= 0) && (channel < num_channels)) { + retval = mix_channel[channel].chunk; + } - return(retval); + return(retval); } /* Close the mixer, halting all playing audio */ void Mix_CloseAudio(void) { - int i; - - if ( audio_opened ) { - if ( audio_opened == 1 ) { - for (i = 0; i < num_channels; i++) { - Mix_UnregisterAllEffects(i); - } - Mix_UnregisterAllEffects(MIX_CHANNEL_POST); - close_music(); - Mix_HaltChannel(-1); - _Mix_DeinitEffects(); - SDL_CloseAudio(); - SDL_free(mix_channel); - mix_channel = NULL; - - /* rcg06042009 report available decoders at runtime. */ - SDL_free(chunk_decoders); - chunk_decoders = NULL; - num_decoders = 0; - } - --audio_opened; - } + int i; + + if ( audio_opened ) { + if ( audio_opened == 1 ) { + for (i = 0; i < num_channels; i++) { + Mix_UnregisterAllEffects(i); + } + Mix_UnregisterAllEffects(MIX_CHANNEL_POST); + close_music(); + Mix_HaltChannel(-1); + _Mix_DeinitEffects(); + SDL_CloseAudio(); + SDL_free(mix_channel); + mix_channel = NULL; + + /* rcg06042009 report available decoders at runtime. */ + SDL_free(chunk_decoders); + chunk_decoders = NULL; + num_decoders = 0; + } + --audio_opened; + } } /* Pause a particular channel (or all) */ void Mix_Pause(int which) { - Uint32 sdl_ticks = SDL_GetTicks(); - if ( which == -1 ) { - int i; - - for ( i=0; i 0 ) { - mix_channel[i].paused = sdl_ticks; - } - } - } else if ( which < num_channels ) { - if ( mix_channel[which].playing > 0 ) { - mix_channel[which].paused = sdl_ticks; - } - } + Uint32 sdl_ticks = SDL_GetTicks(); + if ( which == -1 ) { + int i; + + for ( i=0; i 0 ) { + mix_channel[i].paused = sdl_ticks; + } + } + } else if ( which < num_channels ) { + if ( mix_channel[which].playing > 0 ) { + mix_channel[which].paused = sdl_ticks; + } + } } /* Resume a paused channel */ void Mix_Resume(int which) { - Uint32 sdl_ticks = SDL_GetTicks(); - - SDL_LockAudio(); - if ( which == -1 ) { - int i; - - for ( i=0; i 0 ) { - if(mix_channel[i].expire > 0) - mix_channel[i].expire += sdl_ticks - mix_channel[i].paused; - mix_channel[i].paused = 0; - } - } - } else if ( which < num_channels ) { - if ( mix_channel[which].playing > 0 ) { - if(mix_channel[which].expire > 0) - mix_channel[which].expire += sdl_ticks - mix_channel[which].paused; - mix_channel[which].paused = 0; - } - } - SDL_UnlockAudio(); + Uint32 sdl_ticks = SDL_GetTicks(); + + SDL_LockAudio(); + if ( which == -1 ) { + int i; + + for ( i=0; i 0 ) { + if(mix_channel[i].expire > 0) + mix_channel[i].expire += sdl_ticks - mix_channel[i].paused; + mix_channel[i].paused = 0; + } + } + } else if ( which < num_channels ) { + if ( mix_channel[which].playing > 0 ) { + if(mix_channel[which].expire > 0) + mix_channel[which].expire += sdl_ticks - mix_channel[which].paused; + mix_channel[which].paused = 0; + } + } + SDL_UnlockAudio(); } int Mix_Paused(int which) { - if ( which < 0 ) { - int status = 0; - int i; - for( i=0; i < num_channels; ++i ) { - if ( mix_channel[i].paused ) { - ++ status; - } - } - return(status); - } else if ( which < num_channels ) { - return(mix_channel[which].paused != 0); - } else { - return(0); - } + if ( which < 0 ) { + int status = 0; + int i; + for( i=0; i < num_channels; ++i ) { + if ( mix_channel[i].paused ) { + ++ status; + } + } + return(status); + } else if ( which < num_channels ) { + return(mix_channel[which].paused != 0); + } else { + return(0); + } } /* Change the group of a channel */ int Mix_GroupChannel(int which, int tag) { - if ( which < 0 || which > num_channels ) - return(0); + if ( which < 0 || which > num_channels ) + return(0); - SDL_LockAudio(); - mix_channel[which].tag = tag; - SDL_UnlockAudio(); - return(1); + SDL_LockAudio(); + mix_channel[which].tag = tag; + SDL_UnlockAudio(); + return(1); } /* Assign several consecutive channels to a group */ int Mix_GroupChannels(int from, int to, int tag) { - int status = 0; - for( ; from <= to; ++ from ) { - status += Mix_GroupChannel(from, tag); - } - return(status); + int status = 0; + for( ; from <= to; ++ from ) { + status += Mix_GroupChannel(from, tag); + } + return(status); } /* Finds the first available channel in a group of channels */ int Mix_GroupAvailable(int tag) { - int i; - for( i=0; i < num_channels; i ++ ) { - if ( ((tag == -1) || (tag == mix_channel[i].tag)) && - (mix_channel[i].playing <= 0) ) - return i; - } - return(-1); + int i; + for( i=0; i < num_channels; i ++ ) { + if ( ((tag == -1) || (tag == mix_channel[i].tag)) && + (mix_channel[i].playing <= 0) ) + return i; + } + return(-1); } int Mix_GroupCount(int tag) { - int count = 0; - int i; - for( i=0; i < num_channels; i ++ ) { - if ( mix_channel[i].tag==tag || tag==-1 ) - ++ count; - } - return(count); + int count = 0; + int i; + for( i=0; i < num_channels; i ++ ) { + if ( mix_channel[i].tag==tag || tag==-1 ) + ++ count; + } + return(count); } /* Finds the "oldest" sample playing in a group of channels */ int Mix_GroupOldest(int tag) { - int chan = -1; - Uint32 mintime = SDL_GetTicks(); - int i; - for( i=0; i < num_channels; i ++ ) { - if ( (mix_channel[i].tag==tag || tag==-1) && mix_channel[i].playing > 0 - && mix_channel[i].start_time <= mintime ) { - mintime = mix_channel[i].start_time; - chan = i; - } - } - return(chan); + int chan = -1; + Uint32 mintime = SDL_GetTicks(); + int i; + for( i=0; i < num_channels; i ++ ) { + if ( (mix_channel[i].tag==tag || tag==-1) && mix_channel[i].playing > 0 + && mix_channel[i].start_time <= mintime ) { + mintime = mix_channel[i].start_time; + chan = i; + } + } + return(chan); } /* Finds the "most recent" (i.e. last) sample playing in a group of channels */ int Mix_GroupNewer(int tag) { - int chan = -1; - Uint32 maxtime = 0; - int i; - for( i=0; i < num_channels; i ++ ) { - if ( (mix_channel[i].tag==tag || tag==-1) && mix_channel[i].playing > 0 - && mix_channel[i].start_time >= maxtime ) { - maxtime = mix_channel[i].start_time; - chan = i; - } - } - return(chan); + int chan = -1; + Uint32 maxtime = 0; + int i; + for( i=0; i < num_channels; i ++ ) { + if ( (mix_channel[i].tag==tag || tag==-1) && mix_channel[i].playing > 0 + && mix_channel[i].start_time >= maxtime ) { + maxtime = mix_channel[i].start_time; + chan = i; + } + } + return(chan); } @@ -1300,134 +1300,134 @@ int Mix_GroupNewer(int tag) /* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */ static int _Mix_register_effect(effect_info **e, Mix_EffectFunc_t f, - Mix_EffectDone_t d, void *arg) + Mix_EffectDone_t d, void *arg) { - effect_info *new_e; - - if (!e) { - Mix_SetError("Internal error"); - return(0); - } - - if (f == NULL) { - Mix_SetError("NULL effect callback"); - return(0); - } - - new_e = SDL_malloc(sizeof (effect_info)); - if (new_e == NULL) { - Mix_SetError("Out of memory"); - return(0); - } - - new_e->callback = f; - new_e->done_callback = d; - new_e->udata = arg; - new_e->next = NULL; - - /* add new effect to end of linked list... */ - if (*e == NULL) { - *e = new_e; - } else { - effect_info *cur = *e; - while (1) { - if (cur->next == NULL) { - cur->next = new_e; - break; - } - cur = cur->next; - } - } - - return(1); + effect_info *new_e; + + if (!e) { + Mix_SetError("Internal error"); + return(0); + } + + if (f == NULL) { + Mix_SetError("NULL effect callback"); + return(0); + } + + new_e = SDL_malloc(sizeof (effect_info)); + if (new_e == NULL) { + Mix_SetError("Out of memory"); + return(0); + } + + new_e->callback = f; + new_e->done_callback = d; + new_e->udata = arg; + new_e->next = NULL; + + /* add new effect to end of linked list... */ + if (*e == NULL) { + *e = new_e; + } else { + effect_info *cur = *e; + while (1) { + if (cur->next == NULL) { + cur->next = new_e; + break; + } + cur = cur->next; + } + } + + return(1); } /* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */ static int _Mix_remove_effect(int channel, effect_info **e, Mix_EffectFunc_t f) { - effect_info *cur; - effect_info *prev = NULL; - effect_info *next = NULL; - - if (!e) { - Mix_SetError("Internal error"); - return(0); - } - - for (cur = *e; cur != NULL; cur = cur->next) { - if (cur->callback == f) { - next = cur->next; - if (cur->done_callback != NULL) { - cur->done_callback(channel, cur->udata); - } - SDL_free(cur); - - if (prev == NULL) { /* removing first item of list? */ - *e = next; - } else { - prev->next = next; - } - return(1); - } - prev = cur; - } - - Mix_SetError("No such effect registered"); - return(0); + effect_info *cur; + effect_info *prev = NULL; + effect_info *next = NULL; + + if (!e) { + Mix_SetError("Internal error"); + return(0); + } + + for (cur = *e; cur != NULL; cur = cur->next) { + if (cur->callback == f) { + next = cur->next; + if (cur->done_callback != NULL) { + cur->done_callback(channel, cur->udata); + } + SDL_free(cur); + + if (prev == NULL) { /* removing first item of list? */ + *e = next; + } else { + prev->next = next; + } + return(1); + } + prev = cur; + } + + Mix_SetError("No such effect registered"); + return(0); } /* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */ static int _Mix_remove_all_effects(int channel, effect_info **e) { - effect_info *cur; - effect_info *next; - - if (!e) { - Mix_SetError("Internal error"); - return(0); - } - - for (cur = *e; cur != NULL; cur = next) { - next = cur->next; - if (cur->done_callback != NULL) { - cur->done_callback(channel, cur->udata); - } - SDL_free(cur); - } - *e = NULL; - - return(1); + effect_info *cur; + effect_info *next; + + if (!e) { + Mix_SetError("Internal error"); + return(0); + } + + for (cur = *e; cur != NULL; cur = next) { + next = cur->next; + if (cur->done_callback != NULL) { + cur->done_callback(channel, cur->udata); + } + SDL_free(cur); + } + *e = NULL; + + return(1); } /* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */ int _Mix_RegisterEffect_locked(int channel, Mix_EffectFunc_t f, - Mix_EffectDone_t d, void *arg) + Mix_EffectDone_t d, void *arg) { - effect_info **e = NULL; - - if (channel == MIX_CHANNEL_POST) { - e = &posteffects; - } else { - if ((channel < 0) || (channel >= num_channels)) { - Mix_SetError("Invalid channel number"); - return(0); - } - e = &mix_channel[channel].effects; - } - - return _Mix_register_effect(e, f, d, arg); + effect_info **e = NULL; + + if (channel == MIX_CHANNEL_POST) { + e = &posteffects; + } else { + if ((channel < 0) || (channel >= num_channels)) { + Mix_SetError("Invalid channel number"); + return(0); + } + e = &mix_channel[channel].effects; + } + + return _Mix_register_effect(e, f, d, arg); } int Mix_RegisterEffect(int channel, Mix_EffectFunc_t f, - Mix_EffectDone_t d, void *arg) + Mix_EffectDone_t d, void *arg) { int retval; - SDL_LockAudio(); - retval = _Mix_RegisterEffect_locked(channel, f, d, arg); - SDL_UnlockAudio(); + SDL_LockAudio(); + retval = _Mix_RegisterEffect_locked(channel, f, d, arg); + SDL_UnlockAudio(); return retval; } @@ -1435,55 +1435,55 @@ int Mix_RegisterEffect(int channel, Mix_EffectFunc_t f, /* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */ int _Mix_UnregisterEffect_locked(int channel, Mix_EffectFunc_t f) { - effect_info **e = NULL; - - if (channel == MIX_CHANNEL_POST) { - e = &posteffects; - } else { - if ((channel < 0) || (channel >= num_channels)) { - Mix_SetError("Invalid channel number"); - return(0); - } - e = &mix_channel[channel].effects; - } - - return _Mix_remove_effect(channel, e, f); + effect_info **e = NULL; + + if (channel == MIX_CHANNEL_POST) { + e = &posteffects; + } else { + if ((channel < 0) || (channel >= num_channels)) { + Mix_SetError("Invalid channel number"); + return(0); + } + e = &mix_channel[channel].effects; + } + + return _Mix_remove_effect(channel, e, f); } int Mix_UnregisterEffect(int channel, Mix_EffectFunc_t f) { - int retval; - SDL_LockAudio(); - retval = _Mix_UnregisterEffect_locked(channel, f); - SDL_UnlockAudio(); - return(retval); + int retval; + SDL_LockAudio(); + retval = _Mix_UnregisterEffect_locked(channel, f); + SDL_UnlockAudio(); + return(retval); } /* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */ int _Mix_UnregisterAllEffects_locked(int channel) { - effect_info **e = NULL; - - if (channel == MIX_CHANNEL_POST) { - e = &posteffects; - } else { - if ((channel < 0) || (channel >= num_channels)) { - Mix_SetError("Invalid channel number"); - return(0); - } - e = &mix_channel[channel].effects; - } - - return _Mix_remove_all_effects(channel, e); + effect_info **e = NULL; + + if (channel == MIX_CHANNEL_POST) { + e = &posteffects; + } else { + if ((channel < 0) || (channel >= num_channels)) { + Mix_SetError("Invalid channel number"); + return(0); + } + e = &mix_channel[channel].effects; + } + + return _Mix_remove_all_effects(channel, e); } int Mix_UnregisterAllEffects(int channel) { - int retval; - SDL_LockAudio(); - retval = _Mix_UnregisterAllEffects_locked(channel); - SDL_UnlockAudio(); - return(retval); + int retval; + SDL_LockAudio(); + retval = _Mix_UnregisterAllEffects_locked(channel); + SDL_UnlockAudio(); + return(retval); } /* end of mixer.c ... */ diff --git a/music_cmd.c b/music_cmd.c index b1761963..e58b3e1c 100644 --- a/music_cmd.c +++ b/music_cmd.c @@ -39,204 +39,204 @@ /* Unimplemented */ void MusicCMD_SetVolume(int volume) { - Mix_SetError("No way to modify external player volume"); + Mix_SetError("No way to modify external player volume"); } /* Load a music stream from the given file */ MusicCMD *MusicCMD_LoadSong(const char *cmd, const char *file) { - MusicCMD *music; - - /* Allocate and fill the music structure */ - music = (MusicCMD *)SDL_malloc(sizeof *music); - if ( music == NULL ) { - Mix_SetError("Out of memory"); - return(NULL); - } - music->file = SDL_strdup(file); - music->cmd = SDL_strdup(cmd); - music->pid = 0; - - /* We're done */ - return(music); + MusicCMD *music; + + /* Allocate and fill the music structure */ + music = (MusicCMD *)SDL_malloc(sizeof *music); + if ( music == NULL ) { + Mix_SetError("Out of memory"); + return(NULL); + } + music->file = SDL_strdup(file); + music->cmd = SDL_strdup(cmd); + music->pid = 0; + + /* We're done */ + return(music); } /* Parse a command line buffer into arguments */ static int ParseCommandLine(char *cmdline, char **argv) { - char *bufp; - int argc; - - argc = 0; - for ( bufp = cmdline; *bufp; ) { - /* Skip leading whitespace */ - while ( isspace(*bufp) ) { - ++bufp; - } - /* Skip over argument */ - if ( *bufp == '"' ) { - ++bufp; - if ( *bufp ) { - if ( argv ) { - argv[argc] = bufp; - } - ++argc; - } - /* Skip over word */ - while ( *bufp && (*bufp != '"') ) { - ++bufp; - } - } else { - if ( *bufp ) { - if ( argv ) { - argv[argc] = bufp; - } - ++argc; - } - /* Skip over word */ - while ( *bufp && ! isspace(*bufp) ) { - ++bufp; - } - } - if ( *bufp ) { - if ( argv ) { - *bufp = '\0'; - } - ++bufp; - } - } - if ( argv ) { - argv[argc] = NULL; - } - return(argc); + char *bufp; + int argc; + + argc = 0; + for ( bufp = cmdline; *bufp; ) { + /* Skip leading whitespace */ + while ( isspace(*bufp) ) { + ++bufp; + } + /* Skip over argument */ + if ( *bufp == '"' ) { + ++bufp; + if ( *bufp ) { + if ( argv ) { + argv[argc] = bufp; + } + ++argc; + } + /* Skip over word */ + while ( *bufp && (*bufp != '"') ) { + ++bufp; + } + } else { + if ( *bufp ) { + if ( argv ) { + argv[argc] = bufp; + } + ++argc; + } + /* Skip over word */ + while ( *bufp && ! isspace(*bufp) ) { + ++bufp; + } + } + if ( *bufp ) { + if ( argv ) { + *bufp = '\0'; + } + ++bufp; + } + } + if ( argv ) { + argv[argc] = NULL; + } + return(argc); } static char **parse_args(char *command, char *last_arg) { - int argc; - char **argv; - - /* Parse the command line */ - argc = ParseCommandLine(command, NULL); - if ( last_arg ) { - ++argc; - } - argv = (char **)SDL_malloc((argc+1)*(sizeof *argv)); - if ( argv == NULL ) { - return(NULL); - } - argc = ParseCommandLine(command, argv); - - /* Add last command line argument */ - if ( last_arg ) { - argv[argc++] = last_arg; - } - argv[argc] = NULL; - - /* We're ready! */ - return(argv); + int argc; + char **argv; + + /* Parse the command line */ + argc = ParseCommandLine(command, NULL); + if ( last_arg ) { + ++argc; + } + argv = (char **)SDL_malloc((argc+1)*(sizeof *argv)); + if ( argv == NULL ) { + return(NULL); + } + argc = ParseCommandLine(command, argv); + + /* Add last command line argument */ + if ( last_arg ) { + argv[argc++] = last_arg; + } + argv[argc] = NULL; + + /* We're ready! */ + return(argv); } /* Start playback of a given music stream */ void MusicCMD_Start(MusicCMD *music) { #ifdef HAVE_FORK - music->pid = fork(); + music->pid = fork(); #else - music->pid = vfork(); + music->pid = vfork(); #endif - switch(music->pid) { - /* Failed fork() system call */ - case -1: - Mix_SetError("fork() failed"); - return; - - /* Child process - executes here */ - case 0: { - char *command; - char **argv; - - /* Unblock signals in case we're called from a thread */ - { - sigset_t mask; - sigemptyset(&mask); - sigprocmask(SIG_SETMASK, &mask, NULL); - } - - /* Execute the command */ - command = SDL_strdup(music->cmd); - argv = parse_args(command, music->file); - if ( argv != NULL ) { - execvp(argv[0], argv); - } - SDL_free(command); - - /* exec() failed */ - perror(argv[0]); - _exit(-1); - } - break; - - /* Parent process - executes here */ - default: - break; - } - return; + switch(music->pid) { + /* Failed fork() system call */ + case -1: + Mix_SetError("fork() failed"); + return; + + /* Child process - executes here */ + case 0: { + char *command; + char **argv; + + /* Unblock signals in case we're called from a thread */ + { + sigset_t mask; + sigemptyset(&mask); + sigprocmask(SIG_SETMASK, &mask, NULL); + } + + /* Execute the command */ + command = SDL_strdup(music->cmd); + argv = parse_args(command, music->file); + if ( argv != NULL ) { + execvp(argv[0], argv); + } + SDL_free(command); + + /* exec() failed */ + perror(argv[0]); + _exit(-1); + } + break; + + /* Parent process - executes here */ + default: + break; + } + return; } /* Stop playback of a stream previously started with MusicCMD_Start() */ void MusicCMD_Stop(MusicCMD *music) { - int status; - - if ( music->pid > 0 ) { - while ( kill(music->pid, 0) == 0 ) { - kill(music->pid, SIGTERM); - sleep(1); - waitpid(music->pid, &status, WNOHANG); - } - music->pid = 0; - } + int status; + + if ( music->pid > 0 ) { + while ( kill(music->pid, 0) == 0 ) { + kill(music->pid, SIGTERM); + sleep(1); + waitpid(music->pid, &status, WNOHANG); + } + music->pid = 0; + } } /* Pause playback of a given music stream */ void MusicCMD_Pause(MusicCMD *music) { - if ( music->pid > 0 ) { - kill(music->pid, SIGSTOP); - } + if ( music->pid > 0 ) { + kill(music->pid, SIGSTOP); + } } /* Resume playback of a given music stream */ void MusicCMD_Resume(MusicCMD *music) { - if ( music->pid > 0 ) { - kill(music->pid, SIGCONT); - } + if ( music->pid > 0 ) { + kill(music->pid, SIGCONT); + } } /* Close the given music stream */ void MusicCMD_FreeSong(MusicCMD *music) { - SDL_free(music->file); - SDL_free(music->cmd); - SDL_free(music); + SDL_free(music->file); + SDL_free(music->cmd); + SDL_free(music); } /* Return non-zero if a stream is currently playing */ int MusicCMD_Active(MusicCMD *music) { - int status; - int active; - - active = 0; - if ( music->pid > 0 ) { - waitpid(music->pid, &status, WNOHANG); - if ( kill(music->pid, 0) == 0 ) { - active = 1; - } - } - return(active); + int status; + int active; + + active = 0; + if ( music->pid > 0 ) { + waitpid(music->pid, &status, WNOHANG); + if ( kill(music->pid, 0) == 0 ) { + active = 1; + } + } + return(active); } #endif /* CMD_MUSIC */ diff --git a/music_cmd.h b/music_cmd.h index 70ec3125..5b5b0777 100644 --- a/music_cmd.h +++ b/music_cmd.h @@ -30,9 +30,9 @@ # include #endif typedef struct { - char *file; - char *cmd; - pid_t pid; + char *file; + char *cmd; + pid_t pid; } MusicCMD; /* Unimplemented */ diff --git a/music_flac.c b/music_flac.c index dd00790b..8ee9f4cf 100644 --- a/music_flac.c +++ b/music_flac.c @@ -19,7 +19,7 @@ 3. This notice may not be removed or altered from any source distribution. This file is used to support SDL_LoadMUS playback of FLAC files. - ~ Austen Dicken (admin@cvpcs.org) + ~ Austen Dicken (admin@cvpcs.org) */ #ifdef FLAC_MUSIC @@ -40,555 +40,555 @@ static SDL_AudioSpec mixer; */ int FLAC_init(SDL_AudioSpec *mixerfmt) { - mixer = *mixerfmt; - return(0); + mixer = *mixerfmt; + return(0); } /* Set the volume for an FLAC stream */ void FLAC_setvolume(FLAC_music *music, int volume) { - music->volume = volume; + music->volume = volume; } static FLAC__StreamDecoderReadStatus flac_read_music_cb( - const FLAC__StreamDecoder *decoder, - FLAC__byte buffer[], - size_t *bytes, - void *client_data) + const FLAC__StreamDecoder *decoder, + FLAC__byte buffer[], + size_t *bytes, + void *client_data) { - FLAC_music *data = (FLAC_music*)client_data; - - // make sure there is something to be reading - if (*bytes > 0) { - *bytes = SDL_RWread (data->rwops, buffer, sizeof (FLAC__byte), *bytes); - - if (*bytes == 0 ) { // error or no data was read (EOF) - return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; - } - else { // data was read, continue - return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; - } - } - else { - return FLAC__STREAM_DECODER_READ_STATUS_ABORT; - } + FLAC_music *data = (FLAC_music*)client_data; + + // make sure there is something to be reading + if (*bytes > 0) { + *bytes = SDL_RWread (data->rwops, buffer, sizeof (FLAC__byte), *bytes); + + if (*bytes == 0 ) { // error or no data was read (EOF) + return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; + } + else { // data was read, continue + return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; + } + } + else { + return FLAC__STREAM_DECODER_READ_STATUS_ABORT; + } } static FLAC__StreamDecoderSeekStatus flac_seek_music_cb( - const FLAC__StreamDecoder *decoder, - FLAC__uint64 absolute_byte_offset, - void *client_data) + const FLAC__StreamDecoder *decoder, + FLAC__uint64 absolute_byte_offset, + void *client_data) { - FLAC_music *data = (FLAC_music*)client_data; - - if (SDL_RWseek (data->rwops, absolute_byte_offset, RW_SEEK_SET) < 0) { - return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; - } - else { - return FLAC__STREAM_DECODER_SEEK_STATUS_OK; - } + FLAC_music *data = (FLAC_music*)client_data; + + if (SDL_RWseek (data->rwops, absolute_byte_offset, RW_SEEK_SET) < 0) { + return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; + } + else { + return FLAC__STREAM_DECODER_SEEK_STATUS_OK; + } } static FLAC__StreamDecoderTellStatus flac_tell_music_cb( - const FLAC__StreamDecoder *decoder, - FLAC__uint64 *absolute_byte_offset, - void *client_data ) + const FLAC__StreamDecoder *decoder, + FLAC__uint64 *absolute_byte_offset, + void *client_data ) { - FLAC_music *data = (FLAC_music*)client_data; + FLAC_music *data = (FLAC_music*)client_data; - int pos = SDL_RWtell (data->rwops); + int pos = SDL_RWtell (data->rwops); - if (pos < 0) { - return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; - } - else { - *absolute_byte_offset = (FLAC__uint64)pos; - return FLAC__STREAM_DECODER_TELL_STATUS_OK; - } + if (pos < 0) { + return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; + } + else { + *absolute_byte_offset = (FLAC__uint64)pos; + return FLAC__STREAM_DECODER_TELL_STATUS_OK; + } } static FLAC__StreamDecoderLengthStatus flac_length_music_cb ( - const FLAC__StreamDecoder *decoder, - FLAC__uint64 *stream_length, - void *client_data) + const FLAC__StreamDecoder *decoder, + FLAC__uint64 *stream_length, + void *client_data) { - FLAC_music *data = (FLAC_music*)client_data; - - int pos = SDL_RWtell (data->rwops); - int length = SDL_RWseek (data->rwops, 0, RW_SEEK_END); - - if (SDL_RWseek (data->rwops, pos, RW_SEEK_SET) != pos || length < 0) { - /* there was an error attempting to return the stream to the original - * position, or the length was invalid. */ - return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; - } - else { - *stream_length = (FLAC__uint64)length; - return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; - } + FLAC_music *data = (FLAC_music*)client_data; + + int pos = SDL_RWtell (data->rwops); + int length = SDL_RWseek (data->rwops, 0, RW_SEEK_END); + + if (SDL_RWseek (data->rwops, pos, RW_SEEK_SET) != pos || length < 0) { + /* there was an error attempting to return the stream to the original + * position, or the length was invalid. */ + return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; + } + else { + *stream_length = (FLAC__uint64)length; + return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; + } } static FLAC__bool flac_eof_music_cb( - const FLAC__StreamDecoder *decoder, - void *client_data ) + const FLAC__StreamDecoder *decoder, + void *client_data ) { - FLAC_music *data = (FLAC_music*)client_data; - - int pos = SDL_RWtell (data->rwops); - int end = SDL_RWseek (data->rwops, 0, RW_SEEK_END); - - // was the original position equal to the end (a.k.a. the seek didn't move)? - if (pos == end) { - // must be EOF - return true; - } - else { - // not EOF, return to the original position - SDL_RWseek (data->rwops, pos, RW_SEEK_SET); - - return false; - } + FLAC_music *data = (FLAC_music*)client_data; + + int pos = SDL_RWtell (data->rwops); + int end = SDL_RWseek (data->rwops, 0, RW_SEEK_END); + + // was the original position equal to the end (a.k.a. the seek didn't move)? + if (pos == end) { + // must be EOF + return true; + } + else { + // not EOF, return to the original position + SDL_RWseek (data->rwops, pos, RW_SEEK_SET); + + return false; + } } static FLAC__StreamDecoderWriteStatus flac_write_music_cb( - const FLAC__StreamDecoder *decoder, - const FLAC__Frame *frame, - const FLAC__int32 *const buffer[], - void *client_data) + const FLAC__StreamDecoder *decoder, + const FLAC__Frame *frame, + const FLAC__int32 *const buffer[], + void *client_data) { - FLAC_music *data = (FLAC_music *)client_data; - size_t i; - - if (data->flac_data.total_samples == 0) { - SDL_SetError ("Given FLAC file does not specify its sample count."); - return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; - } - - if (data->flac_data.channels != 2 || - data->flac_data.bits_per_sample != 16) { - SDL_SetError("Current FLAC support is only for 16 bit Stereo files."); - return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; - } - - for (i = 0; i < frame->header.blocksize; i++) { - FLAC__int16 i16; - FLAC__uint16 ui16; - - // make sure we still have at least two bytes that can be read (one for - // each channel) - if (data->flac_data.max_to_read >= 4) { - // does the data block exist? - if (!data->flac_data.data) { - data->flac_data.data_len = data->flac_data.max_to_read; - data->flac_data.data_read = 0; - - // create it - data->flac_data.data = - (char *)SDL_malloc (data->flac_data.data_len); - - if (!data->flac_data.data) { - return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; - } - } - - i16 = (FLAC__int16)buffer[0][i]; - ui16 = (FLAC__uint16)i16; - - *((data->flac_data.data) + (data->flac_data.data_read++)) = - (char)(ui16); - *((data->flac_data.data) + (data->flac_data.data_read++)) = - (char)(ui16 >> 8); - - i16 = (FLAC__int16)buffer[1][i]; - ui16 = (FLAC__uint16)i16; - - *((data->flac_data.data) + (data->flac_data.data_read++)) = - (char)(ui16); - *((data->flac_data.data) + (data->flac_data.data_read++)) = - (char)(ui16 >> 8); - - data->flac_data.max_to_read -= 4; - - if (data->flac_data.max_to_read < 4) { - // we need to set this so that the read halts from the - // FLAC_getsome function. - data->flac_data.max_to_read = 0; - } - } - else { - // we need to write to the overflow - if (!data->flac_data.overflow) { - data->flac_data.overflow_len = - 4 * (frame->header.blocksize - i); - data->flac_data.overflow_read = 0; - - // make it big enough for the rest of the block - data->flac_data.overflow = - (char *)SDL_malloc (data->flac_data.overflow_len); - - if (!data->flac_data.overflow) { - return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; - } - } - - i16 = (FLAC__int16)buffer[0][i]; - ui16 = (FLAC__uint16)i16; - - *((data->flac_data.overflow) + (data->flac_data.overflow_read++)) = - (char)(ui16); - *((data->flac_data.overflow) + (data->flac_data.overflow_read++)) = - (char)(ui16 >> 8); - - i16 = (FLAC__int16)buffer[1][i]; - ui16 = (FLAC__uint16)i16; - - *((data->flac_data.overflow) + (data->flac_data.overflow_read++)) = - (char)(ui16); - *((data->flac_data.overflow) + (data->flac_data.overflow_read++)) = - (char)(ui16 >> 8); - } - } - - return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; + FLAC_music *data = (FLAC_music *)client_data; + size_t i; + + if (data->flac_data.total_samples == 0) { + SDL_SetError ("Given FLAC file does not specify its sample count."); + return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; + } + + if (data->flac_data.channels != 2 || + data->flac_data.bits_per_sample != 16) { + SDL_SetError("Current FLAC support is only for 16 bit Stereo files."); + return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; + } + + for (i = 0; i < frame->header.blocksize; i++) { + FLAC__int16 i16; + FLAC__uint16 ui16; + + // make sure we still have at least two bytes that can be read (one for + // each channel) + if (data->flac_data.max_to_read >= 4) { + // does the data block exist? + if (!data->flac_data.data) { + data->flac_data.data_len = data->flac_data.max_to_read; + data->flac_data.data_read = 0; + + // create it + data->flac_data.data = + (char *)SDL_malloc (data->flac_data.data_len); + + if (!data->flac_data.data) { + return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; + } + } + + i16 = (FLAC__int16)buffer[0][i]; + ui16 = (FLAC__uint16)i16; + + *((data->flac_data.data) + (data->flac_data.data_read++)) = + (char)(ui16); + *((data->flac_data.data) + (data->flac_data.data_read++)) = + (char)(ui16 >> 8); + + i16 = (FLAC__int16)buffer[1][i]; + ui16 = (FLAC__uint16)i16; + + *((data->flac_data.data) + (data->flac_data.data_read++)) = + (char)(ui16); + *((data->flac_data.data) + (data->flac_data.data_read++)) = + (char)(ui16 >> 8); + + data->flac_data.max_to_read -= 4; + + if (data->flac_data.max_to_read < 4) { + // we need to set this so that the read halts from the + // FLAC_getsome function. + data->flac_data.max_to_read = 0; + } + } + else { + // we need to write to the overflow + if (!data->flac_data.overflow) { + data->flac_data.overflow_len = + 4 * (frame->header.blocksize - i); + data->flac_data.overflow_read = 0; + + // make it big enough for the rest of the block + data->flac_data.overflow = + (char *)SDL_malloc (data->flac_data.overflow_len); + + if (!data->flac_data.overflow) { + return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; + } + } + + i16 = (FLAC__int16)buffer[0][i]; + ui16 = (FLAC__uint16)i16; + + *((data->flac_data.overflow) + (data->flac_data.overflow_read++)) = + (char)(ui16); + *((data->flac_data.overflow) + (data->flac_data.overflow_read++)) = + (char)(ui16 >> 8); + + i16 = (FLAC__int16)buffer[1][i]; + ui16 = (FLAC__uint16)i16; + + *((data->flac_data.overflow) + (data->flac_data.overflow_read++)) = + (char)(ui16); + *((data->flac_data.overflow) + (data->flac_data.overflow_read++)) = + (char)(ui16 >> 8); + } + } + + return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; } static void flac_metadata_music_cb( - const FLAC__StreamDecoder *decoder, - const FLAC__StreamMetadata *metadata, - void *client_data) + const FLAC__StreamDecoder *decoder, + const FLAC__StreamMetadata *metadata, + void *client_data) { - FLAC_music *data = (FLAC_music *)client_data; - - if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) { - data->flac_data.sample_rate = metadata->data.stream_info.sample_rate; - data->flac_data.channels = metadata->data.stream_info.channels; - data->flac_data.total_samples = - metadata->data.stream_info.total_samples; - data->flac_data.bits_per_sample = - metadata->data.stream_info.bits_per_sample; - data->flac_data.sample_size = data->flac_data.channels * - ((data->flac_data.bits_per_sample) / 8); - } + FLAC_music *data = (FLAC_music *)client_data; + + if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) { + data->flac_data.sample_rate = metadata->data.stream_info.sample_rate; + data->flac_data.channels = metadata->data.stream_info.channels; + data->flac_data.total_samples = + metadata->data.stream_info.total_samples; + data->flac_data.bits_per_sample = + metadata->data.stream_info.bits_per_sample; + data->flac_data.sample_size = data->flac_data.channels * + ((data->flac_data.bits_per_sample) / 8); + } } static void flac_error_music_cb( - const FLAC__StreamDecoder *decoder, - FLAC__StreamDecoderErrorStatus status, - void *client_data) + const FLAC__StreamDecoder *decoder, + FLAC__StreamDecoderErrorStatus status, + void *client_data) { - // print an SDL error based on the error status - switch (status) { - case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC: - SDL_SetError ("Error processing the FLAC file [LOST_SYNC]."); - break; - case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER: - SDL_SetError ("Error processing the FLAC file [BAD_HEADER]."); - break; - case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH: - SDL_SetError ("Error processing the FLAC file [CRC_MISMATCH]."); - break; - case FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM: - SDL_SetError ("Error processing the FLAC file [UNPARSEABLE]."); - break; - default: - SDL_SetError ("Error processing the FLAC file [UNKNOWN]."); - break; - } + // print an SDL error based on the error status + switch (status) { + case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC: + SDL_SetError ("Error processing the FLAC file [LOST_SYNC]."); + break; + case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER: + SDL_SetError ("Error processing the FLAC file [BAD_HEADER]."); + break; + case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH: + SDL_SetError ("Error processing the FLAC file [CRC_MISMATCH]."); + break; + case FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM: + SDL_SetError ("Error processing the FLAC file [UNPARSEABLE]."); + break; + default: + SDL_SetError ("Error processing the FLAC file [UNKNOWN]."); + break; + } } /* Load an FLAC stream from an SDL_RWops object */ FLAC_music *FLAC_new_RW(SDL_RWops *rw, int freerw) { - FLAC_music *music; - int init_stage = 0; - int was_error = 1; - - if (!Mix_Init(MIX_INIT_FLAC)) { - if (freerw) { - SDL_RWclose(rw); - } - return NULL; - } - - music = (FLAC_music *)SDL_malloc ( sizeof (*music)); - if (music) { - /* Initialize the music structure */ - memset (music, 0, (sizeof (*music))); - FLAC_stop (music); - FLAC_setvolume (music, MIX_MAX_VOLUME); - music->section = -1; - music->rwops = rw; - music->freerw = freerw; - music->flac_data.max_to_read = 0; - music->flac_data.overflow = NULL; - music->flac_data.overflow_len = 0; - music->flac_data.overflow_read = 0; - music->flac_data.data = NULL; - music->flac_data.data_len = 0; - music->flac_data.data_read = 0; - - init_stage++; // stage 1! - - music->flac_decoder = flac.FLAC__stream_decoder_new (); - - if (music->flac_decoder != NULL) { - init_stage++; // stage 2! - - if (flac.FLAC__stream_decoder_init_stream( - music->flac_decoder, - flac_read_music_cb, flac_seek_music_cb, - flac_tell_music_cb, flac_length_music_cb, - flac_eof_music_cb, flac_write_music_cb, - flac_metadata_music_cb, flac_error_music_cb, - music) == FLAC__STREAM_DECODER_INIT_STATUS_OK ) { - init_stage++; // stage 3! - - if (flac.FLAC__stream_decoder_process_until_end_of_metadata - (music->flac_decoder)) { - was_error = 0; - } else { - SDL_SetError("FLAC__stream_decoder_process_until_end_of_metadata() failed"); - } - } else { - SDL_SetError("FLAC__stream_decoder_init_stream() failed"); - } - } else { - SDL_SetError("FLAC__stream_decoder_new() failed"); - } - - if (was_error) { - switch (init_stage) { - case 3: - flac.FLAC__stream_decoder_finish( music->flac_decoder ); - case 2: - flac.FLAC__stream_decoder_delete( music->flac_decoder ); - case 1: - case 0: - SDL_free(music); - if (freerw) { - SDL_RWclose(rw); - } - break; - } - return NULL; - } - } else { - SDL_OutOfMemory(); - if (freerw) { - SDL_RWclose(rw); - } - return NULL; - } - - return music; + FLAC_music *music; + int init_stage = 0; + int was_error = 1; + + if (!Mix_Init(MIX_INIT_FLAC)) { + if (freerw) { + SDL_RWclose(rw); + } + return NULL; + } + + music = (FLAC_music *)SDL_malloc ( sizeof (*music)); + if (music) { + /* Initialize the music structure */ + memset (music, 0, (sizeof (*music))); + FLAC_stop (music); + FLAC_setvolume (music, MIX_MAX_VOLUME); + music->section = -1; + music->rwops = rw; + music->freerw = freerw; + music->flac_data.max_to_read = 0; + music->flac_data.overflow = NULL; + music->flac_data.overflow_len = 0; + music->flac_data.overflow_read = 0; + music->flac_data.data = NULL; + music->flac_data.data_len = 0; + music->flac_data.data_read = 0; + + init_stage++; // stage 1! + + music->flac_decoder = flac.FLAC__stream_decoder_new (); + + if (music->flac_decoder != NULL) { + init_stage++; // stage 2! + + if (flac.FLAC__stream_decoder_init_stream( + music->flac_decoder, + flac_read_music_cb, flac_seek_music_cb, + flac_tell_music_cb, flac_length_music_cb, + flac_eof_music_cb, flac_write_music_cb, + flac_metadata_music_cb, flac_error_music_cb, + music) == FLAC__STREAM_DECODER_INIT_STATUS_OK ) { + init_stage++; // stage 3! + + if (flac.FLAC__stream_decoder_process_until_end_of_metadata + (music->flac_decoder)) { + was_error = 0; + } else { + SDL_SetError("FLAC__stream_decoder_process_until_end_of_metadata() failed"); + } + } else { + SDL_SetError("FLAC__stream_decoder_init_stream() failed"); + } + } else { + SDL_SetError("FLAC__stream_decoder_new() failed"); + } + + if (was_error) { + switch (init_stage) { + case 3: + flac.FLAC__stream_decoder_finish( music->flac_decoder ); + case 2: + flac.FLAC__stream_decoder_delete( music->flac_decoder ); + case 1: + case 0: + SDL_free(music); + if (freerw) { + SDL_RWclose(rw); + } + break; + } + return NULL; + } + } else { + SDL_OutOfMemory(); + if (freerw) { + SDL_RWclose(rw); + } + return NULL; + } + + return music; } /* Start playback of a given FLAC stream */ void FLAC_play(FLAC_music *music) { - music->playing = 1; + music->playing = 1; } /* Return non-zero if a stream is currently playing */ int FLAC_playing(FLAC_music *music) { - return(music->playing); + return(music->playing); } /* Read some FLAC stream data and convert it for output */ static void FLAC_getsome(FLAC_music *music) { - SDL_AudioCVT *cvt; - - /* GET AUDIO WAVE DATA */ - // set the max number of characters to read - music->flac_data.max_to_read = 8192; - music->flac_data.data_len = music->flac_data.max_to_read; - music->flac_data.data_read = 0; - if (!music->flac_data.data) { - music->flac_data.data = (char *)SDL_malloc (music->flac_data.data_len); - } - - // we have data to read - while(music->flac_data.max_to_read > 0) { - // first check if there is data in the overflow from before - if (music->flac_data.overflow) { - size_t overflow_len = music->flac_data.overflow_read; - - if (overflow_len > music->flac_data.max_to_read) { - size_t overflow_extra_len = overflow_len - - music->flac_data.max_to_read; - - memcpy (music->flac_data.data+music->flac_data.data_read, - music->flac_data.overflow, music->flac_data.max_to_read); - music->flac_data.data_read += music->flac_data.max_to_read; - memcpy (music->flac_data.overflow, - music->flac_data.overflow + music->flac_data.max_to_read, - overflow_extra_len); - music->flac_data.overflow_len = overflow_extra_len; - music->flac_data.overflow_read = overflow_extra_len; - music->flac_data.max_to_read = 0; - } - else { - memcpy (music->flac_data.data+music->flac_data.data_read, - music->flac_data.overflow, overflow_len); - music->flac_data.data_read += overflow_len; - free (music->flac_data.overflow); - music->flac_data.overflow = NULL; - music->flac_data.overflow_len = 0; - music->flac_data.overflow_read = 0; - music->flac_data.max_to_read -= overflow_len; - } - } - else { - if (!flac.FLAC__stream_decoder_process_single ( - music->flac_decoder)) { - music->flac_data.max_to_read = 0; - } - - if (flac.FLAC__stream_decoder_get_state (music->flac_decoder) - == FLAC__STREAM_DECODER_END_OF_STREAM) { - music->flac_data.max_to_read = 0; - } - } - } - - if (music->flac_data.data_read <= 0) { - if (music->flac_data.data_read == 0) { - music->playing = 0; - } - return; - } - cvt = &music->cvt; - if (music->section < 0) { - - SDL_BuildAudioCVT (cvt, AUDIO_S16, (Uint8)music->flac_data.channels, - (int)music->flac_data.sample_rate, mixer.format, - mixer.channels, mixer.freq); - if (cvt->buf) { - free (cvt->buf); - } - cvt->buf = (Uint8 *)SDL_malloc (music->flac_data.data_len * cvt->len_mult); - music->section = 0; - } - if (cvt->buf) { - memcpy (cvt->buf, music->flac_data.data, music->flac_data.data_read); - if (cvt->needed) { - cvt->len = music->flac_data.data_read; - SDL_ConvertAudio (cvt); - } - else { - cvt->len_cvt = music->flac_data.data_read; - } - music->len_available = music->cvt.len_cvt; - music->snd_available = music->cvt.buf; - } - else { - SDL_SetError ("Out of memory"); - music->playing = 0; - } + SDL_AudioCVT *cvt; + + /* GET AUDIO WAVE DATA */ + // set the max number of characters to read + music->flac_data.max_to_read = 8192; + music->flac_data.data_len = music->flac_data.max_to_read; + music->flac_data.data_read = 0; + if (!music->flac_data.data) { + music->flac_data.data = (char *)SDL_malloc (music->flac_data.data_len); + } + + // we have data to read + while(music->flac_data.max_to_read > 0) { + // first check if there is data in the overflow from before + if (music->flac_data.overflow) { + size_t overflow_len = music->flac_data.overflow_read; + + if (overflow_len > music->flac_data.max_to_read) { + size_t overflow_extra_len = overflow_len - + music->flac_data.max_to_read; + + memcpy (music->flac_data.data+music->flac_data.data_read, + music->flac_data.overflow, music->flac_data.max_to_read); + music->flac_data.data_read += music->flac_data.max_to_read; + memcpy (music->flac_data.overflow, + music->flac_data.overflow + music->flac_data.max_to_read, + overflow_extra_len); + music->flac_data.overflow_len = overflow_extra_len; + music->flac_data.overflow_read = overflow_extra_len; + music->flac_data.max_to_read = 0; + } + else { + memcpy (music->flac_data.data+music->flac_data.data_read, + music->flac_data.overflow, overflow_len); + music->flac_data.data_read += overflow_len; + free (music->flac_data.overflow); + music->flac_data.overflow = NULL; + music->flac_data.overflow_len = 0; + music->flac_data.overflow_read = 0; + music->flac_data.max_to_read -= overflow_len; + } + } + else { + if (!flac.FLAC__stream_decoder_process_single ( + music->flac_decoder)) { + music->flac_data.max_to_read = 0; + } + + if (flac.FLAC__stream_decoder_get_state (music->flac_decoder) + == FLAC__STREAM_DECODER_END_OF_STREAM) { + music->flac_data.max_to_read = 0; + } + } + } + + if (music->flac_data.data_read <= 0) { + if (music->flac_data.data_read == 0) { + music->playing = 0; + } + return; + } + cvt = &music->cvt; + if (music->section < 0) { + + SDL_BuildAudioCVT (cvt, AUDIO_S16, (Uint8)music->flac_data.channels, + (int)music->flac_data.sample_rate, mixer.format, + mixer.channels, mixer.freq); + if (cvt->buf) { + free (cvt->buf); + } + cvt->buf = (Uint8 *)SDL_malloc (music->flac_data.data_len * cvt->len_mult); + music->section = 0; + } + if (cvt->buf) { + memcpy (cvt->buf, music->flac_data.data, music->flac_data.data_read); + if (cvt->needed) { + cvt->len = music->flac_data.data_read; + SDL_ConvertAudio (cvt); + } + else { + cvt->len_cvt = music->flac_data.data_read; + } + music->len_available = music->cvt.len_cvt; + music->snd_available = music->cvt.buf; + } + else { + SDL_SetError ("Out of memory"); + music->playing = 0; + } } /* Play some of a stream previously started with FLAC_play() */ int FLAC_playAudio(FLAC_music *music, Uint8 *snd, int len) { - int mixable; - - while ((len > 0) && music->playing) { - if (!music->len_available) { - FLAC_getsome (music); - } - mixable = len; - if (mixable > music->len_available) { - mixable = music->len_available; - } - if (music->volume == MIX_MAX_VOLUME) { - memcpy (snd, music->snd_available, mixable); - } - else { - SDL_MixAudio (snd, music->snd_available, mixable, music->volume); - } - music->len_available -= mixable; - music->snd_available += mixable; - len -= mixable; - snd += mixable; - } - - return len; + int mixable; + + while ((len > 0) && music->playing) { + if (!music->len_available) { + FLAC_getsome (music); + } + mixable = len; + if (mixable > music->len_available) { + mixable = music->len_available; + } + if (music->volume == MIX_MAX_VOLUME) { + memcpy (snd, music->snd_available, mixable); + } + else { + SDL_MixAudio (snd, music->snd_available, mixable, music->volume); + } + music->len_available -= mixable; + music->snd_available += mixable; + len -= mixable; + snd += mixable; + } + + return len; } /* Stop playback of a stream previously started with FLAC_play() */ void FLAC_stop(FLAC_music *music) { - music->playing = 0; + music->playing = 0; } /* Close the given FLAC_music object */ void FLAC_delete(FLAC_music *music) { - if (music) { - if (music->flac_decoder) { - flac.FLAC__stream_decoder_finish (music->flac_decoder); - flac.FLAC__stream_decoder_delete (music->flac_decoder); - } - - if (music->flac_data.data) { - free (music->flac_data.data); - } - - if (music->flac_data.overflow) { - free (music->flac_data.overflow); - } - - if (music->cvt.buf) { - free (music->cvt.buf); - } - - if (music->freerw) { - SDL_RWclose(music->rwops); - } - free (music); - } + if (music) { + if (music->flac_decoder) { + flac.FLAC__stream_decoder_finish (music->flac_decoder); + flac.FLAC__stream_decoder_delete (music->flac_decoder); + } + + if (music->flac_data.data) { + free (music->flac_data.data); + } + + if (music->flac_data.overflow) { + free (music->flac_data.overflow); + } + + if (music->cvt.buf) { + free (music->cvt.buf); + } + + if (music->freerw) { + SDL_RWclose(music->rwops); + } + free (music); + } } /* Jump (seek) to a given position (time is in seconds) */ void FLAC_jump_to_time(FLAC_music *music, double time) { - if (music) { - if (music->flac_decoder) { - double seek_sample = music->flac_data.sample_rate * time; - - // clear data if it has data - if (music->flac_data.data) { - free (music->flac_data.data); - music->flac_data.data = NULL; - } - - // clear overflow if it has data - if (music->flac_data.overflow) { - free (music->flac_data.overflow); - music->flac_data.overflow = NULL; - } - - if (!flac.FLAC__stream_decoder_seek_absolute (music->flac_decoder, - (FLAC__uint64)seek_sample)) { - if (flac.FLAC__stream_decoder_get_state (music->flac_decoder) - == FLAC__STREAM_DECODER_SEEK_ERROR) { - flac.FLAC__stream_decoder_flush (music->flac_decoder); - } - - SDL_SetError - ("Seeking of FLAC stream failed: libFLAC seek failed."); - } - } - else { - SDL_SetError - ("Seeking of FLAC stream failed: FLAC decoder was NULL."); - } - } - else { - SDL_SetError ("Seeking of FLAC stream failed: music was NULL."); - } + if (music) { + if (music->flac_decoder) { + double seek_sample = music->flac_data.sample_rate * time; + + // clear data if it has data + if (music->flac_data.data) { + free (music->flac_data.data); + music->flac_data.data = NULL; + } + + // clear overflow if it has data + if (music->flac_data.overflow) { + free (music->flac_data.overflow); + music->flac_data.overflow = NULL; + } + + if (!flac.FLAC__stream_decoder_seek_absolute (music->flac_decoder, + (FLAC__uint64)seek_sample)) { + if (flac.FLAC__stream_decoder_get_state (music->flac_decoder) + == FLAC__STREAM_DECODER_SEEK_ERROR) { + flac.FLAC__stream_decoder_flush (music->flac_decoder); + } + + SDL_SetError + ("Seeking of FLAC stream failed: libFLAC seek failed."); + } + } + else { + SDL_SetError + ("Seeking of FLAC stream failed: FLAC decoder was NULL."); + } + } + else { + SDL_SetError ("Seeking of FLAC stream failed: music was NULL."); + } } #endif /* FLAC_MUSIC */ diff --git a/music_flac.h b/music_flac.h index b716694f..a226af81 100644 --- a/music_flac.h +++ b/music_flac.h @@ -19,7 +19,7 @@ 3. This notice may not be removed or altered from any source distribution. Header to handle loading FLAC music files in SDL. - ~ Austen Dicken (admin@cvpcs.org) + ~ Austen Dicken (admin@cvpcs.org) */ /* $Id: $ */ @@ -29,33 +29,33 @@ #include typedef struct { - FLAC__uint64 sample_size; - unsigned sample_rate; - unsigned channels; - unsigned bits_per_sample; - FLAC__uint64 total_samples; - - // the following are used to handle the callback nature of the writer - int max_to_read; - char *data; // pointer to beginning of data array - int data_len; // size of data array - int data_read; // amount of data array used - char *overflow; // pointer to beginning of overflow array - int overflow_len; // size of overflow array - int overflow_read; // amount of overflow array used + FLAC__uint64 sample_size; + unsigned sample_rate; + unsigned channels; + unsigned bits_per_sample; + FLAC__uint64 total_samples; + + // the following are used to handle the callback nature of the writer + int max_to_read; + char *data; // pointer to beginning of data array + int data_len; // size of data array + int data_read; // amount of data array used + char *overflow; // pointer to beginning of overflow array + int overflow_len; // size of overflow array + int overflow_read; // amount of overflow array used } FLAC_Data; typedef struct { - int playing; - int volume; - int section; - FLAC__StreamDecoder *flac_decoder; - FLAC_Data flac_data; - SDL_RWops *rwops; - int freerw; - SDL_AudioCVT cvt; - int len_available; - Uint8 *snd_available; + int playing; + int volume; + int section; + FLAC__StreamDecoder *flac_decoder; + FLAC_Data flac_data; + SDL_RWops *rwops; + int freerw; + SDL_AudioCVT cvt; + int len_available; + Uint8 *snd_available; } FLAC_music; /* Initialize the FLAC player, with the given mixer settings diff --git a/music_mad.c b/music_mad.c index c142779b..2ec8f149 100644 --- a/music_mad.c +++ b/music_mad.c @@ -32,18 +32,18 @@ mad_openFileRW(SDL_RWops *rw, SDL_AudioSpec *mixer, int freerw) mp3_mad = (mad_data *)SDL_malloc(sizeof(mad_data)); if (mp3_mad) { - mp3_mad->rw = rw; - mp3_mad->freerw = freerw; - mad_stream_init(&mp3_mad->stream); - mad_frame_init(&mp3_mad->frame); - mad_synth_init(&mp3_mad->synth); - mp3_mad->frames_read = 0; - mad_timer_reset(&mp3_mad->next_frame_start); - mp3_mad->volume = MIX_MAX_VOLUME; - mp3_mad->status = 0; - mp3_mad->output_begin = 0; - mp3_mad->output_end = 0; - mp3_mad->mixer = *mixer; + mp3_mad->rw = rw; + mp3_mad->freerw = freerw; + mad_stream_init(&mp3_mad->stream); + mad_frame_init(&mp3_mad->frame); + mad_synth_init(&mp3_mad->synth); + mp3_mad->frames_read = 0; + mad_timer_reset(&mp3_mad->next_frame_start); + mp3_mad->volume = MIX_MAX_VOLUME; + mp3_mad->status = 0; + mp3_mad->output_begin = 0; + mp3_mad->output_end = 0; + mp3_mad->mixer = *mixer; } return mp3_mad; } @@ -56,7 +56,7 @@ mad_closeFile(mad_data *mp3_mad) mad_synth_finish(&mp3_mad->synth); if (mp3_mad->freerw) { - SDL_RWclose(mp3_mad->rw); + SDL_RWclose(mp3_mad->rw); } SDL_free(mp3_mad); } @@ -68,7 +68,7 @@ mad_start(mad_data *mp3_mad) { } /* Stops the playback. */ -void +void mad_stop(mad_data *mp3_mad) { mp3_mad->status &= ~MS_playing; } @@ -83,66 +83,66 @@ mad_isPlaying(mad_data *mp3_mad) { false on failure. */ static int read_next_frame(mad_data *mp3_mad) { - if (mp3_mad->stream.buffer == NULL || - mp3_mad->stream.error == MAD_ERROR_BUFLEN) { - size_t read_size; - size_t remaining; - unsigned char *read_start; - - /* There might be some bytes in the buffer left over from last - time. If so, move them down and read more bytes following - them. */ - if (mp3_mad->stream.next_frame != NULL) { - remaining = mp3_mad->stream.bufend - mp3_mad->stream.next_frame; - memmove(mp3_mad->input_buffer, mp3_mad->stream.next_frame, remaining); - read_start = mp3_mad->input_buffer + remaining; - read_size = MAD_INPUT_BUFFER_SIZE - remaining; - - } else { - read_size = MAD_INPUT_BUFFER_SIZE; - read_start = mp3_mad->input_buffer; - remaining = 0; - } - - /* Now read additional bytes from the input file. */ - read_size = SDL_RWread(mp3_mad->rw, read_start, 1, read_size); - - if (read_size <= 0) { - if ((mp3_mad->status & (MS_input_eof | MS_input_error)) == 0) { - if (read_size == 0) { - mp3_mad->status |= MS_input_eof; - } else { - mp3_mad->status |= MS_input_error; - } - - /* At the end of the file, we must stuff MAD_BUFFER_GUARD - number of 0 bytes. */ - memset(read_start + read_size, 0, MAD_BUFFER_GUARD); - read_size += MAD_BUFFER_GUARD; - } - } - - /* Now feed those bytes into the libmad stream. */ - mad_stream_buffer(&mp3_mad->stream, mp3_mad->input_buffer, - read_size + remaining); - mp3_mad->stream.error = MAD_ERROR_NONE; + if (mp3_mad->stream.buffer == NULL || + mp3_mad->stream.error == MAD_ERROR_BUFLEN) { + size_t read_size; + size_t remaining; + unsigned char *read_start; + + /* There might be some bytes in the buffer left over from last + time. If so, move them down and read more bytes following + them. */ + if (mp3_mad->stream.next_frame != NULL) { + remaining = mp3_mad->stream.bufend - mp3_mad->stream.next_frame; + memmove(mp3_mad->input_buffer, mp3_mad->stream.next_frame, remaining); + read_start = mp3_mad->input_buffer + remaining; + read_size = MAD_INPUT_BUFFER_SIZE - remaining; + + } else { + read_size = MAD_INPUT_BUFFER_SIZE; + read_start = mp3_mad->input_buffer; + remaining = 0; + } + + /* Now read additional bytes from the input file. */ + read_size = SDL_RWread(mp3_mad->rw, read_start, 1, read_size); + + if (read_size <= 0) { + if ((mp3_mad->status & (MS_input_eof | MS_input_error)) == 0) { + if (read_size == 0) { + mp3_mad->status |= MS_input_eof; + } else { + mp3_mad->status |= MS_input_error; + } + + /* At the end of the file, we must stuff MAD_BUFFER_GUARD + number of 0 bytes. */ + memset(read_start + read_size, 0, MAD_BUFFER_GUARD); + read_size += MAD_BUFFER_GUARD; + } + } + + /* Now feed those bytes into the libmad stream. */ + mad_stream_buffer(&mp3_mad->stream, mp3_mad->input_buffer, + read_size + remaining); + mp3_mad->stream.error = MAD_ERROR_NONE; } - + /* Now ask libmad to extract a frame from the data we just put in - its buffer. */ + its buffer. */ if (mad_frame_decode(&mp3_mad->frame, &mp3_mad->stream)) { - if (MAD_RECOVERABLE(mp3_mad->stream.error)) { - return 0; - - } else if (mp3_mad->stream.error == MAD_ERROR_BUFLEN) { - return 0; - - } else { - mp3_mad->status |= MS_decode_error; - return 0; - } + if (MAD_RECOVERABLE(mp3_mad->stream.error)) { + return 0; + + } else if (mp3_mad->stream.error == MAD_ERROR_BUFLEN) { + return 0; + + } else { + mp3_mad->status |= MS_decode_error; + return 0; + } } - + mp3_mad->frames_read++; mad_timer_add(&mp3_mad->next_frame_start, mp3_mad->frame.header.duration); @@ -180,12 +180,12 @@ decode_frame(mad_data *mp3_mad) { out = mp3_mad->output_buffer + mp3_mad->output_end; if ((mp3_mad->status & MS_cvt_decoded) == 0) { - mp3_mad->status |= MS_cvt_decoded; + mp3_mad->status |= MS_cvt_decoded; - /* The first frame determines some key properties of the stream. - In particular, it tells us enough to set up the convert - structure now. */ - SDL_BuildAudioCVT(&mp3_mad->cvt, AUDIO_S16, pcm->channels, mp3_mad->frame.header.samplerate, mp3_mad->mixer.format, mp3_mad->mixer.channels, mp3_mad->mixer.freq); + /* The first frame determines some key properties of the stream. + In particular, it tells us enough to set up the convert + structure now. */ + SDL_BuildAudioCVT(&mp3_mad->cvt, AUDIO_S16, pcm->channels, mp3_mad->frame.header.samplerate, mp3_mad->mixer.format, mp3_mad->mixer.channels, mp3_mad->mixer.freq); } /* pcm->samplerate contains the sampling frequency */ @@ -222,54 +222,54 @@ mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len) { Uint8 *out; if ((mp3_mad->status & MS_playing) == 0) { - /* We're not supposed to be playing, so send silence instead. */ - memset(stream, 0, len); - return 0; + /* We're not supposed to be playing, so send silence instead. */ + memset(stream, 0, len); + return 0; } out = stream; bytes_remaining = len; while (bytes_remaining > 0) { - if (mp3_mad->output_end == mp3_mad->output_begin) { - /* We need to get a new frame. */ - mp3_mad->output_begin = 0; - mp3_mad->output_end = 0; - if (!read_next_frame(mp3_mad)) { - if ((mp3_mad->status & MS_error_flags) != 0) { - /* Couldn't read a frame; either an error condition or - end-of-file. Stop. */ - memset(out, 0, bytes_remaining); - mp3_mad->status &= ~MS_playing; - return bytes_remaining; - } - } else { - decode_frame(mp3_mad); - - /* Now convert the frame data to the appropriate format for - output. */ - mp3_mad->cvt.buf = mp3_mad->output_buffer; - mp3_mad->cvt.len = mp3_mad->output_end; - - mp3_mad->output_end = (int)(mp3_mad->output_end * mp3_mad->cvt.len_ratio); - /*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/ - SDL_ConvertAudio(&mp3_mad->cvt); - } - } - - num_bytes = mp3_mad->output_end - mp3_mad->output_begin; - if (bytes_remaining < num_bytes) { - num_bytes = bytes_remaining; - } - - if (mp3_mad->volume == MIX_MAX_VOLUME) { - memcpy(out, mp3_mad->output_buffer + mp3_mad->output_begin, num_bytes); - } else { - SDL_MixAudio(out, mp3_mad->output_buffer + mp3_mad->output_begin, - num_bytes, mp3_mad->volume); - } - out += num_bytes; - mp3_mad->output_begin += num_bytes; - bytes_remaining -= num_bytes; + if (mp3_mad->output_end == mp3_mad->output_begin) { + /* We need to get a new frame. */ + mp3_mad->output_begin = 0; + mp3_mad->output_end = 0; + if (!read_next_frame(mp3_mad)) { + if ((mp3_mad->status & MS_error_flags) != 0) { + /* Couldn't read a frame; either an error condition or + end-of-file. Stop. */ + memset(out, 0, bytes_remaining); + mp3_mad->status &= ~MS_playing; + return bytes_remaining; + } + } else { + decode_frame(mp3_mad); + + /* Now convert the frame data to the appropriate format for + output. */ + mp3_mad->cvt.buf = mp3_mad->output_buffer; + mp3_mad->cvt.len = mp3_mad->output_end; + + mp3_mad->output_end = (int)(mp3_mad->output_end * mp3_mad->cvt.len_ratio); + /*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/ + SDL_ConvertAudio(&mp3_mad->cvt); + } + } + + num_bytes = mp3_mad->output_end - mp3_mad->output_begin; + if (bytes_remaining < num_bytes) { + num_bytes = bytes_remaining; + } + + if (mp3_mad->volume == MIX_MAX_VOLUME) { + memcpy(out, mp3_mad->output_buffer + mp3_mad->output_begin, num_bytes); + } else { + SDL_MixAudio(out, mp3_mad->output_buffer + mp3_mad->output_begin, + num_bytes, mp3_mad->volume); + } + out += num_bytes; + mp3_mad->output_begin += num_bytes; + bytes_remaining -= num_bytes; } return 0; } @@ -280,42 +280,42 @@ mad_seek(mad_data *mp3_mad, double position) { int int_part; int_part = (int)position; - mad_timer_set(&target, int_part, - (int)((position - int_part) * 1000000), 1000000); + mad_timer_set(&target, int_part, + (int)((position - int_part) * 1000000), 1000000); if (mad_timer_compare(mp3_mad->next_frame_start, target) > 0) { - /* In order to seek backwards in a VBR file, we have to rewind and - start again from the beginning. This isn't necessary if the - file happens to be CBR, of course; in that case we could seek - directly to the frame we want. But I leave that little - optimization for the future developer who discovers she really - needs it. */ - mp3_mad->frames_read = 0; - mad_timer_reset(&mp3_mad->next_frame_start); - mp3_mad->status &= ~MS_error_flags; - mp3_mad->output_begin = 0; - mp3_mad->output_end = 0; - - SDL_RWseek(mp3_mad->rw, 0, RW_SEEK_SET); + /* In order to seek backwards in a VBR file, we have to rewind and + start again from the beginning. This isn't necessary if the + file happens to be CBR, of course; in that case we could seek + directly to the frame we want. But I leave that little + optimization for the future developer who discovers she really + needs it. */ + mp3_mad->frames_read = 0; + mad_timer_reset(&mp3_mad->next_frame_start); + mp3_mad->status &= ~MS_error_flags; + mp3_mad->output_begin = 0; + mp3_mad->output_end = 0; + + SDL_RWseek(mp3_mad->rw, 0, RW_SEEK_SET); } /* Now we have to skip frames until we come to the right one. - Again, only truly necessary if the file is VBR. */ + Again, only truly necessary if the file is VBR. */ while (mad_timer_compare(mp3_mad->next_frame_start, target) < 0) { - if (!read_next_frame(mp3_mad)) { - if ((mp3_mad->status & MS_error_flags) != 0) { - /* Couldn't read a frame; either an error condition or - end-of-file. Stop. */ - mp3_mad->status &= ~MS_playing; - return; - } - } + if (!read_next_frame(mp3_mad)) { + if ((mp3_mad->status & MS_error_flags) != 0) { + /* Couldn't read a frame; either an error condition or + end-of-file. Stop. */ + mp3_mad->status &= ~MS_playing; + return; + } + } } /* Here we are, at the beginning of the frame that contains the - target time. Ehh, I say that's close enough. If we wanted to, - we could get more precise by decoding the frame now and counting - the appropriate number of samples out of it. */ + target time. Ehh, I say that's close enough. If we wanted to, + we could get more precise by decoding the frame now and counting + the appropriate number of samples out of it. */ } void diff --git a/music_mad.h b/music_mad.h index a0564656..72d6c7f0 100644 --- a/music_mad.h +++ b/music_mad.h @@ -26,8 +26,8 @@ #include "SDL_audio.h" #include "SDL_mixer.h" -#define MAD_INPUT_BUFFER_SIZE (5*8192) -#define MAD_OUTPUT_BUFFER_SIZE 8192 +#define MAD_INPUT_BUFFER_SIZE (5*8192) +#define MAD_OUTPUT_BUFFER_SIZE 8192 enum { MS_input_eof = 0x0001, diff --git a/music_mod.c b/music_mod.c index 6f2b6404..8774154a 100644 --- a/music_mod.c +++ b/music_mod.c @@ -50,297 +50,297 @@ static int music_swap16; */ int MOD_init(SDL_AudioSpec *mixerfmt) { - CHAR *list; - - if ( !Mix_Init(MIX_INIT_MOD) ) { - return -1; - } - - /* Set the MikMod music format */ - music_swap8 = 0; - music_swap16 = 0; - switch (mixerfmt->format) { - - case AUDIO_U8: - case AUDIO_S8: { - if ( mixerfmt->format == AUDIO_S8 ) { - music_swap8 = 1; - } - *mikmod.md_mode = 0; - } - break; - - case AUDIO_S16LSB: - case AUDIO_S16MSB: { - /* See if we need to correct MikMod mixing */ + CHAR *list; + + if ( !Mix_Init(MIX_INIT_MOD) ) { + return -1; + } + + /* Set the MikMod music format */ + music_swap8 = 0; + music_swap16 = 0; + switch (mixerfmt->format) { + + case AUDIO_U8: + case AUDIO_S8: { + if ( mixerfmt->format == AUDIO_S8 ) { + music_swap8 = 1; + } + *mikmod.md_mode = 0; + } + break; + + case AUDIO_S16LSB: + case AUDIO_S16MSB: { + /* See if we need to correct MikMod mixing */ #if SDL_BYTEORDER == SDL_LIL_ENDIAN - if ( mixerfmt->format == AUDIO_S16MSB ) { + if ( mixerfmt->format == AUDIO_S16MSB ) { #else - if ( mixerfmt->format == AUDIO_S16LSB ) { + if ( mixerfmt->format == AUDIO_S16LSB ) { #endif - music_swap16 = 1; - } - *mikmod.md_mode = DMODE_16BITS; - } - break; - - default: { - Mix_SetError("Unknown hardware audio format"); - return -1; - } - } - current_output_channels = mixerfmt->channels; - current_output_format = mixerfmt->format; - if ( mixerfmt->channels > 1 ) { - if ( mixerfmt->channels > MAX_OUTPUT_CHANNELS ) { - Mix_SetError("Hardware uses more channels than mixerfmt"); - return -1; - } - *mikmod.md_mode |= DMODE_STEREO; - } - *mikmod.md_mixfreq = mixerfmt->freq; - *mikmod.md_device = 0; - *mikmod.md_volume = 96; - *mikmod.md_musicvolume = 128; - *mikmod.md_sndfxvolume = 128; - *mikmod.md_pansep = 128; - *mikmod.md_reverb = 0; - *mikmod.md_mode |= DMODE_HQMIXER|DMODE_SOFT_MUSIC|DMODE_SURROUND; - - list = mikmod.MikMod_InfoDriver(); - if ( list ) - mikmod.MikMod_free(list); - else - mikmod.MikMod_RegisterDriver(mikmod.drv_nos); - - list = mikmod.MikMod_InfoLoader(); - if ( list ) - mikmod.MikMod_free(list); - else - mikmod.MikMod_RegisterAllLoaders(); - - if ( mikmod.MikMod_Init(NULL) ) { - Mix_SetError("%s", mikmod.MikMod_strerror(*mikmod.MikMod_errno)); - return -1; - } - - return 0; + music_swap16 = 1; + } + *mikmod.md_mode = DMODE_16BITS; + } + break; + + default: { + Mix_SetError("Unknown hardware audio format"); + return -1; + } + } + current_output_channels = mixerfmt->channels; + current_output_format = mixerfmt->format; + if ( mixerfmt->channels > 1 ) { + if ( mixerfmt->channels > MAX_OUTPUT_CHANNELS ) { + Mix_SetError("Hardware uses more channels than mixerfmt"); + return -1; + } + *mikmod.md_mode |= DMODE_STEREO; + } + *mikmod.md_mixfreq = mixerfmt->freq; + *mikmod.md_device = 0; + *mikmod.md_volume = 96; + *mikmod.md_musicvolume = 128; + *mikmod.md_sndfxvolume = 128; + *mikmod.md_pansep = 128; + *mikmod.md_reverb = 0; + *mikmod.md_mode |= DMODE_HQMIXER|DMODE_SOFT_MUSIC|DMODE_SURROUND; + + list = mikmod.MikMod_InfoDriver(); + if ( list ) + mikmod.MikMod_free(list); + else + mikmod.MikMod_RegisterDriver(mikmod.drv_nos); + + list = mikmod.MikMod_InfoLoader(); + if ( list ) + mikmod.MikMod_free(list); + else + mikmod.MikMod_RegisterAllLoaders(); + + if ( mikmod.MikMod_Init(NULL) ) { + Mix_SetError("%s", mikmod.MikMod_strerror(*mikmod.MikMod_errno)); + return -1; + } + + return 0; } /* Uninitialize the music players */ void MOD_exit(void) { - if (mikmod.MikMod_Exit) { - mikmod.MikMod_Exit(); - } + if (mikmod.MikMod_Exit) { + mikmod.MikMod_Exit(); + } } /* Set the volume for a MOD stream */ void MOD_setvolume(MODULE *music, int volume) { - mikmod.Player_SetVolume((SWORD)volume); + mikmod.Player_SetVolume((SWORD)volume); } typedef struct { - MREADER mr; - long offset; - long eof; - SDL_RWops *rw; + MREADER mr; + long offset; + long eof; + SDL_RWops *rw; } LMM_MREADER; BOOL LMM_Seek(struct MREADER *mr,long to,int dir) { - LMM_MREADER* lmmmr = (LMM_MREADER*)mr; - if ( dir == SEEK_SET ) { - to += lmmmr->offset; - } - return (SDL_RWseek(lmmmr->rw, to, dir) < lmmmr->offset); + LMM_MREADER* lmmmr = (LMM_MREADER*)mr; + if ( dir == SEEK_SET ) { + to += lmmmr->offset; + } + return (SDL_RWseek(lmmmr->rw, to, dir) < lmmmr->offset); } long LMM_Tell(struct MREADER *mr) { - LMM_MREADER* lmmmr = (LMM_MREADER*)mr; - return SDL_RWtell(lmmmr->rw) - lmmmr->offset; + LMM_MREADER* lmmmr = (LMM_MREADER*)mr; + return SDL_RWtell(lmmmr->rw) - lmmmr->offset; } BOOL LMM_Read(struct MREADER *mr,void *buf,size_t sz) { - LMM_MREADER* lmmmr = (LMM_MREADER*)mr; - return SDL_RWread(lmmmr->rw, buf, sz, 1); + LMM_MREADER* lmmmr = (LMM_MREADER*)mr; + return SDL_RWread(lmmmr->rw, buf, sz, 1); } int LMM_Get(struct MREADER *mr) { - unsigned char c; - LMM_MREADER* lmmmr = (LMM_MREADER*)mr; - if ( SDL_RWread(lmmmr->rw, &c, 1, 1) ) { - return c; - } - return EOF; + unsigned char c; + LMM_MREADER* lmmmr = (LMM_MREADER*)mr; + if ( SDL_RWread(lmmmr->rw, &c, 1, 1) ) { + return c; + } + return EOF; } BOOL LMM_Eof(struct MREADER *mr) { - long offset; - LMM_MREADER* lmmmr = (LMM_MREADER*)mr; - offset = LMM_Tell(mr); - return offset >= lmmmr->eof; + long offset; + LMM_MREADER* lmmmr = (LMM_MREADER*)mr; + offset = LMM_Tell(mr); + return offset >= lmmmr->eof; } MODULE *MikMod_LoadSongRW(SDL_RWops *rw, int maxchan) { - LMM_MREADER lmmmr = { - { LMM_Seek, LMM_Tell, LMM_Read, LMM_Get, LMM_Eof }, - 0, - 0, - 0 - }; - lmmmr.offset = SDL_RWtell(rw); - SDL_RWseek(rw, 0, RW_SEEK_END); - lmmmr.eof = SDL_RWtell(rw); - SDL_RWseek(rw, lmmmr.offset, RW_SEEK_SET); + LMM_MREADER lmmmr = { + { LMM_Seek, LMM_Tell, LMM_Read, LMM_Get, LMM_Eof }, + 0, + 0, + 0 + }; + lmmmr.offset = SDL_RWtell(rw); + SDL_RWseek(rw, 0, RW_SEEK_END); + lmmmr.eof = SDL_RWtell(rw); + SDL_RWseek(rw, lmmmr.offset, RW_SEEK_SET); lmmmr.rw = rw; - return mikmod.Player_LoadGeneric((MREADER*)&lmmmr, maxchan, 0); + return mikmod.Player_LoadGeneric((MREADER*)&lmmmr, maxchan, 0); } /* Load a MOD stream from an SDL_RWops object */ MODULE *MOD_new_RW(SDL_RWops *rw, int freerw) { - MODULE *module; - - /* Make sure the mikmod library is loaded */ - if ( !Mix_Init(MIX_INIT_MOD) ) { - if ( freerw ) { - SDL_RWclose(rw); - } - return NULL; - } - - module = MikMod_LoadSongRW(rw,64); - if (!module) { - Mix_SetError("%s", mikmod.MikMod_strerror(*mikmod.MikMod_errno)); - if ( freerw ) { - SDL_RWclose(rw); - } - return NULL; - } - - /* Stop implicit looping, fade out and other flags. */ - module->extspd = 1; - module->panflag = 1; - module->wrap = 0; - module->loop = 0; + MODULE *module; + + /* Make sure the mikmod library is loaded */ + if ( !Mix_Init(MIX_INIT_MOD) ) { + if ( freerw ) { + SDL_RWclose(rw); + } + return NULL; + } + + module = MikMod_LoadSongRW(rw,64); + if (!module) { + Mix_SetError("%s", mikmod.MikMod_strerror(*mikmod.MikMod_errno)); + if ( freerw ) { + SDL_RWclose(rw); + } + return NULL; + } + + /* Stop implicit looping, fade out and other flags. */ + module->extspd = 1; + module->panflag = 1; + module->wrap = 0; + module->loop = 0; #if 0 /* Don't set fade out by default - unfortunately there's no real way to query the status of the song or set trigger actions. Hum. */ - module->fadeout = 1; + module->fadeout = 1; #endif - if ( freerw ) { - SDL_RWclose(rw); - } - return module; + if ( freerw ) { + SDL_RWclose(rw); + } + return module; } /* Start playback of a given MOD stream */ void MOD_play(MODULE *music) { - mikmod.Player_Start(music); + mikmod.Player_Start(music); } /* Return non-zero if a stream is currently playing */ int MOD_playing(MODULE *music) { - return mikmod.Player_Active(); + return mikmod.Player_Active(); } /* Play some of a stream previously started with MOD_play() */ int MOD_playAudio(MODULE *music, Uint8 *stream, int len) { - if (current_output_channels > 2) { - int small_len = 2 * len / current_output_channels; - int i; - Uint8 *src, *dst; - - mikmod.VC_WriteBytes((SBYTE *)stream, small_len); - /* and extend to len by copying channels */ - src = stream + small_len; - dst = stream + len; - - switch (current_output_format & 0xFF) { - case 8: - for ( i=small_len/2; i; --i ) { - src -= 2; - dst -= current_output_channels; - dst[0] = src[0]; - dst[1] = src[1]; - dst[2] = src[0]; - dst[3] = src[1]; - if (current_output_channels == 6) { - dst[4] = src[0]; - dst[5] = src[1]; - } - } - break; - case 16: - for ( i=small_len/4; i; --i ) { - src -= 4; - dst -= 2 * current_output_channels; - dst[0] = src[0]; - dst[1] = src[1]; - dst[2] = src[2]; - dst[3] = src[3]; - dst[4] = src[0]; - dst[5] = src[1]; - dst[6] = src[2]; - dst[7] = src[3]; - if (current_output_channels == 6) { - dst[8] = src[0]; - dst[9] = src[1]; - dst[10] = src[2]; - dst[11] = src[3]; - } - } - break; - } - } else { - mikmod.VC_WriteBytes((SBYTE *)stream, len); - } - if ( music_swap8 ) { - Uint8 *dst; - int i; - - dst = stream; - for ( i=len; i; --i ) { - *dst++ ^= 0x80; - } - } else - if ( music_swap16 ) { - Uint8 *dst, tmp; - int i; - - dst = stream; - for ( i=(len/2); i; --i ) { - tmp = dst[0]; - dst[0] = dst[1]; - dst[1] = tmp; - dst += 2; - } - } - return 0; + if (current_output_channels > 2) { + int small_len = 2 * len / current_output_channels; + int i; + Uint8 *src, *dst; + + mikmod.VC_WriteBytes((SBYTE *)stream, small_len); + /* and extend to len by copying channels */ + src = stream + small_len; + dst = stream + len; + + switch (current_output_format & 0xFF) { + case 8: + for ( i=small_len/2; i; --i ) { + src -= 2; + dst -= current_output_channels; + dst[0] = src[0]; + dst[1] = src[1]; + dst[2] = src[0]; + dst[3] = src[1]; + if (current_output_channels == 6) { + dst[4] = src[0]; + dst[5] = src[1]; + } + } + break; + case 16: + for ( i=small_len/4; i; --i ) { + src -= 4; + dst -= 2 * current_output_channels; + dst[0] = src[0]; + dst[1] = src[1]; + dst[2] = src[2]; + dst[3] = src[3]; + dst[4] = src[0]; + dst[5] = src[1]; + dst[6] = src[2]; + dst[7] = src[3]; + if (current_output_channels == 6) { + dst[8] = src[0]; + dst[9] = src[1]; + dst[10] = src[2]; + dst[11] = src[3]; + } + } + break; + } + } else { + mikmod.VC_WriteBytes((SBYTE *)stream, len); + } + if ( music_swap8 ) { + Uint8 *dst; + int i; + + dst = stream; + for ( i=len; i; --i ) { + *dst++ ^= 0x80; + } + } else + if ( music_swap16 ) { + Uint8 *dst, tmp; + int i; + + dst = stream; + for ( i=(len/2); i; --i ) { + tmp = dst[0]; + dst[0] = dst[1]; + dst[1] = tmp; + dst += 2; + } + } + return 0; } /* Stop playback of a stream previously started with MOD_play() */ void MOD_stop(MODULE *music) { - mikmod.Player_Stop(); + mikmod.Player_Stop(); } /* Close the given MOD stream */ void MOD_delete(MODULE *music) { - mikmod.Player_Free(music); + mikmod.Player_Free(music); } /* Jump (seek) to a given position (time is in seconds) */ void MOD_jump_to_time(MODULE *music, double time) { - mikmod.Player_SetPosition((UWORD)time); + mikmod.Player_SetPosition((UWORD)time); } #endif /* MOD_MUSIC */ diff --git a/music_modplug.c b/music_modplug.c index 31a7ba20..9b928d13 100644 --- a/music_modplug.c +++ b/music_modplug.c @@ -9,58 +9,58 @@ static ModPlug_Settings settings; int modplug_init(SDL_AudioSpec *spec) { - ModPlug_GetSettings(&settings); - settings.mFlags=MODPLUG_ENABLE_OVERSAMPLING; - current_output_channels=spec->channels; - settings.mChannels=spec->channels>1?2:1; - settings.mBits=spec->format&0xFF; - - music_swap8 = 0; - music_swap16 = 0; - - switch(spec->format) - { - case AUDIO_U8: - case AUDIO_S8: { - if ( spec->format == AUDIO_S8 ) { - music_swap8 = 1; - } - settings.mBits=8; - } - break; - - case AUDIO_S16LSB: - case AUDIO_S16MSB: { - /* See if we need to correct MikMod mixing */ + ModPlug_GetSettings(&settings); + settings.mFlags=MODPLUG_ENABLE_OVERSAMPLING; + current_output_channels=spec->channels; + settings.mChannels=spec->channels>1?2:1; + settings.mBits=spec->format&0xFF; + + music_swap8 = 0; + music_swap16 = 0; + + switch(spec->format) + { + case AUDIO_U8: + case AUDIO_S8: { + if ( spec->format == AUDIO_S8 ) { + music_swap8 = 1; + } + settings.mBits=8; + } + break; + + case AUDIO_S16LSB: + case AUDIO_S16MSB: { + /* See if we need to correct MikMod mixing */ #if SDL_BYTEORDER == SDL_LIL_ENDIAN - if ( spec->format == AUDIO_S16MSB ) { + if ( spec->format == AUDIO_S16MSB ) { #else - if ( spec->format == AUDIO_S16LSB ) { + if ( spec->format == AUDIO_S16LSB ) { #endif - music_swap16 = 1; - } - settings.mBits=16; - } - break; - - default: { - Mix_SetError("Unknown hardware audio format"); - return -1; - } - - } - - settings.mFrequency=spec->freq; /*TODO: limit to 11025, 22050, or 44100 ? */ - settings.mResamplingMode=MODPLUG_RESAMPLE_FIR; - settings.mReverbDepth=0; - settings.mReverbDelay=100; - settings.mBassAmount=0; - settings.mBassRange=50; - settings.mSurroundDepth=0; - settings.mSurroundDelay=10; - settings.mLoopCount=0; - ModPlug_SetSettings(&settings); - return 0; + music_swap16 = 1; + } + settings.mBits=16; + } + break; + + default: { + Mix_SetError("Unknown hardware audio format"); + return -1; + } + + } + + settings.mFrequency=spec->freq; /*TODO: limit to 11025, 22050, or 44100 ? */ + settings.mResamplingMode=MODPLUG_RESAMPLE_FIR; + settings.mReverbDepth=0; + settings.mReverbDelay=100; + settings.mBassAmount=0; + settings.mBassRange=50; + settings.mSurroundDepth=0; + settings.mSurroundDelay=10; + settings.mLoopCount=0; + ModPlug_SetSettings(&settings); + return 0; } /* Uninitialize the music players */ @@ -71,169 +71,169 @@ void modplug_exit() /* Set the volume for a modplug stream */ void modplug_setvolume(modplug_data *music, int volume) { - ModPlug_SetMasterVolume(music->file, volume*4); + ModPlug_SetMasterVolume(music->file, volume*4); } /* Load a modplug stream from an SDL_RWops object */ modplug_data *modplug_new_RW(SDL_RWops *rw, int freerw) { - modplug_data *music=NULL; - long offset,sz; - char *buf=NULL; - - offset = SDL_RWtell(rw); - SDL_RWseek(rw, 0, RW_SEEK_END); - sz = SDL_RWtell(rw)-offset; - SDL_RWseek(rw, offset, RW_SEEK_SET); - buf=(char*)SDL_malloc(sz); - if(buf) - { - if(SDL_RWread(rw, buf, sz, 1)==1) - { - music=(modplug_data*)SDL_malloc(sizeof(modplug_data)); - if (music) - { - music->playing=0; - music->file=ModPlug_Load(buf,sz); - if(!music->file) - { - SDL_free(music); - music=NULL; - } - } - else - { - SDL_OutOfMemory(); - } - } - SDL_free(buf); - } - else - { - SDL_OutOfMemory(); - } - if (freerw) { - SDL_RWclose(rw); - } - return music; + modplug_data *music=NULL; + long offset,sz; + char *buf=NULL; + + offset = SDL_RWtell(rw); + SDL_RWseek(rw, 0, RW_SEEK_END); + sz = SDL_RWtell(rw)-offset; + SDL_RWseek(rw, offset, RW_SEEK_SET); + buf=(char*)SDL_malloc(sz); + if(buf) + { + if(SDL_RWread(rw, buf, sz, 1)==1) + { + music=(modplug_data*)SDL_malloc(sizeof(modplug_data)); + if (music) + { + music->playing=0; + music->file=ModPlug_Load(buf,sz); + if(!music->file) + { + SDL_free(music); + music=NULL; + } + } + else + { + SDL_OutOfMemory(); + } + } + SDL_free(buf); + } + else + { + SDL_OutOfMemory(); + } + if (freerw) { + SDL_RWclose(rw); + } + return music; } /* Start playback of a given modplug stream */ void modplug_play(modplug_data *music) { - ModPlug_Seek(music->file,0); - music->playing=1; + ModPlug_Seek(music->file,0); + music->playing=1; } /* Return non-zero if a stream is currently playing */ int modplug_playing(modplug_data *music) { - return music && music->playing; + return music && music->playing; } /* Play some of a stream previously started with modplug_play() */ int modplug_playAudio(modplug_data *music, Uint8 *stream, int len) { - if (current_output_channels > 2) { - int small_len = 2 * len / current_output_channels; - int i; - Uint8 *src, *dst; - - i=ModPlug_Read(music->file, stream, small_len); - if(iplaying=0; - } - /* and extend to len by copying channels */ - src = stream + small_len; - dst = stream + len; - - switch (settings.mBits) { - case 8: - for ( i=small_len/2; i; --i ) { - src -= 2; - dst -= current_output_channels; - dst[0] = src[0]; - dst[1] = src[1]; - dst[2] = src[0]; - dst[3] = src[1]; - if (current_output_channels == 6) { - dst[4] = src[0]; - dst[5] = src[1]; - } - } - break; - case 16: - for ( i=small_len/4; i; --i ) { - src -= 4; - dst -= 2 * current_output_channels; - dst[0] = src[0]; - dst[1] = src[1]; - dst[2] = src[2]; - dst[3] = src[3]; - dst[4] = src[0]; - dst[5] = src[1]; - dst[6] = src[2]; - dst[7] = src[3]; - if (current_output_channels == 6) { - dst[8] = src[0]; - dst[9] = src[1]; - dst[10] = src[2]; - dst[11] = src[3]; - } - } - break; - } - } else { - int i=ModPlug_Read(music->file, stream, len); - if(iplaying=0; - } - } - if ( music_swap8 ) { - Uint8 *dst; - int i; - - dst = stream; - for ( i=len; i; --i ) { - *dst++ ^= 0x80; - } - } else - if ( music_swap16 ) { - Uint8 *dst, tmp; - int i; - - dst = stream; - for ( i=(len/2); i; --i ) { - tmp = dst[0]; - dst[0] = dst[1]; - dst[1] = tmp; - dst += 2; - } - } - return 0; + if (current_output_channels > 2) { + int small_len = 2 * len / current_output_channels; + int i; + Uint8 *src, *dst; + + i=ModPlug_Read(music->file, stream, small_len); + if(iplaying=0; + } + /* and extend to len by copying channels */ + src = stream + small_len; + dst = stream + len; + + switch (settings.mBits) { + case 8: + for ( i=small_len/2; i; --i ) { + src -= 2; + dst -= current_output_channels; + dst[0] = src[0]; + dst[1] = src[1]; + dst[2] = src[0]; + dst[3] = src[1]; + if (current_output_channels == 6) { + dst[4] = src[0]; + dst[5] = src[1]; + } + } + break; + case 16: + for ( i=small_len/4; i; --i ) { + src -= 4; + dst -= 2 * current_output_channels; + dst[0] = src[0]; + dst[1] = src[1]; + dst[2] = src[2]; + dst[3] = src[3]; + dst[4] = src[0]; + dst[5] = src[1]; + dst[6] = src[2]; + dst[7] = src[3]; + if (current_output_channels == 6) { + dst[8] = src[0]; + dst[9] = src[1]; + dst[10] = src[2]; + dst[11] = src[3]; + } + } + break; + } + } else { + int i=ModPlug_Read(music->file, stream, len); + if(iplaying=0; + } + } + if ( music_swap8 ) { + Uint8 *dst; + int i; + + dst = stream; + for ( i=len; i; --i ) { + *dst++ ^= 0x80; + } + } else + if ( music_swap16 ) { + Uint8 *dst, tmp; + int i; + + dst = stream; + for ( i=(len/2); i; --i ) { + tmp = dst[0]; + dst[0] = dst[1]; + dst[1] = tmp; + dst += 2; + } + } + return 0; } /* Stop playback of a stream previously started with modplug_play() */ void modplug_stop(modplug_data *music) { - music->playing=0; + music->playing=0; } /* Close the given modplug stream */ void modplug_delete(modplug_data *music) { - ModPlug_Unload(music->file); - SDL_free(music); + ModPlug_Unload(music->file); + SDL_free(music); } /* Jump (seek) to a given position (time is in seconds) */ void modplug_jump_to_time(modplug_data *music, double time) { - ModPlug_Seek(music->file,(int)(time*1000)); + ModPlug_Seek(music->file,(int)(time*1000)); } #endif diff --git a/music_modplug.h b/music_modplug.h index 92cbafd0..c1bfd634 100644 --- a/music_modplug.h +++ b/music_modplug.h @@ -6,8 +6,8 @@ #include "SDL_mixer.h" typedef struct { - ModPlugFile *file; - int playing; + ModPlugFile *file; + int playing; } modplug_data; int modplug_init(SDL_AudioSpec *mixer); diff --git a/music_ogg.c b/music_ogg.c index 7e9b8dc1..2548ca5b 100644 --- a/music_ogg.c +++ b/music_ogg.c @@ -41,14 +41,14 @@ static SDL_AudioSpec mixer; */ int OGG_init(SDL_AudioSpec *mixerfmt) { - mixer = *mixerfmt; - return(0); + mixer = *mixerfmt; + return(0); } /* Set the volume for an OGG stream */ void OGG_setvolume(OGG_music *music, int volume) { - music->volume = volume; + music->volume = volume; } static size_t sdl_read_func(void *ptr, size_t size, size_t nmemb, void *datasource) @@ -69,156 +69,156 @@ static long sdl_tell_func(void *datasource) /* Load an OGG stream from an SDL_RWops object */ OGG_music *OGG_new_RW(SDL_RWops *rw, int freerw) { - OGG_music *music; - ov_callbacks callbacks; - - if ( !Mix_Init(MIX_INIT_OGG) ) { - if ( freerw ) { - SDL_RWclose(rw); - } - return(NULL); - } - - SDL_memset(&callbacks, 0, sizeof(callbacks)); - callbacks.read_func = sdl_read_func; - callbacks.seek_func = sdl_seek_func; - callbacks.tell_func = sdl_tell_func; - - music = (OGG_music *)SDL_malloc(sizeof *music); - if ( music ) { - /* Initialize the music structure */ - memset(music, 0, (sizeof *music)); - music->rw = rw; - music->freerw = freerw; - OGG_stop(music); - OGG_setvolume(music, MIX_MAX_VOLUME); - music->section = -1; - - if ( vorbis.ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) { - SDL_free(music); - if ( freerw ) { - SDL_RWclose(rw); - } - SDL_SetError("Not an Ogg Vorbis audio stream"); - return(NULL); - } - } else { - if ( freerw ) { - SDL_RWclose(rw); - } - SDL_OutOfMemory(); - return(NULL); - } - return(music); + OGG_music *music; + ov_callbacks callbacks; + + if ( !Mix_Init(MIX_INIT_OGG) ) { + if ( freerw ) { + SDL_RWclose(rw); + } + return(NULL); + } + + SDL_memset(&callbacks, 0, sizeof(callbacks)); + callbacks.read_func = sdl_read_func; + callbacks.seek_func = sdl_seek_func; + callbacks.tell_func = sdl_tell_func; + + music = (OGG_music *)SDL_malloc(sizeof *music); + if ( music ) { + /* Initialize the music structure */ + memset(music, 0, (sizeof *music)); + music->rw = rw; + music->freerw = freerw; + OGG_stop(music); + OGG_setvolume(music, MIX_MAX_VOLUME); + music->section = -1; + + if ( vorbis.ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) { + SDL_free(music); + if ( freerw ) { + SDL_RWclose(rw); + } + SDL_SetError("Not an Ogg Vorbis audio stream"); + return(NULL); + } + } else { + if ( freerw ) { + SDL_RWclose(rw); + } + SDL_OutOfMemory(); + return(NULL); + } + return(music); } /* Start playback of a given OGG stream */ void OGG_play(OGG_music *music) { - music->playing = 1; + music->playing = 1; } /* Return non-zero if a stream is currently playing */ int OGG_playing(OGG_music *music) { - return(music->playing); + return(music->playing); } /* Read some Ogg stream data and convert it for output */ static void OGG_getsome(OGG_music *music) { - int section; - int len; - char data[4096]; - SDL_AudioCVT *cvt; + int section; + int len; + char data[4096]; + SDL_AudioCVT *cvt; #ifdef OGG_USE_TREMOR - len = vorbis.ov_read(&music->vf, data, sizeof(data), §ion); + len = vorbis.ov_read(&music->vf, data, sizeof(data), §ion); #else - len = vorbis.ov_read(&music->vf, data, sizeof(data), 0, 2, 1, §ion); + len = vorbis.ov_read(&music->vf, data, sizeof(data), 0, 2, 1, §ion); #endif - if ( len <= 0 ) { - if ( len == 0 ) { - music->playing = 0; - } - return; - } - cvt = &music->cvt; - if ( section != music->section ) { - vorbis_info *vi; - - vi = vorbis.ov_info(&music->vf, -1); - SDL_BuildAudioCVT(cvt, AUDIO_S16, vi->channels, vi->rate, - mixer.format,mixer.channels,mixer.freq); - if ( cvt->buf ) { - SDL_free(cvt->buf); - } - cvt->buf = (Uint8 *)SDL_malloc(sizeof(data)*cvt->len_mult); - music->section = section; - } - if ( cvt->buf ) { - memcpy(cvt->buf, data, len); - if ( cvt->needed ) { - cvt->len = len; - SDL_ConvertAudio(cvt); - } else { - cvt->len_cvt = len; - } - music->len_available = music->cvt.len_cvt; - music->snd_available = music->cvt.buf; - } else { - SDL_SetError("Out of memory"); - music->playing = 0; - } + if ( len <= 0 ) { + if ( len == 0 ) { + music->playing = 0; + } + return; + } + cvt = &music->cvt; + if ( section != music->section ) { + vorbis_info *vi; + + vi = vorbis.ov_info(&music->vf, -1); + SDL_BuildAudioCVT(cvt, AUDIO_S16, vi->channels, vi->rate, + mixer.format,mixer.channels,mixer.freq); + if ( cvt->buf ) { + SDL_free(cvt->buf); + } + cvt->buf = (Uint8 *)SDL_malloc(sizeof(data)*cvt->len_mult); + music->section = section; + } + if ( cvt->buf ) { + memcpy(cvt->buf, data, len); + if ( cvt->needed ) { + cvt->len = len; + SDL_ConvertAudio(cvt); + } else { + cvt->len_cvt = len; + } + music->len_available = music->cvt.len_cvt; + music->snd_available = music->cvt.buf; + } else { + SDL_SetError("Out of memory"); + music->playing = 0; + } } /* Play some of a stream previously started with OGG_play() */ int OGG_playAudio(OGG_music *music, Uint8 *snd, int len) { - int mixable; - - while ( (len > 0) && music->playing ) { - if ( ! music->len_available ) { - OGG_getsome(music); - } - mixable = len; - if ( mixable > music->len_available ) { - mixable = music->len_available; - } - if ( music->volume == MIX_MAX_VOLUME ) { - memcpy(snd, music->snd_available, mixable); - } else { - SDL_MixAudio(snd, music->snd_available, mixable, - music->volume); - } - music->len_available -= mixable; - music->snd_available += mixable; - len -= mixable; - snd += mixable; - } - - return len; + int mixable; + + while ( (len > 0) && music->playing ) { + if ( ! music->len_available ) { + OGG_getsome(music); + } + mixable = len; + if ( mixable > music->len_available ) { + mixable = music->len_available; + } + if ( music->volume == MIX_MAX_VOLUME ) { + memcpy(snd, music->snd_available, mixable); + } else { + SDL_MixAudio(snd, music->snd_available, mixable, + music->volume); + } + music->len_available -= mixable; + music->snd_available += mixable; + len -= mixable; + snd += mixable; + } + + return len; } /* Stop playback of a stream previously started with OGG_play() */ void OGG_stop(OGG_music *music) { - music->playing = 0; + music->playing = 0; } /* Close the given OGG stream */ void OGG_delete(OGG_music *music) { - if ( music ) { - if ( music->cvt.buf ) { - SDL_free(music->cvt.buf); - } - if ( music->freerw ) { - SDL_RWclose(music->rw); - } - vorbis.ov_clear(&music->vf); - SDL_free(music); - } + if ( music ) { + if ( music->cvt.buf ) { + SDL_free(music->cvt.buf); + } + if ( music->freerw ) { + SDL_RWclose(music->rw); + } + vorbis.ov_clear(&music->vf); + SDL_free(music); + } } /* Jump (seek) to a given position (time is in seconds) */ diff --git a/music_ogg.h b/music_ogg.h index 6550e527..f7491cb7 100644 --- a/music_ogg.h +++ b/music_ogg.h @@ -32,15 +32,15 @@ #endif typedef struct { - SDL_RWops *rw; - int freerw; - int playing; - int volume; - OggVorbis_File vf; - int section; - SDL_AudioCVT cvt; - int len_available; - Uint8 *snd_available; + SDL_RWops *rw; + int freerw; + int playing; + int volume; + OggVorbis_File vf; + int section; + SDL_AudioCVT cvt; + int len_available; + Uint8 *snd_available; } OGG_music; /* Initialize the Ogg Vorbis player, with the given mixer settings diff --git a/native_midi/native_midi_common.c b/native_midi/native_midi_common.c index 12294750..6fac450a 100644 --- a/native_midi/native_midi_common.c +++ b/native_midi/native_midi_common.c @@ -29,23 +29,23 @@ #include -/* The maximum number of midi tracks that we can handle +/* The maximum number of midi tracks that we can handle #define MIDI_TRACKS 32 */ /* A single midi track as read from the midi file */ typedef struct { - Uint8 *data; /* MIDI message stream */ - int len; /* length of the track data */ + Uint8 *data; /* MIDI message stream */ + int len; /* length of the track data */ } MIDITrack; /* A midi file, stripped down to the absolute minimum - divison & track data */ typedef struct { - int division; /* number of pulses per quarter note (ppqn) */ + int division; /* number of pulses per quarter note (ppqn) */ int nTracks; /* number of tracks */ - MIDITrack *track; /* tracks */ + MIDITrack *track; /* tracks */ } MIDIFile; @@ -54,11 +54,11 @@ typedef struct #define BE_SHORT(x) (x) #define BE_LONG(x) (x) #else -#define BE_SHORT(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF)) -#define BE_LONG(x) ((((x)&0x0000FF)<<24) | \ - (((x)&0x00FF00)<<8) | \ - (((x)&0xFF0000)>>8) | \ - (((x)>>24)&0xFF)) +#define BE_SHORT(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF)) +#define BE_LONG(x) ((((x)&0x0000FF)<<24) | \ + (((x)&0x00FF00)<<8) | \ + (((x)&0xFF0000)>>8) | \ + (((x)>>24)&0xFF)) #endif @@ -66,152 +66,152 @@ typedef struct /* Get Variable Length Quantity */ static int GetVLQ(MIDITrack *track, int *currentPos) { - int l = 0; - Uint8 c; - while(1) - { - c = track->data[*currentPos]; - (*currentPos)++; - l += (c & 0x7f); - if (!(c & 0x80)) - return l; - l <<= 7; - } + int l = 0; + Uint8 c; + while(1) + { + c = track->data[*currentPos]; + (*currentPos)++; + l += (c & 0x7f); + if (!(c & 0x80)) + return l; + l <<= 7; + } } /* Create a single MIDIEvent */ static MIDIEvent *CreateEvent(Uint32 time, Uint8 event, Uint8 a, Uint8 b) { - MIDIEvent *newEvent; - - newEvent = calloc(1, sizeof(MIDIEvent)); - - if (newEvent) - { - newEvent->time = time; - newEvent->status = event; - newEvent->data[0] = a; - newEvent->data[1] = b; - } - else - Mix_SetError("Out of memory"); - - return newEvent; + MIDIEvent *newEvent; + + newEvent = calloc(1, sizeof(MIDIEvent)); + + if (newEvent) + { + newEvent->time = time; + newEvent->status = event; + newEvent->data[0] = a; + newEvent->data[1] = b; + } + else + Mix_SetError("Out of memory"); + + return newEvent; } /* Convert a single midi track to a list of MIDIEvents */ static MIDIEvent *MIDITracktoStream(MIDITrack *track) { - Uint32 atime = 0; - Uint32 len = 0; - Uint8 event,type,a,b; - Uint8 laststatus = 0; - Uint8 lastchan = 0; - int currentPos = 0; - int end = 0; - MIDIEvent *head = CreateEvent(0,0,0,0); /* dummy event to make handling the list easier */ - MIDIEvent *currentEvent = head; - - while (!end) - { - if (currentPos >= track->len) - break; /* End of data stream reached */ - - atime += GetVLQ(track, ¤tPos); - event = track->data[currentPos++]; - - /* Handle SysEx seperatly */ - if (((event>>4) & 0x0F) == MIDI_STATUS_SYSEX) - { - if (event == 0xFF) - { - type = track->data[currentPos]; - currentPos++; - switch(type) - { - case 0x2f: /* End of data marker */ - end = 1; - case 0x51: /* Tempo change */ - /* - a=track->data[currentPos]; - b=track->data[currentPos+1]; - c=track->data[currentPos+2]; - AddEvent(song, atime, MEVT_TEMPO, c, b, a); - */ - break; - } - } - else - type = 0; - - len = GetVLQ(track, ¤tPos); - - /* Create an event and attach the extra data, if any */ - currentEvent->next = CreateEvent(atime, event, type, 0); - currentEvent = currentEvent->next; - if (NULL == currentEvent) - { - FreeMIDIEventList(head); - return NULL; - } - if (len) - { - currentEvent->extraLen = len; - currentEvent->extraData = malloc(len); - memcpy(currentEvent->extraData, &(track->data[currentPos]), len); - currentPos += len; - } - } - else - { - a = event; - if (a & 0x80) /* It's a status byte */ - { - /* Extract channel and status information */ - lastchan = a & 0x0F; - laststatus = (a>>4) & 0x0F; - - /* Read the next byte which should always be a data byte */ - a = track->data[currentPos++] & 0x7F; - } - switch(laststatus) - { - case MIDI_STATUS_NOTE_OFF: - case MIDI_STATUS_NOTE_ON: /* Note on */ - case MIDI_STATUS_AFTERTOUCH: /* Key Pressure */ - case MIDI_STATUS_CONTROLLER: /* Control change */ - case MIDI_STATUS_PITCH_WHEEL: /* Pitch wheel */ - b = track->data[currentPos++] & 0x7F; - currentEvent->next = CreateEvent(atime, (Uint8)((laststatus<<4)+lastchan), a, b); - currentEvent = currentEvent->next; - if (NULL == currentEvent) - { - FreeMIDIEventList(head); - return NULL; - } - break; - - case MIDI_STATUS_PROG_CHANGE: /* Program change */ - case MIDI_STATUS_PRESSURE: /* Channel pressure */ - a &= 0x7f; - currentEvent->next = CreateEvent(atime, (Uint8)((laststatus<<4)+lastchan), a, 0); - currentEvent = currentEvent->next; - if (NULL == currentEvent) - { - FreeMIDIEventList(head); - return NULL; - } - break; - - default: /* Sysex already handled above */ - break; - } - } - } - - currentEvent = head->next; - free(head); /* release the dummy head event */ - return currentEvent; + Uint32 atime = 0; + Uint32 len = 0; + Uint8 event,type,a,b; + Uint8 laststatus = 0; + Uint8 lastchan = 0; + int currentPos = 0; + int end = 0; + MIDIEvent *head = CreateEvent(0,0,0,0); /* dummy event to make handling the list easier */ + MIDIEvent *currentEvent = head; + + while (!end) + { + if (currentPos >= track->len) + break; /* End of data stream reached */ + + atime += GetVLQ(track, ¤tPos); + event = track->data[currentPos++]; + + /* Handle SysEx seperatly */ + if (((event>>4) & 0x0F) == MIDI_STATUS_SYSEX) + { + if (event == 0xFF) + { + type = track->data[currentPos]; + currentPos++; + switch(type) + { + case 0x2f: /* End of data marker */ + end = 1; + case 0x51: /* Tempo change */ + /* + a=track->data[currentPos]; + b=track->data[currentPos+1]; + c=track->data[currentPos+2]; + AddEvent(song, atime, MEVT_TEMPO, c, b, a); + */ + break; + } + } + else + type = 0; + + len = GetVLQ(track, ¤tPos); + + /* Create an event and attach the extra data, if any */ + currentEvent->next = CreateEvent(atime, event, type, 0); + currentEvent = currentEvent->next; + if (NULL == currentEvent) + { + FreeMIDIEventList(head); + return NULL; + } + if (len) + { + currentEvent->extraLen = len; + currentEvent->extraData = malloc(len); + memcpy(currentEvent->extraData, &(track->data[currentPos]), len); + currentPos += len; + } + } + else + { + a = event; + if (a & 0x80) /* It's a status byte */ + { + /* Extract channel and status information */ + lastchan = a & 0x0F; + laststatus = (a>>4) & 0x0F; + + /* Read the next byte which should always be a data byte */ + a = track->data[currentPos++] & 0x7F; + } + switch(laststatus) + { + case MIDI_STATUS_NOTE_OFF: + case MIDI_STATUS_NOTE_ON: /* Note on */ + case MIDI_STATUS_AFTERTOUCH: /* Key Pressure */ + case MIDI_STATUS_CONTROLLER: /* Control change */ + case MIDI_STATUS_PITCH_WHEEL: /* Pitch wheel */ + b = track->data[currentPos++] & 0x7F; + currentEvent->next = CreateEvent(atime, (Uint8)((laststatus<<4)+lastchan), a, b); + currentEvent = currentEvent->next; + if (NULL == currentEvent) + { + FreeMIDIEventList(head); + return NULL; + } + break; + + case MIDI_STATUS_PROG_CHANGE: /* Program change */ + case MIDI_STATUS_PRESSURE: /* Channel pressure */ + a &= 0x7f; + currentEvent->next = CreateEvent(atime, (Uint8)((laststatus<<4)+lastchan), a, 0); + currentEvent = currentEvent->next; + if (NULL == currentEvent) + { + FreeMIDIEventList(head); + return NULL; + } + break; + + default: /* Sysex already handled above */ + break; + } + } + } + + currentEvent = head->next; + free(head); /* release the dummy head event */ + return currentEvent; } /* @@ -221,96 +221,96 @@ static MIDIEvent *MIDITracktoStream(MIDITrack *track) */ static MIDIEvent *MIDItoStream(MIDIFile *mididata) { - MIDIEvent **track; - MIDIEvent *head = CreateEvent(0,0,0,0); /* dummy event to make handling the list easier */ - MIDIEvent *currentEvent = head; - int trackID; + MIDIEvent **track; + MIDIEvent *head = CreateEvent(0,0,0,0); /* dummy event to make handling the list easier */ + MIDIEvent *currentEvent = head; + int trackID; if (NULL == head) - return NULL; - + return NULL; + track = (MIDIEvent**) calloc(1, sizeof(MIDIEvent*) * mididata->nTracks); - if (NULL == head) + if (NULL == head) return NULL; - - /* First, convert all tracks to MIDIEvent lists */ - for (trackID = 0; trackID < mididata->nTracks; trackID++) - track[trackID] = MIDITracktoStream(&mididata->track[trackID]); - - /* Now, merge the lists. */ - /* TODO */ - while(1) - { - Uint32 lowestTime = INT_MAX; - int currentTrackID = -1; - - /* Find the next event */ - for (trackID = 0; trackID < mididata->nTracks; trackID++) - { - if (track[trackID] && (track[trackID]->time < lowestTime)) - { - currentTrackID = trackID; - lowestTime = track[currentTrackID]->time; - } - } - - /* Check if we processes all events */ - if (currentTrackID == -1) - break; - - currentEvent->next = track[currentTrackID]; - track[currentTrackID] = track[currentTrackID]->next; - - currentEvent = currentEvent->next; - - - lowestTime = 0; - } - - /* Make sure the list is properly terminated */ - currentEvent->next = 0; - - currentEvent = head->next; + + /* First, convert all tracks to MIDIEvent lists */ + for (trackID = 0; trackID < mididata->nTracks; trackID++) + track[trackID] = MIDITracktoStream(&mididata->track[trackID]); + + /* Now, merge the lists. */ + /* TODO */ + while(1) + { + Uint32 lowestTime = INT_MAX; + int currentTrackID = -1; + + /* Find the next event */ + for (trackID = 0; trackID < mididata->nTracks; trackID++) + { + if (track[trackID] && (track[trackID]->time < lowestTime)) + { + currentTrackID = trackID; + lowestTime = track[currentTrackID]->time; + } + } + + /* Check if we processes all events */ + if (currentTrackID == -1) + break; + + currentEvent->next = track[currentTrackID]; + track[currentTrackID] = track[currentTrackID]->next; + + currentEvent = currentEvent->next; + + + lowestTime = 0; + } + + /* Make sure the list is properly terminated */ + currentEvent->next = 0; + + currentEvent = head->next; free(track); - free(head); /* release the dummy head event */ - return currentEvent; + free(head); /* release the dummy head event */ + return currentEvent; } static int ReadMIDIFile(MIDIFile *mididata, SDL_RWops *rw) { - int i = 0; - Uint32 ID; - Uint32 size; - Uint16 format; - Uint16 tracks; - Uint16 division; - - if (!mididata) - return 0; - if (!rw) - return 0; - - /* Make sure this is really a MIDI file */ - SDL_RWread(rw, &ID, 1, 4); - if (BE_LONG(ID) != 'MThd') - return 0; - - /* Header size must be 6 */ - SDL_RWread(rw, &size, 1, 4); - size = BE_LONG(size); - if (size != 6) - return 0; - - /* We only support format 0 and 1, but not 2 */ - SDL_RWread(rw, &format, 1, 2); - format = BE_SHORT(format); - if (format != 0 && format != 1) - return 0; - - SDL_RWread(rw, &tracks, 1, 2); - tracks = BE_SHORT(tracks); - mididata->nTracks = tracks; - + int i = 0; + Uint32 ID; + Uint32 size; + Uint16 format; + Uint16 tracks; + Uint16 division; + + if (!mididata) + return 0; + if (!rw) + return 0; + + /* Make sure this is really a MIDI file */ + SDL_RWread(rw, &ID, 1, 4); + if (BE_LONG(ID) != 'MThd') + return 0; + + /* Header size must be 6 */ + SDL_RWread(rw, &size, 1, 4); + size = BE_LONG(size); + if (size != 6) + return 0; + + /* We only support format 0 and 1, but not 2 */ + SDL_RWread(rw, &format, 1, 2); + format = BE_SHORT(format); + if (format != 0 && format != 1) + return 0; + + SDL_RWread(rw, &tracks, 1, 2); + tracks = BE_SHORT(tracks); + mididata->nTracks = tracks; + /* Allocate tracks */ mididata->track = (MIDITrack*) calloc(1, sizeof(MIDITrack) * mididata->nTracks); if (NULL == mididata->track) @@ -318,92 +318,92 @@ static int ReadMIDIFile(MIDIFile *mididata, SDL_RWops *rw) Mix_SetError("Out of memory"); goto bail; } - - /* Retrieve the PPQN value, needed for playback */ - SDL_RWread(rw, &division, 1, 2); - mididata->division = BE_SHORT(division); - - - for (i=0; itrack[i].len = size; - mididata->track[i].data = malloc(size); - if (NULL == mididata->track[i].data) - { - Mix_SetError("Out of memory"); - goto bail; - } - SDL_RWread(rw, mididata->track[i].data, 1, size); - } - return 1; + + /* Retrieve the PPQN value, needed for playback */ + SDL_RWread(rw, &division, 1, 2); + mididata->division = BE_SHORT(division); + + + for (i=0; itrack[i].len = size; + mididata->track[i].data = malloc(size); + if (NULL == mididata->track[i].data) + { + Mix_SetError("Out of memory"); + goto bail; + } + SDL_RWread(rw, mididata->track[i].data, 1, size); + } + return 1; bail: - for(;i >= 0; i--) - { - if (mididata->track[i].data) - free(mididata->track[i].data); - } + for(;i >= 0; i--) + { + if (mididata->track[i].data) + free(mididata->track[i].data); + } - return 0; + return 0; } MIDIEvent *CreateMIDIEventList(SDL_RWops *rw, Uint16 *division) { - MIDIFile *mididata = NULL; - MIDIEvent *eventList; - int trackID; - - mididata = calloc(1, sizeof(MIDIFile)); - if (!mididata) - return NULL; - - /* Open the file */ - if ( rw != NULL ) - { - /* Read in the data */ - if ( ! ReadMIDIFile(mididata, rw)) - { - free(mididata); - return NULL; - } - } - else - { - free(mididata); - return NULL; - } - - if (division) - *division = mididata->division; - - eventList = MIDItoStream(mididata); - - for(trackID = 0; trackID < mididata->nTracks; trackID++) - { - if (mididata->track[trackID].data) - free(mididata->track[trackID].data); - } - free(mididata->track); + MIDIFile *mididata = NULL; + MIDIEvent *eventList; + int trackID; + + mididata = calloc(1, sizeof(MIDIFile)); + if (!mididata) + return NULL; + + /* Open the file */ + if ( rw != NULL ) + { + /* Read in the data */ + if ( ! ReadMIDIFile(mididata, rw)) + { + free(mididata); + return NULL; + } + } + else + { + free(mididata); + return NULL; + } + + if (division) + *division = mididata->division; + + eventList = MIDItoStream(mididata); + + for(trackID = 0; trackID < mididata->nTracks; trackID++) + { + if (mididata->track[trackID].data) + free(mididata->track[trackID].data); + } + free(mididata->track); free(mididata); - - return eventList; + + return eventList; } void FreeMIDIEventList(MIDIEvent *head) { - MIDIEvent *cur, *next; - - cur = head; - - while (cur) - { - next = cur->next; - if (cur->extraData) - free (cur->extraData); - free (cur); - cur = next; - } + MIDIEvent *cur, *next; + + cur = head; + + while (cur) + { + next = cur->next; + if (cur->extraData) + free (cur->extraData); + free (cur); + cur = next; + } } diff --git a/native_midi/native_midi_common.h b/native_midi/native_midi_common.h index e0400272..438d53d4 100644 --- a/native_midi/native_midi_common.h +++ b/native_midi/native_midi_common.h @@ -25,14 +25,14 @@ #include "SDL.h" /* Midi Status Bytes */ -#define MIDI_STATUS_NOTE_OFF 0x8 -#define MIDI_STATUS_NOTE_ON 0x9 -#define MIDI_STATUS_AFTERTOUCH 0xA -#define MIDI_STATUS_CONTROLLER 0xB -#define MIDI_STATUS_PROG_CHANGE 0xC -#define MIDI_STATUS_PRESSURE 0xD -#define MIDI_STATUS_PITCH_WHEEL 0xE -#define MIDI_STATUS_SYSEX 0xF +#define MIDI_STATUS_NOTE_OFF 0x8 +#define MIDI_STATUS_NOTE_ON 0x9 +#define MIDI_STATUS_AFTERTOUCH 0xA +#define MIDI_STATUS_CONTROLLER 0xB +#define MIDI_STATUS_PROG_CHANGE 0xC +#define MIDI_STATUS_PRESSURE 0xD +#define MIDI_STATUS_PITCH_WHEEL 0xE +#define MIDI_STATUS_SYSEX 0xF /* We store the midi events in a linked list; this way it is easy to shuffle the tracks together later on; and we are @@ -40,20 +40,20 @@ */ typedef struct MIDIEvent { - Uint32 time; /* Time at which this midi events occurs */ - Uint8 status; /* Status byte */ - Uint8 data[2]; /* 1 or 2 bytes additional data for most events */ - - Uint32 extraLen; /* For some SysEx events, we need additional storage */ - Uint8 *extraData; - - struct MIDIEvent *next; + Uint32 time; /* Time at which this midi events occurs */ + Uint8 status; /* Status byte */ + Uint8 data[2]; /* 1 or 2 bytes additional data for most events */ + + Uint32 extraLen; /* For some SysEx events, we need additional storage */ + Uint8 *extraData; + + struct MIDIEvent *next; } MIDIEvent; /* Load a midifile to memory, converting it to a list of MIDIEvents. This function returns a linked lists of MIDIEvents, 0 if an error occured. - */ + */ MIDIEvent *CreateMIDIEventList(SDL_RWops *rw, Uint16 *division); /* Release a MIDIEvent list after usage. */ diff --git a/native_midi/native_midi_haiku.cpp b/native_midi/native_midi_haiku.cpp index 8de350e8..97a49a48 100644 --- a/native_midi/native_midi_haiku.cpp +++ b/native_midi/native_midi_haiku.cpp @@ -139,7 +139,7 @@ class MidiEventsStore : public BMidi { case B_SYS_EX_START: SpraySystemExclusive(ev->extraData, ev->extraLen, time); - break; + break; case B_MIDI_TIME_CODE: case B_SONG_POSITION: case B_SONG_SELECT: @@ -147,26 +147,26 @@ class MidiEventsStore : public BMidi case B_TUNE_REQUEST: case B_SYS_EX_END: SpraySystemCommon(ev->status, ev->data[0], ev->data[1], time); - break; + break; case B_TIMING_CLOCK: case B_START: case B_STOP: case B_CONTINUE: case B_ACTIVE_SENSING: SpraySystemRealTime(ev->status, time); - break; + break; case B_SYSTEM_RESET: if (ev->data[0] == 0x51 && ev->data[1] == 0x03) - { - assert(ev->extraLen == 3); - int val = (ev->extraData[0] << 16) | (ev->extraData[1] << 8) | ev->extraData[2]; - int tempo = 60000000 / val; - SprayTempoChange(tempo, time); - } - else - { - SpraySystemRealTime(ev->status, time); - } + { + assert(ev->extraLen == 3); + int val = (ev->extraData[0] << 16) | (ev->extraData[1] << 8) | ev->extraData[2]; + int tempo = 60000000 / val; + SprayTempoChange(tempo, time); + } + else + { + SpraySystemRealTime(ev->status, time); + } } break; } @@ -243,7 +243,7 @@ void native_midi_freesong(NativeMidiSong *song) if (song == NULL) return; song->store->Stop(); song->store->Disconnect(&synth); - if (currentSong == song) + if (currentSong == song) { currentSong = NULL; } diff --git a/native_midi/native_midi_mac.c b/native_midi/native_midi_mac.c index 01e78774..5c55b710 100644 --- a/native_midi/native_midi_mac.c +++ b/native_midi/native_midi_mac.c @@ -40,605 +40,605 @@ /* Native Midi song */ struct _NativeMidiSong { - Uint32 *tuneSequence; - Uint32 *tuneHeader; + Uint32 *tuneSequence; + Uint32 *tuneHeader; }; enum { - /* number of (32-bit) long words in a note request event */ - kNoteRequestEventLength = ((sizeof(NoteRequest)/sizeof(long)) + 2), + /* number of (32-bit) long words in a note request event */ + kNoteRequestEventLength = ((sizeof(NoteRequest)/sizeof(long)) + 2), - /* number of (32-bit) long words in a marker event */ - kMarkerEventLength = 1, + /* number of (32-bit) long words in a marker event */ + kMarkerEventLength = 1, - /* number of (32-bit) long words in a general event, minus its data */ - kGeneralEventLength = 2 + /* number of (32-bit) long words in a general event, minus its data */ + kGeneralEventLength = 2 }; -#define ERROR_BUF_SIZE 256 -#define BUFFER_INCREMENT 5000 +#define ERROR_BUF_SIZE 256 +#define BUFFER_INCREMENT 5000 -#define REST_IF_NECESSARY() do {\ - int timeDiff = eventPos->time - lastEventTime; \ - if(timeDiff) \ - { \ - timeDiff = (int)(timeDiff*tick); \ - qtma_StuffRestEvent(*tunePos, timeDiff); \ - tunePos++; \ - lastEventTime = eventPos->time; \ - } \ - } while(0) +#define REST_IF_NECESSARY() do {\ + int timeDiff = eventPos->time - lastEventTime; \ + if(timeDiff) \ + { \ + timeDiff = (int)(timeDiff*tick); \ + qtma_StuffRestEvent(*tunePos, timeDiff); \ + tunePos++; \ + lastEventTime = eventPos->time; \ + } \ + } while(0) static Uint32 *BuildTuneSequence(MIDIEvent *evntlist, int ppqn, int part_poly_max[32], int part_to_inst[32], int *numParts); static Uint32 *BuildTuneHeader(int part_poly_max[32], int part_to_inst[32], int numParts); /* The global TunePlayer instance */ -static TunePlayer gTunePlayer = NULL; -static int gInstaceCount = 0; -static Uint32 *gCurrentTuneSequence = NULL; -static char gErrorBuffer[ERROR_BUF_SIZE] = ""; +static TunePlayer gTunePlayer = NULL; +static int gInstaceCount = 0; +static Uint32 *gCurrentTuneSequence = NULL; +static char gErrorBuffer[ERROR_BUF_SIZE] = ""; /* Check whether QuickTime is available */ int native_midi_detect() { - /* TODO */ - return 1; + /* TODO */ + return 1; } NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw, int freerw) { - NativeMidiSong *song = NULL; - MIDIEvent *evntlist = NULL; - int part_to_inst[32]; - int part_poly_max[32]; - int numParts = 0; - Uint16 ppqn; - - /* Init the arrays */ - memset(part_poly_max,0,sizeof(part_poly_max)); - memset(part_to_inst,-1,sizeof(part_to_inst)); - - /* Attempt to load the midi file */ - evntlist = CreateMIDIEventList(rw, &ppqn); - if (!evntlist) - goto bail; - - /* Allocate memory for the song struct */ - song = malloc(sizeof(NativeMidiSong)); - if (!song) - goto bail; - - /* Build a tune sequence from the event list */ - song->tuneSequence = BuildTuneSequence(evntlist, ppqn, part_poly_max, part_to_inst, &numParts); - if(!song->tuneSequence) - goto bail; - - /* Now build a tune header from the data we collect above, create - all parts as needed and assign them the correct instrument. - */ - song->tuneHeader = BuildTuneHeader(part_poly_max, part_to_inst, numParts); - if(!song->tuneHeader) - goto bail; - - /* Increment the instance count */ - gInstaceCount++; - if (gTunePlayer == NULL) - gTunePlayer = OpenDefaultComponent(kTunePlayerComponentType, 0); - - /* Finally, free the event list */ - FreeMIDIEventList(evntlist); - - if (freerw) { - SDL_RWclose(rw); - } - return song; - + NativeMidiSong *song = NULL; + MIDIEvent *evntlist = NULL; + int part_to_inst[32]; + int part_poly_max[32]; + int numParts = 0; + Uint16 ppqn; + + /* Init the arrays */ + memset(part_poly_max,0,sizeof(part_poly_max)); + memset(part_to_inst,-1,sizeof(part_to_inst)); + + /* Attempt to load the midi file */ + evntlist = CreateMIDIEventList(rw, &ppqn); + if (!evntlist) + goto bail; + + /* Allocate memory for the song struct */ + song = malloc(sizeof(NativeMidiSong)); + if (!song) + goto bail; + + /* Build a tune sequence from the event list */ + song->tuneSequence = BuildTuneSequence(evntlist, ppqn, part_poly_max, part_to_inst, &numParts); + if(!song->tuneSequence) + goto bail; + + /* Now build a tune header from the data we collect above, create + all parts as needed and assign them the correct instrument. + */ + song->tuneHeader = BuildTuneHeader(part_poly_max, part_to_inst, numParts); + if(!song->tuneHeader) + goto bail; + + /* Increment the instance count */ + gInstaceCount++; + if (gTunePlayer == NULL) + gTunePlayer = OpenDefaultComponent(kTunePlayerComponentType, 0); + + /* Finally, free the event list */ + FreeMIDIEventList(evntlist); + + if (freerw) { + SDL_RWclose(rw); + } + return song; + bail: - if (evntlist) - FreeMIDIEventList(evntlist); - - if (song) - { - if(song->tuneSequence) - free(song->tuneSequence); - - if(song->tuneHeader) - DisposePtr((Ptr)song->tuneHeader); - - free(song); - } - - if (freerw) { - SDL_RWclose(rw); - } - return NULL; + if (evntlist) + FreeMIDIEventList(evntlist); + + if (song) + { + if(song->tuneSequence) + free(song->tuneSequence); + + if(song->tuneHeader) + DisposePtr((Ptr)song->tuneHeader); + + free(song); + } + + if (freerw) { + SDL_RWclose(rw); + } + return NULL; } void native_midi_freesong(NativeMidiSong *song) { - if(!song || !song->tuneSequence) - return; - - /* If this is the currently playing song, stop it now */ - if (song->tuneSequence == gCurrentTuneSequence) - native_midi_stop(); - - /* Finally, free the data storage */ - free(song->tuneSequence); - DisposePtr((Ptr)song->tuneHeader); - free(song); - - /* Increment the instance count */ - gInstaceCount--; - if ((gTunePlayer != NULL) && (gInstaceCount == 0)) - { - CloseComponent(gTunePlayer); - gTunePlayer = NULL; - } + if(!song || !song->tuneSequence) + return; + + /* If this is the currently playing song, stop it now */ + if (song->tuneSequence == gCurrentTuneSequence) + native_midi_stop(); + + /* Finally, free the data storage */ + free(song->tuneSequence); + DisposePtr((Ptr)song->tuneHeader); + free(song); + + /* Increment the instance count */ + gInstaceCount--; + if ((gTunePlayer != NULL) && (gInstaceCount == 0)) + { + CloseComponent(gTunePlayer); + gTunePlayer = NULL; + } } void native_midi_start(NativeMidiSong *song, int loops) { - UInt32 queueFlags = 0; - ComponentResult tpError; - - assert (gTunePlayer != NULL); - - /* FIXME: is this code even used anymore? */ - assert (loops == 0); - - SDL_PauseAudio(1); - SDL_UnlockAudio(); - - /* First, stop the currently playing music */ - native_midi_stop(); - - /* Set up the queue flags */ - queueFlags = kTuneStartNow; - - /* Set the time scale (units per second), we want milliseconds */ - tpError = TuneSetTimeScale(gTunePlayer, 1000); - if (tpError != noErr) - { - strncpy (gErrorBuffer, "MIDI error during TuneSetTimeScale", ERROR_BUF_SIZE); - goto done; - } - - /* Set the header, to tell what instruments are used */ - tpError = TuneSetHeader(gTunePlayer, (UInt32 *)song->tuneHeader); - if (tpError != noErr) - { - strncpy (gErrorBuffer, "MIDI error during TuneSetHeader", ERROR_BUF_SIZE); - goto done; - } - - /* Have it allocate whatever resources are needed */ - tpError = TunePreroll(gTunePlayer); - if (tpError != noErr) - { - strncpy (gErrorBuffer, "MIDI error during TunePreroll", ERROR_BUF_SIZE); - goto done; - } - - /* We want to play at normal volume */ - tpError = TuneSetVolume(gTunePlayer, 0x00010000); - if (tpError != noErr) - { - strncpy (gErrorBuffer, "MIDI error during TuneSetVolume", ERROR_BUF_SIZE); - goto done; - } - - /* Finally, start playing the full song */ - gCurrentTuneSequence = song->tuneSequence; - tpError = TuneQueue(gTunePlayer, (UInt32 *)song->tuneSequence, 0x00010000, 0, 0xFFFFFFFF, queueFlags, NULL, 0); - if (tpError != noErr) - { - strncpy (gErrorBuffer, "MIDI error during TuneQueue", ERROR_BUF_SIZE); - goto done; - } - + UInt32 queueFlags = 0; + ComponentResult tpError; + + assert (gTunePlayer != NULL); + + /* FIXME: is this code even used anymore? */ + assert (loops == 0); + + SDL_PauseAudio(1); + SDL_UnlockAudio(); + + /* First, stop the currently playing music */ + native_midi_stop(); + + /* Set up the queue flags */ + queueFlags = kTuneStartNow; + + /* Set the time scale (units per second), we want milliseconds */ + tpError = TuneSetTimeScale(gTunePlayer, 1000); + if (tpError != noErr) + { + strncpy (gErrorBuffer, "MIDI error during TuneSetTimeScale", ERROR_BUF_SIZE); + goto done; + } + + /* Set the header, to tell what instruments are used */ + tpError = TuneSetHeader(gTunePlayer, (UInt32 *)song->tuneHeader); + if (tpError != noErr) + { + strncpy (gErrorBuffer, "MIDI error during TuneSetHeader", ERROR_BUF_SIZE); + goto done; + } + + /* Have it allocate whatever resources are needed */ + tpError = TunePreroll(gTunePlayer); + if (tpError != noErr) + { + strncpy (gErrorBuffer, "MIDI error during TunePreroll", ERROR_BUF_SIZE); + goto done; + } + + /* We want to play at normal volume */ + tpError = TuneSetVolume(gTunePlayer, 0x00010000); + if (tpError != noErr) + { + strncpy (gErrorBuffer, "MIDI error during TuneSetVolume", ERROR_BUF_SIZE); + goto done; + } + + /* Finally, start playing the full song */ + gCurrentTuneSequence = song->tuneSequence; + tpError = TuneQueue(gTunePlayer, (UInt32 *)song->tuneSequence, 0x00010000, 0, 0xFFFFFFFF, queueFlags, NULL, 0); + if (tpError != noErr) + { + strncpy (gErrorBuffer, "MIDI error during TuneQueue", ERROR_BUF_SIZE); + goto done; + } + done: - SDL_LockAudio(); - SDL_PauseAudio(0); + SDL_LockAudio(); + SDL_PauseAudio(0); } void native_midi_stop() { - if (gTunePlayer == NULL) - return; - - /* Stop music */ - TuneStop(gTunePlayer, 0); - - /* Deallocate all instruments */ - TuneUnroll(gTunePlayer); + if (gTunePlayer == NULL) + return; + + /* Stop music */ + TuneStop(gTunePlayer, 0); + + /* Deallocate all instruments */ + TuneUnroll(gTunePlayer); } int native_midi_active() { - if (gTunePlayer != NULL) - { - TuneStatus ts; - - TuneGetStatus(gTunePlayer,&ts); - return ts.queueTime != 0; - } - else - return 0; + if (gTunePlayer != NULL) + { + TuneStatus ts; + + TuneGetStatus(gTunePlayer,&ts); + return ts.queueTime != 0; + } + else + return 0; } void native_midi_setvolume(int volume) { - if (gTunePlayer == NULL) - return; + if (gTunePlayer == NULL) + return; - /* QTMA olume may range from 0.0 to 1.0 (in 16.16 fixed point encoding) */ - TuneSetVolume(gTunePlayer, (0x00010000 * volume)/SDL_MIX_MAXVOLUME); + /* QTMA olume may range from 0.0 to 1.0 (in 16.16 fixed point encoding) */ + TuneSetVolume(gTunePlayer, (0x00010000 * volume)/SDL_MIX_MAXVOLUME); } const char *native_midi_error(void) { - return gErrorBuffer; + return gErrorBuffer; } Uint32 *BuildTuneSequence(MIDIEvent *evntlist, int ppqn, int part_poly_max[32], int part_to_inst[32], int *numParts) { - int part_poly[32]; - int channel_to_part[16]; - - int channel_pan[16]; - int channel_vol[16]; - int channel_pitch_bend[16]; - - int lastEventTime = 0; - int tempo = 500000; - double Ippqn = 1.0 / (1000*ppqn); - double tick = tempo * Ippqn; - MIDIEvent *eventPos = evntlist; - MIDIEvent *noteOffPos; - Uint32 *tunePos, *endPos; - Uint32 *tuneSequence; - size_t tuneSize; - - /* allocate space for the tune header */ - tuneSize = 5000; - tuneSequence = (Uint32 *)malloc(tuneSize * sizeof(Uint32)); - if (tuneSequence == NULL) - return NULL; - - /* Set starting position in our tune memory */ - tunePos = tuneSequence; - endPos = tuneSequence + tuneSize; - - /* Initialise the arrays */ - memset(part_poly,0,sizeof(part_poly)); - - memset(channel_to_part,-1,sizeof(channel_to_part)); - memset(channel_pan,-1,sizeof(channel_pan)); - memset(channel_vol,-1,sizeof(channel_vol)); - memset(channel_pitch_bend,-1,sizeof(channel_pitch_bend)); - - *numParts = 0; - - /* - * Now the major work - iterate over all GM events, - * and turn them into QuickTime Music format. - * At the same time, calculate the max. polyphony for each part, - * and also the part->instrument mapping. - */ - while(eventPos) - { - int status = (eventPos->status&0xF0)>>4; - int channel = eventPos->status&0x0F; - int part = channel_to_part[channel]; + int part_poly[32]; + int channel_to_part[16]; + + int channel_pan[16]; + int channel_vol[16]; + int channel_pitch_bend[16]; + + int lastEventTime = 0; + int tempo = 500000; + double Ippqn = 1.0 / (1000*ppqn); + double tick = tempo * Ippqn; + MIDIEvent *eventPos = evntlist; + MIDIEvent *noteOffPos; + Uint32 *tunePos, *endPos; + Uint32 *tuneSequence; + size_t tuneSize; + + /* allocate space for the tune header */ + tuneSize = 5000; + tuneSequence = (Uint32 *)malloc(tuneSize * sizeof(Uint32)); + if (tuneSequence == NULL) + return NULL; + + /* Set starting position in our tune memory */ + tunePos = tuneSequence; + endPos = tuneSequence + tuneSize; + + /* Initialise the arrays */ + memset(part_poly,0,sizeof(part_poly)); + + memset(channel_to_part,-1,sizeof(channel_to_part)); + memset(channel_pan,-1,sizeof(channel_pan)); + memset(channel_vol,-1,sizeof(channel_vol)); + memset(channel_pitch_bend,-1,sizeof(channel_pitch_bend)); + + *numParts = 0; + + /* + * Now the major work - iterate over all GM events, + * and turn them into QuickTime Music format. + * At the same time, calculate the max. polyphony for each part, + * and also the part->instrument mapping. + */ + while(eventPos) + { + int status = (eventPos->status&0xF0)>>4; + int channel = eventPos->status&0x0F; + int part = channel_to_part[channel]; int velocity, pitch; int value, controller; int bend; int newInst; - - /* Check if we are running low on space... */ - if((tunePos+16) > endPos) - { - /* Resize our data storage. */ - Uint32 *oldTuneSequence = tuneSequence; - - tuneSize += BUFFER_INCREMENT; - tuneSequence = (Uint32 *)realloc(tuneSequence, tuneSize * sizeof(Uint32)); - if(oldTuneSequence != tuneSequence) - tunePos += tuneSequence - oldTuneSequence; - endPos = tuneSequence + tuneSize; - } - - switch (status) - { - case MIDI_STATUS_NOTE_OFF: - assert(part>=0 && part<=31); - - /* Keep track of the polyphony of the current part */ - part_poly[part]--; - break; - case MIDI_STATUS_NOTE_ON: - if (part < 0) - { - /* If no part is specified yet, we default to the first instrument, which - is piano (or the first drum kit if we are on the drum channel) - */ - int newInst; - - if (channel == 9) - newInst = kFirstDrumkit + 1; /* the first drum kit is the "no drum" kit! */ - else - newInst = kFirstGMInstrument; - part = channel_to_part[channel] = *numParts; - part_to_inst[(*numParts)++] = newInst; - } - /* TODO - add support for more than 32 parts using eXtended QTMA events */ - assert(part<=31); - - /* Decode pitch & velocity */ - pitch = eventPos->data[0]; - velocity = eventPos->data[1]; - - if (velocity == 0) - { - /* was a NOTE OFF in disguise, so we decrement the polyphony */ - part_poly[part]--; - } - else - { - /* Keep track of the polyphony of the current part */ - int foo = ++part_poly[part]; - if (part_poly_max[part] < foo) - part_poly_max[part] = foo; - - /* Now scan forward to find the matching NOTE OFF event */ - for(noteOffPos = eventPos; noteOffPos; noteOffPos = noteOffPos->next) - { - if ((noteOffPos->status&0xF0)>>4 == MIDI_STATUS_NOTE_OFF - && channel == (eventPos->status&0x0F) - && pitch == noteOffPos->data[0]) - break; - /* NOTE ON with velocity == 0 is the same as a NOTE OFF */ - if ((noteOffPos->status&0xF0)>>4 == MIDI_STATUS_NOTE_ON - && channel == (eventPos->status&0x0F) - && pitch == noteOffPos->data[0] - && 0 == noteOffPos->data[1]) - break; - } - - /* Did we find a note off? Should always be the case, but who knows... */ - if (noteOffPos) - { - /* We found a NOTE OFF, now calculate the note duration */ - int duration = (int)((noteOffPos->time - eventPos->time)*tick); - - REST_IF_NECESSARY(); - /* Now we need to check if we get along with a normal Note Event, or if we need an extended one... */ - if (duration < 2048 && pitch>=32 && pitch<=95 && velocity>=0 && velocity<=127) - { - qtma_StuffNoteEvent(*tunePos, part, pitch, velocity, duration); - tunePos++; - } - else - { - qtma_StuffXNoteEvent(*tunePos, *(tunePos+1), part, pitch, velocity, duration); - tunePos+=2; - } - } - } - break; - case MIDI_STATUS_AFTERTOUCH: - /* NYI - use kControllerAfterTouch. But how are the parameters to be mapped? */ - break; - case MIDI_STATUS_CONTROLLER: - controller = eventPos->data[0]; - value = eventPos->data[1]; - - switch(controller) - { - case 0: /* bank change - igore for now */ - break; - case kControllerVolume: - if(channel_vol[channel] != value<<8) - { - channel_vol[channel] = value<<8; - if(part>=0 && part<=31) - { - REST_IF_NECESSARY(); - qtma_StuffControlEvent(*tunePos, part, kControllerVolume, channel_vol[channel]); - tunePos++; - } - } - break; - case kControllerPan: - if(channel_pan[channel] != (value << 1) + 256) - { - channel_pan[channel] = (value << 1) + 256; - if(part>=0 && part<=31) - { - REST_IF_NECESSARY(); - qtma_StuffControlEvent(*tunePos, part, kControllerPan, channel_pan[channel]); - tunePos++; - } - } - break; - default: - /* No other controllers implemented yet */; - break; - } - - break; - case MIDI_STATUS_PROG_CHANGE: - /* Instrument changed */ - newInst = eventPos->data[0]; - - /* Channel 9 (the 10th channel) is different, it indicates a drum kit */ - if (channel == 9) - newInst += kFirstDrumkit; - else - newInst += kFirstGMInstrument; - /* Only if the instrument for this channel *really* changed, add a new part. */ - if(newInst != part_to_inst[part]) - { - /* TODO maybe make use of kGeneralEventPartChange here, - to help QT reuse note channels? - */ - part = channel_to_part[channel] = *numParts; - part_to_inst[(*numParts)++] = newInst; - - if(channel_vol[channel] >= 0) - { - REST_IF_NECESSARY(); - qtma_StuffControlEvent(*tunePos, part, kControllerVolume, channel_vol[channel]); - tunePos++; - } - if(channel_pan[channel] >= 0) - { - REST_IF_NECESSARY(); - qtma_StuffControlEvent(*tunePos, part, kControllerPan, channel_pan[channel]); - tunePos++; - } - if(channel_pitch_bend[channel] >= 0) - { - REST_IF_NECESSARY(); - qtma_StuffControlEvent(*tunePos, part, kControllerPitchBend, channel_pitch_bend[channel]); - tunePos++; - } - } - break; - case MIDI_STATUS_PRESSURE: - /* NYI */ - break; - case MIDI_STATUS_PITCH_WHEEL: - /* In the midi spec, 0x2000 = center, 0x0000 = - 2 semitones, 0x3FFF = +2 semitones - but for QTMA, we specify it as a 8.8 fixed point of semitones - TODO: detect "pitch bend range changes" & honor them! - */ - bend = (eventPos->data[0] & 0x7f) | ((eventPos->data[1] & 0x7f) << 7); - - /* "Center" the bend */ - bend -= 0x2000; - - /* Move it to our format: */ - bend <<= 4; - - /* If it turns out the pitch bend didn't change, stop here */ - if(channel_pitch_bend[channel] == bend) - break; - - channel_pitch_bend[channel] = bend; - if(part>=0 && part<=31) - { - /* Stuff a control event */ - REST_IF_NECESSARY(); - qtma_StuffControlEvent(*tunePos, part, kControllerPitchBend, bend); - tunePos++; - } - break; - case MIDI_STATUS_SYSEX: - if (eventPos->status == 0xFF && eventPos->data[0] == 0x51) /* Tempo change */ - { - tempo = (eventPos->extraData[0] << 16) + - (eventPos->extraData[1] << 8) + - eventPos->extraData[2]; - - tick = tempo * Ippqn; - } - break; - } - - /* on to the next event */ - eventPos = eventPos->next; - } - - /* Finally, place an end marker */ - *tunePos = kEndMarkerValue; - - return tuneSequence; + + /* Check if we are running low on space... */ + if((tunePos+16) > endPos) + { + /* Resize our data storage. */ + Uint32 *oldTuneSequence = tuneSequence; + + tuneSize += BUFFER_INCREMENT; + tuneSequence = (Uint32 *)realloc(tuneSequence, tuneSize * sizeof(Uint32)); + if(oldTuneSequence != tuneSequence) + tunePos += tuneSequence - oldTuneSequence; + endPos = tuneSequence + tuneSize; + } + + switch (status) + { + case MIDI_STATUS_NOTE_OFF: + assert(part>=0 && part<=31); + + /* Keep track of the polyphony of the current part */ + part_poly[part]--; + break; + case MIDI_STATUS_NOTE_ON: + if (part < 0) + { + /* If no part is specified yet, we default to the first instrument, which + is piano (or the first drum kit if we are on the drum channel) + */ + int newInst; + + if (channel == 9) + newInst = kFirstDrumkit + 1; /* the first drum kit is the "no drum" kit! */ + else + newInst = kFirstGMInstrument; + part = channel_to_part[channel] = *numParts; + part_to_inst[(*numParts)++] = newInst; + } + /* TODO - add support for more than 32 parts using eXtended QTMA events */ + assert(part<=31); + + /* Decode pitch & velocity */ + pitch = eventPos->data[0]; + velocity = eventPos->data[1]; + + if (velocity == 0) + { + /* was a NOTE OFF in disguise, so we decrement the polyphony */ + part_poly[part]--; + } + else + { + /* Keep track of the polyphony of the current part */ + int foo = ++part_poly[part]; + if (part_poly_max[part] < foo) + part_poly_max[part] = foo; + + /* Now scan forward to find the matching NOTE OFF event */ + for(noteOffPos = eventPos; noteOffPos; noteOffPos = noteOffPos->next) + { + if ((noteOffPos->status&0xF0)>>4 == MIDI_STATUS_NOTE_OFF + && channel == (eventPos->status&0x0F) + && pitch == noteOffPos->data[0]) + break; + /* NOTE ON with velocity == 0 is the same as a NOTE OFF */ + if ((noteOffPos->status&0xF0)>>4 == MIDI_STATUS_NOTE_ON + && channel == (eventPos->status&0x0F) + && pitch == noteOffPos->data[0] + && 0 == noteOffPos->data[1]) + break; + } + + /* Did we find a note off? Should always be the case, but who knows... */ + if (noteOffPos) + { + /* We found a NOTE OFF, now calculate the note duration */ + int duration = (int)((noteOffPos->time - eventPos->time)*tick); + + REST_IF_NECESSARY(); + /* Now we need to check if we get along with a normal Note Event, or if we need an extended one... */ + if (duration < 2048 && pitch>=32 && pitch<=95 && velocity>=0 && velocity<=127) + { + qtma_StuffNoteEvent(*tunePos, part, pitch, velocity, duration); + tunePos++; + } + else + { + qtma_StuffXNoteEvent(*tunePos, *(tunePos+1), part, pitch, velocity, duration); + tunePos+=2; + } + } + } + break; + case MIDI_STATUS_AFTERTOUCH: + /* NYI - use kControllerAfterTouch. But how are the parameters to be mapped? */ + break; + case MIDI_STATUS_CONTROLLER: + controller = eventPos->data[0]; + value = eventPos->data[1]; + + switch(controller) + { + case 0: /* bank change - igore for now */ + break; + case kControllerVolume: + if(channel_vol[channel] != value<<8) + { + channel_vol[channel] = value<<8; + if(part>=0 && part<=31) + { + REST_IF_NECESSARY(); + qtma_StuffControlEvent(*tunePos, part, kControllerVolume, channel_vol[channel]); + tunePos++; + } + } + break; + case kControllerPan: + if(channel_pan[channel] != (value << 1) + 256) + { + channel_pan[channel] = (value << 1) + 256; + if(part>=0 && part<=31) + { + REST_IF_NECESSARY(); + qtma_StuffControlEvent(*tunePos, part, kControllerPan, channel_pan[channel]); + tunePos++; + } + } + break; + default: + /* No other controllers implemented yet */; + break; + } + + break; + case MIDI_STATUS_PROG_CHANGE: + /* Instrument changed */ + newInst = eventPos->data[0]; + + /* Channel 9 (the 10th channel) is different, it indicates a drum kit */ + if (channel == 9) + newInst += kFirstDrumkit; + else + newInst += kFirstGMInstrument; + /* Only if the instrument for this channel *really* changed, add a new part. */ + if(newInst != part_to_inst[part]) + { + /* TODO maybe make use of kGeneralEventPartChange here, + to help QT reuse note channels? + */ + part = channel_to_part[channel] = *numParts; + part_to_inst[(*numParts)++] = newInst; + + if(channel_vol[channel] >= 0) + { + REST_IF_NECESSARY(); + qtma_StuffControlEvent(*tunePos, part, kControllerVolume, channel_vol[channel]); + tunePos++; + } + if(channel_pan[channel] >= 0) + { + REST_IF_NECESSARY(); + qtma_StuffControlEvent(*tunePos, part, kControllerPan, channel_pan[channel]); + tunePos++; + } + if(channel_pitch_bend[channel] >= 0) + { + REST_IF_NECESSARY(); + qtma_StuffControlEvent(*tunePos, part, kControllerPitchBend, channel_pitch_bend[channel]); + tunePos++; + } + } + break; + case MIDI_STATUS_PRESSURE: + /* NYI */ + break; + case MIDI_STATUS_PITCH_WHEEL: + /* In the midi spec, 0x2000 = center, 0x0000 = - 2 semitones, 0x3FFF = +2 semitones + but for QTMA, we specify it as a 8.8 fixed point of semitones + TODO: detect "pitch bend range changes" & honor them! + */ + bend = (eventPos->data[0] & 0x7f) | ((eventPos->data[1] & 0x7f) << 7); + + /* "Center" the bend */ + bend -= 0x2000; + + /* Move it to our format: */ + bend <<= 4; + + /* If it turns out the pitch bend didn't change, stop here */ + if(channel_pitch_bend[channel] == bend) + break; + + channel_pitch_bend[channel] = bend; + if(part>=0 && part<=31) + { + /* Stuff a control event */ + REST_IF_NECESSARY(); + qtma_StuffControlEvent(*tunePos, part, kControllerPitchBend, bend); + tunePos++; + } + break; + case MIDI_STATUS_SYSEX: + if (eventPos->status == 0xFF && eventPos->data[0] == 0x51) /* Tempo change */ + { + tempo = (eventPos->extraData[0] << 16) + + (eventPos->extraData[1] << 8) + + eventPos->extraData[2]; + + tick = tempo * Ippqn; + } + break; + } + + /* on to the next event */ + eventPos = eventPos->next; + } + + /* Finally, place an end marker */ + *tunePos = kEndMarkerValue; + + return tuneSequence; } Uint32 *BuildTuneHeader(int part_poly_max[32], int part_to_inst[32], int numParts) { - Uint32 *myHeader; - Uint32 *myPos1, *myPos2; /* pointers to the head and tail long words of a music event */ - NoteRequest *myNoteRequest; - NoteAllocator myNoteAllocator; /* for the NAStuffToneDescription call */ - ComponentResult myErr = noErr; - int part; - - myHeader = NULL; - myNoteAllocator = NULL; - - /* - * Open up the Note Allocator - */ - myNoteAllocator = OpenDefaultComponent(kNoteAllocatorComponentType,0); - if (myNoteAllocator == NULL) - goto bail; - - /* - * Allocate space for the tune header - */ - myHeader = (Uint32 *) - NewPtrClear((numParts * kNoteRequestEventLength + kMarkerEventLength) * sizeof(Uint32)); - if (myHeader == NULL) - goto bail; - - myPos1 = myHeader; - - /* - * Loop over all parts - */ - for(part = 0; part < numParts; ++part) - { - /* - * Stuff request for the instrument with the given polyphony - */ - myPos2 = myPos1 + (kNoteRequestEventLength - 1); /* last longword of general event */ - qtma_StuffGeneralEvent(*myPos1, *myPos2, part, kGeneralEventNoteRequest, kNoteRequestEventLength); - myNoteRequest = (NoteRequest *)(myPos1 + 1); - myNoteRequest->info.flags = 0; - /* I'm told by the Apple people that the Quicktime types were poorly designed and it was - * too late to change them. On little endian, the BigEndian(Short|Fixed) types are structs - * while on big endian they are primitive types. Furthermore, Quicktime failed to - * provide setter and getter functions. To get this to work, we need to case the - * code for the two possible situations. - * My assumption is that the right-side value was always expected to be BigEndian - * as it was written way before the Universal Binary transition. So in the little endian - * case, OSSwap is used. - */ + Uint32 *myHeader; + Uint32 *myPos1, *myPos2; /* pointers to the head and tail long words of a music event */ + NoteRequest *myNoteRequest; + NoteAllocator myNoteAllocator; /* for the NAStuffToneDescription call */ + ComponentResult myErr = noErr; + int part; + + myHeader = NULL; + myNoteAllocator = NULL; + + /* + * Open up the Note Allocator + */ + myNoteAllocator = OpenDefaultComponent(kNoteAllocatorComponentType,0); + if (myNoteAllocator == NULL) + goto bail; + + /* + * Allocate space for the tune header + */ + myHeader = (Uint32 *) + NewPtrClear((numParts * kNoteRequestEventLength + kMarkerEventLength) * sizeof(Uint32)); + if (myHeader == NULL) + goto bail; + + myPos1 = myHeader; + + /* + * Loop over all parts + */ + for(part = 0; part < numParts; ++part) + { + /* + * Stuff request for the instrument with the given polyphony + */ + myPos2 = myPos1 + (kNoteRequestEventLength - 1); /* last longword of general event */ + qtma_StuffGeneralEvent(*myPos1, *myPos2, part, kGeneralEventNoteRequest, kNoteRequestEventLength); + myNoteRequest = (NoteRequest *)(myPos1 + 1); + myNoteRequest->info.flags = 0; + /* I'm told by the Apple people that the Quicktime types were poorly designed and it was + * too late to change them. On little endian, the BigEndian(Short|Fixed) types are structs + * while on big endian they are primitive types. Furthermore, Quicktime failed to + * provide setter and getter functions. To get this to work, we need to case the + * code for the two possible situations. + * My assumption is that the right-side value was always expected to be BigEndian + * as it was written way before the Universal Binary transition. So in the little endian + * case, OSSwap is used. + */ #if SDL_BYTEORDER == SDL_LIL_ENDIAN - myNoteRequest->info.polyphony.bigEndianValue = OSSwapHostToBigInt16(part_poly_max[part]); - myNoteRequest->info.typicalPolyphony.bigEndianValue = OSSwapHostToBigInt32(0x00010000); + myNoteRequest->info.polyphony.bigEndianValue = OSSwapHostToBigInt16(part_poly_max[part]); + myNoteRequest->info.typicalPolyphony.bigEndianValue = OSSwapHostToBigInt32(0x00010000); #else - myNoteRequest->info.polyphony = part_poly_max[part]; - myNoteRequest->info.typicalPolyphony = 0x00010000; + myNoteRequest->info.polyphony = part_poly_max[part]; + myNoteRequest->info.typicalPolyphony = 0x00010000; #endif - myErr = NAStuffToneDescription(myNoteAllocator,part_to_inst[part],&myNoteRequest->tone); - if (myErr != noErr) - goto bail; - - /* move pointer to beginning of next event */ - myPos1 += kNoteRequestEventLength; - } + myErr = NAStuffToneDescription(myNoteAllocator,part_to_inst[part],&myNoteRequest->tone); + if (myErr != noErr) + goto bail; + + /* move pointer to beginning of next event */ + myPos1 += kNoteRequestEventLength; + } - *myPos1 = kEndMarkerValue; /* end of sequence marker */ + *myPos1 = kEndMarkerValue; /* end of sequence marker */ bail: - if(myNoteAllocator) - CloseComponent(myNoteAllocator); + if(myNoteAllocator) + CloseComponent(myNoteAllocator); - /* if we encountered an error, dispose of the storage we allocated and return NULL */ - if (myErr != noErr) { - DisposePtr((Ptr)myHeader); - myHeader = NULL; - } + /* if we encountered an error, dispose of the storage we allocated and return NULL */ + if (myErr != noErr) { + DisposePtr((Ptr)myHeader); + myHeader = NULL; + } - return myHeader; + return myHeader; } #endif /* MacOS native MIDI support */ diff --git a/native_midi/native_midi_macosx.c b/native_midi/native_midi_macosx.c index 7084bf72..3aae073a 100644 --- a/native_midi/native_midi_macosx.c +++ b/native_midi/native_midi_macosx.c @@ -26,7 +26,7 @@ #if __MACOSX__ -#include /* ComponentDescription */ +#include /* ComponentDescription */ #include #include #include diff --git a/native_midi/native_midi_win32.c b/native_midi/native_midi_win32.c index 187d989f..5e6bbe54 100644 --- a/native_midi/native_midi_win32.c +++ b/native_midi/native_midi_win32.c @@ -106,34 +106,34 @@ static void MIDItoStream(NativeMidiSong *song, MIDIEvent *evntlist) newevent=song->NewEvents; while (event) { - int status = (event->status&0xF0)>>4; - switch (status) - { - case MIDI_STATUS_NOTE_OFF: - case MIDI_STATUS_NOTE_ON: - case MIDI_STATUS_AFTERTOUCH: - case MIDI_STATUS_CONTROLLER: - case MIDI_STATUS_PROG_CHANGE: - case MIDI_STATUS_PRESSURE: - case MIDI_STATUS_PITCH_WHEEL: + int status = (event->status&0xF0)>>4; + switch (status) + { + case MIDI_STATUS_NOTE_OFF: + case MIDI_STATUS_NOTE_ON: + case MIDI_STATUS_AFTERTOUCH: + case MIDI_STATUS_CONTROLLER: + case MIDI_STATUS_PROG_CHANGE: + case MIDI_STATUS_PRESSURE: + case MIDI_STATUS_PITCH_WHEEL: newevent->dwDeltaTime=event->time; - newevent->dwEvent=(event->status|0x80)|(event->data[0]<<8)|(event->data[1]<<16)|(MEVT_SHORTMSG<<24); + newevent->dwEvent=(event->status|0x80)|(event->data[0]<<8)|(event->data[1]<<16)|(MEVT_SHORTMSG<<24); newevent=(MIDIEVENT*)((char*)newevent+(3*sizeof(DWORD))); eventcount++; - break; - - case MIDI_STATUS_SYSEX: - if (event->status == 0xFF && event->data[0] == 0x51) /* Tempo change */ - { - int tempo = (event->extraData[0] << 16) | - (event->extraData[1] << 8) | - event->extraData[2]; + break; + + case MIDI_STATUS_SYSEX: + if (event->status == 0xFF && event->data[0] == 0x51) /* Tempo change */ + { + int tempo = (event->extraData[0] << 16) | + (event->extraData[1] << 8) | + event->extraData[2]; newevent->dwDeltaTime=event->time; - newevent->dwEvent=(MEVT_TEMPO<<24) | tempo; + newevent->dwEvent=(MEVT_TEMPO<<24) | tempo; newevent=(MIDIEVENT*)((char*)newevent+(3*sizeof(DWORD))); eventcount++; - } - break; + } + break; } event=event->next; @@ -202,37 +202,37 @@ int native_midi_detect() NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw, int freerw) { - NativeMidiSong *newsong; - MIDIEvent *evntlist = NULL; - - newsong=malloc(sizeof(NativeMidiSong)); - if (!newsong) { - if (freerw) { - SDL_RWclose(rw); - } - return NULL; - } - memset(newsong,0,sizeof(NativeMidiSong)); - - /* Attempt to load the midi file */ - evntlist = CreateMIDIEventList(rw, &newsong->ppqn); - if (!evntlist) - { - free(newsong); - if (freerw) { - SDL_RWclose(rw); - } - return NULL; - } - - MIDItoStream(newsong, evntlist); - - FreeMIDIEventList(evntlist); - - if (freerw) { - SDL_RWclose(rw); - } - return newsong; + NativeMidiSong *newsong; + MIDIEvent *evntlist = NULL; + + newsong=malloc(sizeof(NativeMidiSong)); + if (!newsong) { + if (freerw) { + SDL_RWclose(rw); + } + return NULL; + } + memset(newsong,0,sizeof(NativeMidiSong)); + + /* Attempt to load the midi file */ + evntlist = CreateMIDIEventList(rw, &newsong->ppqn); + if (!evntlist) + { + free(newsong); + if (freerw) { + SDL_RWclose(rw); + } + return NULL; + } + + MIDItoStream(newsong, evntlist); + + FreeMIDIEventList(evntlist); + + if (freerw) { + SDL_RWclose(rw); + } + return newsong; } void native_midi_freesong(NativeMidiSong *song) diff --git a/playmus.c b/playmus.c index 44fe8192..7af7445f 100644 --- a/playmus.c +++ b/playmus.c @@ -43,197 +43,197 @@ static int next_track = 0; void CleanUp(int exitcode) { - if( Mix_PlayingMusic() ) { - Mix_FadeOutMusic(1500); - SDL_Delay(1500); - } - if ( music ) { - Mix_FreeMusic(music); - music = NULL; - } - if ( audio_open ) { - Mix_CloseAudio(); - audio_open = 0; - } - SDL_Quit(); - exit(exitcode); + if( Mix_PlayingMusic() ) { + Mix_FadeOutMusic(1500); + SDL_Delay(1500); + } + if ( music ) { + Mix_FreeMusic(music); + music = NULL; + } + if ( audio_open ) { + Mix_CloseAudio(); + audio_open = 0; + } + SDL_Quit(); + exit(exitcode); } void Usage(char *argv0) { - fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] [-rwops] \n", argv0); + fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] [-rwops] \n", argv0); } void Menu(void) { - char buf[10]; - - printf("Available commands: (p)ause (r)esume (h)alt volume(v#) > "); - fflush(stdin); - if (scanf("%s",buf) == 1) { - switch(buf[0]){ - case 'p': case 'P': - Mix_PauseMusic(); - break; - case 'r': case 'R': - Mix_ResumeMusic(); - break; - case 'h': case 'H': - Mix_HaltMusic(); - break; - case 'v': case 'V': - Mix_VolumeMusic(atoi(buf+1)); - break; - } - } - printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", - Mix_PausedMusic() ? "yes" : "no"); + char buf[10]; + + printf("Available commands: (p)ause (r)esume (h)alt volume(v#) > "); + fflush(stdin); + if (scanf("%s",buf) == 1) { + switch(buf[0]){ + case 'p': case 'P': + Mix_PauseMusic(); + break; + case 'r': case 'R': + Mix_ResumeMusic(); + break; + case 'h': case 'H': + Mix_HaltMusic(); + break; + case 'v': case 'V': + Mix_VolumeMusic(atoi(buf+1)); + break; + } + } + printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", + Mix_PausedMusic() ? "yes" : "no"); } #ifdef HAVE_SIGNAL_H void IntHandler(int sig) { - switch (sig) { - case SIGINT: - next_track++; - break; - } + switch (sig) { + case SIGINT: + next_track++; + break; + } } #endif int main(int argc, char *argv[]) { - SDL_RWops *rwfp = NULL; - int audio_rate; - Uint16 audio_format; - int audio_channels; - int audio_buffers; - int audio_volume = MIX_MAX_VOLUME; - int looping = 0; - int interactive = 0; - int rwops = 0; - int i; - - /* Initialize variables */ - audio_rate = 22050; - audio_format = AUDIO_S16; - audio_channels = 2; - audio_buffers = 4096; - - /* Check command line usage */ - for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) { - if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) { - ++i; - audio_rate = atoi(argv[i]); - } else - if ( strcmp(argv[i], "-m") == 0 ) { - audio_channels = 1; - } else - if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) { - ++i; - audio_channels = atoi(argv[i]); - } else - if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) { - ++i; - audio_buffers = atoi(argv[i]); - } else - if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) { - ++i; - audio_volume = atoi(argv[i]); - } else - if ( strcmp(argv[i], "-l") == 0 ) { - looping = -1; - } else - if ( strcmp(argv[i], "-i") == 0 ) { - interactive = 1; - } else - if ( strcmp(argv[i], "-8") == 0 ) { - audio_format = AUDIO_U8; - } else - if ( strcmp(argv[i], "-rwops") == 0 ) { - rwops = 1; - } else { - Usage(argv[0]); - return(1); - } - } - if ( ! argv[i] ) { - Usage(argv[0]); - return(1); - } - - /* Initialize the SDL library */ - if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) { - fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); - return(255); - } + SDL_RWops *rwfp = NULL; + int audio_rate; + Uint16 audio_format; + int audio_channels; + int audio_buffers; + int audio_volume = MIX_MAX_VOLUME; + int looping = 0; + int interactive = 0; + int rwops = 0; + int i; + + /* Initialize variables */ + audio_rate = 22050; + audio_format = AUDIO_S16; + audio_channels = 2; + audio_buffers = 4096; + + /* Check command line usage */ + for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) { + if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) { + ++i; + audio_rate = atoi(argv[i]); + } else + if ( strcmp(argv[i], "-m") == 0 ) { + audio_channels = 1; + } else + if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) { + ++i; + audio_channels = atoi(argv[i]); + } else + if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) { + ++i; + audio_buffers = atoi(argv[i]); + } else + if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) { + ++i; + audio_volume = atoi(argv[i]); + } else + if ( strcmp(argv[i], "-l") == 0 ) { + looping = -1; + } else + if ( strcmp(argv[i], "-i") == 0 ) { + interactive = 1; + } else + if ( strcmp(argv[i], "-8") == 0 ) { + audio_format = AUDIO_U8; + } else + if ( strcmp(argv[i], "-rwops") == 0 ) { + rwops = 1; + } else { + Usage(argv[0]); + return(1); + } + } + if ( ! argv[i] ) { + Usage(argv[0]); + return(1); + } + + /* Initialize the SDL library */ + if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) { + fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); + return(255); + } #ifdef HAVE_SIGNAL_H - signal(SIGINT, IntHandler); - signal(SIGTERM, CleanUp); + signal(SIGINT, IntHandler); + signal(SIGTERM, CleanUp); #endif - /* Open the audio device */ - if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) { - fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); - return(2); - } else { - Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); - printf("Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer\n", audio_rate, - (audio_format&0xFF), - (audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono", - (audio_format&0x1000) ? "BE" : "LE", - audio_buffers ); - } - audio_open = 1; - - /* Set the music volume */ - Mix_VolumeMusic(audio_volume); - - /* Set the external music player, if any */ - Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD")); - - while (argv[i]) { - next_track = 0; - - /* Load the requested music file */ - if ( rwops ) { - rwfp = SDL_RWFromFile(argv[i], "rb"); - music = Mix_LoadMUS_RW(rwfp); - } else { - music = Mix_LoadMUS(argv[i]); - } - if ( music == NULL ) { - fprintf(stderr, "Couldn't load %s: %s\n", - argv[i], SDL_GetError()); - CleanUp(2); - } - - /* Play and then exit */ - printf("Playing %s\n", argv[i]); - Mix_FadeInMusic(music,looping,2000); - while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) { - if(interactive) - Menu(); - else - SDL_Delay(100); - } - Mix_FreeMusic(music); - if ( rwops ) { - SDL_RWclose(rwfp); - } - music = NULL; - - /* If the user presses Ctrl-C more than once, exit. */ - SDL_Delay(500); - if ( next_track > 1 ) break; - - i++; - } - CleanUp(0); - - /* Not reached, but fixes compiler warnings */ - return 0; + /* Open the audio device */ + if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) { + fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); + return(2); + } else { + Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); + printf("Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer\n", audio_rate, + (audio_format&0xFF), + (audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono", + (audio_format&0x1000) ? "BE" : "LE", + audio_buffers ); + } + audio_open = 1; + + /* Set the music volume */ + Mix_VolumeMusic(audio_volume); + + /* Set the external music player, if any */ + Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD")); + + while (argv[i]) { + next_track = 0; + + /* Load the requested music file */ + if ( rwops ) { + rwfp = SDL_RWFromFile(argv[i], "rb"); + music = Mix_LoadMUS_RW(rwfp); + } else { + music = Mix_LoadMUS(argv[i]); + } + if ( music == NULL ) { + fprintf(stderr, "Couldn't load %s: %s\n", + argv[i], SDL_GetError()); + CleanUp(2); + } + + /* Play and then exit */ + printf("Playing %s\n", argv[i]); + Mix_FadeInMusic(music,looping,2000); + while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) { + if(interactive) + Menu(); + else + SDL_Delay(100); + } + Mix_FreeMusic(music); + if ( rwops ) { + SDL_RWclose(rwfp); + } + music = NULL; + + /* If the user presses Ctrl-C more than once, exit. */ + SDL_Delay(500); + if ( next_track > 1 ) break; + + i++; + } + CleanUp(0); + + /* Not reached, but fixes compiler warnings */ + return 0; } diff --git a/playwave.c b/playwave.c index 3cdf6deb..9b652d44 100644 --- a/playwave.c +++ b/playwave.c @@ -65,19 +65,19 @@ static void output_test_warnings(void) { #if (defined TEST_MIX_CHANNELFINISHED) - fprintf(stderr, "Warning: TEST_MIX_CHANNELFINISHED is enabled in this binary...\n"); + fprintf(stderr, "Warning: TEST_MIX_CHANNELFINISHED is enabled in this binary...\n"); #endif #if (defined TEST_MIX_PANNING) - fprintf(stderr, "Warning: TEST_MIX_PANNING is enabled in this binary...\n"); + fprintf(stderr, "Warning: TEST_MIX_PANNING is enabled in this binary...\n"); #endif #if (defined TEST_MIX_VERSIONS) - fprintf(stderr, "Warning: TEST_MIX_VERSIONS is enabled in this binary...\n"); + fprintf(stderr, "Warning: TEST_MIX_VERSIONS is enabled in this binary...\n"); #endif #if (defined TEST_MIX_DISTANCE) - fprintf(stderr, "Warning: TEST_MIX_DISTANCE is enabled in this binary...\n"); + fprintf(stderr, "Warning: TEST_MIX_DISTANCE is enabled in this binary...\n"); #endif #if (defined TEST_MIX_POSITION) - fprintf(stderr, "Warning: TEST_MIX_POSITION is enabled in this binary...\n"); + fprintf(stderr, "Warning: TEST_MIX_POSITION is enabled in this binary...\n"); #endif } @@ -89,45 +89,45 @@ static Mix_Chunk *wave = NULL; #if (defined TEST_MIX_DECODERS) static void report_decoders(void) { - int i, total; + int i, total; printf("Supported decoders...\n"); - total = Mix_GetNumChunkDecoders(); - for (i = 0; i < total; i++) { - fprintf(stderr, " - chunk decoder: %s\n", Mix_GetChunkDecoder(i)); - } - - total = Mix_GetNumMusicDecoders(); - for (i = 0; i < total; i++) { - fprintf(stderr, " - music decoder: %s\n", Mix_GetMusicDecoder(i)); - } + total = Mix_GetNumChunkDecoders(); + for (i = 0; i < total; i++) { + fprintf(stderr, " - chunk decoder: %s\n", Mix_GetChunkDecoder(i)); + } + + total = Mix_GetNumMusicDecoders(); + for (i = 0; i < total; i++) { + fprintf(stderr, " - music decoder: %s\n", Mix_GetMusicDecoder(i)); + } } #endif /* rcg06192001 Check new Mixer version API. */ #if (defined TEST_MIX_VERSIONS) static void output_versions(const char *libname, const SDL_version *compiled, - const SDL_version *linked) + const SDL_version *linked) { - fprintf(stderr, - "This program was compiled against %s %d.%d.%d,\n" - " and is dynamically linked to %d.%d.%d.\n", libname, - compiled->major, compiled->minor, compiled->patch, - linked->major, linked->minor, linked->patch); + fprintf(stderr, + "This program was compiled against %s %d.%d.%d,\n" + " and is dynamically linked to %d.%d.%d.\n", libname, + compiled->major, compiled->minor, compiled->patch, + linked->major, linked->minor, linked->patch); } static void test_versions(void) { - SDL_version compiled; - const SDL_version *linked; + SDL_version compiled; + const SDL_version *linked; - SDL_VERSION(&compiled); - linked = SDL_Linked_Version(); - output_versions("SDL", &compiled, linked); + SDL_VERSION(&compiled); + linked = SDL_Linked_Version(); + output_versions("SDL", &compiled, linked); - SDL_MIXER_VERSION(&compiled); - linked = Mix_Linked_Version(); - output_versions("SDL_mixer", &compiled, linked); + SDL_MIXER_VERSION(&compiled); + linked = Mix_Linked_Version(); + output_versions("SDL_mixer", &compiled, linked); } #endif @@ -136,11 +136,11 @@ static void test_versions(void) static volatile int channel_is_done = 0; static void channel_complete_callback(int chan) { - Mix_Chunk *done_chunk = Mix_GetChunk(chan); - fprintf(stderr, "We were just alerted that Mixer channel #%d is done.\n", chan); - fprintf(stderr, "Channel's chunk pointer is (%p).\n", done_chunk); - fprintf(stderr, " Which %s correct.\n", (wave == done_chunk) ? "is" : "is NOT"); - channel_is_done = 1; + Mix_Chunk *done_chunk = Mix_GetChunk(chan); + fprintf(stderr, "We were just alerted that Mixer channel #%d is done.\n", chan); + fprintf(stderr, "Channel's chunk pointer is (%p).\n", done_chunk); + fprintf(stderr, " Which %s correct.\n", (wave == done_chunk) ? "is" : "is NOT"); + channel_is_done = 1; } #endif @@ -149,9 +149,9 @@ static void channel_complete_callback(int chan) static int still_playing(void) { #ifdef TEST_MIX_CHANNELFINISHED - return(!channel_is_done); + return(!channel_is_done); #else - return(Mix_Playing(0)); + return(Mix_Playing(0)); #endif } @@ -159,37 +159,37 @@ static int still_playing(void) #if (defined TEST_MIX_PANNING) static void do_panning_update(void) { - static Uint8 leftvol = 128; - static Uint8 rightvol = 128; - static Uint8 leftincr = -1; - static Uint8 rightincr = 1; - static int panningok = 1; - static Uint32 next_panning_update = 0; - - if ((panningok) && (SDL_GetTicks() >= next_panning_update)) { - panningok = Mix_SetPanning(0, leftvol, rightvol); - if (!panningok) { - fprintf(stderr, "Mix_SetPanning(0, %d, %d) failed!\n", - (int) leftvol, (int) rightvol); - fprintf(stderr, "Reason: [%s].\n", Mix_GetError()); - } - - if ((leftvol == 255) || (leftvol == 0)) { - if (leftvol == 255) - printf("All the way in the left speaker.\n"); - leftincr *= -1; - } - - if ((rightvol == 255) || (rightvol == 0)) { - if (rightvol == 255) - printf("All the way in the right speaker.\n"); - rightincr *= -1; - } - - leftvol += leftincr; - rightvol += rightincr; - next_panning_update = SDL_GetTicks() + 10; - } + static Uint8 leftvol = 128; + static Uint8 rightvol = 128; + static Uint8 leftincr = -1; + static Uint8 rightincr = 1; + static int panningok = 1; + static Uint32 next_panning_update = 0; + + if ((panningok) && (SDL_GetTicks() >= next_panning_update)) { + panningok = Mix_SetPanning(0, leftvol, rightvol); + if (!panningok) { + fprintf(stderr, "Mix_SetPanning(0, %d, %d) failed!\n", + (int) leftvol, (int) rightvol); + fprintf(stderr, "Reason: [%s].\n", Mix_GetError()); + } + + if ((leftvol == 255) || (leftvol == 0)) { + if (leftvol == 255) + printf("All the way in the left speaker.\n"); + leftincr *= -1; + } + + if ((rightvol == 255) || (rightvol == 0)) { + if (rightvol == 255) + printf("All the way in the right speaker.\n"); + rightincr *= -1; + } + + leftvol += leftincr; + rightvol += rightincr; + next_panning_update = SDL_GetTicks() + 10; + } } #endif @@ -197,30 +197,30 @@ static void do_panning_update(void) #if (defined TEST_MIX_DISTANCE) static void do_distance_update(void) { - static Uint8 distance = 1; - static Uint8 distincr = 1; - static int distanceok = 1; - static Uint32 next_distance_update = 0; - - if ((distanceok) && (SDL_GetTicks() >= next_distance_update)) { - distanceok = Mix_SetDistance(0, distance); - if (!distanceok) { - fprintf(stderr, "Mix_SetDistance(0, %d) failed!\n", (int) distance); - fprintf(stderr, "Reason: [%s].\n", Mix_GetError()); - } - - if (distance == 0) { - printf("Distance at nearest point.\n"); - distincr *= -1; - } - else if (distance == 255) { - printf("Distance at furthest point.\n"); - distincr *= -1; - } - - distance += distincr; - next_distance_update = SDL_GetTicks() + 15; - } + static Uint8 distance = 1; + static Uint8 distincr = 1; + static int distanceok = 1; + static Uint32 next_distance_update = 0; + + if ((distanceok) && (SDL_GetTicks() >= next_distance_update)) { + distanceok = Mix_SetDistance(0, distance); + if (!distanceok) { + fprintf(stderr, "Mix_SetDistance(0, %d) failed!\n", (int) distance); + fprintf(stderr, "Reason: [%s].\n", Mix_GetError()); + } + + if (distance == 0) { + printf("Distance at nearest point.\n"); + distincr *= -1; + } + else if (distance == 255) { + printf("Distance at furthest point.\n"); + distincr *= -1; + } + + distance += distincr; + next_distance_update = SDL_GetTicks() + 15; + } } #endif @@ -228,69 +228,69 @@ static void do_distance_update(void) #if (defined TEST_MIX_POSITION) static void do_position_update(void) { - static Sint16 distance = 1; - static Sint8 distincr = 1; - static Uint16 angle = 0; - static Sint8 angleincr = 1; - static int positionok = 1; - static Uint32 next_position_update = 0; - - if ((positionok) && (SDL_GetTicks() >= next_position_update)) { - positionok = Mix_SetPosition(0, angle, distance); - if (!positionok) { - fprintf(stderr, "Mix_SetPosition(0, %d, %d) failed!\n", - (int) angle, (int) distance); - fprintf(stderr, "Reason: [%s].\n", Mix_GetError()); - } - - if (angle == 0) { - printf("Due north; now rotating clockwise...\n"); - angleincr = 1; - } - - else if (angle == 360) { - printf("Due north; now rotating counter-clockwise...\n"); - angleincr = -1; - } - - distance += distincr; - - if (distance < 0) { - distance = 0; - distincr = 3; - printf("Distance is very, very near. Stepping away by threes...\n"); - } else if (distance > 255) { - distance = 255; - distincr = -3; - printf("Distance is very, very far. Stepping towards by threes...\n"); - } - - angle += angleincr; - next_position_update = SDL_GetTicks() + 30; - } + static Sint16 distance = 1; + static Sint8 distincr = 1; + static Uint16 angle = 0; + static Sint8 angleincr = 1; + static int positionok = 1; + static Uint32 next_position_update = 0; + + if ((positionok) && (SDL_GetTicks() >= next_position_update)) { + positionok = Mix_SetPosition(0, angle, distance); + if (!positionok) { + fprintf(stderr, "Mix_SetPosition(0, %d, %d) failed!\n", + (int) angle, (int) distance); + fprintf(stderr, "Reason: [%s].\n", Mix_GetError()); + } + + if (angle == 0) { + printf("Due north; now rotating clockwise...\n"); + angleincr = 1; + } + + else if (angle == 360) { + printf("Due north; now rotating counter-clockwise...\n"); + angleincr = -1; + } + + distance += distincr; + + if (distance < 0) { + distance = 0; + distincr = 3; + printf("Distance is very, very near. Stepping away by threes...\n"); + } else if (distance > 255) { + distance = 255; + distincr = -3; + printf("Distance is very, very far. Stepping towards by threes...\n"); + } + + angle += angleincr; + next_position_update = SDL_GetTicks() + 30; + } } #endif static void CleanUp(int exitcode) { - if ( wave ) { - Mix_FreeChunk(wave); - wave = NULL; - } - if ( audio_open ) { - Mix_CloseAudio(); - audio_open = 0; - } - SDL_Quit(); - - exit(exitcode); + if ( wave ) { + Mix_FreeChunk(wave); + wave = NULL; + } + if ( audio_open ) { + Mix_CloseAudio(); + audio_open = 0; + } + SDL_Quit(); + + exit(exitcode); } static void Usage(char *argv0) { - fprintf(stderr, "Usage: %s [-8] [-r rate] [-c channels] [-f] [-F] [-l] [-m] \n", argv0); + fprintf(stderr, "Usage: %s [-8] [-r rate] [-c channels] [-f] [-F] [-l] [-m] \n", argv0); } @@ -308,193 +308,193 @@ static void Usage(char *argv0) */ static void flip_sample(Mix_Chunk *wave) { - Uint16 format; - int channels, i, incr; - Uint8 *start = wave->abuf; - Uint8 *end = wave->abuf + wave->alen; - - Mix_QuerySpec(NULL, &format, &channels); - incr = (format & 0xFF) * channels; - - end -= incr; - - switch (incr) { - case 8: - for (i = wave->alen / 2; i >= 0; i -= 1) { - Uint8 tmp = *start; - *start = *end; - *end = tmp; - start++; - end--; - } - break; - - case 16: - for (i = wave->alen / 2; i >= 0; i -= 2) { - Uint16 tmp = *start; - *((Uint16 *) start) = *((Uint16 *) end); - *((Uint16 *) end) = tmp; - start += 2; - end -= 2; - } - break; - - case 32: - for (i = wave->alen / 2; i >= 0; i -= 4) { - Uint32 tmp = *start; - *((Uint32 *) start) = *((Uint32 *) end); - *((Uint32 *) end) = tmp; - start += 4; - end -= 4; - } - break; - - default: - fprintf(stderr, "Unhandled format in sample flipping.\n"); - return; - } + Uint16 format; + int channels, i, incr; + Uint8 *start = wave->abuf; + Uint8 *end = wave->abuf + wave->alen; + + Mix_QuerySpec(NULL, &format, &channels); + incr = (format & 0xFF) * channels; + + end -= incr; + + switch (incr) { + case 8: + for (i = wave->alen / 2; i >= 0; i -= 1) { + Uint8 tmp = *start; + *start = *end; + *end = tmp; + start++; + end--; + } + break; + + case 16: + for (i = wave->alen / 2; i >= 0; i -= 2) { + Uint16 tmp = *start; + *((Uint16 *) start) = *((Uint16 *) end); + *((Uint16 *) end) = tmp; + start += 2; + end -= 2; + } + break; + + case 32: + for (i = wave->alen / 2; i >= 0; i -= 4) { + Uint32 tmp = *start; + *((Uint32 *) start) = *((Uint32 *) end); + *((Uint32 *) end) = tmp; + start += 4; + end -= 4; + } + break; + + default: + fprintf(stderr, "Unhandled format in sample flipping.\n"); + return; + } } int main(int argc, char *argv[]) { - int audio_rate; - Uint16 audio_format; - int audio_channels; - int loops = 0; - int i; - int reverse_stereo = 0; - int reverse_sample = 0; + int audio_rate; + Uint16 audio_format; + int audio_channels; + int loops = 0; + int i; + int reverse_stereo = 0; + int reverse_sample = 0; #ifdef HAVE_SETBUF - setbuf(stdout, NULL); /* rcg06132001 for debugging purposes. */ - setbuf(stderr, NULL); /* rcg06192001 for debugging purposes, too. */ + setbuf(stdout, NULL); /* rcg06132001 for debugging purposes. */ + setbuf(stderr, NULL); /* rcg06192001 for debugging purposes, too. */ #endif - output_test_warnings(); - - /* Initialize variables */ - audio_rate = MIX_DEFAULT_FREQUENCY; - audio_format = MIX_DEFAULT_FORMAT; - audio_channels = 2; - - /* Check command line usage */ - for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) { - if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) { - ++i; - audio_rate = atoi(argv[i]); - } else - if ( strcmp(argv[i], "-m") == 0 ) { - audio_channels = 1; - } else - if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) { - ++i; - audio_channels = atoi(argv[i]); - } else - if ( strcmp(argv[i], "-l") == 0 ) { - loops = -1; - } else - if ( strcmp(argv[i], "-8") == 0 ) { - audio_format = AUDIO_U8; - } else - if ( strcmp(argv[i], "-f") == 0 ) { /* rcg06122001 flip stereo */ - reverse_stereo = 1; - } else - if ( strcmp(argv[i], "-F") == 0 ) { /* rcg06172001 flip sample */ - reverse_sample = 1; - } else { - Usage(argv[0]); - return(1); - } - } - if ( ! argv[i] ) { - Usage(argv[0]); - return(1); - } - - /* Initialize the SDL library */ - if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) { - fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); - return(255); - } + output_test_warnings(); + + /* Initialize variables */ + audio_rate = MIX_DEFAULT_FREQUENCY; + audio_format = MIX_DEFAULT_FORMAT; + audio_channels = 2; + + /* Check command line usage */ + for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) { + if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) { + ++i; + audio_rate = atoi(argv[i]); + } else + if ( strcmp(argv[i], "-m") == 0 ) { + audio_channels = 1; + } else + if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) { + ++i; + audio_channels = atoi(argv[i]); + } else + if ( strcmp(argv[i], "-l") == 0 ) { + loops = -1; + } else + if ( strcmp(argv[i], "-8") == 0 ) { + audio_format = AUDIO_U8; + } else + if ( strcmp(argv[i], "-f") == 0 ) { /* rcg06122001 flip stereo */ + reverse_stereo = 1; + } else + if ( strcmp(argv[i], "-F") == 0 ) { /* rcg06172001 flip sample */ + reverse_sample = 1; + } else { + Usage(argv[0]); + return(1); + } + } + if ( ! argv[i] ) { + Usage(argv[0]); + return(1); + } + + /* Initialize the SDL library */ + if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) { + fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); + return(255); + } #ifdef HAVE_SIGNAL_H - signal(SIGINT, CleanUp); - signal(SIGTERM, CleanUp); + signal(SIGINT, CleanUp); + signal(SIGTERM, CleanUp); #endif - /* Open the audio device */ - if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) { - fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); - CleanUp(2); - } else { - Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); - printf("Opened audio at %d Hz %d bit %s", audio_rate, - (audio_format&0xFF), - (audio_channels > 2) ? "surround" : - (audio_channels > 1) ? "stereo" : "mono"); - if ( loops ) { - printf(" (looping)\n"); - } else { - putchar('\n'); - } - } - audio_open = 1; + /* Open the audio device */ + if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) { + fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); + CleanUp(2); + } else { + Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); + printf("Opened audio at %d Hz %d bit %s", audio_rate, + (audio_format&0xFF), + (audio_channels > 2) ? "surround" : + (audio_channels > 1) ? "stereo" : "mono"); + if ( loops ) { + printf(" (looping)\n"); + } else { + putchar('\n'); + } + } + audio_open = 1; #if (defined TEST_MIX_VERSIONS) - test_versions(); + test_versions(); #endif #if (defined TEST_MIX_DECODERS) - report_decoders(); + report_decoders(); #endif - /* Load the requested wave file */ - wave = Mix_LoadWAV(argv[i]); - if ( wave == NULL ) { - fprintf(stderr, "Couldn't load %s: %s\n", - argv[i], SDL_GetError()); - CleanUp(2); - } + /* Load the requested wave file */ + wave = Mix_LoadWAV(argv[i]); + if ( wave == NULL ) { + fprintf(stderr, "Couldn't load %s: %s\n", + argv[i], SDL_GetError()); + CleanUp(2); + } - if (reverse_sample) { - flip_sample(wave); - } + if (reverse_sample) { + flip_sample(wave); + } #ifdef TEST_MIX_CHANNELFINISHED /* rcg06072001 */ - Mix_ChannelFinished(channel_complete_callback); + Mix_ChannelFinished(channel_complete_callback); #endif - if ( (!Mix_SetReverseStereo(MIX_CHANNEL_POST, reverse_stereo)) && - (reverse_stereo) ) - { - printf("Failed to set up reverse stereo effect!\n"); - printf("Reason: [%s].\n", Mix_GetError()); - } + if ( (!Mix_SetReverseStereo(MIX_CHANNEL_POST, reverse_stereo)) && + (reverse_stereo) ) + { + printf("Failed to set up reverse stereo effect!\n"); + printf("Reason: [%s].\n", Mix_GetError()); + } - /* Play and then exit */ - Mix_PlayChannel(0, wave, loops); + /* Play and then exit */ + Mix_PlayChannel(0, wave, loops); - while (still_playing()) { + while (still_playing()) { #if (defined TEST_MIX_PANNING) /* rcg06132001 */ - do_panning_update(); + do_panning_update(); #endif #if (defined TEST_MIX_DISTANCE) /* rcg06192001 */ - do_distance_update(); + do_distance_update(); #endif #if (defined TEST_MIX_POSITION) /* rcg06202001 */ - do_position_update(); + do_position_update(); #endif - SDL_Delay(1); + SDL_Delay(1); - } /* while still_playing() loop... */ + } /* while still_playing() loop... */ - CleanUp(0); + CleanUp(0); - /* Not reached, but fixes compiler warnings */ - return 0; + /* Not reached, but fixes compiler warnings */ + return 0; } /* end of playwave.c ... */ diff --git a/wavestream.c b/wavestream.c index 4386a33b..38c79865 100644 --- a/wavestream.c +++ b/wavestream.c @@ -45,45 +45,45 @@ /*******************************************/ /* Define values for Microsoft WAVE format */ /*******************************************/ -#define RIFF 0x46464952 /* "RIFF" */ -#define WAVE 0x45564157 /* "WAVE" */ -#define FACT 0x74636166 /* "fact" */ -#define LIST 0x5453494c /* "LIST" */ -#define FMT 0x20746D66 /* "fmt " */ -#define DATA 0x61746164 /* "data" */ -#define PCM_CODE 1 -#define ADPCM_CODE 2 -#define WAVE_MONO 1 -#define WAVE_STEREO 2 +#define RIFF 0x46464952 /* "RIFF" */ +#define WAVE 0x45564157 /* "WAVE" */ +#define FACT 0x74636166 /* "fact" */ +#define LIST 0x5453494c /* "LIST" */ +#define FMT 0x20746D66 /* "fmt " */ +#define DATA 0x61746164 /* "data" */ +#define PCM_CODE 1 +#define ADPCM_CODE 2 +#define WAVE_MONO 1 +#define WAVE_STEREO 2 /* Normally, these three chunks come consecutively in a WAVE file */ typedef struct WaveFMT { /* Not saved in the chunk we read: - Uint32 FMTchunk; - Uint32 fmtlen; + Uint32 FMTchunk; + Uint32 fmtlen; */ - Uint16 encoding; - Uint16 channels; /* 1 = mono, 2 = stereo */ - Uint32 frequency; /* One of 11025, 22050, or 44100 Hz */ - Uint32 byterate; /* Average bytes per second */ - Uint16 blockalign; /* Bytes per sample block */ - Uint16 bitspersample; /* One of 8, 12, 16, or 4 for ADPCM */ + Uint16 encoding; + Uint16 channels; /* 1 = mono, 2 = stereo */ + Uint32 frequency; /* One of 11025, 22050, or 44100 Hz */ + Uint32 byterate; /* Average bytes per second */ + Uint16 blockalign; /* Bytes per sample block */ + Uint16 bitspersample; /* One of 8, 12, 16, or 4 for ADPCM */ } WaveFMT; /* The general chunk found in the WAVE file */ typedef struct Chunk { - Uint32 magic; - Uint32 length; - Uint8 *data; /* Data includes magic and length */ + Uint32 magic; + Uint32 length; + Uint8 *data; /* Data includes magic and length */ } Chunk; /*********************************************/ /* Define values for AIFF (IFF audio) format */ /*********************************************/ -#define FORM 0x4d524f46 /* "FORM" */ -#define AIFF 0x46464941 /* "AIFF" */ -#define SSND 0x444e5353 /* "SSND" */ -#define COMM 0x4d4d4f43 /* "COMM" */ +#define FORM 0x4d524f46 /* "FORM" */ +#define AIFF 0x46464941 /* "AIFF" */ +#define SSND 0x444e5353 /* "SSND" */ +#define COMM 0x4d4d4f43 /* "COMM" */ /* Currently we only support a single stream at a time */ @@ -95,286 +95,286 @@ static int wavestream_volume = MIX_MAX_VOLUME; /* Function to load the WAV/AIFF stream */ static SDL_RWops *LoadWAVStream (SDL_RWops *rw, SDL_AudioSpec *spec, - long *start, long *stop); + long *start, long *stop); static SDL_RWops *LoadAIFFStream (SDL_RWops *rw, SDL_AudioSpec *spec, - long *start, long *stop); + long *start, long *stop); /* Initialize the WAVStream player, with the given mixer settings This function returns 0, or -1 if there was an error. */ int WAVStream_Init(SDL_AudioSpec *mixerfmt) { - mixer = *mixerfmt; - return(0); + mixer = *mixerfmt; + return(0); } void WAVStream_SetVolume(int volume) { - wavestream_volume = volume; + wavestream_volume = volume; } /* Load a WAV stream from the given RWops object */ WAVStream *WAVStream_LoadSong_RW(SDL_RWops *rw, const char *magic, int freerw) { - WAVStream *wave; - SDL_AudioSpec wavespec; - - if ( ! mixer.format ) { - Mix_SetError("WAV music output not started"); - if ( freerw ) { - SDL_RWclose(rw); - } - return(NULL); - } - wave = (WAVStream *)SDL_malloc(sizeof *wave); - if ( wave ) { - memset(wave, 0, (sizeof *wave)); - wave->freerw = freerw; - if ( strcmp(magic, "RIFF") == 0 ) { - wave->rw = LoadWAVStream(rw, &wavespec, - &wave->start, &wave->stop); - } else - if ( strcmp(magic, "FORM") == 0 ) { - wave->rw = LoadAIFFStream(rw, &wavespec, - &wave->start, &wave->stop); - } else { - Mix_SetError("Unknown WAVE format"); - } - if ( wave->rw == NULL ) { - SDL_free(wave); - if ( freerw ) { - SDL_RWclose(rw); - } - return(NULL); - } - SDL_BuildAudioCVT(&wave->cvt, - wavespec.format, wavespec.channels, wavespec.freq, - mixer.format, mixer.channels, mixer.freq); - } else { - SDL_OutOfMemory(); - if ( freerw ) { - SDL_RWclose(rw); - } - return(NULL); - } - return(wave); + WAVStream *wave; + SDL_AudioSpec wavespec; + + if ( ! mixer.format ) { + Mix_SetError("WAV music output not started"); + if ( freerw ) { + SDL_RWclose(rw); + } + return(NULL); + } + wave = (WAVStream *)SDL_malloc(sizeof *wave); + if ( wave ) { + memset(wave, 0, (sizeof *wave)); + wave->freerw = freerw; + if ( strcmp(magic, "RIFF") == 0 ) { + wave->rw = LoadWAVStream(rw, &wavespec, + &wave->start, &wave->stop); + } else + if ( strcmp(magic, "FORM") == 0 ) { + wave->rw = LoadAIFFStream(rw, &wavespec, + &wave->start, &wave->stop); + } else { + Mix_SetError("Unknown WAVE format"); + } + if ( wave->rw == NULL ) { + SDL_free(wave); + if ( freerw ) { + SDL_RWclose(rw); + } + return(NULL); + } + SDL_BuildAudioCVT(&wave->cvt, + wavespec.format, wavespec.channels, wavespec.freq, + mixer.format, mixer.channels, mixer.freq); + } else { + SDL_OutOfMemory(); + if ( freerw ) { + SDL_RWclose(rw); + } + return(NULL); + } + return(wave); } /* Start playback of a given WAV stream */ void WAVStream_Start(WAVStream *wave) { - SDL_RWseek (wave->rw, wave->start, RW_SEEK_SET); - music = wave; + SDL_RWseek (wave->rw, wave->start, RW_SEEK_SET); + music = wave; } /* Play some of a stream previously started with WAVStream_Start() */ int WAVStream_PlaySome(Uint8 *stream, int len) { - long pos; - int left = 0; - - if ( music && ((pos=SDL_RWtell(music->rw)) < music->stop) ) { - if ( music->cvt.needed ) { - int original_len; - - original_len=(int)((double)len/music->cvt.len_ratio); - if ( music->cvt.len != original_len ) { - int worksize; - if ( music->cvt.buf != NULL ) { - SDL_free(music->cvt.buf); - } - worksize = original_len*music->cvt.len_mult; - music->cvt.buf=(Uint8 *)SDL_malloc(worksize); - if ( music->cvt.buf == NULL ) { - return 0; - } - music->cvt.len = original_len; - } - if ( (music->stop - pos) < original_len ) { - left = (original_len - (music->stop - pos)); - original_len -= left; - left = (int)((double)left*music->cvt.len_ratio); - } - original_len = SDL_RWread(music->rw, music->cvt.buf,1,original_len); - /* At least at the time of writing, SDL_ConvertAudio() - does byte-order swapping starting at the end of the - buffer. Thus, if we are reading 16-bit samples, we - had better make damn sure that we get an even - number of bytes, or we'll get garbage. - */ - if ( (music->cvt.src_format & 0x0010) && (original_len & 1) ) { - original_len--; - } - music->cvt.len = original_len; - SDL_ConvertAudio(&music->cvt); - SDL_MixAudio(stream, music->cvt.buf, music->cvt.len_cvt, wavestream_volume); - } else { - Uint8 *data; - if ( (music->stop - pos) < len ) { - left = (len - (music->stop - pos)); - len -= left; - } - data = SDL_stack_alloc(Uint8, len); - if (data) - { - SDL_RWread(music->rw, data, len, 1); - SDL_MixAudio(stream, data, len, wavestream_volume); - SDL_stack_free(data); - } - } - } - return left; + long pos; + int left = 0; + + if ( music && ((pos=SDL_RWtell(music->rw)) < music->stop) ) { + if ( music->cvt.needed ) { + int original_len; + + original_len=(int)((double)len/music->cvt.len_ratio); + if ( music->cvt.len != original_len ) { + int worksize; + if ( music->cvt.buf != NULL ) { + SDL_free(music->cvt.buf); + } + worksize = original_len*music->cvt.len_mult; + music->cvt.buf=(Uint8 *)SDL_malloc(worksize); + if ( music->cvt.buf == NULL ) { + return 0; + } + music->cvt.len = original_len; + } + if ( (music->stop - pos) < original_len ) { + left = (original_len - (music->stop - pos)); + original_len -= left; + left = (int)((double)left*music->cvt.len_ratio); + } + original_len = SDL_RWread(music->rw, music->cvt.buf,1,original_len); + /* At least at the time of writing, SDL_ConvertAudio() + does byte-order swapping starting at the end of the + buffer. Thus, if we are reading 16-bit samples, we + had better make damn sure that we get an even + number of bytes, or we'll get garbage. + */ + if ( (music->cvt.src_format & 0x0010) && (original_len & 1) ) { + original_len--; + } + music->cvt.len = original_len; + SDL_ConvertAudio(&music->cvt); + SDL_MixAudio(stream, music->cvt.buf, music->cvt.len_cvt, wavestream_volume); + } else { + Uint8 *data; + if ( (music->stop - pos) < len ) { + left = (len - (music->stop - pos)); + len -= left; + } + data = SDL_stack_alloc(Uint8, len); + if (data) + { + SDL_RWread(music->rw, data, len, 1); + SDL_MixAudio(stream, data, len, wavestream_volume); + SDL_stack_free(data); + } + } + } + return left; } /* Stop playback of a stream previously started with WAVStream_Start() */ void WAVStream_Stop(void) { - music = NULL; + music = NULL; } /* Close the given WAV stream */ void WAVStream_FreeSong(WAVStream *wave) { - if ( wave ) { - /* Clean up associated data */ - if ( wave->cvt.buf ) { - SDL_free(wave->cvt.buf); - } - if ( wave->freerw ) { - SDL_RWclose(wave->rw); - } - SDL_free(wave); - } + if ( wave ) { + /* Clean up associated data */ + if ( wave->cvt.buf ) { + SDL_free(wave->cvt.buf); + } + if ( wave->freerw ) { + SDL_RWclose(wave->rw); + } + SDL_free(wave); + } } /* Return non-zero if a stream is currently playing */ int WAVStream_Active(void) { - int active; + int active; - active = 0; - if ( music && (SDL_RWtell(music->rw) < music->stop) ) { - active = 1; - } - return(active); + active = 0; + if ( music && (SDL_RWtell(music->rw) < music->stop) ) { + active = 1; + } + return(active); } static int ReadChunk(SDL_RWops *src, Chunk *chunk, int read_data) { - chunk->magic = SDL_ReadLE32(src); - chunk->length = SDL_ReadLE32(src); - if ( read_data ) { - chunk->data = (Uint8 *)SDL_malloc(chunk->length); - if ( chunk->data == NULL ) { - Mix_SetError("Out of memory"); - return(-1); - } - if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) { - Mix_SetError("Couldn't read chunk"); - SDL_free(chunk->data); - return(-1); - } - } else { - SDL_RWseek(src, chunk->length, RW_SEEK_CUR); - } - return(chunk->length); + chunk->magic = SDL_ReadLE32(src); + chunk->length = SDL_ReadLE32(src); + if ( read_data ) { + chunk->data = (Uint8 *)SDL_malloc(chunk->length); + if ( chunk->data == NULL ) { + Mix_SetError("Out of memory"); + return(-1); + } + if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) { + Mix_SetError("Couldn't read chunk"); + SDL_free(chunk->data); + return(-1); + } + } else { + SDL_RWseek(src, chunk->length, RW_SEEK_CUR); + } + return(chunk->length); } static SDL_RWops *LoadWAVStream (SDL_RWops *src, SDL_AudioSpec *spec, - long *start, long *stop) + long *start, long *stop) { - int was_error; - Chunk chunk; - int lenread; - - /* WAV magic header */ - Uint32 RIFFchunk; - Uint32 wavelen; - Uint32 WAVEmagic; - - /* FMT chunk */ - WaveFMT *format = NULL; - - was_error = 0; - - /* Check the magic header */ - RIFFchunk = SDL_ReadLE32(src); - wavelen = SDL_ReadLE32(src); - WAVEmagic = SDL_ReadLE32(src); - if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) { - Mix_SetError("Unrecognized file type (not WAVE)"); - was_error = 1; - goto done; - } - - /* Read the audio data format chunk */ - chunk.data = NULL; - do { - /* FIXME! Add this logic to SDL_LoadWAV_RW() */ - if ( chunk.data ) { - SDL_free(chunk.data); - } - lenread = ReadChunk(src, &chunk, 1); - if ( lenread < 0 ) { - was_error = 1; - goto done; - } - } while ( (chunk.magic == FACT) || (chunk.magic == LIST) ); - - /* Decode the audio data format */ - format = (WaveFMT *)chunk.data; - if ( chunk.magic != FMT ) { - SDL_free(chunk.data); - Mix_SetError("Complex WAVE files not supported"); - was_error = 1; - goto done; - } - switch (SDL_SwapLE16(format->encoding)) { - case PCM_CODE: - /* We can understand this */ - break; - default: - Mix_SetError("Unknown WAVE data format"); - was_error = 1; - goto done; - } - memset(spec, 0, (sizeof *spec)); - spec->freq = SDL_SwapLE32(format->frequency); - switch (SDL_SwapLE16(format->bitspersample)) { - case 8: - spec->format = AUDIO_U8; - break; - case 16: - spec->format = AUDIO_S16; - break; - default: - Mix_SetError("Unknown PCM data format"); - was_error = 1; - goto done; - } - spec->channels = (Uint8) SDL_SwapLE16(format->channels); - spec->samples = 4096; /* Good default buffer size */ - - /* Set the file offset to the DATA chunk data */ - chunk.data = NULL; - do { - *start = SDL_RWtell(src) + 2*sizeof(Uint32); - lenread = ReadChunk(src, &chunk, 0); - if ( lenread < 0 ) { - was_error = 1; - goto done; - } - } while ( chunk.magic != DATA ); - *stop = SDL_RWtell(src); + int was_error; + Chunk chunk; + int lenread; + + /* WAV magic header */ + Uint32 RIFFchunk; + Uint32 wavelen; + Uint32 WAVEmagic; + + /* FMT chunk */ + WaveFMT *format = NULL; + + was_error = 0; + + /* Check the magic header */ + RIFFchunk = SDL_ReadLE32(src); + wavelen = SDL_ReadLE32(src); + WAVEmagic = SDL_ReadLE32(src); + if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) { + Mix_SetError("Unrecognized file type (not WAVE)"); + was_error = 1; + goto done; + } + + /* Read the audio data format chunk */ + chunk.data = NULL; + do { + /* FIXME! Add this logic to SDL_LoadWAV_RW() */ + if ( chunk.data ) { + SDL_free(chunk.data); + } + lenread = ReadChunk(src, &chunk, 1); + if ( lenread < 0 ) { + was_error = 1; + goto done; + } + } while ( (chunk.magic == FACT) || (chunk.magic == LIST) ); + + /* Decode the audio data format */ + format = (WaveFMT *)chunk.data; + if ( chunk.magic != FMT ) { + SDL_free(chunk.data); + Mix_SetError("Complex WAVE files not supported"); + was_error = 1; + goto done; + } + switch (SDL_SwapLE16(format->encoding)) { + case PCM_CODE: + /* We can understand this */ + break; + default: + Mix_SetError("Unknown WAVE data format"); + was_error = 1; + goto done; + } + memset(spec, 0, (sizeof *spec)); + spec->freq = SDL_SwapLE32(format->frequency); + switch (SDL_SwapLE16(format->bitspersample)) { + case 8: + spec->format = AUDIO_U8; + break; + case 16: + spec->format = AUDIO_S16; + break; + default: + Mix_SetError("Unknown PCM data format"); + was_error = 1; + goto done; + } + spec->channels = (Uint8) SDL_SwapLE16(format->channels); + spec->samples = 4096; /* Good default buffer size */ + + /* Set the file offset to the DATA chunk data */ + chunk.data = NULL; + do { + *start = SDL_RWtell(src) + 2*sizeof(Uint32); + lenread = ReadChunk(src, &chunk, 0); + if ( lenread < 0 ) { + was_error = 1; + goto done; + } + } while ( chunk.magic != DATA ); + *stop = SDL_RWtell(src); done: - if ( format != NULL ) { - SDL_free(format); - } - if ( was_error ) { - return NULL; - } - return(src); + if ( format != NULL ) { + SDL_free(format); + } + if ( was_error ) { + return NULL; + } + return(src); } /* I couldn't get SANE_to_double() to work, so I stole this from libsndfile. @@ -383,142 +383,142 @@ static SDL_RWops *LoadWAVStream (SDL_RWops *src, SDL_AudioSpec *spec, static Uint32 SANE_to_Uint32 (Uint8 *sanebuf) { - /* Negative number? */ - if (sanebuf[0] & 0x80) - return 0; + /* Negative number? */ + if (sanebuf[0] & 0x80) + return 0; - /* Less than 1? */ - if (sanebuf[0] <= 0x3F) - return 1; + /* Less than 1? */ + if (sanebuf[0] <= 0x3F) + return 1; - /* Way too big? */ - if (sanebuf[0] > 0x40) - return 0x4000000; + /* Way too big? */ + if (sanebuf[0] > 0x40) + return 0x4000000; - /* Still too big? */ - if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C) - return 800000000; + /* Still too big? */ + if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C) + return 800000000; - return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7) - | (sanebuf[5] >> 1)) >> (29 - sanebuf[1]); + return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7) + | (sanebuf[5] >> 1)) >> (29 - sanebuf[1]); } static SDL_RWops *LoadAIFFStream (SDL_RWops *src, SDL_AudioSpec *spec, - long *start, long *stop) + long *start, long *stop) { - int was_error; - int found_SSND; - int found_COMM; - - Uint32 chunk_type; - Uint32 chunk_length; - long next_chunk; - - /* AIFF magic header */ - Uint32 FORMchunk; - Uint32 AIFFmagic; - /* SSND chunk */ - Uint32 offset; - Uint32 blocksize; - /* COMM format chunk */ - Uint16 channels = 0; - Uint32 numsamples = 0; - Uint16 samplesize = 0; - Uint8 sane_freq[10]; - Uint32 frequency = 0; - - was_error = 0; - - /* Check the magic header */ - FORMchunk = SDL_ReadLE32(src); - chunk_length = SDL_ReadBE32(src); - AIFFmagic = SDL_ReadLE32(src); - if ( (FORMchunk != FORM) || (AIFFmagic != AIFF) ) { - Mix_SetError("Unrecognized file type (not AIFF)"); - was_error = 1; - goto done; - } - - /* From what I understand of the specification, chunks may appear in + int was_error; + int found_SSND; + int found_COMM; + + Uint32 chunk_type; + Uint32 chunk_length; + long next_chunk; + + /* AIFF magic header */ + Uint32 FORMchunk; + Uint32 AIFFmagic; + /* SSND chunk */ + Uint32 offset; + Uint32 blocksize; + /* COMM format chunk */ + Uint16 channels = 0; + Uint32 numsamples = 0; + Uint16 samplesize = 0; + Uint8 sane_freq[10]; + Uint32 frequency = 0; + + was_error = 0; + + /* Check the magic header */ + FORMchunk = SDL_ReadLE32(src); + chunk_length = SDL_ReadBE32(src); + AIFFmagic = SDL_ReadLE32(src); + if ( (FORMchunk != FORM) || (AIFFmagic != AIFF) ) { + Mix_SetError("Unrecognized file type (not AIFF)"); + was_error = 1; + goto done; + } + + /* From what I understand of the specification, chunks may appear in * any order, and we should just ignore unknown ones. - * - * TODO: Better sanity-checking. E.g. what happens if the AIFF file - * contains compressed sound data? + * + * TODO: Better sanity-checking. E.g. what happens if the AIFF file + * contains compressed sound data? */ - found_SSND = 0; - found_COMM = 0; + found_SSND = 0; + found_COMM = 0; - do { - chunk_type = SDL_ReadLE32(src); - chunk_length = SDL_ReadBE32(src); - next_chunk = SDL_RWtell(src) + chunk_length; + do { + chunk_type = SDL_ReadLE32(src); + chunk_length = SDL_ReadBE32(src); + next_chunk = SDL_RWtell(src) + chunk_length; - /* Paranoia to avoid infinite loops */ - if (chunk_length == 0) - break; + /* Paranoia to avoid infinite loops */ + if (chunk_length == 0) + break; switch (chunk_type) { - case SSND: - found_SSND = 1; - offset = SDL_ReadBE32(src); - blocksize = SDL_ReadBE32(src); - *start = SDL_RWtell(src) + offset; - break; - - case COMM: - found_COMM = 1; - - /* Read the audio data format chunk */ - channels = SDL_ReadBE16(src); - numsamples = SDL_ReadBE32(src); - samplesize = SDL_ReadBE16(src); - SDL_RWread(src, sane_freq, sizeof(sane_freq), 1); - frequency = SANE_to_Uint32(sane_freq); - break; - - default: - break; - } - } while ((!found_SSND || !found_COMM) - && SDL_RWseek(src, next_chunk, RW_SEEK_SET) != -1); - - if (!found_SSND) { - Mix_SetError("Bad AIFF file (no SSND chunk)"); - was_error = 1; - goto done; - } - - if (!found_COMM) { - Mix_SetError("Bad AIFF file (no COMM chunk)"); - was_error = 1; - goto done; - } - - *stop = *start + channels * numsamples * (samplesize / 8); - - /* Decode the audio data format */ - memset(spec, 0, (sizeof *spec)); - spec->freq = frequency; - switch (samplesize) { - case 8: - spec->format = AUDIO_S8; - break; - case 16: - spec->format = AUDIO_S16MSB; - break; - default: - Mix_SetError("Unknown samplesize in data format"); - was_error = 1; - goto done; - } - spec->channels = (Uint8) channels; - spec->samples = 4096; /* Good default buffer size */ + case SSND: + found_SSND = 1; + offset = SDL_ReadBE32(src); + blocksize = SDL_ReadBE32(src); + *start = SDL_RWtell(src) + offset; + break; + + case COMM: + found_COMM = 1; + + /* Read the audio data format chunk */ + channels = SDL_ReadBE16(src); + numsamples = SDL_ReadBE32(src); + samplesize = SDL_ReadBE16(src); + SDL_RWread(src, sane_freq, sizeof(sane_freq), 1); + frequency = SANE_to_Uint32(sane_freq); + break; + + default: + break; + } + } while ((!found_SSND || !found_COMM) + && SDL_RWseek(src, next_chunk, RW_SEEK_SET) != -1); + + if (!found_SSND) { + Mix_SetError("Bad AIFF file (no SSND chunk)"); + was_error = 1; + goto done; + } + + if (!found_COMM) { + Mix_SetError("Bad AIFF file (no COMM chunk)"); + was_error = 1; + goto done; + } + + *stop = *start + channels * numsamples * (samplesize / 8); + + /* Decode the audio data format */ + memset(spec, 0, (sizeof *spec)); + spec->freq = frequency; + switch (samplesize) { + case 8: + spec->format = AUDIO_S8; + break; + case 16: + spec->format = AUDIO_S16MSB; + break; + default: + Mix_SetError("Unknown samplesize in data format"); + was_error = 1; + goto done; + } + spec->channels = (Uint8) channels; + spec->samples = 4096; /* Good default buffer size */ done: - if ( was_error ) { - return NULL; - } - return(src); + if ( was_error ) { + return NULL; + } + return(src); } diff --git a/wavestream.h b/wavestream.h index ff7dd367..ed18a847 100644 --- a/wavestream.h +++ b/wavestream.h @@ -26,11 +26,11 @@ #include typedef struct { - SDL_RWops *rw; - SDL_bool freerw; - long start; - long stop; - SDL_AudioCVT cvt; + SDL_RWops *rw; + SDL_bool freerw; + long start; + long stop; + SDL_AudioCVT cvt; } WAVStream; /* Initialize the WAVStream player, with the given mixer settings