// rights reserved.
#include "quakedef.h"
+#include "cdaudio.h"
+#include "sound.h"
+
+#define MAXTRACKS 256
// Prototypes of the system dependent functions
extern void CDAudio_SysEject (void);
extern void CDAudio_SysCloseDoor (void);
extern int CDAudio_SysGetAudioDiskInfo (void);
-extern int CDAudio_SysPlay (qbyte track);
+extern float CDAudio_SysGetVolume (void);
+extern void CDAudio_SysSetVolume (float volume);
+extern int CDAudio_SysPlay (int track);
extern int CDAudio_SysStop (void);
extern int CDAudio_SysPause (void);
extern int CDAudio_SysResume (void);
extern void CDAudio_SysShutdown (void);
// used by menu to ghost CD audio slider
-cvar_t cdaudioinitialized = {CVAR_READONLY,"cdaudioinitialized","0"};
+cvar_t cdaudioinitialized = {CVAR_READONLY,"cdaudioinitialized","0","indicates if CD Audio system is active"};
+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, 4 = play real CD tracks even instead of named fake tracks)"};
static qboolean wasPlaying = false;
static qboolean initialized = false;
static qboolean enabled = false;
static float cdvolume;
-static qbyte remap[100];
-static qbyte maxTrack;
+typedef char filename_t[MAX_QPATH];
+static filename_t remap[MAXTRACKS];
+static unsigned char maxTrack;
static int faketrack = -1;
+static float saved_vol = 1.0f;
+
// exported variables
qboolean cdValid = false;
qboolean cdPlaying = false;
qboolean cdPlayLooping = false;
-qbyte cdPlayTrack;
+unsigned char cdPlayTrack;
+cl_cdstate_t cd;
static void CDAudio_Eject (void)
{
if (!enabled)
return;
+
+ if(cdaudio.integer == 0)
+ return;
CDAudio_SysEject();
}
if (!enabled)
return;
+ if(cdaudio.integer == 0)
+ return;
+
CDAudio_SysCloseDoor();
}
cdValid = false;
+ if(cdaudio.integer == 0)
+ return -1;
+
ret = CDAudio_SysGetAudioDiskInfo();
if (ret < 1)
return -1;
return 0;
}
+qboolean CDAudio_Play_real (int track, qboolean looping, qboolean complain)
+{
+ if(track < 1)
+ {
+ if(complain)
+ Con_Print("Could not load BGM track.\n");
+ return false;
+ }
+
+ if (!cdValid)
+ {
+ CDAudio_GetAudioDiskInfo();
+ if (!cdValid)
+ {
+ if(complain)
+ Con_Print ("No CD in player.\n");
+ return false;
+ }
+ }
+
+ if (track > maxTrack)
+ {
+ if(complain)
+ Con_Printf("CDAudio: Bad track number %u.\n", track);
+ return false;
+ }
+
+ if (CDAudio_SysPlay(track) == -1)
+ return false;
+
+ if(cdaudio.integer != 3 || developer.integer)
+ Con_Printf ("CD track %u playing...\n", track);
-void CDAudio_Play (qbyte track, qboolean looping)
+ return true;
+}
+
+void CDAudio_Play_byName (const char *trackname, qboolean looping)
{
+ unsigned int track;
sfx_t* sfx;
+ Host_StartVideo();
+
if (!enabled)
return;
- track = remap[track];
- if (track < 1)
+ if(strspn(trackname, "0123456789") == strlen(trackname))
{
- Con_DPrintf("CDAudio: Bad track number %u.\n", track);
- return;
+ track = (unsigned char) atoi(trackname);
+ if(track > 0 && track < MAXTRACKS)
+ if(*remap[track])
+ trackname = remap[track];
}
- if (cdPlaying && cdPlayTrack == track)
+ if(strspn(trackname, "0123456789") == strlen(trackname))
+ {
+ track = (unsigned char) atoi(trackname);
+ if (track < 1)
+ {
+ Con_Printf("CDAudio: Bad track number %u.\n", track);
+ return;
+ }
+ }
+ else
+ track = 0;
+
+ if (cdPlaying && cdPlayTrack == track && faketrack == -1)
return;
CDAudio_Stop ();
+ if(track >= 1)
+ {
+ if(cdaudio.integer == 3) // only play real CD tracks at all
+ {
+ if(CDAudio_Play_real(track, looping, true))
+ goto success;
+ return;
+ }
+
+ if(cdaudio.integer == 2) // prefer real CD track over fake
+ {
+ if(CDAudio_Play_real(track, looping, false))
+ goto success;
+ }
+ }
+
+ if(cdaudio.integer == 4) // only play real CD tracks, EVEN instead of fake tracks!
+ {
+ if(CDAudio_Play_real(track, looping, false))
+ goto success;
+
+ if(cdValid && maxTrack > 0)
+ {
+ track = 1 + (rand() % maxTrack);
+ if(CDAudio_Play_real(track, looping, true))
+ goto success;
+ }
+ else
+ {
+ Con_Print ("No CD in player.\n");
+ }
+ return;
+ }
+
// Try playing a fake track (sound file) first
- sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false);
+ if(track >= 1)
+ {
+ sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, false);
+ if (sfx == NULL || !S_IsSoundPrecached (sfx))
+ sfx = S_PrecacheSound (va ("cdtracks/track%03u.wav", track), false, false);
+ if (sfx == NULL || !S_IsSoundPrecached (sfx))
+ sfx = S_PrecacheSound (va ("cdtracks/track%02u", track), false, false);
+ if (sfx == NULL || !S_IsSoundPrecached (sfx))
+ sfx = S_PrecacheSound (va ("cdtracks/track%03u", track), false, false);
+ }
+ else
+ {
+ sfx = S_PrecacheSound (va("cdtracks/%s.wav", trackname), false, false);
+ if (sfx == NULL || !S_IsSoundPrecached (sfx))
+ sfx = S_PrecacheSound (va("cdtracks/%s", trackname), false, false);
+ if (sfx == NULL || !S_IsSoundPrecached (sfx))
+ sfx = S_PrecacheSound (va("%s.wav", trackname), false, false);
+ if (sfx == NULL || !S_IsSoundPrecached (sfx))
+ sfx = S_PrecacheSound (va("%s", trackname), false, false);
+ }
if (sfx != NULL)
{
faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
if (faketrack != -1)
{
if (looping)
- S_LoopChannel (faketrack, true);
- Con_DPrintf ("Fake CD track %u playing...\n", track);
+ S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
+ S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
+ if(track >= 1)
+ {
+ if(cdaudio.integer != 0 || developer.integer) // we don't need these messages if only fake tracks can be played anyway
+ Con_Printf ("Fake CD track %u playing...\n", track);
+ }
+ else
+ Con_DPrintf ("BGM track %s playing...\n", trackname);
}
}
// If we can't play a fake CD track, try the real one
if (faketrack == -1)
{
- if (!cdValid)
+ if(cdaudio.integer == 0 || track < 1)
{
- CDAudio_GetAudioDiskInfo();
- if (!cdValid)
- {
- Con_Print ("No CD in player.\n");
- return;
- }
+ Con_Print("Could not load BGM track.\n");
+ return;
}
-
- if (track > maxTrack)
+ else
{
- Con_DPrintf("CDAudio: Bad track number %u.\n", track);
- return;
+ if(!CDAudio_Play_real(track, looping, true))
+ return;
}
-
- if (CDAudio_SysPlay(track) == -1)
- return;
}
+success:
cdPlayLooping = looping;
cdPlayTrack = track;
cdPlaying = true;
CDAudio_Pause ();
}
+void CDAudio_Play (int track, qboolean looping)
+{
+ char buf[20];
+ dpsnprintf(buf, sizeof(buf), "%d", (int) track);
+ CDAudio_Play_byName(buf, looping);
+}
void CDAudio_Stop (void)
{
- if (!enabled || !cdPlaying)
+ if (!enabled)
return;
if (faketrack != -1)
{
- S_StopChannel (faketrack);
+ S_StopChannel (faketrack, true);
faketrack = -1;
}
- else if (CDAudio_SysStop() == -1)
+ else if (cdPlaying && (CDAudio_SysStop() == -1))
return;
wasPlaying = false;
return;
if (faketrack != -1)
- S_PauseChannel (faketrack, true);
+ S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
else if (CDAudio_SysPause() == -1)
return;
return;
if (faketrack != -1)
- S_PauseChannel (faketrack, false);
+ S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
else if (CDAudio_SysResume() == -1)
return;
cdPlaying = true;
command = Cmd_Argv (1);
+ if (strcasecmp(command, "remap") != 0)
+ Host_StartVideo();
+
if (strcasecmp(command, "on") == 0)
{
enabled = true;
enabled = true;
if (cdPlaying)
CDAudio_Stop();
- for (n = 0; n < 100; n++)
- remap[n] = n;
+ for (n = 0; n < MAXTRACKS; n++)
+ *remap[n] = 0; // empty string, that is, unremapped
CDAudio_GetAudioDiskInfo();
return;
}
ret = Cmd_Argc() - 2;
if (ret <= 0)
{
- for (n = 1; n < 100; n++)
- if (remap[n] != n)
- Con_Printf(" %u -> %u\n", n, remap[n]);
+ for (n = 1; n < MAXTRACKS; n++)
+ if (*remap[n])
+ Con_Printf(" %u -> %s\n", n, remap[n]);
return;
}
for (n = 1; n <= ret; n++)
- remap[n] = atoi(Cmd_Argv (n+1));
+ strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
return;
}
if (strcasecmp(command, "play") == 0)
{
- CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), false);
+ CDAudio_Play_byName(Cmd_Argv (2), false);
return;
}
if (strcasecmp(command, "loop") == 0)
{
- CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), true);
+ CDAudio_Play_byName(Cmd_Argv (2), true);
return;
}
Con_Printf("Volume is %f\n", cdvolume);
return;
}
+
+ Con_Printf("CD commands:\n");
+ Con_Printf("cd on - enables CD audio system\n");
+ Con_Printf("cd off - stops and disables CD audio system\n");
+ Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)");
+ 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");
+ Con_Printf("cd close - closes CD tray\n");
+ Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
+ Con_Printf("cd play <tracknumber> - plays selected track in remapping table\n");
+ Con_Printf("cd loop <tracknumber> - plays and repeats selected track in remapping table\n");
+ Con_Printf("cd stop - stops playing current CD track\n");
+ Con_Printf("cd pause - pauses CD playback\n");
+ Con_Printf("cd resume - unpauses CD playback\n");
+ Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
}
void CDAudio_SetVolume (float newvol)
if (faketrack != -1)
S_SetChannelVolume (faketrack, newvol);
else
- {
- // TODO: add support for the "real CD" mixer
- }
+ CDAudio_SysSetVolume (newvol);
}
cdvolume = newvol;
CDAudio_SetVolume (bgmvolume.value);
- if (faketrack == -1)
+ if (faketrack == -1 && cdaudio.integer != 0)
CDAudio_SysUpdate();
}
if (cls.state == ca_dedicated)
return -1;
- if (COM_CheckParm("-nocdaudio") || COM_CheckParm("-safe"))
+// COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
+ if (COM_CheckParm("-nocdaudio"))
return -1;
CDAudio_SysInit();
- for (i = 0; i < 100; i++)
- remap[i] = i;
+ for (i = 0; i < MAXTRACKS; i++)
+ *remap[i] = 0;
+ Cvar_RegisterVariable(&cdaudio);
Cvar_RegisterVariable(&cdaudioinitialized);
Cvar_SetValueQuick(&cdaudioinitialized, true);
enabled = true;
- Cmd_AddCommand("cd", CD_f);
+ 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");
return 0;
}
int CDAudio_Startup (void)
{
+ if (COM_CheckParm("-nocdaudio"))
+ return -1;
+
CDAudio_SysStartup ();
if (CDAudio_GetAudioDiskInfo())
{
- Con_DPrint("CDAudio_Init: No CD in player.\n");
+ Con_Print("CDAudio_Init: No CD in player.\n");
cdValid = false;
}
+ saved_vol = CDAudio_SysGetVolume ();
+ if (saved_vol < 0.0f)
+ {
+ Con_Print ("Can't get initial CD volume\n");
+ saved_vol = 1.0f;
+ }
+ else
+ Con_Printf ("Initial CD volume: %g\n", saved_vol);
+
initialized = true;
- Con_DPrint("CD Audio Initialized\n");
+ Con_Print("CD Audio Initialized\n");
return 0;
}
{
if (!initialized)
return;
+
+ CDAudio_SysSetVolume (saved_vol);
+
CDAudio_Stop();
CDAudio_SysShutdown();
initialized = false;