<commit_before>#include "audio_device.h"
#include "clip_manager.h"
#include "core/log.h"
#include "engine/engine.h"
#include "engine/iplugin.h"
#include <dsound.h>


namespace Lumix
{


struct AudioDeviceImpl : public AudioDevice
{
	struct Buffer
	{
		LPDIRECTSOUNDBUFFER handle;
		LPDIRECTSOUND3DBUFFER8 handle_3d;
		IDirectSoundBuffer8* handle8;
		const void* data;
		DWORD data_size;
		DWORD written;
		int sparse_idx;
		bool looped;
	};

	Engine* m_engine;
	HMODULE m_library;
	LPDIRECTSOUND8 m_direct_sound;
	LPDIRECTSOUNDBUFFER m_primary_buffer;
	LPDIRECTSOUND3DLISTENER8 m_listener;
	Buffer m_buffers[MAX_PLAYING_SOUNDS];
	int m_buffer_map[MAX_PLAYING_SOUNDS];
	int m_buffer_count;

	static const int STREAM_SIZE = 32768;

	AudioDeviceImpl()
	{
		m_library = nullptr;
		m_engine = nullptr;
		m_direct_sound = nullptr;
		m_primary_buffer = nullptr;
		m_listener = nullptr;
		m_buffer_count = 0;
		for (auto& i : m_buffer_map)
		{
			i = INVALID_BUFFER_HANDLE;
		}
	}


	bool initPrimaryBuffer()
	{
		DSBUFFERDESC primary_buffer_desc = {};
		primary_buffer_desc.dwSize = sizeof(primary_buffer_desc);
		primary_buffer_desc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRL3D;
		primary_buffer_desc.guid3DAlgorithm = DS3DALG_DEFAULT;
		auto result = SUCCEEDED(
			m_direct_sound->CreateSoundBuffer(&primary_buffer_desc, &m_primary_buffer, nullptr));
		if (!result) return false;

		WAVEFORMATEX wave_format = {};
		wave_format.cbSize = 0;
		wave_format.nChannels = 2;
		wave_format.nSamplesPerSec = 44100;
		wave_format.wBitsPerSample = 16;
		wave_format.nBlockAlign = wave_format.nChannels * wave_format.wBitsPerSample / 8;
		wave_format.nAvgBytesPerSec = wave_format.nSamplesPerSec * wave_format.nBlockAlign;
		wave_format.wFormatTag = WAVE_FORMAT_PCM;

		result = SUCCEEDED(m_primary_buffer->SetFormat(&wave_format));
		if (!result) return false;

		result = SUCCEEDED(
			m_primary_buffer->QueryInterface(IID_IDirectSound3DListener8, (void**)&m_listener));
		if (!result) return false;

		m_listener->SetDopplerFactor(1.0, DS3D_DEFERRED);
		m_listener->SetDistanceFactor(1.0f, DS3D_DEFERRED);
		m_listener->SetRolloffFactor(1.0f, DS3D_DEFERRED);
		m_primary_buffer->Play(0, 0, DSBPLAY_LOOPING);

		return true;
	}


	bool init(Engine& engine)
	{
		m_engine = &engine;

		auto result = SUCCEEDED(CoInitialize(nullptr));
		ASSERT(result);

		m_library = LoadLibrary("dsound.dll");
		if (!m_library)
		{
			g_log_error.log("audio") << "Failed to load dsound.dll.";
			return false;
		}
		auto* dsoundCreate =
			(decltype(DirectSoundCreate8)*)GetProcAddress(m_library, "DirectSoundCreate8");
		if (!dsoundCreate)
		{
			g_log_error.log("audio") << "Failed to get DirectSoundCreate8 from dsound.dll.";
			ASSERT(false);
			FreeLibrary(m_library);
			return false;
		}

		result = SUCCEEDED(dsoundCreate(0, &m_direct_sound, nullptr));
		if (!result)
		{
			g_log_error.log("audio") << "Failed to create DirectSound.";
			ASSERT(false);
			FreeLibrary(m_library);
			return false;
		}

		HWND hwnd = (HWND)engine.getPlatformData().window_handle;
		result = SUCCEEDED(m_direct_sound->SetCooperativeLevel(hwnd, DSSCL_PRIORITY));<commit_msg>some more error messages in audio<commit_after>#include "audio_device.h"
#include "clip_manager.h"
#include "core/log.h"
#include "engine/engine.h"
#include "engine/iplugin.h"
#include <dsound.h>


namespace Lumix
{


struct AudioDeviceImpl : public AudioDevice
{
	struct Buffer
	{
		LPDIRECTSOUNDBUFFER handle;
		LPDIRECTSOUND3DBUFFER8 handle_3d;
		IDirectSoundBuffer8* handle8;
		const void* data;
		DWORD data_size;
		DWORD written;
		int sparse_idx;
		bool looped;
	};

	Engine* m_engine;
	HMODULE m_library;
	LPDIRECTSOUND8 m_direct_sound;
	LPDIRECTSOUNDBUFFER m_primary_buffer;
	LPDIRECTSOUND3DLISTENER8 m_listener;
	Buffer m_buffers[MAX_PLAYING_SOUNDS];
	int m_buffer_map[MAX_PLAYING_SOUNDS];
	int m_buffer_count;

	static const int STREAM_SIZE = 32768;

	AudioDeviceImpl()
	{
		m_library = nullptr;
		m_engine = nullptr;
		m_direct_sound = nullptr;
		m_primary_buffer = nullptr;
		m_listener = nullptr;
		m_buffer_count = 0;
		for (auto& i : m_buffer_map)
		{
			i = INVALID_BUFFER_HANDLE;
		}
	}


	bool initPrimaryBuffer()
	{
		DSBUFFERDESC primary_buffer_desc = {};
		primary_buffer_desc.dwSize = sizeof(primary_buffer_desc);
		primary_buffer_desc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRL3D;
		primary_buffer_desc.guid3DAlgorithm = DS3DALG_DEFAULT;
		auto result = SUCCEEDED(
			m_direct_sound->CreateSoundBuffer(&primary_buffer_desc, &m_primary_buffer, nullptr));
		if (!result) return false;

		WAVEFORMATEX wave_format = {};
		wave_format.cbSize = 0;
		wave_format.nChannels = 2;
		wave_format.nSamplesPerSec = 44100;
		wave_format.wBitsPerSample = 16;
		wave_format.nBlockAlign = wave_format.nChannels * wave_format.wBitsPerSample / 8;
		wave_format.nAvgBytesPerSec = wave_format.nSamplesPerSec * wave_format.nBlockAlign;
		wave_format.wFormatTag = WAVE_FORMAT_PCM;

		result = SUCCEEDED(m_primary_buffer->SetFormat(&wave_format));
		if (!result) return false;

		result = SUCCEEDED(
			m_primary_buffer->QueryInterface(IID_IDirectSound3DListener8, (void**)&m_listener));
		if (!result) return false;

		m_listener->SetDopplerFactor(1.0, DS3D_DEFERRED);
		m_listener->SetDistanceFactor(1.0f, DS3D_DEFERRED);
		m_listener->SetRolloffFactor(1.0f, DS3D_DEFERRED);
		m_primary_buffer->Play(0, 0, DSBPLAY_LOOPING);

		return true;
	}


	bool init(Engine& engine)
	{
		m_engine = &engine;

		auto coinitialize_result = CoInitialize(nullptr);
		if (!SUCCEEDED(coinitialize_result))
		{
			g_log_error.log("audio") << "CoInitialize failed. Error code: " << coinitialize_result;
			ASSERT(false);
			return false;
		}

		m_library = LoadLibrary("dsound.dll");
		if (!m_library)
		{
			g_log_error.log("audio") << "Failed to load dsound.dll.";
			return false;
		}
		auto* dsoundCreate =
			(decltype(DirectSoundCreate8)*)GetProcAddress(m_library, "DirectSoundCreate8");
		if (!dsoundCreate)
		{
			g_log_error.log("audio") << "Failed to get DirectSoundCreate8 from dsound.dll.";
			ASSERT(false);
			FreeLibrary(m_library);
			return false;
		}

		auto create_result = dsoundCreate(0, &m_direct_sound, nullptr);
		if (!SUCCEEDED(create_result))
		{
			g_log_error.log("audio") << "Failed to create DirectSound. Error code: " << create_result;
			ASSERT(false);
			FreeLibrary(m_library);
			return false;
		}

		HWND hwnd = (HWND)engine.getPlatformData().window_handle;
		auto result = SUCCEEDED(m_direct_sound->SetCooperativeLevel(hwnd, DSSCL_PRIORITY));
{'fraction_non_alphanumeric': 0.10771581359816654, 'fraction_numerical': 0.013445378151260505, 'mean_word_length': 5.264114832535885, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7223013', 'n_tokens_mistral': 2672, 'n_tokens_neox': 2422, 'n_words': 421}