]> icculus.org git repositories - divverent/darkplaces.git/blob - cd_shared.c
Add support for libmodplug (when found). Sound files with an extension are now all...
[divverent/darkplaces.git] / cd_shared.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // Quake is a trademark of Id Software, Inc., (c) 1996 Id Software, Inc. All
21 // rights reserved.
22
23 #include "quakedef.h"
24 #include "cdaudio.h"
25 #include "sound.h"
26
27 #define MAXTRACKS       256
28
29 // Prototypes of the system dependent functions
30 extern void CDAudio_SysEject (void);
31 extern void CDAudio_SysCloseDoor (void);
32 extern int CDAudio_SysGetAudioDiskInfo (void);
33 extern float CDAudio_SysGetVolume (void);
34 extern void CDAudio_SysSetVolume (float volume);
35 extern int CDAudio_SysPlay (unsigned char track);
36 extern int CDAudio_SysStop (void);
37 extern int CDAudio_SysPause (void);
38 extern int CDAudio_SysResume (void);
39 extern int CDAudio_SysUpdate (void);
40 extern void CDAudio_SysInit (void);
41 extern int CDAudio_SysStartup (void);
42 extern void CDAudio_SysShutdown (void);
43
44 // used by menu to ghost CD audio slider
45 cvar_t cdaudioinitialized = {CVAR_READONLY,"cdaudioinitialized","0","indicates if CD Audio system is active"};
46
47 static qboolean wasPlaying = false;
48 static qboolean initialized = false;
49 static qboolean enabled = false;
50 static float cdvolume;
51 static unsigned char remap[MAXTRACKS];
52 static unsigned char maxTrack;
53 static int faketrack = -1;
54
55 static float saved_vol = 1.0f;
56
57 // exported variables
58 qboolean cdValid = false;
59 qboolean cdPlaying = false;
60 qboolean cdPlayLooping = false;
61 unsigned char cdPlayTrack;
62
63 cl_cdstate_t cd;
64
65 static void CDAudio_Eject (void)
66 {
67         if (!enabled)
68                 return;
69
70         CDAudio_SysEject();
71 }
72
73
74 static void CDAudio_CloseDoor (void)
75 {
76         if (!enabled)
77                 return;
78
79         CDAudio_SysCloseDoor();
80 }
81
82 static int CDAudio_GetAudioDiskInfo (void)
83 {
84         int ret;
85
86         cdValid = false;
87
88         ret = CDAudio_SysGetAudioDiskInfo();
89         if (ret < 1)
90                 return -1;
91
92         cdValid = true;
93         maxTrack = ret;
94
95         return 0;
96 }
97
98
99 void CDAudio_Play_byName (const char *trackname, qboolean looping)
100 {
101         unsigned char track;
102         sfx_t* sfx;
103
104         Host_StartVideo();
105
106         if (!enabled)
107                 return;
108
109         if(strspn(trackname, "0123456789") == strlen(trackname))
110         {
111                 track = (unsigned char) atoi(trackname);
112                 track = remap[track];
113                 if (track < 1)
114                 {
115                         Con_Printf("CDAudio: Bad track number %u.\n", track);
116                         return;
117                 }
118         }
119         else
120                 track = 0;
121
122         if (cdPlaying && cdPlayTrack == track && faketrack == -1)
123                 return;
124         CDAudio_Stop ();
125
126         // Try playing a fake track (sound file) first
127         if(track >= 1)
128         {
129                 sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, false);
130                 if (sfx == NULL || !S_IsSoundPrecached (sfx))
131                         sfx = S_PrecacheSound (va ("cdtracks/track%03u.wav", track), false, false);
132         }
133         else
134         {
135                 sfx = S_PrecacheSound (va("cdtracks/%s.wav", trackname), false, false);
136                 if (sfx == NULL || !S_IsSoundPrecached (sfx))
137                         sfx = S_PrecacheSound (va("cdtracks/%s", trackname), false, false);
138         }
139         if (sfx != NULL)
140         {
141                 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
142                 if (faketrack != -1)
143                 {
144                         if (looping)
145                                 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
146                         S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
147                         if(track >= 1)
148                                 Con_DPrintf ("Fake CD track %u playing...\n", track);
149                         else
150                                 Con_DPrintf ("BGM track %s playing...\n", trackname);
151                 }
152         }
153
154         // If we can't play a fake CD track, try the real one
155         if (faketrack == -1)
156         {
157                 if(track < 1)
158                 {
159                         Con_Print("Could not load BGM track.\n");
160                         return;
161                 }
162         
163                 if (!cdValid)
164                 {
165                         CDAudio_GetAudioDiskInfo();
166                         if (!cdValid)
167                         {
168                                 Con_Print ("No CD in player.\n");
169                                 return;
170                         }
171                 }
172
173                 if (track > maxTrack)
174                 {
175                         Con_Printf("CDAudio: Bad track number %u.\n", track);
176                         return;
177                 }
178
179                 if (CDAudio_SysPlay(track) == -1)
180                         return;
181         }
182
183         cdPlayLooping = looping;
184         cdPlayTrack = track;
185         cdPlaying = true;
186
187         if (cdvolume == 0.0)
188                 CDAudio_Pause ();
189 }
190
191 void CDAudio_Play (unsigned char track, qboolean looping)
192 {
193         char buf[20];
194         dpsnprintf(buf, sizeof(buf), "%d", (int) track);
195         CDAudio_Play_byName(buf, looping);
196 }
197
198 void CDAudio_Stop (void)
199 {
200         if (!enabled || !cdPlaying)
201                 return;
202
203         if (faketrack != -1)
204         {
205                 S_StopChannel (faketrack);
206                 faketrack = -1;
207         }
208         else if (CDAudio_SysStop() == -1)
209                 return;
210
211         wasPlaying = false;
212         cdPlaying = false;
213 }
214
215 void CDAudio_Pause (void)
216 {
217         if (!enabled || !cdPlaying)
218                 return;
219
220         if (faketrack != -1)
221                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
222         else if (CDAudio_SysPause() == -1)
223                 return;
224
225         wasPlaying = cdPlaying;
226         cdPlaying = false;
227 }
228
229
230 void CDAudio_Resume (void)
231 {
232         if (!enabled || cdPlaying || !wasPlaying)
233                 return;
234
235         if (faketrack != -1)
236                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
237         else if (CDAudio_SysResume() == -1)
238                 return;
239         cdPlaying = true;
240 }
241
242 static void CD_f (void)
243 {
244         const char *command;
245         int ret;
246         int n;
247
248         Host_StartVideo();
249
250         if (Cmd_Argc() < 2)
251                 return;
252
253         command = Cmd_Argv (1);
254
255         if (strcasecmp(command, "on") == 0)
256         {
257                 enabled = true;
258                 return;
259         }
260
261         if (strcasecmp(command, "off") == 0)
262         {
263                 if (cdPlaying)
264                         CDAudio_Stop();
265                 enabled = false;
266                 return;
267         }
268
269         if (strcasecmp(command, "reset") == 0)
270         {
271                 enabled = true;
272                 if (cdPlaying)
273                         CDAudio_Stop();
274                 for (n = 0; n < MAXTRACKS; n++)
275                         remap[n] = n;
276                 CDAudio_GetAudioDiskInfo();
277                 return;
278         }
279
280         if (strcasecmp(command, "remap") == 0)
281         {
282                 ret = Cmd_Argc() - 2;
283                 if (ret <= 0)
284                 {
285                         for (n = 1; n < MAXTRACKS; n++)
286                                 if (remap[n] != n)
287                                         Con_Printf("  %u -> %u\n", n, remap[n]);
288                         return;
289                 }
290                 for (n = 1; n <= ret; n++)
291                         remap[n] = atoi(Cmd_Argv (n+1));
292                 return;
293         }
294
295         if (strcasecmp(command, "close") == 0)
296         {
297                 CDAudio_CloseDoor();
298                 return;
299         }
300
301         if (strcasecmp(command, "play") == 0)
302         {
303                 CDAudio_Play_byName(Cmd_Argv (2), false);
304                 return;
305         }
306
307         if (strcasecmp(command, "loop") == 0)
308         {
309                 CDAudio_Play_byName(Cmd_Argv (2), true);
310                 return;
311         }
312
313         if (strcasecmp(command, "stop") == 0)
314         {
315                 CDAudio_Stop();
316                 return;
317         }
318
319         if (strcasecmp(command, "pause") == 0)
320         {
321                 CDAudio_Pause();
322                 return;
323         }
324
325         if (strcasecmp(command, "resume") == 0)
326         {
327                 CDAudio_Resume();
328                 return;
329         }
330
331         if (strcasecmp(command, "eject") == 0)
332         {
333                 if (cdPlaying && faketrack == -1)
334                         CDAudio_Stop();
335                 CDAudio_Eject();
336                 cdValid = false;
337                 return;
338         }
339
340         if (strcasecmp(command, "info") == 0)
341         {
342                 CDAudio_GetAudioDiskInfo ();
343                 if (cdValid)
344                         Con_Printf("%u tracks on CD.\n", maxTrack);
345                 else
346                         Con_Print ("No CD in player.\n");
347                 if (cdPlaying)
348                         Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
349                 else if (wasPlaying)
350                         Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
351                 Con_Printf("Volume is %f\n", cdvolume);
352                 return;
353         }
354
355         Con_Printf("CD commands:\n");
356         Con_Printf("cd on - enables CD audio system\n");
357         Con_Printf("cd off - stops and disables CD audio system\n");
358         Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)");
359         Con_Printf("cd remap <remap1> [remap2] [remap3] [...] - chooses (possibly emulated) CD tracks to play when a map asks for a particular track, this has many uses\n");
360         Con_Printf("cd close - closes CD tray\n");
361         Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
362         Con_Printf("cd play <tracknumber> - plays selected track in remapping table\n");
363         Con_Printf("cd loop <tracknumber> - plays and repeats selected track in remapping table\n");
364         Con_Printf("cd stop - stops playing current CD track\n");
365         Con_Printf("cd pause - pauses CD playback\n");
366         Con_Printf("cd resume - unpauses CD playback\n");
367         Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
368 }
369
370 void CDAudio_SetVolume (float newvol)
371 {
372         // If the volume hasn't changed
373         if (newvol == cdvolume)
374                 return;
375
376         // If the CD has been muted
377         if (newvol == 0.0f)
378                 CDAudio_Pause ();
379         else
380         {
381                 // If the CD has been unmuted
382                 if (cdvolume == 0.0f)
383                         CDAudio_Resume ();
384
385                 if (faketrack != -1)
386                         S_SetChannelVolume (faketrack, newvol);
387                 CDAudio_SysSetVolume (newvol);
388         }
389
390         cdvolume = newvol;
391 }
392
393 void CDAudio_Update (void)
394 {
395         if (!enabled)
396                 return;
397
398         CDAudio_SetVolume (bgmvolume.value);
399
400         if (faketrack == -1)
401                 CDAudio_SysUpdate();
402 }
403
404 int CDAudio_Init (void)
405 {
406         int i;
407
408         if (cls.state == ca_dedicated)
409                 return -1;
410
411 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
412         if (COM_CheckParm("-nocdaudio"))
413                 return -1;
414
415         CDAudio_SysInit();
416
417         for (i = 0; i < MAXTRACKS; i++)
418                 remap[i] = i;
419
420         Cvar_RegisterVariable(&cdaudioinitialized);
421         Cvar_SetValueQuick(&cdaudioinitialized, true);
422         enabled = true;
423
424         Cmd_AddCommand("cd", CD_f, "execute a CD drive command (cd on/off/reset/remap/close/play/loop/stop/pause/resume/eject/info) - use cd by itself for usage");
425
426         return 0;
427 }
428
429 int CDAudio_Startup (void)
430 {
431         if (COM_CheckParm("-nocdaudio"))
432                 return -1;
433
434         CDAudio_SysStartup ();
435
436         if (CDAudio_GetAudioDiskInfo())
437         {
438                 Con_Print("CDAudio_Init: No CD in player.\n");
439                 cdValid = false;
440         }
441
442         saved_vol = CDAudio_SysGetVolume ();
443         if (saved_vol < 0.0f)
444         {
445                 Con_Print ("Can't get initial CD volume\n");
446                 saved_vol = 1.0f;
447         }
448         else
449                 Con_Printf ("Initial CD volume: %g\n", saved_vol);
450
451         initialized = true;
452
453         Con_Print("CD Audio Initialized\n");
454
455         return 0;
456 }
457
458 void CDAudio_Shutdown (void)
459 {
460         if (!initialized)
461                 return;
462
463         CDAudio_SysSetVolume (saved_vol);
464
465         CDAudio_Stop();
466         CDAudio_SysShutdown();
467         initialized = false;
468 }