/* pa-player.c generated by valac 0.36.1.17-97a62, the Vala compiler
 * generated from pa-player.vala, do not modify */

/* This file is part of retro-gtk. License: GPL-3.0+.*/

#include <glib.h>
#include <glib-object.h>
#include <float.h>
#include <math.h>
#include <pulse/simple.h>
#include <pulse/pulseaudio.h>


#define RETRO_TYPE_PA_PLAYER (retro_pa_player_get_type ())
#define RETRO_PA_PLAYER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RETRO_TYPE_PA_PLAYER, RetroPaPlayer))
#define RETRO_PA_PLAYER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RETRO_TYPE_PA_PLAYER, RetroPaPlayerClass))
#define RETRO_IS_PA_PLAYER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RETRO_TYPE_PA_PLAYER))
#define RETRO_IS_PA_PLAYER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RETRO_TYPE_PA_PLAYER))
#define RETRO_PA_PLAYER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RETRO_TYPE_PA_PLAYER, RetroPaPlayerClass))

typedef struct _RetroPaPlayer RetroPaPlayer;
typedef struct _RetroPaPlayerClass RetroPaPlayerClass;
typedef struct _RetroPaPlayerPrivate RetroPaPlayerPrivate;

#define RETRO_TYPE_CORE (retro_core_get_type ())
#define RETRO_CORE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RETRO_TYPE_CORE, RetroCore))
#define RETRO_CORE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RETRO_TYPE_CORE, RetroCoreClass))
#define RETRO_IS_CORE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RETRO_TYPE_CORE))
#define RETRO_IS_CORE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RETRO_TYPE_CORE))
#define RETRO_CORE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RETRO_TYPE_CORE, RetroCoreClass))

typedef struct _RetroCore RetroCore;
typedef struct _RetroCoreClass RetroCoreClass;
enum  {
	RETRO_PA_PLAYER_DUMMY_PROPERTY,
	RETRO_PA_PLAYER_LAST_PROPERTY
};
static GParamSpec* retro_pa_player_properties[RETRO_PA_PLAYER_LAST_PROPERTY];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _pa_simple_free0(var) ((var == NULL) ? NULL : (var = (pa_simple_free (var), NULL)))

struct _RetroPaPlayer {
	GObject parent_instance;
	RetroPaPlayerPrivate * priv;
};

struct _RetroPaPlayerClass {
	GObjectClass parent_class;
};

struct _RetroPaPlayerPrivate {
	RetroCore* core;
	gulong on_audio_output_id;
	gdouble sample_rate;
	pa_simple* simple;
};


static gpointer retro_pa_player_parent_class = NULL;

GType retro_pa_player_get_type (void) G_GNUC_CONST;
GType retro_core_get_type (void) G_GNUC_CONST;
#define RETRO_PA_PLAYER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RETRO_TYPE_PA_PLAYER, RetroPaPlayerPrivate))
void retro_pa_player_set_core (RetroPaPlayer* self, RetroCore* core);
static void retro_pa_player_on_audio_output (RetroPaPlayer* self, gint16* data, int data_length1, gdouble sample_rate);
static void _retro_pa_player_on_audio_output_retro_core_audio_output (RetroCore* _sender, gint16* frames, int frames_length1, gdouble sample_rate, gpointer self);
static void retro_pa_player_prepare_for_sample_rate (RetroPaPlayer* self, gdouble sample_rate);
RetroPaPlayer* retro_pa_player_new (void);
RetroPaPlayer* retro_pa_player_construct (GType object_type);
static void retro_pa_player_finalize (GObject * obj);


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


static void _retro_pa_player_on_audio_output_retro_core_audio_output (RetroCore* _sender, gint16* frames, int frames_length1, gdouble sample_rate, gpointer self) {
	retro_pa_player_on_audio_output ((RetroPaPlayer*) self, frames, frames_length1, sample_rate);
}


void retro_pa_player_set_core (RetroPaPlayer* self, RetroCore* core) {
	RetroCore* _tmp0_;
	RetroCore* _tmp3_;
	RetroCore* _tmp4_;
	RetroCore* _tmp5_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->core;
	if (_tmp0_ != NULL) {
		RetroCore* _tmp1_;
		gulong _tmp2_;
		_tmp1_ = self->priv->core;
		_tmp2_ = self->priv->on_audio_output_id;
		g_signal_handler_disconnect ((GObject*) _tmp1_, _tmp2_);
	}
	_tmp3_ = core;
	_tmp4_ = _g_object_ref0 (_tmp3_);
	_g_object_unref0 (self->priv->core);
	self->priv->core = _tmp4_;
	_tmp5_ = self->priv->core;
	if (_tmp5_ != NULL) {
		RetroCore* _tmp6_;
		gulong _tmp7_;
		_tmp6_ = core;
		_tmp7_ = g_signal_connect_object (_tmp6_, "audio-output", (GCallback) _retro_pa_player_on_audio_output_retro_core_audio_output, self, 0);
		self->priv->on_audio_output_id = _tmp7_;
	}
	_pa_simple_free0 (self->priv->simple);
	self->priv->simple = NULL;
}


static void retro_pa_player_on_audio_output (RetroPaPlayer* self, gint16* data, int data_length1, gdouble sample_rate) {
	gboolean _tmp0_ = FALSE;
	pa_simple* _tmp1_;
	pa_simple* _tmp5_;
	gint16* _tmp6_;
	gint _tmp6__length1;
	gint16* _tmp7_;
	gint _tmp7__length1;
	g_return_if_fail (self != NULL);
	_tmp1_ = self->priv->simple;
	if (_tmp1_ == NULL) {
		_tmp0_ = TRUE;
	} else {
		gdouble _tmp2_;
		gdouble _tmp3_;
		_tmp2_ = sample_rate;
		_tmp3_ = self->priv->sample_rate;
		_tmp0_ = _tmp2_ != _tmp3_;
	}
	if (_tmp0_) {
		gdouble _tmp4_;
		_tmp4_ = sample_rate;
		retro_pa_player_prepare_for_sample_rate (self, _tmp4_);
	}
	_tmp5_ = self->priv->simple;
	_tmp6_ = data;
	_tmp6__length1 = data_length1;
	_tmp7_ = data;
	_tmp7__length1 = data_length1;
	pa_simple_write (_tmp5_, _tmp6_, (gsize) (sizeof (gint16) * _tmp7__length1), NULL);
}


static void retro_pa_player_prepare_for_sample_rate (RetroPaPlayer* self, gdouble sample_rate) {
	gdouble _tmp0_;
	pa_sample_spec sample_spec;
	gdouble _tmp1_;
	pa_sample_spec _tmp2_ = {0};
	pa_sample_spec _tmp3_;
	pa_simple* _tmp4_;
	g_return_if_fail (self != NULL);
	_tmp0_ = sample_rate;
	self->priv->sample_rate = _tmp0_;
	_tmp1_ = sample_rate;
	pa_sample_spec_init (&_tmp2_);
	_tmp2_.format = PA_SAMPLE_S16NE;
	_tmp2_.rate = (guint32) _tmp1_;
	_tmp2_.channels = (guint8) 2;
	sample_spec = _tmp2_;
	_tmp3_ = sample_spec;
	_tmp4_ = pa_simple_new (NULL, NULL, PA_STREAM_PLAYBACK, NULL, "", &_tmp3_, NULL, NULL, NULL);
	_pa_simple_free0 (self->priv->simple);
	self->priv->simple = _tmp4_;
}


RetroPaPlayer* retro_pa_player_construct (GType object_type) {
	RetroPaPlayer * self = NULL;
	self = (RetroPaPlayer*) g_object_new (object_type, NULL);
	return self;
}


RetroPaPlayer* retro_pa_player_new (void) {
	return retro_pa_player_construct (RETRO_TYPE_PA_PLAYER);
}


static void retro_pa_player_class_init (RetroPaPlayerClass * klass) {
	retro_pa_player_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (RetroPaPlayerPrivate));
	G_OBJECT_CLASS (klass)->finalize = retro_pa_player_finalize;
}


static void retro_pa_player_instance_init (RetroPaPlayer * self) {
	self->priv = RETRO_PA_PLAYER_GET_PRIVATE (self);
}


static void retro_pa_player_finalize (GObject * obj) {
	RetroPaPlayer * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RETRO_TYPE_PA_PLAYER, RetroPaPlayer);
	_g_object_unref0 (self->priv->core);
	_pa_simple_free0 (self->priv->simple);
	G_OBJECT_CLASS (retro_pa_player_parent_class)->finalize (obj);
}


GType retro_pa_player_get_type (void) {
	static volatile gsize retro_pa_player_type_id__volatile = 0;
	if (g_once_init_enter (&retro_pa_player_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RetroPaPlayerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) retro_pa_player_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RetroPaPlayer), 0, (GInstanceInitFunc) retro_pa_player_instance_init, NULL };
		GType retro_pa_player_type_id;
		retro_pa_player_type_id = g_type_register_static (G_TYPE_OBJECT, "RetroPaPlayer", &g_define_type_info, 0);
		g_once_init_leave (&retro_pa_player_type_id__volatile, retro_pa_player_type_id);
	}
	return retro_pa_player_type_id__volatile;
}



