Mercurial > SDL_sound_CoreAudio
annotate decoders/modplug.c @ 369:d76a34316c84
Fixed to handle stereo AIFF files.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 24 Jun 2002 23:12:55 +0000 |
parents | f72fd79d6e76 |
children | 51883cd1193e |
rev | line source |
---|---|
208 | 1 /* |
2 * SDL_sound -- An abstract sound format decoding API. | |
3 * Copyright (C) 2001 Ryan C. Gordon. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2.1 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 | |
20 /* | |
21 * Module player for SDL_sound. This driver handles anything that ModPlug does. | |
22 * | |
349
f72fd79d6e76
Added URLs for official and unofficial versions of ModPlug.
Ryan C. Gordon <icculus@icculus.org>
parents:
312
diff
changeset
|
23 * ModPlug can be found at http://sourceforge.net/projects/modplug-xmms |
f72fd79d6e76
Added URLs for official and unofficial versions of ModPlug.
Ryan C. Gordon <icculus@icculus.org>
parents:
312
diff
changeset
|
24 * |
f72fd79d6e76
Added URLs for official and unofficial versions of ModPlug.
Ryan C. Gordon <icculus@icculus.org>
parents:
312
diff
changeset
|
25 * An unofficial version of modplug with all C++ dependencies removed is also |
f72fd79d6e76
Added URLs for official and unofficial versions of ModPlug.
Ryan C. Gordon <icculus@icculus.org>
parents:
312
diff
changeset
|
26 * available: http://freecraft.net/snapshots/ |
f72fd79d6e76
Added URLs for official and unofficial versions of ModPlug.
Ryan C. Gordon <icculus@icculus.org>
parents:
312
diff
changeset
|
27 * (Look for something like "libmodplug-johns-*.tar.gz") |
f72fd79d6e76
Added URLs for official and unofficial versions of ModPlug.
Ryan C. Gordon <icculus@icculus.org>
parents:
312
diff
changeset
|
28 * |
208 | 29 * Please see the file COPYING in the source's root directory. |
30 * | |
31 * This file written by Torbjörn Andersson (d91tan@Update.UU.SE) | |
32 */ | |
33 | |
34 #if HAVE_CONFIG_H | |
35 # include <config.h> | |
36 #endif | |
37 | |
38 #ifdef SOUND_SUPPORTS_MODPLUG | |
39 | |
40 #include <stdio.h> | |
41 #include <stdlib.h> | |
42 #include <string.h> | |
43 #include <assert.h> | |
44 | |
45 #include "SDL_sound.h" | |
46 | |
47 #define __SDL_SOUND_INTERNAL__ | |
48 #include "SDL_sound_internal.h" | |
49 | |
50 #include "modplug.h" | |
51 | |
52 | |
53 static int MODPLUG_init(void); | |
54 static void MODPLUG_quit(void); | |
55 static int MODPLUG_open(Sound_Sample *sample, const char *ext); | |
56 static void MODPLUG_close(Sound_Sample *sample); | |
57 static Uint32 MODPLUG_read(Sound_Sample *sample); | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
58 static int MODPLUG_rewind(Sound_Sample *sample); |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
59 static int MODPLUG_seek(Sound_Sample *sample, Uint32 ms); |
208 | 60 |
61 static const char *extensions_modplug[] = | |
62 { | |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
63 /* The XMMS plugin is apparently able to load compressed modules as |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
64 * well, but libmodplug does not handle this. |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
65 */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
66 "669", /* Composer 669 / UNIS 669 module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
67 "AMF", /* ASYLUM Music Format / Advanced Music Format(DSM) */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
68 "AMS", /* AMS module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
69 "DBM", /* DigiBooster Pro Module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
70 "DMF", /* DMF DELUSION DIGITAL MUSIC FILEFORMAT (X-Tracker) */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
71 "DSM", /* DSIK Internal Format module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
72 "FAR", /* Farandole module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
73 "IT", /* Impulse Tracker IT file */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
74 "MDL", /* DigiTracker module */ |
208 | 75 #if 0 |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
76 "J2B", /* Not implemented? What is it anyway? */ |
208 | 77 #endif |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
78 "MED", /* OctaMed MED file */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
79 "MOD", /* ProTracker / NoiseTracker MOD/NST file */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
80 "MT2", /* MadTracker 2.0 */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
81 "MTM", /* MTM file */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
82 "OKT", /* Oktalyzer module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
83 "PTM", /* PTM PolyTracker module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
84 "PSM", /* PSM module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
85 "S3M", /* ScreamTracker file */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
86 "STM", /* ST 2.xx */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
87 "ULT", |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
88 "UMX", |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
89 "XM", /* FastTracker II */ |
208 | 90 NULL |
91 }; | |
92 | |
93 const Sound_DecoderFunctions __Sound_DecoderFunctions_MODPLUG = | |
94 { | |
95 { | |
96 extensions_modplug, | |
97 "Play modules through ModPlug", | |
98 "Torbjörn Andersson <d91tan@Update.UU.SE>", | |
99 "http://modplug-xmms.sourceforge.net/" | |
100 }, | |
101 | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
102 MODPLUG_init, /* init() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
103 MODPLUG_quit, /* quit() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
104 MODPLUG_open, /* open() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
105 MODPLUG_close, /* close() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
106 MODPLUG_read, /* read() method */ |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
107 MODPLUG_rewind, /* rewind() method */ |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
108 MODPLUG_seek /* seek() method */ |
208 | 109 }; |
110 | |
111 | |
112 static int MODPLUG_init(void) | |
113 { | |
114 ModPlug_Settings settings; | |
115 | |
116 /* The settings will require some experimenting. I've borrowed some | |
117 * of them from the XMMS ModPlug plugin. | |
118 */ | |
119 settings.mFlags = | |
120 MODPLUG_ENABLE_OVERSAMPLING | |
121 | MODPLUG_ENABLE_NOISE_REDUCTION | |
122 | MODPLUG_ENABLE_REVERB | |
123 | MODPLUG_ENABLE_MEGABASS | |
124 | MODPLUG_ENABLE_SURROUND; | |
125 settings.mChannels = 2; | |
126 settings.mBits = 16; | |
127 settings.mFrequency = 44100; | |
128 settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; | |
129 settings.mReverbDepth = 30; | |
130 settings.mReverbDelay = 100; | |
131 settings.mBassAmount = 40; | |
132 settings.mBassRange = 30; | |
133 settings.mSurroundDepth = 20; | |
134 settings.mSurroundDelay = 20; | |
135 settings.mLoopCount = 0; | |
136 | |
137 ModPlug_SetSettings(&settings); | |
138 return(1); /* success. */ | |
139 } /* MODPLUG_init */ | |
140 | |
141 | |
142 static void MODPLUG_quit(void) | |
143 { | |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
144 /* it's a no-op. */ |
208 | 145 } /* MODPLUG_quit */ |
146 | |
147 | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
148 /* |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
149 * Most MOD files I've seen have tended to be a few hundred KB, even if some |
208 | 150 * of them were much smaller than that. |
151 */ | |
152 #define CHUNK_SIZE 65536 | |
153 | |
154 static int MODPLUG_open(Sound_Sample *sample, const char *ext) | |
155 { | |
156 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
157 ModPlugFile *module; | |
158 Uint8 *data; | |
159 size_t size; | |
160 Uint32 retval; | |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
161 int has_extension = 0; |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
162 int i; |
208 | 163 |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
164 /* |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
165 * Apparently ModPlug's loaders are too forgiving. They gladly accept |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
166 * streams that they shouldn't. For now, rely on file extension instead. |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
167 */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
168 for (i = 0; extensions_modplug[i] != NULL; i++) |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
169 { |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
170 if (__Sound_strcasecmp(ext, extensions_modplug[i]) == 0) |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
171 { |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
172 has_extension = 1; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
173 break; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
174 } /* if */ |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
175 } /* for */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
176 |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
177 if (!has_extension) |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
178 { |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
179 SNDDBG(("MODPLUG: Unrecognized file type: %s\n", ext)); |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
180 Sound_SetError("MODPLUG: Not a module file."); |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
181 return(0); |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
182 } /* if */ |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
183 |
208 | 184 /* ModPlug needs the entire stream in one big chunk. I don't like it, |
185 * but I don't think there's any way around it. | |
186 */ | |
187 data = (Uint8 *) malloc(CHUNK_SIZE); | |
188 BAIL_IF_MACRO(data == NULL, ERR_OUT_OF_MEMORY, 0); | |
189 size = 0; | |
190 | |
191 do | |
192 { | |
193 retval = SDL_RWread(internal->rw, &data[size], 1, CHUNK_SIZE); | |
194 size += retval; | |
195 if (retval == CHUNK_SIZE) | |
196 { | |
197 data = (Uint8 *) realloc(data, size + CHUNK_SIZE); | |
198 BAIL_IF_MACRO(data == NULL, ERR_OUT_OF_MEMORY, 0); | |
199 } /* if */ | |
200 } while (retval > 0); | |
201 | |
202 /* The buffer may be a bit too large, but that doesn't matter. I think | |
203 * it's safe to free it as soon as ModPlug_Load() is finished anyway. | |
204 */ | |
205 module = ModPlug_Load((void *) data, size); | |
206 free(data); | |
207 BAIL_IF_MACRO(module == NULL, "MODPLUG: Not a module file.", 0); | |
208 | |
209 SNDDBG(("MODPLUG: [%d ms] %s\n", | |
210 ModPlug_GetLength(module), ModPlug_GetName(module))); | |
211 | |
212 sample->actual.channels = 2; | |
213 sample->actual.rate = 44100; | |
214 sample->actual.format = AUDIO_S16SYS; | |
215 | |
216 internal->decoder_private = (void *) module; | |
312 | 217 sample->flags = SOUND_SAMPLEFLAG_CANSEEK; |
208 | 218 |
219 SNDDBG(("MODPLUG: Accepting data stream\n")); | |
220 return(1); /* we'll handle this data. */ | |
221 } /* MODPLUG_open */ | |
222 | |
223 | |
224 static void MODPLUG_close(Sound_Sample *sample) | |
225 { | |
226 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
227 ModPlugFile *module = (ModPlugFile *) internal->decoder_private; | |
228 | |
229 ModPlug_Unload(module); | |
230 } /* MODPLUG_close */ | |
231 | |
232 | |
233 static Uint32 MODPLUG_read(Sound_Sample *sample) | |
234 { | |
235 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
236 ModPlugFile *module = (ModPlugFile *) internal->decoder_private; | |
237 int retval; | |
238 | |
239 retval = ModPlug_Read(module, internal->buffer, internal->buffer_size); | |
240 if (retval == 0) | |
241 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
242 return(retval); | |
243 } /* MODPLUG_read */ | |
244 | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
245 |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
246 static int MODPLUG_rewind(Sound_Sample *sample) |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
247 { |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
248 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
249 ModPlugFile *module = (ModPlugFile *) internal->decoder_private; |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
250 |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
251 ModPlug_Seek(module, 0); |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
252 return(1); |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
253 } /* MODPLUG_rewind */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
254 |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
255 |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
256 static int MODPLUG_seek(Sound_Sample *sample, Uint32 ms) |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
257 { |
312 | 258 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; |
259 ModPlugFile *module = (ModPlugFile *) internal->decoder_private; | |
260 | |
261 /* Assume that this will work. */ | |
262 ModPlug_Seek(module, ms); | |
263 return(1); | |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
264 } /* MODPLUG_seek */ |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
265 |
208 | 266 #endif /* SOUND_SUPPORTS_MODPLUG */ |
267 | |
268 | |
269 /* end of modplug.c ... */ |