]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cd_shared.c
remove one unneeded cdPlaying check
[xonotic/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 (int 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, 4 = play real CD tracks even instead of named fake 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         char filename[MAX_QPATH];
150
151         Host_StartVideo();
152
153         if (!enabled)
154                 return;
155
156         if(strspn(trackname, "0123456789") == strlen(trackname))
157         {
158                 track = (unsigned char) atoi(trackname);
159                 if(track > 0 && track < MAXTRACKS)
160                         if(*remap[track])
161                         {
162                                 if(strspn(remap[track], "0123456789") == strlen(remap[track]))
163                                 {
164                                         trackname = remap[track];
165                                 }
166                                 else
167                                 {
168                                         // ignore remappings to fake tracks if we're going to play a real track
169                                         switch(cdaudio.integer)
170                                         {
171                                                 case 0: // we never access CD
172                                                 case 1: // we have a replacement
173                                                         trackname = remap[track];
174                                                         break;
175                                                 case 2: // we only use fake track replacement if CD track is invalid
176                                                         CDAudio_GetAudioDiskInfo();
177                                                         if(!cdValid || track > maxTrack)
178                                                                 trackname = remap[track];
179                                                         break;
180                                                 case 3: // we always play from CD - ignore this remapping then
181                                                 case 4: // we randomize anyway
182                                                         break;
183                                         }
184                                 }
185                         }
186         }
187
188         if(strspn(trackname, "0123456789") == strlen(trackname))
189         {
190                 track = (unsigned char) atoi(trackname);
191                 if (track < 1)
192                 {
193                         Con_Printf("CDAudio: Bad track number %u.\n", track);
194                         return;
195                 }
196         }
197         else
198                 track = 0;
199
200         // div0: I assume this code was intentionally there. Maybe turn it into a cvar?
201         if (cdPlaying && cdPlayTrack == track && faketrack == -1)
202                 return;
203         CDAudio_Stop ();
204
205         if(track >= 1)
206         {
207                 if(cdaudio.integer == 3) // only play real CD tracks at all
208                 {
209                         if(CDAudio_Play_real(track, looping, true))
210                                 goto success;
211                         return;
212                 }
213
214                 if(cdaudio.integer == 2) // prefer real CD track over fake
215                 {
216                         if(CDAudio_Play_real(track, looping, false))
217                                 goto success;
218                 }
219         }
220
221         if(cdaudio.integer == 4) // only play real CD tracks, EVEN instead of fake tracks!
222         {
223                 if(CDAudio_Play_real(track, looping, false))
224                         goto success;
225                 
226                 if(cdValid && maxTrack > 0)
227                 {
228                         track = 1 + (rand() % maxTrack);
229                         if(CDAudio_Play_real(track, looping, true))
230                                 goto success;
231                 }
232                 else
233                 {
234                         Con_Print ("No CD in player.\n");
235                 }
236                 return;
237         }
238
239         // Try playing a fake track (sound file) first
240         if(track >= 1)
241         {
242                                               dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%03u.wav", track);
243                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%03u.ogg", track);
244                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%02u.wav", track);
245                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%02u.ogg", track);
246         }
247         else
248         {
249                                               dpsnprintf(filename, sizeof(filename), "%s", trackname);
250                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "%s.wav", trackname);
251                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "%s.ogg", trackname);
252                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/%s", trackname);
253                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/%s.wav", trackname);
254                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/%s.ogg", trackname);
255                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/%s", trackname);
256                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/%s.wav", trackname);
257                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/%s.ogg", trackname);
258         }
259         if (FS_FileExists(filename) && (sfx = S_PrecacheSound (filename, false, true)))
260         {
261                 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
262                 if (faketrack != -1)
263                 {
264                         if (looping)
265                                 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
266                         S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
267                         S_SetChannelFlag (faketrack, CHANNELFLAG_LOCALSOUND, true); // not pausable
268                         if(track >= 1)
269                         {
270                                 if(cdaudio.integer != 0 || developer.integer) // we don't need these messages if only fake tracks can be played anyway
271                                         Con_Printf ("Fake CD track %u playing...\n", track);
272                         }
273                         else
274                                 Con_DPrintf ("BGM track %s playing...\n", trackname);
275                 }
276         }
277
278         // If we can't play a fake CD track, try the real one
279         if (faketrack == -1)
280         {
281                 if(cdaudio.integer == 0 || track < 1)
282                 {
283                         Con_Print("Could not load BGM track.\n");
284                         return;
285                 }
286                 else
287                 {
288                         if(!CDAudio_Play_real(track, looping, true))
289                                 return;
290                 }
291         }
292
293 success:
294         cdPlayLooping = looping;
295         cdPlayTrack = track;
296         cdPlaying = true;
297
298         if (cdvolume == 0.0 || bgmvolume.value == 0)
299                 CDAudio_Pause ();
300 }
301
302 void CDAudio_Play (int track, qboolean looping)
303 {
304         char buf[20];
305         dpsnprintf(buf, sizeof(buf), "%d", (int) track);
306         CDAudio_Play_byName(buf, looping);
307 }
308
309 float CDAudio_GetPosition ()
310 {
311         if(faketrack != -1)
312                 return S_GetChannelPosition(faketrack);
313         return -1;
314 }
315
316 void CDAudio_Stop (void)
317 {
318         if (!enabled)
319                 return;
320
321         if (faketrack != -1)
322         {
323                 S_StopChannel (faketrack, true);
324                 faketrack = -1;
325         }
326         else if (cdPlaying && (CDAudio_SysStop() == -1))
327                 return;
328         else if(wasPlaying)
329         {
330                 CDAudio_Resume(); // needed by SDL - can't stop while paused there
331                 if (cdPlaying && (CDAudio_SysStop() == -1))
332                         return;
333         }
334
335         wasPlaying = false;
336         cdPlaying = false;
337 }
338
339 void CDAudio_Pause (void)
340 {
341         if (!enabled || !cdPlaying)
342                 return;
343
344         if (faketrack != -1)
345                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
346         else if (CDAudio_SysPause() == -1)
347                 return;
348
349         wasPlaying = cdPlaying;
350         cdPlaying = false;
351 }
352
353
354 void CDAudio_Resume (void)
355 {
356         if (!enabled || cdPlaying || !wasPlaying)
357                 return;
358
359         if (faketrack != -1)
360                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
361         else if (CDAudio_SysResume() == -1)
362                 return;
363         cdPlaying = true;
364 }
365
366 static void CD_f (void)
367 {
368         const char *command;
369         int ret;
370         int n;
371
372         command = (Cmd_Argc() >= 2) ? Cmd_Argv (1) : "";
373
374         if (strcasecmp(command, "remap") != 0)
375                 Host_StartVideo();
376
377         if (strcasecmp(command, "on") == 0)
378         {
379                 enabled = true;
380                 return;
381         }
382
383         if (strcasecmp(command, "off") == 0)
384         {
385                 CDAudio_Stop();
386                 enabled = false;
387                 return;
388         }
389
390         if (strcasecmp(command, "reset") == 0)
391         {
392                 enabled = true;
393                 CDAudio_Stop();
394                 for (n = 0; n < MAXTRACKS; n++)
395                         *remap[n] = 0; // empty string, that is, unremapped
396                 CDAudio_GetAudioDiskInfo();
397                 return;
398         }
399
400         if (strcasecmp(command, "rescan") == 0)
401         {
402                 CDAudio_Stop();
403                 CDAudio_Shutdown();
404                 CDAudio_Startup();
405                 return;
406         }
407
408         if (strcasecmp(command, "remap") == 0)
409         {
410                 ret = Cmd_Argc() - 2;
411                 if (ret <= 0)
412                 {
413                         for (n = 1; n < MAXTRACKS; n++)
414                                 if (*remap[n])
415                                         Con_Printf("  %u -> %s\n", n, remap[n]);
416                         return;
417                 }
418                 for (n = 1; n <= ret; n++)
419                         strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
420                 return;
421         }
422
423         if (strcasecmp(command, "close") == 0)
424         {
425                 CDAudio_CloseDoor();
426                 return;
427         }
428
429         if (strcasecmp(command, "play") == 0)
430         {
431                 CDAudio_Play_byName(Cmd_Argv (2), false);
432                 return;
433         }
434
435         if (strcasecmp(command, "loop") == 0)
436         {
437                 CDAudio_Play_byName(Cmd_Argv (2), true);
438                 return;
439         }
440
441         if (strcasecmp(command, "stop") == 0)
442         {
443                 CDAudio_Stop();
444                 return;
445         }
446
447         if (strcasecmp(command, "pause") == 0)
448         {
449                 CDAudio_Pause();
450                 return;
451         }
452
453         if (strcasecmp(command, "resume") == 0)
454         {
455                 CDAudio_Resume();
456                 return;
457         }
458
459         if (strcasecmp(command, "eject") == 0)
460         {
461                 if (cdPlaying && faketrack == -1)
462                         CDAudio_Stop();
463                 CDAudio_Eject();
464                 cdValid = false;
465                 return;
466         }
467
468         if (strcasecmp(command, "info") == 0)
469         {
470                 CDAudio_GetAudioDiskInfo ();
471                 if (cdValid)
472                         Con_Printf("%u tracks on CD.\n", maxTrack);
473                 else
474                         Con_Print ("No CD in player.\n");
475                 if (cdPlaying)
476                         Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
477                 else if (wasPlaying)
478                         Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
479                 if (cdvolume >= 0)
480                         Con_Printf("Volume is %f\n", cdvolume);
481                 else
482                         Con_Printf("Can't get CD volume\n");
483                 return;
484         }
485
486         Con_Printf("CD commands:\n");
487         Con_Printf("cd on - enables CD audio system\n");
488         Con_Printf("cd off - stops and disables CD audio system\n");
489         Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)");
490         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");
491         Con_Printf("cd close - closes CD tray\n");
492         Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
493         Con_Printf("cd play <tracknumber> - plays selected track in remapping table\n");
494         Con_Printf("cd loop <tracknumber> - plays and repeats selected track in remapping table\n");
495         Con_Printf("cd stop - stops playing current CD track\n");
496         Con_Printf("cd pause - pauses CD playback\n");
497         Con_Printf("cd resume - unpauses CD playback\n");
498         Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
499 }
500
501 void CDAudio_SetVolume (float newvol)
502 {
503         // If the volume hasn't changed
504         if (newvol == cdvolume)
505                 return;
506
507         // If the CD has been muted
508         if (newvol == 0.0f)
509                 CDAudio_Pause ();
510         else
511         {
512                 // If the CD has been unmuted
513                 if (cdvolume == 0.0f)
514                         CDAudio_Resume ();
515
516                 if (faketrack != -1)
517                         S_SetChannelVolume (faketrack, newvol);
518                 else
519                         CDAudio_SysSetVolume (newvol);
520         }
521
522         cdvolume = newvol;
523 }
524
525 void CDAudio_Update (void)
526 {
527         if (!enabled)
528                 return;
529
530         CDAudio_SetVolume (bgmvolume.value);
531         
532         if (faketrack == -1 && cdaudio.integer != 0 && bgmvolume.value != 0)
533                 CDAudio_SysUpdate();
534 }
535
536 int CDAudio_Init (void)
537 {
538         int i;
539
540         if (cls.state == ca_dedicated)
541                 return -1;
542
543 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
544         if (COM_CheckParm("-nocdaudio"))
545                 return -1;
546
547         CDAudio_SysInit();
548
549         for (i = 0; i < MAXTRACKS; i++)
550                 *remap[i] = 0;
551
552         Cvar_RegisterVariable(&cdaudio);
553         Cvar_RegisterVariable(&cdaudioinitialized);
554         Cvar_SetValueQuick(&cdaudioinitialized, true);
555         enabled = true;
556
557         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");
558
559         return 0;
560 }
561
562 int CDAudio_Startup (void)
563 {
564         if (COM_CheckParm("-nocdaudio"))
565                 return -1;
566
567         CDAudio_SysStartup ();
568
569         if (CDAudio_GetAudioDiskInfo())
570         {
571                 Con_Print("CDAudio_Init: No CD in player.\n");
572                 cdValid = false;
573         }
574
575         saved_vol = CDAudio_SysGetVolume ();
576         if (saved_vol < 0.0f)
577         {
578                 Con_Print ("Can't get initial CD volume\n");
579                 saved_vol = 1.0f;
580         }
581         else
582                 Con_Printf ("Initial CD volume: %g\n", saved_vol);
583
584         initialized = true;
585
586         Con_Print("CD Audio Initialized\n");
587
588         return 0;
589 }
590
591 void CDAudio_Shutdown (void)
592 {
593         if (!initialized)
594                 return;
595
596         CDAudio_SysSetVolume (saved_vol);
597
598         CDAudio_Stop();
599         CDAudio_SysShutdown();
600         initialized = false;
601 }