45 template <
int MAX_VAL>
49 static_assert((MAX_VAL != 0) && ((MAX_VAL & (MAX_VAL - 1)) == 0),
"MAX_VAL must be a power of two");
55 randomLCG(uint32_t seed) :
61 rand_seed = (214013 * rand_seed + 2531011);
62 return static_cast<int>((rand_seed >> 16) & (MAX_VAL-1));
73 static const int_least32_t SCALE_FACTOR = 1 << 16;
75 static constexpr double SQRT_0_5 = 0.70710678118654746;
77# define SQRT_0_5 0.70710678118654746
79 static const int_least32_t C1 =
static_cast<int_least32_t
>(1.0 / (1.0 + SQRT_0_5) * SCALE_FACTOR);
80 static const int_least32_t C2 =
static_cast<int_least32_t
>(SQRT_0_5 / (1.0 + SQRT_0_5) * SCALE_FACTOR);
83 typedef int_least32_t (
Mixer::*mixer_func_t)() const;
85 typedef int (
Mixer::*scale_func_t)(unsigned int);
92 std::vector<sidemu*> m_chips;
93 std::vector<short*> m_buffers;
95 std::vector<int_least32_t> m_iSamples;
96 std::vector<int_least32_t> m_volume;
98 std::vector<mixer_func_t> m_mix;
99 std::vector<scale_func_t> m_scale;
101 int m_oldRandomValue;
102 int m_fastForwardFactor;
105 short *m_sampleBuffer;
106 uint_least32_t m_sampleCount;
107 uint_least32_t m_sampleIndex;
109 uint_least32_t m_sampleRate;
113 randomLCG<VOLUME_MAX> m_rand;
118 int triangularDithering()
120 const int prevValue = m_oldRandomValue;
121 m_oldRandomValue = m_rand.get();
122 return m_oldRandomValue - prevValue;
125 int scale(
unsigned int ch)
127 const int_least32_t sample = (this->*(m_mix[ch]))();
128 return (sample * m_volume[ch] + triangularDithering()) /
VOLUME_MAX;
131 int noScale(
unsigned int ch)
133 return (this->*(m_mix[ch]))();
159 int_least32_t mono()
const
161 int_least32_t res = 0;
162 for (
int i = 0; i < Chips; i++)
163 res += m_iSamples[i];
168 int_least32_t stereo_OneChip()
const {
return m_iSamples[0]; }
170 int_least32_t stereo_ch1_TwoChips()
const {
return m_iSamples[0]; }
171 int_least32_t stereo_ch2_TwoChips()
const {
return m_iSamples[1]; }
173 int_least32_t stereo_ch1_ThreeChips()
const {
return (C1*m_iSamples[0] + C2*m_iSamples[1]) / SCALE_FACTOR; }
174 int_least32_t stereo_ch2_ThreeChips()
const {
return (C2*m_iSamples[1] + C1*m_iSamples[2]) / SCALE_FACTOR; }
182 m_fastForwardFactor(1),
188 m_mix.push_back(&Mixer::mono<1>);
214 void begin(
short *buffer, uint_least32_t count);
234 sidemu*
getSid(
unsigned int i)
const {
return (i < m_chips.size()) ? m_chips[i] :
nullptr; }
250 void setVolume(int_least32_t left, int_least32_t right);
269 bool notFinished()
const {
return m_sampleIndex != m_sampleCount; }