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 typedef char filename_t[MAX_QPATH];
52 static filename_t remap[MAXTRACKS];
53 static unsigned char maxTrack;
54 static int faketrack = -1;
56 static float saved_vol = 1.0f;
59 qboolean cdValid = false;
60 qboolean cdPlaying = false;
61 qboolean cdPlayLooping = false;
62 unsigned char cdPlayTrack;
66 static void CDAudio_Eject (void)
75 static void CDAudio_CloseDoor (void)
80 CDAudio_SysCloseDoor();
83 static int CDAudio_GetAudioDiskInfo (void)
89 ret = CDAudio_SysGetAudioDiskInfo();
100 void CDAudio_Play_byName (const char *trackname, qboolean looping)
110 if(strspn(trackname, "0123456789") == strlen(trackname))
112 track = (unsigned char) atoi(trackname);
113 if(track > 0 && track < MAXTRACKS)
115 trackname = remap[track];
118 if(strspn(trackname, "0123456789") == strlen(trackname))
120 track = (unsigned char) atoi(trackname);
123 Con_Printf("CDAudio: Bad track number %u.\n", track);
130 if (cdPlaying && cdPlayTrack == track && faketrack == -1)
134 // Try playing a fake track (sound file) first
137 sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, false);
138 if (sfx == NULL || !S_IsSoundPrecached (sfx))
139 sfx = S_PrecacheSound (va ("cdtracks/track%03u.wav", track), false, false);
140 if (sfx == NULL || !S_IsSoundPrecached (sfx))
141 sfx = S_PrecacheSound (va ("cdtracks/track%02u", track), false, false);
142 if (sfx == NULL || !S_IsSoundPrecached (sfx))
143 sfx = S_PrecacheSound (va ("cdtracks/track%03u", track), false, false);
147 sfx = S_PrecacheSound (va("cdtracks/%s.wav", trackname), false, false);
148 if (sfx == NULL || !S_IsSoundPrecached (sfx))
149 sfx = S_PrecacheSound (va("cdtracks/%s", trackname), false, false);
153 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
157 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
158 S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
160 Con_Printf ("Fake CD track %u playing...\n", track);
162 Con_Printf ("BGM track %s playing...\n", trackname);
166 // If we can't play a fake CD track, try the real one
171 Con_Print("Could not load BGM track.\n");
177 CDAudio_GetAudioDiskInfo();
180 Con_Print ("No CD in player.\n");
185 if (track > maxTrack)
187 Con_Printf("CDAudio: Bad track number %u.\n", track);
191 if (CDAudio_SysPlay(track) == -1)
195 cdPlayLooping = looping;
203 void CDAudio_Play (unsigned char track, qboolean looping)
206 dpsnprintf(buf, sizeof(buf), "%d", (int) track);
207 CDAudio_Play_byName(buf, looping);
210 void CDAudio_Stop (void)
217 S_StopChannel (faketrack, true);
220 else if (cdPlaying && (CDAudio_SysStop() == -1))
227 void CDAudio_Pause (void)
229 if (!enabled || !cdPlaying)
233 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
234 else if (CDAudio_SysPause() == -1)
237 wasPlaying = cdPlaying;
242 void CDAudio_Resume (void)
244 if (!enabled || cdPlaying || !wasPlaying)
248 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
249 else if (CDAudio_SysResume() == -1)
254 static void CD_f (void)
263 command = Cmd_Argv (1);
265 if (strcasecmp(command, "remap") != 0)
268 if (strcasecmp(command, "on") == 0)
274 if (strcasecmp(command, "off") == 0)
282 if (strcasecmp(command, "reset") == 0)
287 for (n = 0; n < MAXTRACKS; n++)
288 *remap[n] = 0; // empty string, that is, unremapped
289 CDAudio_GetAudioDiskInfo();
293 if (strcasecmp(command, "remap") == 0)
295 ret = Cmd_Argc() - 2;
298 for (n = 1; n < MAXTRACKS; n++)
300 Con_Printf(" %u -> %s\n", n, remap[n]);
303 for (n = 1; n <= ret; n++)
304 strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
308 if (strcasecmp(command, "close") == 0)
314 if (strcasecmp(command, "play") == 0)
316 CDAudio_Play_byName(Cmd_Argv (2), false);
320 if (strcasecmp(command, "loop") == 0)
322 CDAudio_Play_byName(Cmd_Argv (2), true);
326 if (strcasecmp(command, "stop") == 0)
332 if (strcasecmp(command, "pause") == 0)
338 if (strcasecmp(command, "resume") == 0)
344 if (strcasecmp(command, "eject") == 0)
346 if (cdPlaying && faketrack == -1)
353 if (strcasecmp(command, "info") == 0)
355 CDAudio_GetAudioDiskInfo ();
357 Con_Printf("%u tracks on CD.\n", maxTrack);
359 Con_Print ("No CD in player.\n");
361 Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
363 Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
364 Con_Printf("Volume is %f\n", cdvolume);
368 Con_Printf("CD commands:\n");
369 Con_Printf("cd on - enables CD audio system\n");
370 Con_Printf("cd off - stops and disables CD audio system\n");
371 Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)");
372 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");
373 Con_Printf("cd close - closes CD tray\n");
374 Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
375 Con_Printf("cd play <tracknumber> - plays selected track in remapping table\n");
376 Con_Printf("cd loop <tracknumber> - plays and repeats selected track in remapping table\n");
377 Con_Printf("cd stop - stops playing current CD track\n");
378 Con_Printf("cd pause - pauses CD playback\n");
379 Con_Printf("cd resume - unpauses CD playback\n");
380 Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
383 void CDAudio_SetVolume (float newvol)
385 // If the volume hasn't changed
386 if (newvol == cdvolume)
389 // If the CD has been muted
394 // If the CD has been unmuted
395 if (cdvolume == 0.0f)
399 S_SetChannelVolume (faketrack, newvol);
400 CDAudio_SysSetVolume (newvol);
406 void CDAudio_Update (void)
411 CDAudio_SetVolume (bgmvolume.value);
417 int CDAudio_Init (void)
421 if (cls.state == ca_dedicated)
424 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
425 if (COM_CheckParm("-nocdaudio"))
430 for (i = 0; i < MAXTRACKS; i++)
433 Cvar_RegisterVariable(&cdaudioinitialized);
434 Cvar_SetValueQuick(&cdaudioinitialized, true);
437 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");
442 int CDAudio_Startup (void)
444 if (COM_CheckParm("-nocdaudio"))
447 CDAudio_SysStartup ();
449 if (CDAudio_GetAudioDiskInfo())
451 Con_Print("CDAudio_Init: No CD in player.\n");
455 saved_vol = CDAudio_SysGetVolume ();
456 if (saved_vol < 0.0f)
458 Con_Print ("Can't get initial CD volume\n");
462 Con_Printf ("Initial CD volume: %g\n", saved_vol);
466 Con_Print("CD Audio Initialized\n");
471 void CDAudio_Shutdown (void)
476 CDAudio_SysSetVolume (saved_vol);
479 CDAudio_SysShutdown();