aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/src/openmpt/soundbase/CopyMix.hpp
blob: 1dd8de37aaa2e8e131d80598f4ab8e423116e85e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* SPDX-License-Identifier: BSD-3-Clause */
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */


#pragma once

#include "openmpt/all/BuildSettings.hpp"


#include "mpt/audio/span.hpp"
#include "mpt/base/macros.hpp"
#include "openmpt/soundbase/SampleClip.hpp"
#include "openmpt/soundbase/SampleClipFixedPoint.hpp"
#include "openmpt/soundbase/SampleConvert.hpp"
#include "openmpt/soundbase/SampleConvertFixedPoint.hpp"
#include "openmpt/soundbase/SampleFormat.hpp"

#include <algorithm>

#include <cassert>
#include <cstddef>


OPENMPT_NAMESPACE_BEGIN



template <int fractionalBits, bool clipOutput, typename TOutBuf, typename TInBuf, typename Tdither>
void ConvertBufferMixInternalFixedToBuffer(TOutBuf outBuf, TInBuf inBuf, Tdither &dither, std::size_t channels, std::size_t count)
{
	using TOutSample = typename std::remove_const<typename TOutBuf::sample_type>::type;
	using TInSample = typename std::remove_const<typename TInBuf::sample_type>::type;
	assert(inBuf.size_channels() >= channels);
	assert(outBuf.size_channels() >= channels);
	assert(inBuf.size_frames() >= count);
	assert(outBuf.size_frames() >= count);
	constexpr int ditherBits = SampleFormat(SampleFormatTraits<TOutSample>::sampleFormat()).IsInt()
								 ? SampleFormat(SampleFormatTraits<TOutSample>::sampleFormat()).GetBitsPerSample()
								 : 0;
	SC::ClipFixed<int32, fractionalBits, clipOutput> clip;
	SC::ConvertFixedPoint<TOutSample, TInSample, fractionalBits> conv;
	for(std::size_t i = 0; i < count; ++i)
	{
		for(std::size_t channel = 0; channel < channels; ++channel)
		{
			outBuf(channel, i) = conv(clip(dither.template process<ditherBits>(channel, inBuf(channel, i))));
		}
	}
}


template <int fractionalBits, typename TOutBuf, typename TInBuf>
void ConvertBufferToBufferMixInternalFixed(TOutBuf outBuf, TInBuf inBuf, std::size_t channels, std::size_t count)
{
	using TOutSample = typename std::remove_const<typename TOutBuf::sample_type>::type;
	using TInSample = typename std::remove_const<typename TInBuf::sample_type>::type;
	assert(inBuf.size_channels() >= channels);
	assert(outBuf.size_channels() >= channels);
	assert(inBuf.size_frames() >= count);
	assert(outBuf.size_frames() >= count);
	SC::ConvertToFixedPoint<TOutSample, TInSample, fractionalBits> conv;
	for(std::size_t i = 0; i < count; ++i)
	{
		for(std::size_t channel = 0; channel < channels; ++channel)
		{
			outBuf(channel, i) = conv(inBuf(channel, i));
		}
	}
}


template <bool clipOutput, typename TOutBuf, typename TInBuf, typename Tdither>
void ConvertBufferMixInternalToBuffer(TOutBuf outBuf, TInBuf inBuf, Tdither &dither, std::size_t channels, std::size_t count)
{
	using TOutSample = typename std::remove_const<typename TOutBuf::sample_type>::type;
	using TInSample = typename std::remove_const<typename TInBuf::sample_type>::type;
	assert(inBuf.size_channels() >= channels);
	assert(outBuf.size_channels() >= channels);
	assert(inBuf.size_frames() >= count);
	assert(outBuf.size_frames() >= count);
	constexpr int ditherBits = SampleFormat(SampleFormatTraits<TOutSample>::sampleFormat()).IsInt()
								 ? SampleFormat(SampleFormatTraits<TOutSample>::sampleFormat()).GetBitsPerSample()
								 : 0;
	SC::Clip<TInSample, clipOutput> clip;
	SC::Convert<TOutSample, TInSample> conv;
	for(std::size_t i = 0; i < count; ++i)
	{
		for(std::size_t channel = 0; channel < channels; ++channel)
		{
			outBuf(channel, i) = conv(clip(dither.template process<ditherBits>(channel, inBuf(channel, i))));
		}
	}
}


template <typename TOutBuf, typename TInBuf>
void ConvertBufferToBufferMixInternal(TOutBuf outBuf, TInBuf inBuf, std::size_t channels, std::size_t count)
{
	using TOutSample = typename std::remove_const<typename TOutBuf::sample_type>::type;
	using TInSample = typename std::remove_const<typename TInBuf::sample_type>::type;
	assert(inBuf.size_channels() >= channels);
	assert(outBuf.size_channels() >= channels);
	assert(inBuf.size_frames() >= count);
	assert(outBuf.size_frames() >= count);
	SC::Convert<TOutSample, TInSample> conv;
	for(std::size_t i = 0; i < count; ++i)
	{
		for(std::size_t channel = 0; channel < channels; ++channel)
		{
			outBuf(channel, i) = conv(inBuf(channel, i));
		}
	}
}


OPENMPT_NAMESPACE_END