load_aiff.c
changeset 111 3b709e102787
child 114 83ab4ef4458b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/load_aiff.c	Tue Sep 11 18:24:38 2001 +0000
     1.3 @@ -0,0 +1,203 @@
     1.4 +/*
     1.5 +    SDL_mixer:  An audio mixer library based on the SDL library
     1.6 +    Copyright (C) 1997-2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    This is the source needed to decode an AIFF file into a waveform.
    1.23 +    It's pretty straightforward once you get going. The only
    1.24 +    externally-callable function is Mix_LoadAIFF_RW(), which is meant to
    1.25 +    act as identically to SDL_LoadWAV_RW() as possible.
    1.26 +
    1.27 +    This file by Torbjörn Andersson (torbjorn.andersson@eurotime.se)
    1.28 +*/
    1.29 +
    1.30 +#include "SDL_mutex.h"
    1.31 +#include "SDL_endian.h"
    1.32 +#include "SDL_timer.h"
    1.33 +
    1.34 +#include "SDL_mixer.h"
    1.35 +#include "load_aiff.h"
    1.36 +
    1.37 +/*********************************************/
    1.38 +/* Define values for AIFF (IFF audio) format */
    1.39 +/*********************************************/
    1.40 +#define FORM		0x4d524f46		/* "FORM" */
    1.41 +#define AIFF		0x46464941		/* "AIFF" */
    1.42 +#define SSND		0x444e5353		/* "SSND" */
    1.43 +#define COMM		0x4d4d4f43		/* "COMM" */
    1.44 +
    1.45 +/* This function was taken from libsndfile. I don't pretend to fully
    1.46 + * understand it.
    1.47 + */
    1.48 +
    1.49 +static Uint32 SANE_to_Uint32 (Uint8 *sanebuf)
    1.50 +{
    1.51 +	/* Is the frequency outside of what we can represent with Uint32? */
    1.52 +	if ( (sanebuf[0] & 0x80) || (sanebuf[0] <= 0x3F) || (sanebuf[0] > 0x40)
    1.53 +		|| (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C) )
    1.54 +		return 0;
    1.55 +
    1.56 +	return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7)
    1.57 +		| (sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
    1.58 +}
    1.59 +
    1.60 +/* This function is based on SDL_LoadWAV_RW(). */
    1.61 +
    1.62 +SDL_AudioSpec *Mix_LoadAIFF_RW (SDL_RWops *src, int freesrc,
    1.63 +	SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
    1.64 +{
    1.65 +	int was_error;
    1.66 +	int found_SSND;
    1.67 +	int found_COMM;
    1.68 +	long start;
    1.69 +
    1.70 +	Uint32 chunk_type;
    1.71 +	Uint32 chunk_length;
    1.72 +	long next_chunk;
    1.73 +
    1.74 +	/* AIFF magic header */
    1.75 +	Uint32 FORMchunk;
    1.76 +	Uint32 AIFFmagic;
    1.77 +
    1.78 +	/* SSND chunk */
    1.79 +	Uint32 offset;
    1.80 +	Uint32 blocksize;
    1.81 +
    1.82 +	/* COMM format chunk */
    1.83 +	Uint16 channels;
    1.84 +	Uint32 numsamples;
    1.85 +	Uint16 samplesize;
    1.86 +	Uint8 sane_freq[10];
    1.87 +	Uint32 frequency;
    1.88 +
    1.89 +	/* Make sure we are passed a valid data source */
    1.90 +	was_error = 0;
    1.91 +	if ( src == NULL ) {
    1.92 +		was_error = 1;
    1.93 +		goto done;
    1.94 +	}
    1.95 +
    1.96 +	FORMchunk	= SDL_ReadLE32(src);
    1.97 +	chunk_length	= SDL_ReadBE32(src);
    1.98 +	if ( chunk_length == AIFF ) { /* The FORMchunk has already been read */
    1.99 +		AIFFmagic    = chunk_length;
   1.100 +		chunk_length = FORMchunk;
   1.101 +		FORMchunk    = FORM;
   1.102 +	} else {
   1.103 +		AIFFmagic    = SDL_ReadLE32(src);
   1.104 +	}
   1.105 +	if ( (FORMchunk != FORM) || (AIFFmagic != AIFF) ) {
   1.106 +		SDL_SetError("Unrecognized file type (not AIFF)");
   1.107 +		was_error = 1;
   1.108 +		goto done;
   1.109 +	}
   1.110 +
   1.111 +	/* TODO: Better santity-checking. */
   1.112 +
   1.113 +	found_SSND = 0;
   1.114 +	found_COMM = 0;
   1.115 +
   1.116 +	do {
   1.117 +		chunk_type	= SDL_ReadLE32(src);
   1.118 +		chunk_length	= SDL_ReadBE32(src);
   1.119 +		next_chunk	= SDL_RWtell(src) + chunk_length;
   1.120 +
   1.121 +		/* Paranoia to avoid infinite loops */
   1.122 +		if (chunk_length == 0)
   1.123 +			break;
   1.124 +
   1.125 +		switch (chunk_type) {
   1.126 +			case SSND:
   1.127 +				found_SSND	= 1;
   1.128 +				offset		= SDL_ReadBE32(src);
   1.129 +				blocksize	= SDL_ReadBE32(src);
   1.130 +				start		= SDL_RWtell(src) + offset;
   1.131 +				break;
   1.132 +
   1.133 +			case COMM:
   1.134 +				found_COMM	= 1;
   1.135 +				channels	= SDL_ReadBE16(src);
   1.136 +				numsamples	= SDL_ReadBE32(src);
   1.137 +				samplesize	= SDL_ReadBE16(src);
   1.138 +				SDL_RWread(src, sane_freq, sizeof(sane_freq), 1);
   1.139 +				frequency	= SANE_to_Uint32(sane_freq);
   1.140 +				if (frequency == 0) {
   1.141 +					SDL_SetError("Bad AIFF sample frequency");
   1.142 +					was_error = 1;
   1.143 +					goto done;
   1.144 +				}
   1.145 +				break;
   1.146 +
   1.147 +			default:
   1.148 +				break;
   1.149 +		}
   1.150 +	} while ( ( !found_SSND || !found_COMM )
   1.151 +		  && SDL_RWseek(src, next_chunk, SEEK_SET) != 1 );
   1.152 +
   1.153 +	if ( !found_SSND ) {
   1.154 +		SDL_SetError("Bad AIFF (no SSND chunk)");
   1.155 +		was_error = 1;
   1.156 +		goto done;
   1.157 +	}
   1.158 +
   1.159 +	if ( !found_COMM ) {
   1.160 +		SDL_SetError("Bad AIFF (no COMM chunk)");
   1.161 +		was_error = 1;
   1.162 +		goto done;
   1.163 +	}
   1.164 +
   1.165 +	/* Decode the audio data format */
   1.166 +	memset(spec, 0, sizeof(*spec));
   1.167 +	spec->freq = frequency;
   1.168 +	switch (samplesize) {
   1.169 +		case 8:
   1.170 +			spec->format = AUDIO_S8;
   1.171 +			break;
   1.172 +		case 16:
   1.173 +			spec->format = AUDIO_S16MSB;
   1.174 +			break;
   1.175 +		default:
   1.176 +			SDL_SetError("Unsupported AIFF samplesize");
   1.177 +			was_error = 1;
   1.178 +			goto done;
   1.179 +	}
   1.180 +	spec->channels = (Uint8) channels;
   1.181 +	spec->samples = 4096;		/* Good default buffer size */
   1.182 +
   1.183 +	*audio_len = channels * numsamples * (samplesize / 8);
   1.184 +	*audio_buf = (Uint8 *)malloc(*audio_len);
   1.185 +	if ( *audio_buf == NULL ) {
   1.186 +		SDL_SetError("Out of memory");
   1.187 +		return(NULL);
   1.188 +	}
   1.189 +	SDL_RWseek(src, start, SEEK_SET);
   1.190 +	if ( SDL_RWread(src, *audio_buf, *audio_len, 1) != 1 ) {
   1.191 +		SDL_SetError("Unable to read audio data");
   1.192 +		return(NULL);
   1.193 +	}
   1.194 +
   1.195 +	/* Don't return a buffer that isn't a multiple of samplesize */
   1.196 +	*audio_len &= ~((samplesize / 8) - 1);
   1.197 +
   1.198 +done:
   1.199 +	if ( freesrc && src ) {
   1.200 +		SDL_RWclose(src);
   1.201 +	}
   1.202 +	if ( was_error ) {
   1.203 +		spec = NULL;
   1.204 +	}
   1.205 +	return(spec);
   1.206 +}