libsidplayfp 2.7.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#ifdef HAVE_CXX11
49 static_assert((MAX_VAL != 0) && ((MAX_VAL & (MAX_VAL - 1)) == 0), "MAX_VAL must be a power of two");
50#endif
51 private:
52 uint32_t rand_seed;
53
54 public:
55 randomLCG(uint32_t seed) :
56 rand_seed(seed)
57 {}
58
59 int get()
60 {
61 rand_seed = (214013 * rand_seed + 2531011);
62 return static_cast<int>((rand_seed >> 16) & (MAX_VAL-1));
63 }
64 };
65
66public:
67 class badBufferSize {};
68
69public:
71 static const unsigned int MAX_SIDS = 3;
72
73 static const int_least32_t SCALE_FACTOR = 1 << 16;
74#ifdef HAVE_CXX11
75 static constexpr double SQRT_0_5 = 0.70710678118654746;
76#else
77# define SQRT_0_5 0.70710678118654746
78#endif
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);
81
82private:
83 typedef int_least32_t (Mixer::*mixer_func_t)() const;
84
85 typedef int (Mixer::*scale_func_t)(unsigned int);
86
87public:
89 static const int_least32_t VOLUME_MAX = 1024;
90
91private:
92 std::vector<sidemu*> m_chips;
93 std::vector<short*> m_buffers;
94
95 std::vector<int_least32_t> m_iSamples;
96 std::vector<int_least32_t> m_volume;
97
98 std::vector<mixer_func_t> m_mix;
99 std::vector<scale_func_t> m_scale;
100
101 int m_oldRandomValue;
102 int m_fastForwardFactor;
103
104 // Mixer settings
105 short *m_sampleBuffer;
106 uint_least32_t m_sampleCount;
107 uint_least32_t m_sampleIndex;
108
109 uint_least32_t m_sampleRate;
110
111 bool m_stereo;
112
113 randomLCG<VOLUME_MAX> m_rand;
114
115private:
116 void updateParams();
117
118 int triangularDithering()
119 {
120 const int prevValue = m_oldRandomValue;
121 m_oldRandomValue = m_rand.get();
122 return m_oldRandomValue - prevValue;
123 }
124
125 int scale(unsigned int ch)
126 {
127 const int_least32_t sample = (this->*(m_mix[ch]))();
128 return (sample * m_volume[ch] + triangularDithering()) / VOLUME_MAX;
129 }
130
131 int noScale(unsigned int ch)
132 {
133 return (this->*(m_mix[ch]))();
134 }
135
136 /*
137 * Channel matrix
138 *
139 * C1
140 * L 1.0
141 * R 1.0
142 *
143 * C1 C2
144 * L 1.0 0.0
145 * R 0.0 1.0
146 *
147 * C1 C2 C3
148 * L 1/1.707 0.707/1.707 0.0
149 * R 0.0 0.707/1.707 1/1.707
150 *
151 * FIXME
152 * it seems that scaling down the summed signals is not the correct way of mixing, see:
153 * http://dsp.stackexchange.com/questions/3581/algorithms-to-mix-audio-signals-without-clipping
154 * maybe we should consider some form of soft/hard clipping instead to avoid possible overflows
155 */
156
157 // Mono mixing
158 template <int Chips>
159 int_least32_t mono() const
160 {
161 int_least32_t res = 0;
162 for (int i = 0; i < Chips; i++)
163 res += m_iSamples[i];
164 return res / Chips;
165 }
166
167 // Stereo mixing
168 int_least32_t stereo_OneChip() const { return m_iSamples[0]; }
169
170 int_least32_t stereo_ch1_TwoChips() const { return m_iSamples[0]; }
171 int_least32_t stereo_ch2_TwoChips() const { return m_iSamples[1]; }
172
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; }
175
176public:
181 m_oldRandomValue(0),
182 m_fastForwardFactor(1),
183 m_sampleCount(0),
184 m_sampleRate(0),
185 m_stereo(false),
186 m_rand(257254)
187 {
188 m_mix.push_back(&Mixer::mono<1>);
189 }
190
194 void doMix();
195
199 void clockChips();
200
204 void resetBufs();
205
214 void begin(short *buffer, uint_least32_t count);
215
219 void clearSids();
220
226 void addSid(sidemu *chip);
227
234 sidemu* getSid(unsigned int i) const { return (i < m_chips.size()) ? m_chips[i] : nullptr; }
235
242 bool setFastForward(int ff);
243
250 void setVolume(int_least32_t left, int_least32_t right);
251
257 void setStereo(bool stereo);
258
264 void setSamplerate(uint_least32_t rate);
265
269 bool notFinished() const { return m_sampleIndex != m_sampleCount; }
270
274 uint_least32_t samplesGenerated() const { return m_sampleIndex; }
275};
276
277}
278
279#endif // MIXER_H
Definition mixer.h:42
void addSid(sidemu *chip)
Definition mixer.cpp:174
static const int_least32_t VOLUME_MAX
Maximum allowed volume, must be a power of 2.
Definition mixer.h:89
void resetBufs()
Definition mixer.cpp:69
Mixer()
Definition mixer.h:180
void clearSids()
Definition mixer.cpp:168
bool notFinished() const
Definition mixer.h:269
void clockChips()
Definition mixer.cpp:64
void setStereo(bool stereo)
Definition mixer.cpp:188
void setVolume(int_least32_t left, int_least32_t right)
Definition mixer.cpp:214
sidemu * getSid(unsigned int i) const
Definition mixer.h:234
uint_least32_t samplesGenerated() const
Definition mixer.h:274
void setSamplerate(uint_least32_t rate)
Definition mixer.cpp:200
void doMix()
Definition mixer.cpp:74
void begin(short *buffer, uint_least32_t count)
Definition mixer.cpp:130
bool setFastForward(int ff)
Definition mixer.cpp:205
static const unsigned int MAX_SIDS
Maximum number of supported SIDs.
Definition mixer.h:71
Definition sidemu.h:47