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 /* elementfactory information */
28 GstElementDetails mikmod_details = {
30 "Codec/Decoder/Audio",
31 "Module decoder based on libmikmod",
32 "Jeremy SIMON <jsimon13@yahoo.fr>",
36 /* Filter signals and args */
64 GstClockTime timestamp;
67 static GstStaticPadTemplate mikmod_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
70 GST_STATIC_CAPS ("audio/x-raw-int, "
71 "endianness = (int) BYTE_ORDER, "
72 "signed = (boolean) TRUE, "
75 "rate = (int) { 8000, 11025, 22050, 44100 }, "
76 "channels = (int) [ 1, 2 ]; "
78 "endianness = (int) BYTE_ORDER, "
79 "signed = (boolean) FALSE, "
82 "rate = (int) { 8000, 11025, 22050, 44100 }, "
83 "channels = (int) [ 1, 2 ]")
86 static GstStaticPadTemplate mikmod_sink_factory =
87 GST_STATIC_PAD_TEMPLATE ("sink",
90 GST_STATIC_CAPS ("audio/x-mod")
93 static void gst_mikmod_base_init (gpointer g_class);
94 static void gst_mikmod_class_init (GstMikModClass * klass);
95 static void gst_mikmod_init (GstMikMod * filter);
96 static void gst_mikmod_set_property (GObject * object, guint id,
97 const GValue * value, GParamSpec * pspec);
98 static void gst_mikmod_get_property (GObject * object, guint id, GValue * value,
100 static GstPadLinkReturn gst_mikmod_srclink (GstPad * pad, const GstCaps * caps);
101 static GstCaps *gst_mikmod_srcfixate (GstPad * pad, const GstCaps * caps);
102 static void gst_mikmod_loop (GstElement * element);
103 static gboolean gst_mikmod_setup (GstMikMod * mikmod);
104 static GstStateChangeReturn gst_mikmod_change_state (GstElement * element,
105 GstStateChange transition);
109 static GstElementClass *parent_class = NULL;
112 gst_mikmod_get_type (void)
114 static GType mikmod_type = 0;
117 static const GTypeInfo mikmod_info = {
118 sizeof (GstMikModClass),
119 gst_mikmod_base_init,
121 (GClassInitFunc) gst_mikmod_class_init,
126 (GInstanceInitFunc) gst_mikmod_init,
130 g_type_register_static (GST_TYPE_ELEMENT, "GstMikmod", &mikmod_info, 0);
136 gst_mikmod_base_init (gpointer g_class)
138 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
140 gst_element_class_add_pad_template (element_class,
141 gst_static_pad_template_get (&mikmod_src_factory));
142 gst_element_class_add_pad_template (element_class,
143 gst_static_pad_template_get (&mikmod_sink_factory));
144 gst_element_class_set_details (element_class, &mikmod_details);
148 gst_mikmod_class_init (GstMikModClass * klass)
150 GObjectClass *gobject_class;
151 GstElementClass *gstelement_class;
153 gobject_class = (GObjectClass *) klass;
154 gstelement_class = (GstElementClass *) klass;
156 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
158 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SONGNAME,
159 g_param_spec_string ("songname", "songname", "songname",
160 NULL, G_PARAM_READABLE));
161 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MODTYPE,
162 g_param_spec_string ("modtype", "modtype", "modtype",
163 NULL, G_PARAM_READABLE));
164 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MUSICVOLUME,
165 g_param_spec_int ("musicvolume", "musivolume", "musicvolume",
166 0, 128, 128, G_PARAM_READWRITE));
167 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PANSEP,
168 g_param_spec_int ("pansep", "pansep", "pansep",
169 0, 128, 128, G_PARAM_READWRITE));
170 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_REVERB,
171 g_param_spec_int ("reverb", "reverb", "reverb",
172 0, 15, 0, G_PARAM_READWRITE));
173 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SNDFXVOLUME,
174 g_param_spec_int ("sndfxvolume", "sndfxvolume", "sndfxvolume",
175 0, 128, 128, G_PARAM_READWRITE));
176 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_VOLUME,
177 g_param_spec_int ("volume", "volume", "volume",
178 0, 128, 96, G_PARAM_READWRITE));
180 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_INTERP,
181 g_param_spec_boolean ("interp", "interp", "interp",
182 FALSE, G_PARAM_READWRITE));
183 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_REVERSE,
184 g_param_spec_boolean ("reverse", "reverse", "reverse",
185 FALSE, G_PARAM_READWRITE));
186 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SURROUND,
187 g_param_spec_boolean ("surround", "surround", "surround",
188 TRUE, G_PARAM_READWRITE));
189 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HQMIXER,
190 g_param_spec_boolean ("hqmixer", "hqmixer", "hqmixer",
191 FALSE, G_PARAM_READWRITE));
192 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SOFT_MUSIC,
193 g_param_spec_boolean ("soft_music", "soft_music", "soft_music",
194 TRUE, G_PARAM_READWRITE));
195 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SOFT_SNDFX,
196 g_param_spec_boolean ("soft_sndfx", "soft_sndfx", "soft_sndfx",
197 TRUE, G_PARAM_READWRITE));
200 gobject_class->set_property = gst_mikmod_set_property;
201 gobject_class->get_property = gst_mikmod_get_property;
203 gstelement_class->change_state = gst_mikmod_change_state;
208 gst_mikmod_init (GstMikMod * filter)
211 gst_pad_new_from_template (gst_static_pad_template_get
212 (&mikmod_sink_factory), "sink");
214 gst_pad_new_from_template (gst_static_pad_template_get
215 (&mikmod_src_factory), "src");
217 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
218 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
219 gst_pad_set_link_function (filter->srcpad, gst_mikmod_srclink);
220 gst_pad_set_fixate_function (filter->srcpad, gst_mikmod_srcfixate);
222 gst_element_set_loop_function (GST_ELEMENT (filter), gst_mikmod_loop);
224 filter->Buffer = NULL;
226 filter->stereo = TRUE;
227 filter->surround = TRUE;
228 filter->_16bit = TRUE;
229 filter->soft_music = TRUE;
230 filter->soft_sndfx = TRUE;
231 filter->mixfreq = 44100;
233 filter->pansep = 128;
234 filter->musicvolume = 128;
236 filter->sndfxvolume = 128;
237 filter->songname = NULL;
238 filter->modtype = NULL;
240 filter->initialized = FALSE;
244 gst_mikmod_srcfixate (GstPad * pad, const GstCaps * caps)
247 GstStructure *structure;
249 /* FIXME: select est caps here */
250 if (gst_caps_get_size (caps) > 1)
253 ret = gst_caps_copy (caps);
254 structure = gst_caps_get_structure (ret, 0);
256 if (gst_structure_fixate_field_nearest_int (structure, "channels", 2))
258 if (gst_structure_fixate_field_nearest_int (structure, "rate", 44100))
265 static GstPadLinkReturn
266 gst_mikmod_srclink (GstPad * pad, const GstCaps * caps)
269 GstStructure *structure;
274 filter = GST_MIKMOD (gst_pad_get_parent (pad));
276 structure = gst_caps_get_structure (caps, 0);
278 gst_structure_get_int (structure, "depth", &depth);
279 filter->_16bit = (depth == 16);
280 gst_structure_get_int (structure, "channels", &channels);
281 filter->stereo = (channels == 2);
282 gst_structure_get_int (structure, "rate", &filter->mixfreq);
284 result = gst_mikmod_setup (filter);
285 gst_object_unref (filter);
288 return GST_PAD_LINK_OK;
290 return GST_PAD_LINK_REFUSED;
295 gst_mikmod_loop (GstElement * element)
298 GstBuffer *buffer_in;
300 g_return_if_fail (element != NULL);
301 g_return_if_fail (GST_IS_MIKMOD (element));
303 mikmod = GST_MIKMOD (element);
304 srcpad = mikmod->srcpad;
305 mikmod->Buffer = NULL;
307 if (!mikmod->initialized) {
308 while ((buffer_in = GST_BUFFER (gst_pad_pull (mikmod->sinkpad)))) {
309 if (GST_IS_EVENT (buffer_in)) {
310 GstEvent *event = GST_EVENT (buffer_in);
312 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS)
315 if (mikmod->Buffer) {
318 merge = gst_buffer_merge (mikmod->Buffer, buffer_in);
319 gst_buffer_unref (buffer_in);
320 gst_buffer_unref (mikmod->Buffer);
321 mikmod->Buffer = merge;
323 mikmod->Buffer = buffer_in;
328 if (!GST_PAD_CAPS (mikmod->srcpad)) {
329 if (GST_PAD_LINK_SUCCESSFUL (gst_pad_renegotiate (mikmod->srcpad))) {
330 GST_ELEMENT_ERROR (mikmod, CORE, NEGOTIATION, (NULL), (NULL));
335 MikMod_RegisterDriver (&drv_gst);
336 MikMod_RegisterAllLoaders ();
339 reader = GST_READER_new (mikmod);
340 module = Player_LoadGeneric (reader, 64, 0);
342 gst_buffer_unref (mikmod->Buffer);
344 if (!Player_Active ())
345 Player_Start (module);
347 mikmod->initialized = TRUE;
350 if (Player_Active ()) {
351 timestamp = (module->sngtime / 1024.0) * GST_SECOND;
354 gst_element_set_eos (GST_ELEMENT (mikmod));
355 gst_pad_push (mikmod->srcpad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
361 gst_mikmod_setup (GstMikMod * mikmod)
363 md_musicvolume = mikmod->musicvolume;
364 md_pansep = mikmod->pansep;
365 md_reverb = mikmod->reverb;
366 md_sndfxvolume = mikmod->sndfxvolume;
367 md_volume = mikmod->volume;
368 md_mixfreq = mikmod->mixfreq;
373 md_mode = md_mode | DMODE_INTERP;
376 md_mode = md_mode | DMODE_REVERSE;
378 if (mikmod->surround)
379 md_mode = md_mode | DMODE_SURROUND;
382 md_mode = md_mode | DMODE_16BITS;
385 md_mode = md_mode | DMODE_HQMIXER;
387 if (mikmod->soft_music)
388 md_mode = md_mode | DMODE_SOFT_MUSIC;
390 if (mikmod->soft_sndfx)
391 md_mode = md_mode | DMODE_SOFT_SNDFX;
394 md_mode = md_mode | DMODE_STEREO;
400 static GstStateChangeReturn
401 gst_mikmod_change_state (GstElement * element, GstStateChange transition)
405 g_return_val_if_fail (GST_IS_MIKMOD (element), GST_STATE_CHANGE_FAILURE);
407 mikmod = GST_MIKMOD (element);
409 GST_DEBUG ("state pending %d", GST_STATE_PENDING (element));
411 if (GST_STATE_PENDING (element) == GST_STATE_READY) {
412 gst_mikmod_setup (mikmod);
414 if (Player_Active ()) {
415 Player_TogglePause ();
416 Player_SetPosition (0);
418 mikmod->initialized = FALSE;
421 if (GST_STATE_PENDING (element) == GST_STATE_PLAYING) {
422 if (Player_Active () && Player_Paused ())
423 Player_TogglePause ();
424 else if (!Player_Active ())
425 Player_Start (module);
429 if (GST_STATE_PENDING (element) == GST_STATE_PAUSED)
430 if (Player_Active () && !Player_Paused ())
431 Player_TogglePause ();
433 if (GST_STATE_PENDING (element) == GST_STATE_NULL)
437 if (GST_ELEMENT_CLASS (parent_class)->change_state)
438 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
440 return GST_STATE_CHANGE_SUCCESS;
446 gst_mikmod_set_property (GObject * object, guint id, const GValue * value,
451 g_return_if_fail (GST_IS_MIKMOD (object));
452 filter = GST_MIKMOD (object);
456 g_free (filter->songname);
457 filter->songname = g_strdup (g_value_get_string (value));
460 g_free (filter->modtype);
461 filter->modtype = g_strdup (g_value_get_string (value));
463 case ARG_MUSICVOLUME:
464 filter->musicvolume = g_value_get_int (value);
467 filter->pansep = g_value_get_int (value);
470 filter->reverb = g_value_get_int (value);
472 case ARG_SNDFXVOLUME:
473 filter->sndfxvolume = g_value_get_int (value);
476 filter->volume = g_value_get_int (value);
479 filter->interp = g_value_get_boolean (value);
482 filter->reverse = g_value_get_boolean (value);
485 filter->surround = g_value_get_boolean (value);
488 filter->hqmixer = g_value_get_boolean (value);
491 filter->soft_music = g_value_get_boolean (value);
494 filter->soft_sndfx = g_value_get_boolean (value);
502 gst_mikmod_get_property (GObject * object, guint id, GValue * value,
507 g_return_if_fail (GST_IS_MIKMOD (object));
508 filter = GST_MIKMOD (object);
511 case ARG_MUSICVOLUME:
512 g_value_set_int (value, filter->musicvolume);
515 g_value_set_int (value, filter->pansep);
518 g_value_set_int (value, filter->reverb);
520 case ARG_SNDFXVOLUME:
521 g_value_set_int (value, filter->sndfxvolume);
524 g_value_set_int (value, filter->volume);
527 g_value_set_boolean (value, filter->interp);
530 g_value_set_boolean (value, filter->reverse);
533 g_value_set_boolean (value, filter->surround);
536 g_value_set_boolean (value, filter->hqmixer);
539 g_value_set_boolean (value, filter->soft_music);
542 g_value_set_boolean (value, filter->soft_sndfx);
550 plugin_init (GstPlugin * plugin)
552 if (!gst_element_register (plugin, "mikmod", GST_RANK_SECONDARY,
559 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
562 "Mikmod plugin library",
563 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)