//	Darkstar Engine  (c) UnderWorld Interactive Studio (all coding by Svarog    2007)
//
//      Данный файл является частью Darkstar Engine. Любое использование кода файла
//      без согласия компании, является нарушением закона об авторском праве (в слу-
//      чае если не оговорено обратное).
//
//      Имя файла: soundplayer.cpp
//      Краткое описание: проигрыватель звука с использованием DirectSound
//

#include <Darkstar/common.hpp>
#include <Darkstar/Audio System/Sound/sounddriver.hpp>
#include <Darkstar/Kernel/logh.hpp>

using namespace as;
using namespace ker;

/*SoundDriver::SoundDriver()
{
	lpds = NULL;
	lpdsbuf = NULL;
	seconds_load_for_streaming = 0;
};

SoundDriver::~SoundDriver()
{
	if(lpdsbuf)
	{
		lpdsbuf->Release();
		lpdsbuf = NULL;
	};
	if(lpds)
	{
		lpds->Release();
		lpds = NULL;
	};
};

bool SoundDriver::create(HWND hwnd)
{
	if(FAILED(DirectSoundCreate8(NULL, &lpds, NULL)))
	{
		lerr << "Cannot create DirectSound primary interface";
		return false;
	};
	if(FAILED(lpds->SetCooperativeLevel(hwnd, DSSCL_PRIORITY)))
	{
		lerr << "Cannot set priority cooperative level";
		return false;
	};
	DSCAPS caps;
	caps.dwSize = sizeof(DSCAPS);
	if(FAILED(lpds->GetCaps(&caps)))
	{
		lerr << "Cannot get caps for sound";
		return false;
	};
	scaps.is_emul_driver = (caps.dwFlags & DSCAPS_EMULDRIVER) != 0;
	scaps.is_sup_16bitprimbuf = (caps.dwFlags & DSCAPS_PRIMARY16BIT) != 0;
	scaps.is_sup_16secbuffer = (caps.dwFlags & DSCAPS_SECONDARY16BIT) != 0;
	scaps.is_sup_8bitprimbuf = (caps.dwFlags & DSCAPS_PRIMARY8BIT) != 0;
	scaps.is_sup_8secbuffer = (caps.dwFlags & DSCAPS_SECONDARY8BIT) != 0;
	scaps.is_sup_monoprimbuffer = (caps.dwFlags & DSCAPS_PRIMARYMONO) != 0;
	scaps.is_sup_monosecbuffer = (caps.dwFlags & DSCAPS_SECONDARYMONO) != 0;
	scaps.is_sup_stereoprimbuffer = (caps.dwFlags & DSCAPS_PRIMARYSTEREO) != 0;
	scaps.is_sup_stereosecbuffer = (caps.dwFlags & DSCAPS_SECONDARYSTEREO) != 0;
	scaps.max_static_buf = caps.dwMaxHwMixingStaticBuffers;
	scaps.max_stream_buf = caps.dwMaxHwMixingStreamingBuffers;
	scaps.max_3dstatic_buf = caps.dwMaxHw3DStaticBuffers;
	scaps.max_3dstream_buf = caps.dwMaxHw3DStreamingBuffers;
#ifdef _NOTFINAL
	bool g = Log::ptr()->getIsToConsole();
	Log::ptr()->setIsToConsole(false);
	lmes << "Sound Card Information";
	lmes << "Is directSound driver not installed: " << makeStringFromBool(scaps.is_emul_driver);
	lmes << "Supported 8 bit primary buffer: " << makeStringFromBool(scaps.is_sup_8bitprimbuf);
	lmes << "Supported 16 bit primary buffer: " << makeStringFromBool(scaps.is_sup_16bitprimbuf);
	lmes << "Supported stereo primary buffer: " << makeStringFromBool(scaps.is_sup_stereoprimbuffer);
	lmes << "Supported mono primary buffer: " << makeStringFromBool(scaps.is_sup_monoprimbuffer);
	lmes << "Supported 8 bit secondary buffer: " << makeStringFromBool(scaps.is_sup_8secbuffer);
	lmes << "Supported 16 bit secondary buffer: " << makeStringFromBool(scaps.is_sup_16secbuffer);
	lmes << "Supported stereo secondary buffer: " << makeStringFromBool(scaps.is_sup_stereosecbuffer);
	lmes << "Supported mono secondary buffer: " << makeStringFromBool(scaps.is_sup_monosecbuffer);
	lmes << "Maximum number of static buffers: " << scaps.max_static_buf;
	lmes << "Maximum number of streaming buffers: " <<scaps.max_stream_buf;
	lmes << "Maximum number of 3D static buffers: " << scaps.max_3dstatic_buf;
	lmes << "Maximum number of 3D streaming buffers: " << scaps.max_3dstream_buf;
	Log::ptr()->ptr()->setIsToConsole(g);
#endif

	DSBUFFERDESC prim_buf_desc;
	memset(&prim_buf_desc, 0, sizeof(DSBUFFERDESC));
	prim_buf_desc.dwSize = sizeof(DSBUFFERDESC);
	prim_buf_desc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME;
	prim_buf_desc.guid3DAlgorithm = DS3DALG_DEFAULT;
	if(FAILED(prim_buf->CreateSoundBuffer(&prim_buf_desc, &lpdsbuf, NULL)))
	{
		lerr << "Cannot create primary sound buffer";
		return false;
	};
	return true;
};

bool SoundDriver::setPrimaryBufferFormat(const unsigned int num_channels, const unsigned int bitspersample, const unsigned int sample_rate)
{
	WAVEFORMATEX wfmex;
	ZeroMemory(&wfmex, sizeof(wfmex));
	wfmex.cbSize = sizeof(WAVEFORMATEX);
	wfmex.nChannels = num_channels;
	wfmex.wBitsPerSample = bitspersample;
	wfmex.nSamplesPerSec = sample_rate;
	wfmex.wFormatTag = WAVE_FORMAT_PCM;
	if(FAILED(prim_buf->SetFormat(&wfmex)))
	{
		lwar << "Cannot set new format for primary sound buffer";
		return false;
	};
	return false;
};

LPDIRECTSOUNDBUFFER SoundDriver::createSoundBuffer(bool is_streaming_use, const unsigned int size, const unsigned int ctrl, const unsigned int sample_rate, const unsigned int bits_per_sample, const unsigned int num_chanels, GUID guid_3d_algorithm)
{
	LPDIRECTSOUNDBUFFER buf = NULL;
	LPCDSBUFFERDESC buf_desc;
	WAVEFORMATEX wfmex;
	ZeroMemory(buf_desc, sizeof(DSCBUFFERDESC));
	ZeroMemory(&wfmex, sizeof(WAVEFORMATEX));
	wfmex.cbSize = sizeof(WAVEFORMATEX);
	wfmex.wFormatTag = WAVE_FORMAT_PCM;
	wfmex.nChannels = num_chanels;
	wfmex.nSamplesPerSec = sample_rate;
	wfmex.wBitsPerSample = bits_per_sample;
	wfmex.nBlockAlign = (wfmex.nChannels*wfmex.wBitsPerSample) >> 3;
	wfmex.nAvgBytesPerSec = wfmex.nSamplesPerSec*wfmex.nBlockAlign;
	buf_desc.dwSize = sizeof(DSCBUFFERDESC);
	buf_desc.dwFlags = ctrl;
	buf_desc.dwSize = size;
	buf_desc.lpwfxFormat = &wfmex;
	buf_desc.guid3DAlgorithm = guid_3d_algorithm;
	return NULL;
};
*/