libsidplayfp 2.12.0
mixer.h
1/*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2011-2023 Leandro Nini <drfiemost@users.sourceforge.net>
5 * Copyright 2007-2010 Antti Lankila
6 * Copyright (C) 2000 Simon White
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23
24#ifndef MIXER_H
25#define MIXER_H
26
27#include "sidcxx11.h"
28
29#include <stdint.h>
30
31#include <vector>
32
33namespace libsidplayfp
34{
35
36class sidemu;
37
41class Mixer
42{
43private:
44 // random number generator for dithering
45 template <int MAX_VAL>
46 class randomLCG
47 {
48 static_assert((MAX_VAL != 0) && ((MAX_VAL & (MAX_VAL - 1)) == 0), "MAX_VAL must be a power of two");
49
50 private:
51 uint32_t rand_seed;
52
53 public:
54 randomLCG(uint32_t seed) :
55 rand_seed(seed)
56 {}
57
58 int get()
59 {
60 rand_seed = (214013 * rand_seed + 2531011);
61 return static_cast<int>((rand_seed >> 16) & (MAX_VAL-1));
62 }
63 };
64
65public:
66 class badBufferSize {};
67
68public:
70 static constexpr unsigned int MAX_SIDS = 3;
71
72private:
73 static constexpr int_least32_t SCALE_FACTOR = 1 << 16;
74
75 static constexpr double SQRT_2 = 1.41421356237;
76 static constexpr double SQRT_3 = 1.73205080757;
77
78 static constexpr int_least32_t SCALE[3] = {
79 SCALE_FACTOR, // 1 chip, no scale
80 static_cast<int_least32_t>((1.0 / SQRT_2) * SCALE_FACTOR), // 2 chips, scale by sqrt(2)
81 static_cast<int_least32_t>((1.0 / SQRT_3) * SCALE_FACTOR) // 3 chips, scale by sqrt(3)
82 };
83
84private:
85 using mixer_func_t = int_least32_t (Mixer::*)() const;
86
87 using scale_func_t = int (Mixer::*)(unsigned int);
88
89public:
91 static constexpr int_least32_t VOLUME_MAX = 1024;
92
93private:
94 std::vector<sidemu*> m_chips;
95 std::vector<short*> m_buffers;
96
97 std::vector<int_least32_t> m_iSamples;
98 std::vector<int_least32_t> m_volume;
99
100 std::vector<mixer_func_t> m_mix;
101 std::vector<scale_func_t> m_scale;
102
103 int m_oldRandomValue = 0;
104 int m_fastForwardFactor = 1;
105
106 // Mixer settings
107 short *m_sampleBuffer = nullptr;
108 uint_least32_t m_sampleCount = 0;
109 uint_least32_t m_sampleIndex = 0;
110
111 uint_least32_t m_sampleRate = 0;
112
113 bool m_stereo = false;
114
115 bool m_wait = false;
116
117 randomLCG<VOLUME_MAX> m_rand;
118
119private:
120 void updateParams();
121
122 int triangularDithering()
123 {
124 const int prevValue = m_oldRandomValue;
125 m_oldRandomValue = m_rand.get();
126 return m_oldRandomValue - prevValue;
127 }
128
129 int scale(unsigned int ch)
130 {
131 const int_least32_t sample = (this->*(m_mix[ch]))();
132 return (sample * m_volume[ch] + triangularDithering()) / VOLUME_MAX;
133 }
134
135 int noScale(unsigned int ch)
136 {
137 return (this->*(m_mix[ch]))();
138 }
139
140 /*
141 * Channel matrix
142 *
143 * C1
144 * L 1.0
145 * R 1.0
146 *
147 * C1 C2
148 * L 1.0 0.5
149 * R 0.5 1.0
150 *
151 * C1 C2 C3
152 * L 1.0 1.0 0.5
153 * R 0.5 1.0 1.0
154 */
155
156 // Mono mixing
157 template <int Chips>
158 int_least32_t mono() const
159 {
160 int_least32_t res = 0;
161 for (int i = 0; i < Chips; i++)
162 res += m_iSamples[i];
163 return res * SCALE[Chips-1] / SCALE_FACTOR;
164 }
165
166 // Stereo mixing
167 int_least32_t stereo_OneChip() const { return m_iSamples[0]; }
168
169 int_least32_t stereo_ch1_TwoChips() const
170 {
171 return (m_iSamples[0] + 0.5*m_iSamples[1]) * SCALE[1] / SCALE_FACTOR;
172 }
173 int_least32_t stereo_ch2_TwoChips() const
174 {
175 return (0.5*m_iSamples[0] + m_iSamples[1]) * SCALE[1] / SCALE_FACTOR;
176 }
177
178 int_least32_t stereo_ch1_ThreeChips() const
179 {
180 return (m_iSamples[0] + m_iSamples[1] + 0.5*m_iSamples[2]) * SCALE[2] / SCALE_FACTOR;
181 }
182 int_least32_t stereo_ch2_ThreeChips() const
183 {
184 return (0.5*m_iSamples[0] + m_iSamples[1] + m_iSamples[2]) * SCALE[2] / SCALE_FACTOR;
185 }
186
187public:
192 m_rand(257254)
193 {
194 m_mix.push_back(&Mixer::mono<1>);
195 }
196
200 void doMix();
201
205 void clockChips();
206
210 void resetBufs();
211
220 void begin(short *buffer, uint_least32_t count);
221
225 void clearSids();
226
232 void addSid(sidemu *chip);
233
240 sidemu* getSid(unsigned int i) const { return (i < m_chips.size()) ? m_chips[i] : nullptr; }
241
248 bool setFastForward(int ff);
249
256 void setVolume(int_least32_t left, int_least32_t right);
257
263 void setStereo(bool stereo);
264
270 void setSamplerate(uint_least32_t rate);
271
275 bool notFinished() const { return m_sampleIndex < m_sampleCount; }
276
280 uint_least32_t samplesGenerated() const { return m_sampleIndex; }
281
282 /*
283 * Wait till we consume the buffer.
284 */
285 bool wait() const { return m_wait; }
286};
287
288}
289
290#endif // MIXER_H
Definition mixer.h:42
void addSid(sidemu *chip)
Definition mixer.cpp:142
void resetBufs()
Definition mixer.cpp:40
Mixer()
Definition mixer.h:191
void clearSids()
Definition mixer.cpp:136
bool notFinished() const
Definition mixer.h:275
void clockChips()
Definition mixer.cpp:34
void setStereo(bool stereo)
Definition mixer.cpp:156
void setVolume(int_least32_t left, int_least32_t right)
Definition mixer.cpp:182
static constexpr int_least32_t VOLUME_MAX
Maximum allowed volume, must be a power of 2.
Definition mixer.h:91
sidemu * getSid(unsigned int i) const
Definition mixer.h:240
uint_least32_t samplesGenerated() const
Definition mixer.h:280
void setSamplerate(uint_least32_t rate)
Definition mixer.cpp:168
void doMix()
Definition mixer.cpp:46
void begin(short *buffer, uint_least32_t count)
Definition mixer.cpp:102
static constexpr unsigned int MAX_SIDS
Maximum number of supported SIDs.
Definition mixer.h:70
bool setFastForward(int ff)
Definition mixer.cpp:173
Definition sidemu.h:47