libsidplayfp  1.6.2
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 public:
41  static const unsigned int MAX_SIDS = 2;
42 
43 private:
44  typedef short (Mixer::*mixer_func_t)() const;
45 
46 public:
50  static const int_least32_t VOLUME_MAX = 1024;
51 
52 private:
53  std::vector<sidemu*> m_chips;
54  std::vector<short*> m_buffers;
55 
56  std::vector<int_least32_t> m_iSamples;
57  std::vector<int_least32_t> m_volume;
58 
59  std::vector<mixer_func_t> m_mix;
60 
61  int oldRandomValue;
62  int m_fastForwardFactor;
63 
64  // Mixer settings
65  short *m_sampleBuffer;
66  uint_least32_t m_sampleCount;
67  uint_least32_t m_sampleIndex;
68 
69  bool m_stereo;
70 
71 private:
72  void updateParams();
73 
74  int triangularDithering()
75  {
76  const int prevValue = oldRandomValue;
77  oldRandomValue = rand() & (VOLUME_MAX-1);
78  return oldRandomValue - prevValue;
79  }
80 
81  short channel1MonoMix() const { return static_cast<short>((m_iSamples[0] + m_iSamples[1]) / 2); }
82  short channel1StereoMix() const { return static_cast<short>(m_iSamples[0]); }
83 
84  short channel2FromMonoMix() const { return static_cast<short>(m_iSamples[0]); }
85  short channel2FromStereoMix() const { return static_cast<short>(m_iSamples[1]); }
86 
87 public:
93  Mixer() :
94  oldRandomValue(0),
95  m_fastForwardFactor(1),
96  m_sampleCount(0),
97  m_stereo(false)
98  {
99  m_mix.push_back(&Mixer::channel1MonoMix);
100  }
101 
105  void doMix();
106 
110  void clockChips();
111 
115  void resetBufs();
116 
123  void begin(short *buffer, uint_least32_t count);
124 
128  void clearSids();
129 
135  void addSid(sidemu *chip);
136 
143  sidemu* getSid(unsigned int i) const { return (i < m_chips.size()) ? m_chips[i] : 0; }
144 
151  bool setFastForward(int ff);
152 
159  void setVolume(int_least32_t left, int_least32_t right);
160 
166  void setStereo(bool stereo);
167 
171  bool notFinished() const { return m_sampleIndex != m_sampleCount; }
172 
176  uint_least32_t samplesGenerated() const { return m_sampleIndex; }
177 };
178 
179 #endif // MIXER_H
static const unsigned int MAX_SIDS
Maximum number of supported SIDs (mono and stereo)
Definition: mixer.h:41
void clockChips()
Definition: mixer.cpp:59
static const int_least32_t VOLUME_MAX
Definition: mixer.h:50
Definition: mixer.h:37
void setStereo(bool stereo)
Definition: mixer.cpp:160
Definition: sidemu.h:39
bool notFinished() const
Definition: mixer.h:171
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:176
void doMix()
Definition: mixer.cpp:69
Mixer()
Definition: mixer.h:93
void addSid(sidemu *chip)
Definition: mixer.cpp:146
sidemu * getSid(unsigned int i) const
Definition: mixer.h:143
void clearSids()
Definition: mixer.cpp:140
void begin(short *buffer, uint_least32_t count)
Definition: mixer.cpp:126
void resetBufs()
Definition: mixer.cpp:64