libsidplayfp 2.13.1
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
96 std::vector<int_least32_t> m_iSamples;
97 std::vector<int_least32_t> m_volume;
98
99 std::vector<mixer_func_t> m_mix;
100 std::vector<scale_func_t> m_scale;
101
102 int m_oldRandomValue = 0;
103 int m_fastForwardFactor = 1;
104
105 // Mixer settings
106 short *m_sampleBuffer = nullptr;
107 uint_least32_t m_sampleCount = 0;
108 uint_least32_t m_sampleIndex = 0;
109
110 uint_least32_t m_sampleRate = 0;
111
112 bool m_stereo = false;
113
114 bool m_wait = false;
115
116 randomLCG<VOLUME_MAX> m_rand;
117
118private:
119 void updateParams();
120
121 int triangularDithering()
122 {
123 const int prevValue = m_oldRandomValue;
124 m_oldRandomValue = m_rand.get();
125 return m_oldRandomValue - prevValue;
126 }
127
128 int scale(unsigned int ch)
129 {
130 const int_least32_t sample = (this->*(m_mix[ch]))();
131 return (sample * m_volume[ch] + triangularDithering()) / VOLUME_MAX;
132 }
133
134 int noScale(unsigned int ch)
135 {
136 return (this->*(m_mix[ch]))();
137 }
138
139 /*
140 * Channel matrix
141 *
142 * C1
143 * L 1.0
144 * R 1.0
145 *
146 * C1 C2
147 * L 1.0 0.5
148 * R 0.5 1.0
149 *
150 * C1 C2 C3
151 * L 1.0 1.0 0.5
152 * R 0.5 1.0 1.0
153 */
154
155 // Mono mixing
156 template <int Chips>
157 int_least32_t mono() const
158 {
159 int_least32_t res = 0;
160 for (int i = 0; i < Chips; i++)
161 res += m_iSamples[i];
162 return res * SCALE[Chips-1] / SCALE_FACTOR;
163 }
164
165 // Stereo mixing
166 int_least32_t stereo_OneChip() const { return m_iSamples[0]; }
167
168 int_least32_t stereo_ch1_TwoChips() const
169 {
170 return (m_iSamples[0] + 0.5*m_iSamples[1]) * SCALE[1] / SCALE_FACTOR;
171 }
172 int_least32_t stereo_ch2_TwoChips() const
173 {
174 return (0.5*m_iSamples[0] + m_iSamples[1]) * SCALE[1] / SCALE_FACTOR;
175 }
176
177 int_least32_t stereo_ch1_ThreeChips() const
178 {
179 return (m_iSamples[0] + m_iSamples[1] + 0.5*m_iSamples[2]) * SCALE[2] / SCALE_FACTOR;
180 }
181 int_least32_t stereo_ch2_ThreeChips() const
182 {
183 return (0.5*m_iSamples[0] + m_iSamples[1] + m_iSamples[2]) * SCALE[2] / SCALE_FACTOR;
184 }
185
186public:
191 m_rand(257254)
192 {
193 m_mix.push_back(&Mixer::mono<1>);
194 }
195
199 void doMix();
200
204 void clockChips();
205
209 void resetBufs();
210
219 void begin(short *buffer, uint_least32_t count);
220
224 void clearSids();
225
231 void addSid(sidemu *chip);
232
239 sidemu* getSid(unsigned int i) const { return (i < m_chips.size()) ? m_chips[i] : nullptr; }
240
247 bool setFastForward(int ff);
248
255 void setVolume(int_least32_t left, int_least32_t right);
256
262 void setStereo(bool stereo);
263
269 void setSamplerate(uint_least32_t rate);
270
274 bool notFinished() const { return m_sampleIndex < m_sampleCount; }
275
279 uint_least32_t samplesGenerated() const { return m_sampleIndex; }
280
281 /*
282 * Wait till we consume the buffer.
283 */
284 bool wait() const { return m_wait; }
285};
286
287}
288
289#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:190
void clearSids()
Definition mixer.cpp:137
bool notFinished() const
Definition mixer.h:274
void clockChips()
Definition mixer.cpp:34
void setStereo(bool stereo)
Definition mixer.cpp:155
void setVolume(int_least32_t left, int_least32_t right)
Definition mixer.cpp:181
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:239
uint_least32_t samplesGenerated() const
Definition mixer.h:279
void setSamplerate(uint_least32_t rate)
Definition mixer.cpp:167
void doMix()
Definition mixer.cpp:46
void begin(short *buffer, uint_least32_t count)
Definition mixer.cpp:103
static constexpr unsigned int MAX_SIDS
Maximum number of supported SIDs.
Definition mixer.h:70
bool setFastForward(int ff)
Definition mixer.cpp:172
Definition sidemu.h:47