/*************************************************
*            BK Emulator for Window 3.0          *
*************************************************/

/*
File: Speaker.h
Version: 1.0.0
Written by: Yuriy Kalmykov <kalmykov@stoik.com>
    Copyright (c) 2002-2004 Yuriy Kalmykov

    BK Emulator is a program emulated hardware environment for running
code for BK 0010(01) in different configurations.

    This code may be used in compiled form in any way you desire.
This file or it's parts can't be redistributed without the authors
written consent, but can be modified for your private needs.
    Providing that this notice and the authors name and all copyright
notices remains intact.

    Please, an email me to know how you are using it and where. You can
ask me for any information about this below code or any attendant
knowledge.

    This file is provided "as is" with no expressed or implied warranty.
The author accepts no liability for any damage or loss of business that
this product may cause.
*/


// Speaker.h: interface for the CSpeaker class.
//


#pragma once

#include "Device.h"
#include "Defines.h"

#define AUTOGAIN_BUFFER_MASK 0xfff
#define AUTOGAIN_BUFFER_LEN (AUTOGAIN_BUFFER_MASK+1)


class CSpeaker : public CDevice
{
		static const double Speaker_Values[8];
	protected:
		enum                {DEFAULT_SIZE = 2 * 1024 * 1014};

		BOOL                m_bEnableSound;
		BOOL                m_bFilter;
		int                 m_ips;
		SAMPLE_TYPE        *m_pRecieveSamples; //массив данных получаемых с ленты, из которых узнается, что там прочлось
		SAMPLE_TYPE         m_average;
		double  m_feedbackL, m_feedbackR;

		int     m_nBufferPosL, m_nBufferPosR;
		double  m_fAvgL, m_fAvgR;
		double  *m_pdBufferL;
		double  *m_pdBufferR;

		double AutoGain(double sample, double &fAvg, double *pBuf, int &nBufPos);

	public:
		CSpeaker();
		virtual ~CSpeaker();

		void                SetSampleBuffer(int ips);
		inline LONGLONG     GetSampleCount()
		{
			return m_tickCount;
		}
		void                ReceiveSamples(BYTE *pBuff, int length);
		inline void         EnableSound(BOOL bEnable)
		{
			m_bEnableSound = bEnable;
		}
		inline BOOL         IsSoundEnabled()
		{
			return m_bEnableSound;
		}

		inline void         SetFilter(BOOL b)
		{
			m_bFilter = b;
		}
		inline BOOL         GetFilter()
		{
			return m_bFilter;
		}

		virtual void        GetSample(double &sampleL, double &sampleR);

		// Virtual method called after reset command
		virtual void        OnReset();
		virtual void        NextTick();

		// Methods for Set/Get byte/word
		virtual void        GetByte(WORD addr, BYTE *pValue)
		{
			ASSERT(FALSE);
		}
		virtual void        GetWord(WORD addr, WORD *pValue)
		{
			ASSERT(FALSE);
		}
		virtual void        SetByte(WORD addr, BYTE value)
		{
			ASSERT(FALSE);
		}
		virtual void        SetWord(WORD addr, WORD value)
		{
			ASSERT(FALSE);
		}
};
