45 template <
int MAX_VAL>
48 static_assert((MAX_VAL != 0) && ((MAX_VAL & (MAX_VAL - 1)) == 0),
"MAX_VAL must be a power of two");
54 randomLCG(uint32_t seed) :
60 rand_seed = (214013 * rand_seed + 2531011);
61 return static_cast<int>((rand_seed >> 16) & (MAX_VAL-1));
72 static constexpr int_least32_t SCALE_FACTOR = 1 << 16;
74 static constexpr double SQRT_0_5 = 0.70710678118654746;
76 static constexpr int_least32_t C1 =
static_cast<int_least32_t
>(1.0 / (1.0 + SQRT_0_5) * SCALE_FACTOR);
77 static constexpr int_least32_t C2 =
static_cast<int_least32_t
>(SQRT_0_5 / (1.0 + SQRT_0_5) * SCALE_FACTOR);
80 using mixer_func_t = int_least32_t (
Mixer::*)()
const;
82 using scale_func_t = int (
Mixer::*)(
unsigned int);
89 std::vector<sidemu*> m_chips;
90 std::vector<short*> m_buffers;
92 std::vector<int_least32_t> m_iSamples;
93 std::vector<int_least32_t> m_volume;
95 std::vector<mixer_func_t> m_mix;
96 std::vector<scale_func_t> m_scale;
98 int m_oldRandomValue = 0;
99 int m_fastForwardFactor = 1;
102 short *m_sampleBuffer =
nullptr;
103 uint_least32_t m_sampleCount = 0;
104 uint_least32_t m_sampleIndex = 0;
106 uint_least32_t m_sampleRate = 0;
108 bool m_stereo =
false;
112 randomLCG<VOLUME_MAX> m_rand;
117 int triangularDithering()
119 const int prevValue = m_oldRandomValue;
120 m_oldRandomValue = m_rand.get();
121 return m_oldRandomValue - prevValue;
124 int scale(
unsigned int ch)
126 const int_least32_t sample = (this->*(m_mix[ch]))();
127 return (sample * m_volume[ch] + triangularDithering()) /
VOLUME_MAX;
130 int noScale(
unsigned int ch)
132 return (this->*(m_mix[ch]))();
158 int_least32_t mono()
const
160 int_least32_t res = 0;
161 for (
int i = 0; i < Chips; i++)
162 res += m_iSamples[i];
167 int_least32_t stereo_OneChip()
const {
return m_iSamples[0]; }
169 int_least32_t stereo_ch1_TwoChips()
const {
return m_iSamples[0]; }
170 int_least32_t stereo_ch2_TwoChips()
const {
return m_iSamples[1]; }
172 int_least32_t stereo_ch1_ThreeChips()
const {
return (C1*m_iSamples[0] + C2*m_iSamples[1]) / SCALE_FACTOR; }
173 int_least32_t stereo_ch2_ThreeChips()
const {
return (C2*m_iSamples[1] + C1*m_iSamples[2]) / SCALE_FACTOR; }
182 m_mix.push_back(&Mixer::mono<1>);
208 void begin(
short *buffer, uint_least32_t count);
228 sidemu*
getSid(
unsigned int i)
const {
return (i < m_chips.size()) ? m_chips[i] :
nullptr; }
244 void setVolume(int_least32_t left, int_least32_t right);
263 bool notFinished()
const {
return m_sampleIndex < m_sampleCount; }
273 bool wait()
const {
return m_wait; }