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