libsidplayfp 2.11.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
72 static constexpr int_least32_t SCALE_FACTOR = 1 << 16;
73
74 static constexpr double SQRT_0_5 = 0.70710678118654746;
75
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);
78
79private:
80 using mixer_func_t = int_least32_t (Mixer::*)() const;
81
82 using scale_func_t = int (Mixer::*)(unsigned int);
83
84public:
86 static constexpr int_least32_t VOLUME_MAX = 1024;
87
88private:
89 std::vector<sidemu*> m_chips;
90 std::vector<short*> m_buffers;
91
92 std::vector<int_least32_t> m_iSamples;
93 std::vector<int_least32_t> m_volume;
94
95 std::vector<mixer_func_t> m_mix;
96 std::vector<scale_func_t> m_scale;
97
98 int m_oldRandomValue = 0;
99 int m_fastForwardFactor = 1;
100
101 // Mixer settings
102 short *m_sampleBuffer = nullptr;
103 uint_least32_t m_sampleCount = 0;
104 uint_least32_t m_sampleIndex = 0;
105
106 uint_least32_t m_sampleRate = 0;
107
108 bool m_stereo = false;
109
110 bool m_wait = false;
111
112 randomLCG<VOLUME_MAX> m_rand;
113
114private:
115 void updateParams();
116
117 int triangularDithering()
118 {
119 const int prevValue = m_oldRandomValue;
120 m_oldRandomValue = m_rand.get();
121 return m_oldRandomValue - prevValue;
122 }
123
124 int scale(unsigned int ch)
125 {
126 const int_least32_t sample = (this->*(m_mix[ch]))();
127 return (sample * m_volume[ch] + triangularDithering()) / VOLUME_MAX;
128 }
129
130 int noScale(unsigned int ch)
131 {
132 return (this->*(m_mix[ch]))();
133 }
134
135 /*
136 * Channel matrix
137 *
138 * C1
139 * L 1.0
140 * R 1.0
141 *
142 * C1 C2
143 * L 1.0 0.0
144 * R 0.0 1.0
145 *
146 * C1 C2 C3
147 * L 1/1.707 0.707/1.707 0.0
148 * R 0.0 0.707/1.707 1/1.707
149 *
150 * FIXME
151 * it seems that scaling down the summed signals is not the correct way of mixing, see:
152 * http://dsp.stackexchange.com/questions/3581/algorithms-to-mix-audio-signals-without-clipping
153 * maybe we should consider some form of soft/hard clipping instead to avoid possible overflows
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 / Chips;
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 { return m_iSamples[0]; }
170 int_least32_t stereo_ch2_TwoChips() const { return m_iSamples[1]; }
171
172 int_least32_t stereo_ch1_ThreeChips() const { return (C1*m_iSamples[0] + C2*m_iSamples[1]) / SCALE_FACTOR; }
173 int_least32_t stereo_ch2_ThreeChips() const { return (C2*m_iSamples[1] + C1*m_iSamples[2]) / SCALE_FACTOR; }
174
175public:
180 m_rand(257254)
181 {
182 m_mix.push_back(&Mixer::mono<1>);
183 }
184
188 void doMix();
189
193 void clockChips();
194
198 void resetBufs();
199
208 void begin(short *buffer, uint_least32_t count);
209
213 void clearSids();
214
220 void addSid(sidemu *chip);
221
228 sidemu* getSid(unsigned int i) const { return (i < m_chips.size()) ? m_chips[i] : nullptr; }
229
236 bool setFastForward(int ff);
237
244 void setVolume(int_least32_t left, int_least32_t right);
245
251 void setStereo(bool stereo);
252
258 void setSamplerate(uint_least32_t rate);
259
263 bool notFinished() const { return m_sampleIndex < m_sampleCount; }
264
268 uint_least32_t samplesGenerated() const { return m_sampleIndex; }
269
270 /*
271 * Wait till we consume the buffer.
272 */
273 bool wait() const { return m_wait; }
274};
275
276}
277
278#endif // MIXER_H
Definition mixer.h:42
void addSid(sidemu *chip)
Definition mixer.cpp:140
void resetBufs()
Definition mixer.cpp:40
Mixer()
Definition mixer.h:179
void clearSids()
Definition mixer.cpp:134
bool notFinished() const
Definition mixer.h:263
void clockChips()
Definition mixer.cpp:34
void setStereo(bool stereo)
Definition mixer.cpp:154
void setVolume(int_least32_t left, int_least32_t right)
Definition mixer.cpp:180
static constexpr int_least32_t VOLUME_MAX
Maximum allowed volume, must be a power of 2.
Definition mixer.h:86
sidemu * getSid(unsigned int i) const
Definition mixer.h:228
uint_least32_t samplesGenerated() const
Definition mixer.h:268
void setSamplerate(uint_least32_t rate)
Definition mixer.cpp:166
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:171
Definition sidemu.h:47