blob: b5fbcb2527371de7b1657b485741b3d930b96447 [file] [log] [blame]
Andy Wingob0c01cd2002-03-20 21:45:04 +00001/* GStreamer
Jeremy Simonac87bfc2002-02-28 21:10:42 +00002 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 *
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.
8 *
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.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
Tim-Philipp Müller9e1b75f2012-11-03 20:38:00 +000016 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
Jeremy Simonac87bfc2002-02-28 21:10:42 +000018 */
Thomas Vander Stichele87960e42004-03-14 23:20:41 +000019
Jeremy Simon870168f2002-03-01 17:25:42 +000020/*
21 Code based on modplugxmms
22 XMMS plugin:
23 Kenton Varda <temporal@gauge3d.org>
24 Sound Engine:
25 Olivier Lapicque <olivierl@jps.net>
26*/
Jeremy Simonac87bfc2002-02-28 21:10:42 +000027
Stefan Kostb28ee642007-07-03 07:52:52 +000028/**
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +000029 * SECTION:element-modplug
30 *
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +000031 * Modplug uses the <ulink url="http://modplug-xmms.sourceforge.net/">modplug</ulink>
32 * library to decode tracked music in the MOD/S3M/XM/IT and related formats.
Stefan Kost2affd3d2008-06-13 11:59:23 +000033 *
34 * <refsect2>
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +000035 * <title>Example pipeline</title>
Stefan Kost2affd3d2008-06-13 11:59:23 +000036 * |[
Luis de Bethencourt4d6b9c42015-02-24 13:16:21 +000037 * gst-launch-1.0 -v filesrc location=1990s-nostalgia.xm ! modplug ! audioconvert ! alsasink
Stefan Kost2affd3d2008-06-13 11:59:23 +000038 * ]| Play a FastTracker xm file.
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +000039 * </refsect2>
40 */
41
Ronald S. Bultjecb906222003-11-07 12:47:02 +000042#ifdef HAVE_CONFIG_H
43#include "config.h"
44#endif
45
Benjamin Otte06242ff2010-03-24 15:05:50 +010046/* Required to not get an undefined warning
47 * https://bugzilla.gnome.org/show_bug.cgi?id=613795
48 */
49#ifndef WORDS_BIGENDIAN
50#define WORDS_BIGENDIAN 0
51#endif
52
Iain Lanefd4cb222013-09-10 08:48:32 +000053#include <libmodplug/stdafx.h>
LRNd4ff7292009-03-19 15:37:17 -070054#include <libmodplug/sndfile.h>
Jeremy Simonac87bfc2002-02-28 21:10:42 +000055
56#include "gstmodplug.h"
57
Jeremy Simondc461762002-06-09 19:00:27 +000058#include <gst/gst.h>
Jeremy Simonac87bfc2002-02-28 21:10:42 +000059#include <stdlib.h>
Jeremy Simon80299932002-11-17 12:23:43 +000060#include <gst/audio/audio.h>
Jeremy Simonac87bfc2002-02-28 21:10:42 +000061
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +000062GST_DEBUG_CATEGORY_STATIC (modplug_debug);
63#define GST_CAT_DEFAULT modplug_debug
64
Thomas Vander Stichele87960e42004-03-14 23:20:41 +000065enum
66{
Jeremy Simonac87bfc2002-02-28 21:10:42 +000067 ARG_0,
68 ARG_SONGNAME,
69 ARG_REVERB,
70 ARG_REVERB_DEPTH,
71 ARG_REVERB_DELAY,
72 ARG_MEGABASS,
73 ARG_MEGABASS_AMOUNT,
74 ARG_MEGABASS_RANGE,
Jeremy Simonac87bfc2002-02-28 21:10:42 +000075 ARG_NOISE_REDUCTION,
76 ARG_SURROUND,
77 ARG_SURROUND_DEPTH,
78 ARG_SURROUND_DELAY,
David Schleefb144bc62003-12-22 01:47:09 +000079 ARG_OVERSAMP
Jeremy Simonac87bfc2002-02-28 21:10:42 +000080};
81
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +000082#define DEFAULT_REVERB FALSE
83#define DEFAULT_REVERB_DEPTH 30
84#define DEFAULT_REVERB_DELAY 100
85#define DEFAULT_MEGABASS FALSE
86#define DEFAULT_MEGABASS_AMOUNT 40
87#define DEFAULT_MEGABASS_RANGE 30
88#define DEFAULT_SURROUND TRUE
89#define DEFAULT_SURROUND_DEPTH 20
90#define DEFAULT_SURROUND_DELAY 20
91#define DEFAULT_OVERSAMP TRUE
92#define DEFAULT_NOISE_REDUCTION TRUE
93
Sebastian Drögebce2f8f2016-02-18 00:24:04 +020094#define FORMATS "{ " GST_AUDIO_NE (S32) ", " GST_AUDIO_NE (S16) ", U8 }"
Wim Taymans9fcfa602012-02-09 18:08:22 +010095
David Schleefb144bc62003-12-22 01:47:09 +000096static GstStaticPadTemplate modplug_src_template_factory =
Sebastian Drögeafd50a72012-07-28 00:32:58 +020097GST_STATIC_PAD_TEMPLATE ("src",
Thomas Vander Stichele87960e42004-03-14 23:20:41 +000098 GST_PAD_SRC,
99 GST_PAD_ALWAYS,
Wim Taymans9fcfa602012-02-09 18:08:22 +0100100 GST_STATIC_CAPS ("audio/x-raw,"
101 " format = (string) " FORMATS ", "
102 " layout = (string) interleaved, "
Kaj-Michael Lang212cf432009-12-29 16:07:59 +0200103 " rate = (int) { 8000, 11025, 22050, 44100 },"
Stefan Kost77d01622007-08-21 12:59:00 +0000104 " channels = (int) [ 1, 2 ]"));
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000105
David Schleefb144bc62003-12-22 01:47:09 +0000106static GstStaticPadTemplate modplug_sink_template_factory =
Hans de Goedee4afe4b2007-07-14 18:48:25 +0000107 GST_STATIC_PAD_TEMPLATE ("sink",
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000108 GST_PAD_SINK,
109 GST_PAD_ALWAYS,
Hans de Goedee4afe4b2007-07-14 18:48:25 +0000110 GST_STATIC_CAPS ("audio/x-mod; audio/x-xm; audio/x-it; audio/x-s3m; "
111 "audio/x-stm"));
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000112
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000113static void gst_modplug_dispose (GObject * object);
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000114static void gst_modplug_set_property (GObject * object,
115 guint id, const GValue * value, GParamSpec * pspec);
116static void gst_modplug_get_property (GObject * object,
117 guint id, GValue * value, GParamSpec * pspec);
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000118
Wim Taymans9fcfa602012-02-09 18:08:22 +0100119static gboolean gst_modplug_src_event (GstPad * pad, GstObject * parent,
120 GstEvent * event);
121static gboolean gst_modplug_src_query (GstPad * pad, GstObject * parent,
122 GstQuery * query);
Jan Schmidt012dfb82005-09-05 17:20:29 +0000123static GstStateChangeReturn gst_modplug_change_state (GstElement * element,
124 GstStateChange transition);
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000125
Wim Taymans9fcfa602012-02-09 18:08:22 +0100126static gboolean gst_modplug_sinkpad_activate (GstPad * pad, GstObject * parent);
127static gboolean gst_modplug_sinkpad_activate_mode (GstPad * pad,
128 GstObject * parent, GstPadMode mode, gboolean active);
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000129static void gst_modplug_loop (GstModPlug * element);
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000130
Wim Taymans9fcfa602012-02-09 18:08:22 +0100131#define parent_class gst_modplug_parent_class
132G_DEFINE_TYPE (GstModPlug, gst_modplug, GST_TYPE_ELEMENT);
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000133
134static void
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000135gst_modplug_class_init (GstModPlugClass * klass)
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000136{
137 GObjectClass *gobject_class;
138 GstElementClass *gstelement_class;
139
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000140 gobject_class = (GObjectClass *) klass;
141 gstelement_class = (GstElementClass *) klass;
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000142
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000143 gobject_class->set_property = gst_modplug_set_property;
144 gobject_class->get_property = gst_modplug_get_property;
145 gobject_class->dispose = gst_modplug_dispose;
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000146
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000147 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SONGNAME,
148 g_param_spec_string ("songname", "Songname", "The song name",
Stefan Kost0387a892010-10-19 16:23:23 +0300149 NULL, (GParamFlags) (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000150
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000151 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_REVERB,
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000152 g_param_spec_boolean ("reverb", "reverb", "Reverb",
Stefan Kost0387a892010-10-19 16:23:23 +0300153 DEFAULT_REVERB,
154 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000155
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000156 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_REVERB_DEPTH,
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000157 g_param_spec_int ("reverb-depth", "reverb depth", "Reverb depth",
Stefan Kost0387a892010-10-19 16:23:23 +0300158 0, 100, DEFAULT_REVERB_DEPTH,
159 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000160
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000161 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_REVERB_DELAY,
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000162 g_param_spec_int ("reverb-delay", "reverb delay", "Reverb delay",
Stefan Kost0387a892010-10-19 16:23:23 +0300163 0, 200, DEFAULT_REVERB_DELAY,
164 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000165
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000166 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MEGABASS,
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000167 g_param_spec_boolean ("megabass", "megabass", "Megabass",
Stefan Kost0387a892010-10-19 16:23:23 +0300168 DEFAULT_MEGABASS,
169 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000170
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000171 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MEGABASS_AMOUNT,
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000172 g_param_spec_int ("megabass-amount", "megabass amount", "Megabass amount",
Stefan Kost0387a892010-10-19 16:23:23 +0300173 0, 100, DEFAULT_MEGABASS_AMOUNT,
174 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000175
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000176 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MEGABASS_RANGE,
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000177 g_param_spec_int ("megabass-range", "megabass range", "Megabass range",
Stefan Kost0387a892010-10-19 16:23:23 +0300178 0, 100, DEFAULT_MEGABASS_RANGE,
179 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000180
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000181 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SURROUND,
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000182 g_param_spec_boolean ("surround", "surround", "Surround",
Stefan Kost0387a892010-10-19 16:23:23 +0300183 DEFAULT_SURROUND,
184 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000185
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000186 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SURROUND_DEPTH,
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000187 g_param_spec_int ("surround-depth", "surround depth", "Surround depth",
Stefan Kost0387a892010-10-19 16:23:23 +0300188 0, 100, DEFAULT_SURROUND_DEPTH,
189 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000190
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000191 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SURROUND_DELAY,
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000192 g_param_spec_int ("surround-delay", "surround delay", "Surround delay",
Stefan Kost0387a892010-10-19 16:23:23 +0300193 0, 40, DEFAULT_SURROUND_DELAY,
194 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000195
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000196 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_OVERSAMP,
197 g_param_spec_boolean ("oversamp", "oversamp", "oversamp",
Stefan Kost0387a892010-10-19 16:23:23 +0300198 DEFAULT_OVERSAMP,
199 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000200
201 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NOISE_REDUCTION,
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000202 g_param_spec_boolean ("noise-reduction", "noise reduction",
203 "noise reduction", DEFAULT_NOISE_REDUCTION,
Stefan Kost0387a892010-10-19 16:23:23 +0300204 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
Jeremy Simon3f676b12002-12-30 17:21:45 +0000205
Jens Granseuer9b1bffb2007-01-03 16:41:10 +0000206 gstelement_class->change_state = gst_modplug_change_state;
Wim Taymans9fcfa602012-02-09 18:08:22 +0100207
Vineeth TM8cdfb132016-03-04 15:50:26 +0900208 gst_element_class_add_static_pad_template (gstelement_class, &modplug_sink_template_factory);
209 gst_element_class_add_static_pad_template (gstelement_class, &modplug_src_template_factory);
Wim Taymans9fcfa602012-02-09 18:08:22 +0100210
Tim-Philipp Müller32ba17c2012-10-17 17:34:26 +0100211 gst_element_class_set_static_metadata (gstelement_class, "ModPlug",
Wim Taymans9fcfa602012-02-09 18:08:22 +0100212 "Codec/Decoder/Audio", "Module decoder based on modplug engine",
213 "Jeremy SIMON <jsimon13@yahoo.fr>");
214
215 GST_DEBUG_CATEGORY_INIT (modplug_debug, "modplug", 0, "ModPlug element");
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000216}
217
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000218static void
Wim Taymans9fcfa602012-02-09 18:08:22 +0100219gst_modplug_init (GstModPlug * modplug)
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000220{
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000221 /* create the sink and src pads */
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000222 modplug->sinkpad =
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000223 gst_pad_new_from_static_template (&modplug_sink_template_factory, "sink");
224 gst_pad_set_activate_function (modplug->sinkpad,
225 GST_DEBUG_FUNCPTR (gst_modplug_sinkpad_activate));
Wim Taymans9fcfa602012-02-09 18:08:22 +0100226 gst_pad_set_activatemode_function (modplug->sinkpad,
227 GST_DEBUG_FUNCPTR (gst_modplug_sinkpad_activate_mode));
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000228 gst_element_add_pad (GST_ELEMENT (modplug), modplug->sinkpad);
Jeremy Simon80299932002-11-17 12:23:43 +0000229
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000230 modplug->srcpad =
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000231 gst_pad_new_from_static_template (&modplug_src_template_factory, "src");
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000232 gst_pad_set_event_function (modplug->srcpad,
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000233 GST_DEBUG_FUNCPTR (gst_modplug_src_event));
234 gst_pad_set_query_function (modplug->srcpad,
235 GST_DEBUG_FUNCPTR (gst_modplug_src_query));
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000236 gst_element_add_pad (GST_ELEMENT (modplug), modplug->srcpad);
237
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000238 modplug->reverb = DEFAULT_REVERB;
239 modplug->reverb_depth = DEFAULT_REVERB_DEPTH;
240 modplug->reverb_delay = DEFAULT_REVERB_DELAY;
241 modplug->megabass = DEFAULT_MEGABASS;
242 modplug->megabass_amount = DEFAULT_MEGABASS_AMOUNT;
243 modplug->megabass_range = DEFAULT_MEGABASS_RANGE;
244 modplug->surround = DEFAULT_SURROUND;
245 modplug->surround_depth = DEFAULT_SURROUND_DEPTH;
246 modplug->surround_delay = DEFAULT_SURROUND_DELAY;
247 modplug->oversamp = DEFAULT_OVERSAMP;
248 modplug->noise_reduction = DEFAULT_NOISE_REDUCTION;
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000249
Kaj-Michael Lang212cf432009-12-29 16:07:59 +0200250 modplug->bits = 16;
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000251 modplug->channel = 2;
252 modplug->frequency = 44100;
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000253}
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000254
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000255
256static void
257gst_modplug_dispose (GObject * object)
258{
259 GstModPlug *modplug = GST_MODPLUG (object);
260
261 G_OBJECT_CLASS (parent_class)->dispose (object);
262
263 if (modplug->buffer) {
264 gst_buffer_unref (modplug->buffer);
265 modplug->buffer = NULL;
266 }
267}
268
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000269static gboolean
Wim Taymans9fcfa602012-02-09 18:08:22 +0100270gst_modplug_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000271{
272 GstModPlug *modplug;
273 gboolean res = FALSE;
274
Wim Taymans9fcfa602012-02-09 18:08:22 +0100275 modplug = GST_MODPLUG (parent);
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000276
277 switch (GST_QUERY_TYPE (query)) {
278 case GST_QUERY_DURATION:
279 {
280 GstFormat format;
281
Wim Taymans9fcfa602012-02-09 18:08:22 +0100282 if (!modplug->mSoundFile)
283 goto done;
284
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000285 gst_query_parse_duration (query, &format, NULL);
286 if (format == GST_FORMAT_TIME) {
287 gst_query_set_duration (query, format, modplug->song_length);
288 res = TRUE;
289 }
290 }
291 break;
292 case GST_QUERY_POSITION:
293 {
294 GstFormat format;
295
Wim Taymans9fcfa602012-02-09 18:08:22 +0100296 if (!modplug->mSoundFile)
297 goto done;
298
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000299 gst_query_parse_position (query, &format, NULL);
300 if (format == GST_FORMAT_TIME) {
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000301 gint64 pos;
302
Jonathan Matthewbaf64862006-12-11 09:51:17 +0000303 pos = (modplug->song_length * modplug->mSoundFile->GetCurrentPos ());
304 pos /= modplug->mSoundFile->GetMaxPosition ();
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000305 gst_query_set_position (query, format, pos);
306 res = TRUE;
307 }
308 }
309 break;
310 default:
Wim Taymans9fcfa602012-02-09 18:08:22 +0100311 res = gst_pad_query_default (pad, parent, query);
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000312 break;
313 }
314
315done:
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000316 return res;
317}
318
319static gboolean
Wim Taymans9fcfa602012-02-09 18:08:22 +0100320gst_modplug_do_seek (GstModPlug * modplug, GstEvent * event)
321{
322 gdouble rate;
323 GstFormat format;
324 GstSeekFlags flags;
325 GstSeekType cur_type, stop_type;
326 gboolean flush;
327 gint64 cur, stop;
328 GstSegment seg;
Wim Taymans9fcfa602012-02-09 18:08:22 +0100329
330 if (modplug->frequency == 0)
331 goto no_song;
332
Stefan Sauer0e3917b2013-12-20 18:12:53 +0100333 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
334 &stop_type, &stop);
Wim Taymans9fcfa602012-02-09 18:08:22 +0100335
336 if (format != GST_FORMAT_TIME)
337 goto no_time;
338
339 /* FIXME: we should be using GstSegment for all this */
340 if (cur_type != GST_SEEK_TYPE_SET || stop_type != GST_SEEK_TYPE_NONE)
341 goto not_supported;
342
343 if (stop_type == GST_SEEK_TYPE_NONE)
344 stop = GST_CLOCK_TIME_NONE;
Stefan Sauer0e3917b2013-12-20 18:12:53 +0100345 if (!GST_CLOCK_TIME_IS_VALID (stop) && modplug->song_length > 0)
346 stop = modplug->song_length;
Wim Taymans9fcfa602012-02-09 18:08:22 +0100347
Stefan Sauer0e3917b2013-12-20 18:12:53 +0100348 cur = CLAMP (cur, -1, modplug->song_length);
Wim Taymans9fcfa602012-02-09 18:08:22 +0100349
350 GST_DEBUG_OBJECT (modplug, "seek to %" GST_TIME_FORMAT,
351 GST_TIME_ARGS ((guint64) cur));
352
353 modplug->seek_at = cur;
354
355 flush = ((flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH);
356
357 if (flush) {
358 gst_pad_push_event (modplug->srcpad, gst_event_new_flush_start ());
359 } else {
360 gst_pad_stop_task (modplug->sinkpad);
361 }
362
363 GST_PAD_STREAM_LOCK (modplug->sinkpad);
364
365 if (flags & GST_SEEK_FLAG_SEGMENT) {
366 gst_element_post_message (GST_ELEMENT (modplug),
367 gst_message_new_segment_start (GST_OBJECT (modplug), format, cur));
368 }
Wim Taymans9fcfa602012-02-09 18:08:22 +0100369
370 if (flush) {
371 gst_pad_push_event (modplug->srcpad, gst_event_new_flush_stop (TRUE));
372 }
373
374 GST_LOG_OBJECT (modplug, "sending newsegment from %" GST_TIME_FORMAT "-%"
375 GST_TIME_FORMAT ", pos=%" GST_TIME_FORMAT,
376 GST_TIME_ARGS ((guint64) cur), GST_TIME_ARGS ((guint64) stop),
377 GST_TIME_ARGS ((guint64) cur));
378
379 gst_segment_init (&seg, GST_FORMAT_TIME);
380 seg.rate = rate;
381 seg.start = cur;
382 seg.stop = stop;
383 seg.time = cur;
384 gst_pad_push_event (modplug->srcpad, gst_event_new_segment (&seg));
385
386 modplug->offset =
387 gst_util_uint64_scale_int (cur, modplug->frequency, GST_SECOND);
388
389 gst_pad_start_task (modplug->sinkpad,
Wim Taymansdbed7262012-06-20 10:34:48 +0200390 (GstTaskFunction) gst_modplug_loop, modplug, NULL);
Wim Taymans9fcfa602012-02-09 18:08:22 +0100391
392 GST_PAD_STREAM_UNLOCK (modplug->sinkpad);
393
394 return TRUE;
395
396 /* ERROR */
397no_song:
398 {
399 GST_DEBUG_OBJECT (modplug, "no song loaded yet");
400 return FALSE;
401 }
402no_time:
403 {
404 GST_DEBUG_OBJECT (modplug, "seeking is only supported in TIME format");
405 return FALSE;
406 }
407not_supported:
408 {
409 GST_DEBUG_OBJECT (modplug, "unsupported seek type");
410 return FALSE;
411 }
412}
413
414static gboolean
415gst_modplug_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000416{
417 GstModPlug *modplug;
418 gboolean res = FALSE;
419
Wim Taymans9fcfa602012-02-09 18:08:22 +0100420 modplug = GST_MODPLUG (parent);
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000421
422 switch (GST_EVENT_TYPE (event)) {
423 case GST_EVENT_SEEK:
Wim Taymans9fcfa602012-02-09 18:08:22 +0100424 res = gst_modplug_do_seek (modplug, event);
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000425 break;
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000426 default:
Wim Taymans9fcfa602012-02-09 18:08:22 +0100427 res = gst_pad_event_default (pad, parent, event);
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000428 break;
429 }
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000430 return res;
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000431}
432
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000433static gboolean
434gst_modplug_load_song (GstModPlug * modplug)
435{
Wim Taymans9fcfa602012-02-09 18:08:22 +0100436 GstCaps *newcaps;
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000437 GstStructure *structure;
Wim Taymans9fcfa602012-02-09 18:08:22 +0100438 GstMapInfo map;
Sebastian Drögeafd50a72012-07-28 00:32:58 +0200439 const gchar *format;
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000440
Stefan Kost77d01622007-08-21 12:59:00 +0000441 GST_DEBUG_OBJECT (modplug, "Setting caps");
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000442
443 /* negotiate srcpad caps */
Wim Taymans9fcfa602012-02-09 18:08:22 +0100444 if ((newcaps = gst_pad_get_allowed_caps (modplug->srcpad)) == NULL) {
445 newcaps = gst_pad_get_pad_template_caps (modplug->srcpad);
Stefan Kost77d01622007-08-21 12:59:00 +0000446 }
Wim Taymans9fcfa602012-02-09 18:08:22 +0100447 newcaps = gst_caps_make_writable (newcaps);
448
Sebastian Drögeafd50a72012-07-28 00:32:58 +0200449 GST_DEBUG_OBJECT (modplug, "allowed caps %" GST_PTR_FORMAT, newcaps);
Wim Taymans9fcfa602012-02-09 18:08:22 +0100450
451 structure = gst_caps_get_structure (newcaps, 0);
452
Sebastian Drögeafd50a72012-07-28 00:32:58 +0200453 if (!gst_structure_fixate_field_string (structure, "format",
454 GST_AUDIO_NE (S16)))
Wim Taymans9fcfa602012-02-09 18:08:22 +0100455 GST_WARNING_OBJECT (modplug, "Failed to fixate format to S16NE");
456 if (!gst_structure_fixate_field_nearest_int (structure, "rate", 44100))
457 GST_WARNING_OBJECT (modplug, "Failed to fixate rate to 44100");
458 if (!gst_structure_fixate_field_nearest_int (structure, "channels", 2))
Sebastian Drögeafd50a72012-07-28 00:32:58 +0200459 GST_WARNING_OBJECT (modplug,
460 "Failed to fixate number of channels to stereo");
Wim Taymans9fcfa602012-02-09 18:08:22 +0100461
Sebastian Drögeafd50a72012-07-28 00:32:58 +0200462 GST_DEBUG_OBJECT (modplug, "normalized caps %" GST_PTR_FORMAT, newcaps);
Wim Taymans9fcfa602012-02-09 18:08:22 +0100463
Wim Taymans77299ba2012-03-11 19:06:59 +0100464 newcaps = gst_caps_fixate (newcaps);
Wim Taymans9fcfa602012-02-09 18:08:22 +0100465
Sebastian Drögeafd50a72012-07-28 00:32:58 +0200466 GST_DEBUG_OBJECT (modplug, "fixated caps %" GST_PTR_FORMAT, newcaps);
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000467
468 /* set up modplug to output the negotiated format */
469 structure = gst_caps_get_structure (newcaps, 0);
Wim Taymans9fcfa602012-02-09 18:08:22 +0100470 format = gst_structure_get_string (structure, "format");
471
472 if (g_str_equal (format, GST_AUDIO_NE (S32)))
473 modplug->bits = 32;
474 else if (g_str_equal (format, GST_AUDIO_NE (S16)))
475 modplug->bits = 16;
476 else
477 modplug->bits = 8;
478
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000479 gst_structure_get_int (structure, "channels", &modplug->channel);
480 gst_structure_get_int (structure, "rate", &modplug->frequency);
Sebastian Drögeafd50a72012-07-28 00:32:58 +0200481
482 GST_DEBUG_OBJECT (modplug,
Stefan Sauerbece1c92012-03-23 18:16:08 +0100483 "Audio settings: %d bits, %d channel(s), %d Hz sampling rate",
484 modplug->bits, modplug->channel, modplug->frequency);
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000485
Vincent Penquerc'h57ec7352012-01-10 17:27:54 +0000486 gst_pad_set_caps (modplug->srcpad, newcaps);
487 gst_caps_unref (newcaps);
488
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000489 modplug->read_samples = 1152;
Sebastian Drögeb6486382010-01-04 10:23:37 +0100490 modplug->read_bytes =
491 modplug->read_samples * modplug->channel * modplug->bits / 8;
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000492
Stefan Kost77d01622007-08-21 12:59:00 +0000493 GST_DEBUG_OBJECT (modplug, "Loading song");
494
495 modplug->mSoundFile = new CSoundFile;
496
Kaj-Michael Lang212cf432009-12-29 16:07:59 +0200497 modplug->mSoundFile->SetWaveConfig (modplug->frequency, modplug->bits,
Sebastian Drögeb6486382010-01-04 10:23:37 +0100498 modplug->channel);
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000499
500 modplug->mSoundFile->SetWaveConfigEx (modplug->surround, !modplug->oversamp,
501 modplug->reverb, true, modplug->megabass, modplug->noise_reduction, true);
Jeremy Simon3f676b12002-12-30 17:21:45 +0000502 modplug->mSoundFile->SetResamplingMode (SRCMODE_POLYPHASE);
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000503
Jeremy Simon3f676b12002-12-30 17:21:45 +0000504 if (modplug->surround)
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000505 modplug->mSoundFile->SetSurroundParameters (modplug->surround_depth,
Thomas Vander Stichele4fd57bb2004-03-15 19:32:27 +0000506 modplug->surround_delay);
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000507
Jeremy Simon3f676b12002-12-30 17:21:45 +0000508 if (modplug->megabass)
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000509 modplug->mSoundFile->SetXBassParameters (modplug->megabass_amount,
Thomas Vander Stichele4fd57bb2004-03-15 19:32:27 +0000510 modplug->megabass_range);
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000511
Jeremy Simon3f676b12002-12-30 17:21:45 +0000512 if (modplug->reverb)
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000513 modplug->mSoundFile->SetReverbParameters (modplug->reverb_depth,
Thomas Vander Stichele4fd57bb2004-03-15 19:32:27 +0000514 modplug->reverb_delay);
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000515
Wim Taymans9fcfa602012-02-09 18:08:22 +0100516
517 gst_buffer_map (modplug->buffer, &map, GST_MAP_READ);
518 if (!modplug->mSoundFile->Create (map.data, modplug->song_size))
519 goto load_error;
520 gst_buffer_unmap (modplug->buffer, &map);
Jeremy Simondc461762002-06-09 19:00:27 +0000521
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000522 modplug->song_length = modplug->mSoundFile->GetSongTime () * GST_SECOND;
523 modplug->seek_at = -1;
524
525 GST_INFO_OBJECT (modplug, "Song length: %" GST_TIME_FORMAT,
Julien Moutteca11b122006-03-10 22:41:14 +0000526 GST_TIME_ARGS ((guint64) modplug->song_length));
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000527
528 return TRUE;
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000529
Wim Taymans9fcfa602012-02-09 18:08:22 +0100530 /* ERRORS */
531load_error:
532 {
533 gst_buffer_unmap (modplug->buffer, &map);
534 GST_ELEMENT_ERROR (modplug, STREAM, DECODE, (NULL),
535 ("Unable to load song"));
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000536 return FALSE;
Wim Taymans9fcfa602012-02-09 18:08:22 +0100537 }
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000538}
539
540static gboolean
Wim Taymans9fcfa602012-02-09 18:08:22 +0100541gst_modplug_sinkpad_activate (GstPad * sinkpad, GstObject * parent)
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000542{
Wim Taymans9fcfa602012-02-09 18:08:22 +0100543 GstQuery *query;
544 gboolean pull_mode;
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000545
Wim Taymans9fcfa602012-02-09 18:08:22 +0100546 query = gst_query_new_scheduling ();
547
548 if (!gst_pad_peer_query (sinkpad, query)) {
549 gst_query_unref (query);
550 goto activate_push;
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000551 }
Wim Taymans9fcfa602012-02-09 18:08:22 +0100552
Mark Nauwelaertsb8077532012-09-11 17:47:16 +0200553 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
554 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
Wim Taymans9fcfa602012-02-09 18:08:22 +0100555 gst_query_unref (query);
556
557 if (!pull_mode)
558 goto activate_push;
559
560 GST_DEBUG_OBJECT (sinkpad, "activating pull");
561 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
562
563activate_push:
564 {
565 GST_DEBUG_OBJECT (sinkpad, "activating push");
566 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
567 }
568}
569
570static gboolean
571gst_modplug_sinkpad_activate_mode (GstPad * pad, GstObject * parent,
572 GstPadMode mode, gboolean active)
573{
574 GstModPlug *modplug = GST_MODPLUG (parent);
575 gboolean res;
576
577 switch (mode) {
578 case GST_PAD_MODE_PUSH:
579 res = TRUE;
580 break;
581 case GST_PAD_MODE_PULL:
582 if (active) {
583 res = gst_pad_start_task (pad, (GstTaskFunction) gst_modplug_loop,
Wim Taymansdbed7262012-06-20 10:34:48 +0200584 modplug, NULL);
Wim Taymans9fcfa602012-02-09 18:08:22 +0100585 } else {
586 res = gst_pad_stop_task (pad);
587 }
588 break;
589 default:
590 res = FALSE;
591 break;
592 }
593 return res;
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000594}
595
596static gboolean
597gst_modplug_get_upstream_size (GstModPlug * modplug, gint64 * length)
598{
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000599 gboolean res = FALSE;
600 GstPad *peer;
601
602 peer = gst_pad_get_peer (modplug->sinkpad);
603 if (peer == NULL)
604 return FALSE;
605
Wim Taymans9fcfa602012-02-09 18:08:22 +0100606 if (gst_pad_query_duration (peer, GST_FORMAT_BYTES, length) && *length >= 0) {
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000607 res = TRUE;
608 }
609
610 gst_object_unref (peer);
Jeremy Simondc461762002-06-09 19:00:27 +0000611 return res;
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000612}
613
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000614static void
615gst_modplug_loop (GstModPlug * modplug)
Jeremy Simondc461762002-06-09 19:00:27 +0000616{
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000617 GstFlowReturn flow;
618 GstBuffer *out = NULL;
Wim Taymans9fcfa602012-02-09 18:08:22 +0100619 GstMapInfo map;
Jeremy Simondc461762002-06-09 19:00:27 +0000620
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000621 g_assert (GST_IS_MODPLUG (modplug));
Jeremy Simondc461762002-06-09 19:00:27 +0000622
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000623 /* first, get the size of the song */
624 if (!modplug->song_size) {
625 if (!gst_modplug_get_upstream_size (modplug, &modplug->song_size)) {
626 GST_ELEMENT_ERROR (modplug, STREAM, DECODE, (NULL),
627 ("Unable to load song"));
628 goto pause;
Jeremy Simon3f676b12002-12-30 17:21:45 +0000629 }
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000630
631 if (modplug->buffer) {
632 gst_buffer_unref (modplug->buffer);
633 }
634 modplug->buffer = gst_buffer_new_and_alloc (modplug->song_size);
635 modplug->offset = 0;
Jeremy Simondc461762002-06-09 19:00:27 +0000636 }
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000637
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000638 /* read in the song data */
639 if (!modplug->mSoundFile) {
640 GstBuffer *buffer = NULL;
641 guint64 read_size = modplug->song_size - modplug->offset;
Jeremy Simondc461762002-06-09 19:00:27 +0000642
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000643 if (read_size > 4096)
644 read_size = 4096;
Jeremy Simondc461762002-06-09 19:00:27 +0000645
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000646 flow =
647 gst_pad_pull_range (modplug->sinkpad, modplug->offset, read_size,
648 &buffer);
649 if (flow != GST_FLOW_OK) {
650 GST_ELEMENT_ERROR (modplug, STREAM, DECODE, (NULL),
651 ("Unable to load song"));
652 goto pause;
653 }
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000654
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000655 /* GST_LOG_OBJECT (modplug, "Read %u bytes", GST_BUFFER_SIZE (buffer)); */
Wim Taymans9fcfa602012-02-09 18:08:22 +0100656 gst_buffer_map (buffer, &map, GST_MAP_READ);
657 gst_buffer_fill (modplug->buffer, modplug->offset, map.data, map.size);
658 gst_buffer_unmap (buffer, &map);
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000659 gst_buffer_unref (buffer);
Jeremy Simon3f676b12002-12-30 17:21:45 +0000660
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000661 modplug->offset += read_size;
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000662
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000663 /* actually load it */
664 if (modplug->offset == modplug->song_size) {
Stefan Kosta5bf4022008-11-04 20:26:00 +0000665 GstTagList *tags;
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000666 gboolean ok;
Sebastian Drögeafd50a72012-07-28 00:32:58 +0200667#define COMMENT_SIZE 16384
Stefan Sauerd84d9892012-03-23 18:36:21 +0100668 gchar comment[COMMENT_SIZE];
Wim Taymans9fcfa602012-02-09 18:08:22 +0100669 GstSegment seg;
Jeremy Simon3f676b12002-12-30 17:21:45 +0000670
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000671 ok = gst_modplug_load_song (modplug);
672 gst_buffer_unref (modplug->buffer);
673 modplug->buffer = NULL;
674 modplug->offset = 0;
Jeremy Simon3f676b12002-12-30 17:21:45 +0000675
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000676 if (!ok) {
677 goto pause;
Benjamin Otte94f14032004-03-14 12:40:57 +0000678 }
Jeremy Simon6f920322003-01-20 21:02:58 +0000679
Wim Taymans9fcfa602012-02-09 18:08:22 +0100680 gst_segment_init (&seg, GST_FORMAT_TIME);
681 seg.stop = modplug->song_length;
682 gst_pad_push_event (modplug->srcpad, gst_event_new_segment (&seg));
Stefan Kosta5bf4022008-11-04 20:26:00 +0000683
684 /* get and send metadata */
Wim Taymans9fcfa602012-02-09 18:08:22 +0100685 tags = gst_tag_list_new_empty ();
Stefan Kosta5bf4022008-11-04 20:26:00 +0000686 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
687 GST_TAG_TITLE, modplug->mSoundFile->GetTitle (),
688 GST_TAG_BEATS_PER_MINUTE,
689 (gdouble) modplug->mSoundFile->GetMusicTempo (), NULL);
690
Stefan Sauerd84d9892012-03-23 18:36:21 +0100691 if (modplug->mSoundFile->GetSongComments ((gchar *) & comment,
Sebastian Drögeafd50a72012-07-28 00:32:58 +0200692 COMMENT_SIZE, 32)) {
Stefan Sauerd84d9892012-03-23 18:36:21 +0100693 comment[COMMENT_SIZE - 1] = '\0';
Stefan Kosta5bf4022008-11-04 20:26:00 +0000694 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
695 GST_TAG_COMMENT, comment, NULL);
696 }
Sebastian Drögeafd50a72012-07-28 00:32:58 +0200697 gst_pad_push_event (modplug->srcpad, gst_event_new_tag (tags));
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000698 } else {
699 /* not fully loaded yet */
Jeremy Simon3f676b12002-12-30 17:21:45 +0000700 return;
701 }
Jeremy Simon3f676b12002-12-30 17:21:45 +0000702 }
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000703
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000704 /* could move this to gst_modplug_src_event
705 * if libmodplug was definitely thread safe.. */
706 if (modplug->seek_at != -1) {
707 gint seek_to_pos;
708 gfloat temp;
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000709
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000710 temp = (gfloat) modplug->song_length / modplug->seek_at;
Stefan Sauerbece1c92012-03-23 18:16:08 +0100711 seek_to_pos = (gint) (modplug->mSoundFile->GetMaxPosition () / temp);
Jeremy Simondc461762002-06-09 19:00:27 +0000712
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000713 GST_DEBUG_OBJECT (modplug, "Seeking to row %d", seek_to_pos);
Jeremy Simon3f676b12002-12-30 17:21:45 +0000714
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000715 modplug->mSoundFile->SetCurrentPos (seek_to_pos);
716 modplug->seek_at = -1;
717 }
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000718
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000719 /* read and output a buffer */
Sebastian Drögeafd50a72012-07-28 00:32:58 +0200720 GST_LOG_OBJECT (modplug, "Read %d bytes", (gint) modplug->read_bytes);
Stefan Sauerbece1c92012-03-23 18:16:08 +0100721 /* libmodplug 0.8.7 trashes memory */
Sebastian Dröge860ccd42012-03-29 17:41:53 +0200722 out = gst_buffer_new_allocate (NULL, modplug->read_bytes * 2, NULL);
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000723
Wim Taymans9fcfa602012-02-09 18:08:22 +0100724 gst_buffer_map (out, &map, GST_MAP_WRITE);
725 if (!modplug->mSoundFile->Read (map.data, modplug->read_bytes)) {
726 gst_buffer_unmap (out, &map);
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000727 goto eos;
Wim Taymans9fcfa602012-02-09 18:08:22 +0100728 }
729 gst_buffer_unmap (out, &map);
Sebastian Dröge860ccd42012-03-29 17:41:53 +0200730 gst_buffer_resize (out, 0, modplug->read_bytes);
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000731
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000732 GST_BUFFER_DURATION (out) =
733 gst_util_uint64_scale_int (modplug->read_samples, GST_SECOND,
734 modplug->frequency);
735 GST_BUFFER_OFFSET (out) = modplug->offset;
736 GST_BUFFER_TIMESTAMP (out) =
737 gst_util_uint64_scale_int (modplug->offset, GST_SECOND,
738 modplug->frequency);
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000739
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000740 modplug->offset += modplug->read_samples;
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000741
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000742 flow = gst_pad_push (modplug->srcpad, out);
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000743
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000744 if (flow != GST_FLOW_OK) {
745 GST_LOG_OBJECT (modplug, "pad push flow: %s", gst_flow_get_name (flow));
746 goto pause;
747 }
748
749 return;
750
751eos:
752 {
753 gst_buffer_unref (out);
754 GST_INFO_OBJECT (modplug, "EOS");
755 gst_pad_push_event (modplug->srcpad, gst_event_new_eos ());
756 goto pause;
757 }
758
759pause:
760 {
761 GST_INFO_OBJECT (modplug, "Pausing");
762 gst_pad_pause_task (modplug->sinkpad);
Jeremy Simonc99d5272002-12-07 15:28:24 +0000763 }
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000764}
765
766
Jan Schmidt012dfb82005-09-05 17:20:29 +0000767static GstStateChangeReturn
768gst_modplug_change_state (GstElement * element, GstStateChange transition)
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000769{
Jeremy Simondc461762002-06-09 19:00:27 +0000770 GstModPlug *modplug;
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000771 GstStateChangeReturn ret;
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000772
Jeremy Simon3f676b12002-12-30 17:21:45 +0000773 modplug = GST_MODPLUG (element);
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000774
Jan Schmidt012dfb82005-09-05 17:20:29 +0000775 switch (transition) {
Jan Schmidt012dfb82005-09-05 17:20:29 +0000776 case GST_STATE_CHANGE_READY_TO_PAUSED:
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000777 modplug->buffer = NULL;
778 modplug->offset = 0;
Jeremy Simon3f676b12002-12-30 17:21:45 +0000779 modplug->song_size = 0;
Jeremy Simonc99d5272002-12-07 15:28:24 +0000780 break;
781 default:
Jeremy Simondc461762002-06-09 19:00:27 +0000782 break;
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000783 }
Jeremy Simondc461762002-06-09 19:00:27 +0000784
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000785 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
786 if (ret == GST_STATE_CHANGE_FAILURE)
787 return ret;
788
789 switch (transition) {
790 case GST_STATE_CHANGE_PAUSED_TO_READY:
791 if (modplug->buffer) {
792 gst_buffer_unref (modplug->buffer);
793 modplug->buffer = NULL;
794 }
795 if (modplug->mSoundFile) {
796 modplug->mSoundFile->Destroy ();
Stefan Sauerd84d9892012-03-23 18:36:21 +0100797 delete modplug->mSoundFile;
Tim-Philipp Müller55e2df52006-03-10 17:10:09 +0000798 modplug->mSoundFile = NULL;
799 }
800 break;
801 default:
802 break;
803 }
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000804
Jan Schmidt012dfb82005-09-05 17:20:29 +0000805 return GST_STATE_CHANGE_SUCCESS;
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000806}
807
Jeremy Simon80299932002-11-17 12:23:43 +0000808
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000809static void
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000810gst_modplug_set_property (GObject * object, guint id, const GValue * value,
811 GParamSpec * pspec)
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000812{
813 GstModPlug *modplug;
814
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000815 g_return_if_fail (GST_IS_MODPLUG (object));
Jeremy Simon3f676b12002-12-30 17:21:45 +0000816 modplug = GST_MODPLUG (object);
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000817
818 switch (id) {
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000819 case ARG_REVERB:
820 modplug->reverb = g_value_get_boolean (value);
821 break;
822 case ARG_REVERB_DEPTH:
823 modplug->reverb_depth = g_value_get_int (value);
824 break;
825 case ARG_REVERB_DELAY:
826 modplug->reverb_delay = g_value_get_int (value);
827 break;
828 case ARG_MEGABASS:
829 modplug->megabass = g_value_get_boolean (value);
830 break;
831 case ARG_MEGABASS_AMOUNT:
832 modplug->megabass_amount = g_value_get_int (value);
833 break;
834 case ARG_MEGABASS_RANGE:
835 modplug->megabass_range = g_value_get_int (value);
836 break;
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000837 case ARG_NOISE_REDUCTION:
838 modplug->noise_reduction = g_value_get_boolean (value);
839 break;
840 case ARG_SURROUND:
841 modplug->surround = g_value_get_boolean (value);
842 break;
843 case ARG_SURROUND_DEPTH:
844 modplug->surround_depth = g_value_get_int (value);
845 break;
846 case ARG_SURROUND_DELAY:
847 modplug->surround_delay = g_value_get_int (value);
848 break;
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000849 default:
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000850 break;
851 }
852}
853
854static void
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000855gst_modplug_get_property (GObject * object, guint id, GValue * value,
856 GParamSpec * pspec)
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000857{
858 GstModPlug *modplug;
859
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000860 g_return_if_fail (GST_IS_MODPLUG (object));
Jeremy Simon3f676b12002-12-30 17:21:45 +0000861 modplug = GST_MODPLUG (object);
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000862
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000863 switch (id) {
864 case ARG_REVERB:
865 g_value_set_boolean (value, modplug->reverb);
866 break;
867 case ARG_REVERB_DEPTH:
868 g_value_set_int (value, modplug->reverb_depth);
869 break;
870 case ARG_REVERB_DELAY:
871 g_value_set_int (value, modplug->reverb_delay);
872 break;
873 case ARG_MEGABASS:
874 g_value_set_boolean (value, modplug->megabass);
875 break;
876 case ARG_MEGABASS_AMOUNT:
877 g_value_set_int (value, modplug->megabass_amount);
878 break;
879 case ARG_MEGABASS_RANGE:
880 g_value_set_int (value, modplug->megabass_range);
881 break;
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000882 case ARG_SURROUND:
883 g_value_set_boolean (value, modplug->surround);
884 break;
885 case ARG_SURROUND_DEPTH:
886 g_value_set_int (value, modplug->surround_depth);
887 break;
888 case ARG_SURROUND_DELAY:
889 g_value_set_int (value, modplug->surround_delay);
890 break;
891 case ARG_NOISE_REDUCTION:
892 g_value_set_boolean (value, modplug->noise_reduction);
893 break;
894 default:
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000895 break;
896 }
897}
898
899static gboolean
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000900plugin_init (GstPlugin * plugin)
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000901{
Ronald S. Bultje4e3bf352003-11-02 23:00:38 +0000902 return gst_element_register (plugin, "modplug",
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000903 GST_RANK_PRIMARY, GST_TYPE_MODPLUG);
Jeremy Simonac87bfc2002-02-28 21:10:42 +0000904}
905
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000906GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
907 GST_VERSION_MINOR,
Sebastian Drögecda192b2012-04-05 18:02:56 +0200908 modplug,
Thomas Vander Stichele87960e42004-03-14 23:20:41 +0000909 ".MOD audio decoding",
Tim-Philipp Müllerf9020292006-04-03 10:34:54 +0000910 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)