summaryrefslogtreecommitdiff
path: root/src/libs/sound/decoders/modaud.c
blob: 18c29a2e8907d9aba79791fc88ab30dfdd3557fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* MikMod decoder (.mod adapter)
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "libs/memlib.h"
#include "port.h"
#include "types.h"
#include "endian_uqm.h"
#include "libs/uio.h"
#include "decoder.h"
#include "libs/sound/audiocore.h"
#include "libs/log.h"
#include "modaud.h"

#ifdef USE_INTERNAL_MIKMOD
#	include "libs/mikmod/mikmod.h"
#else
#	include <mikmod.h>
#endif

#define THIS_PTR TFB_SoundDecoder* This

static const char* moda_GetName (void);
static bool moda_InitModule (int flags, const TFB_DecoderFormats*);
static void moda_TermModule (void);
static uint32 moda_GetStructSize (void);
static int moda_GetError (THIS_PTR);
static bool moda_Init (THIS_PTR);
static void moda_Term (THIS_PTR);
static bool moda_Open (THIS_PTR, uio_DirHandle *dir, const char *filename);
static void moda_Close (THIS_PTR);
static int moda_Decode (THIS_PTR, void* buf, sint32 bufsize);
static uint32 moda_Seek (THIS_PTR, uint32 pcm_pos);
static uint32 moda_GetFrame (THIS_PTR);

TFB_SoundDecoderFuncs moda_DecoderVtbl = 
{
	moda_GetName,
	moda_InitModule,
	moda_TermModule,
	moda_GetStructSize,
	moda_GetError,
	moda_Init,
	moda_Term,
	moda_Open,
	moda_Close,
	moda_Decode,
	moda_Seek,
	moda_GetFrame,
};

typedef struct tfb_modsounddecoder
{
	// always the first member
	TFB_SoundDecoder decoder;

	// private
	sint32 last_error;
	MODULE* module;

} TFB_ModSoundDecoder;



// MikMod Output driver
//  we provide our own so that we can use MikMod as
//  generic decoder

static void* buffer;
static ULONG bufsize;
static ULONG written;

static ULONG*
moda_mmout_SetOutputBuffer (void* buf, ULONG size)
{
	buffer = buf;
	bufsize = size;
	written = 0;
	return &written;
}

static BOOL
moda_mmout_IsThere (void)
{
    return 1;
}

static BOOL
moda_mmout_Init (void)
{
    md_mode |= DMODE_SOFT_MUSIC | DMODE_SOFT_SNDFX;
    return VC_Init ();
}

static void
moda_mmout_Exit (void)
{
    VC_Exit ();
}

static void
moda_mmout_Update (void)
{
	written = 0;
	if (!buffer || bufsize == 0)
		return;

	written = VC_WriteBytes (buffer, bufsize);
}

static BOOL
moda_mmout_Reset (void)
{
    return 0;
}

static char MDRIVER_name[] = "Mem Buffer";
static char MDRIVER_version[] = "Mem Buffer driver v1.1";
static char MDRIVER_alias[] = "membuf";

static MDRIVER moda_mmout_drv =
{
	NULL,
	//xxx libmikmod does not declare these fields const; it probably should.
	MDRIVER_name,              // Name
	MDRIVER_version,           // Version
	0, 255,                    // Voice limits
	MDRIVER_alias,             // Alias

// The minimum mikmod version we support is 3.1.8
#if (LIBMIKMOD_VERSION_MAJOR > 3) || \
		((LIBMIKMOD_VERSION_MAJOR == 3) && (LIBMIKMOD_VERSION_MINOR >= 2))
	NULL,                      // Cmdline help
#endif

    NULL,
    moda_mmout_IsThere,
    VC_SampleLoad,
    VC_SampleUnload,
    VC_SampleSpace,
    VC_SampleLength,
    moda_mmout_Init,
    moda_mmout_Exit,
    moda_mmout_Reset,
    VC_SetNumVoices,
    VC_PlayStart,
    VC_PlayStop,
    moda_mmout_Update,
    NULL,               /* FIXME: Pause */
    VC_VoiceSetVolume,
    VC_VoiceGetVolume,
    VC_VoiceSetFrequency,
    VC_VoiceGetFrequency,
    VC_VoiceSetPanning,
    VC_VoiceGetPanning,
    VC_VoicePlay,
    VC_VoiceStop,
    VC_VoiceStopped,
    VC_VoiceGetPosition,
    VC_VoiceRealVolume
};


static const TFB_DecoderFormats* moda_formats = NULL;

// MikMod READER interface
//  we provide our own so that we can do loading via uio 
//
typedef struct MUIOREADER
{
	MREADER     core;
	uio_Stream* file;

} MUIOREADER;

static BOOL
moda_uioReader_Eof (MREADER* reader)
{
	return uio_feof (((MUIOREADER*)reader)->file);
}

static BOOL
moda_uioReader_Read (MREADER* reader, void* ptr, size_t size)
{
	return uio_fread (ptr, size, 1, ((MUIOREADER*)reader)->file);
}

static int
moda_uioReader_Get (MREADER* reader)
{
	return uio_fgetc (((MUIOREADER*)reader)->file);
}

static BOOL
moda_uioReader_Seek (MREADER* reader, long offset, int whence)
{
	return uio_fseek (((MUIOREADER*)reader)->file, offset, whence);
}

static long
moda_uioReader_Tell (MREADER* reader)
{
	return uio_ftell (((MUIOREADER*)reader)->file);
}

static MREADER*
moda_new_uioReader (uio_Stream* fp)
{
	MUIOREADER* reader = (MUIOREADER*) HMalloc (sizeof(MUIOREADER));
	if (reader)
	{
		reader->core.Eof  = &moda_uioReader_Eof;
		reader->core.Read = &moda_uioReader_Read;
		reader->core.Get  = &moda_uioReader_Get;
		reader->core.Seek = &moda_uioReader_Seek;
		reader->core.Tell = &moda_uioReader_Tell;
		reader->file = fp;
	}
	return (MREADER*)reader;
}

static void
moda_delete_uioReader (MREADER* reader)
{
	if (reader)
		HFree (reader);
}


static const char*
moda_GetName (void)
{
	return "MikMod";
}

static bool
moda_InitModule (int flags, const TFB_DecoderFormats* fmts)
{
    MikMod_RegisterDriver (&moda_mmout_drv);
    MikMod_RegisterAllLoaders ();

	if (flags & audio_QUALITY_HIGH)
	{
		md_mode = DMODE_HQMIXER|DMODE_STEREO|DMODE_16BITS|DMODE_INTERP|DMODE_SURROUND;
		md_mixfreq = 44100;
		md_reverb = 1;
	}
	else if (flags & audio_QUALITY_LOW)
	{
		md_mode = DMODE_SOFT_MUSIC|DMODE_STEREO|DMODE_16BITS;
#ifdef __SYMBIAN32__
		md_mixfreq = 11025;
#else
		md_mixfreq = 22050;
#endif		
		md_reverb = 0;
	}
	else
	{
		md_mode = DMODE_SOFT_MUSIC|DMODE_STEREO|DMODE_16BITS|DMODE_INTERP;
		md_mixfreq = 44100;
		md_reverb = 0;
	}
	
	md_pansep = 64;

	if (MikMod_Init (NULL))
	{
		log_add (log_Error, "MikMod_Init() failed, %s", 
			MikMod_strerror (MikMod_errno));
		return false;
	}

	moda_formats = fmts;

	return true;
}

static void
moda_TermModule (void)
{
	MikMod_Exit ();
}

static uint32
moda_GetStructSize (void)
{
	return sizeof (TFB_ModSoundDecoder);
}

static int
moda_GetError (THIS_PTR)
{
	TFB_ModSoundDecoder* moda = (TFB_ModSoundDecoder*) This;
	int ret = moda->last_error;
	moda->last_error = 0;
	return ret;
}

static bool
moda_Init (THIS_PTR)
{
	//TFB_ModSoundDecoder* moda = (TFB_ModSoundDecoder*) This;
	This->need_swap =
			moda_formats->big_endian != moda_formats->want_big_endian;
	return true;
}

static void
moda_Term (THIS_PTR)
{
	//TFB_ModSoundDecoder* moda = (TFB_ModSoundDecoder*) This;
	moda_Close (This); // ensure cleanup
}

static bool
moda_Open (THIS_PTR, uio_DirHandle *dir, const char *filename)
{
	TFB_ModSoundDecoder* moda = (TFB_ModSoundDecoder*) This;
	uio_Stream *fp;
	MREADER* reader;
	MODULE* mod;

	fp = uio_fopen (dir, filename, "rb");
	if (!fp)
	{
		moda->last_error = errno;
		return false;
	}

	reader = moda_new_uioReader (fp);
	if (!reader)
	{
		moda->last_error = -1;
		uio_fclose (fp);
		return false;
	}

	mod = Player_LoadGeneric (reader, 8, 0);
	
	// can already dispose of reader and fileh
	moda_delete_uioReader (reader);
	uio_fclose (fp);
	if (!mod)
	{
		log_add (log_Warning, "moda_Open(): could not load %s", filename);
		return false;
	}

	moda->module = mod;
	mod->extspd = 1;
	mod->panflag = 1;
	mod->wrap = 0;
	mod->loop = 1;

	This->format = moda_formats->stereo16;
	This->frequency = md_mixfreq;
	This->length = 0; // FIXME way to obtain this from mikmod?

	moda->last_error = 0;

	return true;
}

static void
moda_Close (THIS_PTR)
{
	TFB_ModSoundDecoder* moda = (TFB_ModSoundDecoder*) This;
	
	if (moda->module)
	{
		Player_Free (moda->module);
		moda->module = NULL;
	}
}

static int
moda_Decode (THIS_PTR, void* buf, sint32 bufsize)
{
	TFB_ModSoundDecoder* moda = (TFB_ModSoundDecoder*) This;
	volatile ULONG* poutsize;

	Player_Start (moda->module);
	if (!Player_Active())
		return 0;

	poutsize = moda_mmout_SetOutputBuffer (buf, bufsize);
	MikMod_Update ();
	
	return *poutsize;
}

static uint32
moda_Seek (THIS_PTR, uint32 pcm_pos)
{
	TFB_ModSoundDecoder* moda = (TFB_ModSoundDecoder*) This;
	
	Player_Start (moda->module);
	if (pcm_pos)
		log_add (log_Debug, "moda_Seek(): "
				"non-zero seek positions not supported for mod");
	Player_SetPosition (0);

	return 0;
}

static uint32
moda_GetFrame (THIS_PTR)
{
	TFB_ModSoundDecoder* moda = (TFB_ModSoundDecoder*) This;
	return moda->module->sngpos;
}