| 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 |
* SDL CD Audio functions |
| 10 |
* |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "dxxsconf.h" |
| 15 |
#include <algorithm> |
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <ranges> |
| 19 |
|
| 20 |
#include <SDL.h> |
| 21 |
|
| 22 |
#ifdef __linux__ |
| 23 |
#include <sys/ioctl.h> |
| 24 |
#include <linux/cdrom.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "pstypes.h" |
| 28 |
#include "dxxerror.h" |
| 29 |
#include "args.h" |
| 30 |
#include "rbaudio.h" |
| 31 |
#include "console.h" |
| 32 |
#include "timer.h" |
| 33 |
#include "partial_range.h" |
| 34 |
#include "compiler-range_for.h" |
| 35 |
|
| 36 |
#define DXX_CHECK_CD_INDRIVE_0(S) \ |
| 37 |
static_assert(!CD_INDRIVE(S), #S) |
| 38 |
#define DXX_CHECK_CD_INDRIVE_1(S) \ |
| 39 |
static_assert(CD_INDRIVE(S), #S) |
| 40 |
#undef CD_INDRIVE |
| 41 |
#define CD_INDRIVE(s) (static_cast<int>(s) > 0) |
| 42 |
DXX_CHECK_CD_INDRIVE_0(CD_ERROR); |
| 43 |
DXX_CHECK_CD_INDRIVE_0(CD_TRAYEMPTY); |
| 44 |
DXX_CHECK_CD_INDRIVE_1(CD_STOPPED); |
| 45 |
DXX_CHECK_CD_INDRIVE_1(CD_PLAYING); |
| 46 |
DXX_CHECK_CD_INDRIVE_1(CD_PAUSED); |
| 47 |
|
| 48 |
namespace dcx { |
| 49 |
|
| 50 |
#define REDBOOK_VOLUME_SCALE 255 |
| 51 |
|
| 52 |
static SDL_CD *s_cd = NULL; |
| 53 |
static int initialised = 0; |
| 54 |
|
| 55 |
void RBAExit() |
| 56 |
{ |
| 57 |
if (s_cd) |
| 58 |
{ |
| 59 |
SDL_CDStop(s_cd); |
| 60 |
SDL_CDClose(s_cd); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
void RBAInit() |
| 65 |
{ |
| 66 |
int num_cds; |
| 67 |
if (initialised) return; |
| 68 |
|
| 69 |
if (SDL_Init(SDL_INIT_CDROM) < 0) |
| 70 |
{ |
| 71 |
Warning("RBAudio: SDL library initialisation failed: %s.",SDL_GetError()); |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
num_cds = SDL_CDNumDrives(); |
| 76 |
if (num_cds < 1) |
| 77 |
{ |
| 78 |
con_puts(CON_NORMAL, "RBAudio: No cdrom drives found!"); |
| 79 |
#if defined(__APPLE__) || defined(macintosh) |
| 80 |
SDL_QuitSubSystem(SDL_INIT_CDROM); // necessary for rescanning CDROMs |
| 81 |
#endif |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
for (int i = 0; i < num_cds; i++) |
| 86 |
{ |
| 87 |
if (s_cd) |
| 88 |
SDL_CDClose(s_cd); |
| 89 |
s_cd = SDL_CDOpen(i); |
| 90 |
|
| 91 |
if (s_cd && CD_INDRIVE(SDL_CDStatus(s_cd))) |
| 92 |
{ |
| 93 |
const auto &&r = partial_const_range(s_cd->track, static_cast<unsigned>(s_cd->numtracks)); |
| 94 |
if (std::ranges::find(r, SDL_AUDIO_TRACK, &SDL_CDtrack::type) != r.end()) |
| 95 |
{ |
| 96 |
initialised = 1; |
| 97 |
RBAList(); |
| 98 |
return; // we've found an audio CD |
| 99 |
} |
| 100 |
} |
| 101 |
else if (s_cd == NULL) |
| 102 |
Warning("RBAudio: Could not open cdrom %i for redbook audio: %s", i, SDL_GetError()); |
| 103 |
} |
| 104 |
|
| 105 |
{ |
| 106 |
con_puts(CON_NORMAL, "RBAudio: No audio CDs found"); |
| 107 |
if (s_cd) // if there's no audio CD, say that there's no redbook and hence play MIDI instead |
| 108 |
{ |
| 109 |
SDL_CDClose(s_cd); |
| 110 |
s_cd = NULL; |
| 111 |
} |
| 112 |
#if defined(__APPLE__) || defined(macintosh) |
| 113 |
SDL_QuitSubSystem(SDL_INIT_CDROM); // necessary for rescanning CDROMs |
| 114 |
#endif |
| 115 |
return; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
int RBAEnabled() |
| 120 |
{ |
| 121 |
return initialised; |
| 122 |
} |
| 123 |
|
| 124 |
int RBAPlayTrack(int a) |
| 125 |
{ |
| 126 |
if (!s_cd) return -1; |
| 127 |
|
| 128 |
if (CD_INDRIVE(SDL_CDStatus(s_cd)) ) { |
| 129 |
if (SDL_CDPlayTracks(s_cd, a-1, 0, 0, 0) == 0) |
| 130 |
{ |
| 131 |
con_printf(CON_VERBOSE, "RBAudio: Playing track %i", a); |
| 132 |
return a; |
| 133 |
} |
| 134 |
} |
| 135 |
return -1; |
| 136 |
} |
| 137 |
|
| 138 |
static void (*redbook_finished_hook)() = NULL; |
| 139 |
|
| 140 |
void RBAStop() |
| 141 |
{ |
| 142 |
if (!s_cd) return; |
| 143 |
SDL_CDStop(s_cd); |
| 144 |
redbook_finished_hook = NULL; // no calling the finished hook - stopped means stopped here |
| 145 |
con_puts(CON_VERBOSE, "RBAudio: Playback stopped"); |
| 146 |
} |
| 147 |
|
| 148 |
void RBAEjectDisk() |
| 149 |
{ |
| 150 |
if (!s_cd) return; |
| 151 |
SDL_CDEject(s_cd); // play nothing until it tries to load a song |
| 152 |
#if defined(__APPLE__) || defined(macintosh) |
| 153 |
SDL_QuitSubSystem(SDL_INIT_CDROM); // necessary for rescanning CDROMs |
| 154 |
#endif |
| 155 |
initialised = 0; |
| 156 |
} |
| 157 |
|
| 158 |
#ifdef __linux__ |
| 159 |
void RBASetVolume(int volume) |
| 160 |
{ |
| 161 |
int cdfile, level; |
| 162 |
struct cdrom_volctrl volctrl; |
| 163 |
|
| 164 |
if (!s_cd) return; |
| 165 |
|
| 166 |
cdfile = s_cd->id; |
| 167 |
level = volume*REDBOOK_VOLUME_SCALE/8; |
| 168 |
|
| 169 |
if ((level<0) || (level>REDBOOK_VOLUME_SCALE)) { |
| 170 |
con_printf(CON_CRITICAL, "RBAudio: illegal volume value (allowed values 0-%i)",REDBOOK_VOLUME_SCALE); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
volctrl.channel0 |
| 175 |
= volctrl.channel1 |
| 176 |
= volctrl.channel2 |
| 177 |
= volctrl.channel3 |
| 178 |
= level; |
| 179 |
if ( ioctl(cdfile, CDROMVOLCTRL, &volctrl) == -1 ) { |
| 180 |
con_puts(CON_CRITICAL, "RBAudio: CDROMVOLCTRL ioctl failed"); |
| 181 |
return; |
| 182 |
} |
| 183 |
} |
| 184 |
#endif |
| 185 |
|
| 186 |
void RBAPause() |
| 187 |
{ |
| 188 |
if (!s_cd) return; |
| 189 |
SDL_CDPause(s_cd); |
| 190 |
con_puts(CON_VERBOSE, "RBAudio: Playback paused"); |
| 191 |
} |
| 192 |
|
| 193 |
int RBAResume() |
| 194 |
{ |
| 195 |
if (!s_cd) return -1; |
| 196 |
SDL_CDResume(s_cd); |
| 197 |
con_puts(CON_VERBOSE, "RBAudio: Playback resumed"); |
| 198 |
return 1; |
| 199 |
} |
| 200 |
|
| 201 |
int RBAPauseResume() |
| 202 |
{ |
| 203 |
if (!s_cd) return 0; |
| 204 |
|
| 205 |
if (SDL_CDStatus(s_cd) == CD_PLAYING) |
| 206 |
{ |
| 207 |
SDL_CDPause(s_cd); |
| 208 |
con_puts(CON_VERBOSE, "RBAudio: Toggle Playback pause"); |
| 209 |
} |
| 210 |
else if (SDL_CDStatus(s_cd) == CD_PAUSED) |
| 211 |
{ |
| 212 |
SDL_CDResume(s_cd); |
| 213 |
con_puts(CON_VERBOSE, "RBAudio: Toggle Playback resume"); |
| 214 |
} |
| 215 |
else |
| 216 |
return 0; |
| 217 |
|
| 218 |
return 1; |
| 219 |
} |
| 220 |
|
| 221 |
int RBAGetNumberOfTracks() |
| 222 |
{ |
| 223 |
if (!s_cd) return -1; |
| 224 |
SDL_CDStatus(s_cd); |
| 225 |
con_printf(CON_VERBOSE, "RBAudio: Found %i tracks on CD", s_cd->numtracks); |
| 226 |
return s_cd->numtracks; |
| 227 |
} |
| 228 |
|
| 229 |
// check if we need to call the 'finished' hook |
| 230 |
// needs to go in all event loops |
| 231 |
// a real hook would be ideal, but is currently unsupported by SDL Audio CD |
| 232 |
void RBACheckFinishedHook() |
| 233 |
{ |
| 234 |
static fix64 last_check_time = 0; |
| 235 |
|
| 236 |
if (!s_cd) return; |
| 237 |
|
| 238 |
if ((timer_query() - last_check_time) >= F2_0) |
| 239 |
{ |
| 240 |
if ((SDL_CDStatus(s_cd) == CD_STOPPED) && redbook_finished_hook) |
| 241 |
{ |
| 242 |
con_puts(CON_VERBOSE, "RBAudio: Playback done, calling finished-hook"); |
| 243 |
redbook_finished_hook(); |
| 244 |
} |
| 245 |
last_check_time = timer_query(); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// plays tracks first through last, inclusive |
| 250 |
int RBAPlayTracks(int first, int last, void (*hook_finished)(void)) |
| 251 |
{ |
| 252 |
if (!s_cd) |
| 253 |
return 0; |
| 254 |
|
| 255 |
if (CD_INDRIVE(SDL_CDStatus(s_cd))) |
| 256 |
{ |
| 257 |
redbook_finished_hook = hook_finished; |
| 258 |
con_printf(CON_VERBOSE, "RBAudio: Playing tracks %i to %i", first, last); |
| 259 |
return SDL_CDPlayTracks(s_cd, first - 1, 0, last - first + 1, 0) == 0; |
| 260 |
} |
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
// return the track number currently playing. Useful if RBAPlayTracks() |
| 265 |
// is called. Returns 0 if no track playing, else track number |
| 266 |
int RBAGetTrackNum() |
| 267 |
{ |
| 268 |
if (!s_cd) |
| 269 |
return 0; |
| 270 |
|
| 271 |
if (SDL_CDStatus(s_cd) != CD_PLAYING) |
| 272 |
return 0; |
| 273 |
|
| 274 |
return s_cd->cur_track + 1; |
| 275 |
} |
| 276 |
|
| 277 |
int RBAPeekPlayStatus() |
| 278 |
{ |
| 279 |
if (!s_cd) |
| 280 |
return 0; |
| 281 |
|
| 282 |
if (SDL_CDStatus(s_cd) == CD_PLAYING) |
| 283 |
return 1; |
| 284 |
else if (SDL_CDStatus(s_cd) == CD_PAUSED) // hack so it doesn't keep restarting paused music |
| 285 |
return -1; |
| 286 |
|
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
static int cddb_sum(int n) |
| 291 |
{ |
| 292 |
int ret; |
| 293 |
|
| 294 |
/* For backward compatibility this algorithm must not change */ |
| 295 |
|
| 296 |
ret = 0; |
| 297 |
|
| 298 |
while (n > 0) { |
| 299 |
ret = ret + (n % 10); |
| 300 |
n = n / 10; |
| 301 |
} |
| 302 |
|
| 303 |
return (ret); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
unsigned long RBAGetDiscID() |
| 308 |
{ |
| 309 |
int i, t = 0, n = 0; |
| 310 |
|
| 311 |
if (!s_cd) |
| 312 |
return 0; |
| 313 |
|
| 314 |
/* For backward compatibility this algorithm must not change */ |
| 315 |
|
| 316 |
i = 0; |
| 317 |
|
| 318 |
while (i < s_cd->numtracks) { |
| 319 |
n += cddb_sum(s_cd->track[i].offset / CD_FPS); |
| 320 |
i++; |
| 321 |
} |
| 322 |
|
| 323 |
t = (s_cd->track[s_cd->numtracks].offset / CD_FPS) - |
| 324 |
(s_cd->track[0].offset / CD_FPS); |
| 325 |
|
| 326 |
return ((n % 0xff) << 24 | t << 8 | s_cd->numtracks); |
| 327 |
} |
| 328 |
|
| 329 |
void RBAList(void) |
| 330 |
{ |
| 331 |
|
| 332 |
if (!s_cd) |
| 333 |
return; |
| 334 |
|
| 335 |
range_for (auto &i, partial_const_range(s_cd->track, static_cast<unsigned>(s_cd->numtracks))) |
| 336 |
con_printf(CON_VERBOSE, "RBAudio: CD track %d, type %s, length %d, offset %d", i.id, (i.type == SDL_AUDIO_TRACK) ? "audio" : "data", i.length, i.offset); |
| 337 |
} |
| 338 |
|
| 339 |
} |