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 (qbyte 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"};
45 static qboolean wasPlaying = false;
46 static qboolean initialized = false;
47 static qboolean enabled = false;
48 static float cdvolume;
49 static qbyte remap[MAXTRACKS];
50 static qbyte maxTrack;
51 static int faketrack = -1;
53 static float saved_vol = 1.0f;
56 qboolean cdValid = false;
57 qboolean cdPlaying = false;
58 qboolean cdPlayLooping = false;
62 static void CDAudio_Eject (void)
71 static void CDAudio_CloseDoor (void)
76 CDAudio_SysCloseDoor();
79 static int CDAudio_GetAudioDiskInfo (void)
85 ret = CDAudio_SysGetAudioDiskInfo();
96 void CDAudio_Play (qbyte track, qboolean looping)
103 track = remap[track];
106 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
110 if (cdPlaying && cdPlayTrack == track)
114 // Try playing a fake track (sound file) first
115 sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, true, false);
116 // FIXME: perhaps force it to be always %03u (but for compatibility?):
118 sfx = S_PrecacheSound (va ("cdtracks/track%03u.wav", track), false, true, false);
121 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
125 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
126 S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
127 Con_DPrintf ("Fake CD track %u playing...\n", track);
131 // If we can't play a fake CD track, try the real one
136 CDAudio_GetAudioDiskInfo();
139 Con_Print ("No CD in player.\n");
144 if (track > maxTrack)
146 Con_Printf("CDAudio: Bad track number %u.\n", track);
150 if (CDAudio_SysPlay(track) == -1)
154 cdPlayLooping = looping;
163 void CDAudio_Stop (void)
165 if (!enabled || !cdPlaying)
170 S_StopChannel (faketrack);
173 else if (CDAudio_SysStop() == -1)
180 void CDAudio_Pause (void)
182 if (!enabled || !cdPlaying)
186 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
187 else if (CDAudio_SysPause() == -1)
190 wasPlaying = cdPlaying;
195 void CDAudio_Resume (void)
197 if (!enabled || cdPlaying || !wasPlaying)
201 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
202 else if (CDAudio_SysResume() == -1)
207 static void CD_f (void)
216 command = Cmd_Argv (1);
218 if (strcasecmp(command, "on") == 0)
224 if (strcasecmp(command, "off") == 0)
232 if (strcasecmp(command, "reset") == 0)
237 for (n = 0; n < MAXTRACKS; n++)
239 CDAudio_GetAudioDiskInfo();
243 if (strcasecmp(command, "remap") == 0)
245 ret = Cmd_Argc() - 2;
248 for (n = 1; n < MAXTRACKS; n++)
250 Con_Printf(" %u -> %u\n", n, remap[n]);
253 for (n = 1; n <= ret; n++)
254 remap[n] = atoi(Cmd_Argv (n+1));
258 if (strcasecmp(command, "close") == 0)
264 if (strcasecmp(command, "play") == 0)
266 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), false);
270 if (strcasecmp(command, "loop") == 0)
272 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), true);
276 if (strcasecmp(command, "stop") == 0)
282 if (strcasecmp(command, "pause") == 0)
288 if (strcasecmp(command, "resume") == 0)
294 if (strcasecmp(command, "eject") == 0)
296 if (cdPlaying && faketrack == -1)
303 if (strcasecmp(command, "info") == 0)
305 CDAudio_GetAudioDiskInfo ();
307 Con_Printf("%u tracks on CD.\n", maxTrack);
309 Con_Print ("No CD in player.\n");
311 Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
313 Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
314 Con_Printf("Volume is %f\n", cdvolume);
319 void CDAudio_SetVolume (float newvol)
321 // If the volume hasn't changed
322 if (newvol == cdvolume)
325 // If the CD has been muted
330 // If the CD has been unmuted
331 if (cdvolume == 0.0f)
335 S_SetChannelVolume (faketrack, newvol);
336 CDAudio_SysSetVolume (newvol);
342 void CDAudio_Update (void)
347 CDAudio_SetVolume (bgmvolume.value);
353 int CDAudio_Init (void)
357 if (cls.state == ca_dedicated)
360 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
361 if (COM_CheckParm("-nocdaudio") || COM_CheckParm("-safe"))
366 for (i = 0; i < MAXTRACKS; i++)
369 Cvar_RegisterVariable(&cdaudioinitialized);
370 Cvar_SetValueQuick(&cdaudioinitialized, true);
373 Cmd_AddCommand("cd", CD_f);
378 int CDAudio_Startup (void)
380 CDAudio_SysStartup ();
382 if (CDAudio_GetAudioDiskInfo())
384 Con_DPrint("CDAudio_Init: No CD in player.\n");
388 saved_vol = CDAudio_SysGetVolume ();
389 if (saved_vol < 0.0f)
391 Con_DPrint ("Can't get initial CD volume\n");
395 Con_DPrintf ("Initial CD volume: %g\n", saved_vol);
399 Con_DPrint("CD Audio Initialized\n");
404 void CDAudio_Shutdown (void)
409 CDAudio_SysSetVolume (saved_vol);
412 CDAudio_SysShutdown();