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