| 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 | * This is an alternate backend for the music system. |
| 9 | * It uses SDL_mixer to provide a more reliable playback, |
| 10 | * and allow processing of multiple audio formats. |
| 11 | * |
| 12 | * -- MD2211 (2006-04-24) |
| 13 | */ |
| 14 | |
| 15 | #include <span> |
| 16 | #include <SDL.h> |
| 17 | #include <SDL_mixer.h> |
| 18 | #include <string.h> |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include "args.h" |
| 22 | #include "digi.h" |
| 23 | #include "hmp.h" |
| 24 | #include "adlmidi_dynamic.h" |
| 25 | #include "digi_mixer_music.h" |
| 26 | #include "strutil.h" |
| 27 | #include "u_mem.h" |
| 28 | #include "config.h" |
| 29 | #include "console.h" |
| 30 | #include "physfsrwops.h" |
| 31 | |
| 32 | #if SDL_MIXER_MAJOR_VERSION == 1 |
| 33 | /* SDL_mixer-1 is inconsistent in its memory management. On success, it takes |
| 34 | * ownership. On failure, it may free the resource immediately or may leave it |
| 35 | * allocated, with no indication which was done. Therefore, for simplicity, |
| 36 | * always manage the object in Rebirth code. |
| 37 | */ |
| 38 | #define DXX_USE_SDL_RWOPS_MANAGEMENT 0 |
| 39 | #else |
| 40 | /* SDL_mixer-2 (and possibly later versions, though that has not been checked) |
| 41 | * are consistent, and can handle the memory management internally. For |
| 42 | * completeness, provide a compile-time knob for Rebirth to manage this, but |
| 43 | * default it to delegate to SDL_mixer. |
| 44 | */ |
| 45 | #ifndef DXX_USE_SDL_RWOPS_MANAGEMENT |
| 46 | #define DXX_USE_SDL_RWOPS_MANAGEMENT 1 |
| 47 | #endif |
| 48 | #endif |
| 49 | |
| 50 | namespace dcx { |
| 51 | |
| 52 | namespace { |
| 53 | |
| 54 | struct Music_delete |
| 55 | { |
| 56 | void operator()(Mix_Music *m) const |
| 57 | { |
| 58 | Mix_FreeMusic(m); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | class current_music_t : |
| 63 | #if !DXX_USE_SDL_RWOPS_MANAGEMENT |
| 64 | /* If the memory management is not delegated to SDL, then an additional |
| 65 | * `std::unique_ptr` is needed to track and free the memory. Based on code |
| 66 | * inspection, SDL_mixer does not appear to access the `SDL_RWops` |
| 67 | * structure while processing the `Mix_FreeMusic` call. However, since |
| 68 | * logically the `Mix_Music` depends on the `SDL_RWops`, it seems safer to |
| 69 | * assume that `Mix_FreeMusic` might access the `SDL_RWops`, and arrange |
| 70 | * for the lifetime of `SDL_RWops` to exceed the lifetime of the |
| 71 | * `Mix_Music`. Therefore, the `SDL_RWops` is stored earlier in the class |
| 72 | * than the `Mix_Music`, and is destroyed later. |
| 73 | * |
| 74 | * If the memory management is delegated to SDL, then no additional storage |
| 75 | * is needed. |
| 76 | */ |
| 77 | RWops_ptr, |
| 78 | #endif |
| 79 | std::unique_ptr<Mix_Music, Music_delete> |
| 80 | { |
| 81 | using music_pointer = std::unique_ptr<Mix_Music, Music_delete>; |
| 82 | public: |
| 83 | #if DXX_USE_SDL_RWOPS_MANAGEMENT |
| 84 | /* Inherit the zero-argument form of `reset`, since the `reset()` method on |
| 85 | * this class would only be a call to `music_pointer::reset()`. Prohibit |
| 86 | * use of the one-argument form of `reset`, since it would not be visible |
| 87 | * in the `#else` case. |
| 88 | */ |
| 89 | using music_pointer::reset; |
| 90 | void reset(auto) = delete; |
| 91 | #else |
| 92 | void reset() |
| 93 | { |
| 94 | music_pointer::reset(); |
| 95 | RWops_ptr::reset(); |
| 96 | } |
| 97 | #endif |
| 98 | [[nodiscard]] |
| 99 | uintptr_t reset(RWops_ptr rw); |
| 100 | using music_pointer::operator bool; |
| 101 | using music_pointer::get; |
| 102 | }; |
| 103 | |
| 104 | uintptr_t current_music_t::reset(RWops_ptr rw) |
| 105 | { |
| 106 | if (!rw) |
| 107 | { |
| 108 | /* As a special case, exit early if rw is nullptr. SDL is |
| 109 | * guaranteed to fail in this case, but will set an error message |
| 110 | * when it does so. The error message about a nullptr SDL_RWops |
| 111 | * will replace any prior error message, which might have been more |
| 112 | * useful. |
| 113 | */ |
| 114 | reset(); |
| 115 | return 0; |
| 116 | } |
| 117 | const auto music{Mix_LoadMUSType_RW(rw.get(), MUS_NONE, DXX_USE_SDL_RWOPS_MANAGEMENT)}; |
| 118 | music_pointer::reset(music); |
| 119 | #if DXX_USE_SDL_RWOPS_MANAGEMENT |
| 120 | /* The `SDL_RWops` is now owned by SDL_mixer, so clear the |
| 121 | * `std::unique_ptr` to avoid freeing the `SDL_RWops`. |
| 122 | * |
| 123 | * If the load failed, then the `SDL_RWops` has already been freed. |
| 124 | * If the load succeeded, then SDL_mixer will free the `SDL_RWops` when the |
| 125 | * `Mix_Music` is destroyed. |
| 126 | */ |
| 127 | rw.release(); |
| 128 | #else |
| 129 | if (music) |
| 130 | { |
| 131 | /* The `SDL_RWops` remains owned by Rebirth, but must remain alive |
| 132 | * until the `Mix_Music` is destroyed, so transfer the `SDL_RWops` into |
| 133 | * the class to extend its lifetime. |
| 134 | * |
| 135 | * The previous `Mix_Music`, if any, was freed by the |
| 136 | * `music_pointer::reset` above, so the previous `SDL_RWops`, if any, |
| 137 | * can now be freed safely. |
| 138 | */ |
| 139 | RWops_ptr::operator=(std::move(rw)); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | /* If `!music`, the SDL_RWops is owned by Rebirth, but not needed since |
| 144 | * there is no music to use it. Allow it to expire at the end of the |
| 145 | * function. |
| 146 | * |
| 147 | * Clear the prior RWops_ptr, if any, since it corresponds to the prior |
| 148 | * music that was freed above. |
| 149 | */ |
| 150 | RWops_ptr::reset(); |
| 151 | } |
| 152 | #endif |
| 153 | /* Return the pointer as an integer, because callers should only check for |
| 154 | * success versus failure, and should not dereference the `Mix_Music *`. |
| 155 | */ |
| 156 | return reinterpret_cast<uintptr_t>(music); |
| 157 | } |
| 158 | |
| 159 | static current_music_t current_music; |
| 160 | static std::vector<uint8_t> current_music_hndlbuf; |
| 161 | |
| 162 | static void mix_set_music_type_sdlmixer(int loop, void (*const hook_finished_track)()) |
| 163 | { |
| 164 | Mix_PlayMusic(current_music.get(), (loop ? -1 : 1)); |
| 165 | Mix_HookMusicFinished(hook_finished_track); |
| 166 | } |
| 167 | |
| 168 | #if DXX_USE_ADLMIDI |
| 169 | static ADL_MIDIPlayer_t current_adlmidi; |
| 170 | static ADL_MIDIPlayer *get_adlmidi() |
| 171 | { |
| 172 | if (!CGameCfg.ADLMIDI_enabled) |
| 173 | return nullptr; |
| 174 | ADL_MIDIPlayer *adlmidi = current_adlmidi.get(); |
| 175 | if (!adlmidi) |
| 176 | { |
| 177 | int sample_rate; |
| 178 | Mix_QuerySpec(&sample_rate, nullptr, nullptr); |
| 179 | adlmidi = adl_init(sample_rate); |
| 180 | if (adlmidi) |
| 181 | { |
| 182 | adl_switchEmulator(adlmidi, ADLMIDI_EMU_DOSBOX); |
| 183 | adl_setNumChips(adlmidi, CGameCfg.ADLMIDI_num_chips); |
| 184 | adl_setBank(adlmidi, CGameCfg.ADLMIDI_bank); |
| 185 | adl_setSoftPanEnabled(adlmidi, 1); |
| 186 | current_adlmidi.reset(adlmidi); |
| 187 | } |
| 188 | } |
| 189 | return adlmidi; |
| 190 | } |
| 191 | |
| 192 | static void mix_adlmidi(void *udata, Uint8 *stream, int len); |
| 193 | |
| 194 | static void mix_set_music_type_adl(int loop, void (*const hook_finished_track)()) |
| 195 | { |
| 196 | ADL_MIDIPlayer *adlmidi = get_adlmidi(); |
| 197 | adl_setLoopEnabled(adlmidi, loop); |
| 198 | Mix_HookMusic(&mix_adlmidi, nullptr); |
| 199 | Mix_HookMusicFinished(hook_finished_track); |
| 200 | } |
| 201 | #endif |
| 202 | |
| 203 | enum class CurrentMusicType : uint8_t |
| 204 | { |
| 205 | None, |
| 206 | #if DXX_USE_ADLMIDI |
| 207 | ADLMIDI, |
| 208 | #endif |
| 209 | SDLMixer, |
| 210 | }; |
| 211 | |
| 212 | static CurrentMusicType current_music_type{CurrentMusicType::None}; |
| 213 | |
| 214 | static CurrentMusicType load_mus_data(const char *filename, std::span<const uint8_t> data, int loop, void (*const hook_finished_track)()); |
| 215 | static CurrentMusicType load_mus_file(const char *filename, int loop, void (*const hook_finished_track)()); |
| 216 | |
| 217 | #ifdef _WIN32 |
| 218 | // Windows native-MIDI stuff. |
| 219 | static std::unique_ptr<hmp_file> cur_hmp; |
| 220 | static uint8_t digi_win32_midi_song_playing; |
| 221 | static uint8_t already_playing; |
| 222 | #endif |
| 223 | |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Plays a music file from an absolute path or a relative path |
| 228 | */ |
| 229 | |
| 230 | int mix_play_file(const char *filename, int loop, void (*const entry_hook_finished_track)()) |
| 231 | { |
| 232 | mix_free_music(); // stop and free what we're already playing, if anything |
| 233 | |
| 234 | const auto hook_finished_track = entry_hook_finished_track ? entry_hook_finished_track : mix_free_music; |
| 235 | // It's a .hmp! |
| 236 | if (const auto fptr = strrchr(filename, '.'); fptr && !d_stricmp(fptr, ".hmp")) |
| 237 | { |
| 238 | if (auto &&[v, hoe] = hmp2mid(filename); hoe == hmp_open_error::None) |
| 239 | { |
| 240 | current_music_hndlbuf = std::move(v); |
| 241 | current_music_type = load_mus_data(filename, current_music_hndlbuf, loop, hook_finished_track); |
| 242 | if (current_music_type != CurrentMusicType::None) |
| 243 | return 1; |
| 244 | } |
| 245 | else |
| 246 | /* hmp2mid printed an error message, so there is no need for another one here */ |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | // try loading music via given filename |
| 251 | { |
| 252 | current_music_type = load_mus_file(filename, loop, hook_finished_track); |
| 253 | if (current_music_type != CurrentMusicType::None) |
| 254 | return 1; |
| 255 | } |
| 256 | |
| 257 | // allow the shell convention tilde character to mean the user's home folder |
| 258 | // chiefly used for default jukebox level song music referenced in 'descent.m3u' for Mac OS X |
| 259 | if (*filename == '~') |
| 260 | { |
| 261 | const auto sep = PHYSFS_getDirSeparator(); |
| 262 | const auto lensep = strlen(sep); |
| 263 | std::array<char, PATH_MAX> full_path; |
| 264 | snprintf(full_path.data(), PATH_MAX, "%s%s", PHYSFS_getUserDir(), |
| 265 | &filename[1 + (!strncmp(&filename[1], sep, lensep) |
| 266 | ? lensep |
| 267 | : 0)]); |
| 268 | current_music_type = load_mus_file(full_path.data(), loop, hook_finished_track); |
| 269 | if (current_music_type != CurrentMusicType::None) |
| 270 | return 1; |
| 271 | } |
| 272 | |
| 273 | // still nothin'? Let's open via PhysFS in case it's located inside an archive |
| 274 | { |
| 275 | if (RAIIPHYSFS_File filehandle{PHYSFS_openRead(filename)}) |
| 276 | { |
| 277 | const auto len{PHYSFS_fileLength(filehandle)}; |
| 278 | current_music_hndlbuf.resize(len); |
| 279 | const auto bufsize{PHYSFSX_readBytes(filehandle, current_music_hndlbuf.data(), len)}; |
| 280 | current_music_type = load_mus_data(filename, std::span(current_music_hndlbuf).first(bufsize), loop, hook_finished_track); |
| 281 | if (current_music_type != CurrentMusicType::None) |
| 282 | return 1; |
| 283 | } |
| 284 | else |
| 285 | con_printf(CON_VERBOSE, "warning: failed to open PhysFS file \"%s\"", filename); |
| 286 | } |
| 287 | |
| 288 | con_printf(CON_CRITICAL, "Music %s could not be loaded: %s", filename, Mix_GetError()); |
| 289 | mix_stop_music(); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | // What to do when stopping song playback |
| 295 | void mix_free_music() |
| 296 | { |
| 297 | Mix_HaltMusic(); |
| 298 | #if DXX_USE_ADLMIDI |
| 299 | /* Only ADLMIDI can set a hook, so if ADLMIDI is compiled out, there is no |
| 300 | * need to clear the hook. |
| 301 | * |
| 302 | * When ADLMIDI is supported, clear unconditionally, instead of checking |
| 303 | * whether the music type requires it. |
| 304 | */ |
| 305 | Mix_HookMusic(nullptr, nullptr); |
| 306 | #endif |
| 307 | current_music.reset(); |
| 308 | current_music_hndlbuf.clear(); |
| 309 | current_music_type = CurrentMusicType::None; |
| 310 | } |
| 311 | |
| 312 | void mix_set_music_volume(int vol) |
| 313 | { |
| 314 | vol *= MIX_MAX_VOLUME/8; |
| 315 | Mix_VolumeMusic(vol); |
| 316 | } |
| 317 | |
| 318 | void mix_stop_music() |
| 319 | { |
| 320 | Mix_HaltMusic(); |
| 321 | current_music_hndlbuf.clear(); |
| 322 | } |
| 323 | |
| 324 | void mix_pause_music() |
| 325 | { |
| 326 | Mix_PauseMusic(); |
| 327 | } |
| 328 | |
| 329 | void mix_resume_music() |
| 330 | { |
| 331 | Mix_ResumeMusic(); |
| 332 | } |
| 333 | |
| 334 | void mix_pause_resume_music() |
| 335 | { |
| 336 | if (Mix_PausedMusic()) |
| 337 | Mix_ResumeMusic(); |
| 338 | else if (Mix_PlayingMusic()) |
| 339 | Mix_PauseMusic(); |
| 340 | } |
| 341 | |
| 342 | namespace { |
| 343 | |
| 344 | static CurrentMusicType load_mus_data(const char *const filename, const std::span<const uint8_t> data, int loop, void (*const hook_finished_track)()) |
| 345 | { |
| 346 | #if DXX_USE_ADLMIDI |
| 347 | const auto adlmidi = get_adlmidi(); |
| 348 | if (adlmidi && adl_openData(adlmidi, data.data(), data.size()) == 0) |
| 349 | { |
| 350 | mix_set_music_type_adl(loop, hook_finished_track); |
| 351 | return CurrentMusicType::ADLMIDI; |
| 352 | } |
| 353 | else |
| 354 | #endif |
| 355 | { |
| 356 | if (current_music.reset(RWops_ptr{SDL_RWFromConstMem(data.data(), data.size())})) |
| 357 | { |
| 358 | mix_set_music_type_sdlmixer(loop, hook_finished_track); |
| 359 | return CurrentMusicType::SDLMixer; |
| 360 | } |
| 361 | else |
| 362 | con_printf(CON_VERBOSE, "warning: failed to load music from data from file \"%s\"", filename); |
| 363 | } |
| 364 | return CurrentMusicType::None; |
| 365 | } |
| 366 | |
| 367 | static CurrentMusicType load_mus_file(const char *filename, int loop, void (*const hook_finished_track)()) |
| 368 | { |
| 369 | #if DXX_USE_ADLMIDI |
| 370 | const auto adlmidi = get_adlmidi(); |
| 371 | if (adlmidi && adl_openFile(adlmidi, filename) == 0) |
| 372 | { |
| 373 | mix_set_music_type_adl(loop, hook_finished_track); |
| 374 | return CurrentMusicType::ADLMIDI; |
| 375 | } |
| 376 | else |
| 377 | #endif |
| 378 | if (RWops_ptr rw{SDL_RWFromFile(filename, "rb")}) |
| 379 | { |
| 380 | if (current_music.reset(std::move(rw))) |
| 381 | { |
| 382 | mix_set_music_type_sdlmixer(loop, hook_finished_track); |
| 383 | return CurrentMusicType::SDLMixer; |
| 384 | } |
| 385 | else |
| 386 | con_printf(CON_VERBOSE, "warning: failed to load music from filesystem file \"%s\"", filename); |
| 387 | } |
| 388 | else |
| 389 | /* The caller speculatively tries to treat inputs variously as PhysFS |
| 390 | * files and filesystem files. Failure to open this path as a |
| 391 | * filesystem file is not necessarily an error. |
| 392 | */ |
| 393 | con_printf(CON_VERBOSE, "warning: failed to open filesystem file \"%s\"", filename); |
| 394 | return CurrentMusicType::None; |
| 395 | } |
| 396 | |
| 397 | #if DXX_USE_ADLMIDI |
| 398 | static int16_t sat16(int32_t x) |
| 399 | { |
| 400 | x = (x < INT16_MIN) ? INT16_MIN : x; |
| 401 | x = (x > INT16_MAX) ? INT16_MAX : x; |
| 402 | return x; |
| 403 | } |
| 404 | |
| 405 | static void mix_adlmidi(void *, Uint8 *stream, int len) |
| 406 | { |
| 407 | ADLMIDI_AudioFormat format; |
| 408 | format.containerSize = sizeof(int16_t); |
| 409 | format.sampleOffset = 2 * format.containerSize; |
| 410 | format.type = ADLMIDI_SampleType_S16; |
| 411 | |
| 412 | ADL_MIDIPlayer *adlmidi = get_adlmidi(); |
| 413 | int sampleCount = len / format.containerSize; |
| 414 | adl_playFormat(adlmidi, sampleCount, stream, stream + format.containerSize, &format); |
| 415 | |
| 416 | const auto samples = reinterpret_cast<int16_t *>(stream); |
| 417 | const auto amplify = [](int16_t i) { return sat16(2 * i); }; |
| 418 | std::transform(samples, samples + sampleCount, samples, amplify); |
| 419 | } |
| 420 | #endif |
| 421 | |
| 422 | } |
| 423 | |
| 424 | #ifdef _WIN32 |
| 425 | void digi_win32_pause_midi_song() |
| 426 | { |
| 427 | hmp_pause(cur_hmp.get()); |
| 428 | } |
| 429 | |
| 430 | void digi_win32_resume_midi_song() |
| 431 | { |
| 432 | hmp_resume(cur_hmp.get()); |
| 433 | } |
| 434 | |
| 435 | void digi_win32_stop_midi_song() |
| 436 | { |
| 437 | if (!digi_win32_midi_song_playing) |
| 438 | return; |
| 439 | digi_win32_midi_song_playing = 0; |
| 440 | cur_hmp.reset(); |
| 441 | hmp_reset(); |
| 442 | } |
| 443 | |
| 444 | void digi_win32_set_midi_volume( int mvolume ) |
| 445 | { |
| 446 | hmp_setvolume(cur_hmp.get(), mvolume*MIDI_VOLUME_SCALE/8); |
| 447 | } |
| 448 | |
| 449 | int digi_win32_play_midi_song( const char * filename, int loop ) |
| 450 | { |
| 451 | if (!already_playing) |
| 452 | { |
| 453 | hmp_reset(); |
| 454 | already_playing = 1; |
| 455 | } |
| 456 | digi_win32_stop_midi_song(); |
| 457 | |
| 458 | if (filename == NULL) |
| 459 | return 0; |
| 460 | |
| 461 | if ((cur_hmp = std::get<0>(hmp_open(filename)))) |
| 462 | { |
| 463 | /* |
| 464 | * FIXME: to be implemented as soon as we have some kind or checksum function - replacement for ugly hack in hmp.c for descent.hmp |
| 465 | * if (***filesize check*** && ***CRC32 or MD5 check***) |
| 466 | * (((*cur_hmp).trks)[1]).data[6] = 0x6C; |
| 467 | */ |
| 468 | if (hmp_play(cur_hmp.get(),loop) != 0) |
| 469 | return 0; // error |
| 470 | digi_win32_midi_song_playing = 1; |
| 471 | digi_win32_set_midi_volume(CGameCfg.MusicVolume); |
| 472 | return 1; |
| 473 | } |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | #endif |
| 478 | |
| 479 | } |