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 // 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)"};
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
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] =
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"}
49 cvar_t music_playlist_current[MAX_PLAYLISTS] =
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"},
62 cvar_t music_playlist_random[MAX_PLAYLISTS] =
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"},
75 cvar_t music_playlist_sampleposition[MAX_PLAYLISTS] =
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"},
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];
95 static filename_t remap[MAXTRACKS];
97 static unsigned char maxTrack;
98 static int faketrack = -1;
100 static float saved_vol = 1.0f;
102 // exported variables
103 qboolean cdValid = false;
104 qboolean cdPlaying = false;
105 qboolean cdPlayLooping = false;
106 unsigned char cdPlayTrack;
110 static void CDAudio_Eject (void)
115 if(cdaudio.integer == 0)
122 static void CDAudio_CloseDoor (void)
127 if(cdaudio.integer == 0)
130 CDAudio_SysCloseDoor();
133 static int CDAudio_GetAudioDiskInfo (void)
139 if(cdaudio.integer == 0)
142 ret = CDAudio_SysGetAudioDiskInfo();
152 static qboolean CDAudio_Play_real (int track, qboolean looping, qboolean complain)
157 Con_Print("Could not load BGM track.\n");
163 CDAudio_GetAudioDiskInfo();
167 Con_DPrint ("No CD in player.\n");
172 if (track > maxTrack)
175 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
179 if (CDAudio_SysPlay(track) == -1)
182 if(cdaudio.integer != 3)
183 Con_DPrintf ("CD track %u playing...\n", track);
188 void CDAudio_Play_byName (const char *trackname, qboolean looping, qboolean tryreal, float startposition)
192 char filename[MAX_QPATH];
199 if(tryreal && strspn(trackname, "0123456789") == strlen(trackname))
201 track = (unsigned char) atoi(trackname);
203 if(track > 0 && track < MAXTRACKS)
206 if(strspn(remap[track], "0123456789") == strlen(remap[track]))
208 trackname = remap[track];
212 // ignore remappings to fake tracks if we're going to play a real track
213 switch(cdaudio.integer)
215 case 0: // we never access CD
216 case 1: // we have a replacement
217 trackname = remap[track];
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];
224 case 3: // we always play from CD - ignore this remapping then
225 case 4: // we randomize anyway
233 if(tryreal && strspn(trackname, "0123456789") == strlen(trackname))
235 track = (unsigned char) atoi(trackname);
238 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
245 // div0: I assume this code was intentionally there. Maybe turn it into a cvar?
246 if (cdPlaying && cdPlayTrack == track && faketrack == -1)
252 if(cdaudio.integer == 3) // only play real CD tracks at all
254 if(CDAudio_Play_real(track, looping, true))
259 if(cdaudio.integer == 2) // prefer real CD track over fake
261 if(CDAudio_Play_real(track, looping, false))
266 if(cdaudio.integer == 4) // only play real CD tracks, EVEN instead of fake tracks!
268 if(CDAudio_Play_real(track, looping, false))
271 if(cdValid && maxTrack > 0)
273 track = 1 + (rand() % maxTrack);
274 if(CDAudio_Play_real(track, looping, true))
279 Con_DPrint ("No CD in player.\n");
284 // Try playing a fake track (sound file) first
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
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
310 if (FS_FileExists(filename) && (sfx = S_PrecacheSound (filename, false, false)))
312 faketrack = S_StartSound_StartPosition_Flags (-1, 0, sfx, vec3_origin, cdvolume, 0, startposition, (looping ? CHANNELFLAG_FORCELOOP : 0) | CHANNELFLAG_FULLVOLUME | CHANNELFLAG_LOCALSOUND, 1.0f);
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);
321 Con_DPrintf ("BGM track %s playing...\n", trackname);
325 // If we can't play a fake CD track, try the real one
328 if(cdaudio.integer == 0 || track < 1)
330 Con_Print("Could not load BGM track.\n");
335 if(!CDAudio_Play_real(track, looping, true))
341 cdPlayLooping = looping;
345 if (cdvolume == 0.0 || bgmvolume.value == 0)
349 void CDAudio_Play (int track, qboolean looping)
352 if (music_playlist_index.integer >= 0)
354 dpsnprintf(buf, sizeof(buf), "%d", (int) track);
355 CDAudio_Play_byName(buf, looping, true, 0);
358 float CDAudio_GetPosition (void)
361 return S_GetChannelPosition(faketrack);
365 static void CDAudio_StopPlaylistTrack(void);
367 void CDAudio_Stop (void)
372 // save the playlist position
373 CDAudio_StopPlaylistTrack();
377 S_StopChannel (faketrack, true, true);
380 else if (cdPlaying && (CDAudio_SysStop() == -1))
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))
393 void CDAudio_Pause (void)
395 if (!enabled || !cdPlaying)
399 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
400 else if (CDAudio_SysPause() == -1)
403 wasPlaying = cdPlaying;
408 void CDAudio_Resume (void)
410 if (!enabled || cdPlaying || !wasPlaying)
414 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
415 else if (CDAudio_SysResume() == -1)
420 static void CD_f (void)
428 command = Cmd_Argv (1);
430 if (strcasecmp(command, "remap") != 0)
433 if (strcasecmp(command, "on") == 0)
439 if (strcasecmp(command, "off") == 0)
446 if (strcasecmp(command, "reset") == 0)
451 for (n = 0; n < MAXTRACKS; n++)
452 *remap[n] = 0; // empty string, that is, unremapped
454 CDAudio_GetAudioDiskInfo();
458 if (strcasecmp(command, "rescan") == 0)
465 if (strcasecmp(command, "remap") == 0)
468 ret = Cmd_Argc() - 2;
471 for (n = 1; n < MAXTRACKS; n++)
473 Con_Printf(" %u -> %s\n", n, remap[n]);
476 for (n = 1; n <= ret; n++)
477 strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
482 if (strcasecmp(command, "close") == 0)
488 if (strcasecmp(command, "play") == 0)
490 if (music_playlist_index.integer >= 0)
492 CDAudio_Play_byName(Cmd_Argv (2), false, true, (Cmd_Argc() > 3) ? atof( Cmd_Argv(3) ) : 0);
496 if (strcasecmp(command, "loop") == 0)
498 if (music_playlist_index.integer >= 0)
500 CDAudio_Play_byName(Cmd_Argv (2), true, true, (Cmd_Argc() > 3) ? atof( Cmd_Argv(3) ) : 0);
504 if (strcasecmp(command, "stop") == 0)
506 if (music_playlist_index.integer >= 0)
512 if (strcasecmp(command, "pause") == 0)
514 if (music_playlist_index.integer >= 0)
520 if (strcasecmp(command, "resume") == 0)
522 if (music_playlist_index.integer >= 0)
528 if (strcasecmp(command, "eject") == 0)
537 if (strcasecmp(command, "info") == 0)
539 CDAudio_GetAudioDiskInfo ();
541 Con_Printf("%u tracks on CD.\n", maxTrack);
543 Con_Print ("No CD in player.\n");
545 Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
547 Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
549 Con_Printf("Volume is %f\n", cdvolume);
551 Con_Printf("Can't get CD volume\n");
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");
571 static void CDAudio_SetVolume (float newvol)
573 // If the volume hasn't changed
574 if (newvol == cdvolume)
577 // If the CD has been muted
582 // If the CD has been unmuted
583 if (cdvolume == 0.0f)
587 S_SetChannelVolume (faketrack, newvol);
589 CDAudio_SysSetVolume (newvol * mastervolume.value);
595 static void CDAudio_StopPlaylistTrack(void)
597 if (music_playlist_active >= 0 && music_playlist_active < MAX_PLAYLISTS && music_playlist_sampleposition[music_playlist_active].value >= 0)
599 // save position for resume
600 float position = CDAudio_GetPosition();
601 Cvar_SetValueQuick(&music_playlist_sampleposition[music_playlist_active], position >= 0 ? position : 0);
603 music_playlist_active = -1;
604 music_playlist_playing = 0; // not playing
607 void CDAudio_StartPlaylist(qboolean resume)
617 char trackname[MAX_QPATH];
619 index = music_playlist_index.integer;
620 if (index >= 0 && index < MAX_PLAYLISTS && bgmvolume.value > 0)
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;
630 for (t = list;;count++)
632 if (!COM_ParseToken_Console(&t))
634 // if we don't find the desired track, use the first one
636 strlcpy(trackname, com_token, sizeof(trackname));
641 // position < 0 means never resume track
644 // advance to next track in playlist if the last one ended
650 current = (int)lhrandom(0, count);
652 // wrap playlist position if needed
653 if (current >= count)
656 Cvar_SetValueQuick(&music_playlist_current[index], current);
657 // get the Nth trackname
658 if (current >= 0 && current < count)
660 for (listindex = 0, t = list;;listindex++)
662 if (!COM_ParseToken_Console(&t))
664 if (listindex == current)
666 strlcpy(trackname, com_token, sizeof(trackname));
673 CDAudio_Play_byName(trackname, false, false, position);
675 music_playlist_active = index;
679 music_playlist_playing = music_playlist_active >= 0 ? 1 : -1;
682 void CDAudio_Update (void)
684 static int lastplaylist = -1;
688 CDAudio_SetVolume (bgmvolume.value);
689 if (music_playlist_playing > 0 && CDAudio_GetPosition() < 0)
691 // this track ended, start a new track from the beginning
692 CDAudio_StartPlaylist(false);
693 lastplaylist = music_playlist_index.integer;
695 else if (lastplaylist != music_playlist_index.integer
696 || (bgmvolume.value > 0 && !music_playlist_playing && music_playlist_index.integer >= 0))
698 // active playlist changed, save position and switch track
699 CDAudio_StartPlaylist(true);
700 lastplaylist = music_playlist_index.integer;
703 if (faketrack == -1 && cdaudio.integer != 0 && bgmvolume.value != 0)
707 int CDAudio_Init (void)
711 if (cls.state == ca_dedicated)
714 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
715 if (COM_CheckParm("-nocdaudio"))
721 for (i = 0; i < MAXTRACKS; i++)
725 Cvar_RegisterVariable(&cdaudio);
726 Cvar_RegisterVariable(&cdaudioinitialized);
727 Cvar_SetValueQuick(&cdaudioinitialized, true);
730 Cvar_RegisterVariable(&music_playlist_index);
731 for (i = 0;i < MAX_PLAYLISTS;i++)
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]);
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");
744 int CDAudio_Startup (void)
746 if (COM_CheckParm("-nocdaudio"))
749 CDAudio_SysStartup ();
751 if (CDAudio_GetAudioDiskInfo())
753 Con_Print("CDAudio_Init: No CD in player.\n");
757 saved_vol = CDAudio_SysGetVolume ();
758 if (saved_vol < 0.0f)
760 Con_Print ("Can't get initial CD volume\n");
764 Con_Printf ("Initial CD volume: %g\n", saved_vol);
768 Con_Print("CD Audio Initialized\n");
773 void CDAudio_Shutdown (void)
778 CDAudio_SysSetVolume (saved_vol);
781 CDAudio_SysShutdown();