| 1 |
/* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 |
/* SPDX-FileCopyrightText: Olivier Lapicque */ |
| 3 |
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */ |
| 4 |
|
| 5 |
|
| 6 |
#pragma once |
| 7 |
|
| 8 |
#include "openmpt/all/BuildSettings.hpp" |
| 9 |
|
| 10 |
#include "mpt/base/arithmetic_shift.hpp" |
| 11 |
#include "mpt/base/macros.hpp" |
| 12 |
#include "mpt/random/random.hpp" |
| 13 |
#include "openmpt/base/Types.hpp" |
| 14 |
#include "openmpt/random/ModPlug.hpp" |
| 15 |
#include "openmpt/soundbase/MixSample.hpp" |
| 16 |
#include "openmpt/soundbase/MixSampleConvert.hpp" |
| 17 |
|
| 18 |
|
| 19 |
OPENMPT_NAMESPACE_BEGIN |
| 20 |
|
| 21 |
|
| 22 |
struct Dither_ModPlug |
| 23 |
{ |
| 24 |
public: |
| 25 |
using prng_type = mpt::rng::modplug_dither; |
| 26 |
template <typename Trd> |
| 27 |
static prng_type prng_init(Trd &) |
| 28 |
{ |
| 29 |
return prng_type{0, 0}; |
| 30 |
} |
| 31 |
|
| 32 |
public: |
| 33 |
template <uint32 targetbits, typename Trng> |
| 34 |
MPT_FORCEINLINE MixSampleInt process(MixSampleInt sample, Trng &rng) |
| 35 |
{ |
| 36 |
if constexpr(targetbits == 0) |
| 37 |
{ |
| 38 |
MPT_UNUSED(rng); |
| 39 |
return sample; |
| 40 |
} else if constexpr(targetbits + MixSampleIntTraits::mix_headroom_bits + 1 >= 32) |
| 41 |
{ |
| 42 |
MPT_UNUSED(rng); |
| 43 |
return sample; |
| 44 |
} else |
| 45 |
{ |
| 46 |
sample += mpt::rshift_signed(static_cast<int32>(mpt::random<uint32>(rng)), (targetbits + MixSampleIntTraits::mix_headroom_bits + 1)); |
| 47 |
return sample; |
| 48 |
} |
| 49 |
} |
| 50 |
template <uint32 targetbits, typename Trng> |
| 51 |
MPT_FORCEINLINE MixSampleFloat process(MixSampleFloat sample, Trng &prng) |
| 52 |
{ |
| 53 |
return mix_sample_cast<MixSampleFloat>(process<targetbits>(mix_sample_cast<MixSampleInt>(sample), prng)); |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
OPENMPT_NAMESPACE_END |