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
;
27 extern cvar_t bgmvolume
;
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 byte remap
[100];
38 static byte playTrack
;
44 static void CDAudio_Eject(void)
48 if (dwReturn
= mciSendCommand(wDeviceID
, MCI_SET
, MCI_SET_DOOR_OPEN
, (DWORD
)NULL
))
49 Con_DPrintf("MCI_SET_DOOR_OPEN failed (%i)\n", dwReturn
);
53 static void CDAudio_CloseDoor(void)
57 if (dwReturn
= mciSendCommand(wDeviceID
, MCI_SET
, MCI_SET_DOOR_CLOSED
, (DWORD
)NULL
))
58 Con_DPrintf("MCI_SET_DOOR_CLOSED failed (%i)\n", dwReturn
);
62 static int CDAudio_GetAudioDiskInfo(void)
65 MCI_STATUS_PARMS mciStatusParms
;
70 mciStatusParms
.dwItem
= MCI_STATUS_READY
;
71 dwReturn
= mciSendCommand(wDeviceID
, MCI_STATUS
, MCI_STATUS_ITEM
| MCI_WAIT
, (DWORD
) (LPVOID
) &mciStatusParms
);
74 Con_DPrintf("CDAudio: drive ready test - get status failed\n");
77 if (!mciStatusParms
.dwReturn
)
79 Con_DPrintf("CDAudio: drive not ready\n");
83 mciStatusParms
.dwItem
= MCI_STATUS_NUMBER_OF_TRACKS
;
84 dwReturn
= mciSendCommand(wDeviceID
, MCI_STATUS
, MCI_STATUS_ITEM
| MCI_WAIT
, (DWORD
) (LPVOID
) &mciStatusParms
);
87 Con_DPrintf("CDAudio: get tracks - status failed\n");
90 if (mciStatusParms
.dwReturn
< 1)
92 Con_DPrintf("CDAudio: no music tracks\n");
97 maxTrack
= mciStatusParms
.dwReturn
;
103 void CDAudio_Play(byte track
, qboolean looping
)
106 MCI_PLAY_PARMS mciPlayParms
;
107 MCI_STATUS_PARMS mciStatusParms
;
114 CDAudio_GetAudioDiskInfo();
119 track
= remap
[track
];
121 if (track
< 1 || track
> maxTrack
)
123 Con_DPrintf("CDAudio: Bad track number %u.\n", track
);
127 // don't try to play a non-audio track
128 mciStatusParms
.dwItem
= MCI_CDA_STATUS_TYPE_TRACK
;
129 mciStatusParms
.dwTrack
= track
;
130 dwReturn
= mciSendCommand(wDeviceID
, MCI_STATUS
, MCI_STATUS_ITEM
| MCI_TRACK
| MCI_WAIT
, (DWORD
) (LPVOID
) &mciStatusParms
);
133 Con_DPrintf("MCI_STATUS failed (%i)\n", dwReturn
);
136 if (mciStatusParms
.dwReturn
!= MCI_CDA_TRACK_AUDIO
)
138 Con_Printf("CDAudio: track %i is not audio\n", track
);
142 // get the length of the track to be played
143 mciStatusParms
.dwItem
= MCI_STATUS_LENGTH
;
144 mciStatusParms
.dwTrack
= track
;
145 dwReturn
= mciSendCommand(wDeviceID
, MCI_STATUS
, MCI_STATUS_ITEM
| MCI_TRACK
| MCI_WAIT
, (DWORD
) (LPVOID
) &mciStatusParms
);
148 Con_DPrintf("MCI_STATUS failed (%i)\n", dwReturn
);
154 if (playTrack
== track
)
159 mciPlayParms
.dwFrom
= MCI_MAKE_TMSF(track
, 0, 0, 0);
160 mciPlayParms
.dwTo
= (mciStatusParms
.dwReturn
<< 8) | track
;
161 mciPlayParms
.dwCallback
= (DWORD
)mainwindow
;
162 dwReturn
= mciSendCommand(wDeviceID
, MCI_PLAY
, MCI_NOTIFY
| MCI_FROM
| MCI_TO
, (DWORD
)(LPVOID
) &mciPlayParms
);
165 Con_DPrintf("CDAudio: MCI_PLAY failed (%i)\n", dwReturn
);
169 playLooping
= looping
;
178 void CDAudio_Stop(void)
188 if (dwReturn
= mciSendCommand(wDeviceID
, MCI_STOP
, 0, (DWORD
)NULL
))
189 Con_DPrintf("MCI_STOP failed (%i)", dwReturn
);
196 void CDAudio_Pause(void)
199 MCI_GENERIC_PARMS mciGenericParms
;
207 mciGenericParms
.dwCallback
= (DWORD
)mainwindow
;
208 if (dwReturn
= mciSendCommand(wDeviceID
, MCI_PAUSE
, 0, (DWORD
)(LPVOID
) &mciGenericParms
))
209 Con_DPrintf("MCI_PAUSE failed (%i)", dwReturn
);
211 wasPlaying
= playing
;
216 void CDAudio_Resume(void)
219 MCI_PLAY_PARMS mciPlayParms
;
230 mciPlayParms
.dwFrom
= MCI_MAKE_TMSF(playTrack
, 0, 0, 0);
231 mciPlayParms
.dwTo
= MCI_MAKE_TMSF(playTrack
+ 1, 0, 0, 0);
232 mciPlayParms
.dwCallback
= (DWORD
)mainwindow
;
233 dwReturn
= mciSendCommand(wDeviceID
, MCI_PLAY
, MCI_TO
| MCI_NOTIFY
, (DWORD
)(LPVOID
) &mciPlayParms
);
236 Con_DPrintf("CDAudio: MCI_PLAY failed (%i)\n", dwReturn
);
243 static void CD_f (void)
253 command
= Cmd_Argv (1);
255 if (Q_strcasecmp(command
, "on") == 0)
261 if (Q_strcasecmp(command
, "off") == 0)
269 if (Q_strcasecmp(command
, "reset") == 0)
274 for (n
= 0; n
< 100; n
++)
276 CDAudio_GetAudioDiskInfo();
280 if (Q_strcasecmp(command
, "remap") == 0)
282 ret
= Cmd_Argc() - 2;
285 for (n
= 1; n
< 100; n
++)
287 Con_Printf(" %u -> %u\n", n
, remap
[n
]);
290 for (n
= 1; n
<= ret
; n
++)
291 remap
[n
] = Q_atoi(Cmd_Argv (n
+1));
295 if (Q_strcasecmp(command
, "close") == 0)
303 CDAudio_GetAudioDiskInfo();
306 Con_Printf("No CD in player.\n");
311 if (Q_strcasecmp(command
, "play") == 0)
313 CDAudio_Play((byte
)Q_atoi(Cmd_Argv (2)), false);
317 if (Q_strcasecmp(command
, "loop") == 0)
319 CDAudio_Play((byte
)Q_atoi(Cmd_Argv (2)), true);
323 if (Q_strcasecmp(command
, "stop") == 0)
329 if (Q_strcasecmp(command
, "pause") == 0)
335 if (Q_strcasecmp(command
, "resume") == 0)
341 if (Q_strcasecmp(command
, "eject") == 0)
350 if (Q_strcasecmp(command
, "info") == 0)
352 Con_Printf("%u tracks\n", maxTrack
);
354 Con_Printf("Currently %s track %u\n", playLooping
? "looping" : "playing", playTrack
);
356 Con_Printf("Paused %s track %u\n", playLooping
? "looping" : "playing", playTrack
);
357 Con_Printf("Volume is %f\n", cdvolume
);
363 LONG
CDAudio_MessageHandler(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
365 if (lParam
!= wDeviceID
)
370 case MCI_NOTIFY_SUCCESSFUL
:
375 CDAudio_Play(playTrack
, true);
379 case MCI_NOTIFY_ABORTED
:
380 case MCI_NOTIFY_SUPERSEDED
:
383 case MCI_NOTIFY_FAILURE
:
384 Con_DPrintf("MCI_NOTIFY_FAILURE\n");
390 Con_DPrintf("Unexpected MM_MCINOTIFY type (%i)\n", wParam
);
398 void CDAudio_Update(void)
403 if (bgmvolume
.value
!= cdvolume
)
407 Cvar_SetValue ("bgmvolume", 0.0);
408 cdvolume
= bgmvolume
.value
;
413 Cvar_SetValue ("bgmvolume", 1.0);
414 cdvolume
= bgmvolume
.value
;
421 int CDAudio_Init(void)
424 MCI_OPEN_PARMS mciOpenParms
;
425 MCI_SET_PARMS mciSetParms
;
428 if (cls
.state
== ca_dedicated
)
431 if (COM_CheckParm("-nocdaudio"))
434 mciOpenParms
.lpstrDeviceType
= "cdaudio";
435 if (dwReturn
= mciSendCommand(0, MCI_OPEN
, MCI_OPEN_TYPE
| MCI_OPEN_SHAREABLE
, (DWORD
) (LPVOID
) &mciOpenParms
))
437 Con_Printf("CDAudio_Init: MCI_OPEN failed (%i)\n", dwReturn
);
440 wDeviceID
= mciOpenParms
.wDeviceID
;
442 // Set the time format to track/minute/second/frame (TMSF).
443 mciSetParms
.dwTimeFormat
= MCI_FORMAT_TMSF
;
444 if (dwReturn
= mciSendCommand(wDeviceID
, MCI_SET
, MCI_SET_TIME_FORMAT
, (DWORD
)(LPVOID
) &mciSetParms
))
446 Con_Printf("MCI_SET_TIME_FORMAT failed (%i)\n", dwReturn
);
447 mciSendCommand(wDeviceID
, MCI_CLOSE
, 0, (DWORD
)NULL
);
451 for (n
= 0; n
< 100; n
++)
456 if (CDAudio_GetAudioDiskInfo())
458 Con_Printf("CDAudio_Init: No CD in player.\n");
462 Cmd_AddCommand ("cd", CD_f
);
464 Con_Printf("CD Audio Initialized\n");
470 void CDAudio_Shutdown(void)
475 if (mciSendCommand(wDeviceID
, MCI_CLOSE
, MCI_WAIT
, (DWORD
)NULL
))
476 Con_DPrintf("CDAudio_Shutdown: MCI_CLOSE failed\n");