Debian Codegraph @

common/arch/win32/rbaudio.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 /*
8 *
9 * CD Audio functions for Win32
10 *
11 *
12 */
13
14 #include <algorithm>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <windows.h>
18 #include <mmsystem.h>
19 #include <climits>
20
21 #include "pstypes.h"
22 #include "dxxerror.h"
23 #include "args.h"
24 #include "rbaudio.h"
25 #include "console.h"
26 #include "fwd-gr.h"
27 #include "timer.h"
28
29 #if SDL_MAJOR_VERSION == 1
30 // we don't want this on SDL1 because there we have common/arch/sdl/rbaudio.cpp
31 // #pragma message("Skipping win32/rbaudio because of SDL1")
32
33 #elif SDL_MAJOR_VERSION == 2
34
35 namespace dcx {
36 #define CD_FPS 75
37
38 static UINT wCDDeviceID = 0U;
39 static bool initialised;
40 static DWORD playEnd;
41 static DWORD lastFrames;
42 static bool isPaused;
43
44 void RBAExit()
45 {
46 if (wCDDeviceID)
47 {
48 initialised = false;
49 mciSendCommand(wCDDeviceID, MCI_CLOSE, MCI_WAIT, 0);
50 wCDDeviceID = 0U;
51 }
52 }
53
54 static bool mci_HasMedia()
55 {
56 if (!wCDDeviceID) return false;
57
58 MCI_STATUS_PARMS mciStatusParms;
59
60 mciStatusParms.dwItem = MCI_STATUS_MEDIA_PRESENT;
61 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_STATUS, MCI_STATUS_ITEM, reinterpret_cast<DWORD_PTR>(&mciStatusParms))})
62 {
63 Warning("RBAudio win32/MCI: cannot determine MCI media status (%lx)", mciError);
64 RBAExit();
65 return false;
66 }
67 return mciStatusParms.dwReturn != 0;
68 }
69
70 static unsigned mci_TotalFramesMsf(const DWORD msf)
71 {
72 return (MCI_MSF_MINUTE(msf) * 60 + MCI_MSF_SECOND(msf)) * CD_FPS + MCI_MSF_FRAME(msf);
73 }
74
75 static unsigned mci_FramesToMsf(const int frames)
76 {
77 int m = frames / (CD_FPS * 60);
78 int s = (frames / CD_FPS) % 60;
79 int f = frames % CD_FPS;
80 return MCI_MAKE_MSF(m, s, f);
81 }
82
83 static unsigned mci_GetTrackOffset(const int track)
84 {
85 MCI_STATUS_PARMS mciStatusParms;
86
87 mciStatusParms.dwItem = MCI_STATUS_POSITION;
88 mciStatusParms.dwTrack = track;
89 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK, reinterpret_cast<DWORD_PTR>(&mciStatusParms))})
90 {
91 Warning("RBAudio win32/MCI: cannot determine track %i offset (%lx)", track, mciError);
92 return -1;
93 }
94 // dwReturn is a 32-bit value in MSF format, so DWORD_PTR > DWORD is not a problem
95 return mci_TotalFramesMsf(mciStatusParms.dwReturn);
96 }
97
98 static unsigned mci_GetTrackLength(const int track)
99 {
100 MCI_STATUS_PARMS mciStatusParms;
101
102 mciStatusParms.dwItem = MCI_STATUS_LENGTH;
103 mciStatusParms.dwTrack = track;
104 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK, reinterpret_cast<DWORD_PTR>(&mciStatusParms))})
105 {
106 Warning("RBAudio win32/MCI: cannot determine track %i length (%lx)", track, mciError);
107 return -1;
108 }
109 // dwReturn is a 32-bit value in MSF format, so DWORD_PTR > DWORD is not a problem
110 return mci_TotalFramesMsf(mciStatusParms.dwReturn);
111 }
112
113 static unsigned mci_GetTotalLength()
114 {
115 MCI_STATUS_PARMS mciStatusParms;
116
117 mciStatusParms.dwItem = MCI_STATUS_LENGTH;
118 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_STATUS, MCI_STATUS_ITEM, reinterpret_cast<DWORD_PTR>(&mciStatusParms))})
119 {
120 Warning("RBAudio win32/MCI: cannot determine media length (%lx)", mciError);
121 return -1;
122 }
123 return mci_TotalFramesMsf(mciStatusParms.dwReturn);
124 }
125
126 void RBAInit()
127 {
128 MCI_OPEN_PARMS mciOpenParms;
129 MCI_SET_PARMS mciSetParms;
130
131 if (initialised) return;
132
133 mciOpenParms.lpstrDeviceType = "cdaudio";
134 if (const MCIERROR mciError{mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_SHAREABLE, reinterpret_cast<DWORD_PTR>(&mciOpenParms))})
135 {
136 con_puts(CON_NORMAL, "RBAudio win32/MCI: cannot find MCI cdaudio (no CD drive?)");
137 return;
138 }
139
140 wCDDeviceID = mciOpenParms.wDeviceID;
141
142 if (!mci_HasMedia())
143 {
144 con_puts(CON_NORMAL, "RBAudio win32/MCI: no media in CD drive.");
145 RBAExit();
146 return;
147 }
148
149 mciSetParms.dwTimeFormat = MCI_FORMAT_MSF;
150 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, reinterpret_cast<DWORD_PTR>(&mciSetParms))})
151 {
152 Warning_puts("RBAudio win32/MCI: cannot set time format for CD to MSF (strange)");
153 RBAExit();
154 return;
155 }
156
157 initialised = true;
158 RBAList();
159 }
160
161 int RBAEnabled()
162 {
163 return initialised;
164 }
165
166 static void (*redbook_finished_hook)() = nullptr;
167
168 int RBAPlayTrack(int a)
169 {
170 if (!wCDDeviceID)
171 return 0;
172
173 if (mci_HasMedia())
174 {
175 MCI_PLAY_PARMS mciPlayParms;
176 DWORD playStart;
177
178 con_printf(CON_VERBOSE, "RBAudio win32/MCI: Playing track %i", a);
179
180 playStart = mci_GetTrackOffset(a);
181 playEnd = playStart + mci_GetTrackLength(a);
182
183 mciPlayParms.dwFrom = mci_FramesToMsf(playStart);
184 mciPlayParms.dwTo = mci_FramesToMsf(playEnd);
185
186 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_PLAY, MCI_FROM | MCI_TO, reinterpret_cast<DWORD_PTR>(&mciPlayParms))})
187 {
188 Warning("RBAudio win32/MCI: could not play track (%lx)", mciError);
189 return 0;
190 }
191
192 return 1;
193 }
194 return 0;
195 }
196
197 // plays tracks first through last, inclusive
198 int RBAPlayTracks(int first, int last, void (*hook_finished)(void))
199 {
200 if (!wCDDeviceID)
201 return 0;
202
203 if (mci_HasMedia())
204 {
205 MCI_PLAY_PARMS mciPlayParms;
206
207 redbook_finished_hook = hook_finished;
208
209 con_printf(CON_VERBOSE, "RBAudio win32/MCI: Playing tracks %i to %i", first, last);
210
211 playEnd = mci_GetTrackOffset(last) + mci_GetTrackLength(last);
212
213 mciPlayParms.dwFrom = mci_FramesToMsf(mci_GetTrackOffset(first));
214 mciPlayParms.dwTo = mci_FramesToMsf(playEnd);
215
216 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_PLAY, MCI_FROM | MCI_TO, reinterpret_cast<DWORD_PTR>(&mciPlayParms))})
217 {
218 Warning("RBAudio win32/MCI: could not play tracks (%lx)", mciError);
219 return 0;
220 }
221
222 return 1;
223 }
224 return 0;
225 }
226
227 void RBAStop()
228 {
229 if (!wCDDeviceID) return;
230
231 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_STOP, 0, 0)})
232 {
233 Warning("RBAudio win32/MCI: could not stop music (%lx)", mciError);
234 }
235 redbook_finished_hook = nullptr;
236 }
237
238 void RBAEjectDisk()
239 {
240 if (!wCDDeviceID) return;
241
242 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_SET, MCI_SET_DOOR_OPEN | MCI_WAIT, 0)})
243 {
244 Warning("RBAudio win32/MCI: could not open CD tray (%lx)", mciError);
245 }
246 initialised = false;
247 }
248
249 void RBASetVolume(int)
250 {
251 // MCI does not support this
252 }
253
254 void RBAPause()
255 {
256 if (!wCDDeviceID) return;
257
258 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_PAUSE, 0, 0)})
259 {
260 Warning("RBAudio win32/MCI: could not pause music (%lx)", mciError);
261 return;
262 }
263 con_puts(CON_VERBOSE, "RBAudio win32/MCI: Playback paused");
264 isPaused = true;
265 }
266
267 int RBAResume()
268 {
269 if (!wCDDeviceID) return -1;
270
271 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_RESUME, 0, 0)})
272 {
273 Warning("RBAudio win32/MCI: could not resume music (%lx)", mciError);
274 return -1;
275 }
276 con_puts(CON_VERBOSE, "RBAudio win32/MCI: Playback resumed");
277 isPaused = false;
278 return 1;
279 }
280
281 int RBAPauseResume()
282 {
283 if (!wCDDeviceID) return 0;
284 MCI_STATUS_PARMS mciStatusParms;
285
286 mciStatusParms.dwItem = MCI_STATUS_MODE;
287 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_STATUS, MCI_STATUS_ITEM, reinterpret_cast<DWORD_PTR>(&mciStatusParms))})
288 {
289 Warning("RBAudio win32/MCI: cannot determine MCI media status (%lx)", mciError);
290 return 0;
291 }
292
293 if (mciStatusParms.dwReturn == MCI_MODE_PLAY)
294 {
295 con_puts(CON_VERBOSE, "RBAudio win32/MCI: Toggle Playback pause");
296 RBAPause();
297 }
298 // MCI may also use MCI_MODE_STOP for pause instead of MCI_MODE_PAUSE
299 else if (mciStatusParms.dwReturn == MCI_MODE_PAUSE || (isPaused && mciStatusParms.dwReturn == MCI_MODE_STOP))
300 {
301 con_puts(CON_VERBOSE, "RBAudio win32/MCI: Toggle Playback resume");
302 return RBAResume() > 0;
303 }
304 else
305 return 0;
306
307 return 1;
308 }
309
310 int RBAGetNumberOfTracks()
311 {
312 if (!wCDDeviceID) return -1;
313 MCI_STATUS_PARMS mciStatusParms;
314
315 mciStatusParms.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
316 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_STATUS, MCI_STATUS_ITEM, reinterpret_cast<DWORD_PTR>(&mciStatusParms))})
317 {
318 Warning("RBAudio win32/MCI: could not get track count (%lx)", mciError);
319 return -1;
320 }
321
322 return static_cast<int>(mciStatusParms.dwReturn);
323 }
324
325 // check if we need to call the 'finished' hook
326 // needs to go in all event loops
327 // MCI has a hook function via Win32 messages, but it doesn't work for CDs
328 // for whatever reason
329 void RBACheckFinishedHook()
330 {
331 static fix64 last_check_time = 0;
332
333 if (!wCDDeviceID) return;
334
335 if ((timer_query() - last_check_time) >= F2_0)
336 {
337 MCI_STATUS_PARMS mciStatusParms;
338
339 mciStatusParms.dwItem = MCI_STATUS_POSITION;
340 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_STATUS, MCI_STATUS_ITEM, reinterpret_cast<DWORD_PTR>(&mciStatusParms))})
341 {
342 Warning("RBAudio win32/MCI: cannot determine MCI position (%lx)", mciError);
343 return;
344 }
345
346 // time for a hack. for some reason, MCI sometimes stops
347 // from around a few to few dozen frames before it should,
348 // so we will check if we haven't moved from the last check
349 // and allow a bit of a leeway when checking if so.
350
351 DWORD checkValue = playEnd;
352 // dwReturn is a 32-bit value in MSF format, so DWORD_PTR > DWORD is not a problem
353 DWORD thisFrames = mci_TotalFramesMsf(mciStatusParms.dwReturn);
354
355 if (thisFrames == lastFrames)
356 checkValue = checkValue < 64 ? 0 : checkValue - 64; // prevent underflow
357
358 if (redbook_finished_hook && playEnd > 0 && thisFrames >= checkValue)
359 {
360 con_puts(CON_VERBOSE, "RBAudio win32/MCI: Playback done, calling finished-hook");
361 redbook_finished_hook();
362 }
363 lastFrames = thisFrames;
364 last_check_time = timer_query();
365 }
366 }
367
368 // return the track number currently playing. Useful if RBAPlayTracks()
369 // is called. Returns 0 if no track playing, else track number
370 int RBAGetTrackNum()
371 {
372 if (!wCDDeviceID) return 0;
373 MCI_STATUS_PARMS mciStatusParms;
374
375 mciStatusParms.dwItem = MCI_STATUS_MODE;
376 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_STATUS, MCI_STATUS_ITEM, reinterpret_cast<DWORD_PTR>(&mciStatusParms))})
377 {
378 Warning("RBAudio win32/MCI: cannot determine MCI media status (%lx)", mciError);
379 return 0;
380 }
381
382 if (mciStatusParms.dwReturn != MCI_MODE_PLAY)
383 return 0;
384
385 mciStatusParms.dwItem = MCI_STATUS_CURRENT_TRACK;
386 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_STATUS, MCI_STATUS_ITEM, reinterpret_cast<DWORD_PTR>(&mciStatusParms))})
387 {
388 Warning("RBAudio win32/MCI: cannot determine MCI track number (%lx)", mciError);
389 return 0;
390 }
391
392 return static_cast<int>(mciStatusParms.dwReturn);
393 }
394
395 int RBAPeekPlayStatus()
396 {
397 if (!wCDDeviceID) return 0;
398 MCI_STATUS_PARMS mciStatusParms;
399
400 mciStatusParms.dwItem = MCI_STATUS_MODE;
401 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_STATUS, MCI_STATUS_ITEM, reinterpret_cast<DWORD_PTR>(&mciStatusParms))})
402 {
403 Warning("RBAudio win32/MCI: cannot determine MCI media status (%lx)", mciError);
404 return 0;
405 }
406
407 if (mciStatusParms.dwReturn == MCI_MODE_PLAY)
408 return 1;
409 // MCI may also use MCI_MODE_STOP for pause instead of MCI_MODE_PAUSE
410 else if (mciStatusParms.dwReturn == MCI_MODE_PAUSE || (isPaused && mciStatusParms.dwReturn == MCI_MODE_STOP))
411 return -1; // hack so it doesn't keep restarting paused music; -1 is still truthy, unlike 0
412 else
413 return 0;
414 }
415
416 static int cddb_sum(int n)
417 {
418 int ret;
419
420 /* For backward compatibility this algorithm must not change */
421
422 ret = 0;
423
424 while (n > 0) {
425 ret = ret + (n % 10);
426 n = n / 10;
427 }
428
429 return (ret);
430 }
431
432 unsigned long RBAGetDiscID()
433 {
434 int i, t = 0, n = 0, trackCount, totalLength;
435
436 if (!wCDDeviceID)
437 return 0;
438
439 trackCount = RBAGetNumberOfTracks();
440 totalLength = mci_GetTotalLength();
441 if (totalLength < 0)
442 {
443 Warning_puts("RBAudio win32/MCI: cannot determine total length");
444 return 0;
445 }
446
447 /* For backward compatibility this algorithm must not change */
448
449 i = 1;
450
451 while (i <= trackCount)
452 {
453 int offset = mci_GetTrackOffset(i);
454 if (offset < 0)
455 {
456 Warning("RBAudio win32/MCI: cannot determine track %i offset", i);
457 return 0;
458 }
459 n += cddb_sum(offset / CD_FPS);
460 i++;
461 }
462
463 t = totalLength / CD_FPS;
464
465 return ((n % 0xff) << 24 | t << 8 | trackCount);
466 }
467
468 void RBAList(void)
469 {
470 if (!wCDDeviceID) return;
471
472 MCI_STATUS_PARMS mciStatusParms;
473 int trackCount = RBAGetNumberOfTracks();
474
475 for (int i = 1; i <= trackCount; ++i) {
476 int isAudioTrack, length, offset;
477 mciStatusParms.dwTrack = i;
478
479 mciStatusParms.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
480 if (const MCIERROR mciError{mciSendCommand(wCDDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK, reinterpret_cast<DWORD_PTR>(&mciStatusParms))})
481 {
482 Warning("RBAudio win32/MCI: cannot determine track %d type (%lx)", i, mciError);
483 continue;
484 }
485 isAudioTrack = mciStatusParms.dwReturn == MCI_CDA_TRACK_AUDIO;
486
487 offset = mci_GetTrackOffset(i);
488 if (offset < 0)
489 {
490 continue;
491 }
492
493 length = mci_GetTrackLength(i);
494 if (length < 0)
495 {
496 continue;
497 }
498
499 con_printf(CON_VERBOSE, "RBAudio win32/MCI: CD track %d, type %s, length %d, offset %d", i, isAudioTrack ? "audio" : "data", length, offset);
500 }
501 }
502
503 }
504
505 #endif