2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // Quake is a trademark of Id Software, Inc., (c) 1996 Id Software, Inc. All
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);
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)"};
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
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] =
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"}
64 cvar_t music_playlist_current[MAX_PLAYLISTS] =
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"},
77 cvar_t music_playlist_random[MAX_PLAYLISTS] =
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"},
90 cvar_t music_playlist_sampleposition[MAX_PLAYLISTS] =
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"},
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];
110 static filename_t remap[MAXTRACKS];
112 static unsigned char maxTrack;
113 static int faketrack = -1;
115 static float saved_vol = 1.0f;
117 // exported variables
118 qboolean cdValid = false;
119 qboolean cdPlaying = false;
120 qboolean cdPlayLooping = false;
121 unsigned char cdPlayTrack;
125 static void CDAudio_Eject (void)
130 if(cdaudio.integer == 0)
137 static void CDAudio_CloseDoor (void)
142 if(cdaudio.integer == 0)
145 CDAudio_SysCloseDoor();
148 static int CDAudio_GetAudioDiskInfo (void)
154 if(cdaudio.integer == 0)
157 ret = CDAudio_SysGetAudioDiskInfo();
167 qboolean CDAudio_Play_real (int track, qboolean looping, qboolean complain)
172 Con_Print("Could not load BGM track.\n");
178 CDAudio_GetAudioDiskInfo();
182 Con_DPrint ("No CD in player.\n");
187 if (track > maxTrack)
190 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
194 if (CDAudio_SysPlay(track) == -1)
197 if(cdaudio.integer != 3)
198 Con_DPrintf ("CD track %u playing...\n", track);
203 void CDAudio_Play_byName (const char *trackname, qboolean looping, qboolean tryreal, float startposition)
207 char filename[MAX_QPATH];
214 if(tryreal && strspn(trackname, "0123456789") == strlen(trackname))
216 track = (unsigned char) atoi(trackname);
218 if(track > 0 && track < MAXTRACKS)
221 if(strspn(remap[track], "0123456789") == strlen(remap[track]))
223 trackname = remap[track];
227 // ignore remappings to fake tracks if we're going to play a real track
228 switch(cdaudio.integer)
230 case 0: // we never access CD
231 case 1: // we have a replacement
232 trackname = remap[track];
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];
239 case 3: // we always play from CD - ignore this remapping then
240 case 4: // we randomize anyway
248 if(tryreal && strspn(trackname, "0123456789") == strlen(trackname))
250 track = (unsigned char) atoi(trackname);
253 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
260 // div0: I assume this code was intentionally there. Maybe turn it into a cvar?
261 if (cdPlaying && cdPlayTrack == track && faketrack == -1)
267 if(cdaudio.integer == 3) // only play real CD tracks at all
269 if(CDAudio_Play_real(track, looping, true))
274 if(cdaudio.integer == 2) // prefer real CD track over fake
276 if(CDAudio_Play_real(track, looping, false))
281 if(cdaudio.integer == 4) // only play real CD tracks, EVEN instead of fake tracks!
283 if(CDAudio_Play_real(track, looping, false))
286 if(cdValid && maxTrack > 0)
288 track = 1 + (rand() % maxTrack);
289 if(CDAudio_Play_real(track, looping, true))
294 Con_DPrint ("No CD in player.\n");
299 // Try playing a fake track (sound file) first
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
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
325 if (FS_FileExists(filename) && (sfx = S_PrecacheSound (filename, false, true)))
327 faketrack = S_StartSound_StartPosition (-1, 0, sfx, vec3_origin, cdvolume, 0, startposition);
331 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
332 S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
333 S_SetChannelFlag (faketrack, CHANNELFLAG_LOCALSOUND, true); // not pausable
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);
340 Con_DPrintf ("BGM track %s playing...\n", trackname);
344 // If we can't play a fake CD track, try the real one
347 if(cdaudio.integer == 0 || track < 1)
349 Con_Print("Could not load BGM track.\n");
354 if(!CDAudio_Play_real(track, looping, true))
360 cdPlayLooping = looping;
364 if (cdvolume == 0.0 || bgmvolume.value == 0)
368 void CDAudio_Play (int track, qboolean looping)
371 if (music_playlist_index.integer >= 0)
373 dpsnprintf(buf, sizeof(buf), "%d", (int) track);
374 CDAudio_Play_byName(buf, looping, true, 0);
377 float CDAudio_GetPosition (void)
380 return S_GetChannelPosition(faketrack);
384 static void CDAudio_StopPlaylistTrack(void);
386 void CDAudio_Stop (void)
391 // save the playlist position
392 CDAudio_StopPlaylistTrack();
396 S_StopChannel (faketrack, true);
399 else if (cdPlaying && (CDAudio_SysStop() == -1))
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))
412 void CDAudio_Pause (void)
414 if (!enabled || !cdPlaying)
418 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
419 else if (CDAudio_SysPause() == -1)
422 wasPlaying = cdPlaying;
427 void CDAudio_Resume (void)
429 if (!enabled || cdPlaying || !wasPlaying)
433 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
434 else if (CDAudio_SysResume() == -1)
439 static void CD_f (void)
447 command = Cmd_Argv (1);
449 if (strcasecmp(command, "remap") != 0)
452 if (strcasecmp(command, "on") == 0)
458 if (strcasecmp(command, "off") == 0)
465 if (strcasecmp(command, "reset") == 0)
470 for (n = 0; n < MAXTRACKS; n++)
471 *remap[n] = 0; // empty string, that is, unremapped
473 CDAudio_GetAudioDiskInfo();
477 if (strcasecmp(command, "rescan") == 0)
484 if (strcasecmp(command, "remap") == 0)
487 ret = Cmd_Argc() - 2;
490 for (n = 1; n < MAXTRACKS; n++)
492 Con_Printf(" %u -> %s\n", n, remap[n]);
495 for (n = 1; n <= ret; n++)
496 strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
501 if (strcasecmp(command, "close") == 0)
507 if (strcasecmp(command, "play") == 0)
509 if (music_playlist_index.integer >= 0)
511 CDAudio_Play_byName(Cmd_Argv (2), false, true, (Cmd_Argc() > 3) ? atof( Cmd_Argv(3) ) : 0);
515 if (strcasecmp(command, "loop") == 0)
517 if (music_playlist_index.integer >= 0)
519 CDAudio_Play_byName(Cmd_Argv (2), true, true, (Cmd_Argc() > 3) ? atof( Cmd_Argv(3) ) : 0);
523 if (strcasecmp(command, "stop") == 0)
525 if (music_playlist_index.integer >= 0)
531 if (strcasecmp(command, "pause") == 0)
533 if (music_playlist_index.integer >= 0)
539 if (strcasecmp(command, "resume") == 0)
541 if (music_playlist_index.integer >= 0)
547 if (strcasecmp(command, "eject") == 0)
556 if (strcasecmp(command, "info") == 0)
558 CDAudio_GetAudioDiskInfo ();
560 Con_Printf("%u tracks on CD.\n", maxTrack);
562 Con_Print ("No CD in player.\n");
564 Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
566 Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
568 Con_Printf("Volume is %f\n", cdvolume);
570 Con_Printf("Can't get CD volume\n");
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");
590 void CDAudio_SetVolume (float newvol)
592 // If the volume hasn't changed
593 if (newvol == cdvolume)
596 // If the CD has been muted
601 // If the CD has been unmuted
602 if (cdvolume == 0.0f)
606 S_SetChannelVolume (faketrack, newvol);
608 CDAudio_SysSetVolume (newvol * mastervolume.value);
614 static void CDAudio_StopPlaylistTrack(void)
616 if (music_playlist_active >= 0 && music_playlist_active < MAX_PLAYLISTS && music_playlist_sampleposition[music_playlist_active].value >= 0)
618 // save position for resume
619 float position = CDAudio_GetPosition();
620 Cvar_SetValueQuick(&music_playlist_sampleposition[music_playlist_active], position >= 0 ? position : 0);
622 music_playlist_active = -1;
623 music_playlist_playing = 0; // not playing
626 void CDAudio_StartPlaylist(qboolean resume)
636 char trackname[MAX_QPATH];
638 index = music_playlist_index.integer;
639 if (index >= 0 && index < MAX_PLAYLISTS && bgmvolume.value > 0)
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;
649 for (t = list;;count++)
651 if (!COM_ParseToken_Console(&t))
653 // if we don't find the desired track, use the first one
655 strlcpy(trackname, com_token, sizeof(trackname));
660 // position < 0 means never resume track
663 // advance to next track in playlist if the last one ended
669 current = (int)lhrandom(0, count);
671 // wrap playlist position if needed
672 if (current >= count)
675 Cvar_SetValueQuick(&music_playlist_current[index], current);
676 // get the Nth trackname
677 if (current >= 0 && current < count)
679 for (listindex = 0, t = list;;listindex++)
681 if (!COM_ParseToken_Console(&t))
683 if (listindex == current)
685 strlcpy(trackname, com_token, sizeof(trackname));
692 CDAudio_Play_byName(trackname, false, false, position);
694 music_playlist_active = index;
698 music_playlist_playing = music_playlist_active >= 0 ? 1 : -1;
701 void CDAudio_Update (void)
703 static int lastplaylist = -1;
707 CDAudio_SetVolume (bgmvolume.value);
708 if (music_playlist_playing > 0 && CDAudio_GetPosition() < 0)
710 // this track ended, start a new track from the beginning
711 CDAudio_StartPlaylist(false);
712 lastplaylist = music_playlist_index.integer;
714 else if (lastplaylist != music_playlist_index.integer
715 || (bgmvolume.value > 0 && !music_playlist_playing && music_playlist_index.integer >= 0))
717 // active playlist changed, save position and switch track
718 CDAudio_StartPlaylist(true);
719 lastplaylist = music_playlist_index.integer;
722 if (faketrack == -1 && cdaudio.integer != 0 && bgmvolume.value != 0)
726 int CDAudio_Init (void)
730 if (cls.state == ca_dedicated)
733 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
734 if (COM_CheckParm("-nocdaudio"))
740 for (i = 0; i < MAXTRACKS; i++)
744 Cvar_RegisterVariable(&cdaudio);
745 Cvar_RegisterVariable(&cdaudioinitialized);
746 Cvar_SetValueQuick(&cdaudioinitialized, true);
749 Cvar_RegisterVariable(&music_playlist_index);
750 for (i = 0;i < MAX_PLAYLISTS;i++)
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]);
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");
763 int CDAudio_Startup (void)
765 if (COM_CheckParm("-nocdaudio"))
768 CDAudio_SysStartup ();
770 if (CDAudio_GetAudioDiskInfo())
772 Con_Print("CDAudio_Init: No CD in player.\n");
776 saved_vol = CDAudio_SysGetVolume ();
777 if (saved_vol < 0.0f)
779 Con_Print ("Can't get initial CD volume\n");
783 Con_Printf ("Initial CD volume: %g\n", saved_vol);
787 Con_Print("CD Audio Initialized\n");
792 void CDAudio_Shutdown (void)
797 CDAudio_SysSetVolume (saved_vol);
800 CDAudio_SysShutdown();