Torbj�rn Andersson fixed AIFF music playing support
authorSam Lantinga <slouken@libsdl.org>
Sun, 19 Aug 2001 19:13:09 +0000
changeset 95871a7c1b93fb
parent 94 57a263c7f503
child 96 7d996a0c9353
Torbj�rn Andersson fixed AIFF music playing support
I removed malloc.h from timidity so it would build on MacOS X
CHANGES
timidity/instrum.c
timidity/mix.c
timidity/resample.c
wavestream.c
     1.1 --- a/CHANGES	Sat Aug 18 11:14:31 2001 +0000
     1.2 +++ b/CHANGES	Sun Aug 19 19:13:09 2001 +0000
     1.3 @@ -1,5 +1,7 @@
     1.4  
     1.5  1.2.1:
     1.6 +Torbjrn Andersson - Sun, 19 Aug 2001 16:03:30
     1.7 + * Fixed AIFF music playing support
     1.8  Sam Lantinga - Sat Aug 18 04:14:13 PDT 2001
     1.9   * Fixed building Ogg Vorbis support on Windows
    1.10  Ryan C. Gordon - Thu, 7 Jun 2001 13:15:51
     2.1 --- a/timidity/instrum.c	Sat Aug 18 11:14:31 2001 +0000
     2.2 +++ b/timidity/instrum.c	Sun Aug 19 19:13:09 2001 +0000
     2.3 @@ -25,12 +25,7 @@
     2.4  
     2.5  #include <stdio.h>
     2.6  #include <string.h>
     2.7 -
     2.8 -#if defined(__FreeBSD__) || defined (__WIN32__)
     2.9  #include <stdlib.h>
    2.10 -#else
    2.11 -#include <malloc.h>
    2.12 -#endif
    2.13  
    2.14  #include "config.h"
    2.15  #include "common.h"
     3.1 --- a/timidity/mix.c	Sat Aug 18 11:14:31 2001 +0000
     3.2 +++ b/timidity/mix.c	Sun Aug 19 19:13:09 2001 +0000
     3.3 @@ -23,7 +23,7 @@
     3.4  
     3.5  #include <math.h>
     3.6  #include <stdio.h>
     3.7 -#include <malloc.h>
     3.8 +#include <stdlib.h>
     3.9  
    3.10  #include "config.h"
    3.11  #include "common.h"
     4.1 --- a/timidity/resample.c	Sat Aug 18 11:14:31 2001 +0000
     4.2 +++ b/timidity/resample.c	Sun Aug 19 19:13:09 2001 +0000
     4.3 @@ -22,7 +22,7 @@
     4.4  
     4.5  #include <math.h>
     4.6  #include <stdio.h>
     4.7 -#include <malloc.h>
     4.8 +#include <stdlib.h>
     4.9  
    4.10  #include "config.h"
    4.11  #include "common.h"
     5.1 --- a/wavestream.c	Sat Aug 18 11:14:31 2001 +0000
     5.2 +++ b/wavestream.c	Sun Aug 19 19:13:09 2001 +0000
     5.3 @@ -337,49 +337,58 @@
     5.4  	return(wavefp);
     5.5  }
     5.6  
     5.7 -static double SANE_to_double(Uint32 l1, Uint32 l2, Uint16 s1)
     5.8 +/* I couldn't get SANE_to_double() to work, so I stole this from libsndfile.
     5.9 + * I don't pretend to fully understand it.
    5.10 + */
    5.11 +
    5.12 +static Uint32 SANE_to_Uint32 (Uint8 *sanebuf)
    5.13  {
    5.14 -	double d;
    5.15 -	struct almost_double {
    5.16 -		Uint32 hi, lo;
    5.17 -	} *dp = (struct almost_double *)&d;
    5.18 +	/* Negative number? */
    5.19 +	if (sanebuf[0] & 0x80)
    5.20 +		return 0;
    5.21  
    5.22 -	dp->hi = ((l1 << 4) & 0x3ff00000) | (l1 & 0xc0000000);
    5.23 -	dp->hi |= (l1 << 5) & 0xffff0;
    5.24 -	dp->hi |= (l2 >> 27) & 0x1f;
    5.25 -	dp->lo = (l2 << 5) & 0xffffffe0;
    5.26 -	dp->lo |= ((s1 >> 11) & 0x1f);
    5.27 -	return(d);
    5.28 +	/* Less than 1? */
    5.29 +	if (sanebuf[0] <= 0x3F)
    5.30 +		return 1;
    5.31 +
    5.32 +	/* Way too big? */
    5.33 +	if (sanebuf[0] > 0x40)
    5.34 +		return 0x4000000;
    5.35 +
    5.36 +	/* Still too big? */
    5.37 +	if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C)
    5.38 +		return 800000000;
    5.39 +
    5.40 +	return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7)
    5.41 +		| (sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
    5.42  }
    5.43  
    5.44  static FILE *LoadAIFFStream (const char *file, SDL_AudioSpec *spec,
    5.45  					long *start, long *stop)
    5.46  {
    5.47  	int was_error;
    5.48 +	int found_SSND;
    5.49 +	int found_COMM;
    5.50  	FILE *wavefp;
    5.51  	SDL_RWops *src;
    5.52  
    5.53 +	Uint32 chunk_type;
    5.54 +	Uint32 chunk_length;
    5.55 +	long next_chunk;
    5.56 +
    5.57  	/* AIFF magic header */
    5.58  	Uint32 FORMchunk;
    5.59 -	Uint32 chunklen;
    5.60  	Uint32 AIFFmagic;
    5.61  	/* SSND chunk        */
    5.62  	Uint32 SSNDchunk;
    5.63 -	Uint32 ssndlen;
    5.64  	Uint32 offset;
    5.65  	Uint32 blocksize;
    5.66  	/* COMM format chunk */
    5.67  	Uint32 COMMchunk;
    5.68 -	Uint32 commlen;
    5.69  	Uint16 channels;
    5.70  	Uint32 numsamples;
    5.71  	Uint16 samplesize;
    5.72 -	struct { /* plus a SANE format double precision number */
    5.73 -		Uint32 l1;
    5.74 -		Uint32 l2;
    5.75 -		Uint16 s1;
    5.76 -	} sane_freq;
    5.77 -
    5.78 +	Uint8 sane_freq[10];
    5.79  	Uint32 frequency;
    5.80  
    5.81  
    5.82 @@ -397,7 +406,7 @@
    5.83  
    5.84  	/* Check the magic header */
    5.85  	FORMchunk	= SDL_ReadLE32(src);
    5.86 -	chunklen	= SDL_ReadLE32(src);
    5.87 +	chunk_length	= SDL_ReadBE32(src);
    5.88  	AIFFmagic	= SDL_ReadLE32(src);
    5.89  	if ( (FORMchunk != FORM) || (AIFFmagic != AIFF) ) {
    5.90  		SDL_SetError("Unrecognized file type (not AIFF)");
    5.91 @@ -405,39 +414,60 @@
    5.92  		goto done;
    5.93  	}
    5.94  
    5.95 -	/* Read the SSND data chunk */
    5.96 -	SSNDchunk	= SDL_ReadLE32(src);
    5.97 -	if ( SSNDchunk != SSND ) {
    5.98 -		SDL_SetError("Unrecognized AIFF chunk (not SSND)");
    5.99 -		was_error = 1;
   5.100 -		goto done;
   5.101 +	/* From what I understand of the specification, chunks may appear in
   5.102 +         * any order, and we should just ignore unknown ones.
   5.103 +         */
   5.104 +
   5.105 +	found_SSND = 0;
   5.106 +	found_COMM = 0;
   5.107 +
   5.108 +	do {
   5.109 +	    chunk_type		= SDL_ReadLE32(src);
   5.110 +	    chunk_length	= SDL_ReadBE32(src);
   5.111 +	    next_chunk		= SDL_RWtell(src) + chunk_length;
   5.112 +
   5.113 +	    /* Paranoia to avoid infinite loops */
   5.114 +	    if (chunk_length == 0)
   5.115 +		break;
   5.116 +
   5.117 +            switch (chunk_type) {
   5.118 +		case SSND:
   5.119 +		    found_SSND		= 1;
   5.120 +		    offset		= SDL_ReadBE32(src);
   5.121 +		    blocksize		= SDL_ReadBE32(src);
   5.122 +		    *start		= SDL_RWtell(src) + offset;
   5.123 +		    break;
   5.124 +
   5.125 +		case COMM:
   5.126 +		    found_COMM		= 1;
   5.127 +
   5.128 +		    /* Read the audio data format chunk */
   5.129 +		    channels		= SDL_ReadBE16(src);
   5.130 +		    numsamples		= SDL_ReadBE32(src);
   5.131 +		    samplesize		= SDL_ReadBE16(src);
   5.132 +		    SDL_RWread(src, sane_freq, sizeof(sane_freq), 1);
   5.133 +		    frequency		= SANE_to_Uint32(sane_freq);
   5.134 +		    break;
   5.135 +
   5.136 +		default:
   5.137 +		    break;
   5.138 +	    }
   5.139 +	} while ((!found_SSND || !found_COMM)
   5.140 +		 && SDL_RWseek(src, next_chunk, SEEK_SET) != -1);
   5.141 +
   5.142 +	if (!found_SSND) {
   5.143 +	    SDL_SetError("Bad AIFF file (no SSND chunk)");
   5.144 +	    was_error = 1;
   5.145 +	    goto done;
   5.146  	}
   5.147 -	ssndlen		= SDL_ReadLE32(src);
   5.148 -	offset		= SDL_ReadLE32(src);
   5.149 -	blocksize	= SDL_ReadLE32(src);
   5.150 +		    
   5.151 +	if (!found_COMM) {
   5.152 +	    SDL_SetError("Bad AIFF file (no COMM chunk)");
   5.153 +	    was_error = 1;
   5.154 +	    goto done;
   5.155 +	}
   5.156  
   5.157 -	/* Fill in start and stop pointers, then seek to format chunk */
   5.158 -	ssndlen -= (2*sizeof(Uint32));
   5.159 -	*start = SDL_RWtell(src) + offset;
   5.160 -	*stop = SDL_RWtell(src) + ssndlen;
   5.161 -	SDL_RWseek(src, *stop, SEEK_SET);
   5.162 -
   5.163 -	/* Read the audio data format chunk */
   5.164 -	COMMchunk	= SDL_ReadLE32(src);
   5.165 -	if ( COMMchunk != COMM ) {
   5.166 -		SDL_SetError("Unrecognized AIFF chunk (not COMM)");
   5.167 -		was_error = 1;
   5.168 -		goto done;
   5.169 -	}
   5.170 -	commlen		= SDL_ReadLE32(src);
   5.171 -	channels	= SDL_ReadLE16(src);
   5.172 -	numsamples	= SDL_ReadLE32(src);
   5.173 -	samplesize	= SDL_ReadLE16(src);
   5.174 -	sane_freq.l1	= SDL_ReadLE32(src);
   5.175 -	sane_freq.l2	= SDL_ReadLE32(src);
   5.176 -	sane_freq.s1	= SDL_ReadLE16(src);
   5.177 -	frequency	= (Uint32)SANE_to_double(sane_freq.l1, sane_freq.l2,
   5.178 -								sane_freq.s1);
   5.179 +	*stop = *start + channels * numsamples * (samplesize / 8) - 1;
   5.180  
   5.181  	/* Decode the audio data format */
   5.182  	memset(spec, 0, (sizeof *spec));
   5.183 @@ -447,7 +477,7 @@
   5.184  			spec->format = AUDIO_U8;
   5.185  			break;
   5.186  		case 16:
   5.187 -			spec->format = AUDIO_S16;
   5.188 +			spec->format = AUDIO_S16MSB;
   5.189  			break;
   5.190  		default:
   5.191  			SDL_SetError("Unknown samplesize in data format");