dxx-rebirth/common/arch/sdl/digi_mixer_music.cpp

333 lines
8.6 KiB
C++
Raw Normal View History

2014-06-01 17:55:23 +00:00
/*
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
2014-06-01 17:55:23 +00:00
* It is copyright by its individual contributors, as recorded in the
* project's Git history. See COPYING.txt at the top level for license
* terms and a link to the Git history.
*/
/*
* This is an alternate backend for the music system.
* It uses SDL_mixer to provide a more reliable playback,
* and allow processing of multiple audio formats.
*
* -- MD2211 (2006-04-24)
*/
2022-09-24 17:47:52 +00:00
#include <span>
2013-06-30 02:22:56 +00:00
#include <SDL.h>
#include <SDL_mixer.h>
#include <string.h>
#include <stdlib.h>
#include "args.h"
#include "hmp.h"
#include "adlmidi_dynamic.h"
#include "digi_mixer_music.h"
2013-12-26 04:18:28 +00:00
#include "strutil.h"
#include "u_mem.h"
#include "config.h"
#include "console.h"
#include "physfsrwops.h"
2015-12-13 18:00:49 +00:00
namespace dcx {
namespace {
struct Music_delete
2014-08-27 02:52:21 +00:00
{
void operator()(Mix_Music *m) const
2014-08-27 02:52:21 +00:00
{
Mix_FreeMusic(m);
}
};
class current_music_t : std::unique_ptr<Mix_Music, Music_delete>
{
using music_pointer = std::unique_ptr<Mix_Music, Music_delete>;
2014-08-27 02:52:21 +00:00
public:
using music_pointer::reset;
void reset(SDL_RWops *rw);
using music_pointer::operator bool;
using music_pointer::get;
2014-08-27 02:52:21 +00:00
};
void current_music_t::reset(SDL_RWops *const rw)
{
if (!rw)
{
/* As a special case, exit early if rw is nullptr. SDL is
* guaranteed to fail in this case, but will set an error message
* when it does so. The error message about a nullptr SDL_RWops
* will replace any prior error message, which might have been more
* useful.
*/
reset();
return;
}
reset(Mix_LoadMUSType_RW(rw, MUS_NONE, SDL_TRUE));
if constexpr (SDL_MIXER_MAJOR_VERSION == 1)
{
/* In SDL_mixer-1, setting freesrc==SDL_TRUE only transfers ownership
* of the RWops structure on success. On failure, the structure is
* still owned by the caller, and must be freed here.
*
* In SDL_mixer-2, setting freesrc==SDL_TRUE always transfers ownership
* of the RWops structure. On failure, SDL_mixer-2 will free the RWops
* before returning, so the structure must not be freed here.
*/
if (!*this)
SDL_RWclose(rw);
}
}
2014-08-27 02:52:21 +00:00
static current_music_t current_music;
static std::vector<uint8_t> current_music_hndlbuf;
static void mix_set_music_type_sdlmixer(int loop, void (*const hook_finished_track)())
{
Mix_PlayMusic(current_music.get(), (loop ? -1 : 1));
Mix_HookMusicFinished(hook_finished_track);
}
#if DXX_USE_ADLMIDI
static ADL_MIDIPlayer_t current_adlmidi;
2018-10-03 23:31:19 +00:00
static ADL_MIDIPlayer *get_adlmidi()
{
if (!CGameCfg.ADLMIDI_enabled)
return nullptr;
2018-10-03 23:31:19 +00:00
ADL_MIDIPlayer *adlmidi = current_adlmidi.get();
if (!adlmidi)
{
int sample_rate;
Mix_QuerySpec(&sample_rate, nullptr, nullptr);
adlmidi = adl_init(sample_rate);
if (adlmidi)
{
adl_switchEmulator(adlmidi, ADLMIDI_EMU_DOSBOX);
adl_setNumChips(adlmidi, CGameCfg.ADLMIDI_num_chips);
adl_setBank(adlmidi, CGameCfg.ADLMIDI_bank);
adl_setSoftPanEnabled(adlmidi, 1);
current_adlmidi.reset(adlmidi);
}
2018-10-03 23:31:19 +00:00
}
return adlmidi;
}
static void mix_adlmidi(void *udata, Uint8 *stream, int len);
static void mix_set_music_type_adl(int loop, void (*const hook_finished_track)())
{
ADL_MIDIPlayer *adlmidi = get_adlmidi();
adl_setLoopEnabled(adlmidi, loop);
Mix_HookMusic(&mix_adlmidi, nullptr);
Mix_HookMusicFinished(hook_finished_track);
}
#endif
enum class CurrentMusicType
{
None,
#if DXX_USE_ADLMIDI
ADLMIDI,
#endif
SDLMixer,
2018-10-03 23:31:19 +00:00
};
static CurrentMusicType current_music_type = CurrentMusicType::None;
2018-10-03 23:31:19 +00:00
2022-09-24 17:47:52 +00:00
static CurrentMusicType load_mus_data(std::span<const uint8_t> data, int loop, void (*const hook_finished_track)());
static CurrentMusicType load_mus_file(const char *filename, int loop, void (*const hook_finished_track)());
2018-10-03 23:31:19 +00:00
}
/*
* Plays a music file from an absolute path or a relative path
*/
int mix_play_file(const char *filename, int loop, void (*const entry_hook_finished_track)())
{
mix_free_music(); // stop and free what we're already playing, if anything
const auto hook_finished_track = entry_hook_finished_track ? entry_hook_finished_track : mix_free_music;
// It's a .hmp!
if (const auto fptr = strrchr(filename, '.'); fptr && !d_stricmp(fptr, ".hmp"))
{
if (auto &&[v, hoe] = hmp2mid(filename); hoe == hmp_open_error::None)
{
current_music_hndlbuf = std::move(v);
2022-09-24 17:47:52 +00:00
current_music_type = load_mus_data(current_music_hndlbuf, loop, hook_finished_track);
if (current_music_type != CurrentMusicType::None)
return 1;
}
else
/* hmp2mid printed an error message, so there is no need for another one here */
return 0;
}
// try loading music via given filename
{
current_music_type = load_mus_file(filename, loop, hook_finished_track);
if (current_music_type != CurrentMusicType::None)
return 1;
}
// allow the shell convention tilde character to mean the user's home folder
// chiefly used for default jukebox level song music referenced in 'descent.m3u' for Mac OS X
if (*filename == '~')
{
const auto sep = PHYSFS_getDirSeparator();
const auto lensep = strlen(sep);
std::array<char, PATH_MAX> full_path;
snprintf(full_path.data(), PATH_MAX, "%s%s", PHYSFS_getUserDir(),
&filename[1 + (!strncmp(&filename[1], sep, lensep)
? lensep
: 0)]);
current_music_type = load_mus_file(full_path.data(), loop, hook_finished_track);
if (current_music_type != CurrentMusicType::None)
return 1;
}
// still nothin'? Let's open via PhysFS in case it's located inside an archive
{
if (RAIIPHYSFS_File filehandle{PHYSFS_openRead(filename)})
{
2022-09-24 17:47:52 +00:00
const auto len = PHYSFS_fileLength(filehandle);
current_music_hndlbuf.resize(len);
2022-09-24 17:47:52 +00:00
const auto bufsize = PHYSFS_read(filehandle, &current_music_hndlbuf[0], sizeof(char), len);
current_music_type = load_mus_data(std::span(current_music_hndlbuf).first(bufsize), loop, hook_finished_track);
if (current_music_type != CurrentMusicType::None)
return 1;
}
}
con_printf(CON_CRITICAL, "Music %s could not be loaded: %s", filename, Mix_GetError());
mix_stop_music();
2018-10-03 23:31:19 +00:00
return 0;
}
// What to do when stopping song playback
void mix_free_music()
{
Mix_HaltMusic();
#if DXX_USE_ADLMIDI
/* Only ADLMIDI can set a hook, so if ADLMIDI is compiled out, there is no
* need to clear the hook.
*
* When ADLMIDI is supported, clear unconditionally, instead of checking
* whether the music type requires it.
*/
2018-10-03 23:31:19 +00:00
Mix_HookMusic(nullptr, nullptr);
#endif
2014-08-27 02:52:21 +00:00
current_music.reset();
current_music_hndlbuf.clear();
current_music_type = CurrentMusicType::None;
}
void mix_set_music_volume(int vol)
{
vol *= MIX_MAX_VOLUME/8;
Mix_VolumeMusic(vol);
}
void mix_stop_music()
{
Mix_HaltMusic();
current_music_hndlbuf.clear();
}
void mix_pause_music()
{
Mix_PauseMusic();
}
void mix_resume_music()
{
Mix_ResumeMusic();
}
void mix_pause_resume_music()
{
if (Mix_PausedMusic())
Mix_ResumeMusic();
else if (Mix_PlayingMusic())
Mix_PauseMusic();
}
namespace {
2022-09-24 17:47:52 +00:00
static CurrentMusicType load_mus_data(const std::span<const uint8_t> data, int loop, void (*const hook_finished_track)())
2018-10-03 23:31:19 +00:00
{
#if DXX_USE_ADLMIDI
const auto adlmidi = get_adlmidi();
2022-09-24 17:47:52 +00:00
if (adlmidi && adl_openData(adlmidi, data.data(), data.size()) == 0)
{
mix_set_music_type_adl(loop, hook_finished_track);
return CurrentMusicType::ADLMIDI;
}
else
#endif
2018-10-03 23:31:19 +00:00
{
2022-09-24 17:47:52 +00:00
const auto rw = SDL_RWFromConstMem(data.data(), data.size());
current_music.reset(rw);
2018-10-03 23:31:19 +00:00
if (current_music)
{
mix_set_music_type_sdlmixer(loop, hook_finished_track);
return CurrentMusicType::SDLMixer;
}
2018-10-03 23:31:19 +00:00
}
return CurrentMusicType::None;
2018-10-03 23:31:19 +00:00
}
static CurrentMusicType load_mus_file(const char *filename, int loop, void (*const hook_finished_track)())
2018-10-03 23:31:19 +00:00
{
CurrentMusicType type = CurrentMusicType::None;
#if DXX_USE_ADLMIDI
const auto adlmidi = get_adlmidi();
if (adlmidi && adl_openFile(adlmidi, filename) == 0)
{
mix_set_music_type_adl(loop, hook_finished_track);
return CurrentMusicType::ADLMIDI;
}
else
#endif
2018-10-03 23:31:19 +00:00
{
const auto rw = SDL_RWFromFile(filename, "rb");
current_music.reset(rw);
2018-10-03 23:31:19 +00:00
if (current_music)
{
mix_set_music_type_sdlmixer(loop, hook_finished_track);
return CurrentMusicType::SDLMixer;
}
2018-10-03 23:31:19 +00:00
}
return type;
}
#if DXX_USE_ADLMIDI
2018-10-03 23:31:19 +00:00
static int16_t sat16(int32_t x)
{
x = (x < INT16_MIN) ? INT16_MIN : x;
x = (x > INT16_MAX) ? INT16_MAX : x;
return x;
}
static void mix_adlmidi(void *, Uint8 *stream, int len)
{
ADLMIDI_AudioFormat format;
format.containerSize = sizeof(int16_t);
format.sampleOffset = 2 * format.containerSize;
format.type = ADLMIDI_SampleType_S16;
ADL_MIDIPlayer *adlmidi = get_adlmidi();
2018-10-03 23:31:19 +00:00
int sampleCount = len / format.containerSize;
adl_playFormat(adlmidi, sampleCount, stream, stream + format.containerSize, &format);
const auto samples = reinterpret_cast<int16_t *>(stream);
const auto amplify = [](int16_t i) { return sat16(2 * i); };
std::transform(samples, samples + sampleCount, samples, amplify);
2018-10-03 23:31:19 +00:00
}
#endif
2018-10-03 23:31:19 +00:00
}
}