File: modplug.c

package info (click to toggle)
cmus 2.4.3-2%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,516 kB
  • sloc: ansic: 29,728; sh: 1,236; makefile: 194; python: 159
file content (221 lines) | stat: -rw-r--r-- 5,353 bytes parent folder | download
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
/* 
 * Copyright 2005 Timo Hirvonen
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include "ip.h"
#include "file.h"
#include "xmalloc.h"

#include <modplug.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

struct mod_private {
	ModPlugFile *file;
};

static int mod_open(struct input_plugin_data *ip_data)
{
	struct mod_private *priv;
	char *contents;
	int size, rc;
	ModPlugFile *file;
	ModPlug_Settings settings;

	size = lseek(ip_data->fd, 0, SEEK_END);
	if (size == -1)
		return -IP_ERROR_ERRNO;
	if (lseek(ip_data->fd, 0, SEEK_SET) == -1)
		return -IP_ERROR_ERRNO;

	contents = xnew(char, size);
	rc = read_all(ip_data->fd, contents, size);
	if (rc == -1) {
		int save = errno;

		free(contents);
		errno = save;
		return -IP_ERROR_ERRNO;
	}
	if (rc != size) {
		free(contents);
		return -IP_ERROR_FILE_FORMAT;
	}
	errno = 0;
	file = ModPlug_Load(contents, size);
	if (file == NULL) {
		int save = errno;

		free(contents);
		errno = save;
		if (errno == 0) {
			/* libmodplug never sets errno? */
			return -IP_ERROR_FILE_FORMAT;
		}
		return -IP_ERROR_ERRNO;
	}
	free(contents);

	priv = xnew(struct mod_private, 1);
	priv->file = file;

	ModPlug_GetSettings(&settings);
	settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING | MODPLUG_ENABLE_NOISE_REDUCTION;
/* 	settings.mFlags |= MODPLUG_ENABLE_REVERB; */
/* 	settings.mFlags |= MODPLUG_ENABLE_MEGABASS; */
/* 	settings.mFlags |= MODPLUG_ENABLE_SURROUND; */
	settings.mChannels = 2;
	settings.mBits = 16;
	settings.mFrequency = 44100;
	settings.mResamplingMode = MODPLUG_RESAMPLE_FIR;
	ModPlug_SetSettings(&settings);

	ip_data->private = priv;
	ip_data->sf = sf_bits(16) | sf_rate(44100) | sf_channels(2) | sf_signed(1);
#ifdef WORDS_BIGENDIAN
	ip_data->sf |= sf_bigendian(1);
#endif
	return 0;
}

static int mod_close(struct input_plugin_data *ip_data)
{
	struct mod_private *priv = ip_data->private;

	ModPlug_Unload(priv->file);
	free(priv);
	ip_data->private = NULL;
	return 0;
}

static int mod_read(struct input_plugin_data *ip_data, char *buffer, int count)
{
	struct mod_private *priv = ip_data->private;
	int rc;
	
	errno = 0;
	rc = ModPlug_Read(priv->file, buffer, count);
	if (rc < 0) {
		if (errno == 0)
			return -IP_ERROR_INTERNAL;
		return -IP_ERROR_ERRNO;
	}
	return rc;
}

static int mod_seek(struct input_plugin_data *ip_data, double offset)
{
	struct mod_private *priv = ip_data->private;
	int ms = (int)(offset * 1000.0 + 0.5);

	/* void */
	ModPlug_Seek(priv->file, ms);
	return 0;
}

static int mod_read_comments(struct input_plugin_data *ip_data, struct keyval **comments)
{
	struct mod_private *priv = ip_data->private;
	struct keyval *c;
	const char *name;

	c = xnew0(struct keyval, 2);
	name = ModPlug_GetName(priv->file);
	if (name != NULL && *name != 0) {
		c[0].key = xstrdup("title");
		c[0].val = xstrdup(name);
	}
	*comments = c;
	return 0;
}

static int mod_duration(struct input_plugin_data *ip_data)
{
	struct mod_private *priv = ip_data->private;

	return (ModPlug_GetLength(priv->file) + 500) / 1000;
}

static long mod_bitrate(struct input_plugin_data *ip_data)
{
	return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
}

static const char *mod_type_to_string(int type)
{
	/* from <libmodplug/sndfile.h>, which is C++ */
	switch (type) {
	case 0x01:	return "mod";
	case 0x02:	return "s3m";
	case 0x04:	return "xm";
	case 0x08:	return "med";
	case 0x10:	return "mtm";
	case 0x20:	return "it";
	case 0x40:	return "699";
	case 0x80:	return "ult";
	case 0x100:	return "stm";
	case 0x200:	return "far";
	case 0x800:	return "amf";
	case 0x1000:	return "ams";
	case 0x2000:	return "dsm";
	case 0x4000:	return "mdl";
	case 0x8000:	return "okt";
	case 0x10000:	return "midi";
	case 0x20000:	return "dmf";
	case 0x40000:	return "ptm";
	case 0x80000:	return "dbm";
	case 0x100000:	return "mt2";
	case 0x200000:	return "amf0";
	case 0x400000:	return "psm";
	case 0x80000000:return "umx";
	}
	return NULL;
}

static char *mod_codec(struct input_plugin_data *ip_data)
{
	struct mod_private *priv = ip_data->private;
	const char *codec;
	int type;

	type = ModPlug_GetModuleType(priv->file);
	codec = mod_type_to_string(type);

	return codec ? xstrdup(codec) : NULL;
}

const struct input_plugin_ops ip_ops = {
	.open = mod_open,
	.close = mod_close,
	.read = mod_read,
	.seek = mod_seek,
	.read_comments = mod_read_comments,
	.duration = mod_duration,
	.bitrate = mod_bitrate,
	.codec = mod_codec
};

const int ip_priority = 50;
const char * const ip_extensions[] = {
	"mod", "s3m", "xm", "it", "669", "amf", "ams", "dbm", "dmf", "dsm",
	"far", "mdl", "med", "mtm", "okt", "ptm", "stm", "ult", "umx", "mt2",
	"psm", NULL
};
const char * const ip_mime_types[] = { NULL };