1 /*
2 * gstfluiddec - fluiddec plugin for gstreamer
3 *
4 * Copyright 2013 Wim Taymans <wim.taymans@gmail.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /**
23 * SECTION:element-fluiddec
24 * @title: fluiddec
25 * @see_also: timidity, wildmidi
26 *
27 * This element renders midi-events as audio streams using
28 * <ulink url="http://fluidsynth.sourceforge.net//">Fluidsynth</ulink>.
29 * It offers better sound quality compared to the timidity or wildmidi element.
30 *
31 * ## Example pipeline
32 * |[
33 * gst-launch-1.0 filesrc location=song.mid ! midiparse ! fluiddec ! pulsesink
34 * ]| This example pipeline will parse the midi and render to raw audio which is
35 * played via pulseaudio.
36 *
37 */
38
39 #ifdef HAVE_CONFIG_H
40 # include <config.h>
41 #endif
42
43 #define FLUID_DEC_RATE 44100
44 #define FLUID_DEC_BPS (4 * 2)
45
46 #include <gst/gst.h>
47 #include <string.h>
48 #include <glib.h>
49 #include <glib/gstdio.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52
53 #include <gst/audio/audio.h>
54
55 #include "gstfluiddec.h"
56
57 GST_DEBUG_CATEGORY_STATIC (gst_fluid_dec_debug);
58 #define GST_CAT_DEFAULT gst_fluid_dec_debug
59
60 enum
61 {
62 /* FILL ME */
63 LAST_SIGNAL
64 };
65
66 #define DEFAULT_SOUNDFONT NULL
67 #define DEFAULT_SYNTH_CHORUS TRUE
68 #define DEFAULT_SYNTH_REVERB TRUE
69 #define DEFAULT_SYNTH_GAIN 0.2
70 #define DEFAULT_SYNTH_POLYPHONY 256
71
72 enum
73 {
74 PROP_0,
75 PROP_SOUNDFONT,
76 PROP_SYNTH_CHORUS,
77 PROP_SYNTH_REVERB,
78 PROP_SYNTH_GAIN,
79 PROP_SYNTH_POLYPHONY
80 };
81
82 static void gst_fluid_dec_finalize (GObject * object);
83
84 static gboolean gst_fluid_dec_sink_event (GstPad * pad, GstObject * parent,
85 GstEvent * event);
86
87 static GstStateChangeReturn gst_fluid_dec_change_state (GstElement * element,
88 GstStateChange transition);
89
90 static GstFlowReturn gst_fluid_dec_chain (GstPad * sinkpad, GstObject * parent,
91 GstBuffer * buffer);
92
93 static void gst_fluid_dec_set_property (GObject * object, guint prop_id,
94 const GValue * value, GParamSpec * pspec);
95 static void gst_fluid_dec_get_property (GObject * object, guint prop_id,
96 GValue * value, GParamSpec * pspec);
97
98 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
99 GST_PAD_SINK,
100 GST_PAD_ALWAYS,
101 GST_STATIC_CAPS ("audio/x-midi-event")
102 );
103
104 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
105 GST_PAD_SRC,
106 GST_PAD_ALWAYS,
107 GST_STATIC_CAPS ("audio/x-raw, "
108 "format = (string) " GST_AUDIO_NE (F32) ", "
109 "rate = (int) " G_STRINGIFY (FLUID_DEC_RATE) ", "
110 "channels = (int) 2, " "layout = (string) interleaved"));
111
112 #define parent_class gst_fluid_dec_parent_class
113 G_DEFINE_TYPE (GstFluidDec, gst_fluid_dec, GST_TYPE_ELEMENT);
114
115 /* fluid_synth log handler */
116 static void
gst_fluid_synth_error_log_function(int level,const char * message,void * data)117 gst_fluid_synth_error_log_function (int level, const char *message, void *data)
118 {
119 GST_ERROR ("%s", message);
120 }
121
122 static void
gst_fluid_synth_warning_log_function(int level,const char * message,void * data)123 gst_fluid_synth_warning_log_function (int level, const char *message,
124 void *data)
125 {
126 GST_WARNING ("%s", message);
127 }
128
129 static void
gst_fluid_synth_info_log_function(int level,const char * message,void * data)130 gst_fluid_synth_info_log_function (int level, const char *message, void *data)
131 {
132 GST_INFO ("%s", message);
133 }
134
135 static void
gst_fluid_synth_debug_log_function(int level,const char * message,void * data)136 gst_fluid_synth_debug_log_function (int level, const char *message, void *data)
137 {
138 GST_DEBUG ("%s", message);
139 }
140
141
142 /* initialize the plugin's class */
143 static void
gst_fluid_dec_class_init(GstFluidDecClass * klass)144 gst_fluid_dec_class_init (GstFluidDecClass * klass)
145 {
146 GObjectClass *gobject_class;
147 GstElementClass *gstelement_class;
148
149 gobject_class = (GObjectClass *) klass;
150 gstelement_class = (GstElementClass *) klass;
151
152 gobject_class->finalize = gst_fluid_dec_finalize;
153 gobject_class->set_property = gst_fluid_dec_set_property;
154 gobject_class->get_property = gst_fluid_dec_get_property;
155
156 g_object_class_install_property (gobject_class, PROP_SOUNDFONT,
157 g_param_spec_string ("soundfont",
158 "Soundfont", "the filename of a soundfont (NULL for default)",
159 DEFAULT_SOUNDFONT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160
161 g_object_class_install_property (gobject_class, PROP_SYNTH_CHORUS,
162 g_param_spec_boolean ("synth-chorus",
163 "Synth Chorus", "Turn the chorus on or off",
164 DEFAULT_SYNTH_CHORUS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165
166 g_object_class_install_property (gobject_class, PROP_SYNTH_REVERB,
167 g_param_spec_boolean ("synth-reverb",
168 "Synth Reverb", "Turn the reverb on or off",
169 DEFAULT_SYNTH_REVERB, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170
171 g_object_class_install_property (gobject_class, PROP_SYNTH_GAIN,
172 g_param_spec_double ("synth-gain",
173 "Synth Gain", "Set the master gain", 0.0, 10.0,
174 DEFAULT_SYNTH_GAIN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175
176 g_object_class_install_property (gobject_class, PROP_SYNTH_POLYPHONY,
177 g_param_spec_int ("synth-polyphony",
178 "Synth Polyphony", "The number of simultaneous voices", 1, 65535,
179 DEFAULT_SYNTH_POLYPHONY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
180
181 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
182 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
183
184 gst_element_class_set_static_metadata (gstelement_class, "Fluidsynth",
185 "Codec/Decoder/Audio",
186 "Midi Synthesizer Element", "Wim Taymans <wim.taymans@gmail.com>");
187
188 gstelement_class->change_state = gst_fluid_dec_change_state;
189
190 #ifndef GST_DISABLE_GST_DEBUG
191 fluid_set_log_function (FLUID_PANIC,
192 (fluid_log_function_t) gst_fluid_synth_error_log_function, NULL);
193 fluid_set_log_function (FLUID_ERR,
194 (fluid_log_function_t) gst_fluid_synth_warning_log_function, NULL);
195 fluid_set_log_function (FLUID_WARN,
196 (fluid_log_function_t) gst_fluid_synth_warning_log_function, NULL);
197 fluid_set_log_function (FLUID_INFO,
198 (fluid_log_function_t) gst_fluid_synth_info_log_function, NULL);
199 fluid_set_log_function (FLUID_DBG,
200 (fluid_log_function_t) gst_fluid_synth_debug_log_function, NULL);
201 #else
202 fluid_set_log_function (FLUID_PANIC, NULL, NULL);
203 fluid_set_log_function (FLUID_ERR, NULL, NULL);
204 fluid_set_log_function (FLUID_WARN, NULL, NULL);
205 fluid_set_log_function (FLUID_INFO, NULL, NULL);
206 fluid_set_log_function (FLUID_DBG, NULL, NULL);
207 #endif
208 }
209
210 /* initialize the new element
211 * instantiate pads and add them to element
212 * set functions
213 * initialize structure
214 */
215 static void
gst_fluid_dec_init(GstFluidDec * filter)216 gst_fluid_dec_init (GstFluidDec * filter)
217 {
218 filter->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
219 gst_pad_set_event_function (filter->sinkpad, gst_fluid_dec_sink_event);
220 gst_pad_set_chain_function (filter->sinkpad, gst_fluid_dec_chain);
221 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
222
223 filter->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
224 gst_pad_use_fixed_caps (filter->srcpad);
225 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
226
227 filter->soundfont = g_strdup (DEFAULT_SOUNDFONT);
228 filter->synth_chorus = DEFAULT_SYNTH_CHORUS;
229 filter->synth_reverb = DEFAULT_SYNTH_REVERB;
230 filter->synth_gain = DEFAULT_SYNTH_GAIN;
231 filter->synth_polyphony = DEFAULT_SYNTH_POLYPHONY;
232
233 filter->settings = new_fluid_settings ();
234 filter->synth = new_fluid_synth (filter->settings);
235 filter->sf = -1;
236
237 fluid_synth_set_chorus_on (filter->synth, filter->synth_chorus);
238 fluid_synth_set_reverb_on (filter->synth, filter->synth_reverb);
239 fluid_synth_set_gain (filter->synth, filter->synth_gain);
240 fluid_synth_set_polyphony (filter->synth, filter->synth_polyphony);
241 }
242
243 static void
gst_fluid_dec_finalize(GObject * object)244 gst_fluid_dec_finalize (GObject * object)
245 {
246 GstFluidDec *fluiddec = GST_FLUID_DEC (object);
247
248 delete_fluid_synth (fluiddec->synth);
249 delete_fluid_settings (fluiddec->settings);
250 g_free (fluiddec->soundfont);
251
252 G_OBJECT_CLASS (parent_class)->finalize (object);
253 }
254
255 #if 0
256 static GstBuffer *
257 gst_fluid_dec_clip_buffer (GstFluidDec * fluiddec, GstBuffer * buffer)
258 {
259 guint64 start, stop;
260 guint64 new_start, new_stop;
261 gint64 offset, length;
262
263 /* clipping disabled for now */
264 return buffer;
265
266 start = GST_BUFFER_OFFSET (buffer);
267 stop = GST_BUFFER_OFFSET_END (buffer);
268
269 if (!gst_segment_clip (&fluiddec->segment, GST_FORMAT_DEFAULT,
270 start, stop, &new_start, &new_stop)) {
271 gst_buffer_unref (buffer);
272 return NULL;
273 }
274
275 if (start == new_start && stop == new_stop)
276 return buffer;
277
278 offset = new_start - start;
279 length = new_stop - new_start;
280
281 buffer = gst_buffer_make_writable (buffer);
282 gst_buffer_resize (buffer, offset, length);
283
284 GST_BUFFER_OFFSET (buffer) = new_start;
285 GST_BUFFER_OFFSET_END (buffer) = new_stop;
286 GST_BUFFER_TIMESTAMP (buffer) =
287 gst_util_uint64_scale_int (new_start, GST_SECOND, FLUID_DEC_RATE);
288 GST_BUFFER_DURATION (buffer) =
289 gst_util_uint64_scale_int (new_stop, GST_SECOND, FLUID_DEC_RATE) -
290 GST_BUFFER_TIMESTAMP (buffer);
291
292 return buffer;
293 }
294 #endif
295
296 static void
gst_fluid_dec_reset(GstFluidDec * fluiddec)297 gst_fluid_dec_reset (GstFluidDec * fluiddec)
298 {
299 fluid_synth_system_reset (fluiddec->synth);
300 fluiddec->last_pts = GST_CLOCK_TIME_NONE;
301 }
302
303 static gboolean
gst_fluid_dec_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)304 gst_fluid_dec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
305 {
306 gboolean res;
307 GstFluidDec *fluiddec = GST_FLUID_DEC (parent);
308
309 GST_DEBUG_OBJECT (pad, "%s event received", GST_EVENT_TYPE_NAME (event));
310
311 switch (GST_EVENT_TYPE (event)) {
312 case GST_EVENT_CAPS:
313 {
314 GstCaps *caps;
315
316 caps = gst_caps_new_simple ("audio/x-raw",
317 "format", G_TYPE_STRING, GST_AUDIO_NE (F32),
318 "rate", G_TYPE_INT, FLUID_DEC_RATE,
319 "channels", G_TYPE_INT, 2,
320 "layout", G_TYPE_STRING, "interleaved", NULL);
321
322 fluid_synth_set_sample_rate (fluiddec->synth, FLUID_DEC_RATE);
323
324 res = gst_pad_push_event (fluiddec->srcpad, gst_event_new_caps (caps));
325 gst_caps_unref (caps);
326 gst_event_unref (event);
327 break;
328 }
329 case GST_EVENT_SEGMENT:
330 gst_event_copy_segment (event, &fluiddec->segment);
331 GST_DEBUG_OBJECT (fluiddec, "configured segment %" GST_SEGMENT_FORMAT,
332 &fluiddec->segment);
333 res = gst_pad_event_default (pad, parent, event);
334 break;
335 case GST_EVENT_FLUSH_STOP:
336 gst_fluid_dec_reset (fluiddec);
337 res = gst_pad_event_default (pad, parent, event);
338 break;
339 case GST_EVENT_EOS:
340 /* FIXME, push last samples */
341 res = gst_pad_event_default (pad, parent, event);
342 break;
343 default:
344 res = gst_pad_event_default (pad, parent, event);
345 break;
346 }
347 return res;
348 }
349
350 static GstFlowReturn
produce_samples(GstFluidDec * fluiddec,GstClockTime pts,guint64 sample)351 produce_samples (GstFluidDec * fluiddec, GstClockTime pts, guint64 sample)
352 {
353 GstClockTime duration, timestamp;
354 guint64 samples, offset;
355 GstMapInfo info;
356 GstBuffer *outbuf;
357
358 samples = sample - fluiddec->last_sample;
359 duration = pts - fluiddec->last_pts;
360 offset = fluiddec->last_sample;
361 timestamp = fluiddec->last_pts;
362
363 fluiddec->last_pts = pts;
364 fluiddec->last_sample = sample;
365
366 if (samples == 0)
367 return GST_FLOW_OK;
368
369 GST_DEBUG_OBJECT (fluiddec, "duration %" GST_TIME_FORMAT
370 ", samples %" G_GUINT64_FORMAT, GST_TIME_ARGS (duration), samples);
371
372 outbuf = gst_buffer_new_allocate (NULL, samples * FLUID_DEC_BPS, NULL);
373
374 gst_buffer_map (outbuf, &info, GST_MAP_WRITE);
375 fluid_synth_write_float (fluiddec->synth, samples, info.data, 0, 2,
376 info.data, 1, 2);
377 gst_buffer_unmap (outbuf, &info);
378
379 GST_BUFFER_DTS (outbuf) = timestamp;
380 GST_BUFFER_PTS (outbuf) = timestamp;
381 GST_BUFFER_DURATION (outbuf) = duration;
382 GST_BUFFER_OFFSET (outbuf) = offset;
383 GST_BUFFER_OFFSET_END (outbuf) = offset + samples;
384
385 if (fluiddec->discont) {
386 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
387 fluiddec->discont = FALSE;
388 }
389
390 return gst_pad_push (fluiddec->srcpad, outbuf);
391 }
392
393 static void
handle_buffer(GstFluidDec * fluiddec,GstBuffer * buffer)394 handle_buffer (GstFluidDec * fluiddec, GstBuffer * buffer)
395 {
396 GstMapInfo info;
397 guint8 event;
398
399 gst_buffer_map (buffer, &info, GST_MAP_READ);
400
401 if (info.size == 0)
402 goto done;
403
404 event = info.data[0];
405
406 switch (event & 0xf0) {
407 case 0xf0:
408 switch (event) {
409 case 0xff:
410 GST_DEBUG_OBJECT (fluiddec, "system reset");
411 fluid_synth_system_reset (fluiddec->synth);
412 break;
413 case 0xf0:
414 case 0xf7:
415 GST_DEBUG_OBJECT (fluiddec, "sysex 0x%02x", event);
416 GST_MEMDUMP_OBJECT (fluiddec, "bytes ", info.data + 1, info.size - 1);
417 fluid_synth_sysex (fluiddec->synth, (char *) info.data + 1,
418 info.size - 1, NULL, NULL, NULL, 0);
419
420 break;
421 case 0xf9:
422 GST_LOG_OBJECT (fluiddec, "midi tick");
423 break;
424 default:
425 GST_WARNING_OBJECT (fluiddec, "unhandled event 0x%02x", event);
426 break;
427 }
428 break;
429 default:
430 {
431 guint8 channel, p1, p2;
432
433 channel = event & 0x0f;
434
435 p1 = info.size > 1 ? info.data[1] & 0x7f : 0;
436 p2 = info.size > 2 ? info.data[2] & 0x7f : 0;
437
438 GST_DEBUG_OBJECT (fluiddec, "event 0x%02x channel %d, 0x%02x 0x%02x",
439 event, channel, p1, p2);
440
441 switch (event & 0xf0) {
442 case 0x80:
443 fluid_synth_noteoff (fluiddec->synth, channel, p1);
444 break;
445 case 0x90:
446 fluid_synth_noteon (fluiddec->synth, channel, p1, p2);
447 break;
448 case 0xA0:
449 /* aftertouch */
450 break;
451 case 0xB0:
452 fluid_synth_cc (fluiddec->synth, channel, p1, p2);
453 break;
454 case 0xC0:
455 fluid_synth_program_change (fluiddec->synth, channel, p1);
456 break;
457 case 0xD0:
458 fluid_synth_channel_pressure (fluiddec->synth, channel, p1);
459 break;
460 case 0xE0:
461 fluid_synth_pitch_bend (fluiddec->synth, channel, (p2 << 7) | p1);
462 break;
463 default:
464 break;
465 }
466 break;
467 }
468 }
469
470 done:
471
472 gst_buffer_unmap (buffer, &info);
473 }
474
475 static GstFlowReturn
gst_fluid_dec_chain(GstPad * sinkpad,GstObject * parent,GstBuffer * buffer)476 gst_fluid_dec_chain (GstPad * sinkpad, GstObject * parent, GstBuffer * buffer)
477 {
478 GstFlowReturn res = GST_FLOW_OK;
479 GstFluidDec *fluiddec;
480 GstClockTime pts;
481
482 fluiddec = GST_FLUID_DEC (parent);
483
484 if (GST_BUFFER_IS_DISCONT (buffer)) {
485 fluiddec->discont = TRUE;
486 }
487
488 pts = GST_BUFFER_PTS (buffer);
489
490 if (pts != GST_CLOCK_TIME_NONE) {
491 guint64 sample =
492 gst_util_uint64_scale_int (pts, FLUID_DEC_RATE, GST_SECOND);
493
494 if (fluiddec->last_pts == GST_CLOCK_TIME_NONE) {
495 fluiddec->last_pts = pts;
496 fluiddec->last_sample = sample;
497 } else if (fluiddec->last_pts < pts) {
498 /* generate samples for the elapsed time */
499 res = produce_samples (fluiddec, pts, sample);
500 }
501 }
502
503 if (res == GST_FLOW_OK) {
504 handle_buffer (fluiddec, buffer);
505 }
506 gst_buffer_unref (buffer);
507
508 return res;
509 }
510
511 static gboolean
gst_fluid_dec_open(GstFluidDec * fluiddec)512 gst_fluid_dec_open (GstFluidDec * fluiddec)
513 {
514 GDir *dir;
515 GError *error = NULL;
516 const gchar *const *sharedirs;
517
518 if (fluiddec->sf != -1)
519 return TRUE;
520
521 if (fluiddec->soundfont) {
522 GST_DEBUG_OBJECT (fluiddec, "loading soundfont file %s",
523 fluiddec->soundfont);
524
525 fluiddec->sf = fluid_synth_sfload (fluiddec->synth, fluiddec->soundfont, 1);
526 if (fluiddec->sf == -1)
527 goto load_failed;
528
529 GST_DEBUG_OBJECT (fluiddec, "loaded soundfont file %s",
530 fluiddec->soundfont);
531 } else {
532 gint i, j;
533 /* ubuntu/debian in sounds/sf[23], fedora in soundfonts */
534 static const gchar *paths[] =
535 { "sounds/sf3/", "sounds/sf2/", "soundfonts/", NULL };
536
537 sharedirs = g_get_system_data_dirs ();
538
539 for (i = 0; sharedirs[i]; i++) {
540 for (j = 0; paths[j]; j++) {
541 gchar *soundfont_path = g_build_path ("/", sharedirs[i], paths[j],
542 NULL);
543 GST_DEBUG_OBJECT (fluiddec, "Trying to list contents of a %s directory",
544 soundfont_path);
545 error = NULL;
546 dir = g_dir_open (soundfont_path, 0, &error);
547 if (dir == NULL) {
548 GST_DEBUG_OBJECT (fluiddec,
549 "Can't open a potential soundfont directory %s: %s",
550 soundfont_path, error->message);
551 g_free (soundfont_path);
552 g_error_free (error);
553 continue;
554 }
555
556 while (TRUE) {
557 const gchar *name;
558 gchar *filename;
559
560 if ((name = g_dir_read_name (dir)) == NULL)
561 break;
562
563 filename = g_build_filename (soundfont_path, name, NULL);
564
565 GST_DEBUG_OBJECT (fluiddec, "loading soundfont file %s", filename);
566 fluiddec->sf = fluid_synth_sfload (fluiddec->synth, filename, 1);
567 if (fluiddec->sf != -1) {
568 GST_DEBUG_OBJECT (fluiddec, "loaded soundfont file %s", filename);
569 g_free (filename);
570 g_dir_close (dir);
571 g_free (soundfont_path);
572 goto done;
573 }
574 GST_DEBUG_OBJECT (fluiddec, "could not load soundfont file %s",
575 filename);
576 g_free (filename);
577 }
578 g_dir_close (dir);
579 g_free (soundfont_path);
580 }
581 }
582 if (fluiddec->sf == -1) {
583 goto no_soundfont;
584 }
585 }
586 done:
587 return TRUE;
588
589 /* ERRORS */
590 load_failed:
591 {
592 GST_ELEMENT_ERROR (fluiddec, RESOURCE, OPEN_READ,
593 ("Can't open soundfont %s", fluiddec->soundfont),
594 ("failed to open soundfont file %s for reading", fluiddec->soundfont));
595 return FALSE;
596 }
597 no_soundfont:
598 {
599 GST_ELEMENT_ERROR (fluiddec, RESOURCE, OPEN_READ,
600 ("Can't find a soundfont file in subdirectories of XDG_DATA_DIRS paths"),
601 ("no usable soundfont files found in subdirectories of XDG_DATA_DIRS"));
602 return FALSE;
603 }
604 }
605
606 static gboolean
gst_fluid_dec_close(GstFluidDec * fluiddec)607 gst_fluid_dec_close (GstFluidDec * fluiddec)
608 {
609 if (fluiddec->sf) {
610 fluid_synth_sfunload (fluiddec->synth, fluiddec->sf, 1);
611 fluiddec->sf = -1;
612 }
613 return TRUE;
614 }
615
616 static GstStateChangeReturn
gst_fluid_dec_change_state(GstElement * element,GstStateChange transition)617 gst_fluid_dec_change_state (GstElement * element, GstStateChange transition)
618 {
619 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
620 GstFluidDec *fluiddec = GST_FLUID_DEC (element);
621
622 switch (transition) {
623 case GST_STATE_CHANGE_NULL_TO_READY:
624 if (!gst_fluid_dec_open (fluiddec))
625 goto open_failed;
626 break;
627 case GST_STATE_CHANGE_READY_TO_PAUSED:
628 gst_fluid_dec_reset (fluiddec);
629 break;
630 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
631 break;
632 default:
633 break;
634 }
635
636 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
637
638 switch (transition) {
639 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
640 break;
641 case GST_STATE_CHANGE_PAUSED_TO_READY:
642 break;
643 case GST_STATE_CHANGE_READY_TO_NULL:
644 gst_fluid_dec_close (fluiddec);
645 break;
646 default:
647 break;
648 }
649
650 return ret;
651
652 /* ERRORS */
653 open_failed:
654 {
655 GST_ERROR_OBJECT (fluiddec, "could not open");
656 return GST_STATE_CHANGE_FAILURE;
657 }
658 }
659
660 static void
gst_fluid_dec_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)661 gst_fluid_dec_set_property (GObject * object, guint prop_id,
662 const GValue * value, GParamSpec * pspec)
663 {
664 GstFluidDec *fluiddec = GST_FLUID_DEC (object);
665
666 switch (prop_id) {
667 case PROP_SOUNDFONT:
668 g_free (fluiddec->soundfont);
669 fluiddec->soundfont = g_value_dup_string (value);
670 break;
671 case PROP_SYNTH_CHORUS:
672 fluiddec->synth_chorus = g_value_get_boolean (value);
673 fluid_synth_set_chorus_on (fluiddec->synth, fluiddec->synth_chorus);
674 break;
675 case PROP_SYNTH_REVERB:
676 fluiddec->synth_reverb = g_value_get_boolean (value);
677 fluid_synth_set_reverb_on (fluiddec->synth, fluiddec->synth_reverb);
678 break;
679 case PROP_SYNTH_GAIN:
680 fluiddec->synth_gain = g_value_get_double (value);
681 fluid_synth_set_gain (fluiddec->synth, fluiddec->synth_gain);
682 break;
683 case PROP_SYNTH_POLYPHONY:
684 fluiddec->synth_polyphony = g_value_get_int (value);
685 fluid_synth_set_polyphony (fluiddec->synth, fluiddec->synth_polyphony);
686 break;
687 default:
688 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
689 break;
690 }
691 }
692
693 static void
gst_fluid_dec_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)694 gst_fluid_dec_get_property (GObject * object, guint prop_id,
695 GValue * value, GParamSpec * pspec)
696 {
697 GstFluidDec *fluiddec = GST_FLUID_DEC (object);
698
699 switch (prop_id) {
700 case PROP_SOUNDFONT:
701 g_value_set_string (value, fluiddec->soundfont);
702 break;
703 case PROP_SYNTH_CHORUS:
704 g_value_set_boolean (value, fluiddec->synth_chorus);
705 break;
706 case PROP_SYNTH_REVERB:
707 g_value_set_boolean (value, fluiddec->synth_reverb);
708 break;
709 case PROP_SYNTH_GAIN:
710 g_value_set_double (value, fluiddec->synth_gain);
711 break;
712 case PROP_SYNTH_POLYPHONY:
713 g_value_set_int (value, fluiddec->synth_polyphony);
714 break;
715 default:
716 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
717 break;
718 }
719 }
720
721 static gboolean
plugin_init(GstPlugin * plugin)722 plugin_init (GstPlugin * plugin)
723 {
724 GST_DEBUG_CATEGORY_INIT (gst_fluid_dec_debug, "fluiddec",
725 0, "Fluidsynth MIDI decoder plugin");
726
727 return gst_element_register (plugin, "fluiddec",
728 GST_RANK_SECONDARY, GST_TYPE_FLUID_DEC);
729 }
730
731 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
732 GST_VERSION_MINOR,
733 fluidsynthmidi,
734 "Fluidsynth MIDI Plugin",
735 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
736