]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cd_shared.c
default effectinfo velocitymultiplier to 0 in nexuiz mode
[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 #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), "music/track%03u.ogg", track);// added by motorsep
305                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "music/cdtracks/track%03u.ogg", track);// added by motorsep
306                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%02u.wav", track);
307                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%02u.ogg", track);
308                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "music/track%02u.ogg", track);// added by motorsep
309                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "music/cdtracks/track%02u.ogg", track);// added by motorsep
310         }
311         else
312         {
313                                               dpsnprintf(filename, sizeof(filename), "%s", trackname);
314                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "%s.wav", trackname);
315                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "%s.ogg", trackname);
316                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/%s", trackname);
317                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/%s.wav", trackname);
318                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/%s.ogg", trackname);
319                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/%s", trackname);
320                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/%s.wav", trackname);
321                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/%s.ogg", trackname);
322                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "music/%s.ogg", trackname); // added by motorsep
323                 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "music/cdtracks/%s.ogg", trackname); // added by motorsep
324         }
325         if (FS_FileExists(filename) && (sfx = S_PrecacheSound (filename, false, false)))
326         {
327                 faketrack = S_StartSound_StartPosition_Flags (-1, 0, sfx, vec3_origin, cdvolume, 0, startposition, (looping ? CHANNELFLAG_FORCELOOP : 0) | CHANNELFLAG_FULLVOLUME | CHANNELFLAG_LOCALSOUND);
328                 if (faketrack != -1)
329                 {
330                         if(track >= 1)
331                         {
332                                 if(cdaudio.integer != 0) // we don't need these messages if only fake tracks can be played anyway
333                                         Con_DPrintf ("Fake CD track %u playing...\n", track);
334                         }
335                         else
336                                 Con_DPrintf ("BGM track %s playing...\n", trackname);
337                 }
338         }
339
340         // If we can't play a fake CD track, try the real one
341         if (faketrack == -1)
342         {
343                 if(cdaudio.integer == 0 || track < 1)
344                 {
345                         Con_Print("Could not load BGM track.\n");
346                         return;
347                 }
348                 else
349                 {
350                         if(!CDAudio_Play_real(track, looping, true))
351                                 return;
352                 }
353         }
354
355 success:
356         cdPlayLooping = looping;
357         cdPlayTrack = track;
358         cdPlaying = true;
359
360         if (cdvolume == 0.0 || bgmvolume.value == 0)
361                 CDAudio_Pause ();
362 }
363
364 void CDAudio_Play (int track, qboolean looping)
365 {
366         char buf[20];
367         if (music_playlist_index.integer >= 0)
368                 return;
369         dpsnprintf(buf, sizeof(buf), "%d", (int) track);
370         CDAudio_Play_byName(buf, looping, true, 0);
371 }
372
373 float CDAudio_GetPosition (void)
374 {
375         if(faketrack != -1)
376                 return S_GetChannelPosition(faketrack);
377         return -1;
378 }
379
380 static void CDAudio_StopPlaylistTrack(void);
381
382 void CDAudio_Stop (void)
383 {
384         if (!enabled)
385                 return;
386
387         // save the playlist position
388         CDAudio_StopPlaylistTrack();
389
390         if (faketrack != -1)
391         {
392                 S_StopChannel (faketrack, true, true);
393                 faketrack = -1;
394         }
395         else if (cdPlaying && (CDAudio_SysStop() == -1))
396                 return;
397         else if(wasPlaying)
398         {
399                 CDAudio_Resume(); // needed by SDL - can't stop while paused there (causing pause/stop to fail after play, pause, stop, play otherwise)
400                 if (cdPlaying && (CDAudio_SysStop() == -1))
401                         return;
402         }
403
404         wasPlaying = false;
405         cdPlaying = false;
406 }
407
408 void CDAudio_Pause (void)
409 {
410         if (!enabled || !cdPlaying)
411                 return;
412
413         if (faketrack != -1)
414                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
415         else if (CDAudio_SysPause() == -1)
416                 return;
417
418         wasPlaying = cdPlaying;
419         cdPlaying = false;
420 }
421
422
423 void CDAudio_Resume (void)
424 {
425         if (!enabled || cdPlaying || !wasPlaying)
426                 return;
427
428         if (faketrack != -1)
429                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
430         else if (CDAudio_SysResume() == -1)
431                 return;
432         cdPlaying = true;
433 }
434
435 static void CD_f (void)
436 {
437         const char *command;
438 #ifdef MAXTRACKS
439         int ret;
440         int n;
441 #endif
442
443         command = Cmd_Argv (1);
444
445         if (strcasecmp(command, "remap") != 0)
446                 Host_StartVideo();
447
448         if (strcasecmp(command, "on") == 0)
449         {
450                 enabled = true;
451                 return;
452         }
453
454         if (strcasecmp(command, "off") == 0)
455         {
456                 CDAudio_Stop();
457                 enabled = false;
458                 return;
459         }
460
461         if (strcasecmp(command, "reset") == 0)
462         {
463                 enabled = true;
464                 CDAudio_Stop();
465 #ifdef MAXTRACKS
466                 for (n = 0; n < MAXTRACKS; n++)
467                         *remap[n] = 0; // empty string, that is, unremapped
468 #endif
469                 CDAudio_GetAudioDiskInfo();
470                 return;
471         }
472
473         if (strcasecmp(command, "rescan") == 0)
474         {
475                 CDAudio_Shutdown();
476                 CDAudio_Startup();
477                 return;
478         }
479
480         if (strcasecmp(command, "remap") == 0)
481         {
482 #ifdef MAXTRACKS
483                 ret = Cmd_Argc() - 2;
484                 if (ret <= 0)
485                 {
486                         for (n = 1; n < MAXTRACKS; n++)
487                                 if (*remap[n])
488                                         Con_Printf("  %u -> %s\n", n, remap[n]);
489                         return;
490                 }
491                 for (n = 1; n <= ret; n++)
492                         strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
493 #endif
494                 return;
495         }
496
497         if (strcasecmp(command, "close") == 0)
498         {
499                 CDAudio_CloseDoor();
500                 return;
501         }
502
503         if (strcasecmp(command, "play") == 0)
504         {
505                 if (music_playlist_index.integer >= 0)
506                         return;
507                 CDAudio_Play_byName(Cmd_Argv (2), false, true, (Cmd_Argc() > 3) ? atof( Cmd_Argv(3) ) : 0);
508                 return;
509         }
510
511         if (strcasecmp(command, "loop") == 0)
512         {
513                 if (music_playlist_index.integer >= 0)
514                         return;
515                 CDAudio_Play_byName(Cmd_Argv (2), true, true, (Cmd_Argc() > 3) ? atof( Cmd_Argv(3) ) : 0);
516                 return;
517         }
518
519         if (strcasecmp(command, "stop") == 0)
520         {
521                 if (music_playlist_index.integer >= 0)
522                         return;
523                 CDAudio_Stop();
524                 return;
525         }
526
527         if (strcasecmp(command, "pause") == 0)
528         {
529                 if (music_playlist_index.integer >= 0)
530                         return;
531                 CDAudio_Pause();
532                 return;
533         }
534
535         if (strcasecmp(command, "resume") == 0)
536         {
537                 if (music_playlist_index.integer >= 0)
538                         return;
539                 CDAudio_Resume();
540                 return;
541         }
542
543         if (strcasecmp(command, "eject") == 0)
544         {
545                 if (faketrack == -1)
546                         CDAudio_Stop();
547                 CDAudio_Eject();
548                 cdValid = false;
549                 return;
550         }
551
552         if (strcasecmp(command, "info") == 0)
553         {
554                 CDAudio_GetAudioDiskInfo ();
555                 if (cdValid)
556                         Con_Printf("%u tracks on CD.\n", maxTrack);
557                 else
558                         Con_Print ("No CD in player.\n");
559                 if (cdPlaying)
560                         Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
561                 else if (wasPlaying)
562                         Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
563                 if (cdvolume >= 0)
564                         Con_Printf("Volume is %f\n", cdvolume);
565                 else
566                         Con_Printf("Can't get CD volume\n");
567                 return;
568         }
569
570         Con_Printf("CD commands:\n");
571         Con_Printf("cd on - enables CD audio system\n");
572         Con_Printf("cd off - stops and disables CD audio system\n");
573         Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)\n");
574         Con_Printf("cd rescan - rescans disks in drives (to use another disc)\n");
575         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");
576         Con_Printf("cd close - closes CD tray\n");
577         Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
578         Con_Printf("cd play <tracknumber> <startposition> - plays selected track in remapping table\n");
579         Con_Printf("cd loop <tracknumber> <startposition> - plays and repeats selected track in remapping table\n");
580         Con_Printf("cd stop - stops playing current CD track\n");
581         Con_Printf("cd pause - pauses CD playback\n");
582         Con_Printf("cd resume - unpauses CD playback\n");
583         Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
584 }
585
586 void CDAudio_SetVolume (float newvol)
587 {
588         // If the volume hasn't changed
589         if (newvol == cdvolume)
590                 return;
591
592         // If the CD has been muted
593         if (newvol == 0.0f)
594                 CDAudio_Pause ();
595         else
596         {
597                 // If the CD has been unmuted
598                 if (cdvolume == 0.0f)
599                         CDAudio_Resume ();
600
601                 if (faketrack != -1)
602                         S_SetChannelVolume (faketrack, newvol);
603                 else
604                         CDAudio_SysSetVolume (newvol * mastervolume.value);
605         }
606
607         cdvolume = newvol;
608 }
609
610 static void CDAudio_StopPlaylistTrack(void)
611 {
612         if (music_playlist_active >= 0 && music_playlist_active < MAX_PLAYLISTS && music_playlist_sampleposition[music_playlist_active].value >= 0)
613         {
614                 // save position for resume
615                 float position = CDAudio_GetPosition();
616                 Cvar_SetValueQuick(&music_playlist_sampleposition[music_playlist_active], position >= 0 ? position : 0);
617         }
618         music_playlist_active = -1;
619         music_playlist_playing = 0; // not playing
620 }
621
622 void CDAudio_StartPlaylist(qboolean resume)
623 {
624         const char *list;
625         const char *t;
626         int index;
627         int current;
628         int randomplay;
629         int count;
630         int listindex;
631         float position;
632         char trackname[MAX_QPATH];
633         CDAudio_Stop();
634         index = music_playlist_index.integer;
635         if (index >= 0 && index < MAX_PLAYLISTS && bgmvolume.value > 0)
636         {
637                 list = music_playlist_list[index].string;
638                 current = music_playlist_current[index].integer;
639                 randomplay = music_playlist_random[index].integer;
640                 position = music_playlist_sampleposition[index].value;
641                 count = 0;
642                 trackname[0] = 0;
643                 if (list && list[0])
644                 {
645                         for (t = list;;count++)
646                         {
647                                 if (!COM_ParseToken_Console(&t))
648                                         break;
649                                 // if we don't find the desired track, use the first one
650                                 if (count == 0)
651                                         strlcpy(trackname, com_token, sizeof(trackname));
652                         }
653                 }
654                 if (count > 0)
655                 {
656                         // position < 0 means never resume track
657                         if (position < 0)
658                                 position = 0;
659                         // advance to next track in playlist if the last one ended
660                         if (!resume)
661                         {
662                                 position = 0;
663                                 current++;
664                                 if (randomplay)
665                                         current = (int)lhrandom(0, count);
666                         }
667                         // wrap playlist position if needed
668                         if (current >= count)
669                                 current = 0;
670                         // set current
671                         Cvar_SetValueQuick(&music_playlist_current[index], current);
672                         // get the Nth trackname
673                         if (current >= 0 && current < count)
674                         {
675                                 for (listindex = 0, t = list;;listindex++)
676                                 {
677                                         if (!COM_ParseToken_Console(&t))
678                                                 break;
679                                         if (listindex == current)
680                                         {
681                                                 strlcpy(trackname, com_token, sizeof(trackname));
682                                                 break;
683                                         }
684                                 }
685                         }
686                         if (trackname[0])
687                         {
688                                 CDAudio_Play_byName(trackname, false, false, position);
689                                 if (faketrack != -1)
690                                         music_playlist_active = index;
691                         }
692                 }
693         }
694         music_playlist_playing = music_playlist_active >= 0 ? 1 : -1;
695 }
696
697 void CDAudio_Update (void)
698 {
699         static int lastplaylist = -1;
700         if (!enabled)
701                 return;
702
703         CDAudio_SetVolume (bgmvolume.value);
704         if (music_playlist_playing > 0 && CDAudio_GetPosition() < 0)
705         {
706                 // this track ended, start a new track from the beginning
707                 CDAudio_StartPlaylist(false);
708                 lastplaylist = music_playlist_index.integer;
709         }
710         else if (lastplaylist != music_playlist_index.integer
711         || (bgmvolume.value > 0 && !music_playlist_playing && music_playlist_index.integer >= 0))
712         {
713                 // active playlist changed, save position and switch track
714                 CDAudio_StartPlaylist(true);
715                 lastplaylist = music_playlist_index.integer;
716         }
717
718         if (faketrack == -1 && cdaudio.integer != 0 && bgmvolume.value != 0)
719                 CDAudio_SysUpdate();
720 }
721
722 int CDAudio_Init (void)
723 {
724         int i;
725
726         if (cls.state == ca_dedicated)
727                 return -1;
728
729 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
730         if (COM_CheckParm("-nocdaudio"))
731                 return -1;
732
733         CDAudio_SysInit();
734
735 #ifdef MAXTRACKS
736         for (i = 0; i < MAXTRACKS; i++)
737                 *remap[i] = 0;
738 #endif
739
740         Cvar_RegisterVariable(&cdaudio);
741         Cvar_RegisterVariable(&cdaudioinitialized);
742         Cvar_SetValueQuick(&cdaudioinitialized, true);
743         enabled = true;
744
745         Cvar_RegisterVariable(&music_playlist_index);
746         for (i = 0;i < MAX_PLAYLISTS;i++)
747         {
748                 Cvar_RegisterVariable(&music_playlist_list[i]);
749                 Cvar_RegisterVariable(&music_playlist_current[i]);
750                 Cvar_RegisterVariable(&music_playlist_random[i]);
751                 Cvar_RegisterVariable(&music_playlist_sampleposition[i]);
752         }
753
754         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");
755
756         return 0;
757 }
758
759 int CDAudio_Startup (void)
760 {
761         if (COM_CheckParm("-nocdaudio"))
762                 return -1;
763
764         CDAudio_SysStartup ();
765
766         if (CDAudio_GetAudioDiskInfo())
767         {
768                 Con_Print("CDAudio_Init: No CD in player.\n");
769                 cdValid = false;
770         }
771
772         saved_vol = CDAudio_SysGetVolume ();
773         if (saved_vol < 0.0f)
774         {
775                 Con_Print ("Can't get initial CD volume\n");
776                 saved_vol = 1.0f;
777         }
778         else
779                 Con_Printf ("Initial CD volume: %g\n", saved_vol);
780
781         initialized = true;
782
783         Con_Print("CD Audio Initialized\n");
784
785         return 0;
786 }
787
788 void CDAudio_Shutdown (void)
789 {
790         if (!initialized)
791                 return;
792
793         CDAudio_SysSetVolume (saved_vol);
794
795         CDAudio_Stop();
796         CDAudio_SysShutdown();
797         initialized = false;
798 }