/*
 * July 5, 1991
 * Copyright 1991 Lance Norskog And Sundry Contributors
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained. 
 * Lance Norskog And Sundry Contributors are not responsible for 
 * the consequences of using this software.
 */

/*
 * Sound Tools MAUD file format driver, by Lutz Vieweg 1993
 *
 * supports: mono and stereo, linear, a-lawa and u-law reading and writing
 *
 */

#include "st.h"
#include "libst.h"

#ifdef	SYSV
#include <unistd.h>
#endif
/* or wherever SEEK_CUR comes from */

/* Private data for MAUD file */
struct maudstuff { /* max. 100 bytes!!!! */
	unsigned long nsamples;
};

/*
 * Do anything required before you start reading samples.
 * Read file header. 
 *	Find out sampling rate, 
 *	size and style of samples, 
 *	mono/stereo/quad.
 */
int maudstartread(ft_t ft) 
{
	struct maudstuff * p = (struct maudstuff *) ft->priv;
	
	char buf[12];
	char *endptr;
	char *chunk_buf;
	
	unsigned short bitpersam;
	unsigned long nom;
	unsigned short denom;
	unsigned short chaninf;
	
	unsigned long chunksize;
	
	int channels;
	int littlendian = 0;
	int i;
	
	unsigned long chan1_pos;
	
	/* read FORM chunk */
	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "FORM", 4) != 0)
		return 0;
//		fail("MAUD: header does not begin with magic word 'FORM'");
	
	rblong(ft); /* totalsize */
	
	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "MAUD", 4) != 0)
		return 0;
//		fail("MAUD: 'FORM' chunk does not specify 'MAUD' as type");
	
	/* read chunks until 'BODY' (or end) */
	
	while (fread(buf,1,4,ft->fp) == 4 && strncmp(buf,"MDAT",4) != 0) {
		
		/*
		buf[4] = 0;
		report("chunk %s",buf);
		*/
		
		if (strncmp(buf,"MHDR",4) == 0) {
			
			chunksize = rblong(ft);
			if (chunksize != 8*4) 
				return 0;
//				fail ("MAUD: MHDR chunk has bad size");
			
			/* fseek(ft->fp,12,SEEK_CUR); */
			
			p->nsamples = rblong(ft); /* number of samples stored in MDAT */
			bitpersam = rbshort(ft);  /* number of bits per sample as stored in MDAT */
			rbshort(ft);              /* number of bits per sample after decompression */
			nom = rblong(ft);         /* clock source frequency */
			denom = rbshort(ft);       /* clock devide           */
			if (denom == 0) 
				return 0;
//				fail("MAUD: frequency denominator == 0, failed");
			
			ft->info.rate = nom / denom;
			
			chaninf = rbshort(ft); /* channel information */
			switch (chaninf) {
			case 0:
				ft->info.channels = 1;
				break;
			case 1:
				ft->info.channels = 2;
				break;
			default:
				return 0;
//				fail("MAUD: unsupported number of channels in file");
				break;
			}
			
			chaninf = rbshort(ft); /* number of channels (mono: 1, stereo: 2, ...) */
			if (chaninf != ft->info.channels) 
				return 0;
//				fail("MAUD: unsupported number of channels in file");
			
			chaninf = rbshort(ft); /* compression type */
			
			rblong(ft); /* rest of chunk, unused yet */
			rblong(ft);
			rblong(ft);
			
			if (bitpersam == 8 && chaninf == 0) {
				ft->info.size = BYTE;
				ft->info.style = UNSIGNED;
			}
			else if (bitpersam == 8 && chaninf == 2) {
				ft->info.size = BYTE;
				ft->info.style = ALAW;
			}
			else if (bitpersam == 8 && chaninf == 3) {
				ft->info.size = BYTE;
				ft->info.style = ULAW;
			}
			else if (bitpersam == 16 && chaninf == 0) {
				ft->info.size = WORD;
				ft->info.style = SIGN2;
			}
			else return 0;
//			else fail("MAUD: unsupported compression type detected");
			
			ft->comment = 0;
			
			continue;
		}
		
		if (strncmp(buf,"ANNO",4) == 0) {
			chunksize = rblong(ft);
			if (chunksize & 1)
				chunksize++;
			chunk_buf = (char *) malloc(chunksize + 1);
			if (fread(chunk_buf,1,(int)chunksize,ft->fp) 
					!= chunksize)
				return 0;
//				fail("MAUD: Unexpected EOF in ANNO header");
			chunk_buf[chunksize] = '\0';
//			report ("%s",chunk_buf);
			free(chunk_buf);
			
			continue;
		}
		
		/* some other kind of chunk */
		chunksize = rblong(ft);
		if (chunksize & 1)
			chunksize++;
		fseek(ft->fp,chunksize,SEEK_CUR);
		continue;
		
	}
	
	if (strncmp(buf,"MDAT",4) != 0) 
		return 0;
//		fail("MAUD: MDAT chunk not found");
	p->nsamples = rblong(ft);
	
	endptr = (char *) &littlendian;
	*endptr = 1;
	if (littlendian == 1) ft->swap = 1;

	return 1;
}

/*
 * Read up to len samples from file.
 * Convert to signed longs.
 * Place in buf[].
 * Return number of samples read.
 */

maudread(ft_t ft, long *buf, int len) 
{
	struct maudstuff * p = (struct maudstuff *) ft->priv;
	register int datum;
	int done = 0;
	
	if (ft->info.channels == 1) {
		if (ft->info.size == BYTE) {
			switch(ft->info.style) {
			case UNSIGNED:
				while(done < len) {
					datum = getc(ft->fp);
					if (feof(ft->fp)) return done;
					/* Convert to signed */
					datum ^= 128;
					/* scale signed up to long's range */
					*buf++ = LEFT(datum, 24);
					done++;
				}
				break;
			case ULAW:
				/* grab table from Posk stuff */
				while(done < len) {
					datum = getc(ft->fp);
					if (feof(ft->fp)) return done;
					datum = st_ulaw_to_linear(datum);
					/* scale signed up to long's range */
					*buf++ = LEFT(datum, 16);
					done++;
				}
				break;
			case ALAW:
				while(done < len) {
					datum = st_Alaw_to_linear((unsigned char) getc(ft->fp));
					if (feof(ft->fp)) return done;
					/* scale signed up to long's range */
					*buf++ = LEFT(datum, 16);
					done++;
				}
				break;
			}
		}
		else {
			while(done < len) {
				datum = rbshort(ft);
				if (feof(ft->fp)) return done;
				/* scale signed up to long's range */
				*buf++ = LEFT(datum, 16);
				done++;
			}
		}
	}
	else { /* stereo */
		if (ft->info.size == BYTE) {
			switch(ft->info.style) {
			case UNSIGNED:
				while(done < len) {
					datum = getc(ft->fp);
					if (feof(ft->fp)) return done;
					/* Convert to signed */
					datum ^= 128;
					/* scale signed up to long's range */
					*buf++ = LEFT(datum, 24);
					
					datum = getc(ft->fp);
					if (feof(ft->fp)) return done;
					/* Convert to signed */
					datum ^= 128;
					/* scale signed up to long's range */
					*buf++ = LEFT(datum, 24);
					done += 2;
				}
				break;
			case ULAW:
				/* grab table from Posk stuff */
				while(done < len) {
					datum = getc(ft->fp);
					if (feof(ft->fp)) return done;
					datum = st_ulaw_to_linear(datum);
					/* scale signed up to long's range */
					*buf++ = LEFT(datum, 16);
					
					datum = getc(ft->fp);
					if (feof(ft->fp)) return done;
					datum = st_ulaw_to_linear(datum);
					/* scale signed up to long's range */
					*buf++ = LEFT(datum, 16);
					done += 2;
				}
				break;
			case ALAW:
				while(done < len) {
					datum = st_Alaw_to_linear((unsigned char) getc(ft->fp));
					if (feof(ft->fp)) return done;
					/* scale signed up to long's range */
					*buf++ = LEFT(datum, 16);
					
					datum = st_Alaw_to_linear((unsigned char) getc(ft->fp));
					if (feof(ft->fp)) return done;
					/* scale signed up to long's range */
					*buf++ = LEFT(datum, 16);
					done += 2;
				}
				break;
			}
		}
		else {
			while(done < len) {
				datum = rbshort(ft);
				if (feof(ft->fp)) return done;
				/* scale signed up to long's range */
				*buf++ = LEFT(datum, 16);
				
				datum = rbshort(ft);
				if (feof(ft->fp)) return done;
				/* scale signed up to long's range */
				*buf++ = LEFT(datum, 16);
				done += 2;
			}
		}
	}
	return done;
}

/*
 * Do anything required when you stop reading samples.  
 * Don't close input file! 
 */
maudstopread(ft_t ft) 
{
}
