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 (qbyte 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"};
47 static qboolean wasPlaying = false;
48 static qboolean initialized = false;
49 static qboolean enabled = false;
50 static float cdvolume;
51 static qbyte remap[MAXTRACKS];
52 static qbyte maxTrack;
53 static int faketrack = -1;
55 static float saved_vol = 1.0f;
58 qboolean cdValid = false;
59 qboolean cdPlaying = false;
60 qboolean cdPlayLooping = false;
64 static void CDAudio_Eject (void)
73 static void CDAudio_CloseDoor (void)
78 CDAudio_SysCloseDoor();
81 static int CDAudio_GetAudioDiskInfo (void)
87 ret = CDAudio_SysGetAudioDiskInfo();
98 void CDAudio_Play (qbyte track, qboolean looping)
105 track = remap[track];
108 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
112 if (cdPlaying && cdPlayTrack == track && faketrack == -1)
116 // Try playing a fake track (sound file) first
117 sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, false);
118 // FIXME: perhaps force it to be always %03u (but for compatibility?):
119 if (sfx == NULL || sfx->fetcher == NULL)
120 sfx = S_PrecacheSound (va ("cdtracks/track%03u.wav", track), false, false);
123 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
127 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
128 S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
129 Con_DPrintf ("Fake CD track %u playing...\n", track);
133 // If we can't play a fake CD track, try the real one
138 CDAudio_GetAudioDiskInfo();
141 Con_Print ("No CD in player.\n");
146 if (track > maxTrack)
148 Con_Printf("CDAudio: Bad track number %u.\n", track);
152 if (CDAudio_SysPlay(track) == -1)
156 cdPlayLooping = looping;
165 void CDAudio_Stop (void)
167 if (!enabled || !cdPlaying)
172 S_StopChannel (faketrack);
175 else if (CDAudio_SysStop() == -1)
182 void CDAudio_Pause (void)
184 if (!enabled || !cdPlaying)
188 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
189 else if (CDAudio_SysPause() == -1)
192 wasPlaying = cdPlaying;
197 void CDAudio_Resume (void)
199 if (!enabled || cdPlaying || !wasPlaying)
203 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
204 else if (CDAudio_SysResume() == -1)
209 static void CD_f (void)
218 command = Cmd_Argv (1);
220 if (strcasecmp(command, "on") == 0)
226 if (strcasecmp(command, "off") == 0)
234 if (strcasecmp(command, "reset") == 0)
239 for (n = 0; n < MAXTRACKS; n++)
241 CDAudio_GetAudioDiskInfo();
245 if (strcasecmp(command, "remap") == 0)
247 ret = Cmd_Argc() - 2;
250 for (n = 1; n < MAXTRACKS; n++)
252 Con_Printf(" %u -> %u\n", n, remap[n]);
255 for (n = 1; n <= ret; n++)
256 remap[n] = atoi(Cmd_Argv (n+1));
260 if (strcasecmp(command, "close") == 0)
266 if (strcasecmp(command, "play") == 0)
268 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), false);
272 if (strcasecmp(command, "loop") == 0)
274 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), true);
278 if (strcasecmp(command, "stop") == 0)
284 if (strcasecmp(command, "pause") == 0)
290 if (strcasecmp(command, "resume") == 0)
296 if (strcasecmp(command, "eject") == 0)
298 if (cdPlaying && faketrack == -1)
305 if (strcasecmp(command, "info") == 0)
307 CDAudio_GetAudioDiskInfo ();
309 Con_Printf("%u tracks on CD.\n", maxTrack);
311 Con_Print ("No CD in player.\n");
313 Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
315 Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
316 Con_Printf("Volume is %f\n", cdvolume);
321 void CDAudio_SetVolume (float newvol)
323 // If the volume hasn't changed
324 if (newvol == cdvolume)
327 // If the CD has been muted
332 // If the CD has been unmuted
333 if (cdvolume == 0.0f)
337 S_SetChannelVolume (faketrack, newvol);
338 CDAudio_SysSetVolume (newvol);
344 void CDAudio_Update (void)
349 CDAudio_SetVolume (bgmvolume.value);
355 int CDAudio_Init (void)
359 if (cls.state == ca_dedicated)
362 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
363 if (COM_CheckParm("-nocdaudio") || COM_CheckParm("-safe"))
368 for (i = 0; i < MAXTRACKS; i++)
371 Cvar_RegisterVariable(&cdaudioinitialized);
372 Cvar_SetValueQuick(&cdaudioinitialized, true);
375 Cmd_AddCommand("cd", CD_f);
380 int CDAudio_Startup (void)
382 CDAudio_SysStartup ();
384 if (CDAudio_GetAudioDiskInfo())
386 Con_DPrint("CDAudio_Init: No CD in player.\n");
390 saved_vol = CDAudio_SysGetVolume ();
391 if (saved_vol < 0.0f)
393 Con_DPrint ("Can't get initial CD volume\n");
397 Con_DPrintf ("Initial CD volume: %g\n", saved_vol);
401 Con_DPrint("CD Audio Initialized\n");
406 void CDAudio_Shutdown (void)
411 CDAudio_SysSetVolume (saved_vol);
414 CDAudio_SysShutdown();