aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/src/openmpt/sounddevice/SoundDeviceBuffer.hpp
blob: 81ce72585471fcb2128337f6337dbe201dc88b40 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* SPDX-License-Identifier: BSD-3-Clause */
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */


#pragma once

#include "openmpt/all/BuildSettings.hpp"

#include "SoundDeviceCallback.hpp"

#include "mpt/audio/span.hpp"
#include "openmpt/base/Types.hpp"
#include "openmpt/soundbase/Dither.hpp"
#include "openmpt/soundbase/CopyMix.hpp"

#include <variant>

#include <cassert>
#include <cstddef>


OPENMPT_NAMESPACE_BEGIN


namespace SoundDevice
{


template <typename Tsample>
class BufferIO
{

private:
	mpt::audio_span_interleaved<const Tsample> const m_src;
	mpt::audio_span_interleaved<Tsample> const m_dst;
	std::size_t m_countFramesReadProcessed;
	std::size_t m_countFramesWriteProcessed;
	const BufferFormat m_bufferFormat;

public:
	inline BufferIO(Tsample *dst, const Tsample *src, std::size_t numFrames, BufferFormat bufferFormat)
		: m_src(src, bufferFormat.InputChannels, numFrames)
		, m_dst(dst, bufferFormat.Channels, numFrames)
		, m_countFramesReadProcessed(0)
		, m_countFramesWriteProcessed(0)
		, m_bufferFormat(bufferFormat)
	{
		return;
	}

	template <typename audio_span_dst>
	inline void Read(audio_span_dst dst)
	{
		assert(m_countFramesReadProcessed + dst.size_frames() <= m_src.size_frames());
		ConvertBufferToBufferMixInternal(dst, mpt::make_audio_span_with_offset(m_src, m_countFramesReadProcessed), m_bufferFormat.InputChannels, dst.size_frames());
		m_countFramesReadProcessed += dst.size_frames();
	}

	template <int fractionalBits, typename audio_span_dst>
	inline void ReadFixedPoint(audio_span_dst dst)
	{
		assert(m_countFramesReadProcessed + dst.size_frames() <= m_src.size_frames());
		ConvertBufferToBufferMixInternalFixed<fractionalBits>(dst, mpt::make_audio_span_with_offset(m_src, m_countFramesReadProcessed), m_bufferFormat.InputChannels, dst.size_frames());
		m_countFramesReadProcessed += dst.size_frames();
	}

	template <typename audio_span_src, typename TDither>
	inline void Write(audio_span_src src, TDither &dither)
	{
		assert(m_countFramesWriteProcessed + src.size_frames() <= m_dst.size_frames());
		if(m_bufferFormat.WantsClippedOutput)
		{
			ConvertBufferMixInternalToBuffer<true>(mpt::make_audio_span_with_offset(m_dst, m_countFramesWriteProcessed), src, dither, m_bufferFormat.Channels, src.size_frames());
		} else
		{
			ConvertBufferMixInternalToBuffer<false>(mpt::make_audio_span_with_offset(m_dst, m_countFramesWriteProcessed), src, dither, m_bufferFormat.Channels, src.size_frames());
		}
		m_countFramesWriteProcessed += src.size_frames();
	}

	template <int fractionalBits, typename audio_span_src, typename TDither>
	inline void WriteFixedPoint(audio_span_src src, TDither &dither)
	{
		assert(m_countFramesWriteProcessed + src.size_frames() <= m_dst.size_frames());
		if(m_bufferFormat.WantsClippedOutput)
		{
			ConvertBufferMixInternalFixedToBuffer<fractionalBits, true>(mpt::make_audio_span_with_offset(m_dst, m_countFramesWriteProcessed), src, dither, m_bufferFormat.Channels, src.size_frames());
		} else
		{
			ConvertBufferMixInternalFixedToBuffer<fractionalBits, false>(mpt::make_audio_span_with_offset(m_dst, m_countFramesWriteProcessed), src, dither, m_bufferFormat.Channels, src.size_frames());
		}
		m_countFramesWriteProcessed += src.size_frames();
	}

	inline ~BufferIO()
	{
		// fill remaining buffer with silence
		while(m_countFramesWriteProcessed < m_dst.size_frames())
		{
			for(std::size_t channel = 0; channel < m_dst.size_channels(); ++channel)
			{
				m_dst(channel, m_countFramesWriteProcessed) = SC::sample_cast<Tsample>(static_cast<int16>(0));
			}
			m_countFramesWriteProcessed += 1;
		}
	}
};


template <typename TDithers>
class CallbackBuffer
{
private:
	std::variant<
		BufferIO<uint8>,
		BufferIO<int8>,
		BufferIO<int16>,
		BufferIO<int24>,
		BufferIO<int32>,
		BufferIO<float>,
		BufferIO<double>>
		m_BufferIO;
	TDithers &m_Dithers;
	std::size_t m_NumFrames;

public:
	template <typename Tsample>
	explicit inline CallbackBuffer(Tsample *dst, const Tsample *src, std::size_t numFrames, TDithers &dithers, BufferFormat bufferFormat)
		: m_BufferIO(BufferIO<Tsample>{dst, src, numFrames, bufferFormat})
		, m_Dithers(dithers)
		, m_NumFrames(numFrames)
	{
		return;
	}

	inline std::size_t GetNumFrames() const
	{
		return m_NumFrames;
	}

	template <typename audio_span_dst>
	inline void Read(audio_span_dst dst)
	{
		std::visit(
			[&](auto &bufferIO)
			{
			bufferIO.Read(dst);
			},
			m_BufferIO);
	}

	template <int fractionalBits, typename audio_span_dst>
	inline void ReadFixedPoint(audio_span_dst dst)
	{
		std::visit(
			[&](auto &bufferIO)
			{
			bufferIO.template ReadFixedPoint<fractionalBits>(dst);
			},
			m_BufferIO);
	}

	template <typename audio_span_src>
	inline void Write(audio_span_src src)
	{
		std::visit(
			[&](auto &bufferIO)
			{
			std::visit(
				[&](auto &ditherInstance)
				{
				bufferIO.Write(src, ditherInstance);
				},
				m_Dithers.Variant());
			},
			m_BufferIO);
	}

	template <int fractionalBits, typename audio_span_src>
	inline void WriteFixedPoint(audio_span_src src)
	{
		std::visit(
			[&](auto &bufferIO)
			{
			std::visit(
				[&](auto &ditherInstance)
				{
				bufferIO.template WriteFixedPoint<fractionalBits>(src, ditherInstance);
				},
				m_Dithers.Variant());
			},
			m_BufferIO);
	}
};


template <typename TDithers>
class CallbackBufferHandler
	: public ICallback
{
private:
	TDithers m_Dithers;

protected:
	template <typename Trd>
	explicit CallbackBufferHandler(Trd &rd)
		: m_Dithers(rd)
	{
		return;
	}

protected:
	inline TDithers &Dithers()
	{
		return m_Dithers;
	}

private:
	template <typename Tsample>
	inline void SoundCallbackLockedProcessImpl(BufferFormat bufferFormat, std::size_t numFrames, Tsample *buffer, const Tsample *inputBuffer)
	{
		CallbackBuffer<TDithers> callbackBuffer{buffer, inputBuffer, numFrames, m_Dithers, bufferFormat};
		SoundCallbackLockedCallback(callbackBuffer);
	}

public:
	inline void SoundCallbackLockedProcess(BufferFormat bufferFormat, std::size_t numFrames, uint8 *buffer, const uint8 *inputBuffer) final
	{
		SoundCallbackLockedProcessImpl(bufferFormat, numFrames, buffer, inputBuffer);
	}
	inline void SoundCallbackLockedProcess(BufferFormat bufferFormat, std::size_t numFrames, int8 *buffer, const int8 *inputBuffer) final
	{
		SoundCallbackLockedProcessImpl(bufferFormat, numFrames, buffer, inputBuffer);
	}
	inline void SoundCallbackLockedProcess(BufferFormat bufferFormat, std::size_t numFrames, int16 *buffer, const int16 *inputBuffer) final
	{
		SoundCallbackLockedProcessImpl(bufferFormat, numFrames, buffer, inputBuffer);
	}
	inline void SoundCallbackLockedProcess(BufferFormat bufferFormat, std::size_t numFrames, int24 *buffer, const int24 *inputBuffer) final
	{
		SoundCallbackLockedProcessImpl(bufferFormat, numFrames, buffer, inputBuffer);
	}
	inline void SoundCallbackLockedProcess(BufferFormat bufferFormat, std::size_t numFrames, int32 *buffer, const int32 *inputBuffer) final
	{
		SoundCallbackLockedProcessImpl(bufferFormat, numFrames, buffer, inputBuffer);
	}
	inline void SoundCallbackLockedProcess(BufferFormat bufferFormat, std::size_t numFrames, float *buffer, const float *inputBuffer) final
	{
		SoundCallbackLockedProcessImpl(bufferFormat, numFrames, buffer, inputBuffer);
	}
	inline void SoundCallbackLockedProcess(BufferFormat bufferFormat, std::size_t numFrames, double *buffer, const double *inputBuffer) final
	{
		SoundCallbackLockedProcessImpl(bufferFormat, numFrames, buffer, inputBuffer);
	}
	virtual void SoundCallbackLockedCallback(CallbackBuffer<TDithers> &buffer) = 0;
};


}  // namespace SoundDevice


OPENMPT_NAMESPACE_END