Mercurial > SDL_sound_CoreAudio
annotate decoders/modplug.c @ 312:498240aa76f1
Seek implementations.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Wed, 24 Apr 2002 20:47:21 +0000 |
parents | c97be6e1bd27 |
children | f72fd79d6e76 |
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 * | |
23 * Please see the file COPYING in the source's root directory. | |
24 * | |
25 * This file written by Torbjörn Andersson (d91tan@Update.UU.SE) | |
26 */ | |
27 | |
28 #if HAVE_CONFIG_H | |
29 # include <config.h> | |
30 #endif | |
31 | |
32 #ifdef SOUND_SUPPORTS_MODPLUG | |
33 | |
34 #include <stdio.h> | |
35 #include <stdlib.h> | |
36 #include <string.h> | |
37 #include <assert.h> | |
38 | |
39 #include "SDL_sound.h" | |
40 | |
41 #define __SDL_SOUND_INTERNAL__ | |
42 #include "SDL_sound_internal.h" | |
43 | |
44 #include "modplug.h" | |
45 | |
46 | |
47 static int MODPLUG_init(void); | |
48 static void MODPLUG_quit(void); | |
49 static int MODPLUG_open(Sound_Sample *sample, const char *ext); | |
50 static void MODPLUG_close(Sound_Sample *sample); | |
51 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
|
52 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
|
53 static int MODPLUG_seek(Sound_Sample *sample, Uint32 ms); |
208 | 54 |
55 static const char *extensions_modplug[] = | |
56 { | |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
57 /* 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
|
58 * 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
|
59 */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
60 "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
|
61 "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
|
62 "AMS", /* AMS module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
63 "DBM", /* DigiBooster Pro Module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
64 "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
|
65 "DSM", /* DSIK Internal Format module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
66 "FAR", /* Farandole module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
67 "IT", /* Impulse Tracker IT file */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
68 "MDL", /* DigiTracker module */ |
208 | 69 #if 0 |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
70 "J2B", /* Not implemented? What is it anyway? */ |
208 | 71 #endif |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
72 "MED", /* OctaMed MED file */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
73 "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
|
74 "MT2", /* MadTracker 2.0 */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
75 "MTM", /* MTM file */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
76 "OKT", /* Oktalyzer module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
77 "PTM", /* PTM PolyTracker module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
78 "PSM", /* PSM module */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
79 "S3M", /* ScreamTracker file */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
80 "STM", /* ST 2.xx */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
81 "ULT", |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
82 "UMX", |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
83 "XM", /* FastTracker II */ |
208 | 84 NULL |
85 }; | |
86 | |
87 const Sound_DecoderFunctions __Sound_DecoderFunctions_MODPLUG = | |
88 { | |
89 { | |
90 extensions_modplug, | |
91 "Play modules through ModPlug", | |
92 "Torbjörn Andersson <d91tan@Update.UU.SE>", | |
93 "http://modplug-xmms.sourceforge.net/" | |
94 }, | |
95 | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
96 MODPLUG_init, /* init() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
97 MODPLUG_quit, /* quit() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
98 MODPLUG_open, /* open() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
99 MODPLUG_close, /* close() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
100 MODPLUG_read, /* read() method */ |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
101 MODPLUG_rewind, /* rewind() method */ |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
102 MODPLUG_seek /* seek() method */ |
208 | 103 }; |
104 | |
105 | |
106 static int MODPLUG_init(void) | |
107 { | |
108 ModPlug_Settings settings; | |
109 | |
110 /* The settings will require some experimenting. I've borrowed some | |
111 * of them from the XMMS ModPlug plugin. | |
112 */ | |
113 settings.mFlags = | |
114 MODPLUG_ENABLE_OVERSAMPLING | |
115 | MODPLUG_ENABLE_NOISE_REDUCTION | |
116 | MODPLUG_ENABLE_REVERB | |
117 | MODPLUG_ENABLE_MEGABASS | |
118 | MODPLUG_ENABLE_SURROUND; | |
119 settings.mChannels = 2; | |
120 settings.mBits = 16; | |
121 settings.mFrequency = 44100; | |
122 settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; | |
123 settings.mReverbDepth = 30; | |
124 settings.mReverbDelay = 100; | |
125 settings.mBassAmount = 40; | |
126 settings.mBassRange = 30; | |
127 settings.mSurroundDepth = 20; | |
128 settings.mSurroundDelay = 20; | |
129 settings.mLoopCount = 0; | |
130 | |
131 ModPlug_SetSettings(&settings); | |
132 return(1); /* success. */ | |
133 } /* MODPLUG_init */ | |
134 | |
135 | |
136 static void MODPLUG_quit(void) | |
137 { | |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
138 /* it's a no-op. */ |
208 | 139 } /* MODPLUG_quit */ |
140 | |
141 | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
142 /* |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
143 * Most MOD files I've seen have tended to be a few hundred KB, even if some |
208 | 144 * of them were much smaller than that. |
145 */ | |
146 #define CHUNK_SIZE 65536 | |
147 | |
148 static int MODPLUG_open(Sound_Sample *sample, const char *ext) | |
149 { | |
150 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
151 ModPlugFile *module; | |
152 Uint8 *data; | |
153 size_t size; | |
154 Uint32 retval; | |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
155 int has_extension = 0; |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
156 int i; |
208 | 157 |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
158 /* |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
159 * 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
|
160 * 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
|
161 */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
162 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
|
163 { |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
164 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
|
165 { |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
166 has_extension = 1; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
167 break; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
168 } /* if */ |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
169 } /* for */ |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
170 |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
171 if (!has_extension) |
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
172 { |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
173 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
|
174 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
|
175 return(0); |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
176 } /* if */ |
211
b35c04e4691e
Patched to select streams to handle more carefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
208
diff
changeset
|
177 |
208 | 178 /* ModPlug needs the entire stream in one big chunk. I don't like it, |
179 * but I don't think there's any way around it. | |
180 */ | |
181 data = (Uint8 *) malloc(CHUNK_SIZE); | |
182 BAIL_IF_MACRO(data == NULL, ERR_OUT_OF_MEMORY, 0); | |
183 size = 0; | |
184 | |
185 do | |
186 { | |
187 retval = SDL_RWread(internal->rw, &data[size], 1, CHUNK_SIZE); | |
188 size += retval; | |
189 if (retval == CHUNK_SIZE) | |
190 { | |
191 data = (Uint8 *) realloc(data, size + CHUNK_SIZE); | |
192 BAIL_IF_MACRO(data == NULL, ERR_OUT_OF_MEMORY, 0); | |
193 } /* if */ | |
194 } while (retval > 0); | |
195 | |
196 /* The buffer may be a bit too large, but that doesn't matter. I think | |
197 * it's safe to free it as soon as ModPlug_Load() is finished anyway. | |
198 */ | |
199 module = ModPlug_Load((void *) data, size); | |
200 free(data); | |
201 BAIL_IF_MACRO(module == NULL, "MODPLUG: Not a module file.", 0); | |
202 | |
203 SNDDBG(("MODPLUG: [%d ms] %s\n", | |
204 ModPlug_GetLength(module), ModPlug_GetName(module))); | |
205 | |
206 sample->actual.channels = 2; | |
207 sample->actual.rate = 44100; | |
208 sample->actual.format = AUDIO_S16SYS; | |
209 | |
210 internal->decoder_private = (void *) module; | |
312 | 211 sample->flags = SOUND_SAMPLEFLAG_CANSEEK; |
208 | 212 |
213 SNDDBG(("MODPLUG: Accepting data stream\n")); | |
214 return(1); /* we'll handle this data. */ | |
215 } /* MODPLUG_open */ | |
216 | |
217 | |
218 static void MODPLUG_close(Sound_Sample *sample) | |
219 { | |
220 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
221 ModPlugFile *module = (ModPlugFile *) internal->decoder_private; | |
222 | |
223 ModPlug_Unload(module); | |
224 } /* MODPLUG_close */ | |
225 | |
226 | |
227 static Uint32 MODPLUG_read(Sound_Sample *sample) | |
228 { | |
229 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
230 ModPlugFile *module = (ModPlugFile *) internal->decoder_private; | |
231 int retval; | |
232 | |
233 retval = ModPlug_Read(module, internal->buffer, internal->buffer_size); | |
234 if (retval == 0) | |
235 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
236 return(retval); | |
237 } /* MODPLUG_read */ | |
238 | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
239 |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
240 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
|
241 { |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
242 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
243 ModPlugFile *module = (ModPlugFile *) internal->decoder_private; |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
244 |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
245 ModPlug_Seek(module, 0); |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
246 return(1); |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
247 } /* MODPLUG_rewind */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
211
diff
changeset
|
248 |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
249 |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
250 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
|
251 { |
312 | 252 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; |
253 ModPlugFile *module = (ModPlugFile *) internal->decoder_private; | |
254 | |
255 /* Assume that this will work. */ | |
256 ModPlug_Seek(module, ms); | |
257 return(1); | |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
258 } /* MODPLUG_seek */ |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
259 |
208 | 260 #endif /* SOUND_SUPPORTS_MODPLUG */ |
261 | |
262 | |
263 /* end of modplug.c ... */ |