<commit_before>#include "Audio.hpp"
#include <AL/al.h>
#include <AL/alext.h>
#include "Game.hpp"
#include "Sound.hpp"
#include "GlobalProperties.hpp"

#define AUDIO_GC_DEBUG 0

namespace Diggler {

Audio::Audio(Game* G) : G(G), sounds(m_sounds) {
	if (!GlobalProperties::IsSoundEnabled)
		return;
	
	int alMajor, alMinor;
	alcGetIntegerv(nullptr, ALC_MAJOR_VERSION, 1, &alMajor);
	alcGetIntegerv(nullptr, ALC_MINOR_VERSION, 1, &alMinor);
	getDebugStream() << "OpenAL v" << alMajor << '.' << alMinor << std::endl;
	
	ALboolean enumeration = alcIsExtensionPresent(nullptr, "ALC_ENUMERATION_EXT");
	if (enumeration == AL_FALSE)
		; // enumeration not supported
	else {
		const ALCchar *devices = alcGetString(nullptr, ALC_DEVICE_SPECIFIER);
		const ALCchar *device = devices, *next = devices + 1;
		size_t len = 0;
		while (device && *device != '\0' && next && *next != '\0') {
			len = strlen(device);
			device += (len + 1);
			next += (len + 2);
		}
	}
	const ALCchar *deviceName = nullptr;
	m_audioDevice = alcOpenDevice(deviceName);
	if (!m_audioDevice) {
		getDebugStream() << "Failed opening AL device '" << deviceName << "': " << alcGetError(m_audioDevice) << std::endl;
		GlobalProperties::IsSoundEnabled = false;
	}
	deviceName = alcGetString(m_audioDevice, ALC_DEVICE_SPECIFIER);
	getDebugStream() << "Using device '" << deviceName << '\'' << std::endl;
	
	ALCint attrs[] = {
		0, 0
	};
	
	m_audioContext = alcCreateContext(m_audioDevice, attrs);
	if (!alcMakeContextCurrent(m_audioContext)) {
		getDebugStream() << "Failed setting context on AL device '" << deviceName << "': " << alcGetError(m_audioDevice) << std::endl;
		alcCloseDevice(m_audioDevice);
		GlobalProperties::IsSoundEnabled = false;
	}
}

Audio::~Audio() {
	if (!GlobalProperties::IsSoundEnabled)
		return;
	
	alcMakeContextCurrent(nullptr);
	alcDestroyContext(m_audioContext);
	alcCloseDevice(m_audioDevice);
}

static ALfloat float3data[3], plrOrient[6];

void Audio::updateAngle() {
	const glm::vec3 &at = G->LP->camera.getLookAt(),
					&up = G->LP->camera.getUp();
	plrOrient[0] = at.x;
	plrOrient[1] = at.y;
	plrOrient[2] = at.z;
	plrOrient[3] = up.x;
	plrOrient[4] = up.y;
	plrOrient[5] = up.z;
	alListenerfv(AL_ORIENTATION, plrOrient);
}

void Audio::updatePos() {
	const glm::vec3 &pos = G->LP->position,
					&vel = G->LP->velocity;
	float3data[0] = pos.x;
	float3data[1] = pos.y;
	float3data[2] = pos.z;
	alListenerfv(AL_POSITION, float3data);
	float3data[0] = vel.x;
	float3data[1] = vel.y;
	float3data[2] = vel.z;
	alListenerfv(AL_VELOCITY, float3data);
}

void Audio::update() {
	updatePos();
	updateAngle();
}

void Audio::loadSoundAssets() {
	std::string soundsDir = getAssetsDirectory() + "sounds/";
	for (const std::string &fn : fs::getFiles(soundsDir)) {
		if (fn.length() >= 4 && fn.substr(fn.length()-4) == ".ogg") {
			addSound(fn.substr(0, fn.length()-4), soundsDir+fn);
		}
	}
}

void Audio::addSound(const std::string& name, const std::string& path) {<commit_msg>Fix bad sound paths leading to crash by std::out_of_range<commit_after>#include "Audio.hpp"
#include <AL/al.h>
#include <AL/alext.h>
#include "Game.hpp"
#include "Sound.hpp"
#include "GlobalProperties.hpp"

#define AUDIO_GC_DEBUG 0

namespace Diggler {

Audio::Audio(Game* G) : G(G), sounds(m_sounds) {
	if (!GlobalProperties::IsSoundEnabled)
		return;
	
	int alMajor, alMinor;
	alcGetIntegerv(nullptr, ALC_MAJOR_VERSION, 1, &alMajor);
	alcGetIntegerv(nullptr, ALC_MINOR_VERSION, 1, &alMinor);
	getDebugStream() << "OpenAL v" << alMajor << '.' << alMinor << std::endl;
	
	ALboolean enumeration = alcIsExtensionPresent(nullptr, "ALC_ENUMERATION_EXT");
	if (enumeration == AL_FALSE)
		; // enumeration not supported
	else {
		const ALCchar *devices = alcGetString(nullptr, ALC_DEVICE_SPECIFIER);
		const ALCchar *device = devices, *next = devices + 1;
		size_t len = 0;
		while (device && *device != '\0' && next && *next != '\0') {
			len = strlen(device);
			device += (len + 1);
			next += (len + 2);
		}
	}
	const ALCchar *deviceName = nullptr;
	m_audioDevice = alcOpenDevice(deviceName);
	if (!m_audioDevice) {
		getDebugStream() << "Failed opening AL device '" << deviceName << "': " << alcGetError(m_audioDevice) << std::endl;
		GlobalProperties::IsSoundEnabled = false;
	}
	deviceName = alcGetString(m_audioDevice, ALC_DEVICE_SPECIFIER);
	getDebugStream() << "Using device '" << deviceName << '\'' << std::endl;
	
	ALCint attrs[] = {
		0, 0
	};
	
	m_audioContext = alcCreateContext(m_audioDevice, attrs);
	if (!alcMakeContextCurrent(m_audioContext)) {
		getDebugStream() << "Failed setting context on AL device '" << deviceName << "': " << alcGetError(m_audioDevice) << std::endl;
		alcCloseDevice(m_audioDevice);
		GlobalProperties::IsSoundEnabled = false;
	}
}

Audio::~Audio() {
	if (!GlobalProperties::IsSoundEnabled)
		return;
	
	alcMakeContextCurrent(nullptr);
	alcDestroyContext(m_audioContext);
	alcCloseDevice(m_audioDevice);
}

static ALfloat float3data[3], plrOrient[6];

void Audio::updateAngle() {
	const glm::vec3 &at = G->LP->camera.getLookAt(),
					&up = G->LP->camera.getUp();
	plrOrient[0] = at.x;
	plrOrient[1] = at.y;
	plrOrient[2] = at.z;
	plrOrient[3] = up.x;
	plrOrient[4] = up.y;
	plrOrient[5] = up.z;
	alListenerfv(AL_ORIENTATION, plrOrient);
}

void Audio::updatePos() {
	const glm::vec3 &pos = G->LP->position,
					&vel = G->LP->velocity;
	float3data[0] = pos.x;
	float3data[1] = pos.y;
	float3data[2] = pos.z;
	alListenerfv(AL_POSITION, float3data);
	float3data[0] = vel.x;
	float3data[1] = vel.y;
	float3data[2] = vel.z;
	alListenerfv(AL_VELOCITY, float3data);
}

void Audio::update() {
	updatePos();
	updateAngle();
}

void Audio::loadSoundAssets() {
	std::string soundsDir = getAssetsDirectory() + "/sounds/";
	for (const std::string &fn : fs::getFiles(soundsDir)) {
		if (fn.length() >= 4 && fn.substr(fn.length()-4) == ".ogg") {
			addSound(fn.substr(0, fn.length()-4), soundsDir+fn);
		}
	}
}

void Audio::addSound(const std::string& name, const std::string& path) {
{'fraction_non_alphanumeric': 0.1706581812091777, 'fraction_numerical': 0.013398090772064981, 'mean_word_length': 5.081466395112017, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 25, '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': '2779602', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2274, 'n_words': 442}