/**************************************************************************/
/**************************************************************************/
/*
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.
*/
/**************************************************************************/

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

extern HWORD	intelFlag;


/********************************************************************/
/** 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) {
		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 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 */




short endian_swap_word( short i )
{
unsigned short a, b;

	a = (i >> 8) & 0x00ff;
	b = (i << 8) & 0xff00;
	return( a | b );
}

