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
29 // Prototypes of the system dependent functions
30 extern void CDAudio_SysEject (void);
31 extern void CDAudio_SysCloseDoor (void);
32 extern int CDAudio_SysGetAudioDiskInfo (void);
33 extern float CDAudio_SysGetVolume (void);
34 extern void CDAudio_SysSetVolume (float volume);
35 extern int CDAudio_SysPlay (unsigned char track);
36 extern int CDAudio_SysStop (void);
37 extern int CDAudio_SysPause (void);
38 extern int CDAudio_SysResume (void);
39 extern int CDAudio_SysUpdate (void);
40 extern void CDAudio_SysInit (void);
41 extern int CDAudio_SysStartup (void);
42 extern void CDAudio_SysShutdown (void);
44 // used by menu to ghost CD audio slider
45 cvar_t cdaudioinitialized = {CVAR_READONLY,"cdaudioinitialized","0","indicates if CD Audio system is active"};
46 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)"};
48 static qboolean wasPlaying = false;
49 static qboolean initialized = false;
50 static qboolean enabled = false;
51 static float cdvolume;
52 typedef char filename_t[MAX_QPATH];
53 static filename_t remap[MAXTRACKS];
54 static unsigned char maxTrack;
55 static int faketrack = -1;
57 static float saved_vol = 1.0f;
60 qboolean cdValid = false;
61 qboolean cdPlaying = false;
62 qboolean cdPlayLooping = false;
63 unsigned char cdPlayTrack;
67 static void CDAudio_Eject (void)
72 if(cdaudio.integer == 0)
79 static void CDAudio_CloseDoor (void)
84 if(cdaudio.integer == 0)
87 CDAudio_SysCloseDoor();
90 static int CDAudio_GetAudioDiskInfo (void)
96 if(cdaudio.integer == 0)
99 ret = CDAudio_SysGetAudioDiskInfo();
109 qboolean CDAudio_Play_real (int track, qboolean looping, qboolean complain)
114 Con_Print("Could not load BGM track.\n");
120 CDAudio_GetAudioDiskInfo();
124 Con_Print ("No CD in player.\n");
129 if (track > maxTrack)
132 Con_Printf("CDAudio: Bad track number %u.\n", track);
136 if (CDAudio_SysPlay(track) == -1)
139 if(cdaudio.integer != 3 || developer.integer)
140 Con_Printf ("CD track %u playing...\n", track);
145 void CDAudio_Play_byName (const char *trackname, qboolean looping)
155 if(strspn(trackname, "0123456789") == strlen(trackname))
157 track = (unsigned char) atoi(trackname);
158 if(track > 0 && track < MAXTRACKS)
160 trackname = remap[track];
163 if(strspn(trackname, "0123456789") == strlen(trackname))
165 track = (unsigned char) atoi(trackname);
168 Con_Printf("CDAudio: Bad track number %u.\n", track);
175 if (cdPlaying && cdPlayTrack == track && faketrack == -1)
181 if(cdaudio.integer == 3) // only play real CD tracks at all
183 if(CDAudio_Play_real(track, looping, true))
188 if(cdaudio.integer == 2) // prefer real CD track over fake
190 if(CDAudio_Play_real(track, looping, false))
195 // Try playing a fake track (sound file) first
198 sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, false);
199 if (sfx == NULL || !S_IsSoundPrecached (sfx))
200 sfx = S_PrecacheSound (va ("cdtracks/track%03u.wav", track), false, false);
201 if (sfx == NULL || !S_IsSoundPrecached (sfx))
202 sfx = S_PrecacheSound (va ("cdtracks/track%02u", track), false, false);
203 if (sfx == NULL || !S_IsSoundPrecached (sfx))
204 sfx = S_PrecacheSound (va ("cdtracks/track%03u", track), false, false);
208 sfx = S_PrecacheSound (va("cdtracks/%s.wav", trackname), false, false);
209 if (sfx == NULL || !S_IsSoundPrecached (sfx))
210 sfx = S_PrecacheSound (va("cdtracks/%s", trackname), false, false);
214 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
218 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
219 S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
222 if(cdaudio.integer != 0 || developer.integer) // we don't need these messages if only fake tracks can be played anyway
223 Con_Printf ("Fake CD track %u playing...\n", track);
226 Con_DPrintf ("BGM track %s playing...\n", trackname);
230 // If we can't play a fake CD track, try the real one
233 if(cdaudio.integer == 0 || track < 1)
235 Con_Print("Could not load BGM track.\n");
240 if(!CDAudio_Play_real(track, looping, true))
246 cdPlayLooping = looping;
254 void CDAudio_Play (unsigned char track, qboolean looping)
257 dpsnprintf(buf, sizeof(buf), "%d", (int) track);
258 CDAudio_Play_byName(buf, looping);
261 void CDAudio_Stop (void)
268 S_StopChannel (faketrack, true);
271 else if (cdPlaying && (CDAudio_SysStop() == -1))
278 void CDAudio_Pause (void)
280 if (!enabled || !cdPlaying)
284 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
285 else if (CDAudio_SysPause() == -1)
288 wasPlaying = cdPlaying;
293 void CDAudio_Resume (void)
295 if (!enabled || cdPlaying || !wasPlaying)
299 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
300 else if (CDAudio_SysResume() == -1)
305 static void CD_f (void)
314 command = Cmd_Argv (1);
316 if (strcasecmp(command, "remap") != 0)
319 if (strcasecmp(command, "on") == 0)
325 if (strcasecmp(command, "off") == 0)
333 if (strcasecmp(command, "reset") == 0)
338 for (n = 0; n < MAXTRACKS; n++)
339 *remap[n] = 0; // empty string, that is, unremapped
340 CDAudio_GetAudioDiskInfo();
344 if (strcasecmp(command, "remap") == 0)
346 ret = Cmd_Argc() - 2;
349 for (n = 1; n < MAXTRACKS; n++)
351 Con_Printf(" %u -> %s\n", n, remap[n]);
354 for (n = 1; n <= ret; n++)
355 strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
359 if (strcasecmp(command, "close") == 0)
365 if (strcasecmp(command, "play") == 0)
367 CDAudio_Play_byName(Cmd_Argv (2), false);
371 if (strcasecmp(command, "loop") == 0)
373 CDAudio_Play_byName(Cmd_Argv (2), true);
377 if (strcasecmp(command, "stop") == 0)
383 if (strcasecmp(command, "pause") == 0)
389 if (strcasecmp(command, "resume") == 0)
395 if (strcasecmp(command, "eject") == 0)
397 if (cdPlaying && faketrack == -1)
404 if (strcasecmp(command, "info") == 0)
406 CDAudio_GetAudioDiskInfo ();
408 Con_Printf("%u tracks on CD.\n", maxTrack);
410 Con_Print ("No CD in player.\n");
412 Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
414 Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
415 Con_Printf("Volume is %f\n", cdvolume);
419 Con_Printf("CD commands:\n");
420 Con_Printf("cd on - enables CD audio system\n");
421 Con_Printf("cd off - stops and disables CD audio system\n");
422 Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)");
423 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");
424 Con_Printf("cd close - closes CD tray\n");
425 Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
426 Con_Printf("cd play <tracknumber> - plays selected track in remapping table\n");
427 Con_Printf("cd loop <tracknumber> - plays and repeats selected track in remapping table\n");
428 Con_Printf("cd stop - stops playing current CD track\n");
429 Con_Printf("cd pause - pauses CD playback\n");
430 Con_Printf("cd resume - unpauses CD playback\n");
431 Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
434 void CDAudio_SetVolume (float newvol)
436 // If the volume hasn't changed
437 if (newvol == cdvolume)
440 // If the CD has been muted
445 // If the CD has been unmuted
446 if (cdvolume == 0.0f)
450 S_SetChannelVolume (faketrack, newvol);
452 CDAudio_SysSetVolume (newvol);
458 void CDAudio_Update (void)
463 CDAudio_SetVolume (bgmvolume.value);
465 if (faketrack == -1 && cdaudio.integer != 0)
469 int CDAudio_Init (void)
473 if (cls.state == ca_dedicated)
476 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
477 if (COM_CheckParm("-nocdaudio"))
482 for (i = 0; i < MAXTRACKS; i++)
485 Cvar_RegisterVariable(&cdaudio);
486 Cvar_RegisterVariable(&cdaudioinitialized);
487 Cvar_SetValueQuick(&cdaudioinitialized, true);
490 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");
495 int CDAudio_Startup (void)
497 if (COM_CheckParm("-nocdaudio"))
500 CDAudio_SysStartup ();
502 if (CDAudio_GetAudioDiskInfo())
504 Con_Print("CDAudio_Init: No CD in player.\n");
508 saved_vol = CDAudio_SysGetVolume ();
509 if (saved_vol < 0.0f)
511 Con_Print ("Can't get initial CD volume\n");
515 Con_Printf ("Initial CD volume: %g\n", saved_vol);
519 Con_Print("CD Audio Initialized\n");
524 void CDAudio_Shutdown (void)
529 CDAudio_SysSetVolume (saved_vol);
532 CDAudio_SysShutdown();