| 1 |
/* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 |
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */ |
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include "openmpt/all/BuildSettings.hpp" |
| 8 |
|
| 9 |
#include "mpt/base/macros.hpp" |
| 10 |
#include "mpt/random/default_engines.hpp" |
| 11 |
#include "mpt/random/engine.hpp" |
| 12 |
#include "mpt/random/seed.hpp" |
| 13 |
#include "openmpt/soundbase/MixSample.hpp" |
| 14 |
|
| 15 |
#include <vector> |
| 16 |
#include <variant> |
| 17 |
|
| 18 |
#include <cstddef> |
| 19 |
|
| 20 |
|
| 21 |
OPENMPT_NAMESPACE_BEGIN |
| 22 |
|
| 23 |
|
| 24 |
template <typename Tdither> |
| 25 |
class MultiChannelDither |
| 26 |
{ |
| 27 |
private: |
| 28 |
std::vector<Tdither> DitherChannels; |
| 29 |
typename Tdither::prng_type prng; |
| 30 |
|
| 31 |
public: |
| 32 |
template <typename Trd> |
| 33 |
MultiChannelDither(Trd &rd, std::size_t channels) |
| 34 |
: DitherChannels(channels) |
| 35 |
, prng(Tdither::prng_init(rd)) |
| 36 |
{ |
| 37 |
return; |
| 38 |
} |
| 39 |
void Reset() |
| 40 |
{ |
| 41 |
for(std::size_t channel = 0; channel < DitherChannels.size(); ++channel) |
| 42 |
{ |
| 43 |
DitherChannels[channel] = Tdither{}; |
| 44 |
} |
| 45 |
} |
| 46 |
std::size_t GetChannels() const |
| 47 |
{ |
| 48 |
return DitherChannels.size(); |
| 49 |
} |
| 50 |
template <uint32 targetbits> |
| 51 |
MPT_FORCEINLINE MixSampleInt process(std::size_t channel, MixSampleInt sample) |
| 52 |
{ |
| 53 |
return DitherChannels[channel].template process<targetbits>(sample, prng); |
| 54 |
} |
| 55 |
template <uint32 targetbits> |
| 56 |
MPT_FORCEINLINE MixSampleFloat process(std::size_t channel, MixSampleFloat sample) |
| 57 |
{ |
| 58 |
return DitherChannels[channel].template process<targetbits>(sample, prng); |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
template <typename AvailableDithers, typename DitherNames, std::size_t defaultChannels, std::size_t defaultDither, std::size_t noDither, typename seeding_random_engine = mpt::good_engine> |
| 64 |
class Dithers |
| 65 |
: public DitherNames |
| 66 |
{ |
| 67 |
|
| 68 |
public: |
| 69 |
static constexpr std::size_t NoDither = noDither; |
| 70 |
static constexpr std::size_t DefaultDither = defaultDither; |
| 71 |
static constexpr std::size_t DefaultChannels = defaultChannels; |
| 72 |
|
| 73 |
private: |
| 74 |
seeding_random_engine m_PRNG; |
| 75 |
AvailableDithers m_Dithers; |
| 76 |
|
| 77 |
public: |
| 78 |
template <typename Trd> |
| 79 |
Dithers(Trd &rd, std::size_t mode = defaultDither, std::size_t channels = defaultChannels) |
| 80 |
: m_PRNG(mpt::make_prng<seeding_random_engine>(rd)) |
| 81 |
, m_Dithers(std::in_place_index<defaultDither>, m_PRNG, channels) |
| 82 |
{ |
| 83 |
SetMode(mode, channels); |
| 84 |
} |
| 85 |
|
| 86 |
AvailableDithers &Variant() |
| 87 |
{ |
| 88 |
return m_Dithers; |
| 89 |
} |
| 90 |
|
| 91 |
static std::size_t GetNumDithers() |
| 92 |
{ |
| 93 |
return std::variant_size<AvailableDithers>::value; |
| 94 |
} |
| 95 |
|
| 96 |
static std::size_t GetDefaultDither() |
| 97 |
{ |
| 98 |
return defaultDither; |
| 99 |
} |
| 100 |
|
| 101 |
static std::size_t GetNoDither() |
| 102 |
{ |
| 103 |
return noDither; |
| 104 |
} |
| 105 |
|
| 106 |
private: |
| 107 |
template <std::size_t i = 0> |
| 108 |
void set_mode(std::size_t mode, std::size_t channels) |
| 109 |
{ |
| 110 |
if constexpr(i < std::variant_size<AvailableDithers>::value) |
| 111 |
{ |
| 112 |
if(mode == i) |
| 113 |
{ |
| 114 |
m_Dithers.template emplace<i>(m_PRNG, channels); |
| 115 |
} else |
| 116 |
{ |
| 117 |
this->template set_mode<i + 1>(mode, channels); |
| 118 |
} |
| 119 |
} else |
| 120 |
{ |
| 121 |
MPT_UNUSED(mode); |
| 122 |
m_Dithers.template emplace<defaultDither>(m_PRNG, channels); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
public: |
| 127 |
void SetMode(std::size_t mode, std::size_t channels) |
| 128 |
{ |
| 129 |
if((mode == GetMode()) && (channels == GetChannels())) |
| 130 |
{ |
| 131 |
std::visit([](auto &dither) |
| 132 |
{ return dither.Reset(); }, |
| 133 |
m_Dithers); |
| 134 |
return; |
| 135 |
} |
| 136 |
set_mode(mode, channels); |
| 137 |
} |
| 138 |
void SetMode(std::size_t mode) |
| 139 |
{ |
| 140 |
if(mode == GetMode()) |
| 141 |
{ |
| 142 |
std::visit([](auto &dither) |
| 143 |
{ return dither.Reset(); }, |
| 144 |
m_Dithers); |
| 145 |
return; |
| 146 |
} |
| 147 |
set_mode(mode, GetChannels()); |
| 148 |
} |
| 149 |
void SetChannels(std::size_t channels) |
| 150 |
{ |
| 151 |
if(channels == GetChannels()) |
| 152 |
{ |
| 153 |
return; |
| 154 |
} |
| 155 |
set_mode(GetMode(), channels); |
| 156 |
} |
| 157 |
void Reset() |
| 158 |
{ |
| 159 |
std::visit([](auto &dither) |
| 160 |
{ return dither.Reset(); }, |
| 161 |
m_Dithers); |
| 162 |
} |
| 163 |
std::size_t GetMode() const |
| 164 |
{ |
| 165 |
return m_Dithers.index(); |
| 166 |
} |
| 167 |
std::size_t GetChannels() const |
| 168 |
{ |
| 169 |
return std::visit([](auto &dither) |
| 170 |
{ return dither.GetChannels(); }, |
| 171 |
m_Dithers); |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
OPENMPT_NAMESPACE_END |