libsidplayfp  2.15.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 
33 namespace libsidplayfp
34 {
35 
36 class sidemu;
37 
41 class Mixer
42 {
43 private:
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 
65 public:
66  class badBufferSize {};
67 
68 public:
70  static constexpr unsigned int MAX_SIDS = 3;
71 
72 private:
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 
84 private:
85  using mixer_func_t = int_least32_t (Mixer::*)() const;
86 
87  using scale_func_t = int (Mixer::*)(unsigned int);
88 
89 public:
91  static constexpr int_least32_t VOLUME_MAX = 1024;
92 
93 private:
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  bool m_stereo = false;
111 
112  bool m_wait = false;
113 
114  randomLCG<VOLUME_MAX> m_rand;
115 
116 private:
117  void updateParams();
118 
119  int triangularDithering()
120  {
121  const int prevValue = m_oldRandomValue;
122  m_oldRandomValue = m_rand.get();
123  return m_oldRandomValue - prevValue;
124  }
125 
126  int scale(unsigned int ch)
127  {
128  const int_least32_t sample = (this->*(m_mix[ch]))();
129  return (sample * m_volume[ch] + triangularDithering()) / VOLUME_MAX;
130  }
131 
132  int noScale(unsigned int ch)
133  {
134  return (this->*(m_mix[ch]))();
135  }
136 
137  /*
138  * Channel matrix
139  *
140  * C1
141  * L 1.0
142  * R 1.0
143  *
144  * C1 C2
145  * L 1.0 0.5
146  * R 0.5 1.0
147  *
148  * C1 C2 C3
149  * L 1.0 1.0 0.5
150  * R 0.5 1.0 1.0
151  */
152 
153  // Mono mixing
154  template <int Chips>
155  int_least32_t mono() const
156  {
157  int_least32_t res = 0;
158  for (int i = 0; i < Chips; i++)
159  res += m_iSamples[i];
160  return res * SCALE[Chips-1] / SCALE_FACTOR;
161  }
162 
163  // Stereo mixing
164  int_least32_t stereo_OneChip() const { return m_iSamples[0]; }
165 
166  int_least32_t stereo_ch1_TwoChips() const
167  {
168  return (m_iSamples[0] + 0.5*m_iSamples[1]) * SCALE[1] / SCALE_FACTOR;
169  }
170  int_least32_t stereo_ch2_TwoChips() const
171  {
172  return (0.5*m_iSamples[0] + m_iSamples[1]) * SCALE[1] / SCALE_FACTOR;
173  }
174 
175  int_least32_t stereo_ch1_ThreeChips() const
176  {
177  return (m_iSamples[0] + m_iSamples[1] + 0.5*m_iSamples[2]) * SCALE[2] / SCALE_FACTOR;
178  }
179  int_least32_t stereo_ch2_ThreeChips() const
180  {
181  return (0.5*m_iSamples[0] + m_iSamples[1] + m_iSamples[2]) * SCALE[2] / SCALE_FACTOR;
182  }
183 
184 public:
188  Mixer() :
189  m_rand(257254)
190  {
191  m_mix.push_back(&Mixer::mono<1>);
192  }
193 
197  void doMix();
198 
202  void clockChips();
203 
207  void resetBufs();
208 
217  void begin(short *buffer, uint_least32_t count);
218 
222  void clearSids() { m_chips.clear(); }
223 
229  void addSid(sidemu *chip);
230 
237  sidemu* getSid(unsigned int i) const { return (i < m_chips.size()) ? m_chips[i] : nullptr; }
238 
245  bool setFastForward(int ff);
246 
253  void setVolume(int_least32_t left, int_least32_t right);
254 
260  void setStereo(bool stereo);
261 
265  bool notFinished() const { return m_sampleIndex < m_sampleCount; }
266 
270  uint_least32_t samplesGenerated() const { return m_sampleIndex; }
271 
272  /*
273  * Wait till we consume the buffer.
274  */
275  bool wait() const { return m_wait; }
276 };
277 
278 }
279 
280 #endif // MIXER_H
Definition: mixer.h:42
void addSid(sidemu *chip)
Definition: mixer.cpp:137
void resetBufs()
Definition: mixer.cpp:40
Mixer()
Definition: mixer.h:188
void clearSids()
Definition: mixer.h:222
bool notFinished() const
Definition: mixer.h:265
void clockChips()
Definition: mixer.cpp:34
void setStereo(bool stereo)
Definition: mixer.cpp:150
void setVolume(int_least32_t left, int_least32_t right)
Definition: mixer.cpp:171
static constexpr int_least32_t VOLUME_MAX
Maximum allowed volume, must be a power of 2.
Definition: mixer.h:91
uint_least32_t samplesGenerated() const
Definition: mixer.h:270
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:162
sidemu * getSid(unsigned int i) const
Definition: mixer.h:237
Definition: sidemu.h:47