]> icculus.org git repositories - divverent/darkplaces.git/blob - cd_shared.c
add new cvar "cdaudio" to select priorities of CD and fake tracks;
[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 cvar_t cdaudio = {CVAR_SAVE,"cdaudio","1","CD playing mode (0 = never access CD drive, 1 = play CD tracks if no replacement available, 2 = play fake tracks if no CD track available, 3 = play only real CD tracks)"};
47
48 static qboolean wasPlaying = false;
49 static qboolean initialized = false;
50 static qboolean enabled = false;
51 static float cdvolume;
52 typedef char filename_t[MAX_QPATH];
53 static filename_t remap[MAXTRACKS];
54 static unsigned char maxTrack;
55 static int faketrack = -1;
56
57 static float saved_vol = 1.0f;
58
59 // exported variables
60 qboolean cdValid = false;
61 qboolean cdPlaying = false;
62 qboolean cdPlayLooping = false;
63 unsigned char cdPlayTrack;
64
65 cl_cdstate_t cd;
66
67 static void CDAudio_Eject (void)
68 {
69         if (!enabled)
70                 return;
71         
72         if(cdaudio.integer == 0)
73                 return;
74
75         CDAudio_SysEject();
76 }
77
78
79 static void CDAudio_CloseDoor (void)
80 {
81         if (!enabled)
82                 return;
83
84         if(cdaudio.integer == 0)
85                 return;
86
87         CDAudio_SysCloseDoor();
88 }
89
90 static int CDAudio_GetAudioDiskInfo (void)
91 {
92         int ret;
93
94         cdValid = false;
95
96         if(cdaudio.integer == 0)
97                 return -1;
98
99         ret = CDAudio_SysGetAudioDiskInfo();
100         if (ret < 1)
101                 return -1;
102
103         cdValid = true;
104         maxTrack = ret;
105
106         return 0;
107 }
108
109 qboolean CDAudio_Play_real (int track, qboolean looping, qboolean complain)
110 {
111         if(track < 1)
112         {
113                 if(complain)
114                         Con_Print("Could not load BGM track.\n");
115                 return false;
116         }
117
118         if (!cdValid)
119         {
120                 CDAudio_GetAudioDiskInfo();
121                 if (!cdValid)
122                 {
123                         if(complain)
124                                 Con_Print ("No CD in player.\n");
125                         return false;
126                 }
127         }
128
129         if (track > maxTrack)
130         {
131                 if(complain)
132                         Con_Printf("CDAudio: Bad track number %u.\n", track);
133                 return false;
134         }
135
136         if (CDAudio_SysPlay(track) == -1)
137                 return false;
138
139         if(cdaudio.integer != 3 || developer.integer)
140                 Con_Printf ("CD track %u playing...\n", track);
141
142         return true;
143 }
144
145 void CDAudio_Play_byName (const char *trackname, qboolean looping)
146 {
147         unsigned int track;
148         sfx_t* sfx;
149
150         Host_StartVideo();
151
152         if (!enabled)
153                 return;
154
155         if(strspn(trackname, "0123456789") == strlen(trackname))
156         {
157                 track = (unsigned char) atoi(trackname);
158                 if(track > 0 && track < MAXTRACKS)
159                                 if(*remap[track])
160                                         trackname = remap[track];
161         }
162
163         if(strspn(trackname, "0123456789") == strlen(trackname))
164         {
165                 track = (unsigned char) atoi(trackname);
166                 if (track < 1)
167                 {
168                         Con_Printf("CDAudio: Bad track number %u.\n", track);
169                         return;
170                 }
171         }
172         else
173                 track = 0;
174
175         if (cdPlaying && cdPlayTrack == track && faketrack == -1)
176                 return;
177         CDAudio_Stop ();
178
179         if(track >= 1)
180         {
181                 if(cdaudio.integer == 3) // only play real CD tracks at all
182                 {
183                         if(CDAudio_Play_real(track, looping, true))
184                                 goto success;
185                         return;
186                 }
187
188                 if(cdaudio.integer == 2) // prefer real CD track over fake
189                 {
190                                 if(CDAudio_Play_real(track, looping, false))
191                                         goto success;
192                 }
193         }
194
195         // Try playing a fake track (sound file) first
196         if(track >= 1)
197         {
198                 sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, false);
199                 if (sfx == NULL || !S_IsSoundPrecached (sfx))
200                         sfx = S_PrecacheSound (va ("cdtracks/track%03u.wav", track), false, false);
201                 if (sfx == NULL || !S_IsSoundPrecached (sfx))
202                         sfx = S_PrecacheSound (va ("cdtracks/track%02u", track), false, false);
203                 if (sfx == NULL || !S_IsSoundPrecached (sfx))
204                         sfx = S_PrecacheSound (va ("cdtracks/track%03u", track), false, false);
205         }
206         else
207         {
208                 sfx = S_PrecacheSound (va("cdtracks/%s.wav", trackname), false, false);
209                 if (sfx == NULL || !S_IsSoundPrecached (sfx))
210                         sfx = S_PrecacheSound (va("cdtracks/%s", trackname), false, false);
211         }
212         if (sfx != NULL)
213         {
214                 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
215                 if (faketrack != -1)
216                 {
217                         if (looping)
218                                 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
219                         S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
220                         if(track >= 1)
221                         {
222                                 if(cdaudio.integer != 0 || developer.integer) // we don't need these messages if only fake tracks can be played anyway
223                                         Con_Printf ("Fake CD track %u playing...\n", track);
224                         }
225                         else
226                                 Con_DPrintf ("BGM track %s playing...\n", trackname);
227                 }
228         }
229
230         // If we can't play a fake CD track, try the real one
231         if (faketrack == -1)
232         {
233                 if(cdaudio.integer == 0 || track < 1)
234                 {
235                         Con_Print("Could not load BGM track.\n");
236                         return;
237                 }
238                 else
239                 {
240                         if(!CDAudio_Play_real(track, looping, true))
241                                 return;
242                 }
243         }
244
245 success:
246         cdPlayLooping = looping;
247         cdPlayTrack = track;
248         cdPlaying = true;
249
250         if (cdvolume == 0.0)
251                 CDAudio_Pause ();
252 }
253
254 void CDAudio_Play (unsigned char track, qboolean looping)
255 {
256         char buf[20];
257         dpsnprintf(buf, sizeof(buf), "%d", (int) track);
258         CDAudio_Play_byName(buf, looping);
259 }
260
261 void CDAudio_Stop (void)
262 {
263         if (!enabled)
264                 return;
265
266         if (faketrack != -1)
267         {
268                 S_StopChannel (faketrack, true);
269                 faketrack = -1;
270         }
271         else if (cdPlaying && (CDAudio_SysStop() == -1))
272                 return;
273
274         wasPlaying = false;
275         cdPlaying = false;
276 }
277
278 void CDAudio_Pause (void)
279 {
280         if (!enabled || !cdPlaying)
281                 return;
282
283         if (faketrack != -1)
284                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
285         else if (CDAudio_SysPause() == -1)
286                 return;
287
288         wasPlaying = cdPlaying;
289         cdPlaying = false;
290 }
291
292
293 void CDAudio_Resume (void)
294 {
295         if (!enabled || cdPlaying || !wasPlaying)
296                 return;
297
298         if (faketrack != -1)
299                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
300         else if (CDAudio_SysResume() == -1)
301                 return;
302         cdPlaying = true;
303 }
304
305 static void CD_f (void)
306 {
307         const char *command;
308         int ret;
309         int n;
310
311         if (Cmd_Argc() < 2)
312                 return;
313
314         command = Cmd_Argv (1);
315
316         if (strcasecmp(command, "remap") != 0)
317                 Host_StartVideo();
318
319         if (strcasecmp(command, "on") == 0)
320         {
321                 enabled = true;
322                 return;
323         }
324
325         if (strcasecmp(command, "off") == 0)
326         {
327                 if (cdPlaying)
328                         CDAudio_Stop();
329                 enabled = false;
330                 return;
331         }
332
333         if (strcasecmp(command, "reset") == 0)
334         {
335                 enabled = true;
336                 if (cdPlaying)
337                         CDAudio_Stop();
338                 for (n = 0; n < MAXTRACKS; n++)
339                         *remap[n] = 0; // empty string, that is, unremapped
340                 CDAudio_GetAudioDiskInfo();
341                 return;
342         }
343
344         if (strcasecmp(command, "remap") == 0)
345         {
346                 ret = Cmd_Argc() - 2;
347                 if (ret <= 0)
348                 {
349                         for (n = 1; n < MAXTRACKS; n++)
350                                 if (*remap[n])
351                                         Con_Printf("  %u -> %s\n", n, remap[n]);
352                         return;
353                 }
354                 for (n = 1; n <= ret; n++)
355                         strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
356                 return;
357         }
358
359         if (strcasecmp(command, "close") == 0)
360         {
361                 CDAudio_CloseDoor();
362                 return;
363         }
364
365         if (strcasecmp(command, "play") == 0)
366         {
367                 CDAudio_Play_byName(Cmd_Argv (2), false);
368                 return;
369         }
370
371         if (strcasecmp(command, "loop") == 0)
372         {
373                 CDAudio_Play_byName(Cmd_Argv (2), true);
374                 return;
375         }
376
377         if (strcasecmp(command, "stop") == 0)
378         {
379                 CDAudio_Stop();
380                 return;
381         }
382
383         if (strcasecmp(command, "pause") == 0)
384         {
385                 CDAudio_Pause();
386                 return;
387         }
388
389         if (strcasecmp(command, "resume") == 0)
390         {
391                 CDAudio_Resume();
392                 return;
393         }
394
395         if (strcasecmp(command, "eject") == 0)
396         {
397                 if (cdPlaying && faketrack == -1)
398                         CDAudio_Stop();
399                 CDAudio_Eject();
400                 cdValid = false;
401                 return;
402         }
403
404         if (strcasecmp(command, "info") == 0)
405         {
406                 CDAudio_GetAudioDiskInfo ();
407                 if (cdValid)
408                         Con_Printf("%u tracks on CD.\n", maxTrack);
409                 else
410                         Con_Print ("No CD in player.\n");
411                 if (cdPlaying)
412                         Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
413                 else if (wasPlaying)
414                         Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
415                 Con_Printf("Volume is %f\n", cdvolume);
416                 return;
417         }
418
419         Con_Printf("CD commands:\n");
420         Con_Printf("cd on - enables CD audio system\n");
421         Con_Printf("cd off - stops and disables CD audio system\n");
422         Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)");
423         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");
424         Con_Printf("cd close - closes CD tray\n");
425         Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
426         Con_Printf("cd play <tracknumber> - plays selected track in remapping table\n");
427         Con_Printf("cd loop <tracknumber> - plays and repeats selected track in remapping table\n");
428         Con_Printf("cd stop - stops playing current CD track\n");
429         Con_Printf("cd pause - pauses CD playback\n");
430         Con_Printf("cd resume - unpauses CD playback\n");
431         Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
432 }
433
434 void CDAudio_SetVolume (float newvol)
435 {
436         // If the volume hasn't changed
437         if (newvol == cdvolume)
438                 return;
439
440         // If the CD has been muted
441         if (newvol == 0.0f)
442                 CDAudio_Pause ();
443         else
444         {
445                 // If the CD has been unmuted
446                 if (cdvolume == 0.0f)
447                         CDAudio_Resume ();
448
449                 if (faketrack != -1)
450                         S_SetChannelVolume (faketrack, newvol);
451                 else
452                         CDAudio_SysSetVolume (newvol);
453         }
454
455         cdvolume = newvol;
456 }
457
458 void CDAudio_Update (void)
459 {
460         if (!enabled)
461                 return;
462
463         CDAudio_SetVolume (bgmvolume.value);
464
465         if (faketrack == -1 && cdaudio.integer != 0)
466                 CDAudio_SysUpdate();
467 }
468
469 int CDAudio_Init (void)
470 {
471         int i;
472
473         if (cls.state == ca_dedicated)
474                 return -1;
475
476 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
477         if (COM_CheckParm("-nocdaudio"))
478                 return -1;
479
480         CDAudio_SysInit();
481
482         for (i = 0; i < MAXTRACKS; i++)
483                 *remap[i] = 0;
484
485         Cvar_RegisterVariable(&cdaudio);
486         Cvar_RegisterVariable(&cdaudioinitialized);
487         Cvar_SetValueQuick(&cdaudioinitialized, true);
488         enabled = true;
489
490         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");
491
492         return 0;
493 }
494
495 int CDAudio_Startup (void)
496 {
497         if (COM_CheckParm("-nocdaudio"))
498                 return -1;
499
500         CDAudio_SysStartup ();
501
502         if (CDAudio_GetAudioDiskInfo())
503         {
504                 Con_Print("CDAudio_Init: No CD in player.\n");
505                 cdValid = false;
506         }
507
508         saved_vol = CDAudio_SysGetVolume ();
509         if (saved_vol < 0.0f)
510         {
511                 Con_Print ("Can't get initial CD volume\n");
512                 saved_vol = 1.0f;
513         }
514         else
515                 Con_Printf ("Initial CD volume: %g\n", saved_vol);
516
517         initialized = true;
518
519         Con_Print("CD Audio Initialized\n");
520
521         return 0;
522 }
523
524 void CDAudio_Shutdown (void)
525 {
526         if (!initialized)
527                 return;
528
529         CDAudio_SysSetVolume (saved_vol);
530
531         CDAudio_Stop();
532         CDAudio_SysShutdown();
533         initialized = false;
534 }