Mercurial > SDL_sound_CoreAudio
annotate decoders/mod.c @ 113:7cd718877f3f
Updated.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Thu, 04 Oct 2001 01:22:03 +0000 |
parents | 40de367eb59e |
children | bd224f22e6b2 |
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 * | |
94
230f75fac1d1
Changed comment regarding "the mikmod directory".
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
26 * Please see the file LICENSE 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 | |
56 const Sound_DecoderFunctions __Sound_DecoderFunctions_MOD = | |
57 { | |
58 { | |
59 "MOD", | |
60 "Play modules through MikMod", | |
61 "Torbjörn Andersson <d91tan@Update.UU.SE>", | |
62 "http://www.mikmod.org/" | |
63 }, | |
64 | |
65 MOD_init, /* init() method */ | |
66 MOD_quit, /* quit() method */ | |
67 MOD_open, /* open() method */ | |
68 MOD_close, /* close() method */ | |
69 MOD_read /* read() method */ | |
70 }; | |
71 | |
72 /* this is what we store in our internal->decoder_private field... */ | |
73 typedef struct { | |
74 MODULE *module; | |
75 } mod_t; | |
76 | |
77 | |
78 /* Make MikMod read from a RWops... */ | |
79 | |
80 typedef struct MRWOPSREADER { | |
81 MREADER core; | |
82 Sound_Sample *sample; | |
83 int end; | |
84 } MRWOPSREADER; | |
85 | |
86 static BOOL _mm_RWopsReader_eof(MREADER *reader) | |
87 { | |
88 MRWOPSREADER *rwops_reader = (MRWOPSREADER *) reader; | |
89 Sound_Sample *sample = rwops_reader->sample; | |
90 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
91 int pos = SDL_RWtell(internal->rw); | |
92 | |
93 if (rwops_reader->end == pos) | |
94 return(1); | |
95 | |
96 return(0); | |
97 } /* _mm_RWopsReader_eof */ | |
98 | |
99 | |
100 static BOOL _mm_RWopsReader_read(MREADER *reader, void *ptr, size_t size) | |
101 { | |
102 MRWOPSREADER *rwops_reader = (MRWOPSREADER *) reader; | |
103 Sound_Sample *sample = rwops_reader->sample; | |
104 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
105 return(SDL_RWread(internal->rw, ptr, size, 1)); | |
106 } /* _mm_RWopsReader_Read */ | |
107 | |
108 | |
109 static int _mm_RWopsReader_get(MREADER *reader) | |
110 { | |
111 char buf; | |
112 MRWOPSREADER *rwops_reader = (MRWOPSREADER *) reader; | |
113 Sound_Sample *sample = rwops_reader->sample; | |
114 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
115 | |
116 if (SDL_RWread(internal->rw, &buf, 1, 1) != 1) | |
117 return(EOF); | |
118 | |
119 return((int) buf); | |
120 } /* _mm_RWopsReader_get */ | |
121 | |
122 | |
123 static BOOL _mm_RWopsReader_seek(MREADER *reader, long offset, int whence) | |
124 { | |
125 MRWOPSREADER *rwops_reader = (MRWOPSREADER *) reader; | |
126 Sound_Sample *sample = rwops_reader->sample; | |
127 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
128 | |
129 return(SDL_RWseek(internal->rw, offset, whence)); | |
130 } /* _mm_RWopsReader_seek */ | |
131 | |
132 | |
133 static long _mm_RWopsReader_tell(MREADER *reader) | |
134 { | |
135 MRWOPSREADER *rwops_reader = (MRWOPSREADER *) reader; | |
136 Sound_Sample *sample = rwops_reader->sample; | |
137 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
138 | |
139 return(SDL_RWtell(internal->rw)); | |
140 } /* _mm_RWopsReader_tell */ | |
141 | |
142 | |
143 static MREADER *_mm_new_rwops_reader(Sound_Sample *sample) | |
144 { | |
145 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
146 | |
147 MRWOPSREADER *reader = (MRWOPSREADER *) malloc(sizeof (MRWOPSREADER)); | |
148 if (reader != NULL) | |
149 { | |
150 int here; | |
151 reader->core.Eof = _mm_RWopsReader_eof; | |
152 reader->core.Read = _mm_RWopsReader_read; | |
153 reader->core.Get = _mm_RWopsReader_get; | |
154 reader->core.Seek = _mm_RWopsReader_seek; | |
155 reader->core.Tell = _mm_RWopsReader_tell; | |
156 reader->sample = sample; | |
157 | |
158 /* RWops does not explicitly support an eof check, so we shall find | |
159 the end manually - this requires seek support for the RWop */ | |
160 here = SDL_RWtell(internal->rw); | |
161 reader->end = SDL_RWseek(internal->rw, 0, SEEK_END); | |
162 SDL_RWseek(internal->rw, here, SEEK_SET); /* Move back */ | |
163 /* !!! FIXME: What happens if the seek fails? */ | |
164 } /* if */ | |
165 | |
166 return((MREADER *) reader); | |
167 } /* _mm_new_rwops_reader */ | |
168 | |
169 | |
170 static void _mm_delete_rwops_reader(MREADER *reader) | |
171 { | |
172 /* SDL_sound will delete the RWops and sample at a higher level... */ | |
173 if (reader != NULL) | |
174 free(reader); | |
175 } /* _mm_delete_rwops_reader */ | |
176 | |
177 | |
178 | |
179 static int MOD_init(void) | |
180 { | |
52
69d56e196de7
going with MikMod defaults (adds more reverb) and only register the "no
Ryan C. Gordon <icculus@icculus.org>
parents:
43
diff
changeset
|
181 MikMod_RegisterDriver(&drv_nos); |
43 | 182 MikMod_RegisterAllLoaders(); |
183 | |
184 /* | |
185 * Both DMODE_SOFT_MUSIC and DMODE_16BITS should be set by default, | |
186 * so this is just for clarity. I haven't experimented with any of | |
187 * the other flags. There are a few which are said to give better | |
188 * sound quality. | |
189 */ | |
190 md_mode |= (DMODE_SOFT_MUSIC | DMODE_16BITS); | |
52
69d56e196de7
going with MikMod defaults (adds more reverb) and only register the "no
Ryan C. Gordon <icculus@icculus.org>
parents:
43
diff
changeset
|
191 |
69d56e196de7
going with MikMod defaults (adds more reverb) and only register the "no
Ryan C. Gordon <icculus@icculus.org>
parents:
43
diff
changeset
|
192 #if 0 |
43 | 193 /* |
194 * SDL_mixer used to set these, but I don't know... is there | |
195 * something wrong with the defaults? Actually, the only difference | |
196 * from the defaults is md_reverb, which is usually 6. | |
197 */ | |
198 md_device = 0; /* Selects sound driver. 0 = autodetect */ | |
199 md_volume = 96; /* Overall sound volume, 0 - 128 */ | |
200 md_musicvolume = 128; /* Module volume, 0 - 128 */ | |
201 md_sndfxvolume = 128; /* Sound effect volume, 0 - 128 */ | |
202 md_pansep = 128; /* Stereo channels separation, 0 - 128 */ | |
203 md_reverb = 0; /* Sound reverbation, 0 - 15 */ | |
204 #endif | |
205 | |
206 BAIL_IF_MACRO(MikMod_Init(""), MikMod_strerror(MikMod_errno), 0); | |
207 | |
208 return(1); /* success. */ | |
209 } /* MOD_init */ | |
210 | |
211 | |
212 static void MOD_quit(void) | |
213 { | |
214 MikMod_Exit(); | |
215 } /* MOD_quit */ | |
216 | |
217 | |
218 static int MOD_open(Sound_Sample *sample, const char *ext) | |
219 { | |
220 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
221 MREADER *reader; | |
222 mod_t *m; | |
223 | |
224 m = (mod_t *) malloc(sizeof(mod_t)); | |
225 BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, 0); | |
226 reader = _mm_new_rwops_reader(sample); | |
227 BAIL_IF_MACRO(reader == NULL, ERR_OUT_OF_MEMORY, 0); | |
228 m->module = Player_LoadGeneric(reader, 64, 0); | |
229 _mm_delete_rwops_reader(reader); | |
230 BAIL_IF_MACRO(m->module == NULL, "MOD: Not a module file.", 0); | |
231 | |
232 md_mixfreq = sample->desired.rate; | |
233 sample->actual.channels = 2; | |
234 sample->actual.rate = md_mixfreq; | |
235 sample->actual.format = AUDIO_S16SYS; | |
236 internal->decoder_private = (void *) m; | |
237 | |
238 Player_Start(m->module); | |
239 Player_SetPosition(0); | |
240 | |
241 /* !!! FIXME: A little late to be giving this information... */ | |
242 sample->flags = SOUND_SAMPLEFLAG_NEEDSEEK; | |
243 | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
52
diff
changeset
|
244 SNDDBG(("MOD: Accepting data stream\n")); |
43 | 245 return(1); /* we'll handle this data. */ |
246 } /* MOD_open */ | |
247 | |
248 | |
249 static void MOD_close(Sound_Sample *sample) | |
250 { | |
251 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
252 mod_t *m = (mod_t *) internal->decoder_private; | |
253 | |
254 Player_Free(m->module); | |
92
6252979e2453
Fixed a memory leak in the close() method.
Ryan C. Gordon <icculus@icculus.org>
parents:
64
diff
changeset
|
255 free(m); |
43 | 256 } /* MOD_close */ |
257 | |
258 | |
259 static Uint32 MOD_read(Sound_Sample *sample) | |
260 { | |
261 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
262 mod_t *m = (mod_t *) internal->decoder_private; | |
263 | |
264 /* Switch to the current module, stopping any previous one. */ | |
265 Player_Start(m->module); | |
266 if (!Player_Active()) | |
267 { | |
268 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
269 return(0); | |
270 } /* if */ | |
271 return((Uint32) VC_WriteBytes(internal->buffer, internal->buffer_size)); | |
272 } /* MOD_read */ | |
273 | |
64
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
274 #endif /* SOUND_SUPPORTS_MOD */ |
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
275 |
43 | 276 |
277 /* end of mod.c ... */ | |
278 |