File: modplug.c

package info (click to toggle)
xmms2 0.8%2Bdfsg-28
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 14,216 kB
  • sloc: ansic: 63,803; python: 15,537; cpp: 5,718; xml: 1,479; perl: 338; ruby: 243; makefile: 79; sh: 59; asm: 7
file content (380 lines) | stat: -rw-r--r-- 12,231 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*  XMMS2 - X Music Multiplexer System
 *  Copyright (C) 2003-2011 XMMS2 Team
 *
 *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
 */


#include "xmms/xmms_xformplugin.h"
#include "xmms/xmms_sample.h"
#include "xmms/xmms_medialib.h"
#include "xmms/xmms_log.h"
#include <libmodplug/modplug.h>

#include <glib.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>

/*
 * Type definitions
 */
static const struct {
	const gchar *key;
	const gchar *value;
} config_params[] = {
	{"freq", "44100"},
	{"resample", "fir"},
	{"enable_oversampling", "1"},
	{"enable_noise_reduction", "1"},
	{"enable_reverb", "0"},
	{"enable_megabass", "0"},
	{"enable_surround", "0"},
	{"reverb_depth", "100"},
	{"reverb_delay", "80"},
	{"bass_amount", "40"},
	{"bass_range", "30"},
	{"surround_depth", "10"},
	{"surround_delay", "40"},
	{"loop", "0"}
};

typedef struct xmms_modplug_data_St {
	ModPlug_Settings settings;
	ModPlugFile *mod;
	GString *buffer;
} xmms_modplug_data_t;

/*
 * Function prototypes
 */

static gboolean xmms_modplug_plugin_setup (xmms_xform_plugin_t *xform_plugin);
static gint xmms_modplug_read (xmms_xform_t *xform, xmms_sample_t *buf, gint len, xmms_error_t *err);
static void xmms_modplug_destroy (xmms_xform_t *xform);
static gboolean xmms_modplug_init (xmms_xform_t *xform);
static gint64 xmms_modplug_seek (xmms_xform_t *xform, gint64 samples, xmms_xform_seek_mode_t whence, xmms_error_t *error);
static void xmms_modplug_config_changed (xmms_object_t *obj, xmmsv_t *_value, gpointer udata);

/*
 * Plugin header
 */
XMMS_XFORM_PLUGIN ("modplug",
                   "MODPLUG decoder ",
                   XMMS_VERSION,
                   "Module file decoder",
                   xmms_modplug_plugin_setup);

static gboolean
xmms_modplug_plugin_setup (xmms_xform_plugin_t *xform_plugin)
{
	xmms_xform_methods_t methods;
	int i;

	XMMS_XFORM_METHODS_INIT (methods);
	methods.init = xmms_modplug_init;
	methods.destroy = xmms_modplug_destroy;
	methods.read = xmms_modplug_read;
	methods.seek = xmms_modplug_seek;

	xmms_xform_plugin_methods_set (xform_plugin, &methods);

	/*
	xmms_plugin_info_add (plugin, "URL", "http://xmms2.org/");
	xmms_plugin_info_add (plugin, "Author", "XMMS Team");
	xmms_plugin_info_add (plugin, "License", "GPL");
	*/

	xmms_xform_plugin_indata_add (xform_plugin,
	                              XMMS_STREAM_TYPE_MIMETYPE,
	                              "audio/mod",
	                              NULL);
	xmms_xform_plugin_indata_add (xform_plugin,
	                              XMMS_STREAM_TYPE_MIMETYPE,
	                              "audio/xm",
	                              NULL);
	xmms_xform_plugin_indata_add (xform_plugin,
	                              XMMS_STREAM_TYPE_MIMETYPE,
	                              "audio/s3m",
	                              NULL);
	xmms_xform_plugin_indata_add (xform_plugin,
	                              XMMS_STREAM_TYPE_MIMETYPE,
	                              "audio/it",
	                              NULL);
	xmms_xform_plugin_indata_add (xform_plugin,
	                              XMMS_STREAM_TYPE_MIMETYPE,
	                              "audio/med",
	                              NULL);
	xmms_xform_plugin_indata_add (xform_plugin,
	                              XMMS_STREAM_TYPE_MIMETYPE,
	                              "audio/amf",
	                              NULL);
	xmms_xform_plugin_indata_add (xform_plugin,
	                              XMMS_STREAM_TYPE_MIMETYPE,
	                              "audio/umx",
	                              NULL);

	xmms_magic_add ("Fasttracker II module", "audio/xm",
	                "0 string Extended Module:", NULL);
	xmms_magic_add ("ScreamTracker III module", "audio/s3m",
	                "44 string SCRM", NULL);
	xmms_magic_add ("Impulse Tracker module", "audio/it",
	                "0 string IMPM", NULL);
	xmms_magic_add ("MED module", "audio/med",
	                "0 string MMD", NULL);
	xmms_magic_add ("AMF module", "audio/amf",
	                "0 string AMF", NULL);

	/* http://www.unrealwiki.com/wiki/Package_File_Format */
	xmms_magic_add ("Unreal Engine package", "audio/umx",
	                "0 belong 0xc1832a9e", NULL);

	/* these are for all (not all but should be most) various types of .mod files */
	xmms_magic_add ("4-channel Protracker module", "audio/mod",
	                "1080 string M.K.", NULL);
	xmms_magic_add ("4-channel Protracker module", "audio/mod",
	                "1080 string M!K!", NULL);
	xmms_magic_add ("4-channel Startracker module", "audio/mod",
	                "1080 string FLT4", NULL);
	xmms_magic_add ("8-channel Startracker module", "audio/mod",
	                "1080 string FLT8", NULL);
	xmms_magic_add ("4-channel Fasttracker module", "audio/mod",
	                "1080 string 4CHN", NULL);
	xmms_magic_add ("6-channel Fasttracker module", "audio/mod",
	                "1080 string 6CHN", NULL);
	xmms_magic_add ("8-channel Fasttracker module", "audio/mod",
	                "1080 string 8CHN", NULL);
	xmms_magic_add ("8-channel Octalyzer module", "audio/mod",
	                "1080 string CD81", NULL);
	xmms_magic_add ("8-channel Octalyzer module", "audio/mod",
	                "1080 string OKTA", NULL);
	xmms_magic_add ("16-channel Taketracker module", "audio/mod",
	                "1080 string 16CN", NULL);
	xmms_magic_add ("32-channel Taketracker module", "audio/mod",
	                "1080 string 32CN", NULL);

	for (i = 0; i < G_N_ELEMENTS (config_params); i++) {
		xmms_xform_plugin_config_property_register (xform_plugin,
		                                            config_params[i].key,
		                                            config_params[i].value,
		                                            NULL, NULL);
	}

	return TRUE;
}

static void
xmms_modplug_destroy (xmms_xform_t *xform)
{
	xmms_modplug_data_t *data;
	xmms_config_property_t *cfgv;
	int i;

	g_return_if_fail (xform);

	data = xmms_xform_private_data_get (xform);
	g_return_if_fail (data);

	if (data->buffer)
		g_string_free (data->buffer, TRUE);

	if (data->mod)
		ModPlug_Unload (data->mod);

	for (i = 0; i < G_N_ELEMENTS (config_params); i++) {
		cfgv = xmms_xform_config_lookup (xform, config_params[i].key);

		xmms_config_property_callback_remove (cfgv,
		                                      xmms_modplug_config_changed,
		                                      data);
	}

	g_free (data);

}

static gint64
xmms_modplug_seek (xmms_xform_t *xform, gint64 samples,
                xmms_xform_seek_mode_t whence, xmms_error_t *error)
{
	xmms_modplug_data_t *data;

	g_return_val_if_fail (xform, -1);
	g_return_val_if_fail (samples >= 0, -1);
	g_return_val_if_fail (whence == XMMS_XFORM_SEEK_SET, -1);

	data = xmms_xform_private_data_get (xform);

	ModPlug_Seek (data->mod, (int) ((gdouble)1000 * samples / 44100));

	return samples;
}

static gboolean
xmms_modplug_init (xmms_xform_t *xform)
{
	xmms_modplug_data_t *data;
	const gchar *metakey;
	gint filesize;
	xmms_config_property_t *cfgv;
	gint i;

	g_return_val_if_fail (xform, FALSE);

	data = g_new0 (xmms_modplug_data_t, 1);

	xmms_xform_private_data_set (xform, data);

	for (i = 0; i < G_N_ELEMENTS (config_params); i++) {
		cfgv = xmms_xform_config_lookup (xform, config_params[i].key);
		xmms_config_property_callback_set (cfgv,
		                                   xmms_modplug_config_changed,
		                                   data);

		xmms_modplug_config_changed (XMMS_OBJECT (cfgv), NULL, data);
	}

	/* mFrequency and mResamplingMode are set in config_changed */
	data->settings.mChannels = 2;
	data->settings.mBits = 16;
	ModPlug_SetSettings (&data->settings);

	xmms_xform_outdata_type_add (xform,
	                             XMMS_STREAM_TYPE_MIMETYPE,
	                             "audio/pcm",
	                             XMMS_STREAM_TYPE_FMT_FORMAT,
	                             XMMS_SAMPLE_FORMAT_S16,
	                             XMMS_STREAM_TYPE_FMT_CHANNELS,
	                             2,
	                             XMMS_STREAM_TYPE_FMT_SAMPLERATE,
	                             data->settings.mFrequency,
	                             XMMS_STREAM_TYPE_END);

	data->buffer = g_string_new ("");

	for (;;) {
		xmms_error_t error;
		gchar buf[4096];
		gint ret;

		ret = xmms_xform_read (xform, buf, sizeof (buf), &error);
		if (ret == -1) {
			XMMS_DBG ("Error reading mod");
			return FALSE;
		}
		if (ret == 0) {
			break;
		}
		g_string_append_len (data->buffer, buf, ret);
	}

	data->mod = ModPlug_Load (data->buffer->str, data->buffer->len);
	if (!data->mod) {
		XMMS_DBG ("Error loading mod");
		return FALSE;
	}

	metakey = XMMS_MEDIALIB_ENTRY_PROPERTY_SIZE;
	if (xmms_xform_metadata_get_int (xform, metakey, &filesize)) {
		metakey = XMMS_MEDIALIB_ENTRY_PROPERTY_DURATION;
		xmms_xform_metadata_set_int (xform, metakey,
		                             ModPlug_GetLength (data->mod));
	}

	metakey = XMMS_MEDIALIB_ENTRY_PROPERTY_TITLE;
	xmms_xform_metadata_set_str (xform, metakey, ModPlug_GetName (data->mod));

	return TRUE;
}


static gint
xmms_modplug_read (xmms_xform_t *xform, xmms_sample_t *buf, gint len, xmms_error_t *err)
{
	xmms_modplug_data_t *data;

	data = xmms_xform_private_data_get (xform);

	return ModPlug_Read (data->mod, buf, len);
}

static void
xmms_modplug_config_changed (xmms_object_t *obj, xmmsv_t *_value,
                             gpointer udata)
{
	xmms_modplug_data_t *data = udata;
	xmms_config_property_t *prop = (xmms_config_property_t *) obj;
	const gchar *name;
	const gchar *value;
	gint intvalue;

	name = xmms_config_property_get_name (prop);

	if (!g_ascii_strcasecmp (name, "modplug.resample")) {
		value = xmms_config_property_get_string (prop);

		if (!g_ascii_strcasecmp (value, "fir")) {
			data->settings.mResamplingMode = MODPLUG_RESAMPLE_FIR;
		} else if (!g_ascii_strcasecmp (value, "spline")) {
			data->settings.mResamplingMode = MODPLUG_RESAMPLE_SPLINE;
		} else if (!g_ascii_strcasecmp (value, "linear")) {
			data->settings.mResamplingMode = MODPLUG_RESAMPLE_LINEAR;
		} else {
			data->settings.mResamplingMode = MODPLUG_RESAMPLE_NEAREST;
		}
	} else {
		intvalue = xmms_config_property_get_int (prop);

		if (!g_ascii_strcasecmp (name, "modplug.freq")) {
			data->settings.mFrequency = intvalue;
		} else if (!g_ascii_strcasecmp (name, "modplug.reverb_depth")) {
			data->settings.mReverbDepth = intvalue;
		} else if (!g_ascii_strcasecmp (name, "modplug.reverb_delay")) {
			data->settings.mReverbDelay = intvalue;
		} else if (!g_ascii_strcasecmp (name, "modplug.bass_amount")) {
			data->settings.mBassAmount = intvalue;
		} else if (!g_ascii_strcasecmp (name, "modplug.bass_range")) {
			data->settings.mBassRange = intvalue;
		} else if (!g_ascii_strcasecmp (name, "modplug.surround_depth")) {
			data->settings.mSurroundDepth = intvalue;
		} else if (!g_ascii_strcasecmp (name, "modplug.surround_delay")) {
			data->settings.mSurroundDelay = intvalue;
		} else if (!g_ascii_strcasecmp (name, "modplug.loop")) {
			data->settings.mLoopCount = intvalue;
		} else if (!g_ascii_strcasecmp (name, "modplug.enable_oversampling")) {
			if (intvalue) {
				data->settings.mFlags |= MODPLUG_ENABLE_OVERSAMPLING;
			} else {
				data->settings.mFlags &= ~MODPLUG_ENABLE_OVERSAMPLING;
			}
		} else if (!g_ascii_strcasecmp (name,
		                                "modplug.enable_noise_reduction")) {
			if (intvalue) {
				data->settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
			} else {
				data->settings.mFlags &= ~MODPLUG_ENABLE_NOISE_REDUCTION;
			}
		} else if (!g_ascii_strcasecmp (name, "modplug.enable_reverb")) {
			if (intvalue) {
				data->settings.mFlags |= MODPLUG_ENABLE_REVERB;
			} else {
				data->settings.mFlags &= ~MODPLUG_ENABLE_REVERB;
			}
		} else if (!g_ascii_strcasecmp (name, "modplug.enable_megabass")) {
			if (intvalue) {
				data->settings.mFlags |= MODPLUG_ENABLE_MEGABASS;
			} else {
				data->settings.mFlags &= ~MODPLUG_ENABLE_MEGABASS;
			}
		} else if (!g_ascii_strcasecmp (name, "modplug.enable_surround")) {
			if (intvalue) {
				data->settings.mFlags |= MODPLUG_ENABLE_SURROUND;
			} else {
				data->settings.mFlags &= ~MODPLUG_ENABLE_SURROUND;
			}
		}
	}

	ModPlug_SetSettings (&data->settings);
}