Skip to content

Commit

Permalink
Sam Lantinga - Sun Nov 8 08:34:48 PST 2009
Browse files Browse the repository at this point in the history
 * Added Mix_Init()/Mix_Quit() to prevent constantly loading and unloading DLLs
  • Loading branch information
slouken committed Nov 8, 2009
1 parent a1e428d commit b540241
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 53 deletions.
2 changes: 2 additions & 0 deletions CHANGES
@@ -1,4 +1,6 @@
1.2.10:
Sam Lantinga - Sun Nov 8 08:34:48 PST 2009
* Added Mix_Init()/Mix_Quit() to prevent constantly loading and unloading DLLs
Mike Frysinger - 2009-11-05 09:11:43 PST
* Check for fork/vfork on any platform, don't just assume it on UNIX
Jon Atkins - Thu Nov 5 00:02:50 2009 UTC
Expand Down
17 changes: 17 additions & 0 deletions SDL_mixer.h
Expand Up @@ -65,6 +65,23 @@ extern "C" {
*/
extern DECLSPEC const SDL_version * SDLCALL Mix_Linked_Version(void);

typedef enum
{
MIX_INIT_FLAC = 0x00000001,
MIX_INIT_MOD = 0x00000002,
MIX_INIT_MP3 = 0x00000004,
MIX_INIT_OGG = 0x00000008
} MIX_InitFlags;

/* Loads dynamic libraries and prepares them for use. Flags should be
one or more flags from MIX_InitFlags OR'd together.
It returns the flags successfully initialized, or 0 on failure.
*/
extern DECLSPEC int SDLCALL Mix_Init(int flags);

/* Unloads libraries loaded with Mix_Init */
extern DECLSPEC void SDLCALL Mix_Quit();


/* The default mixer has 8 simultaneous mixing channels */
#ifndef MIX_CHANNELS
Expand Down
29 changes: 22 additions & 7 deletions dynamic_flac.c
Expand Up @@ -35,8 +35,8 @@ flac_loader flac = {
};

#ifdef FLAC_DYNAMIC

int Mix_InitFLAC() {
int Mix_InitFLAC()
{
if ( flac.loaded == 0 ) {
flac.handle = SDL_LoadObject(FLAC_DYNAMIC);
if ( flac.handle == NULL ) {
Expand Down Expand Up @@ -130,7 +130,8 @@ int Mix_InitFLAC() {

return 0;
}
void Mix_QuitFLAC() {
void Mix_QuitFLAC()
{
if ( flac.loaded == 0 ) {
return;
}
Expand All @@ -140,7 +141,8 @@ void Mix_QuitFLAC() {
--flac.loaded;
}
#else
int Mix_InitFLAC() {
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;
Expand All @@ -163,14 +165,27 @@ int Mix_InitFLAC() {

return 0;
}
void Mix_QuitFLAC() {
void Mix_QuitFLAC()
{
if ( flac.loaded == 0 ) {
return;
}
if ( flac.loaded == 1 ) {
}
--flac.loaded;
}
#endif // FLAC_DYNAMIC
#endif /* FLAC_DYNAMIC */

#else

int Mix_InitFLAC()
{
Mix_SetError("FLAC audio is not supported");
return -1;
}

void Mix_QuitFLAC()
{
}

#endif // FLAC_MUSIC
#endif /* FLAC_MUSIC */
4 changes: 2 additions & 2 deletions dynamic_flac.h
Expand Up @@ -62,7 +62,7 @@ typedef struct {

extern flac_loader flac;

#endif /* FLAC_MUSIC */

extern int Mix_InitFLAC();
extern void Mix_QuitFLAC();

#endif // FLAC_MUSIC
12 changes: 12 additions & 0 deletions dynamic_mod.c
Expand Up @@ -273,4 +273,16 @@ void Mix_QuitMOD()
}
#endif /* MOD_DYNAMIC */

#else

int Mix_InitMOD()
{
Mix_SetError("MOD audio is not supported");
return -1;
}

void Mix_QuitMOD()
{
}

#endif /* MOD_MUSIC */
4 changes: 2 additions & 2 deletions dynamic_mod.h
Expand Up @@ -57,7 +57,7 @@ typedef struct {

extern mikmod_loader mikmod;

#endif /* MOD_MUSIC */

extern int Mix_InitMOD();
extern void Mix_QuitMOD();

#endif
12 changes: 12 additions & 0 deletions dynamic_mp3.c
Expand Up @@ -177,4 +177,16 @@ void Mix_QuitMP3()
}
#endif /* MP3_DYNAMIC */

#else

int Mix_InitMP3()
{
Mix_SetError("MP3 audio is not supported");
return -1;
}

void Mix_QuitMP3()
{
}

#endif /* MP3_MUSIC */
4 changes: 2 additions & 2 deletions dynamic_mp3.h
Expand Up @@ -43,7 +43,7 @@ typedef struct {

extern smpeg_loader smpeg;

#endif /* MUSIC_MP3 */

extern int Mix_InitMP3();
extern void Mix_QuitMP3();

#endif
12 changes: 12 additions & 0 deletions dynamic_ogg.c
Expand Up @@ -125,4 +125,16 @@ void Mix_QuitOgg()
}
#endif /* OGG_DYNAMIC */

#else

int Mix_InitOgg()
{
Mix_SetError("Ogg Vorbis audio is not supported");
return -1;
}

void Mix_QuitOgg()
{
}

#endif /* OGG_MUSIC */
4 changes: 2 additions & 2 deletions dynamic_ogg.h
Expand Up @@ -44,7 +44,7 @@ typedef struct {

extern vorbis_loader vorbis;

#endif /* OGG_MUSIC */

extern int Mix_InitOgg();
extern void Mix_QuitOgg();

#endif
39 changes: 20 additions & 19 deletions load_flac.c
Expand Up @@ -208,7 +208,8 @@ static FLAC__StreamDecoderWriteStatus flac_write_load_cb(
static void flac_metadata_load_cb(
const FLAC__StreamDecoder *decoder,
const FLAC__StreamMetadata *metadata,
void *client_data) {
void *client_data)
{
FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
FLAC__uint64 total_samples;
unsigned bps;
Expand Down Expand Up @@ -237,7 +238,8 @@ static void flac_metadata_load_cb(
static void flac_error_load_cb(
const FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderErrorStatus status,
void *client_data) {
void *client_data)
{
// print an SDL error based on the error status
switch (status) {
case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
Expand All @@ -260,7 +262,8 @@ static void flac_error_load_cb(

/* 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)
{
FLAC__StreamDecoder *decoder = 0;
FLAC__StreamDecoderInitStatus init_status;
int was_error = 1;
Expand All @@ -271,11 +274,11 @@ SDL_AudioSpec *Mix_LoadFLAC_RW (SDL_RWops *src, int freesrc,
FLAC_SDL_Data* client_data;
client_data = (FLAC_SDL_Data *)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_InitFLAC() < 0)
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.");
Expand Down Expand Up @@ -313,25 +316,23 @@ SDL_AudioSpec *Mix_LoadFLAC_RW (SDL_RWops *src, int freesrc,
*audio_len &= ~(samplesize - 1);

done:
if(was_init && decoder) {
if (was_init && decoder) {
flac.FLAC__stream_decoder_finish (decoder);
}

if(decoder) {
if (decoder) {
flac.FLAC__stream_decoder_delete (decoder);
}

if (src) {
if (freesrc)
SDL_RWclose (src);
else
SDL_RWseek (src, 0, SEEK_SET);
}

if (was_error)
spec = NULL;
if (src) {
if (freesrc)
SDL_RWclose (src);
else
SDL_RWseek (src, 0, SEEK_SET);
}

Mix_QuitFLAC ();
if (was_error)
spec = NULL;

return spec;
}
Expand Down
4 changes: 1 addition & 3 deletions load_ogg.c
Expand Up @@ -80,7 +80,7 @@ SDL_AudioSpec *Mix_LoadOGG_RW (SDL_RWops *src, int freesrc,
if ( (!src) || (!audio_buf) || (!audio_len) ) /* sanity checks. */
goto done;

if ( Mix_InitOgg() < 0 )
if ( !Mix_Init(MIX_INIT_OGG) )
goto done;

callbacks.read_func = sdl_read_func;
Expand Down Expand Up @@ -153,8 +153,6 @@ SDL_AudioSpec *Mix_LoadOGG_RW (SDL_RWops *src, int freesrc,
if ( was_error )
spec = NULL;

Mix_QuitOgg();

return(spec);
} /* Mix_LoadOGG_RW */

Expand Down
52 changes: 52 additions & 0 deletions mixer.c
Expand Up @@ -35,6 +35,10 @@
#include "load_voc.h"
#include "load_ogg.h"
#include "load_flac.h"
#include "dynamic_flac.h"
#include "dynamic_mod.h"
#include "dynamic_mp3.h"
#include "dynamic_ogg.h"

#define __MIX_INTERNAL_EFFECT__
#include "effects_internal.h"
Expand Down Expand Up @@ -134,6 +138,54 @@ const SDL_version *Mix_Linked_Version(void)
return(&linked_version);
}

static int initialized = 0;

int Mix_Init(int flags)
{
int result = 0;

if ((flags & MIX_INIT_FLAC) && !(initialized & MIX_INIT_FLAC)) {
if (Mix_InitFLAC() == 0) {
result |= MIX_INIT_FLAC;
}
}
if ((flags & MIX_INIT_MOD) && !(initialized & MIX_INIT_MOD)) {
if (Mix_InitMOD() == 0) {
result |= MIX_INIT_MOD;
}
}
if ((flags & MIX_INIT_MP3) && !(initialized & MIX_INIT_MP3)) {
if (Mix_InitMP3() == 0) {
result |= MIX_INIT_MP3;
}
}
if ((flags & MIX_INIT_OGG) && !(initialized & MIX_INIT_OGG)) {
if (Mix_InitOgg() == 0) {
result |= MIX_INIT_OGG;
}
}
initialized |= result;

return (result);
}

void Mix_Quit()
{
if (initialized & MIX_INIT_FLAC) {
Mix_QuitFLAC();
}
if (initialized & MIX_INIT_MOD) {
Mix_QuitMOD();
}
if (initialized & MIX_INIT_MP3) {
Mix_QuitMP3();
}
if (initialized & MIX_INIT_OGG) {
Mix_QuitOgg();
}
initialized = 0;
}

static int _Mix_remove_all_effects(int channel, effect_info **e);

/*
Expand Down
5 changes: 2 additions & 3 deletions music.c
Expand Up @@ -510,7 +510,7 @@ Mix_Music *Mix_LoadMUS(const char *file)
(ext && MIX_string_equals(ext, "MPEG")) ||
(magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ||
(strncmp((char *)magic, "ID3", 3) == 0) ) {
if ( Mix_InitMP3() == 0 ) {
if ( Mix_Init(MIX_INIT_MP3) ) {
SMPEG_Info info;
music->type = MUS_MP3;
music->data.mp3 = smpeg.SMPEG_new(file, &info, 0);
Expand Down Expand Up @@ -621,7 +621,6 @@ void Mix_FreeMusic(Mix_Music *music)
#ifdef MP3_MUSIC
case MUS_MP3:
smpeg.SMPEG_delete(music->data.mp3);
Mix_QuitMP3();
break;
#endif
#ifdef MP3_MAD_MUSIC
Expand Down Expand Up @@ -1297,7 +1296,7 @@ Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw)
#endif
#ifdef MP3_MUSIC
if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
if ( Mix_InitMP3() == 0 ) {
if ( Mix_Init(MIX_INIT_MP3) ) {
SMPEG_Info info;
music->type = MUS_MP3;
music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0);
Expand Down

0 comments on commit b540241

Please sign in to comment.