libsidplayfp 2.8.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 const unsigned int MAX_SIDS = 3;
71
72 static const int_least32_t SCALE_FACTOR = 1 << 16;
73
74 static constexpr double SQRT_0_5 = 0.70710678118654746;
75
76 static const int_least32_t C1 = static_cast<int_least32_t>(1.0 / (1.0 + SQRT_0_5) * SCALE_FACTOR);
77 static const int_least32_t C2 = static_cast<int_least32_t>(SQRT_0_5 / (1.0 + SQRT_0_5) * SCALE_FACTOR);
78
79private:
80 typedef int_least32_t (Mixer::*mixer_func_t)() const;
81
82 typedef int (Mixer::*scale_func_t)(unsigned int);
83
84public:
86 static const 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;
99 int m_fastForwardFactor;
100
101 // Mixer settings
102 short *m_sampleBuffer;
103 uint_least32_t m_sampleCount;
104 uint_least32_t m_sampleIndex;
105
106 uint_least32_t m_sampleRate;
107
108 bool m_stereo;
109
110 randomLCG<VOLUME_MAX> m_rand;
111
112private:
113 void updateParams();
114
115 int triangularDithering()
116 {
117 const int prevValue = m_oldRandomValue;
118 m_oldRandomValue = m_rand.get();
119 return m_oldRandomValue - prevValue;
120 }
121
122 int scale(unsigned int ch)
123 {
124 const int_least32_t sample = (this->*(m_mix[ch]))();
125 return (sample * m_volume[ch] + triangularDithering()) / VOLUME_MAX;
126 }
127
128 int noScale(unsigned int ch)
129 {
130 return (this->*(m_mix[ch]))();
131 }
132
133 /*
134 * Channel matrix
135 *
136 * C1
137 * L 1.0
138 * R 1.0
139 *
140 * C1 C2
141 * L 1.0 0.0
142 * R 0.0 1.0
143 *
144 * C1 C2 C3
145 * L 1/1.707 0.707/1.707 0.0
146 * R 0.0 0.707/1.707 1/1.707
147 *
148 * FIXME
149 * it seems that scaling down the summed signals is not the correct way of mixing, see:
150 * http://dsp.stackexchange.com/questions/3581/algorithms-to-mix-audio-signals-without-clipping
151 * maybe we should consider some form of soft/hard clipping instead to avoid possible overflows
152 */
153
154 // Mono mixing
155 template <int Chips>
156 int_least32_t mono() const
157 {
158 int_least32_t res = 0;
159 for (int i = 0; i < Chips; i++)
160 res += m_iSamples[i];
161 return res / Chips;
162 }
163
164 // Stereo mixing
165 int_least32_t stereo_OneChip() const { return m_iSamples[0]; }
166
167 int_least32_t stereo_ch1_TwoChips() const { return m_iSamples[0]; }
168 int_least32_t stereo_ch2_TwoChips() const { return m_iSamples[1]; }
169
170 int_least32_t stereo_ch1_ThreeChips() const { return (C1*m_iSamples[0] + C2*m_iSamples[1]) / SCALE_FACTOR; }
171 int_least32_t stereo_ch2_ThreeChips() const { return (C2*m_iSamples[1] + C1*m_iSamples[2]) / SCALE_FACTOR; }
172
173public:
178 m_oldRandomValue(0),
179 m_fastForwardFactor(1),
180 m_sampleCount(0),
181 m_sampleRate(0),
182 m_stereo(false),
183 m_rand(257254)
184 {
185 m_mix.push_back(&Mixer::mono<1>);
186 }
187
191 void doMix();
192
196 void clockChips();
197
201 void resetBufs();
202
211 void begin(short *buffer, uint_least32_t count);
212
216 void clearSids();
217
223 void addSid(sidemu *chip);
224
231 sidemu* getSid(unsigned int i) const { return (i < m_chips.size()) ? m_chips[i] : nullptr; }
232
239 bool setFastForward(int ff);
240
247 void setVolume(int_least32_t left, int_least32_t right);
248
254 void setStereo(bool stereo);
255
261 void setSamplerate(uint_least32_t rate);
262
266 bool notFinished() const { return m_sampleIndex != m_sampleCount; }
267
271 uint_least32_t samplesGenerated() const { return m_sampleIndex; }
272};
273
274}
275
276#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:86
void resetBufs()
Definition mixer.cpp:69
Mixer()
Definition mixer.h:177
void clearSids()
Definition mixer.cpp:168
bool notFinished() const
Definition mixer.h:266
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:231
uint_least32_t samplesGenerated() const
Definition mixer.h:271
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:70
Definition sidemu.h:47