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