forked from rjking58/development
/
sys_musd_fmod.c
482 lines (426 loc) · 12 KB
/
sys_musd_fmod.c
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* DE1: $Id: sys_musd_fmod.c,v 1.9 2004/05/31 15:04:44 skyjake Exp $
* Copyright (C) 2003, 2004 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* 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: http://www.opensource.org/
*/
/*
* sys_musd_fmod.c: Music Driver for FMOD (http://www.fmod.org/)
*
* Plays CD music and non-MUS songs (MIDI, MP3, MOD...) using
* the excellent FMOD. Link with fmodvc.lib.
*/
// HEADER FILES ------------------------------------------------------------
#include <fmod.h>
#include <fmod_errors.h>
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_audio.h"
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
int DM_FModInit(void);
void DM_FModShutdown(void);
// Ext Interface.
int DM_FModExtInit(void);
void DM_FModExtShutdown(void);
void DM_FModExtReset();
void DM_FModExtUpdate(void);
void DM_FModExtSet(int property, float value);
int DM_FModExtGet(int property, void *ptr);
void DM_FModExtPause(int pause);
void DM_FModExtStop(void);
void *DM_FModExtSongBuffer(int length);
int DM_FModExtPlayFile(const char *path, int looped);
int DM_FModExtPlayBuffer(int looped);
// CD Interface.
int DM_FModCDInit(void);
void DM_FModCDShutdown(void);
void DM_FModCDUpdate(void);
void DM_FModCDSet(int property, float value);
int DM_FModCDGet(int property, void *ptr);
void DM_FModCDPause(int pause);
void DM_FModCDStop(void);
int DM_FModCDPlay(int track, int looped);
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
musdriver_t musd_fmod = {
DM_FModInit,
DM_FModShutdown
};
musinterface_ext_t musd_fmod_iext = {
DM_FModExtInit,
DM_FModExtUpdate,
DM_FModExtSet,
DM_FModExtGet,
DM_FModExtPause,
DM_FModExtStop,
DM_FModExtSongBuffer,
DM_FModExtPlayFile,
DM_FModExtPlayBuffer
};
musinterface_cd_t musd_fmod_icd = {
DM_FModCDInit,
DM_FModCDUpdate,
DM_FModCDSet,
DM_FModCDGet,
DM_FModCDPause,
DM_FModCDStop,
DM_FModCDPlay
};
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static boolean inited = false, ext_inited = false;
static int ext_volume = 200;
//static boolean ext_looped_play, ext_playing;
//static int ext_start_time;
static int original_cd_volume;
static void *song;
static int song_size, stream_channel;
static FMUSIC_MODULE *module;
static FSOUND_STREAM *stream;
// CODE --------------------------------------------------------------------
//===========================================================================
// DM_FModInit
//===========================================================================
int DM_FModInit(void)
{
if(inited)
return true;
if(FSOUND_GetVersion() < FMOD_VERSION)
{
Con_Message
("DM_FModInit: You are using the wrong version of FMOD.DLL!\n"
" You should be using version %.02f.\n", FMOD_VERSION);
return false;
}
if(!FSOUND_SetOutput(FSOUND_OUTPUT_DSOUND))
{
Con_Message("DM_FModInit: Can't use DirectSound.\n");
if(!FSOUND_SetOutput(FSOUND_OUTPUT_WINMM))
{
Con_Message("DM_FModInit: Can't use WINMM!! Aborting...\n");
return false;
}
}
if(!FSOUND_Init(44100, 16, 0))
{
Con_Message("DM_FModInit: Init failed. (%s)\n",
FMOD_ErrorString(FSOUND_GetError()));
return false;
}
ext_inited = false;
return inited = true;
}
//===========================================================================
// DM_FModShutdown
//===========================================================================
void DM_FModShutdown(void)
{
if(!inited)
return;
inited = false;
// Shut down the interfaces.
DM_FModExtShutdown();
DM_FModCDShutdown();
FSOUND_Close();
}
//===========================================================================
// DM_FModExtInit
//===========================================================================
int DM_FModExtInit(void)
{
if(ext_inited)
return true;
// Clear the song buffer.
song = NULL;
song_size = 0;
module = NULL;
stream = NULL;
return ext_inited = true;
}
//===========================================================================
// DM_FModExtShutdown
//===========================================================================
void DM_FModExtShutdown(void)
{
if(!ext_inited)
return;
ext_inited = false;
DM_FModExtReset();
// Free the song buffer.
if(song)
free(song);
song = NULL;
song_size = 0;
}
//===========================================================================
// DM_FModExtReset
//===========================================================================
void DM_FModExtReset(void)
{
// Get rid of old module/stream.
if(module)
FMUSIC_FreeSong(module);
if(stream)
FSOUND_Stream_Close(stream);
module = 0;
stream = 0;
stream_channel = -1;
}
//===========================================================================
// DM_FModExtUpdate
//===========================================================================
void DM_FModExtUpdate(void)
{
if(!ext_inited)
return;
/*if(module
&& ext_playing
&& ext_looped_play
&& Sys_GetTime() - ext_start_time > TICSPERSEC*3
&& !FMUSIC_IsPlaying(module))
{
FMUSIC_PlaySong(module);
} */
}
//===========================================================================
// DM_FModExtSet
//===========================================================================
void DM_FModExtSet(int property, float value)
{
if(!ext_inited)
return;
switch (property)
{
case MUSIP_VOLUME:
// This affects streams.
FSOUND_SetSFXMasterVolume(ext_volume = value * 255 + 0.5);
if(module)
FMUSIC_SetMasterVolume(module, ext_volume);
break;
}
}
//===========================================================================
// DM_FModExtGet
//===========================================================================
int DM_FModExtGet(int property, void *ptr)
{
if(!ext_inited)
return false;
switch (property)
{
case MUSIP_ID:
strcpy(ptr, "FMod/Ext");
break;
default:
return false;
}
return true;
}
//===========================================================================
// DM_FModExtPause
//===========================================================================
void DM_FModExtPause(int pause)
{
if(!ext_inited)
return;
//ext_playing = !pause;
if(module)
FMUSIC_SetPaused(module, pause != 0);
if(stream)
FSOUND_SetPaused(stream_channel, pause != 0);
}
//===========================================================================
// DM_FModExtStop
//===========================================================================
void DM_FModExtStop(void)
{
if(!ext_inited)
return;
//ext_playing = false;
if(module)
FMUSIC_StopSong(module);
if(stream)
FSOUND_Stream_Stop(stream);
}
//===========================================================================
// DM_FModExtSongBuffer
//===========================================================================
void *DM_FModExtSongBuffer(int length)
{
if(!ext_inited)
return NULL;
// Free the previous data.
if(song)
free(song);
song_size = length;
return song = malloc(song_size);
}
//===========================================================================
// DM_FModExtStartPlaying
//===========================================================================
void DM_FModExtStartPlaying(void)
{
if(module)
{
FMUSIC_SetMasterVolume(module, ext_volume);
FMUSIC_PlaySong(module);
}
if(stream)
{
stream_channel = FSOUND_Stream_Play(FSOUND_FREE, stream);
}
// ext_playing = true;
// ext_start_time = Sys_GetTime();
}
//===========================================================================
// DM_FModExtPlayFile
//===========================================================================
int DM_FModExtPlayFile(const char *path, int looped)
{
if(!ext_inited)
return false;
DM_FModExtReset();
// Try playing as a module first.
if((module = FMUSIC_LoadSong(path)))
{
FMUSIC_SetLooping(module, looped);
}
else
{
// Try as a stream.
stream =
FSOUND_Stream_Open(path, looped ? FSOUND_LOOP_NORMAL : 0, 0, 0);
if(!stream)
return false; // Failed...!
}
DM_FModExtStartPlaying();
//ext_looped_play = looped;
return true;
}
//===========================================================================
// DM_FModExtPlayBuffer
//===========================================================================
int DM_FModExtPlayBuffer(int looped)
{
if(!ext_inited)
return false;
DM_FModExtReset();
// Try playing as a module first.
if((module =
FMUSIC_LoadSongEx(song, 0, song_size, FSOUND_LOADMEMORY, NULL, 0)))
{
FMUSIC_SetLooping(module, looped);
}
else
{
// Try as a stream.
stream =
FSOUND_Stream_Open(song,
FSOUND_LOADMEMORY | (looped ? FSOUND_LOOP_NORMAL
: 0), 0, song_size);
if(!stream)
return false;
}
DM_FModExtStartPlaying();
//ext_looped_play = looped;
return true;
}
//===========================================================================
// DM_FModCDInit
//===========================================================================
int DM_FModCDInit(void)
{
original_cd_volume = FSOUND_CD_GetVolume(0);
// If FMOD is OK, then is this.
return inited;
}
//===========================================================================
// DM_FModCDShutdown
//===========================================================================
void DM_FModCDShutdown(void)
{
// Stop playing the CD track.
FSOUND_CD_Stop(0);
FSOUND_CD_SetVolume(0, original_cd_volume);
}
//===========================================================================
// DM_FModCDUpdate
//===========================================================================
void DM_FModCDUpdate(void)
{
// Nothing needs to be done.
}
//===========================================================================
// DM_FModCDSet
//===========================================================================
void DM_FModCDSet(int property, float value)
{
if(!inited)
return;
switch (property)
{
case MUSIP_VOLUME:
FSOUND_CD_SetVolume(0, value * 255 + 0.5);
break;
}
}
//===========================================================================
// DM_FmodCDGet
//===========================================================================
int DM_FModCDGet(int property, void *ptr)
{
if(!ext_inited)
return false;
switch (property)
{
case MUSIP_ID:
strcpy(ptr, "FMod/CD");
break;
default:
return false;
}
return true;
}
//===========================================================================
// DM_FModCDPause
//===========================================================================
void DM_FModCDPause(int pause)
{
if(!inited)
return;
FSOUND_CD_SetPaused(0, pause != 0);
}
//===========================================================================
// DM_FModCDStop
//===========================================================================
void DM_FModCDStop(void)
{
if(!inited)
return;
FSOUND_CD_Stop(0);
}
//===========================================================================
// DM_FModCDPlay
//===========================================================================
int DM_FModCDPlay(int track, int looped)
{
if(!inited)
return false;
FSOUND_CD_SetPlayMode(0,
looped ? FSOUND_CD_PLAYLOOPED : FSOUND_CD_PLAYONCE);
return FSOUND_CD_Play(0, track);
}