libsidplayfp  1.7.2
mixer.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2015 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 <stdint.h>
28 #include <cstdlib>
29 
30 #include <vector>
31 
32 class sidemu;
33 
37 class Mixer
38 {
39 public:
41  static const unsigned int MAX_SIDS = 2;
42 
43 private:
44  typedef int_least32_t (Mixer::*mixer_func_t)() const;
45 
46 public:
48  static const int_least32_t VOLUME_MAX = 1024;
49 
50 private:
51  std::vector<sidemu*> m_chips;
52  std::vector<short*> m_buffers;
53 
54  std::vector<int_least32_t> m_iSamples;
55  std::vector<int_least32_t> m_volume;
56 
57  std::vector<mixer_func_t> m_mix;
58 
59  int oldRandomValue;
60  int m_fastForwardFactor;
61 
62  // Mixer settings
63  short *m_sampleBuffer;
64  uint_least32_t m_sampleCount;
65  uint_least32_t m_sampleIndex;
66 
67  bool m_stereo;
68 
69 private:
70  void updateParams();
71 
72  int triangularDithering()
73  {
74  const int prevValue = oldRandomValue;
75  oldRandomValue = rand() & (VOLUME_MAX-1);
76  return oldRandomValue - prevValue;
77  }
78 
79  int_least32_t channel1MonoMix() const { return (m_iSamples[0] + m_iSamples[1]) / 2; }
80  int_least32_t channel1StereoMix() const { return m_iSamples[0]; }
81 
82  int_least32_t channel2FromMonoMix() const { return m_iSamples[0]; }
83  int_least32_t channel2FromStereoMix() const { return m_iSamples[1]; }
84 
85 public:
89  Mixer() :
90  oldRandomValue(0),
91  m_fastForwardFactor(1),
92  m_sampleCount(0),
93  m_stereo(false)
94  {
95  m_mix.push_back(&Mixer::channel1MonoMix);
96  }
97 
101  void doMix();
102 
106  void clockChips();
107 
111  void resetBufs();
112 
119  void begin(short *buffer, uint_least32_t count);
120 
124  void clearSids();
125 
131  void addSid(sidemu *chip);
132 
139  sidemu* getSid(unsigned int i) const { return (i < m_chips.size()) ? m_chips[i] : 0; }
140 
147  bool setFastForward(int ff);
148 
155  void setVolume(int_least32_t left, int_least32_t right);
156 
162  void setStereo(bool stereo);
163 
167  bool notFinished() const { return m_sampleIndex != m_sampleCount; }
168 
172  uint_least32_t samplesGenerated() const { return m_sampleIndex; }
173 };
174 
175 #endif // MIXER_H
static const unsigned int MAX_SIDS
Maximum number of supported SIDs.
Definition: mixer.h:41
void clockChips()
Definition: mixer.cpp:59
static const int_least32_t VOLUME_MAX
Maximum allowed volume, must be a power of 2.
Definition: mixer.h:48
Definition: mixer.h:37
void setStereo(bool stereo)
Definition: mixer.cpp:159
Definition: sidemu.h:39
bool notFinished() const
Definition: mixer.h:167
bool setFastForward(int ff)
Definition: mixer.cpp:171
void setVolume(int_least32_t left, int_least32_t right)
Definition: mixer.cpp:180
uint_least32_t samplesGenerated() const
Definition: mixer.h:172
void doMix()
Definition: mixer.cpp:69
Mixer()
Definition: mixer.h:89
void addSid(sidemu *chip)
Definition: mixer.cpp:145
sidemu * getSid(unsigned int i) const
Definition: mixer.h:139
void clearSids()
Definition: mixer.cpp:139
void begin(short *buffer, uint_least32_t count)
Definition: mixer.cpp:125
void resetBufs()
Definition: mixer.cpp:64