Split the mikmod support out into a separate file for clean coding
authorSam Lantinga <slouken@libsdl.org>
Sat, 03 Oct 2009 08:09:27 +0000
changeset 411cade1e3a4cb8
parent 410 5694b5d9b80a
child 412 4c1f81481f87
Split the mikmod support out into a separate file for clean coding
configure.in
music.c
music_mod.c
music_mod.h
     1.1 --- a/configure.in	Sat Oct 03 07:53:24 2009 +0000
     1.2 +++ b/configure.in	Sat Oct 03 08:09:27 2009 +0000
     1.3 @@ -191,17 +191,26 @@
     1.4      printf("no\n*** libmikmod is older than %d.%d.%d, not using.\n",maj,min,rev);
     1.5      return 1;
     1.6  }
     1.7 -], EXTRA_CFLAGS="$EXTRA_CFLAGS -DLIBMIKMOD_MUSIC `$LIBMIKMOD_CONFIG --cflags`";EXTRA_LDFLAGS="$EXTRA_LDFLAGS `$LIBMIKMOD_CONFIG --libs`", CFLAGS="$CFLAGS_SAVED";LIBS="$LIBS_SAVED",
     1.8 -[echo $ac_n "cross compiling; assumed OK... $ac_c"])
     1.9 +], , , [echo $ac_n "cross compiling; assumed OK... $ac_c"])
    1.10      fi
    1.11 +    CFLAGS="$CFLAGS_SAVED"
    1.12 +    LIBS="$LIBS_SAVED"
    1.13  fi
    1.14  
    1.15  AC_ARG_ENABLE([music-mod],
    1.16  AC_HELP_STRING([--enable-music-mod], [enable MOD music via mikmod [[default=yes]]]),
    1.17 -              [], [enable_music_mod=$no_libmikmod])
    1.18 -if test x$enable_music_mod = xyes -a x$enable_music_libmikmod = xno; then
    1.19 -    EXTRA_CFLAGS="$EXTRA_CFLAGS -DMOD_MUSIC -I\$(srcdir)/mikmod"
    1.20 -    SOURCES="$SOURCES $srcdir/mikmod/*.c"
    1.21 +              [], [enable_music_mod=yes])
    1.22 +if test x$enable_music_mod = xyes; then
    1.23 +    EXTRA_CFLAGS="$EXTRA_CFLAGS -DMOD_MUSIC"
    1.24 +    SOURCES="$SOURCES $srcdir/music_mod.c"
    1.25 +
    1.26 +    if test x$enable_music_libmikmod = xyes; then
    1.27 +        EXTRA_CFLAGS="$EXTRA_CFLAGS -DLIBMIKMOD_MUSIC `$LIBMIKMOD_CONFIG --cflags`"
    1.28 +        EXTRA_LDFLAGS="$EXTRA_LDFLAGS `$LIBMIKMOD_CONFIG --libs`"
    1.29 +    else
    1.30 +        EXTRA_CFLAGS="$EXTRA_CFLAGS -I\$(srcdir)/mikmod"
    1.31 +        SOURCES="$SOURCES $srcdir/mikmod/*.c"
    1.32 +    fi
    1.33  fi
    1.34  AC_ARG_ENABLE([music-midi],
    1.35  AC_HELP_STRING([--enable-music-midi], [enable MIDI music via timidity [[default=yes]]]),
     2.1 --- a/music.c	Sat Oct 03 07:53:24 2009 +0000
     2.2 +++ b/music.c	Sat Oct 03 08:09:27 2009 +0000
     2.3 @@ -32,14 +32,6 @@
     2.4  
     2.5  #include "SDL_mixer.h"
     2.6  
     2.7 -#define SDL_SURROUND
     2.8 -
     2.9 -#ifdef SDL_SURROUND
    2.10 -#define MAX_OUTPUT_CHANNELS 6
    2.11 -#else
    2.12 -#define MAX_OUTPUT_CHANNELS 2
    2.13 -#endif
    2.14 -
    2.15  /* The music command hack is UNIX specific */
    2.16  #ifndef unix
    2.17  #undef CMD_MUSIC
    2.18 @@ -51,21 +43,8 @@
    2.19  #ifdef WAV_MUSIC
    2.20  #include "wavestream.h"
    2.21  #endif
    2.22 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
    2.23 -#  include "mikmod.h"
    2.24 -#  if defined(LIBMIKMOD_VERSION)                /* libmikmod 3.1.8 */
    2.25 -#    define UNIMOD			MODULE
    2.26 -#    define MikMod_Init()		MikMod_Init(NULL)
    2.27 -#    define MikMod_LoadSong(a,b)	Player_Load(a,b,0)
    2.28 -#    ifndef LIBMIKMOD_MUSIC
    2.29 -#    define MikMod_LoadSongRW(a,b)	Player_LoadRW(a,b,0)
    2.30 -#    endif
    2.31 -#    define MikMod_FreeSong		Player_Free
    2.32 -     extern int MikMod_errno;
    2.33 -#  else                                        /* old MikMod 3.0.3 */
    2.34 -#    define MikMod_strerror(x)		_mm_errmsg[x])
    2.35 -#    define MikMod_errno		_mm_errno
    2.36 -#  endif
    2.37 +#ifdef MOD_MUSIC
    2.38 +#include "music_mod.h"
    2.39  #endif
    2.40  #ifdef MID_MUSIC
    2.41  #  ifdef USE_TIMIDITY_MIDI
    2.42 @@ -104,8 +83,6 @@
    2.43  static char *music_cmd = NULL;
    2.44  static Mix_Music * volatile music_playing = NULL;
    2.45  static int music_volume = MIX_MAX_VOLUME;
    2.46 -static int music_swap8;
    2.47 -static int music_swap16;
    2.48  
    2.49  struct _Mix_Music {
    2.50  	Mix_MusicType type;
    2.51 @@ -116,8 +93,8 @@
    2.52  #ifdef WAV_MUSIC
    2.53  		WAVStream *wave;
    2.54  #endif
    2.55 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
    2.56 -		UNIMOD *module;
    2.57 +#ifdef MOD_MUSIC
    2.58 +		struct MODULE *module;
    2.59  #endif
    2.60  #ifdef MID_MUSIC
    2.61  #ifdef USE_TIMIDITY_MIDI
    2.62 @@ -155,10 +132,6 @@
    2.63  #endif
    2.64  #endif
    2.65  
    2.66 -/* Reference for converting mikmod output to 4/6 channels */
    2.67 -static int current_output_channels;
    2.68 -static Uint16 current_output_format;
    2.69 -
    2.70  /* Used to calculate fading steps */
    2.71  static int ms_per_step;
    2.72  
    2.73 @@ -185,7 +158,7 @@
    2.74  	if (ptr == NULL) {
    2.75  		return;  /* oh well, go on without it. */
    2.76  	}
    2.77 -    music_decoders = (const char **) ptr;
    2.78 +	music_decoders = (const char **) ptr;
    2.79  	music_decoders[num_decoders++] = decoder;
    2.80  }
    2.81  
    2.82 @@ -285,80 +258,9 @@
    2.83  				left = WAVStream_PlaySome(stream, len);
    2.84  				break;
    2.85  #endif
    2.86 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
    2.87 +#ifdef MOD_MUSIC
    2.88  			case MUS_MOD:
    2.89 -				if (current_output_channels > 2) {
    2.90 -					int small_len = 2 * len / current_output_channels;
    2.91 -					int i;
    2.92 -					Uint8 *src, *dst;
    2.93 -
    2.94 -					VC_WriteBytes((SBYTE *)stream, small_len);
    2.95 -					/* and extend to len by copying channels */
    2.96 -					src = stream + small_len;
    2.97 -					dst = stream + len;
    2.98 -
    2.99 -					switch (current_output_format & 0xFF) {
   2.100 -						case 8:
   2.101 -							for ( i=small_len/2; i; --i ) {
   2.102 -								src -= 2;
   2.103 -								dst -= current_output_channels;
   2.104 -								dst[0] = src[0];
   2.105 -								dst[1] = src[1];
   2.106 -								dst[2] = src[0];
   2.107 -								dst[3] = src[1];
   2.108 -								if (current_output_channels == 6) {
   2.109 -									dst[4] = src[0];
   2.110 -									dst[5] = src[1];
   2.111 -								}
   2.112 -							}
   2.113 -							break;
   2.114 -						case 16:
   2.115 -							for ( i=small_len/4; i; --i ) {
   2.116 -								src -= 4;
   2.117 -								dst -= 2 * current_output_channels;
   2.118 -								dst[0] = src[0];
   2.119 -								dst[1] = src[1];
   2.120 -								dst[2] = src[2];
   2.121 -								dst[3] = src[3];
   2.122 -								dst[4] = src[0];
   2.123 -								dst[5] = src[1];
   2.124 -								dst[6] = src[2];
   2.125 -								dst[7] = src[3];
   2.126 -								if (current_output_channels == 6) {
   2.127 -									dst[8] = src[0];
   2.128 -									dst[9] = src[1];
   2.129 -									dst[10] = src[2];
   2.130 -									dst[11] = src[3];
   2.131 -								}
   2.132 -							}
   2.133 -							break;
   2.134 -					}
   2.135 -
   2.136 -
   2.137 -
   2.138 -				}
   2.139 -				else VC_WriteBytes((SBYTE *)stream, len);
   2.140 -				if ( music_swap8 ) {
   2.141 -					Uint8 *dst;
   2.142 -					int i;
   2.143 -
   2.144 -					dst = stream;
   2.145 -					for ( i=len; i; --i ) {
   2.146 -						*dst++ ^= 0x80;
   2.147 -					}
   2.148 -				} else
   2.149 -				if ( music_swap16 ) {
   2.150 -					Uint8 *dst, tmp;
   2.151 -					int i;
   2.152 -
   2.153 -					dst = stream;
   2.154 -					for ( i=(len/2); i; --i ) {
   2.155 -						tmp = dst[0];
   2.156 -						dst[0] = dst[1];
   2.157 -						dst[1] = tmp;
   2.158 -						dst += 2;
   2.159 -					}
   2.160 -				}
   2.161 +				left = MOD_playAudio(music_playing->data.module, stream, len);
   2.162  				break;
   2.163  #endif
   2.164  #ifdef MID_MUSIC
   2.165 @@ -407,12 +309,8 @@
   2.166  /* Initialize the music players with a certain desired audio format */
   2.167  int open_music(SDL_AudioSpec *mixer)
   2.168  {
   2.169 -	int music_error;
   2.170 -#ifdef LIBMIKMOD_MUSIC
   2.171 -	CHAR *list;
   2.172 -#endif
   2.173 +	int music_error = 0;
   2.174  
   2.175 -	music_error = 0;
   2.176  #ifdef WAV_MUSIC
   2.177  	if ( WAVStream_Init(mixer) < 0 ) {
   2.178  		++music_error;
   2.179 @@ -420,76 +318,12 @@
   2.180  		add_music_decoder("WAVE");
   2.181  	}
   2.182  #endif
   2.183 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
   2.184 -	/* Set the MikMod music format */
   2.185 -	music_swap8 = 0;
   2.186 -	music_swap16 = 0;
   2.187 -	switch (mixer->format) {
   2.188 -
   2.189 -		case AUDIO_U8:
   2.190 -		case AUDIO_S8: {
   2.191 -			if ( mixer->format == AUDIO_S8 ) {
   2.192 -				music_swap8 = 1;
   2.193 -			}
   2.194 -			md_mode = 0;
   2.195 -		}
   2.196 -		break;
   2.197 -
   2.198 -		case AUDIO_S16LSB:
   2.199 -		case AUDIO_S16MSB: {
   2.200 -			/* See if we need to correct MikMod mixing */
   2.201 -#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   2.202 -			if ( mixer->format == AUDIO_S16MSB ) {
   2.203 -#else
   2.204 -			if ( mixer->format == AUDIO_S16LSB ) {
   2.205 -#endif
   2.206 -				music_swap16 = 1;
   2.207 -			}
   2.208 -			md_mode = DMODE_16BITS;
   2.209 -		}
   2.210 -		break;
   2.211 -
   2.212 -		default: {
   2.213 -			Mix_SetError("Unknown hardware audio format");
   2.214 -			++music_error;
   2.215 -		}
   2.216 +#ifdef MOD_MUSIC
   2.217 +	if ( MOD_init(mixer) < 0 ) {
   2.218 +		++music_error;
   2.219 +	} else {
   2.220 +		add_music_decoder("MIKMOD");
   2.221  	}
   2.222 -	current_output_channels = mixer->channels;
   2.223 -	current_output_format = mixer->format;
   2.224 -	if ( mixer->channels > 1 ) {
   2.225 -		if ( mixer->channels > MAX_OUTPUT_CHANNELS ) {
   2.226 -			Mix_SetError("Hardware uses more channels than mixer");
   2.227 -			++music_error;
   2.228 -		}
   2.229 -		md_mode |= DMODE_STEREO;
   2.230 -	}
   2.231 -	md_mixfreq = mixer->freq;
   2.232 -	md_device  = 0;
   2.233 -	md_volume  = 96;
   2.234 -	md_musicvolume = 128;
   2.235 -	md_sndfxvolume = 128;
   2.236 -	md_pansep  = 128;
   2.237 -	md_reverb  = 0;
   2.238 -	md_mode    |= DMODE_HQMIXER|DMODE_SOFT_MUSIC|DMODE_SURROUND;
   2.239 -#ifdef LIBMIKMOD_MUSIC
   2.240 -	list = MikMod_InfoDriver();
   2.241 -	if ( list )
   2.242 -	  free(list);
   2.243 -	else
   2.244 -#endif
   2.245 -	MikMod_RegisterDriver(&drv_nos);
   2.246 -#ifdef LIBMIKMOD_MUSIC
   2.247 -	list = MikMod_InfoLoader();
   2.248 -	if ( list )
   2.249 -	  free(list);
   2.250 -	else
   2.251 -#endif
   2.252 -	MikMod_RegisterAllLoaders();
   2.253 -	if ( MikMod_Init() ) {
   2.254 -		Mix_SetError("%s", MikMod_strerror(MikMod_errno));
   2.255 -		++music_error;
   2.256 -	}
   2.257 -	add_music_decoder("MIKMOD");
   2.258  #endif
   2.259  #ifdef MID_MUSIC
   2.260  #ifdef USE_TIMIDITY_MIDI
   2.261 @@ -708,23 +542,12 @@
   2.262  		}
   2.263  	} else
   2.264  #endif
   2.265 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
   2.266 +#ifdef MOD_MUSIC
   2.267  	if ( 1 ) {
   2.268  		music->type = MUS_MOD;
   2.269 -		music->data.module = MikMod_LoadSong((char *)file, 64);
   2.270 +		music->data.module = MOD_new(file);
   2.271  		if ( music->data.module == NULL ) {
   2.272 -			Mix_SetError("%s", MikMod_strerror(MikMod_errno));
   2.273  			music->error = 1;
   2.274 -		} else {
   2.275 -			/* Stop implicit looping, fade out and other flags. */
   2.276 -			music->data.module->extspd  = 1;
   2.277 -			music->data.module->panflag = 1;
   2.278 -			music->data.module->wrap    = 0;
   2.279 -			music->data.module->loop    = 1;
   2.280 -#if 0 /* Don't set fade out by default - unfortunately there's no real way
   2.281 -         to query the status of the song or set trigger actions.  Hum. */
   2.282 -			music->data.module->fadeout = 1;
   2.283 -#endif
   2.284  		}
   2.285  	} else
   2.286  #endif
   2.287 @@ -768,9 +591,9 @@
   2.288  				WAVStream_FreeSong(music->data.wave);
   2.289  				break;
   2.290  #endif
   2.291 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
   2.292 +#ifdef MOD_MUSIC
   2.293  			case MUS_MOD:
   2.294 -				MikMod_FreeSong(music->data.module);
   2.295 +				MOD_delete(music->data.module);
   2.296  				break;
   2.297  #endif
   2.298  #ifdef MID_MUSIC
   2.299 @@ -864,9 +687,9 @@
   2.300  		WAVStream_Start(music->data.wave);
   2.301  		break;
   2.302  #endif
   2.303 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
   2.304 +#ifdef MOD_MUSIC
   2.305  	    case MUS_MOD:
   2.306 -		Player_Start(music->data.module);
   2.307 +		MOD_play(music->data.module);
   2.308  		/* Player_SetVolume() does nothing before Player_Start() */
   2.309  		music_internal_initialize_volume();
   2.310  		break;
   2.311 @@ -980,9 +803,9 @@
   2.312  	int retval = 0;
   2.313  
   2.314  	switch (music_playing->type) {
   2.315 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
   2.316 +#ifdef MOD_MUSIC
   2.317  	    case MUS_MOD:
   2.318 -		Player_SetPosition((UWORD)position);
   2.319 +		MOD_jump_to_time(music_playing->data.module, position);
   2.320  		break;
   2.321  #endif
   2.322  #ifdef OGG_MUSIC
   2.323 @@ -1060,9 +883,9 @@
   2.324  		WAVStream_SetVolume(volume);
   2.325  		break;
   2.326  #endif
   2.327 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
   2.328 +#ifdef MOD_MUSIC
   2.329  	    case MUS_MOD:
   2.330 -		Player_SetVolume((SWORD)volume);
   2.331 +		MOD_setvolume(music_playing->data.module, volume);
   2.332  		break;
   2.333  #endif
   2.334  #ifdef MID_MUSIC
   2.335 @@ -1138,9 +961,9 @@
   2.336  		WAVStream_Stop();
   2.337  		break;
   2.338  #endif
   2.339 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
   2.340 +#ifdef MOD_MUSIC
   2.341  	    case MUS_MOD:
   2.342 -		Player_Stop();
   2.343 +		MOD_stop(music_playing->data.module);
   2.344  		break;
   2.345  #endif
   2.346  #ifdef MID_MUSIC
   2.347 @@ -1284,9 +1107,9 @@
   2.348  		}
   2.349  		break;
   2.350  #endif
   2.351 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
   2.352 +#ifdef MOD_MUSIC
   2.353  	    case MUS_MOD:
   2.354 -		if ( ! Player_Active() ) {
   2.355 +		if ( ! MOD_playing(music_playing->data.module) ) {
   2.356  			playing = 0;
   2.357  		}
   2.358  		break;
   2.359 @@ -1371,32 +1194,13 @@
   2.360  	return(0);
   2.361  }
   2.362  
   2.363 -#ifdef LIBMIKMOD_MUSIC
   2.364 -static int _pl_synchro_value;
   2.365 -void Player_SetSynchroValue(int i)
   2.366 -{
   2.367 -	fprintf(stderr,"SDL_mixer: Player_SetSynchroValue is not supported.\n");
   2.368 -	_pl_synchro_value = i;
   2.369 -}
   2.370 -
   2.371 -int Player_GetSynchroValue(void)
   2.372 -{
   2.373 -	fprintf(stderr,"SDL_mixer: Player_GetSynchroValue is not supported.\n");
   2.374 -	return _pl_synchro_value;
   2.375 -}
   2.376 -#endif
   2.377 -
   2.378  int Mix_SetSynchroValue(int i)
   2.379  {
   2.380  	if ( music_playing && ! music_stopped ) {
   2.381  		switch (music_playing->type) {
   2.382 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
   2.383 +#ifdef MOD_MUSIC
   2.384  		    case MUS_MOD:
   2.385 -			if ( ! Player_Active() ) {
   2.386 -				return(-1);
   2.387 -			}
   2.388 -			Player_SetSynchroValue(i);
   2.389 -			return 0;
   2.390 +			return MOD_SetSynchroValue(i);
   2.391  			break;
   2.392  #endif
   2.393  		    default:
   2.394 @@ -1412,12 +1216,9 @@
   2.395  {
   2.396  	if ( music_playing && ! music_stopped ) {
   2.397  		switch (music_playing->type) {
   2.398 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
   2.399 +#ifdef MOD_MUSIC
   2.400  		    case MUS_MOD:
   2.401 -			if ( ! Player_Active() ) {
   2.402 -				return(-1);
   2.403 -			}
   2.404 -			return Player_GetSynchroValue();
   2.405 +			return MOD_GetSynchroValue();
   2.406  			break;
   2.407  #endif
   2.408  		    default:
   2.409 @@ -1437,12 +1238,8 @@
   2.410  #ifdef CMD_MUSIC
   2.411  	Mix_SetMusicCMD(NULL);
   2.412  #endif
   2.413 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
   2.414 -	MikMod_Exit();
   2.415 -# ifndef LIBMIKMOD_MUSIC
   2.416 -	MikMod_UnregisterAllLoaders();
   2.417 -	MikMod_UnregisterAllDrivers();
   2.418 -# endif
   2.419 +#ifdef MOD_MUSIC
   2.420 +	MOD_exit();
   2.421  #endif
   2.422  #ifdef MID_MUSIC
   2.423  # ifdef USE_TIMIDITY_MIDI
   2.424 @@ -1456,72 +1253,6 @@
   2.425  	num_decoders = 0;
   2.426  }
   2.427  
   2.428 -# ifdef LIBMIKMOD_MUSIC
   2.429 -typedef struct
   2.430 -{
   2.431 -	MREADER mr;
   2.432 -	int offset;
   2.433 -	int eof;
   2.434 -	SDL_RWops *rw;
   2.435 -} LMM_MREADER;
   2.436 -BOOL LMM_Seek(struct MREADER *mr,long to,int dir)
   2.437 -{
   2.438 -	int at;
   2.439 -	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
   2.440 -	if(dir==SEEK_SET)
   2.441 -		to+=lmmmr->offset;
   2.442 -	at=SDL_RWseek(lmmmr->rw, to, dir);
   2.443 -	return at<lmmmr->offset;
   2.444 -}
   2.445 -long LMM_Tell(struct MREADER *mr)
   2.446 -{
   2.447 -	int at;
   2.448 -	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
   2.449 -	at=SDL_RWtell(lmmmr->rw)-lmmmr->offset;
   2.450 -	return at;
   2.451 -}
   2.452 -BOOL LMM_Read(struct MREADER *mr,void *buf,size_t sz)
   2.453 -{
   2.454 -	int got;
   2.455 -	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
   2.456 -	got=SDL_RWread(lmmmr->rw, buf, sz, 1);
   2.457 -	return got;
   2.458 -}
   2.459 -int LMM_Get(struct MREADER *mr)
   2.460 -{
   2.461 -	unsigned char c;
   2.462 -	int i=EOF;
   2.463 -	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
   2.464 -	if(SDL_RWread(lmmmr->rw,&c,1,1))
   2.465 -		i=c;
   2.466 -	return i;
   2.467 -}
   2.468 -BOOL LMM_Eof(struct MREADER *mr)
   2.469 -{
   2.470 -	int offset;
   2.471 -	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
   2.472 -	offset=LMM_Tell(mr);
   2.473 -	return offset>=lmmmr->eof;
   2.474 -}
   2.475 -MODULE *MikMod_LoadSongRW(SDL_RWops *rw, int maxchan)
   2.476 -{
   2.477 -	LMM_MREADER lmmmr = {
   2.478 -		{ LMM_Seek, LMM_Tell, LMM_Read, LMM_Get, LMM_Eof },
   2.479 -		0,
   2.480 -		0,
   2.481 -		0
   2.482 -	};
   2.483 -	MODULE *m;
   2.484 -        lmmmr.rw = rw;
   2.485 -	lmmmr.offset=SDL_RWtell(rw);
   2.486 -	SDL_RWseek(rw,0,SEEK_END);
   2.487 -	lmmmr.eof=SDL_RWtell(rw);
   2.488 -	SDL_RWseek(rw,lmmmr.offset,SEEK_SET);
   2.489 -	m=Player_LoadGeneric((MREADER*)&lmmmr,maxchan,0);
   2.490 -	return m;
   2.491 -}
   2.492 -# endif
   2.493 -
   2.494  Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw)
   2.495  {
   2.496  	Uint8 magic[5];	  /*Apparently there is no way to check if the file is really a MOD,*/
   2.497 @@ -1644,23 +1375,12 @@
   2.498  #endif
   2.499  	} else
   2.500  #endif
   2.501 -#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
   2.502 +#ifdef MOD_MUSIC
   2.503  	if (1) {
   2.504  		music->type=MUS_MOD;
   2.505 -		music->data.module=MikMod_LoadSongRW(rw,64);
   2.506 -		if (music->data.module==NULL) {
   2.507 -			Mix_SetError("%s",MikMod_strerror(MikMod_errno));
   2.508 -			music->error=1;
   2.509 -		} else {
   2.510 -			/* Stop implicit looping, fade out and other flags. */
   2.511 -			music->data.module->extspd  = 1;
   2.512 -			music->data.module->panflag = 1;
   2.513 -			music->data.module->wrap    = 0;
   2.514 -			music->data.module->loop    = 0;
   2.515 -#if 0 /* Don't set fade out by default - unfortunately there's no real way
   2.516 -         to query the status of the song or set trigger actions.  Hum. */
   2.517 -			music->data.module->fadeout = 1;
   2.518 -#endif
   2.519 +		music->data.module = MOD_new_RW(rw);
   2.520 +		if ( music->data.module == NULL ) {
   2.521 +			music->error = 1;
   2.522  		}
   2.523  	} else
   2.524  #endif
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/music_mod.c	Sat Oct 03 08:09:27 2009 +0000
     3.3 @@ -0,0 +1,403 @@
     3.4 +/*
     3.5 +    SDL_mixer:  An audio mixer library based on the SDL library
     3.6 +    Copyright (C) 1997-2009 Sam Lantinga
     3.7 +
     3.8 +    This library is free software; you can redistribute it and/or
     3.9 +    modify it under the terms of the GNU Library General Public
    3.10 +    License as published by the Free Software Foundation; either
    3.11 +    version 2 of the License, or (at your option) any later version.
    3.12 +
    3.13 +    This library is distributed in the hope that it will be useful,
    3.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.16 +    Library General Public License for more details.
    3.17 +
    3.18 +    You should have received a copy of the GNU Library General Public
    3.19 +    License along with this library; if not, write to the Free
    3.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    3.21 +
    3.22 +    Sam Lantinga
    3.23 +    slouken@libsdl.org
    3.24 +*/
    3.25 +
    3.26 +/* $Id: music_mod.c 4211 2008-12-08 00:27:32Z slouken $ */
    3.27 +
    3.28 +#ifdef MOD_MUSIC
    3.29 +
    3.30 +/* This file supports MOD tracker music streams */
    3.31 +
    3.32 +#include "SDL_mixer.h"
    3.33 +/*#include "dynamic_mod.h"*/
    3.34 +#include "music_mod.h"
    3.35 +
    3.36 +#include "mikmod.h"
    3.37 +
    3.38 +#if defined(LIBMIKMOD_VERSION)                /* libmikmod 3.1.8 */
    3.39 +#define UNIMOD			MODULE
    3.40 +#define MikMod_Init()		MikMod_Init(NULL)
    3.41 +#define MikMod_LoadSong(a,b)	Player_Load(a,b,0)
    3.42 +#ifndef LIBMIKMOD_MUSIC
    3.43 +#define MikMod_LoadSongRW(a,b)	Player_LoadRW(a,b,0)
    3.44 +#endif
    3.45 +#define MikMod_FreeSong		Player_Free
    3.46 +   extern int MikMod_errno;
    3.47 +#else                                        /* old MikMod 3.0.3 */
    3.48 +#define MikMod_strerror(x)	_mm_errmsg[x])
    3.49 +#define MikMod_errno		_mm_errno
    3.50 +#endif
    3.51 +
    3.52 +#define SDL_SURROUND
    3.53 +#ifdef SDL_SURROUND
    3.54 +#define MAX_OUTPUT_CHANNELS 6
    3.55 +#else
    3.56 +#define MAX_OUTPUT_CHANNELS 2
    3.57 +#endif
    3.58 +
    3.59 +/* Reference for converting mikmod output to 4/6 channels */
    3.60 +static int current_output_channels;
    3.61 +static Uint16 current_output_format;
    3.62 +
    3.63 +static int music_swap8;
    3.64 +static int music_swap16;
    3.65 +
    3.66 +/* Initialize the Ogg Vorbis player, with the given mixer settings
    3.67 +   This function returns 0, or -1 if there was an error.
    3.68 + */
    3.69 +int MOD_init(SDL_AudioSpec *mixerfmt)
    3.70 +{
    3.71 +#ifdef LIBMIKMOD_MUSIC
    3.72 +	CHAR *list;
    3.73 +#endif
    3.74 +
    3.75 +	/* Set the MikMod music format */
    3.76 +	music_swap8 = 0;
    3.77 +	music_swap16 = 0;
    3.78 +	switch (mixerfmt->format) {
    3.79 +
    3.80 +		case AUDIO_U8:
    3.81 +		case AUDIO_S8: {
    3.82 +			if ( mixerfmt->format == AUDIO_S8 ) {
    3.83 +				music_swap8 = 1;
    3.84 +			}
    3.85 +			md_mode = 0;
    3.86 +		}
    3.87 +		break;
    3.88 +
    3.89 +		case AUDIO_S16LSB:
    3.90 +		case AUDIO_S16MSB: {
    3.91 +			/* See if we need to correct MikMod mixing */
    3.92 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
    3.93 +			if ( mixerfmt->format == AUDIO_S16MSB ) {
    3.94 +#else
    3.95 +			if ( mixerfmt->format == AUDIO_S16LSB ) {
    3.96 +#endif
    3.97 +				music_swap16 = 1;
    3.98 +			}
    3.99 +			md_mode = DMODE_16BITS;
   3.100 +		}
   3.101 +		break;
   3.102 +
   3.103 +		default: {
   3.104 +			Mix_SetError("Unknown hardware audio format");
   3.105 +			return -1;
   3.106 +		}
   3.107 +	}
   3.108 +	current_output_channels = mixerfmt->channels;
   3.109 +	current_output_format = mixerfmt->format;
   3.110 +	if ( mixerfmt->channels > 1 ) {
   3.111 +		if ( mixerfmt->channels > MAX_OUTPUT_CHANNELS ) {
   3.112 +			Mix_SetError("Hardware uses more channels than mixerfmt");
   3.113 +			return -1;
   3.114 +		}
   3.115 +		md_mode |= DMODE_STEREO;
   3.116 +	}
   3.117 +	md_mixfreq = mixerfmt->freq;
   3.118 +	md_device  = 0;
   3.119 +	md_volume  = 96;
   3.120 +	md_musicvolume = 128;
   3.121 +	md_sndfxvolume = 128;
   3.122 +	md_pansep  = 128;
   3.123 +	md_reverb  = 0;
   3.124 +	md_mode    |= DMODE_HQMIXER|DMODE_SOFT_MUSIC|DMODE_SURROUND;
   3.125 +#ifdef LIBMIKMOD_MUSIC
   3.126 +	list = MikMod_InfoDriver();
   3.127 +	if ( list )
   3.128 +	  free(list);
   3.129 +	else
   3.130 +#endif
   3.131 +	MikMod_RegisterDriver(&drv_nos);
   3.132 +#ifdef LIBMIKMOD_MUSIC
   3.133 +	list = MikMod_InfoLoader();
   3.134 +	if ( list )
   3.135 +	  free(list);
   3.136 +	else
   3.137 +#endif
   3.138 +	MikMod_RegisterAllLoaders();
   3.139 +	if ( MikMod_Init() ) {
   3.140 +		Mix_SetError("%s", MikMod_strerror(MikMod_errno));
   3.141 +		return -1;
   3.142 +	}
   3.143 +
   3.144 +	return 0;
   3.145 +}
   3.146 +
   3.147 +/* Uninitialize the music players */
   3.148 +void MOD_exit(void)
   3.149 +{
   3.150 +	MikMod_Exit();
   3.151 +#ifndef LIBMIKMOD_MUSIC
   3.152 +	MikMod_UnregisterAllLoaders();
   3.153 +	MikMod_UnregisterAllDrivers();
   3.154 +#endif
   3.155 +}
   3.156 +
   3.157 +/* Set the volume for a MOD stream */
   3.158 +void MOD_setvolume(MODULE *music, int volume)
   3.159 +{
   3.160 +	Player_SetVolume((SWORD)volume);
   3.161 +}
   3.162 +
   3.163 +/* Load a MOD stream from the given file */
   3.164 +MODULE *MOD_new(const char *file)
   3.165 +{
   3.166 +	SDL_RWops *rw;
   3.167 +
   3.168 +	rw = SDL_RWFromFile(file, "rb");
   3.169 +	if ( rw == NULL ) {
   3.170 +		/* FIXME: Free rw, need to free on delete */
   3.171 +		SDL_SetError("Couldn't open %s", file);
   3.172 +		return NULL;
   3.173 +	}
   3.174 +	return MOD_new_RW(rw);
   3.175 +}
   3.176 +
   3.177 +
   3.178 +#ifdef LIBMIKMOD_MUSIC
   3.179 +typedef struct
   3.180 +{
   3.181 +	MREADER mr;
   3.182 +	int offset;
   3.183 +	int eof;
   3.184 +	SDL_RWops *rw;
   3.185 +} LMM_MREADER;
   3.186 +
   3.187 +BOOL LMM_Seek(struct MREADER *mr,long to,int dir)
   3.188 +{
   3.189 +	int at;
   3.190 +	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
   3.191 +	if(dir==SEEK_SET)
   3.192 +		to+=lmmmr->offset;
   3.193 +	at=SDL_RWseek(lmmmr->rw, to, dir);
   3.194 +	return at<lmmmr->offset;
   3.195 +}
   3.196 +long LMM_Tell(struct MREADER *mr)
   3.197 +{
   3.198 +	int at;
   3.199 +	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
   3.200 +	at=SDL_RWtell(lmmmr->rw)-lmmmr->offset;
   3.201 +	return at;
   3.202 +}
   3.203 +BOOL LMM_Read(struct MREADER *mr,void *buf,size_t sz)
   3.204 +{
   3.205 +	int got;
   3.206 +	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
   3.207 +	got=SDL_RWread(lmmmr->rw, buf, sz, 1);
   3.208 +	return got;
   3.209 +}
   3.210 +int LMM_Get(struct MREADER *mr)
   3.211 +{
   3.212 +	unsigned char c;
   3.213 +	int i=EOF;
   3.214 +	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
   3.215 +	if(SDL_RWread(lmmmr->rw,&c,1,1))
   3.216 +		i=c;
   3.217 +	return i;
   3.218 +}
   3.219 +BOOL LMM_Eof(struct MREADER *mr)
   3.220 +{
   3.221 +	int offset;
   3.222 +	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
   3.223 +	offset=LMM_Tell(mr);
   3.224 +	return offset>=lmmmr->eof;
   3.225 +}
   3.226 +MODULE *MikMod_LoadSongRW(SDL_RWops *rw, int maxchan)
   3.227 +{
   3.228 +	LMM_MREADER lmmmr = {
   3.229 +		{ LMM_Seek, LMM_Tell, LMM_Read, LMM_Get, LMM_Eof },
   3.230 +		0,
   3.231 +		0,
   3.232 +		0
   3.233 +	};
   3.234 +	MODULE *m;
   3.235 +        lmmmr.rw = rw;
   3.236 +	lmmmr.offset=SDL_RWtell(rw);
   3.237 +	SDL_RWseek(rw,0,SEEK_END);
   3.238 +	lmmmr.eof=SDL_RWtell(rw);
   3.239 +	SDL_RWseek(rw,lmmmr.offset,SEEK_SET);
   3.240 +	m=Player_LoadGeneric((MREADER*)&lmmmr,maxchan,0);
   3.241 +	return m;
   3.242 +}
   3.243 +#endif
   3.244 +
   3.245 +/* Load a MOD stream from an SDL_RWops object */
   3.246 +MODULE *MOD_new_RW(SDL_RWops *rw)
   3.247 +{
   3.248 +	MODULE *module = MikMod_LoadSongRW(rw,64);
   3.249 +	if (!module) {
   3.250 +		Mix_SetError("%s", MikMod_strerror(MikMod_errno));
   3.251 +		return NULL;
   3.252 +	}
   3.253 +
   3.254 +	/* Stop implicit looping, fade out and other flags. */
   3.255 +	module->extspd  = 1;
   3.256 +	module->panflag = 1;
   3.257 +	module->wrap    = 0;
   3.258 +	module->loop    = 0;
   3.259 +#if 0 /* Don't set fade out by default - unfortunately there's no real way
   3.260 +to query the status of the song or set trigger actions.  Hum. */
   3.261 +	module->fadeout = 1;
   3.262 +#endif
   3.263 +	return module;
   3.264 +}
   3.265 +
   3.266 +/* Start playback of a given MOD stream */
   3.267 +void MOD_play(MODULE *music)
   3.268 +{
   3.269 +	Player_Start(music);
   3.270 +}
   3.271 +
   3.272 +/* Return non-zero if a stream is currently playing */
   3.273 +int MOD_playing(MODULE *music)
   3.274 +{
   3.275 +	return Player_Active();
   3.276 +}
   3.277 +
   3.278 +/* Play some of a stream previously started with MOD_play() */
   3.279 +int MOD_playAudio(MODULE *music, Uint8 *stream, int len)
   3.280 +{
   3.281 +	if (current_output_channels > 2) {
   3.282 +		int small_len = 2 * len / current_output_channels;
   3.283 +		int i;
   3.284 +		Uint8 *src, *dst;
   3.285 +
   3.286 +		VC_WriteBytes((SBYTE *)stream, small_len);
   3.287 +		/* and extend to len by copying channels */
   3.288 +		src = stream + small_len;
   3.289 +		dst = stream + len;
   3.290 +
   3.291 +		switch (current_output_format & 0xFF) {
   3.292 +			case 8:
   3.293 +				for ( i=small_len/2; i; --i ) {
   3.294 +					src -= 2;
   3.295 +					dst -= current_output_channels;
   3.296 +					dst[0] = src[0];
   3.297 +					dst[1] = src[1];
   3.298 +					dst[2] = src[0];
   3.299 +					dst[3] = src[1];
   3.300 +					if (current_output_channels == 6) {
   3.301 +						dst[4] = src[0];
   3.302 +						dst[5] = src[1];
   3.303 +					}
   3.304 +				}
   3.305 +				break;
   3.306 +			case 16:
   3.307 +				for ( i=small_len/4; i; --i ) {
   3.308 +					src -= 4;
   3.309 +					dst -= 2 * current_output_channels;
   3.310 +					dst[0] = src[0];
   3.311 +					dst[1] = src[1];
   3.312 +					dst[2] = src[2];
   3.313 +					dst[3] = src[3];
   3.314 +					dst[4] = src[0];
   3.315 +					dst[5] = src[1];
   3.316 +					dst[6] = src[2];
   3.317 +					dst[7] = src[3];
   3.318 +					if (current_output_channels == 6) {
   3.319 +						dst[8] = src[0];
   3.320 +						dst[9] = src[1];
   3.321 +						dst[10] = src[2];
   3.322 +						dst[11] = src[3];
   3.323 +					}
   3.324 +				}
   3.325 +				break;
   3.326 +		}
   3.327 +	} else {
   3.328 +		VC_WriteBytes((SBYTE *)stream, len);
   3.329 +	}
   3.330 +	if ( music_swap8 ) {
   3.331 +		Uint8 *dst;
   3.332 +		int i;
   3.333 +
   3.334 +		dst = stream;
   3.335 +		for ( i=len; i; --i ) {
   3.336 +			*dst++ ^= 0x80;
   3.337 +		}
   3.338 +	} else
   3.339 +	if ( music_swap16 ) {
   3.340 +		Uint8 *dst, tmp;
   3.341 +		int i;
   3.342 +
   3.343 +		dst = stream;
   3.344 +		for ( i=(len/2); i; --i ) {
   3.345 +			tmp = dst[0];
   3.346 +			dst[0] = dst[1];
   3.347 +			dst[1] = tmp;
   3.348 +			dst += 2;
   3.349 +		}
   3.350 +	}
   3.351 +	return 0;
   3.352 +}
   3.353 +
   3.354 +/* Stop playback of a stream previously started with MOD_play() */
   3.355 +void MOD_stop(MODULE *music)
   3.356 +{
   3.357 +	Player_Stop();
   3.358 +}
   3.359 +
   3.360 +/* Close the given MOD stream */
   3.361 +void MOD_delete(MODULE *music)
   3.362 +{
   3.363 +	MikMod_FreeSong(music);
   3.364 +}
   3.365 +
   3.366 +/* Jump (seek) to a given position (time is in seconds) */
   3.367 +void MOD_jump_to_time(MODULE *music, double time)
   3.368 +{
   3.369 +	Player_SetPosition((UWORD)time);
   3.370 +}
   3.371 +
   3.372 +#ifdef LIBMIKMOD_MUSIC
   3.373 +static int _pl_synchro_value;
   3.374 +void Player_SetSynchroValue(int i)
   3.375 +{
   3.376 +	fprintf(stderr,"SDL_mixer: Player_SetSynchroValue is not supported.\n");
   3.377 +	_pl_synchro_value = i;
   3.378 +}
   3.379 +
   3.380 +int Player_GetSynchroValue(void)
   3.381 +{
   3.382 +	fprintf(stderr,"SDL_mixer: Player_GetSynchroValue is not supported.\n");
   3.383 +	return _pl_synchro_value;
   3.384 +}
   3.385 +#endif
   3.386 +
   3.387 +/* Set the MOD synchronization value */
   3.388 +int MOD_SetSynchroValue(int i)
   3.389 +{
   3.390 +	if ( ! Player_Active() ) {
   3.391 +		return(-1);
   3.392 +	}
   3.393 +	Player_SetSynchroValue(i);
   3.394 +	return 0;
   3.395 +}
   3.396 +
   3.397 +/* Get the MOD synchronization value */
   3.398 +int MOD_GetSynchroValue(void)
   3.399 +{
   3.400 +	if ( ! Player_Active() ) {
   3.401 +		return(-1);
   3.402 +	}
   3.403 +	return Player_GetSynchroValue();
   3.404 +}
   3.405 +
   3.406 +#endif /* MOD_MUSIC */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/music_mod.h	Sat Oct 03 08:09:27 2009 +0000
     4.3 @@ -0,0 +1,72 @@
     4.4 +/*
     4.5 +    SDL_mixer:  An audio mixer library based on the SDL library
     4.6 +    Copyright (C) 1997-2009 Sam Lantinga
     4.7 +
     4.8 +    This library is free software; you can redistribute it and/or
     4.9 +    modify it under the terms of the GNU Library General Public
    4.10 +    License as published by the Free Software Foundation; either
    4.11 +    version 2 of the License, or (at your option) any later version.
    4.12 +
    4.13 +    This library is distributed in the hope that it will be useful,
    4.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    4.16 +    Library General Public License for more details.
    4.17 +
    4.18 +    You should have received a copy of the GNU Library General Public
    4.19 +    License along with this library; if not, write to the Free
    4.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    4.21 +
    4.22 +    Sam Lantinga
    4.23 +    slouken@libsdl.org
    4.24 +*/
    4.25 +
    4.26 +/* $Id: music_mod.h 4211 2008-12-08 00:27:32Z slouken $ */
    4.27 +
    4.28 +#ifdef MOD_MUSIC
    4.29 +
    4.30 +/* This file supports MOD tracker music streams */
    4.31 +
    4.32 +struct MODULE;
    4.33 +
    4.34 +/* Initialize the Ogg Vorbis player, with the given mixer settings
    4.35 +   This function returns 0, or -1 if there was an error.
    4.36 + */
    4.37 +extern int MOD_init(SDL_AudioSpec *mixer);
    4.38 +
    4.39 +/* Uninitialize the music players */
    4.40 +extern void MOD_exit(void);
    4.41 +
    4.42 +/* Set the volume for a MOD stream */
    4.43 +extern void MOD_setvolume(struct MODULE *music, int volume);
    4.44 +
    4.45 +/* Load a MOD stream from the given file */
    4.46 +extern struct MODULE *MOD_new(const char *file);
    4.47 +
    4.48 +/* Load a MOD stream from an SDL_RWops object */
    4.49 +extern struct MODULE *MOD_new_RW(SDL_RWops *rw);
    4.50 +
    4.51 +/* Start playback of a given MOD stream */
    4.52 +extern void MOD_play(struct MODULE *music);
    4.53 +
    4.54 +/* Return non-zero if a stream is currently playing */
    4.55 +extern int MOD_playing(struct MODULE *music);
    4.56 +
    4.57 +/* Play some of a stream previously started with MOD_play() */
    4.58 +extern int MOD_playAudio(struct MODULE *music, Uint8 *stream, int len);
    4.59 +
    4.60 +/* Stop playback of a stream previously started with MOD_play() */
    4.61 +extern void MOD_stop(struct MODULE *music);
    4.62 +
    4.63 +/* Close the given MOD stream */
    4.64 +extern void MOD_delete(struct MODULE *music);
    4.65 +
    4.66 +/* Jump (seek) to a given position (time is in seconds) */
    4.67 +extern void MOD_jump_to_time(struct MODULE *music, double time);
    4.68 +
    4.69 +/* Set the MOD synchronization value */
    4.70 +extern int MOD_SetSynchroValue(int i);
    4.71 +
    4.72 +/* Get the MOD synchronization value */
    4.73 +extern int MOD_GetSynchroValue(void);
    4.74 +
    4.75 +#endif /* MOD_MUSIC */