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
26 extern HWND mainwindow;
28 qboolean cdaudioinitialized = false;
29 static qboolean cdValid = false;
30 static qboolean playing = false;
31 static qboolean wasPlaying = false;
32 static qboolean initialized = false;
33 static qboolean enabled = false;
34 static qboolean playLooping = false;
35 static float cdvolume;
36 static qbyte remap[100];
37 static qbyte playTrack;
38 static qbyte maxTrack;
43 static void CDAudio_Eject(void)
47 if ((dwReturn = mciSendCommand(wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, (DWORD)NULL)))
48 Con_DPrintf("MCI_SET_DOOR_OPEN failed (%i)\n", dwReturn);
52 static void CDAudio_CloseDoor(void)
56 if ((dwReturn = mciSendCommand(wDeviceID, MCI_SET, MCI_SET_DOOR_CLOSED, (DWORD)NULL)))
57 Con_DPrintf("MCI_SET_DOOR_CLOSED failed (%i)\n", dwReturn);
61 static int CDAudio_GetAudioDiskInfo(void)
64 MCI_STATUS_PARMS mciStatusParms;
69 mciStatusParms.dwItem = MCI_STATUS_READY;
70 dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
73 Con_DPrintf("CDAudio: drive ready test - get status failed\n");
76 if (!mciStatusParms.dwReturn)
78 Con_DPrintf("CDAudio: drive not ready\n");
82 mciStatusParms.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
83 dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
86 Con_DPrintf("CDAudio: get tracks - status failed\n");
89 if (mciStatusParms.dwReturn < 1)
91 Con_DPrintf("CDAudio: no music tracks\n");
96 maxTrack = mciStatusParms.dwReturn;
102 void CDAudio_Play(qbyte track, qboolean looping)
105 MCI_PLAY_PARMS mciPlayParms;
106 MCI_STATUS_PARMS mciStatusParms;
113 CDAudio_GetAudioDiskInfo();
118 track = remap[track];
120 if (track < 1 || track > maxTrack)
122 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
126 // don't try to play a non-audio track
127 mciStatusParms.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
128 mciStatusParms.dwTrack = track;
129 dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
132 Con_DPrintf("MCI_STATUS failed (%i)\n", dwReturn);
135 if (mciStatusParms.dwReturn != MCI_CDA_TRACK_AUDIO)
137 Con_Printf("CDAudio: track %i is not audio\n", track);
141 // get the length of the track to be played
142 mciStatusParms.dwItem = MCI_STATUS_LENGTH;
143 mciStatusParms.dwTrack = track;
144 dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
147 Con_DPrintf("MCI_STATUS failed (%i)\n", dwReturn);
153 if (playTrack == track)
158 mciPlayParms.dwFrom = MCI_MAKE_TMSF(track, 0, 0, 0);
159 mciPlayParms.dwTo = (mciStatusParms.dwReturn << 8) | track;
160 mciPlayParms.dwCallback = (DWORD)mainwindow;
161 dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_NOTIFY | MCI_FROM | MCI_TO, (DWORD)(LPVOID) &mciPlayParms);
164 Con_DPrintf("CDAudio: MCI_PLAY failed (%i)\n", dwReturn);
168 playLooping = looping;
177 void CDAudio_Stop(void)
187 if ((dwReturn = mciSendCommand(wDeviceID, MCI_STOP, 0, (DWORD)NULL)))
188 Con_DPrintf("MCI_STOP failed (%i)", dwReturn);
195 void CDAudio_Pause(void)
198 MCI_GENERIC_PARMS mciGenericParms;
206 mciGenericParms.dwCallback = (DWORD)mainwindow;
207 if ((dwReturn = mciSendCommand(wDeviceID, MCI_PAUSE, 0, (DWORD)(LPVOID) &mciGenericParms)))
208 Con_DPrintf("MCI_PAUSE failed (%i)", dwReturn);
210 wasPlaying = playing;
215 void CDAudio_Resume(void)
218 MCI_PLAY_PARMS mciPlayParms;
229 mciPlayParms.dwFrom = MCI_MAKE_TMSF(playTrack, 0, 0, 0);
230 mciPlayParms.dwTo = MCI_MAKE_TMSF(playTrack + 1, 0, 0, 0);
231 mciPlayParms.dwCallback = (DWORD)mainwindow;
232 dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_TO | MCI_NOTIFY, (DWORD)(LPVOID) &mciPlayParms);
235 Con_DPrintf("CDAudio: MCI_PLAY failed (%i)\n", dwReturn);
242 static void CD_f (void)
251 command = Cmd_Argv (1);
253 if (Q_strcasecmp(command, "on") == 0)
259 if (Q_strcasecmp(command, "off") == 0)
267 if (Q_strcasecmp(command, "reset") == 0)
272 for (n = 0; n < 100; n++)
274 CDAudio_GetAudioDiskInfo();
278 if (Q_strcasecmp(command, "remap") == 0)
280 ret = Cmd_Argc() - 2;
283 for (n = 1; n < 100; n++)
285 Con_Printf(" %u -> %u\n", n, remap[n]);
288 for (n = 1; n <= ret; n++)
289 remap[n] = atoi(Cmd_Argv (n+1));
293 if (Q_strcasecmp(command, "close") == 0)
301 CDAudio_GetAudioDiskInfo();
304 Con_Printf("No CD in player.\n");
309 if (Q_strcasecmp(command, "play") == 0)
311 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), false);
315 if (Q_strcasecmp(command, "loop") == 0)
317 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), true);
321 if (Q_strcasecmp(command, "stop") == 0)
327 if (Q_strcasecmp(command, "pause") == 0)
333 if (Q_strcasecmp(command, "resume") == 0)
339 if (Q_strcasecmp(command, "eject") == 0)
348 if (Q_strcasecmp(command, "info") == 0)
350 Con_Printf("%u tracks\n", maxTrack);
352 Con_Printf("Currently %s track %u\n", playLooping ? "looping" : "playing", playTrack);
354 Con_Printf("Paused %s track %u\n", playLooping ? "looping" : "playing", playTrack);
355 Con_Printf("Volume is %f\n", cdvolume);
361 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
363 if (lParam != wDeviceID)
368 case MCI_NOTIFY_SUCCESSFUL:
373 CDAudio_Play(playTrack, true);
377 case MCI_NOTIFY_ABORTED:
378 case MCI_NOTIFY_SUPERSEDED:
381 case MCI_NOTIFY_FAILURE:
382 Con_DPrintf("MCI_NOTIFY_FAILURE\n");
388 Con_DPrintf("Unexpected MM_MCINOTIFY type (%i)\n", wParam);
396 void CDAudio_Update(void)
401 if (bgmvolume.value != cdvolume)
405 Cvar_SetValueQuick (&bgmvolume, 0.0);
406 cdvolume = bgmvolume.value;
411 Cvar_SetValueQuick (&bgmvolume, 1.0);
412 cdvolume = bgmvolume.value;
419 int CDAudio_Init(void)
422 MCI_OPEN_PARMS mciOpenParms;
423 MCI_SET_PARMS mciSetParms;
426 if (cls.state == ca_dedicated)
429 if (COM_CheckParm("-nocdaudio") || COM_CheckParm("-safe"))
432 mciOpenParms.lpstrDeviceType = "cdaudio";
433 if ((dwReturn = mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_SHAREABLE, (DWORD) (LPVOID) &mciOpenParms)))
435 Con_Printf("CDAudio_Init: MCI_OPEN failed (%i)\n", dwReturn);
438 wDeviceID = mciOpenParms.wDeviceID;
440 // Set the time format to track/minute/second/frame (TMSF).
441 mciSetParms.dwTimeFormat = MCI_FORMAT_TMSF;
442 if ((dwReturn = mciSendCommand(wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD)(LPVOID) &mciSetParms)))
444 Con_Printf("MCI_SET_TIME_FORMAT failed (%i)\n", dwReturn);
445 mciSendCommand(wDeviceID, MCI_CLOSE, 0, (DWORD)NULL);
449 for (n = 0; n < 100; n++)
451 cdaudioinitialized = true;
455 if (CDAudio_GetAudioDiskInfo())
457 Con_Printf("CDAudio_Init: No CD in player.\n");
461 Cmd_AddCommand ("cd", CD_f);
463 Con_Printf("CD Audio Initialized\n");
469 void CDAudio_Shutdown(void)
474 if (mciSendCommand(wDeviceID, MCI_CLOSE, MCI_WAIT, (DWORD)NULL))
475 Con_DPrintf("CDAudio_Shutdown: MCI_CLOSE failed\n");