/*  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 "Defines.h"

//их почему-то нельзя сделать членами класса
static CRITICAL_SECTION m_csWaveCriticalSection;
static volatile int     m_nWaveFreeBlockCount;

class CBkSound
{
		enum                {BLOCK_COUNT = 8};

	public:

		CBkSound(CWnd *pWnd);
		virtual ~CBkSound();

		void SoundGen_SetVolume(WORD volume);
		WORD SoundGen_GetVolume();
		void SoundGen_SetSample(double &L, double &R);
		void SoundGen_MixSample(double &L, double &R);
		void SoundGen_FeedDAC_Mixer(SAMPLE_TYPE &L, SAMPLE_TYPE &R);
		void SoundGen_FeedDAC(SAMPLE_TYPE L, SAMPLE_TYPE R);

		void ResetSample(SAMPLE_TYPE L, SAMPLE_TYPE R)
		{
			m_dSampleL = (double)L / FLOAT_BASE;
			m_dSampleR = (double)R / FLOAT_BASE;
		}

	protected:
		WAVEHDR                 *m_pWaveBlocks;
		int                     m_nWaveCurrentBlock;
		BOOL                    m_bSoundGenInitialized;
		int                     m_nBufSize, m_nBufSizeW; //размер буфера в байтах и в словах.
		HWAVEOUT                m_hWaveOut;
		WAVEFORMATEX            m_wfx;
		BYTE                    *m_mBuffer;
		SAMPLE_TYPE             *m_mBufferST; // этот тот же m_mBuffer, но в словах
		int                     m_nBufCurPos;
		double                  m_dSampleL, m_dSampleR;
		double                  m_feedbackL, m_feedbackR;

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