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 typedef int_least32_t (
Mixer::*mixer_func_t)()
const;
82 typedef int (
Mixer::*scale_func_t)(
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;
110 randomLCG<VOLUME_MAX> m_rand;
115 int triangularDithering()
117 const int prevValue = m_oldRandomValue;
118 m_oldRandomValue = m_rand.get();
119 return m_oldRandomValue - prevValue;
122 int scale(
unsigned int ch)
124 const int_least32_t sample = (this->*(m_mix[ch]))();
125 return (sample * m_volume[ch] + triangularDithering()) /
VOLUME_MAX;
128 int noScale(
unsigned int ch)
130 return (this->*(m_mix[ch]))();
156 int_least32_t mono()
const
158 int_least32_t res = 0;
159 for (
int i = 0; i < Chips; i++)
160 res += m_iSamples[i];
165 int_least32_t stereo_OneChip()
const {
return m_iSamples[0]; }
167 int_least32_t stereo_ch1_TwoChips()
const {
return m_iSamples[0]; }
168 int_least32_t stereo_ch2_TwoChips()
const {
return m_iSamples[1]; }
170 int_least32_t stereo_ch1_ThreeChips()
const {
return (C1*m_iSamples[0] + C2*m_iSamples[1]) / SCALE_FACTOR; }
171 int_least32_t stereo_ch2_ThreeChips()
const {
return (C2*m_iSamples[1] + C1*m_iSamples[2]) / SCALE_FACTOR; }
180 m_mix.push_back(&Mixer::mono<1>);
206 void begin(
short *buffer, uint_least32_t count);
226 sidemu*
getSid(
unsigned int i)
const {
return (i < m_chips.size()) ? m_chips[i] :
nullptr; }
242 void setVolume(int_least32_t left, int_least32_t right);
261 bool notFinished()
const {
return m_sampleIndex != m_sampleCount; }