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