2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include "gstmikmod.h"
27 /* Filter signals and args */
55 GstClockTime timestamp;
58 static GstStaticPadTemplate mikmod_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
61 GST_STATIC_CAPS ("audio/x-raw-int, "
62 "endianness = (int) BYTE_ORDER, "
63 "signed = (boolean) TRUE, "
66 "rate = (int) { 8000, 11025, 22050, 44100 }, "
67 "channels = (int) [ 1, 2 ]; "
69 "endianness = (int) BYTE_ORDER, "
70 "signed = (boolean) FALSE, "
73 "rate = (int) { 8000, 11025, 22050, 44100 }, "
74 "channels = (int) [ 1, 2 ]")
77 static GstStaticPadTemplate mikmod_sink_factory =
78 GST_STATIC_PAD_TEMPLATE ("sink",
81 GST_STATIC_CAPS ("audio/x-mod")
84 static void gst_mikmod_base_init (gpointer g_class);
85 static void gst_mikmod_class_init (GstMikModClass * klass);
86 static void gst_mikmod_init (GstMikMod * filter);
87 static void gst_mikmod_set_property (GObject * object, guint id,
88 const GValue * value, GParamSpec * pspec);
89 static void gst_mikmod_get_property (GObject * object, guint id, GValue * value,
91 static GstPadLinkReturn gst_mikmod_srclink (GstPad * pad, const GstCaps * caps);
92 static GstCaps *gst_mikmod_srcfixate (GstPad * pad, const GstCaps * caps);
93 static void gst_mikmod_loop (GstElement * element);
94 static gboolean gst_mikmod_setup (GstMikMod * mikmod);
95 static GstStateChangeReturn gst_mikmod_change_state (GstElement * element,
96 GstStateChange transition);
100 static GstElementClass *parent_class = NULL;
103 gst_mikmod_get_type (void)
105 static GType mikmod_type = 0;
108 static const GTypeInfo mikmod_info = {
109 sizeof (GstMikModClass),
110 gst_mikmod_base_init,
112 (GClassInitFunc) gst_mikmod_class_init,
117 (GInstanceInitFunc) gst_mikmod_init,
121 g_type_register_static (GST_TYPE_ELEMENT, "GstMikmod", &mikmod_info, 0);
127 gst_mikmod_base_init (gpointer g_class)
129 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
131 gst_element_class_add_pad_template (element_class,
132 gst_static_pad_template_get (&mikmod_src_factory));
133 gst_element_class_add_pad_template (element_class,
134 gst_static_pad_template_get (&mikmod_sink_factory));
135 gst_element_class_set_details_simple (element_class, "MikMod audio decoder",
136 "Codec/Decoder/Audio",
137 "Module decoder based on libmikmod", "Jeremy SIMON <jsimon13@yahoo.fr>");
141 gst_mikmod_class_init (GstMikModClass * klass)
143 GObjectClass *gobject_class;
144 GstElementClass *gstelement_class;
146 gobject_class = (GObjectClass *) klass;
147 gstelement_class = (GstElementClass *) klass;
149 parent_class = g_type_class_peek_parent (klass);
151 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SONGNAME,
152 g_param_spec_string ("songname", "songname", "songname",
153 NULL, G_PARAM_READABLE));
154 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MODTYPE,
155 g_param_spec_string ("modtype", "modtype", "modtype",
156 NULL, G_PARAM_READABLE));
157 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MUSICVOLUME,
158 g_param_spec_int ("musicvolume", "musivolume", "musicvolume",
159 0, 128, 128, G_PARAM_READWRITE));
160 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PANSEP,
161 g_param_spec_int ("pansep", "pansep", "pansep",
162 0, 128, 128, G_PARAM_READWRITE));
163 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_REVERB,
164 g_param_spec_int ("reverb", "reverb", "reverb",
165 0, 15, 0, G_PARAM_READWRITE));
166 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SNDFXVOLUME,
167 g_param_spec_int ("sndfxvolume", "sndfxvolume", "sndfxvolume",
168 0, 128, 128, G_PARAM_READWRITE));
169 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_VOLUME,
170 g_param_spec_int ("volume", "volume", "volume",
171 0, 128, 96, G_PARAM_READWRITE));
173 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_INTERP,
174 g_param_spec_boolean ("interp", "interp", "interp",
175 FALSE, G_PARAM_READWRITE));
176 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_REVERSE,
177 g_param_spec_boolean ("reverse", "reverse", "reverse",
178 FALSE, G_PARAM_READWRITE));
179 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SURROUND,
180 g_param_spec_boolean ("surround", "surround", "surround",
181 TRUE, G_PARAM_READWRITE));
182 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HQMIXER,
183 g_param_spec_boolean ("hqmixer", "hqmixer", "hqmixer",
184 FALSE, G_PARAM_READWRITE));
185 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SOFT_MUSIC,
186 g_param_spec_boolean ("soft-music", "soft music", "soft music",
187 TRUE, G_PARAM_READWRITE));
188 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SOFT_SNDFX,
189 g_param_spec_boolean ("soft-sndfx", "soft sndfx", "soft sndfx",
190 TRUE, G_PARAM_READWRITE));
193 gobject_class->set_property = gst_mikmod_set_property;
194 gobject_class->get_property = gst_mikmod_get_property;
196 gstelement_class->change_state = gst_mikmod_change_state;
201 gst_mikmod_init (GstMikMod * filter)
204 gst_pad_new_from_static_template (&mikmod_sink_factory, "sink");
206 gst_pad_new_from_static_template (&mikmod_src_factory, "src");
208 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
209 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
210 gst_pad_set_link_function (filter->srcpad, gst_mikmod_srclink);
211 gst_pad_set_fixate_function (filter->srcpad, gst_mikmod_srcfixate);
213 gst_element_set_loop_function (GST_ELEMENT (filter), gst_mikmod_loop);
215 filter->Buffer = NULL;
217 filter->stereo = TRUE;
218 filter->surround = TRUE;
219 filter->_16bit = TRUE;
220 filter->soft_music = TRUE;
221 filter->soft_sndfx = TRUE;
222 filter->mixfreq = 44100;
224 filter->pansep = 128;
225 filter->musicvolume = 128;
227 filter->sndfxvolume = 128;
228 filter->songname = NULL;
229 filter->modtype = NULL;
231 filter->initialized = FALSE;
235 gst_mikmod_srcfixate (GstPad * pad, const GstCaps * caps)
238 GstStructure *structure;
240 /* FIXME: select est caps here */
241 if (gst_caps_get_size (caps) > 1)
244 ret = gst_caps_copy (caps);
245 structure = gst_caps_get_structure (ret, 0);
247 if (gst_structure_fixate_field_nearest_int (structure, "channels", 2))
249 if (gst_structure_fixate_field_nearest_int (structure, "rate", 44100))
256 static GstPadLinkReturn
257 gst_mikmod_srclink (GstPad * pad, const GstCaps * caps)
260 GstStructure *structure;
265 filter = GST_MIKMOD (gst_pad_get_parent (pad));
267 structure = gst_caps_get_structure (caps, 0);
269 gst_structure_get_int (structure, "depth", &depth);
270 filter->_16bit = (depth == 16);
271 gst_structure_get_int (structure, "channels", &channels);
272 filter->stereo = (channels == 2);
273 gst_structure_get_int (structure, "rate", &filter->mixfreq);
275 result = gst_mikmod_setup (filter);
276 gst_object_unref (filter);
279 return GST_PAD_LINK_OK;
281 return GST_PAD_LINK_REFUSED;
286 gst_mikmod_loop (GstElement * element)
289 GstBuffer *buffer_in;
291 g_return_if_fail (element != NULL);
292 g_return_if_fail (GST_IS_MIKMOD (element));
294 mikmod = GST_MIKMOD (element);
295 srcpad = mikmod->srcpad;
296 mikmod->Buffer = NULL;
298 if (!mikmod->initialized) {
299 while ((buffer_in = GST_BUFFER (gst_pad_pull (mikmod->sinkpad)))) {
300 if (GST_IS_EVENT (buffer_in)) {
301 GstEvent *event = GST_EVENT (buffer_in);
303 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS)
306 if (mikmod->Buffer) {
309 merge = gst_buffer_merge (mikmod->Buffer, buffer_in);
310 gst_buffer_unref (buffer_in);
311 gst_buffer_unref (mikmod->Buffer);
312 mikmod->Buffer = merge;
314 mikmod->Buffer = buffer_in;
319 if (!GST_PAD_CAPS (mikmod->srcpad)) {
320 if (GST_PAD_LINK_SUCCESSFUL (gst_pad_renegotiate (mikmod->srcpad))) {
321 GST_ELEMENT_ERROR (mikmod, CORE, NEGOTIATION, (NULL), (NULL));
326 MikMod_RegisterDriver (&drv_gst);
327 MikMod_RegisterAllLoaders ();
330 reader = GST_READER_new (mikmod);
331 module = Player_LoadGeneric (reader, 64, 0);
333 gst_buffer_unref (mikmod->Buffer);
335 if (!Player_Active ())
336 Player_Start (module);
338 mikmod->initialized = TRUE;
341 if (Player_Active ()) {
342 timestamp = (module->sngtime / 1024.0) * GST_SECOND;
345 gst_element_set_eos (GST_ELEMENT (mikmod));
346 gst_pad_push (mikmod->srcpad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
352 gst_mikmod_setup (GstMikMod * mikmod)
354 md_musicvolume = mikmod->musicvolume;
355 md_pansep = mikmod->pansep;
356 md_reverb = mikmod->reverb;
357 md_sndfxvolume = mikmod->sndfxvolume;
358 md_volume = mikmod->volume;
359 md_mixfreq = mikmod->mixfreq;
364 md_mode = md_mode | DMODE_INTERP;
367 md_mode = md_mode | DMODE_REVERSE;
369 if (mikmod->surround)
370 md_mode = md_mode | DMODE_SURROUND;
373 md_mode = md_mode | DMODE_16BITS;
376 md_mode = md_mode | DMODE_HQMIXER;
378 if (mikmod->soft_music)
379 md_mode = md_mode | DMODE_SOFT_MUSIC;
381 if (mikmod->soft_sndfx)
382 md_mode = md_mode | DMODE_SOFT_SNDFX;
385 md_mode = md_mode | DMODE_STEREO;
391 static GstStateChangeReturn
392 gst_mikmod_change_state (GstElement * element, GstStateChange transition)
396 g_return_val_if_fail (GST_IS_MIKMOD (element), GST_STATE_CHANGE_FAILURE);
398 mikmod = GST_MIKMOD (element);
400 GST_DEBUG ("state pending %d", GST_STATE_PENDING (element));
402 if (GST_STATE_PENDING (element) == GST_STATE_READY) {
403 gst_mikmod_setup (mikmod);
405 if (Player_Active ()) {
406 Player_TogglePause ();
407 Player_SetPosition (0);
409 mikmod->initialized = FALSE;
412 if (GST_STATE_PENDING (element) == GST_STATE_PLAYING) {
413 if (Player_Active () && Player_Paused ())
414 Player_TogglePause ();
415 else if (!Player_Active ())
416 Player_Start (module);
420 if (GST_STATE_PENDING (element) == GST_STATE_PAUSED)
421 if (Player_Active () && !Player_Paused ())
422 Player_TogglePause ();
424 if (GST_STATE_PENDING (element) == GST_STATE_NULL)
428 if (GST_ELEMENT_CLASS (parent_class)->change_state)
429 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
431 return GST_STATE_CHANGE_SUCCESS;
437 gst_mikmod_set_property (GObject * object, guint id, const GValue * value,
442 g_return_if_fail (GST_IS_MIKMOD (object));
443 filter = GST_MIKMOD (object);
447 g_free (filter->songname);
448 filter->songname = g_strdup (g_value_get_string (value));
451 g_free (filter->modtype);
452 filter->modtype = g_strdup (g_value_get_string (value));
454 case ARG_MUSICVOLUME:
455 filter->musicvolume = g_value_get_int (value);
458 filter->pansep = g_value_get_int (value);
461 filter->reverb = g_value_get_int (value);
463 case ARG_SNDFXVOLUME:
464 filter->sndfxvolume = g_value_get_int (value);
467 filter->volume = g_value_get_int (value);
470 filter->interp = g_value_get_boolean (value);
473 filter->reverse = g_value_get_boolean (value);
476 filter->surround = g_value_get_boolean (value);
479 filter->hqmixer = g_value_get_boolean (value);
482 filter->soft_music = g_value_get_boolean (value);
485 filter->soft_sndfx = g_value_get_boolean (value);
493 gst_mikmod_get_property (GObject * object, guint id, GValue * value,
498 g_return_if_fail (GST_IS_MIKMOD (object));
499 filter = GST_MIKMOD (object);
502 case ARG_MUSICVOLUME:
503 g_value_set_int (value, filter->musicvolume);
506 g_value_set_int (value, filter->pansep);
509 g_value_set_int (value, filter->reverb);
511 case ARG_SNDFXVOLUME:
512 g_value_set_int (value, filter->sndfxvolume);
515 g_value_set_int (value, filter->volume);
518 g_value_set_boolean (value, filter->interp);
521 g_value_set_boolean (value, filter->reverse);
524 g_value_set_boolean (value, filter->surround);
527 g_value_set_boolean (value, filter->hqmixer);
530 g_value_set_boolean (value, filter->soft_music);
533 g_value_set_boolean (value, filter->soft_sndfx);
541 plugin_init (GstPlugin * plugin)
543 if (!gst_element_register (plugin, "mikmod", GST_RANK_SECONDARY,
550 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
553 "Mikmod plugin library",
554 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)