// ###########################################################################
// #
// # OSCAAudio - Definition of OSCA Audio Emulation Class
// # Copyright (C) 2009-2011 Tony Friery [DialTone]
// #
// # ALL RIGHTS RESERVED
// #
// ###########################################################################

#ifndef OSCAAudioH
#define OSCAAudioH

#include <Classes.hpp>
#include "bass.h"

///////////////////////////////////////////////////////////////////////
//
//		Definition of Audio Class
//
///////////////////////////////////////////////////////////////////////

class TOSCABoard;

typedef struct _AudioRegs
{
	// Common Registers
	bool		Playing;				// Channel is currently playing?
	bool		Looping;          // Channel should loop after this sample?
	BYTE		Volume;				// Volume (0 thru 64)

	// V6 Format Registers
	WORD 		Location;			// Sample Location
	WORD		Length;				// Sample Duration
	WORD		SampleRate;			// Sample Rate

	// DirectX Adjusted Registers
	DWORD		RealLocation;		// Sample Location
	DWORD		RealLength;			// Sample Duration
	DWORD		RealSampRate;		// Sample Rate
} AudioRegs;

class TOSCAAudioSystem
{
	private:
		TOSCABoard				*m_pOwner;              // Pointer to V6 Board Class
		HWND						m_hHandle;					// Handle for main window (req'd by DX)

		// DirectX Properties
		HSTREAM					m_hChannels[4];			// Pointer to the 4x BASS streams used for playback
		short						*m_pBuffers[4];			// Pointer to the channel buffers
		DWORD						m_nWritePos[4];			// Buffer write positions for the playback buffers
		double					fSamplePrescaler[4];		// Resampling counters for the playback buffers
		DWORD						fOutputPos[4];				// Sample data pointer for the playback buffers

		// V6Z80 Properties
		AudioRegs				ControlRegs[4];			// Control Register values
		BYTE						LoopFlags;					// Loop / Playing flags

		bool __fastcall 		InitSound(void);
		void __fastcall		UpdateChannel(BYTE channel,
										unsigned int bytes, unsigned int cycles);
		void __fastcall		StartChannel(BYTE channel);

	public:
		void __fastcall      WriteAudioPort(BYTE addr, WORD value);
		WORD __fastcall      ReadAudioPort(BYTE addr);
		void __fastcall		SetPlaybackFlags(BYTE flags);
		BYTE __fastcall		GetAudioFlags(void);
		void __fastcall		ClearLoopFlag(BYTE channel);
		void __fastcall		UpdateSound(unsigned int cycles);

		__fastcall 				TOSCAAudioSystem(HWND handle, TOSCABoard *owner);
		__fastcall 				~TOSCAAudioSystem();
};

//---------------------------------------------------------------------------

#endif

