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