/* sounds.c generated by valac 0.56.19, the Vala compiler
 * generated from sounds.vala, do not modify */

/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright © 2026 Will Warner
 *
 * This file is part of libgnome-games-support.
 *
 * libgnome-games-support 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.
 *
 * libgnome-games-support 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 libgnome-games-support.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "libgnome-games-support.h"
#include <alc.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <al.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <sndfile.h>

#define GAMES_SOUNDS_MAX_SOURCES 32
#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	GAMES_SOUNDS_0_PROPERTY,
	GAMES_SOUNDS_NUM_PROPERTIES
};
static GParamSpec* games_sounds_properties[GAMES_SOUNDS_NUM_PROPERTIES];
#define _alcDestroyContext0(var) ((var == NULL) ? NULL : (var = (alcDestroyContext (var), NULL)))
#define _alcCloseDevice0(var) ((var == NULL) ? NULL : (var = (alcCloseDevice (var), NULL)))
#define _g_hash_table_unref0(var) ((var == NULL) ? NULL : (var = (g_hash_table_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _sf_close0(var) ((var == NULL) ? NULL : (var = (sf_close (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_ptr_array_unref0(var) ((var == NULL) ? NULL : (var = (g_ptr_array_unref (var), NULL)))

struct _GamesSoundsPrivate {
	ALCcontext* context;
	ALCdevice* device;
	GHashTable* library;
	ALuint* sources;
	gint sources_length1;
	gint _sources_size_;
	guint source_index;
};

static gint GamesSounds_private_offset;
static gpointer games_sounds_parent_class = NULL;

static void _g_free0_ (gpointer var);
static void games_sounds_real_dispose (GObject* base);
static void games_sounds_finalize (GObject * obj);
static GType games_sounds_get_type_once (void);

static inline gpointer
games_sounds_get_instance_private (GamesSounds* self)
{
	return G_STRUCT_MEMBER_P (self, GamesSounds_private_offset);
}

static void
_g_free0_ (gpointer var)
{
	var = (g_free (var), NULL);
}

GamesSounds*
games_sounds_construct (GType object_type,
                        const gchar* sound_dir,
                        GError** error)
{
	GamesSounds * self = NULL;
	ALCdevice* _tmp0_;
	ALCenum device_error = 0;
	ALCdevice* _tmp1_;
	ALCdevice* _tmp2_;
	ALCcontext* _tmp3_;
	ALCenum context_error = 0;
	ALCdevice* _tmp4_;
	ALCcontext* _tmp5_;
	GFile* directory = NULL;
	GFile* _tmp6_;
	GFile* _tmp7_;
	GFileEnumerator* enumerator = NULL;
	GFile* _tmp9_;
	GFileEnumerator* _tmp10_;
	GFileInfo* file_info = NULL;
	ALuint* _tmp39_;
	ALuint* _tmp40_;
	gint _tmp40__length1;
	ALuint* _tmp41_;
	gint _tmp41__length1;
	GError* _inner_error0_ = NULL;
	g_return_val_if_fail (sound_dir != NULL, NULL);
	self = (GamesSounds*) g_object_new (object_type, NULL);
	_tmp0_ = alcOpenDevice (NULL);
	_alcCloseDevice0 (self->priv->device);
	self->priv->device = _tmp0_;
	_tmp1_ = self->priv->device;
	device_error = alcGetError (_tmp1_);
	if (device_error != ALC_NO_ERROR) {
		g_error ("sounds.vala:40: Got an ALC error while trying to open device: code %d", (gint) device_error);
	}
	_tmp2_ = self->priv->device;
	_tmp3_ = alcCreateContext (_tmp2_, NULL);
	_alcDestroyContext0 (self->priv->context);
	self->priv->context = _tmp3_;
	_tmp4_ = self->priv->device;
	context_error = alcGetError (_tmp4_);
	if (context_error != ALC_NO_ERROR) {
		g_error ("sounds.vala:45: Got an ALC error while trying to create context: code " \
"%d", (gint) context_error);
	}
	_tmp5_ = self->priv->context;
	alcMakeContextCurrent (_tmp5_);
	_tmp6_ = g_file_new_for_path (sound_dir);
	directory = _tmp6_;
	_tmp7_ = directory;
	if (!g_file_query_exists (_tmp7_, NULL)) {
		GError* _tmp8_;
		_tmp8_ = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_NOENT, "Sound directory does not exist");
		_inner_error0_ = _tmp8_;
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (directory);
		_g_object_unref0 (self);
		return NULL;
	}
	_tmp9_ = directory;
	_tmp10_ = g_file_enumerate_children (_tmp9_, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &_inner_error0_);
	enumerator = _tmp10_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (directory);
		_g_object_unref0 (self);
		return NULL;
	}
	while (TRUE) {
		GFileInfo* _tmp11_ = NULL;
		GFileEnumerator* _tmp12_;
		GFileInfo* _tmp13_;
		GFileInfo* _tmp14_;
		GFileInfo* _tmp15_;
		gchar* filename = NULL;
		GFileInfo* _tmp16_;
		const gchar* _tmp17_;
		gchar* _tmp18_;
		gchar* path = NULL;
		const gchar* _tmp19_;
		gchar* _tmp20_;
		SF_INFO info = {0};
		SNDFILE* file = NULL;
		const gchar* _tmp21_;
		SNDFILE* _tmp22_;
		SNDFILE* _tmp23_;
		ALenum format = 0;
		SF_INFO _tmp25_;
		sf_count_t sample_count = 0;
		SF_INFO _tmp26_;
		SF_INFO _tmp27_;
		ALshort* data = NULL;
		ALshort* _tmp28_;
		gint data_length1;
		gint _data_size_;
		SNDFILE* _tmp29_;
		ALshort* _tmp30_;
		gint _tmp30__length1;
		SF_INFO _tmp31_;
		SF_INFO _tmp32_;
		ALuint buffer = 0U;
		ALuint _tmp33_ = 0U;
		ALshort* _tmp34_;
		gint _tmp34__length1;
		SF_INFO _tmp35_;
		ALenum sound_error = 0;
		_tmp12_ = enumerator;
		_tmp13_ = g_file_enumerator_next_file (_tmp12_, NULL, &_inner_error0_);
		_tmp11_ = _tmp13_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			g_propagate_error (error, _inner_error0_);
			_g_object_unref0 (file_info);
			_g_object_unref0 (enumerator);
			_g_object_unref0 (directory);
			_g_object_unref0 (self);
			return NULL;
		}
		_tmp14_ = _tmp11_;
		_tmp11_ = NULL;
		_g_object_unref0 (file_info);
		file_info = _tmp14_;
		_tmp15_ = file_info;
		if (!(_tmp15_ != NULL)) {
			_g_object_unref0 (_tmp11_);
			break;
		}
		_tmp16_ = file_info;
		_tmp17_ = g_file_info_get_name (_tmp16_);
		_tmp18_ = g_strdup (_tmp17_);
		filename = _tmp18_;
		_tmp19_ = filename;
		_tmp20_ = g_build_filename (sound_dir, _tmp19_, NULL);
		path = _tmp20_;
		memset (&info, 0, sizeof (SF_INFO));
		_tmp21_ = path;
		_tmp22_ = sf_open (_tmp21_, SFM_READ, &info);
		file = _tmp22_;
		_tmp23_ = file;
		if (_tmp23_ == NULL) {
			const gchar* _tmp24_;
			_tmp24_ = filename;
			g_critical ("sounds.vala:64: Could not open audio file: %s", _tmp24_);
			_sf_close0 (file);
			 (&info);
			_g_free0 (path);
			_g_free0 (filename);
			_g_object_unref0 (_tmp11_);
			continue;
		}
		_tmp25_ = info;
		switch (_tmp25_.channels) {
			case 1:
			{
				format = AL_FORMAT_MONO16;
				break;
			}
			case 2:
			{
				format = AL_FORMAT_STEREO16;
				break;
			}
			default:
			{
				g_warning ("sounds.vala:77: Unable to load sound: More than two channels are not s" \
"upported");
				_sf_close0 (file);
				 (&info);
				_g_free0 (path);
				_g_free0 (filename);
				_g_object_unref0 (_tmp11_);
				continue;
			}
		}
		_tmp26_ = info;
		_tmp27_ = info;
		sample_count = _tmp26_.frames * _tmp27_.channels;
		_tmp28_ = g_new0 (ALshort, sample_count);
		data = _tmp28_;
		data_length1 = sample_count;
		_data_size_ = data_length1;
		_tmp29_ = file;
		_tmp30_ = data;
		_tmp30__length1 = data_length1;
		_tmp31_ = info;
		_tmp32_ = info;
		if (sf_readf_short (_tmp29_, _tmp30_, _tmp31_.frames) < _tmp32_.frames) {
			g_warning ("sounds.vala:85: Unable to load sound: file ended unexpectedly");
			data = (g_free (data), NULL);
			_sf_close0 (file);
			 (&info);
			_g_free0 (path);
			_g_free0 (filename);
			_g_object_unref0 (_tmp11_);
			continue;
		}
		alGenBuffers ((ALsizei) 1, &_tmp33_);
		buffer = _tmp33_;
		_tmp34_ = data;
		_tmp34__length1 = data_length1;
		_tmp35_ = info;
		alBufferData (buffer, format, _tmp34_, (ALsizei) (sample_count * 2), (ALsizei) _tmp35_.samplerate);
		sound_error = alGetError ();
		if (sound_error == AL_NO_ERROR) {
			GHashTable* _tmp36_;
			const gchar* _tmp37_;
			gchar* _tmp38_;
			_tmp36_ = self->priv->library;
			_tmp37_ = filename;
			_tmp38_ = g_strdup (_tmp37_);
			g_hash_table_insert (_tmp36_, _tmp38_, (gpointer) ((guintptr) buffer));
		} else {
			g_warning ("sounds.vala:97: Got an AL error while loading sound: code %d", (gint) sound_error);
		}
		data = (g_free (data), NULL);
		_sf_close0 (file);
		 (&info);
		_g_free0 (path);
		_g_free0 (filename);
		_g_object_unref0 (_tmp11_);
	}
	_tmp39_ = g_new0 (ALuint, GAMES_SOUNDS_MAX_SOURCES);
	self->priv->sources = (g_free (self->priv->sources), NULL);
	self->priv->sources = _tmp39_;
	self->priv->sources_length1 = GAMES_SOUNDS_MAX_SOURCES;
	self->priv->_sources_size_ = self->priv->sources_length1;
	_tmp40_ = self->priv->sources;
	_tmp40__length1 = self->priv->sources_length1;
	alGenSources ((ALsizei) GAMES_SOUNDS_MAX_SOURCES, _tmp40_);
	_tmp41_ = self->priv->sources;
	_tmp41__length1 = self->priv->sources_length1;
	{
		ALuint* source_collection = NULL;
		gint source_collection_length1 = 0;
		gint _source_collection_size_ = 0;
		gint source_it = 0;
		source_collection = _tmp41_;
		source_collection_length1 = _tmp41__length1;
		for (source_it = 0; source_it < source_collection_length1; source_it = source_it + 1) {
			ALuint source = 0U;
			source = source_collection[source_it];
			{
				alSourcef (source, AL_PITCH, (ALfloat) 1.0f);
				alSourcef (source, AL_GAIN, (ALfloat) 1.0f);
			}
		}
	}
	alDistanceModel (AL_NONE);
	_g_object_unref0 (file_info);
	_g_object_unref0 (enumerator);
	_g_object_unref0 (directory);
	return self;
}

GamesSounds*
games_sounds_new (const gchar* sound_dir,
                  GError** error)
{
	return games_sounds_construct (GAMES_TYPE_SOUNDS, sound_dir, error);
}

/**
     * Plays a sound by filename
     */
void
games_sounds_play (GamesSounds* self,
                   const gchar* filename)
{
	ALuint* buffer = NULL;
	GHashTable* _tmp0_;
	gconstpointer _tmp1_;
	ALuint _tmp2_;
	ALuint* _tmp3_;
	ALuint source = 0U;
	ALuint* _tmp4_;
	gint _tmp4__length1;
	ALuint _tmp5_;
	ALuint* _tmp6_;
	ALenum _error_ = 0;
	g_return_if_fail (self != NULL);
	g_return_if_fail (filename != NULL);
	_tmp0_ = self->priv->library;
	_tmp1_ = g_hash_table_lookup (_tmp0_, filename);
	_tmp2_ = (ALuint) ((guintptr) _tmp1_);
	buffer = &_tmp2_;
	_tmp3_ = buffer;
	if (_tmp3_ == NULL) {
		g_warning ("sounds.vala:119: Sound not found: %s", filename);
		return;
	}
	alGetError ();
	self->priv->source_index = (self->priv->source_index + 1) % GAMES_SOUNDS_MAX_SOURCES;
	_tmp4_ = self->priv->sources;
	_tmp4__length1 = self->priv->sources_length1;
	_tmp5_ = _tmp4_[self->priv->source_index];
	source = _tmp5_;
	alSourceStop (source);
	alSourceRewind (source);
	_tmp6_ = buffer;
	alSourcei (source, AL_BUFFER, (ALint) (*_tmp6_));
	alSourcePlay (source);
	_error_ = alGetError ();
	if (_error_ != AL_NO_ERROR) {
		g_warning ("sounds.vala:132: Got an AL error while trying to play sound: code %d", (gint) _error_);
	}
}

static void
games_sounds_real_dispose (GObject* base)
{
	GamesSounds * self;
	ALuint* _tmp0_;
	gint _tmp0__length1;
	GHashTable* _tmp1_;
	GPtrArray* _tmp2_;
	self = (GamesSounds*) base;
	_tmp0_ = self->priv->sources;
	_tmp0__length1 = self->priv->sources_length1;
	alDeleteSources ((ALsizei) GAMES_SOUNDS_MAX_SOURCES, _tmp0_);
	_tmp1_ = self->priv->library;
	_tmp2_ = g_hash_table_get_values_as_ptr_array (_tmp1_);
	{
		GPtrArray* buffer_collection = NULL;
		guint buffer_index = 0U;
		buffer_collection = _tmp2_;
		for (buffer_index = 0; buffer_index < buffer_collection->len; buffer_index = buffer_index + 1) {
			ALuint buffer = 0U;
			buffer = (ALuint) ((guintptr) g_ptr_array_index (buffer_collection, buffer_index));
			{
				alDeleteBuffers ((ALsizei) 1, &buffer);
			}
		}
		_g_ptr_array_unref0 (buffer_collection);
	}
	G_OBJECT_CLASS (games_sounds_parent_class)->dispose (G_TYPE_CHECK_INSTANCE_CAST (self, G_TYPE_OBJECT, GObject));
}

static void
games_sounds_class_init (GamesSoundsClass * klass,
                         gpointer klass_data)
{
	games_sounds_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GamesSounds_private_offset);
	((GObjectClass *) klass)->dispose = (void (*) (GObject*)) games_sounds_real_dispose;
	G_OBJECT_CLASS (klass)->finalize = games_sounds_finalize;
}

static void
games_sounds_instance_init (GamesSounds * self,
                            gpointer klass)
{
	GHashFunc _tmp0_;
	GEqualFunc _tmp1_;
	GHashTable* _tmp2_;
	self->priv = games_sounds_get_instance_private (self);
	_tmp0_ = g_str_hash;
	_tmp1_ = g_str_equal;
	_tmp2_ = g_hash_table_new_full (_tmp0_, _tmp1_, _g_free0_, NULL);
	self->priv->library = _tmp2_;
	self->priv->source_index = (guint) 0;
}

static void
games_sounds_finalize (GObject * obj)
{
	GamesSounds * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_SOUNDS, GamesSounds);
	_alcDestroyContext0 (self->priv->context);
	_alcCloseDevice0 (self->priv->device);
	_g_hash_table_unref0 (self->priv->library);
	self->priv->sources = (g_free (self->priv->sources), NULL);
	G_OBJECT_CLASS (games_sounds_parent_class)->finalize (obj);
}

static GType
games_sounds_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GamesSoundsClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_sounds_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesSounds), 0, (GInstanceInitFunc) games_sounds_instance_init, NULL };
	GType games_sounds_type_id;
	games_sounds_type_id = g_type_register_static (G_TYPE_OBJECT, "GamesSounds", &g_define_type_info, 0);
	GamesSounds_private_offset = g_type_add_instance_private (games_sounds_type_id, sizeof (GamesSoundsPrivate));
	return games_sounds_type_id;
}

GType
games_sounds_get_type (void)
{
	static volatile gsize games_sounds_type_id__once = 0;
	if (g_once_init_enter (&games_sounds_type_id__once)) {
		GType games_sounds_type_id;
		games_sounds_type_id = games_sounds_get_type_once ();
		g_once_init_leave (&games_sounds_type_id__once, games_sounds_type_id);
	}
	return games_sounds_type_id__once;
}

