| 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/audio/sample.hpp" |
| 11 |
#include "mpt/base/float.hpp" |
| 12 |
|
| 13 |
#include <type_traits> |
| 14 |
|
| 15 |
#include <cstddef> |
| 16 |
|
| 17 |
|
| 18 |
OPENMPT_NAMESPACE_BEGIN |
| 19 |
|
| 20 |
|
| 21 |
template <typename Tsample, std::size_t MIX_HEADROOM_BITS, std::size_t FILTER_HEADROOM_BITS> |
| 22 |
struct FixedPointSampleTraits |
| 23 |
{ |
| 24 |
static_assert(std::is_integral<Tsample>::value); |
| 25 |
static_assert(std::is_signed<Tsample>::value); |
| 26 |
static_assert((sizeof(Tsample) * 8u) - 1 > MIX_HEADROOM_BITS); |
| 27 |
static_assert((sizeof(Tsample) * 8u) - 1 > FILTER_HEADROOM_BITS); |
| 28 |
using sample_type = Tsample; |
| 29 |
enum class sample_type_strong : sample_type |
| 30 |
{ |
| 31 |
}; |
| 32 |
static constexpr int mix_headroom_bits = static_cast<int>(MIX_HEADROOM_BITS); |
| 33 |
static constexpr int mix_precision_bits = static_cast<int>((sizeof(Tsample) * 8) - MIX_HEADROOM_BITS); // including sign bit |
| 34 |
static constexpr int mix_fractional_bits = static_cast<int>((sizeof(Tsample) * 8) - 1 - MIX_HEADROOM_BITS); // excluding sign bit |
| 35 |
static constexpr sample_type mix_clip_max = ((sample_type(1) << mix_fractional_bits) - sample_type(1)); |
| 36 |
static constexpr sample_type mix_clip_min = -((sample_type(1) << mix_fractional_bits) - sample_type(1)); |
| 37 |
static constexpr int filter_headroom_bits = static_cast<int>(FILTER_HEADROOM_BITS); |
| 38 |
static constexpr int filter_precision_bits = static_cast<int>((sizeof(Tsample) * 8) - FILTER_HEADROOM_BITS); // including sign bit |
| 39 |
static constexpr int filter_fractional_bits = static_cast<int>((sizeof(Tsample) * 8) - 1 - FILTER_HEADROOM_BITS); // excluding sign bit |
| 40 |
template <typename Tfloat> |
| 41 |
static constexpr Tfloat mix_scale = static_cast<Tfloat>(sample_type(1) << mix_fractional_bits); |
| 42 |
}; |
| 43 |
|
| 44 |
using MixSampleIntTraits = FixedPointSampleTraits<int32, 4, 8>; |
| 45 |
|
| 46 |
using MixSampleInt = MixSampleIntTraits::sample_type; |
| 47 |
using MixSampleFloat = mpt::audio_sample_float; |
| 48 |
|
| 49 |
using MixSample = std::conditional<mpt::float_traits<nativefloat>::is_hard, MixSampleFloat, MixSampleInt>::type; |
| 50 |
|
| 51 |
|
| 52 |
OPENMPT_NAMESPACE_END |