/*  This file is part of BKBTL.
    BKBTL is free software: you can redistribute it and/or modify it under the terms
of the GNU Lesser General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
    BKBTL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public License along with
BKBTL. If not, see <http://www.gnu.org/licenses/>. */

// SoundGen.h
//

#pragma once

#define DIRECTSOUND_VERSION 0x1000

#include <Mmsystem.h>
#include "Config.h"
#include "LockVarType.h"

class CBkSound
{
	protected:
		double          m_dSampleL, m_dSampleR;
		double          m_dFeedbackL, m_dFeedbackR;
		int             m_nWaveCurrentBlock;
		int             m_nBufCurPos;
		int             m_nBufSize;     //размер звукового буфера в байтах
		int             m_nBufSizeST;   //размер звукового буфера в сэмплах
		int             m_nSoundSampleRate;
		SAMPLE_TYPE    *m_mBufferST;
		WAVEHDR        *m_pWaveBlocks;
		HWAVEOUT        m_hWaveOut;
		WAVEFORMATEX    m_wfx;

	public:
		bool            m_bSoundGenInitialized;

		CBkSound();
		virtual ~CBkSound();
		void            ReInit();
		void            SoundGen_SetVolume(WORD volume);
		WORD            SoundGen_GetVolume();
		void            SoundGen_SetSample(double &L, double &R);
		void            SoundGen_MixSample(double &L, double &R);
		void            SoundGen_ResetSample(SAMPLE_TYPE L, SAMPLE_TYPE R);
		void            SoundGen_FeedDAC_Mixer(SAMPLE_TYPE &L, SAMPLE_TYPE &R);

	protected:

		void            SoundGen_Initialize(WORD volume);
		void            SoundGen_Finalize();

		void            Init(WORD volume);
		void            UnInit();

		static CRITICAL_SECTION m_csWaveCriticalSection;
		static volatile int     m_nWaveFreeBlockCount;
		static LockVarType		m_lock;

		static void CALLBACK WaveCallback(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2);
};
