<commit_before>/*
 * mehstation - Audio engine.
 *
 * Copyright © 2016 <NAME>
 */

#include "system/audio.h"
#include "system/sound.h"

static void meh_audio_stop(Audio* audio, Sound* sound);

Audio* meh_audio_new() {
	Audio* audio = g_new(Audio, 1);	

	audio->sounds = g_queue_new();
	audio->mutex = SDL_CreateMutex();

	if (!audio->mutex) {
		g_critical("can't create the audio mutex.");
	}

	/* open the audio device */

	SDL_AudioSpec want;
	want.channels = 2;
	want.samples = 64;
	want.callback = NULL;

	audio->device_id = SDL_OpenAudioDevice(
		NULL, 0, &want, &(audio->spec), SDL_AUDIO_ALLOW_FORMAT_CHANGE
	);

	SDL_PauseAudioDevice(audio->device_id, 0);

	/* start the playing thread */

	audio->thread_running = TRUE;
	audio->thread = SDL_CreateThread(meh_audio_start, "audio", audio);

	/* loads the sound bank */

	audio->soundbank = g_new(Sound*, SFX_END);
	audio->soundbank[0] = meh_sound_new("res/bip.wav", TRUE);

	return audio;
}

void meh_audio_destroy(Audio* audio) {
	g_assert(audio != NULL);

	/* free each sounds */
	if (audio->soundbank != NULL) {
		for (int i = 0; i < SFX_END; i++) {
			if (audio->soundbank[i] != NULL) {
				meh_sound_destroy(audio->soundbank[i]);
			}
		}
		g_free(audio->soundbank);
		audio->soundbank = NULL;
	}

	if (audio->thread) {
		audio->thread_running = FALSE;
		SDL_WaitThread(audio->thread, NULL);
		audio->thread = NULL;
	}

	if (audio->mutex) {
		SDL_DestroyMutex(audio->mutex);
		audio->mutex = NULL;
	}

	// TODO(remy): test the GDestroyNotify cast
	g_queue_free_full(audio->sounds, (GDestroyNotify)meh_sound_destroy);
	g_free(audio);
}

void meh_audio_play_sound(Audio* audio, Sound* sound) {
	if (audio == NULL) {
		return;
	}

	if (sound == NULL) {
		return;
	}

	SDL_LockMutex(audio->mutex);

	g_debug("sound: start playing '%s'", sound->filename);
	g_queue_push_tail(audio->sounds, sound);

	SDL_UnlockMutex(audio->mutex);
}

void meh_audio_stop(Audio* audio, Sound* sound) {
	g_assert(audio != NULL);
	g_assert(sound != NULL);

	SDL_LockMutex(audio->mutex);

	gboolean removed = g_queue_remove(audio->sounds, sound);
	if (removed) {
		meh_sound_destroy(sound);
	}

	SDL_UnlockMutex(audio->mutex);
}

void meh_audio_play(Audio* audio, guint sound) {
	if (audio == NULL) {
		return;
	}

	if (sound >= SFX_END) {
		return;
	}

	Sound* s = audio->soundbank[sound];

	if (s == NULL) {
		return;
	}

	meh_audio_play_sound(audio, s);
}

/* meh_audio_start starts the background threading
 * reading the sound to play on the sound card */
int meh_audio_start(void* audio) {
	g_assert(audio != NULL);

	Audio* a = (Audio*)audio;

	while (a->thread_running) {

		/* for each sound */
		for (int i = 0; i < g_queue_get_length(a->sounds); i++) {
			Sound* s = g_queue_peek_nth(a->sounds, i);
			/* NOTE(remy): I think we'll need to copy
			 * the bytes here and probably copy batch
			 * per batch and not the full data at once.*/

			/* NOTE(remy): lowering the volume could be costly
			 * and could be remove in the sound loading if
			 * necessary but I prefer to avoid the dependency
			 * of sound -> audio format as long as I can. */
			/* lower a bit the volume (to avoid saturation)
			 * and push it to the audio card */
			void* lowered = g_malloc0(s->data->len);
			if (lowered != NULL) {
				SDL_MixAudioFormat(
						lowered,
						s->data->data,
						a->spec.format,
						s->data->len,
						SDL_MIX_MAXVOLUME * 0.80
						);
				SDL_QueueAudio(a->device_id, lowered, s->data->len);
				g_free(lowered);
			}
		}

		Sound* sound = NULL;
		while ((sound = g_queue_pop_tail(a->sounds)) != NULL) {
			meh_audio_stop(a, sound);
		}

		SDL_PauseAudioDevice(a->device_id, 0);
		SDL_Delay(1);
	}

	g_debug("audio engine is stopping.");
	return 0;
}<commit_msg>audio: comment the sound play with SDL_QueueAudio atm.<commit_after>/*
 * mehstation - Audio engine.
 *
 * Copyright © 2016 <NAME>
 */

#include "system/audio.h"
#include "system/sound.h"

static void meh_audio_stop(Audio* audio, Sound* sound);

Audio* meh_audio_new() {
	Audio* audio = g_new(Audio, 1);	

	audio->sounds = g_queue_new();
	audio->mutex = SDL_CreateMutex();

	if (!audio->mutex) {
		g_critical("can't create the audio mutex.");
	}

	/* open the audio device */

	SDL_AudioSpec want;
	want.channels = 2;
	want.samples = 64;
	want.callback = NULL;

	audio->device_id = SDL_OpenAudioDevice(
		NULL, 0, &want, &(audio->spec), SDL_AUDIO_ALLOW_FORMAT_CHANGE
	);

	SDL_PauseAudioDevice(audio->device_id, 0);

	/* start the playing thread */

	audio->thread_running = TRUE;
	audio->thread = SDL_CreateThread(meh_audio_start, "audio", audio);

	/* loads the sound bank */

	audio->soundbank = g_new(Sound*, SFX_END);
	audio->soundbank[0] = meh_sound_new("res/bip.wav", TRUE);

	return audio;
}

void meh_audio_destroy(Audio* audio) {
	g_assert(audio != NULL);

	/* free each sounds */
	if (audio->soundbank != NULL) {
		for (int i = 0; i < SFX_END; i++) {
			if (audio->soundbank[i] != NULL) {
				meh_sound_destroy(audio->soundbank[i]);
			}
		}
		g_free(audio->soundbank);
		audio->soundbank = NULL;
	}

	if (audio->thread) {
		audio->thread_running = FALSE;
		SDL_WaitThread(audio->thread, NULL);
		audio->thread = NULL;
	}

	if (audio->mutex) {
		SDL_DestroyMutex(audio->mutex);
		audio->mutex = NULL;
	}

	// TODO(remy): test the GDestroyNotify cast
	g_queue_free_full(audio->sounds, (GDestroyNotify)meh_sound_destroy);
	g_free(audio);
}

void meh_audio_play_sound(Audio* audio, Sound* sound) {
	if (audio == NULL) {
		return;
	}

	if (sound == NULL) {
		return;
	}

	SDL_LockMutex(audio->mutex);

	g_debug("sound: start playing '%s'", sound->filename);
	g_queue_push_tail(audio->sounds, sound);

	SDL_UnlockMutex(audio->mutex);
}

void meh_audio_stop(Audio* audio, Sound* sound) {
	g_assert(audio != NULL);
	g_assert(sound != NULL);

	SDL_LockMutex(audio->mutex);

	gboolean removed = g_queue_remove(audio->sounds, sound);
	if (removed) {
		meh_sound_destroy(sound);
	}

	SDL_UnlockMutex(audio->mutex);
}

void meh_audio_play(Audio* audio, guint sound) {
	if (audio == NULL) {
		return;
	}

	if (sound >= SFX_END) {
		return;
	}

	Sound* s = audio->soundbank[sound];

	if (s == NULL) {
		return;
	}

	meh_audio_play_sound(audio, s);
}

/* meh_audio_start starts the background threading
 * reading the sound to play on the sound card */
int meh_audio_start(void* audio) {
	g_assert(audio != NULL);

	Audio* a = (Audio*)audio;

	while (a->thread_running) {

		/* for each sound */
		for (int i = 0; i < g_queue_get_length(a->sounds); i++) {
			Sound* s = g_queue_peek_nth(a->sounds, i);
			/* NOTE(remy): I think we'll need to copy
			 * the bytes here and probably copy batch
			 * per batch and not the full data at once.*/

			/* NOTE(remy): lowering the volume could be costly
			 * and could be remove in the sound loading if
			 * necessary but I prefer to avoid the dependency
			 * of sound -> audio format as long as I can. */
			/* lower a bit the volume (to avoid saturation)
			 * and push it to the audio card */
			void* lowered = g_malloc0(s->data->len);
			if (lowered != NULL) {
				SDL_MixAudioFormat(
						lowered,
						s->data->data,
						a->spec.format,
						s->data->len,
						SDL_MIX_MAXVOLUME * 0.80
						);
				//SDL_QueueAudio(a->device_id, lowered, s->data->len);
				g_free(lowered);
			}
		}

		Sound* sound = NULL;
		while ((sound = g_queue_pop_tail(a->sounds)) != NULL) {
			meh_audio_stop(a, sound);
		}

		SDL_PauseAudioDevice(a->device_id, 0);
		SDL_Delay(1);
	}

	g_debug("audio engine is stopping.");
	return 0;
}
{'fraction_non_alphanumeric': 0.14848444385098145, 'fraction_numerical': 0.005341167044999332, 'mean_word_length': 3.6783260462211116, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 101, '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': '7443065', 'n_tokens_mistral': 3084, 'n_tokens_neox': 2748, 'n_words': 667}