Mercurial > SDL_sound_CoreAudio
annotate decoders/mod.c @ 200:f75ed2d72238
Rewrite to call an included, patched version of Timidity routines directly.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Fri, 04 Jan 2002 06:50:37 +0000 |
parents | 47cc2de2ae36 |
children |
rev | line source |
---|---|
43 | 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 - through MikMod - for SDL_sound. | |
22 * | |
23 * This driver handles anything our subset of MikMod handles. It is based on | |
24 * SDL_mixer's MikMod music player. | |
25 * | |
184
47cc2de2ae36
Changed reference to "LICENSE" file to "COPYING".
Ryan C. Gordon <icculus@icculus.org>
parents:
149
diff
changeset
|
26 * Please see the file COPYING in the source's root directory. |
43 | 27 * |
28 * This file written by Torbjörn Andersson (d91tan@Update.UU.SE) | |
29 */ | |
30 | |
106
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
31 #if HAVE_CONFIG_H |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
32 # include <config.h> |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
33 #endif |
100
6d9fdec2f708
added config.h, added --enable-debug flag, various other changes to the build system
fingolfin
parents:
94
diff
changeset
|
34 |
104
103cfcb3c014
Updated to fix build system problem.
Ryan C. Gordon <icculus@icculus.org>
parents:
100
diff
changeset
|
35 #ifdef SOUND_SUPPORTS_MOD |
103cfcb3c014
Updated to fix build system problem.
Ryan C. Gordon <icculus@icculus.org>
parents:
100
diff
changeset
|
36 |
43 | 37 #include <stdio.h> |
38 #include <stdlib.h> | |
39 #include <string.h> | |
40 #include <assert.h> | |
106
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
41 |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
42 #include "SDL_sound.h" |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
43 |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
44 #define __SDL_SOUND_INTERNAL__ |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
45 #include "SDL_sound_internal.h" |
40de367eb59e
Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents:
104
diff
changeset
|
46 |
43 | 47 #include "mikmod.h" |
48 | |
49 | |
50 static int MOD_init(void); | |
51 static void MOD_quit(void); | |
52 static int MOD_open(Sound_Sample *sample, const char *ext); | |
53 static void MOD_close(Sound_Sample *sample); | |
54 static Uint32 MOD_read(Sound_Sample *sample); | |
55 | |
149
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
120
diff
changeset
|
56 static const char *extensions_mikmod[] = |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
120
diff
changeset
|
57 { |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
120
diff
changeset
|
58 "MOD", "IT", "XM", "S3M", "MTM", "669", "STM", "ULT", |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
120
diff
changeset
|
59 "FAR", "MED", "AMF", "DSM", "IMF", "GDM", "STX", "OKT", |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
120
diff
changeset
|
60 NULL |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
120
diff
changeset
|
61 }; |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
120
diff
changeset
|
62 |
43 | 63 const Sound_DecoderFunctions __Sound_DecoderFunctions_MOD = |
64 { | |
65 { | |
149
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
120
diff
changeset
|
66 extensions_mikmod, |
43 | 67 "Play modules through MikMod", |
68 "Torbjörn Andersson <d91tan@Update.UU.SE>", | |
69 "http://www.mikmod.org/" | |
70 }, | |
71 | |
72 MOD_init, /* init() method */ | |
73 MOD_quit, /* quit() method */ | |
74 MOD_open, /* open() method */ | |
75 MOD_close, /* close() method */ | |
76 MOD_read /* read() method */ | |
77 }; | |
78 | |
79 /* this is what we store in our internal->decoder_private field... */ | |
80 typedef struct { | |
81 MODULE *module; | |
82 } mod_t; | |
83 | |
84 | |
85 /* Make MikMod read from a RWops... */ | |
86 | |
87 typedef struct MRWOPSREADER { | |
88 MREADER core; | |
89 Sound_Sample *sample; | |
90 int end; | |
91 } MRWOPSREADER; | |
92 | |
93 static BOOL _mm_RWopsReader_eof(MREADER *reader) | |
94 { | |
95 MRWOPSREADER *rwops_reader = (MRWOPSREADER *) reader; | |
96 Sound_Sample *sample = rwops_reader->sample; | |
97 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
98 int pos = SDL_RWtell(internal->rw); | |
99 | |
100 if (rwops_reader->end == pos) | |
101 return(1); | |
102 | |
103 return(0); | |
104 } /* _mm_RWopsReader_eof */ | |
105 | |
106 | |
107 static BOOL _mm_RWopsReader_read(MREADER *reader, void *ptr, size_t size) | |
108 { | |
109 MRWOPSREADER *rwops_reader = (MRWOPSREADER *) reader; | |
110 Sound_Sample *sample = rwops_reader->sample; | |
111 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
112 return(SDL_RWread(internal->rw, ptr, size, 1)); | |
113 } /* _mm_RWopsReader_Read */ | |
114 | |
115 | |
116 static int _mm_RWopsReader_get(MREADER *reader) | |
117 { | |
118 char buf; | |
119 MRWOPSREADER *rwops_reader = (MRWOPSREADER *) reader; | |
120 Sound_Sample *sample = rwops_reader->sample; | |
121 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
122 | |
123 if (SDL_RWread(internal->rw, &buf, 1, 1) != 1) | |
124 return(EOF); | |
125 | |
126 return((int) buf); | |
127 } /* _mm_RWopsReader_get */ | |
128 | |
129 | |
130 static BOOL _mm_RWopsReader_seek(MREADER *reader, long offset, int whence) | |
131 { | |
132 MRWOPSREADER *rwops_reader = (MRWOPSREADER *) reader; | |
133 Sound_Sample *sample = rwops_reader->sample; | |
134 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
135 | |
136 return(SDL_RWseek(internal->rw, offset, whence)); | |
137 } /* _mm_RWopsReader_seek */ | |
138 | |
139 | |
140 static long _mm_RWopsReader_tell(MREADER *reader) | |
141 { | |
142 MRWOPSREADER *rwops_reader = (MRWOPSREADER *) reader; | |
143 Sound_Sample *sample = rwops_reader->sample; | |
144 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
145 | |
146 return(SDL_RWtell(internal->rw)); | |
147 } /* _mm_RWopsReader_tell */ | |
148 | |
149 | |
150 static MREADER *_mm_new_rwops_reader(Sound_Sample *sample) | |
151 { | |
152 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
153 | |
154 MRWOPSREADER *reader = (MRWOPSREADER *) malloc(sizeof (MRWOPSREADER)); | |
155 if (reader != NULL) | |
156 { | |
157 int here; | |
158 reader->core.Eof = _mm_RWopsReader_eof; | |
159 reader->core.Read = _mm_RWopsReader_read; | |
160 reader->core.Get = _mm_RWopsReader_get; | |
161 reader->core.Seek = _mm_RWopsReader_seek; | |
162 reader->core.Tell = _mm_RWopsReader_tell; | |
163 reader->sample = sample; | |
164 | |
165 /* RWops does not explicitly support an eof check, so we shall find | |
166 the end manually - this requires seek support for the RWop */ | |
167 here = SDL_RWtell(internal->rw); | |
168 reader->end = SDL_RWseek(internal->rw, 0, SEEK_END); | |
169 SDL_RWseek(internal->rw, here, SEEK_SET); /* Move back */ | |
170 /* !!! FIXME: What happens if the seek fails? */ | |
171 } /* if */ | |
172 | |
173 return((MREADER *) reader); | |
174 } /* _mm_new_rwops_reader */ | |
175 | |
176 | |
177 static void _mm_delete_rwops_reader(MREADER *reader) | |
178 { | |
179 /* SDL_sound will delete the RWops and sample at a higher level... */ | |
180 if (reader != NULL) | |
181 free(reader); | |
182 } /* _mm_delete_rwops_reader */ | |
183 | |
184 | |
185 | |
186 static int MOD_init(void) | |
187 { | |
52
69d56e196de7
going with MikMod defaults (adds more reverb) and only register the "no
Ryan C. Gordon <icculus@icculus.org>
parents:
43
diff
changeset
|
188 MikMod_RegisterDriver(&drv_nos); |
43 | 189 MikMod_RegisterAllLoaders(); |
190 | |
191 /* | |
192 * Both DMODE_SOFT_MUSIC and DMODE_16BITS should be set by default, | |
193 * so this is just for clarity. I haven't experimented with any of | |
194 * the other flags. There are a few which are said to give better | |
195 * sound quality. | |
196 */ | |
197 md_mode |= (DMODE_SOFT_MUSIC | DMODE_16BITS); | |
120
bd224f22e6b2
Handles sample rate somewhat more robustly.
Ryan C. Gordon <icculus@icculus.org>
parents:
106
diff
changeset
|
198 md_mixfreq = 0; |
43 | 199 |
200 BAIL_IF_MACRO(MikMod_Init(""), MikMod_strerror(MikMod_errno), 0); | |
201 | |
202 return(1); /* success. */ | |
203 } /* MOD_init */ | |
204 | |
205 | |
206 static void MOD_quit(void) | |
207 { | |
208 MikMod_Exit(); | |
120
bd224f22e6b2
Handles sample rate somewhat more robustly.
Ryan C. Gordon <icculus@icculus.org>
parents:
106
diff
changeset
|
209 md_mixfreq = 0; |
43 | 210 } /* MOD_quit */ |
211 | |
212 | |
213 static int MOD_open(Sound_Sample *sample, const char *ext) | |
214 { | |
215 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
216 MREADER *reader; | |
217 mod_t *m; | |
218 | |
219 m = (mod_t *) malloc(sizeof(mod_t)); | |
220 BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, 0); | |
221 reader = _mm_new_rwops_reader(sample); | |
222 BAIL_IF_MACRO(reader == NULL, ERR_OUT_OF_MEMORY, 0); | |
223 m->module = Player_LoadGeneric(reader, 64, 0); | |
224 _mm_delete_rwops_reader(reader); | |
225 BAIL_IF_MACRO(m->module == NULL, "MOD: Not a module file.", 0); | |
226 | |
120
bd224f22e6b2
Handles sample rate somewhat more robustly.
Ryan C. Gordon <icculus@icculus.org>
parents:
106
diff
changeset
|
227 if (md_mixfreq == 0) |
bd224f22e6b2
Handles sample rate somewhat more robustly.
Ryan C. Gordon <icculus@icculus.org>
parents:
106
diff
changeset
|
228 md_mixfreq = (!sample->desired.rate) ? 44100 : sample->desired.rate; |
bd224f22e6b2
Handles sample rate somewhat more robustly.
Ryan C. Gordon <icculus@icculus.org>
parents:
106
diff
changeset
|
229 |
43 | 230 sample->actual.channels = 2; |
231 sample->actual.rate = md_mixfreq; | |
232 sample->actual.format = AUDIO_S16SYS; | |
233 internal->decoder_private = (void *) m; | |
234 | |
235 Player_Start(m->module); | |
236 Player_SetPosition(0); | |
237 | |
238 /* !!! FIXME: A little late to be giving this information... */ | |
239 sample->flags = SOUND_SAMPLEFLAG_NEEDSEEK; | |
240 | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
52
diff
changeset
|
241 SNDDBG(("MOD: Accepting data stream\n")); |
43 | 242 return(1); /* we'll handle this data. */ |
243 } /* MOD_open */ | |
244 | |
245 | |
246 static void MOD_close(Sound_Sample *sample) | |
247 { | |
248 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
249 mod_t *m = (mod_t *) internal->decoder_private; | |
250 | |
251 Player_Free(m->module); | |
92
6252979e2453
Fixed a memory leak in the close() method.
Ryan C. Gordon <icculus@icculus.org>
parents:
64
diff
changeset
|
252 free(m); |
43 | 253 } /* MOD_close */ |
254 | |
255 | |
256 static Uint32 MOD_read(Sound_Sample *sample) | |
257 { | |
258 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
259 mod_t *m = (mod_t *) internal->decoder_private; | |
260 | |
261 /* Switch to the current module, stopping any previous one. */ | |
262 Player_Start(m->module); | |
263 if (!Player_Active()) | |
264 { | |
265 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
266 return(0); | |
267 } /* if */ | |
268 return((Uint32) VC_WriteBytes(internal->buffer, internal->buffer_size)); | |
269 } /* MOD_read */ | |
270 | |
64
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
271 #endif /* SOUND_SUPPORTS_MOD */ |
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
272 |
43 | 273 |
274 /* end of mod.c ... */ | |
275 |