/**********************************************************************

Copyright(c) Analog Devices, Inc. All Rights Reserved. 

This software is proprietary and confidential.  By using this software 
you agree to the terms of the associated Analog Devices License Agreement.  

$RCSfile: codec_1980.h,v $
$Revision: 1.2 $
$Date: 2008/06/26 06:04:19 $

Description:
	
******************************************************************************

Include files

*****************************************************************************/

#ifndef __AUDIOPLAYER_H__
#define __AUDIOPLAYER_H__

#include <mediaplayer.h>

/*****************************************************************************

Macros to adjust audio volume

****************************************************************************/
#define		VOLUME_STRIDE			0x0202
#define		VOLUME_MIN				0x7FFF
#define		VOLUME_MAX				0x6FEF

/*****************************************************************************

Macros to adjust Audio CODEC (AD1980) buffer memory allocation

****************************************************************************/

/* 
	AD1980 (AC'97) RFS is fixed at 48kHz and that generates a SPORT interrupt 
	approximately every 22.5us. 
   	This interrupt/callback overload can be adjusted by providing a 
   	bigger audio data frame to AC'97 driver.
   	For example, providing a audio data frame buffer which is 44 times of the 
   	base data frame size sets SPORT interrupt to occur every 1ms
*/

/* Audio frame size set for 1ms SPORT interrupt interval */
#define     AUDIO_FRAME_SIZE        	            (ADI_AD1980_DATA_FRAME_BASE_MEMORY * 44)

/* Number of DAC channels (Front Out Left, Front Out Right) */
/* Stereo mode */
#define		NUM_AUDIO_DAC_CHANNELS			        2

/* Number of ADC channels (Line In Left, Line In Right) */
#define		NUM_AUDIO_ADC_CHANNELS			        2

/* PCM sample resolution = 16 bits (2bytes) */
#define     PCM_SAMPLE_RESOLUTION_IN_BYTES          2

/* Base buffer size per DAC channel */
#define		BUFFER_SIZE_PER_AUDIO_DAC_CHANNEL	    (1024 * 1)	/* 1kB */
/* Base buffer size per ADC channel */
#define		BUFFER_SIZE_PER_AUDIO_ADC_CHANNEL       (1024 * 1)	/* 1kB */

/* Max Buffer size to hold Audio COCEC - DAC samples */
#define		AUDIO_CODEC_DAC_BUFFER_SIZE   		    (BUFFER_SIZE_PER_AUDIO_DAC_CHANNEL *\
                                                     NUM_AUDIO_DAC_CHANNELS *\
                                                     PCM_SAMPLE_RESOLUTION_IN_BYTES)
/* Max Buffer size to hold Audio COCEC - ADC samples */
#define		AUDIO_CODEC_ADC_BUFFER_SIZE	    	    (BUFFER_SIZE_PER_AUDIO_ADC_CHANNEL *\
                                                     NUM_AUDIO_ADC_CHANNELS *\
                                                     PCM_SAMPLE_RESOLUTION_IN_BYTES)

/*****************************************************************************

Macros to adjust Audio Encoder/Decoder (algorithm) buffer memory allocation

****************************************************************************/

/* Maximum Input samples per channel */
#define     AUDIO_MAX_INPUT_SAMPLES_PER_CHAN        (1024 * 2)  /* 2kB */
/* Maximum Output samples per channel */
#define     AUDIO_MAX_OUTPUT_PCM_SAMPLES_PER_CHAN   (1024 * 2)  /* 2kB */

/* Max Buffer size allocated for Audio Decoder output stream */
#define		AUDIO_DECODER_OUT_BUFFER_SIZE           (NUM_AUDIO_DAC_CHANNELS*\
                                                     AUDIO_MAX_OUTPUT_PCM_SAMPLES_PER_CHAN*\
                                                     PCM_SAMPLE_RESOLUTION_IN_BYTES)

/* Max Buffer size allocated for Audio Decoder input stream */
#define		AUDIO_DECODER_IN_BUFFER_SIZE            (NUM_AUDIO_DAC_CHANNELS*\
                                                     AUDIO_MAX_INPUT_SAMPLES_PER_CHAN*\
                                                     PCM_SAMPLE_RESOLUTION_IN_BYTES)
/* Define number of FIFOs to use */
#define     NUM_PCM_FIFO_BUFFERS                    4  /* bigger FIFO to handle MP3 */

/* PCM FIFO Size */
#define     PCM_FIFO_BUFFER_SIZE                    (AUDIO_MAX_OUTPUT_PCM_SAMPLES_PER_CHAN *\
                                                     NUM_AUDIO_DAC_CHANNELS *\
                                                     (NUM_PCM_FIFO_BUFFERS) *\
                                                     PCM_SAMPLE_RESOLUTION_IN_BYTES)
                                                     
                                                     
                                                     

/*****************************************************************************

Processor specific Macros

*****************************************************************************/

/* Use Pushbutton 4 (PB4) to terminate this program */
#define     TERMINATE_BUTTON        	            ADI_FLAG_PB11
/* AD1980 reset pin - PB3 */
#define     AD1980_RESET_FLAG       	            ADI_FLAG_PB3
/* SPORT device connected to AD1980 */
#define     AD1980_SPORT_DEV_NUMBER                 0   /* SPORT 0  */


/*****************************************************************************

Variable declaratioins

*****************************************************************************/

extern volatile u8  Ac97RegAccessComplete;
extern volatile u8  AudioEnabledFlag;
extern volatile u8  LastSecond;
extern volatile u8  AudioDecoderOutBuf[AUDIO_DECODER_OUT_BUFFER_SIZE];
extern volatile u8  AudioDecoderInBuf[AUDIO_DECODER_IN_BUFFER_SIZE];
extern volatile u32 TotalPcmSamplesRead;

/*****************************************************************************

Function prototypes

*****************************************************************************/ 
void DisplayProgress(           /* Displays file progress */
    FILE    *fp, 
    u32     FileLength, 
    u32     TrackLength, 
    u32     NumDecodedSamples, 
    u32     SamplingRate
);
u32     ConfigAudioDevice       (void);
u32     EnableCodecDataflow     (u32 SamplingRate);
u32     DisableCodecDataflow    (void);
u32     CloseAudioDevice        (void);
void    PlayAudioFile           (char *FileName);
u32 AdjustCodecVolume(u32 VolumeLevel);
void ClearOutboundBuffer(void);

#endif /*__AUDIOPLAYER_H__*/

/*****/
