dxx-rebirth/common/main/d_uspan.h
Kp 56d1814489 Add alternative mixer resamplers
SDL2_mixer changed how it upsamples sounds, and some users complained
about the difference.  Add an internal resampler that emulates how
SDL_mixer upsamples sounds.  Since all Rebirth upsampling is an integer
upsample (11Khz -> 44KHz, 22KHz -> 44Khz), this internal emulation is
considerably simpler than a general purpose resampler.

With this commit, the builder can choose which resamplers to enable.
The available resamplers are chosen by preprocessor directive, and
presently do not have an SConstruct flag.  For each resampler, if no
choice is made, then the resampler will be enabled if it is reasonable.
At least one of the resamplers must be enabled, or the build will fail
with a `#error` message.

The user may choose at program start time which of the available
resamplers to use for that execution of the program, through passing one
of the command line arguments:
- `-sdlmixer-resampler=sdl-native`
- `-sdlmixer-resampler=emulate-sdl1`
- `-sdlmixer-resampler=emulate-soundblaster16`

Runtime switching is not supported.  If the user does not choose, then
the first enabled resampler from the list below will be used.  The
available resamplers are:
- sdl_native (DXX_FEATURE_EXTERNAL_RESAMPLER_SDL_NATIVE) - delegates to
  SDL_mixer / SDL2_mixer, the way Rebirth has historically done.
- emulate_sdl1 (DXX_FEATURE_INTERNAL_RESAMPLER_EMULATE_SDL1) - an
  internal resampler that emulates how SDL_mixer worked.  This should be
  equivalent to sdl_native when using SDL_mixer, so by default it is
  enabled when Rebirth is built to use SDL2_mixer and disabled when
  Rebirth is built to use SDL_mixer.  It can still be enabled manually
  even when building for SDL_mixer, but this does not seem likely to be
  useful.
- emulate_soundblaster16
  (DXX_FEATURE_INTERNAL_RESAMPLER_EMULATE_SOUNDBLASTER16) - an internal
  resampler submitted by @raptor in
  5165efbc46.  Some users reported audio
  quality issues with this resampler, so it is not presently the
  default.
2023-02-11 10:50:21 +00:00

72 lines
2 KiB
C++

/*
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
* 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.
*/
#include <memory>
#include <span>
namespace dcx {
/* `unique_span` wraps `unique_ptr<T[]>`, but also remembers the extent of the
* array `T[]`. The method `span()` returns a
* `std::span<T, std::dynamic_extent>` that covers the memory region managed by
* `unique_span`. There is no support for a `std::span<T, static_length>`,
* because that could be more simply handled by
* `std::unique_ptr<std::array<T, static_length>>`, which would avoid
* storing the extent in the `unique_span`.
*/
/* Set a default deleter from the underlying unique_ptr, but allow it to be
* overridden if the caller needs special handling.
*/
template <typename T, typename Deleter = typename std::unique_ptr<T[]>::deleter_type>
class unique_span : std::unique_ptr<T[], Deleter>
{
using base_type = std::unique_ptr<T[], Deleter>;
std::size_t extent{};
public:
constexpr unique_span() = default;
constexpr unique_span(base_type &&b, const std::size_t e) :
base_type(std::move(b)),
extent(e)
{
}
unique_span(const std::size_t e) :
base_type(new T[e]()),
extent(e)
{
}
unique_span(unique_span &&) = default;
unique_span &operator=(unique_span &&) = default;
using base_type::get;
/* Require an lvalue input, since the returned pointer is borrowed from
* this object. If the method is called on an rvalue input, then the
* unique_ptr would be destroyed and free the memory before the returned
* span was destroyed, which would leave the span dangling.
*/
[[nodiscard]]
std::span<T> span() &
{
return {get(), extent};
}
[[nodiscard]]
std::span<const T> span() const &
{
return {get(), extent};
}
std::span<const T> span() const && = delete;
auto release()
{
extent = 0;
return this->base_type::release();
}
std::size_t size() const
{
return extent;
}
};
}