libsidplayfp  1.4.0
mixer.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2013 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 private:
40  typedef short (Mixer::*mixer_func_t)() const;
41 
42 public:
44  static const int_least32_t VOLUME_MAX = 1024;
45 
46 private:
47  std::vector<sidemu*> m_chips;
48  std::vector<short*> m_buffers;
49 
50  std::vector<int_least32_t> m_iSamples;
51  std::vector<int_least32_t> m_volume;
52 
53  std::vector<mixer_func_t> m_mix;
54 
55  int oldRandomValue;
56  int m_fastForwardFactor;
57 
58  // Mixer settings
59  short *m_sampleBuffer;
60  uint_least32_t m_sampleCount;
61  uint_least32_t m_sampleIndex;
62 
63  bool m_stereo;
64 
65 private:
66  void updateParams();
67 
68  int triangularDithering()
69  {
70  const int prevValue = oldRandomValue;
71  oldRandomValue = rand() & (VOLUME_MAX-1);
72  return oldRandomValue - prevValue;
73  }
74 
75  short channel1MonoMix() const { return static_cast<short>((m_iSamples[0] + m_iSamples[1]) / 2); }
76  short channel1StereoMix() const { return static_cast<short>(m_iSamples[0]); }
77 
78  short channel2FromMonoMix() const { return static_cast<short>(m_iSamples[0]); }
79  short channel2FromStereoMix() const { return static_cast<short>(m_iSamples[1]); }
80 
81 public:
87  Mixer() :
88  oldRandomValue(0),
89  m_fastForwardFactor(1),
90  m_sampleCount(0),
91  m_stereo(false)
92  {
93  m_mix.push_back(&Mixer::channel1MonoMix);
94  }
95 
99  void doMix();
100 
104  void clockChips();
105 
109  void resetBufs();
110 
117  void begin(short *buffer, uint_least32_t count);
118 
122  void clearSids();
123 
129  void addSid(sidemu *chip);
130 
137  sidemu* getSid(unsigned int i) const { return (i < m_chips.size()) ? m_chips[i] : 0; }
138 
145  bool setFastForward(int ff);
146 
153  void setVolume(int_least32_t left, int_least32_t right);
154 
160  void setStereo(bool stereo);
161 
165  bool notFinished() const { return m_sampleIndex != m_sampleCount; }
166 
170  uint_least32_t samplesGenerated() const { return m_sampleIndex; }
171 };
172 
173 #endif // MIXER_H
void clockChips()
Definition: mixer.cpp:59
static const int_least32_t VOLUME_MAX
Definition: mixer.h:44
Definition: mixer.h:37
void setStereo(bool stereo)
Definition: mixer.cpp:160
Definition: sidemu.h:45
bool notFinished() const
Definition: mixer.h:165
bool setFastForward(int ff)
Definition: mixer.cpp:172
void setVolume(int_least32_t left, int_least32_t right)
Definition: mixer.cpp:181
uint_least32_t samplesGenerated() const
Definition: mixer.h:170
void doMix()
Definition: mixer.cpp:69
Mixer()
Definition: mixer.h:87
void addSid(sidemu *chip)
Definition: mixer.cpp:146
sidemu * getSid(unsigned int i) const
Definition: mixer.h:137
void clearSids()
Definition: mixer.cpp:140
void begin(short *buffer, uint_least32_t count)
Definition: mixer.cpp:126
void resetBufs()
Definition: mixer.cpp:64