/**************************************************************************/
/**************************************************************************/
/*
fileio.c: This file contains those functions needed to do file i/o for the
CSX analysis translator. It can read in mono or stereo, and output any
length packets.
*/
/**************************************************************************/
/*
Copyright, 1995 Atari Corporation.
Written for Atari by Highlight Software Solutions.

+++ MArch 1996 HMK 	Made changes for CSXJAG!
*/

/**************************************************************************/

#include "hdef.h"	/* this file contains the data types and constants. */

short endian_swap_word( short );

 /*
  * ---	External Data --------------------------------------------------------
  */    
extern short	*iobuf;
extern short	*iobufp;
extern long	iobuf_size;
extern HWORD 	intelFlag;


/*
 * HMK: The following function is not used anymore. We use MGetPCM()
 *      which gets the data from a memory block read in by csxjag.
 */
/********************************************************************/
/** HGetPCM: This function is called with the number of words 	   **/
/** (two bytes per word) PER AUDIO channel to be read from the     **/
/** input file used in the parameters. Mono or Stereo can be used. **/
/** Arguments: 							   **/
/**	fp: file pointer (previously opened)			   **/
/** 	channels: HMONO or HSTEREO				   **/
/** 	num: Number of words per channel to fetch from the file    **/
/** 	(maximum size: 256 words)				   **/
/** 	chan1: 1st channel word buffer big enough to read.	   **/
/** 	chan2: 2nd channel word buffer for stereo use 		   **/
/** Returns:							   **/
/**	Number of words fetched. If it is the EOF, Zeros will be   **/
/**	padded into the buffer and less than the total words may   **/
/** 	Be returned. This would imply either a disk error or, 	   **/
/**	More Likely, End of File (EOF).				   **/
/********************************************************************/
HINT	HGetPCM(LPHFILE fp, HINT channels, HINT num,
		LPHWORD chan1, LPHWORD chan2)
{
HINT	totalnum;		/* number of words to get from the file */
HLONG	wordsread;		/* number of words read */
HINT	wbuf[(2 * MAXREADSIZE) + 4];	/* Define word buffer size */
HINT	i,j;

	totalnum = num;
	if (channels == HSTEREO) totalnum = totalnum * 2;
	wordsread = fread(wbuf, 2, totalnum, fp);

	if (channels > 2) channels = 2;

	if (totalnum > (HINT)wordsread) {
		for (i = 0; i < num; i++) {
			chan1[i] = 0;
			if (channels == HSTEREO) chan2[i] = 0;
		}
	}
	i = 0;
	j = 0;
	while (i < (HINT)wordsread) {
		chan1[j] = wbuf[i++];
		if (channels == HSTEREO) chan2[j] = wbuf[i++];
		j++;
	}
if (channels == HSTEREO) return((HINT) (wordsread/2));
else return((HINT) wordsread);
} /* end of HGetPCM */




/********************************************************************/
/** HPutPacket: This function is called with the number of words   **/
/** (two bytes per word) and a buffer address to write to the      **/
/** output file fp.						   **/
/** Arguments: 							   **/
/**	fp: file pointer (previously opened)			   **/
/** 	num: Number of words per channel to write to the file      **/
/**	buf: buffer where the words to write are 		   **/
/** 	swap: The number of words at the beginning to swap	   **/
/** Returns:							   **/
/** 	Number of words written to the file.			   **/
/********************************************************************/
HINT	HPutPacket(LPHFILE fp, HINT num, LPHWORD buf,HINT swap)
{
HINT wordswrite;
LPHWORD	tbuf;
HINT	i,temp, temp1;

	if (swap) {
		/* need to swap the bytes in each word */
		tbuf = buf;
		for (i = 0; i < num; i++) {
			temp = *tbuf;
			temp1 = (temp >> 8) & 0x00ff;
			temp = (temp << 8) & 0xff00;
			temp |= temp1;
			*tbuf++ = temp;
		}
	} /* if swap needed */

	wordswrite = fwrite(buf, 2, num, fp);

	return(wordswrite);
} /* end of HPutPacket */




/*
 * HMK: This functions gets the data from memory (iobuf) instead of a file!
 *      But in general it's the same as HGetPCM (see above).
 */

HINT	MGetPCM(HINT channels, HINT num,
		LPHWORD chan1, LPHWORD chan2)
{
	HINT	totalnum;		/* number of words to get from the file */
	HLONG	wordsread;		/* number of words read */
	HINT	*wbuf;			/* Define word buffer size */
	HINT	i,j;

	totalnum = num;
	if (channels == HSTEREO) totalnum = totalnum * 2;

 	wbuf = iobufp;
	if ((iobufp + totalnum) > (iobuf + iobuf_size)) {
		wordsread = iobuf + iobuf_size - iobufp;
	} else {
		wordsread = totalnum;
		iobufp   += totalnum;
	}

	if (channels > 2) channels = 2;

	if (totalnum > (HINT)wordsread) {
		for (i = 0; i < num; i++) {
			chan1[i] = 0;
			if (channels == HSTEREO) chan2[i] = 0;
		}
	}
	i = 0;
	j = 0;
	while (i < (HINT)wordsread) {
		if (intelFlag)
			chan1[j] = wbuf[i++];
		else
			chan1[j] = endian_swap_word(wbuf[i++]);	
		if (channels == HSTEREO) {
			if (intelFlag)
				chan2[j] = wbuf[i++];
			else
				chan2[j] = endian_swap_word(wbuf[i++]);			
		}
		j++;
	}
	if (channels == HSTEREO)
		return((HINT) (wordsread/2));
	else
		return((HINT) wordsread);
} /* end of MGetPCM */




