]> icculus.org git repositories - divverent/darkplaces.git/blob - cd_shared.c
don't shift the shadowmapping focus based on view angle when r_shadows_focus is used
[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 // 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 #define MAX_PLAYLISTS 10
47 int music_playlist_active = -1;
48 int music_playlist_playing = 0; // 0 = not playing, 1 = playing, -1 = tried and failed
49
50 cvar_t music_playlist_index = {0, "music_playlist_index", "-1", "selects which of the music_playlist_ variables is the active one, -1 disables playlists"};
51 cvar_t music_playlist_list[MAX_PLAYLISTS] =
52 {
53         {0, "music_playlist_list0", "", "list of tracks to play"},
54         {0, "music_playlist_list1", "", "list of tracks to play"},
55         {0, "music_playlist_list2", "", "list of tracks to play"},
56         {0, "music_playlist_list3", "", "list of tracks to play"},
57         {0, "music_playlist_list4", "", "list of tracks to play"},
58         {0, "music_playlist_list5", "", "list of tracks to play"},
59         {0, "music_playlist_list6", "", "list of tracks to play"},
60         {0, "music_playlist_list7", "", "list of tracks to play"},
61         {0, "music_playlist_list8", "", "list of tracks to play"},
62         {0, "music_playlist_list9", "", "list of tracks to play"}
63 };
64 cvar_t music_playlist_current[MAX_PLAYLISTS] =
65 {
66         {0, "music_playlist_current0", "0", "current track index to play in list"},
67         {0, "music_playlist_current1", "0", "current track index to play in list"},
68         {0, "music_playlist_current2", "0", "current track index to play in list"},
69         {0, "music_playlist_current3", "0", "current track index to play in list"},
70         {0, "music_playlist_current4", "0", "current track index to play in list"},
71         {0, "music_playlist_current5", "0", "current track index to play in list"},
72         {0, "music_playlist_current6", "0", "current track index to play in list"},
73         {0, "music_playlist_current7", "0", "current track index to play in list"},
74         {0, "music_playlist_current8", "0", "current track index to play in list"},
75         {0, "music_playlist_current9", "0", "current track index to play in list"},
76 };
77 cvar_t music_playlist_random[MAX_PLAYLISTS] =
78 {
79         {0, "music_playlist_random0", "0", "enables random play order if 1, 0 is sequential play"},
80         {0, "music_playlist_random1", "0", "enables random play order if 1, 0 is sequential play"},
81         {0, "music_playlist_random2", "0", "enables random play order if 1, 0 is sequential play"},
82         {0, "music_playlist_random3", "0", "enables random play order if 1, 0 is sequential play"},
83         {0, "music_playlist_random4", "0", "enables random play order if 1, 0 is sequential play"},
84         {0, "music_playlist_random5", "0", "enables random play order if 1, 0 is sequential play"},
85         {0, "music_playlist_random6", "0", "enables random play order if 1, 0 is sequential play"},
86         {0, "music_playlist_random7", "0", "enables random play order if 1, 0 is sequential play"},
87         {0, "music_playlist_random8", "0", "enables random play order if 1, 0 is sequential play"},
88         {0, "music_playlist_random9", "0", "enables random play order if 1, 0 is sequential play"},
89 };
90 cvar_t music_playlist_sampleposition[MAX_PLAYLISTS] =
91 {
92         {0, "music_playlist_sampleposition0", "-1", "resume position for track, -1 restarts every time"},
93         {0, "music_playlist_sampleposition1", "-1", "resume position for track, -1 restarts every time"},
94         {0, "music_playlist_sampleposition2", "-1", "resume position for track, -1 restarts every time"},
95         {0, "music_playlist_sampleposition3", "-1", "resume position for track, -1 restarts every time"},
96         {0, "music_playlist_sampleposition4", "-1", "resume position for track, -1 restarts every time"},
97         {0, "music_playlist_sampleposition5", "-1", "resume position for track, -1 restarts every time"},
98         {0, "music_playlist_sampleposition6", "-1", "resume position for track, -1 restarts every time"},
99         {0, "music_playlist_sampleposition7", "-1", "resume position for track, -1 restarts every time"},
100         {0, "music_playlist_sampleposition8", "-1", "resume position for track, -1 restarts every time"},
101         {0, "music_playlist_sampleposition9", "-1", "resume position for track, -1 restarts every time"},
102 };
103
104 static qboolean wasPlaying = false;
105 static qboolean initialized = false;
106 static qboolean enabled = false;
107 static float cdvolume;
108 typedef char filename_t[MAX_QPATH];
109 #ifdef MAXTRACKS
110 static filename_t remap[MAXTRACKS];
111 #endif
112 static unsigned char maxTrack;
113 static int faketrack = -1;
114
115 static float saved_vol = 1.0f;
116
117 // exported variables
118 qboolean cdValid = false;
119 qboolean cdPlaying = false;
120 qboolean cdPlayLooping = false;
121 unsigned char cdPlayTrack;
122
123 cl_cdstate_t cd;
124
125 static void CDAudio_Eject (void)
126 {
127         if (!enabled)
128                 return;
129         
130         if(cdaudio.integer == 0)
131                 return;
132
133         CDAudio_SysEject();
134 }
135
136
137 static void CDAudio_CloseDoor (void)
138 {
139         if (!enabled)
140                 return;
141
142         if(cdaudio.integer == 0)
143                 return;
144
145         CDAudio_SysCloseDoor();
146 }
147
148 static int CDAudio_GetAudioDiskInfo (void)
149 {
150         int ret;
151
152         cdValid = false;
153
154         if(cdaudio.integer == 0)
155                 return -1;
156
157         ret = CDAudio_SysGetAudioDiskInfo();
158         if (ret < 1)
159                 return -1;
160
161         cdValid = true;
162         maxTrack = ret;
163
164         return 0;
165 }
166
167 qboolean CDAudio_Play_real (int track, qboolean looping, qboolean complain)
168 {
169         if(track < 1)
170         {
171                 if(complain)
172                         Con_Print("Could not load BGM track.\n");
173                 return false;
174         }
175
176         if (!cdValid)
177         {
178                 CDAudio_GetAudioDiskInfo();
179                 if (!cdValid)
180                 {
181                         if(complain)
182                                 Con_DPrint ("No CD in player.\n");
183                         return false;
184                 }
185         }
186
187         if (track > maxTrack)
188         {
189                 if(complain)
190                         Con_DPrintf("CDAudio: Bad track number %u.\n", track);
191                 return false;
192         }
193
194         if (CDAudio_SysPlay(track) == -1)
195                 return false;
196
197         if(cdaudio.integer != 3)
198                 Con_DPrintf ("CD track %u playing...\n", track);
199
200         return true;
201 }
202
203 void CDAudio_Play_byName (const char *trackname, qboolean looping, qboolean tryreal, float startposition)
204 {
205         unsigned int track;
206         sfx_t* sfx;
207         char filename[MAX_QPATH];
208
209         Host_StartVideo();
210
211         if (!enabled)
212                 return;
213
214         if(tryreal && strspn(trackname, "0123456789") == strlen(trackname))
215         {
216                 track = (unsigned char) atoi(trackname);
217 #ifdef MAXTRACKS
218                 if(track > 0 && track < MAXTRACKS)
219                         if(*remap[track])
220                         {
221                                 if(strspn(remap[track], "0123456789") == strlen(remap[track]))
222                                 {
223                                         trackname = remap[track];
224                                 }
225                                 else
226                                 {
227                                         // ignore remappings to fake tracks if we're going to play a real track
228                                         switch(cdaudio.integer)
229                                         {
230                                                 case 0: // we never access CD
231                                                 case 1: // we have a replacement
232                                                         trackname = remap[track];
233                                                         break;
234                                                 case 2: // we only use fake track replacement if CD track is invalid
235                                                         CDAudio_GetAudioDiskInfo();
236                                                         if(!cdValid || track > maxTrack)
237                                                                 trackname = remap[track];
238                                                         break;
239                                                 case 3: // we always play from CD - ignore this remapping then
240                                                 case 4: // we randomize anyway
241                                                         break;
242                                         }
243                                 }
244                         }
245 #endif
246         }
247
248         if(tryreal && strspn(trackname, "0123456789") == strlen(trackname))
249         {
250                 track = (unsigned char) atoi(trackname);
251                 if (track < 1)
252                 {
253                         Con_DPrintf("CDAudio: Bad track number %u.\n", track);
254                         return;
255                 }
256         }
257         else
258                 track = 0;
259
260         // div0: I assume this code was intentionally there. Maybe turn it into a cvar?
261         if (cdPlaying && cdPlayTrack == track && faketrack == -1)
262                 return;
263         CDAudio_Stop ();
264
265         if(track >= 1)
266         {
267                 if(cdaudio.integer == 3) // only play real CD tracks at all
268                 {
269                         if(CDAudio_Play_real(track, looping, true))
270                                 goto success;
271                         return;
272                 }
273
274                 if(cdaudio.integer == 2) // prefer real CD track over fake
275                 {
276                         if(CDAudio_Play_real(track, looping, false))
277                                 goto success;
278                 }
279         }
280
281         if(cdaudio.integer == 4) // only play real CD tracks, EVEN instead of fake tracks!
282         {
283                 if(CDAudio_Play_real(track, looping, false))
284                         goto success;
285                 
286                 if(cdValid && maxTrack > 0)
287                 {
288                         track = 1 + (rand() % maxTrack);
289                         if(CDAudio_Play_real(track, looping, true))
290                                 goto success;
291                 }
292                 else
293                 {
294                         Con_DPrint ("No CD in player.\n");
295                 }
296                 return;
297         }
298
299         // Try playing a fake track (sound file) first
300         if(track >= 1)
301         {
302                                               dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%03u.wav", track);
303                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%03u.ogg", track);
304                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%02u.wav", track);
305                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%02u.ogg", track);
306         }
307         else
308         {
309                                               dpsnprintf(filename, sizeof(filename), "%s", trackname);
310                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "%s.wav", trackname);
311                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "%s.ogg", trackname);
312                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/%s", trackname);
313                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/%s.wav", trackname);
314                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/%s.ogg", trackname);
315                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/%s", trackname);
316                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/%s.wav", trackname);
317                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/%s.ogg", trackname);
318         }
319         if (FS_FileExists(filename) && (sfx = S_PrecacheSound (filename, false, true)))
320         {
321                 faketrack = S_StartSound_StartPosition (-1, 0, sfx, vec3_origin, cdvolume, 0, startposition);
322                 if (faketrack != -1)
323                 {
324                         if (looping)
325                                 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
326                         S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
327                         S_SetChannelFlag (faketrack, CHANNELFLAG_LOCALSOUND, true); // not pausable
328                         if(track >= 1)
329                         {
330                                 if(cdaudio.integer != 0) // we don't need these messages if only fake tracks can be played anyway
331                                         Con_DPrintf ("Fake CD track %u playing...\n", track);
332                         }
333                         else
334                                 Con_DPrintf ("BGM track %s playing...\n", trackname);
335                 }
336         }
337
338         // If we can't play a fake CD track, try the real one
339         if (faketrack == -1)
340         {
341                 if(cdaudio.integer == 0 || track < 1)
342                 {
343                         Con_Print("Could not load BGM track.\n");
344                         return;
345                 }
346                 else
347                 {
348                         if(!CDAudio_Play_real(track, looping, true))
349                                 return;
350                 }
351         }
352
353 success:
354         cdPlayLooping = looping;
355         cdPlayTrack = track;
356         cdPlaying = true;
357
358         if (cdvolume == 0.0 || bgmvolume.value == 0)
359                 CDAudio_Pause ();
360 }
361
362 void CDAudio_Play (int track, qboolean looping)
363 {
364         char buf[20];
365         if (music_playlist_index.integer >= 0)
366                 return;
367         dpsnprintf(buf, sizeof(buf), "%d", (int) track);
368         CDAudio_Play_byName(buf, looping, true, 0);
369 }
370
371 float CDAudio_GetPosition (void)
372 {
373         if(faketrack != -1)
374                 return S_GetChannelPosition(faketrack);
375         return -1;
376 }
377
378 static void CDAudio_StopPlaylistTrack(void);
379
380 void CDAudio_Stop (void)
381 {
382         if (!enabled)
383                 return;
384
385         // save the playlist position
386         CDAudio_StopPlaylistTrack();
387
388         if (faketrack != -1)
389         {
390                 S_StopChannel (faketrack, true);
391                 faketrack = -1;
392         }
393         else if (cdPlaying && (CDAudio_SysStop() == -1))
394                 return;
395         else if(wasPlaying)
396         {
397                 CDAudio_Resume(); // needed by SDL - can't stop while paused there (causing pause/stop to fail after play, pause, stop, play otherwise)
398                 if (cdPlaying && (CDAudio_SysStop() == -1))
399                         return;
400         }
401
402         wasPlaying = false;
403         cdPlaying = false;
404 }
405
406 void CDAudio_Pause (void)
407 {
408         if (!enabled || !cdPlaying)
409                 return;
410
411         if (faketrack != -1)
412                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
413         else if (CDAudio_SysPause() == -1)
414                 return;
415
416         wasPlaying = cdPlaying;
417         cdPlaying = false;
418 }
419
420
421 void CDAudio_Resume (void)
422 {
423         if (!enabled || cdPlaying || !wasPlaying)
424                 return;
425
426         if (faketrack != -1)
427                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
428         else if (CDAudio_SysResume() == -1)
429                 return;
430         cdPlaying = true;
431 }
432
433 static void CD_f (void)
434 {
435         const char *command;
436 #ifdef MAXTRACKS
437         int ret;
438         int n;
439 #endif
440
441         command = Cmd_Argv (1);
442
443         if (strcasecmp(command, "remap") != 0)
444                 Host_StartVideo();
445
446         if (strcasecmp(command, "on") == 0)
447         {
448                 enabled = true;
449                 return;
450         }
451
452         if (strcasecmp(command, "off") == 0)
453         {
454                 CDAudio_Stop();
455                 enabled = false;
456                 return;
457         }
458
459         if (strcasecmp(command, "reset") == 0)
460         {
461                 enabled = true;
462                 CDAudio_Stop();
463 #ifdef MAXTRACKS
464                 for (n = 0; n < MAXTRACKS; n++)
465                         *remap[n] = 0; // empty string, that is, unremapped
466 #endif
467                 CDAudio_GetAudioDiskInfo();
468                 return;
469         }
470
471         if (strcasecmp(command, "rescan") == 0)
472         {
473                 CDAudio_Shutdown();
474                 CDAudio_Startup();
475                 return;
476         }
477
478         if (strcasecmp(command, "remap") == 0)
479         {
480 #ifdef MAXTRACKS
481                 ret = Cmd_Argc() - 2;
482                 if (ret <= 0)
483                 {
484                         for (n = 1; n < MAXTRACKS; n++)
485                                 if (*remap[n])
486                                         Con_Printf("  %u -> %s\n", n, remap[n]);
487                         return;
488                 }
489                 for (n = 1; n <= ret; n++)
490                         strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
491 #endif
492                 return;
493         }
494
495         if (strcasecmp(command, "close") == 0)
496         {
497                 CDAudio_CloseDoor();
498                 return;
499         }
500
501         if (strcasecmp(command, "play") == 0)
502         {
503                 if (music_playlist_index.integer >= 0)
504                         return;
505                 CDAudio_Play_byName(Cmd_Argv (2), false, true, 0);
506                 return;
507         }
508
509         if (strcasecmp(command, "loop") == 0)
510         {
511                 if (music_playlist_index.integer >= 0)
512                         return;
513                 CDAudio_Play_byName(Cmd_Argv (2), true, true, 0);
514                 return;
515         }
516
517         if (strcasecmp(command, "stop") == 0)
518         {
519                 if (music_playlist_index.integer >= 0)
520                         return;
521                 CDAudio_Stop();
522                 return;
523         }
524
525         if (strcasecmp(command, "pause") == 0)
526         {
527                 if (music_playlist_index.integer >= 0)
528                         return;
529                 CDAudio_Pause();
530                 return;
531         }
532
533         if (strcasecmp(command, "resume") == 0)
534         {
535                 if (music_playlist_index.integer >= 0)
536                         return;
537                 CDAudio_Resume();
538                 return;
539         }
540
541         if (strcasecmp(command, "eject") == 0)
542         {
543                 if (faketrack == -1)
544                         CDAudio_Stop();
545                 CDAudio_Eject();
546                 cdValid = false;
547                 return;
548         }
549
550         if (strcasecmp(command, "info") == 0)
551         {
552                 CDAudio_GetAudioDiskInfo ();
553                 if (cdValid)
554                         Con_Printf("%u tracks on CD.\n", maxTrack);
555                 else
556                         Con_Print ("No CD in player.\n");
557                 if (cdPlaying)
558                         Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
559                 else if (wasPlaying)
560                         Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
561                 if (cdvolume >= 0)
562                         Con_Printf("Volume is %f\n", cdvolume);
563                 else
564                         Con_Printf("Can't get CD volume\n");
565                 return;
566         }
567
568         Con_Printf("CD commands:\n");
569         Con_Printf("cd on - enables CD audio system\n");
570         Con_Printf("cd off - stops and disables CD audio system\n");
571         Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)\n");
572         Con_Printf("cd rescan - rescans disks in drives (to use another disc)\n");
573         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");
574         Con_Printf("cd close - closes CD tray\n");
575         Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
576         Con_Printf("cd play <tracknumber> - plays selected track in remapping table\n");
577         Con_Printf("cd loop <tracknumber> - plays and repeats selected track in remapping table\n");
578         Con_Printf("cd stop - stops playing current CD track\n");
579         Con_Printf("cd pause - pauses CD playback\n");
580         Con_Printf("cd resume - unpauses CD playback\n");
581         Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
582 }
583
584 void CDAudio_SetVolume (float newvol)
585 {
586         // If the volume hasn't changed
587         if (newvol == cdvolume)
588                 return;
589
590         // If the CD has been muted
591         if (newvol == 0.0f)
592                 CDAudio_Pause ();
593         else
594         {
595                 // If the CD has been unmuted
596                 if (cdvolume == 0.0f)
597                         CDAudio_Resume ();
598
599                 if (faketrack != -1)
600                         S_SetChannelVolume (faketrack, newvol);
601                 else
602                         CDAudio_SysSetVolume (newvol);
603         }
604
605         cdvolume = newvol;
606 }
607
608 static void CDAudio_StopPlaylistTrack(void)
609 {
610         if (music_playlist_active >= 0 && music_playlist_active < MAX_PLAYLISTS && music_playlist_sampleposition[music_playlist_active].value >= 0)
611         {
612                 // save position for resume
613                 float position = CDAudio_GetPosition();
614                 Cvar_SetValueQuick(&music_playlist_sampleposition[music_playlist_active], position >= 0 ? position : 0);
615         }
616         music_playlist_active = -1;
617         music_playlist_playing = 0; // not playing
618 }
619
620 void CDAudio_StartPlaylist(qboolean resume)
621 {
622         const char *list;
623         const char *t;
624         int index;
625         int current;
626         int randomplay;
627         int count;
628         int listindex;
629         float position;
630         char trackname[MAX_QPATH];
631         CDAudio_Stop();
632         index = music_playlist_index.integer;
633         if (index >= 0 && index < MAX_PLAYLISTS && bgmvolume.value > 0)
634         {
635                 list = music_playlist_list[index].string;
636                 current = music_playlist_current[index].integer;
637                 randomplay = music_playlist_random[index].integer;
638                 position = music_playlist_sampleposition[index].value;
639                 count = 0;
640                 trackname[0] = 0;
641                 if (list && list[0])
642                 {
643                         for (t = list;;count++)
644                         {
645                                 if (!COM_ParseToken_Console(&t))
646                                         break;
647                                 // if we don't find the desired track, use the first one
648                                 if (count == 0)
649                                         strlcpy(trackname, com_token, sizeof(trackname));
650                         }
651                 }
652                 if (count > 0)
653                 {
654                         // position < 0 means never resume track
655                         if (position < 0)
656                                 position = 0;
657                         // advance to next track in playlist if the last one ended
658                         if (!resume)
659                         {
660                                 position = 0;
661                                 current++;
662                                 if (randomplay)
663                                         current = (int)lhrandom(0, count);
664                         }
665                         // wrap playlist position if needed
666                         if (current >= count)
667                                 current = 0;
668                         // set current
669                         Cvar_SetValueQuick(&music_playlist_current[index], current);
670                         // get the Nth trackname
671                         if (current >= 0 && current < count)
672                         {
673                                 for (listindex = 0, t = list;;listindex++)
674                                 {
675                                         if (!COM_ParseToken_Console(&t))
676                                                 break;
677                                         if (listindex == current)
678                                         {
679                                                 strlcpy(trackname, com_token, sizeof(trackname));
680                                                 break;
681                                         }
682                                 }
683                         }
684                         if (trackname[0])
685                         {
686                                 CDAudio_Play_byName(trackname, false, false, position);
687                                 if (faketrack != -1)
688                                         music_playlist_active = index;
689                         }
690                 }
691         }
692         music_playlist_playing = music_playlist_active >= 0 ? 1 : -1;
693 }
694
695 void CDAudio_Update (void)
696 {
697         static int lastplaylist = -1;
698         if (!enabled)
699                 return;
700
701         CDAudio_SetVolume (bgmvolume.value);
702         if (music_playlist_playing > 0 && CDAudio_GetPosition() < 0)
703         {
704                 // this track ended, start a new track from the beginning
705                 CDAudio_StartPlaylist(false);
706                 lastplaylist = music_playlist_index.integer;
707         }
708         else if (lastplaylist != music_playlist_index.integer
709         || (bgmvolume.value > 0 && !music_playlist_playing && music_playlist_index.integer >= 0))
710         {
711                 // active playlist changed, save position and switch track
712                 CDAudio_StartPlaylist(true);
713                 lastplaylist = music_playlist_index.integer;
714         }
715
716         if (faketrack == -1 && cdaudio.integer != 0 && bgmvolume.value != 0)
717                 CDAudio_SysUpdate();
718 }
719
720 int CDAudio_Init (void)
721 {
722         int i;
723
724         if (cls.state == ca_dedicated)
725                 return -1;
726
727 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
728         if (COM_CheckParm("-nocdaudio"))
729                 return -1;
730
731         CDAudio_SysInit();
732
733 #ifdef MAXTRACKS
734         for (i = 0; i < MAXTRACKS; i++)
735                 *remap[i] = 0;
736 #endif
737
738         Cvar_RegisterVariable(&cdaudio);
739         Cvar_RegisterVariable(&cdaudioinitialized);
740         Cvar_SetValueQuick(&cdaudioinitialized, true);
741         enabled = true;
742
743         Cvar_RegisterVariable(&music_playlist_index);
744         for (i = 0;i < MAX_PLAYLISTS;i++)
745         {
746                 Cvar_RegisterVariable(&music_playlist_list[i]);
747                 Cvar_RegisterVariable(&music_playlist_current[i]);
748                 Cvar_RegisterVariable(&music_playlist_random[i]);
749                 Cvar_RegisterVariable(&music_playlist_sampleposition[i]);
750         }
751
752         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");
753
754         return 0;
755 }
756
757 int CDAudio_Startup (void)
758 {
759         if (COM_CheckParm("-nocdaudio"))
760                 return -1;
761
762         CDAudio_SysStartup ();
763
764         if (CDAudio_GetAudioDiskInfo())
765         {
766                 Con_Print("CDAudio_Init: No CD in player.\n");
767                 cdValid = false;
768         }
769
770         saved_vol = CDAudio_SysGetVolume ();
771         if (saved_vol < 0.0f)
772         {
773                 Con_Print ("Can't get initial CD volume\n");
774                 saved_vol = 1.0f;
775         }
776         else
777                 Con_Printf ("Initial CD volume: %g\n", saved_vol);
778
779         initialized = true;
780
781         Con_Print("CD Audio Initialized\n");
782
783         return 0;
784 }
785
786 void CDAudio_Shutdown (void)
787 {
788         if (!initialized)
789                 return;
790
791         CDAudio_SysSetVolume (saved_vol);
792
793         CDAudio_Stop();
794         CDAudio_SysShutdown();
795         initialized = false;
796 }