OXYGENHUB
◆ main openmsx / src / sound / BlipBuffer.hh
1.2 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
// Heavily based on:
2
//
3
// Band-limited sound synthesis and buffering
4
// Blip_Buffer 0.4.0
5
// http://www.slack.net/~ant/
6
7
#ifndef BLIPBUFFER_HH
8
#define BLIPBUFFER_HH
9
10
#include "FixedPoint.hh"
11
#include <array>
12
13
namespace openmsx {
14
15
class BlipBuffer
16
{
17
public:
18
	// Number of bits in phase offset. Fewer than 6 bits (64 phase offsets) results
19
	// in noticeable broadband noise when synthesizing high frequency square waves.
20
	static constexpr int BLIP_PHASE_BITS = 10;
21
22
	using TimeIndex = FixedPoint<BLIP_PHASE_BITS>;
23
24
	BlipBuffer();
25
26
	// Update amplitude of waveform at given time. Time is in output sample
27
	// units and since the last time readSamples() was called.
28
	void addDelta(TimeIndex time, float delta);
29
30
	// Read the given amount of samples into destination buffer.
31
	template<size_t PITCH>
32
	bool readSamples(float* out, size_t samples);
33
34
private:
35
	template<size_t PITCH>
36
	void readSamplesHelper(float* out, size_t samples);
37
38
private:
39
	static constexpr size_t BUFFER_SIZE = 1 << 14;
40
	static constexpr size_t BUFFER_MASK = BUFFER_SIZE - 1;
41
	std::array<float, BUFFER_SIZE> buffer;
42
	size_t offset = 0;
43
	ptrdiff_t availSamp = 0;
44
	float accum = 0.0f;
45
};
46
47
} // namespace openmsx
48
49
#endif