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));
73 static constexpr int_least32_t SCALE_FACTOR = 1 << 16;
75 static constexpr double SQRT_2 = 1.41421356237;
76 static constexpr double SQRT_3 = 1.73205080757;
78 static constexpr int_least32_t SCALE[3] = {
80 static_cast<int_least32_t
>((1.0 / SQRT_2) * SCALE_FACTOR),
81 static_cast<int_least32_t
>((1.0 / SQRT_3) * SCALE_FACTOR)
85 using mixer_func_t = int_least32_t (
Mixer::*)() const;
87 using scale_func_t = int (
Mixer::*)(unsigned int);
94 std::vector<sidemu*> m_chips;
96 std::vector<int_least32_t> m_iSamples;
97 std::vector<int_least32_t> m_volume;
99 std::vector<mixer_func_t> m_mix;
100 std::vector<scale_func_t> m_scale;
102 int m_oldRandomValue = 0;
103 int m_fastForwardFactor = 1;
106 short *m_sampleBuffer =
nullptr;
107 uint_least32_t m_sampleCount = 0;
108 uint_least32_t m_sampleIndex = 0;
110 bool m_stereo =
false;
114 randomLCG<VOLUME_MAX> m_rand;
119 int triangularDithering()
121 const int prevValue = m_oldRandomValue;
122 m_oldRandomValue = m_rand.get();
123 return m_oldRandomValue - prevValue;
126 int scale(
unsigned int ch)
128 const int_least32_t sample = (this->*(m_mix[ch]))();
129 return (sample * m_volume[ch] + triangularDithering()) /
VOLUME_MAX;
132 int noScale(
unsigned int ch)
134 return (this->*(m_mix[ch]))();
155 int_least32_t mono()
const
157 int_least32_t res = 0;
158 for (
int i = 0; i < Chips; i++)
159 res += m_iSamples[i];
160 return res * SCALE[Chips-1] / SCALE_FACTOR;
164 int_least32_t stereo_OneChip()
const {
return m_iSamples[0]; }
166 int_least32_t stereo_ch1_TwoChips()
const
168 return (m_iSamples[0] + 0.5*m_iSamples[1]) * SCALE[1] / SCALE_FACTOR;
170 int_least32_t stereo_ch2_TwoChips()
const
172 return (0.5*m_iSamples[0] + m_iSamples[1]) * SCALE[1] / SCALE_FACTOR;
175 int_least32_t stereo_ch1_ThreeChips()
const
177 return (m_iSamples[0] + m_iSamples[1] + 0.5*m_iSamples[2]) * SCALE[2] / SCALE_FACTOR;
179 int_least32_t stereo_ch2_ThreeChips()
const
181 return (0.5*m_iSamples[0] + m_iSamples[1] + m_iSamples[2]) * SCALE[2] / SCALE_FACTOR;
191 m_mix.push_back(&Mixer::mono<1>);
217 void begin(
short *buffer, uint_least32_t count);
237 sidemu*
getSid(
unsigned int i)
const {
return (i < m_chips.size()) ? m_chips[i] :
nullptr; }
253 void setVolume(int_least32_t left, int_least32_t right);
265 bool notFinished()
const {
return m_sampleIndex < m_sampleCount; }
275 bool wait()
const {
return m_wait; }