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"};
47 static qboolean wasPlaying = false;
48 static qboolean initialized = false;
49 static qboolean enabled = false;
50 static float cdvolume;
51 static unsigned char remap[MAXTRACKS];
52 static unsigned char 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;
61 unsigned char cdPlayTrack;
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 (unsigned char track, qboolean looping)
107 track = remap[track];
110 Con_Printf("CDAudio: Bad track number %u.\n", track);
114 if (cdPlaying && cdPlayTrack == track && faketrack == -1)
118 // Try playing a fake track (sound file) first
119 sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, false);
120 if (sfx == NULL || !S_IsSoundPrecached (sfx))
121 sfx = S_PrecacheSound (va ("cdtracks/track%03u.wav", track), false, false);
124 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
128 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
129 S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
130 Con_Printf ("Fake CD track %u playing...\n", track);
134 // If we can't play a fake CD track, try the real one
139 CDAudio_GetAudioDiskInfo();
142 Con_Print ("No CD in player.\n");
147 if (track > maxTrack)
149 Con_Printf("CDAudio: Bad track number %u.\n", track);
153 if (CDAudio_SysPlay(track) == -1)
157 cdPlayLooping = looping;
166 void CDAudio_Stop (void)
168 if (!enabled || !cdPlaying)
173 S_StopChannel (faketrack);
176 else if (CDAudio_SysStop() == -1)
183 void CDAudio_Pause (void)
185 if (!enabled || !cdPlaying)
189 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
190 else if (CDAudio_SysPause() == -1)
193 wasPlaying = cdPlaying;
198 void CDAudio_Resume (void)
200 if (!enabled || cdPlaying || !wasPlaying)
204 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
205 else if (CDAudio_SysResume() == -1)
210 static void CD_f (void)
221 command = Cmd_Argv (1);
223 if (strcasecmp(command, "on") == 0)
229 if (strcasecmp(command, "off") == 0)
237 if (strcasecmp(command, "reset") == 0)
242 for (n = 0; n < MAXTRACKS; n++)
244 CDAudio_GetAudioDiskInfo();
248 if (strcasecmp(command, "remap") == 0)
250 ret = Cmd_Argc() - 2;
253 for (n = 1; n < MAXTRACKS; n++)
255 Con_Printf(" %u -> %u\n", n, remap[n]);
258 for (n = 1; n <= ret; n++)
259 remap[n] = atoi(Cmd_Argv (n+1));
263 if (strcasecmp(command, "close") == 0)
269 if (strcasecmp(command, "play") == 0)
271 CDAudio_Play((unsigned char)atoi(Cmd_Argv (2)), false);
275 if (strcasecmp(command, "loop") == 0)
277 CDAudio_Play((unsigned char)atoi(Cmd_Argv (2)), true);
281 if (strcasecmp(command, "stop") == 0)
287 if (strcasecmp(command, "pause") == 0)
293 if (strcasecmp(command, "resume") == 0)
299 if (strcasecmp(command, "eject") == 0)
301 if (cdPlaying && faketrack == -1)
308 if (strcasecmp(command, "info") == 0)
310 CDAudio_GetAudioDiskInfo ();
312 Con_Printf("%u tracks on CD.\n", maxTrack);
314 Con_Print ("No CD in player.\n");
316 Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
318 Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
319 Con_Printf("Volume is %f\n", cdvolume);
323 Con_Printf("CD commands:\n");
324 Con_Printf("cd on - enables CD audio system\n");
325 Con_Printf("cd off - stops and disables CD audio system\n");
326 Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)");
327 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");
328 Con_Printf("cd close - closes CD tray\n");
329 Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
330 Con_Printf("cd play <tracknumber> - plays selected track in remapping table\n");
331 Con_Printf("cd loop <tracknumber> - plays and repeats selected track in remapping table\n");
332 Con_Printf("cd stop - stops playing current CD track\n");
333 Con_Printf("cd pause - pauses CD playback\n");
334 Con_Printf("cd resume - unpauses CD playback\n");
335 Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
338 void CDAudio_SetVolume (float newvol)
340 // If the volume hasn't changed
341 if (newvol == cdvolume)
344 // If the CD has been muted
349 // If the CD has been unmuted
350 if (cdvolume == 0.0f)
354 S_SetChannelVolume (faketrack, newvol);
355 CDAudio_SysSetVolume (newvol);
361 void CDAudio_Update (void)
366 CDAudio_SetVolume (bgmvolume.value);
372 int CDAudio_Init (void)
376 if (cls.state == ca_dedicated)
379 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
380 if (COM_CheckParm("-nocdaudio") || COM_CheckParm("-safe"))
385 for (i = 0; i < MAXTRACKS; i++)
388 Cvar_RegisterVariable(&cdaudioinitialized);
389 Cvar_SetValueQuick(&cdaudioinitialized, true);
392 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");
397 int CDAudio_Startup (void)
399 CDAudio_SysStartup ();
401 if (CDAudio_GetAudioDiskInfo())
403 Con_Print("CDAudio_Init: No CD in player.\n");
407 saved_vol = CDAudio_SysGetVolume ();
408 if (saved_vol < 0.0f)
410 Con_Print ("Can't get initial CD volume\n");
414 Con_Printf ("Initial CD volume: %g\n", saved_vol);
418 Con_Print("CD Audio Initialized\n");
423 void CDAudio_Shutdown (void)
428 CDAudio_SysSetVolume (saved_vol);
431 CDAudio_SysShutdown();