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"};
30 #define MAX_PLAYLISTS 10
31 int music_playlist_active = -1;
32 int music_playlist_playing = 0; // 0 = not playing, 1 = playing, -1 = tried and failed
34 cvar_t music_playlist_index = {0, "music_playlist_index", "-1", "selects which of the music_playlist_ variables is the active one, -1 disables playlists"};
35 cvar_t music_playlist_list[MAX_PLAYLISTS] =
37 {0, "music_playlist_list0", "", "list of tracks to play"},
38 {0, "music_playlist_list1", "", "list of tracks to play"},
39 {0, "music_playlist_list2", "", "list of tracks to play"},
40 {0, "music_playlist_list3", "", "list of tracks to play"},
41 {0, "music_playlist_list4", "", "list of tracks to play"},
42 {0, "music_playlist_list5", "", "list of tracks to play"},
43 {0, "music_playlist_list6", "", "list of tracks to play"},
44 {0, "music_playlist_list7", "", "list of tracks to play"},
45 {0, "music_playlist_list8", "", "list of tracks to play"},
46 {0, "music_playlist_list9", "", "list of tracks to play"}
48 cvar_t music_playlist_current[MAX_PLAYLISTS] =
50 {0, "music_playlist_current0", "0", "current track index to play in list"},
51 {0, "music_playlist_current1", "0", "current track index to play in list"},
52 {0, "music_playlist_current2", "0", "current track index to play in list"},
53 {0, "music_playlist_current3", "0", "current track index to play in list"},
54 {0, "music_playlist_current4", "0", "current track index to play in list"},
55 {0, "music_playlist_current5", "0", "current track index to play in list"},
56 {0, "music_playlist_current6", "0", "current track index to play in list"},
57 {0, "music_playlist_current7", "0", "current track index to play in list"},
58 {0, "music_playlist_current8", "0", "current track index to play in list"},
59 {0, "music_playlist_current9", "0", "current track index to play in list"},
61 cvar_t music_playlist_random[MAX_PLAYLISTS] =
63 {0, "music_playlist_random0", "0", "enables random play order if 1, 0 is sequential play"},
64 {0, "music_playlist_random1", "0", "enables random play order if 1, 0 is sequential play"},
65 {0, "music_playlist_random2", "0", "enables random play order if 1, 0 is sequential play"},
66 {0, "music_playlist_random3", "0", "enables random play order if 1, 0 is sequential play"},
67 {0, "music_playlist_random4", "0", "enables random play order if 1, 0 is sequential play"},
68 {0, "music_playlist_random5", "0", "enables random play order if 1, 0 is sequential play"},
69 {0, "music_playlist_random6", "0", "enables random play order if 1, 0 is sequential play"},
70 {0, "music_playlist_random7", "0", "enables random play order if 1, 0 is sequential play"},
71 {0, "music_playlist_random8", "0", "enables random play order if 1, 0 is sequential play"},
72 {0, "music_playlist_random9", "0", "enables random play order if 1, 0 is sequential play"},
74 cvar_t music_playlist_sampleposition[MAX_PLAYLISTS] =
76 {0, "music_playlist_sampleposition0", "-1", "resume position for track, -1 restarts every time"},
77 {0, "music_playlist_sampleposition1", "-1", "resume position for track, -1 restarts every time"},
78 {0, "music_playlist_sampleposition2", "-1", "resume position for track, -1 restarts every time"},
79 {0, "music_playlist_sampleposition3", "-1", "resume position for track, -1 restarts every time"},
80 {0, "music_playlist_sampleposition4", "-1", "resume position for track, -1 restarts every time"},
81 {0, "music_playlist_sampleposition5", "-1", "resume position for track, -1 restarts every time"},
82 {0, "music_playlist_sampleposition6", "-1", "resume position for track, -1 restarts every time"},
83 {0, "music_playlist_sampleposition7", "-1", "resume position for track, -1 restarts every time"},
84 {0, "music_playlist_sampleposition8", "-1", "resume position for track, -1 restarts every time"},
85 {0, "music_playlist_sampleposition9", "-1", "resume position for track, -1 restarts every time"},
88 static qboolean wasPlaying = false;
89 static qboolean initialized = false;
90 static qboolean enabled = false;
91 static float cdvolume;
92 typedef char filename_t[MAX_QPATH];
94 static filename_t remap[MAXTRACKS];
96 static int faketrack = -1;
99 qboolean cdPlaying = false;
100 qboolean cdPlayLooping = false;
101 unsigned char cdPlayTrack;
105 static void CDAudio_Eject (void)
110 static void CDAudio_CloseDoor (void)
114 static int CDAudio_GetAudioDiskInfo (void)
119 void CDAudio_Play_byName (const char *trackname, qboolean looping, qboolean tryreal, float startposition)
123 char filename[MAX_QPATH];
130 if(tryreal && strspn(trackname, "0123456789") == strlen(trackname))
132 track = (unsigned int) atoi(trackname);
134 if(track > 0 && track < MAXTRACKS && *remap[track])
135 trackname = remap[track];
139 if(tryreal && strspn(trackname, "0123456789") == strlen(trackname))
141 track = (unsigned int) atoi(trackname);
144 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
151 // div0: I assume this code was intentionally there. Maybe turn it into a cvar?
152 if (cdPlaying && cdPlayTrack == track && faketrack == -1)
156 // Try playing a fake track (sound file) first
159 dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%03u.wav", track);
160 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%03u.ogg", track);
161 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "music/track%03u.ogg", track);// added by motorsep
162 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "music/cdtracks/track%03u.ogg", track);// added by motorsep
163 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%02u.wav", track);
164 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/track%02u.ogg", track);
165 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "music/track%02u.ogg", track);// added by motorsep
166 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "music/cdtracks/track%02u.ogg", track);// added by motorsep
170 dpsnprintf(filename, sizeof(filename), "%s", trackname);
171 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "%s.wav", trackname);
172 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "%s.ogg", trackname);
173 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/%s", trackname);
174 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/%s.wav", trackname);
175 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/%s.ogg", trackname);
176 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/%s", trackname);
177 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/%s.wav", trackname);
178 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "sound/cdtracks/%s.ogg", trackname);
179 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "music/%s.ogg", trackname); // added by motorsep
180 if (!FS_FileExists(filename)) dpsnprintf(filename, sizeof(filename), "music/cdtracks/%s.ogg", trackname); // added by motorsep
182 if (FS_FileExists(filename) && (sfx = S_PrecacheSound (filename, false, false)))
184 faketrack = S_StartSound_StartPosition_Flags (-1, 0, sfx, vec3_origin, cdvolume, 0, startposition, (looping ? CHANNELFLAG_FORCELOOP : 0) | CHANNELFLAG_FULLVOLUME | CHANNELFLAG_LOCALSOUND, 1.0f);
188 Con_DPrintf ("BGM track %u playing...\n", track);
190 Con_DPrintf ("BGM track %s playing...\n", trackname);
197 Con_DPrintf ("Could not load BGM track %u.\n", track);
199 Con_DPrintf ("Could not load BGM track %s.\n", trackname);
203 cdPlayLooping = looping;
207 if (cdvolume == 0.0 || bgmvolume.value == 0)
211 void CDAudio_Play (int track, qboolean looping)
214 if (music_playlist_index.integer >= 0)
216 dpsnprintf(buf, sizeof(buf), "%d", (int) track);
217 CDAudio_Play_byName(buf, looping, true, 0);
220 float CDAudio_GetPosition (void)
223 return S_GetChannelPosition(faketrack);
227 static void CDAudio_StopPlaylistTrack(void);
229 void CDAudio_Stop (void)
234 // save the playlist position
235 CDAudio_StopPlaylistTrack();
239 S_StopChannel (faketrack, true, true);
247 void CDAudio_Pause (void)
249 if (!enabled || !cdPlaying || faketrack == -1)
252 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
253 wasPlaying = cdPlaying;
258 void CDAudio_Resume (void)
260 if (!enabled || cdPlaying || !wasPlaying || faketrack == -1)
263 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
267 static void CD_f (void)
275 command = Cmd_Argv (1);
277 if (strcasecmp(command, "remap") != 0)
280 if (strcasecmp(command, "on") == 0)
286 if (strcasecmp(command, "off") == 0)
293 if (strcasecmp(command, "reset") == 0)
298 for (n = 0; n < MAXTRACKS; n++)
299 *remap[n] = 0; // empty string, that is, unremapped
301 CDAudio_GetAudioDiskInfo();
305 if (strcasecmp(command, "rescan") == 0)
312 if (strcasecmp(command, "remap") == 0)
315 ret = Cmd_Argc() - 2;
318 for (n = 1; n < MAXTRACKS; n++)
320 Con_Printf(" %u -> %s\n", n, remap[n]);
323 for (n = 1; n <= ret; n++)
324 strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
329 if (strcasecmp(command, "close") == 0)
335 if (strcasecmp(command, "play") == 0)
337 if (music_playlist_index.integer >= 0)
339 CDAudio_Play_byName(Cmd_Argv (2), false, true, (Cmd_Argc() > 3) ? atof( Cmd_Argv(3) ) : 0);
343 if (strcasecmp(command, "loop") == 0)
345 if (music_playlist_index.integer >= 0)
347 CDAudio_Play_byName(Cmd_Argv (2), true, true, (Cmd_Argc() > 3) ? atof( Cmd_Argv(3) ) : 0);
351 if (strcasecmp(command, "stop") == 0)
353 if (music_playlist_index.integer >= 0)
359 if (strcasecmp(command, "pause") == 0)
361 if (music_playlist_index.integer >= 0)
367 if (strcasecmp(command, "resume") == 0)
369 if (music_playlist_index.integer >= 0)
375 if (strcasecmp(command, "eject") == 0)
383 if (strcasecmp(command, "info") == 0)
385 CDAudio_GetAudioDiskInfo ();
387 Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
389 Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
391 Con_Printf("Volume is %f\n", cdvolume);
393 Con_Printf("Can't get CD volume\n");
397 Con_Printf("CD commands:\n");
398 Con_Printf("cd on - enables CD audio system\n");
399 Con_Printf("cd off - stops and disables CD audio system\n");
400 Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)\n");
401 Con_Printf("cd rescan - rescans disks in drives (to use another disc)\n");
402 Con_Printf("cd remap <remap1> [remap2] [remap3] [...] - chooses emulated CD tracks to play when a map asks for a particular track, this has many uses\n");
403 Con_Printf("cd close - closes CD tray\n");
404 Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
405 Con_Printf("cd play <tracknumber> <startposition> - plays selected track in remapping table\n");
406 Con_Printf("cd loop <tracknumber> <startposition> - plays and repeats selected track in remapping table\n");
407 Con_Printf("cd stop - stops playing current CD track\n");
408 Con_Printf("cd pause - pauses CD playback\n");
409 Con_Printf("cd resume - unpauses CD playback\n");
410 Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
413 static void CDAudio_SetVolume (float newvol)
415 // If the volume hasn't changed
416 if (newvol == cdvolume)
419 // If the CD has been muted
424 // If the CD has been unmuted
425 if (cdvolume == 0.0f)
429 S_SetChannelVolume (faketrack, newvol);
435 static void CDAudio_StopPlaylistTrack(void)
437 if (music_playlist_active >= 0 && music_playlist_active < MAX_PLAYLISTS && music_playlist_sampleposition[music_playlist_active].value >= 0)
439 // save position for resume
440 float position = CDAudio_GetPosition();
441 Cvar_SetValueQuick(&music_playlist_sampleposition[music_playlist_active], position >= 0 ? position : 0);
443 music_playlist_active = -1;
444 music_playlist_playing = 0; // not playing
447 void CDAudio_StartPlaylist(qboolean resume)
457 char trackname[MAX_QPATH];
459 index = music_playlist_index.integer;
460 if (index >= 0 && index < MAX_PLAYLISTS && bgmvolume.value > 0)
462 list = music_playlist_list[index].string;
463 current = music_playlist_current[index].integer;
464 randomplay = music_playlist_random[index].integer;
465 position = music_playlist_sampleposition[index].value;
470 for (t = list;;count++)
472 if (!COM_ParseToken_Console(&t))
474 // if we don't find the desired track, use the first one
476 strlcpy(trackname, com_token, sizeof(trackname));
481 // position < 0 means never resume track
484 // advance to next track in playlist if the last one ended
490 current = (int)lhrandom(0, count);
492 // wrap playlist position if needed
493 if (current >= count)
496 Cvar_SetValueQuick(&music_playlist_current[index], current);
497 // get the Nth trackname
498 if (current >= 0 && current < count)
500 for (listindex = 0, t = list;;listindex++)
502 if (!COM_ParseToken_Console(&t))
504 if (listindex == current)
506 strlcpy(trackname, com_token, sizeof(trackname));
513 CDAudio_Play_byName(trackname, false, false, position);
515 music_playlist_active = index;
519 music_playlist_playing = music_playlist_active >= 0 ? 1 : -1;
522 void CDAudio_Update (void)
524 static int lastplaylist = -1;
528 CDAudio_SetVolume (bgmvolume.value);
529 if (music_playlist_playing > 0 && CDAudio_GetPosition() < 0)
531 // this track ended, start a new track from the beginning
532 CDAudio_StartPlaylist(false);
533 lastplaylist = music_playlist_index.integer;
535 else if (lastplaylist != music_playlist_index.integer
536 || (bgmvolume.value > 0 && !music_playlist_playing && music_playlist_index.integer >= 0))
538 // active playlist changed, save position and switch track
539 CDAudio_StartPlaylist(true);
540 lastplaylist = music_playlist_index.integer;
544 int CDAudio_Init (void)
548 if (cls.state == ca_dedicated)
551 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
552 if (COM_CheckParm("-nocdaudio"))
556 for (i = 0; i < MAXTRACKS; i++)
560 Cvar_RegisterVariable(&cdaudioinitialized);
561 Cvar_SetValueQuick(&cdaudioinitialized, true);
564 Cvar_RegisterVariable(&music_playlist_index);
565 for (i = 0;i < MAX_PLAYLISTS;i++)
567 Cvar_RegisterVariable(&music_playlist_list[i]);
568 Cvar_RegisterVariable(&music_playlist_current[i]);
569 Cvar_RegisterVariable(&music_playlist_random[i]);
570 Cvar_RegisterVariable(&music_playlist_sampleposition[i]);
573 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");
578 int CDAudio_Startup (void)
580 if (COM_CheckParm("-nocdaudio"))
585 Con_Print("CD Audio Initialized\n");
590 void CDAudio_Shutdown (void)