| 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 |
|
| 10 |
#include "mpt/audio/span.hpp" |
| 11 |
#include "mpt/base/macros.hpp" |
| 12 |
#include "openmpt/soundbase/SampleClip.hpp" |
| 13 |
#include "openmpt/soundbase/SampleClipFixedPoint.hpp" |
| 14 |
#include "openmpt/soundbase/SampleConvert.hpp" |
| 15 |
#include "openmpt/soundbase/SampleConvertFixedPoint.hpp" |
| 16 |
#include "openmpt/soundbase/SampleFormat.hpp" |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
|
| 20 |
#include <cassert> |
| 21 |
#include <cstddef> |
| 22 |
|
| 23 |
|
| 24 |
OPENMPT_NAMESPACE_BEGIN |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
template <int fractionalBits, bool clipOutput, typename TOutBuf, typename TInBuf, typename Tdither> |
| 29 |
void ConvertBufferMixInternalFixedToBuffer(TOutBuf outBuf, TInBuf inBuf, Tdither &dither, std::size_t channels, std::size_t count) |
| 30 |
{ |
| 31 |
using TOutSample = typename std::remove_const<typename TOutBuf::sample_type>::type; |
| 32 |
using TInSample = typename std::remove_const<typename TInBuf::sample_type>::type; |
| 33 |
assert(inBuf.size_channels() >= channels); |
| 34 |
assert(outBuf.size_channels() >= channels); |
| 35 |
assert(inBuf.size_frames() >= count); |
| 36 |
assert(outBuf.size_frames() >= count); |
| 37 |
constexpr int ditherBits = SampleFormat(SampleFormatTraits<TOutSample>::sampleFormat()).IsInt() |
| 38 |
? SampleFormat(SampleFormatTraits<TOutSample>::sampleFormat()).GetBitsPerSample() |
| 39 |
: 0; |
| 40 |
SC::ClipFixed<int32, fractionalBits, clipOutput> clip; |
| 41 |
SC::ConvertFixedPoint<TOutSample, TInSample, fractionalBits> conv; |
| 42 |
for(std::size_t i = 0; i < count; ++i) |
| 43 |
{ |
| 44 |
for(std::size_t channel = 0; channel < channels; ++channel) |
| 45 |
{ |
| 46 |
outBuf(channel, i) = conv(clip(dither.template process<ditherBits>(channel, inBuf(channel, i)))); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
template <int fractionalBits, typename TOutBuf, typename TInBuf> |
| 53 |
void ConvertBufferToBufferMixInternalFixed(TOutBuf outBuf, TInBuf inBuf, std::size_t channels, std::size_t count) |
| 54 |
{ |
| 55 |
using TOutSample = typename std::remove_const<typename TOutBuf::sample_type>::type; |
| 56 |
using TInSample = typename std::remove_const<typename TInBuf::sample_type>::type; |
| 57 |
assert(inBuf.size_channels() >= channels); |
| 58 |
assert(outBuf.size_channels() >= channels); |
| 59 |
assert(inBuf.size_frames() >= count); |
| 60 |
assert(outBuf.size_frames() >= count); |
| 61 |
SC::ConvertToFixedPoint<TOutSample, TInSample, fractionalBits> conv; |
| 62 |
for(std::size_t i = 0; i < count; ++i) |
| 63 |
{ |
| 64 |
for(std::size_t channel = 0; channel < channels; ++channel) |
| 65 |
{ |
| 66 |
outBuf(channel, i) = conv(inBuf(channel, i)); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
template <bool clipOutput, typename TOutBuf, typename TInBuf, typename Tdither> |
| 73 |
void ConvertBufferMixInternalToBuffer(TOutBuf outBuf, TInBuf inBuf, Tdither &dither, std::size_t channels, std::size_t count) |
| 74 |
{ |
| 75 |
using TOutSample = typename std::remove_const<typename TOutBuf::sample_type>::type; |
| 76 |
using TInSample = typename std::remove_const<typename TInBuf::sample_type>::type; |
| 77 |
assert(inBuf.size_channels() >= channels); |
| 78 |
assert(outBuf.size_channels() >= channels); |
| 79 |
assert(inBuf.size_frames() >= count); |
| 80 |
assert(outBuf.size_frames() >= count); |
| 81 |
constexpr int ditherBits = SampleFormat(SampleFormatTraits<TOutSample>::sampleFormat()).IsInt() |
| 82 |
? SampleFormat(SampleFormatTraits<TOutSample>::sampleFormat()).GetBitsPerSample() |
| 83 |
: 0; |
| 84 |
SC::Clip<TInSample, clipOutput> clip; |
| 85 |
SC::Convert<TOutSample, TInSample> conv; |
| 86 |
for(std::size_t i = 0; i < count; ++i) |
| 87 |
{ |
| 88 |
for(std::size_t channel = 0; channel < channels; ++channel) |
| 89 |
{ |
| 90 |
outBuf(channel, i) = conv(clip(dither.template process<ditherBits>(channel, inBuf(channel, i)))); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
template <typename TOutBuf, typename TInBuf> |
| 97 |
void ConvertBufferToBufferMixInternal(TOutBuf outBuf, TInBuf inBuf, std::size_t channels, std::size_t count) |
| 98 |
{ |
| 99 |
using TOutSample = typename std::remove_const<typename TOutBuf::sample_type>::type; |
| 100 |
using TInSample = typename std::remove_const<typename TInBuf::sample_type>::type; |
| 101 |
assert(inBuf.size_channels() >= channels); |
| 102 |
assert(outBuf.size_channels() >= channels); |
| 103 |
assert(inBuf.size_frames() >= count); |
| 104 |
assert(outBuf.size_frames() >= count); |
| 105 |
SC::Convert<TOutSample, TInSample> conv; |
| 106 |
for(std::size_t i = 0; i < count; ++i) |
| 107 |
{ |
| 108 |
for(std::size_t channel = 0; channel < channels; ++channel) |
| 109 |
{ |
| 110 |
outBuf(channel, i) = conv(inBuf(channel, i)); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
OPENMPT_NAMESPACE_END |