music.c
changeset 245 63b3650714de
parent 241 503416fca921
child 246 9fa5d0f9d042
     1.1 --- a/music.c	Fri Aug 20 19:32:09 2004 +0000
     1.2 +++ b/music.c	Sat Aug 21 12:27:02 2004 +0000
     1.3 @@ -31,6 +31,14 @@
     1.4  
     1.5  #include "SDL_mixer.h"
     1.6  
     1.7 +#define SDL_SURROUND
     1.8 +
     1.9 +#ifdef SDL_SURROUND
    1.10 +#define MAX_OUTPUT_CHANNELS 6
    1.11 +#else
    1.12 +#define MAX_OUTPUT_CHANNELS 2
    1.13 +#endif
    1.14 +
    1.15  /* The music command hack is UNIX specific */
    1.16  #ifndef unix
    1.17  #undef CMD_MUSIC
    1.18 @@ -131,6 +139,10 @@
    1.19  #endif
    1.20  #endif
    1.21  
    1.22 +/* Reference for converting mikmod output to 4/6 channels */
    1.23 +static int current_output_channels;
    1.24 +static Uint16 current_output_format;
    1.25 +
    1.26  /* Used to calculate fading steps */
    1.27  static int ms_per_step;
    1.28  
    1.29 @@ -210,7 +222,57 @@
    1.30  #endif
    1.31  #ifdef MOD_MUSIC
    1.32  			case MUS_MOD:
    1.33 -				VC_WriteBytes((SBYTE *)stream, len);
    1.34 +				if (current_output_channels > 2) {
    1.35 +					int small_len = 2 * len / current_output_channels;
    1.36 +					int i;
    1.37 +					Uint8 *src, *dst;
    1.38 +
    1.39 +					VC_WriteBytes((SBYTE *)stream, small_len);
    1.40 +					/* and extend to len by copying channels */
    1.41 +					src = stream + small_len;
    1.42 +					dst = stream + len;
    1.43 +
    1.44 +					switch (current_output_format & 0xFF) {
    1.45 +						case 8:
    1.46 +							for ( i=small_len/2; i; --i ) {
    1.47 +								src -= 2;
    1.48 +								dst -= current_output_channels;
    1.49 +								dst[0] = src[0];
    1.50 +								dst[1] = src[1];
    1.51 +								dst[2] = src[0];
    1.52 +								dst[3] = src[1];
    1.53 +								if (current_output_channels == 6) {
    1.54 +									dst[4] = src[0];
    1.55 +									dst[5] = src[1];
    1.56 +								}
    1.57 +							}
    1.58 +							break;
    1.59 +						case 16:
    1.60 +							for ( i=small_len/4; i; --i ) {
    1.61 +								src -= 4;
    1.62 +								dst -= 2 * current_output_channels;
    1.63 +								dst[0] = src[0];
    1.64 +								dst[1] = src[1];
    1.65 +								dst[2] = src[2];
    1.66 +								dst[3] = src[3];
    1.67 +								dst[4] = src[0];
    1.68 +								dst[5] = src[1];
    1.69 +								dst[6] = src[2];
    1.70 +								dst[7] = src[3];
    1.71 +								if (current_output_channels == 6) {
    1.72 +									dst[8] = src[0];
    1.73 +									dst[9] = src[1];
    1.74 +									dst[10] = src[2];
    1.75 +									dst[11] = src[3];
    1.76 +								}
    1.77 +							}
    1.78 +							break;
    1.79 +					}
    1.80 +
    1.81 +
    1.82 +
    1.83 +				}
    1.84 +				else VC_WriteBytes((SBYTE *)stream, len);
    1.85  				if ( music_swap8 ) {
    1.86  					Uint8 *dst;
    1.87  					int i;
    1.88 @@ -306,8 +368,10 @@
    1.89  			++music_error;
    1.90  		}
    1.91  	}
    1.92 +	current_output_channels = mixer->channels;
    1.93 +	current_output_format = mixer->format;
    1.94  	if ( mixer->channels > 1 ) {
    1.95 -		if ( mixer->channels > 2 ) {
    1.96 +		if ( mixer->channels > MAX_OUTPUT_CHANNELS ) {
    1.97  			Mix_SetError("Hardware uses more channels than mixer");
    1.98  			++music_error;
    1.99  		}
   1.100 @@ -385,7 +449,7 @@
   1.101  {
   1.102  	FILE *fp;
   1.103  	char *ext;
   1.104 -	Uint8 magic[5];
   1.105 +	Uint8 magic[5], moremagic[9];
   1.106  	Mix_Music *music;
   1.107  
   1.108  	/* Figure out what kind of file this is */
   1.109 @@ -397,7 +461,12 @@
   1.110  		Mix_SetError("Couldn't read from '%s'", file);
   1.111  		return(NULL);
   1.112  	}
   1.113 +	if (!fread(moremagic, 8, 1, fp)) {
   1.114 +		Mix_SetError("Couldn't read from '%s'", file);
   1.115 +		return(NULL);
   1.116 +	}
   1.117  	magic[4] = '\0';
   1.118 +	moremagic[8] = '\0';
   1.119  	fclose(fp);
   1.120  
   1.121  	/* Figure out the file extension, so we can determine the type */
   1.122 @@ -426,7 +495,7 @@
   1.123  	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
   1.124  	 */
   1.125  	if ( (ext && MIX_string_equals(ext, "WAV")) ||
   1.126 -	     (strcmp((char *)magic, "RIFF") == 0) ||
   1.127 +	     ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
   1.128  	     (strcmp((char *)magic, "FORM") == 0) ) {
   1.129  		music->type = MUS_WAV;
   1.130  		music->data.wave = WAVStream_LoadSong(file, (char *)magic);
   1.131 @@ -440,7 +509,9 @@
   1.132  	/* MIDI files have the magic four bytes "MThd" */
   1.133  	if ( (ext && MIX_string_equals(ext, "MID")) ||
   1.134  	     (ext && MIX_string_equals(ext, "MIDI")) ||
   1.135 -	     strcmp((char *)magic, "MThd") == 0 ) {
   1.136 +	     strcmp((char *)magic, "MThd") == 0  ||
   1.137 +	     ( strcmp((char *)magic, "RIFF") == 0  &&
   1.138 +	  	strcmp((char *)(moremagic+4), "RMID") == 0 ) ) {
   1.139  		music->type = MUS_MID;
   1.140  #ifdef USE_NATIVE_MIDI
   1.141    		if ( native_midi_ok ) {