comparison decoders/mikmod.c @ 207:857e53c5a2da

Renamed from mod.c (see modplug.c for the OTHER mod decoder).
author Ryan C. Gordon <icculus@icculus.org>
date Thu, 10 Jan 2002 01:16:24 +0000
parents
children b35c04e4691e
comparison
equal deleted inserted replaced
206:0254ff448a63 207:857e53c5a2da
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 MikMod does, and
22 * is based on SDL_mixer.
23 *
24 * Please see the file COPYING in the source's root directory.
25 *
26 * This file written by Torbjörn Andersson (d91tan@Update.UU.SE)
27 */
28
29 #if HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #ifdef SOUND_SUPPORTS_MIKMOD
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <assert.h>
39
40 #include "SDL_sound.h"
41
42 #define __SDL_SOUND_INTERNAL__
43 #include "SDL_sound_internal.h"
44
45 #include "mikmod.h"
46
47
48 static int MIKMOD_init(void);
49 static void MIKMOD_quit(void);
50 static int MIKMOD_open(Sound_Sample *sample, const char *ext);
51 static void MIKMOD_close(Sound_Sample *sample);
52 static Uint32 MIKMOD_read(Sound_Sample *sample);
53
54 static const char *extensions_mikmod[] =
55 {
56 "MOD", "IT", "XM", "S3M", "MTM", "669", "STM", "ULT",
57 "FAR", "MED", "AMF", "DSM", "IMF", "GDM", "STX", "OKT",
58 NULL
59 };
60
61 const Sound_DecoderFunctions __Sound_DecoderFunctions_MIKMOD =
62 {
63 {
64 extensions_mikmod,
65 "Play modules through MikMod",
66 "Torbjörn Andersson <d91tan@Update.UU.SE>",
67 "http://www.mikmod.org/"
68 },
69
70 MIKMOD_init, /* init() method */
71 MIKMOD_quit, /* quit() method */
72 MIKMOD_open, /* open() method */
73 MIKMOD_close, /* close() method */
74 MIKMOD_read /* read() method */
75 };
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 MIKMOD_init(void)
180 {
181 MikMod_RegisterDriver(&drv_nos);
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);
191 md_mixfreq = 0;
192
193 BAIL_IF_MACRO(MikMod_Init(""), MikMod_strerror(MikMod_errno), 0);
194
195 return(1); /* success. */
196 } /* MIKMOD_init */
197
198
199 static void MIKMOD_quit(void)
200 {
201 MikMod_Exit();
202 md_mixfreq = 0;
203 } /* MIKMOD_quit */
204
205
206 static int MIKMOD_open(Sound_Sample *sample, const char *ext)
207 {
208 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
209 MREADER *reader;
210 MODULE *module;
211
212 reader = _mm_new_rwops_reader(sample);
213 BAIL_IF_MACRO(reader == NULL, ERR_OUT_OF_MEMORY, 0);
214 module = Player_LoadGeneric(reader, 64, 0);
215 _mm_delete_rwops_reader(reader);
216 BAIL_IF_MACRO(module == NULL, "MIKMOD: Not a module file.", 0);
217
218 module->extspd = 1;
219 module->panflag = 1;
220 module->wrap = 0;
221 module->loop = 0;
222
223 if (md_mixfreq == 0)
224 md_mixfreq = (!sample->desired.rate) ? 44100 : sample->desired.rate;
225
226 sample->actual.channels = 2;
227 sample->actual.rate = md_mixfreq;
228 sample->actual.format = AUDIO_S16SYS;
229 internal->decoder_private = (void *) module;
230
231 Player_Start(module);
232 Player_SetPosition(0);
233
234 /* !!! FIXME: A little late to be giving this information... */
235 sample->flags = SOUND_SAMPLEFLAG_NEEDSEEK;
236
237 SNDDBG(("MIKMOD: Name: %s\n", module->songname));
238 SNDDBG(("MIKMOD: Type: %s\n", module->modtype));
239 SNDDBG(("MIKMOD: Accepting data stream\n"));
240
241 return(1); /* we'll handle this data. */
242 } /* MIKMOD_open */
243
244
245 static void MIKMOD_close(Sound_Sample *sample)
246 {
247 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
248 MODULE *module = (MODULE *) internal->decoder_private;
249
250 Player_Free(module);
251 } /* MIKMOD_close */
252
253
254 static Uint32 MIKMOD_read(Sound_Sample *sample)
255 {
256 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
257 MODULE *module = (MODULE *) internal->decoder_private;
258
259 /* Switch to the current module, stopping any previous one. */
260 Player_Start(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 } /* MIKMOD_read */
268
269 #endif /* SOUND_SUPPORTS_MIKMOD */
270
271
272 /* end of mikmod.c ... */