libsidplayfp  1.3.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 #include "event.h"
33 
34 class sidemu;
35 
39 class Mixer : private Event
40 {
41 private:
42  typedef short (Mixer::*mixer_func_t)() const;
43 
44 public:
46  static const int_least32_t VOLUME_MAX = 1024;
47 
48 private:
52  EventContext &event_context;
53 
54  std::vector<sidemu*> m_chips;
55  std::vector<short*> m_buffers;
56 
57  std::vector<int_least32_t> m_iSamples;
58  std::vector<int_least32_t> m_volume;
59 
60  std::vector<mixer_func_t> m_mix;
61 
62  int oldRandomValue;
63  int m_fastForwardFactor;
64 
65  // Mixer settings
66  short *m_sampleBuffer;
67  uint_least32_t m_sampleCount;
68  uint_least32_t m_sampleIndex;
69 
70  bool m_stereo;
71 
72 private:
73  void updateParams();
74 
75  int triangularDithering()
76  {
77  const int prevValue = oldRandomValue;
78  oldRandomValue = rand() & (VOLUME_MAX-1);
79  return oldRandomValue - prevValue;
80  }
81 
82  short channel1MonoMix() const { return static_cast<short>((m_iSamples[0] + m_iSamples[1]) / 2); }
83  short channel1StereoMix() const { return static_cast<short>(m_iSamples[0]); }
84 
85  short channel2FromMonoMix() const { return static_cast<short>(m_iSamples[0]); }
86  short channel2FromStereoMix() const { return static_cast<short>(m_iSamples[1]); }
87 
88 public:
94  Mixer(EventContext *context) :
95  Event("Mixer"),
96  event_context(*context),
97  oldRandomValue(0),
98  m_fastForwardFactor(1),
99  m_sampleCount(0),
100  m_stereo(false)
101  {
102  m_mix.push_back(&Mixer::channel1MonoMix);
103  }
104 
108  void event();
109 
113  void reset();
114 
121  void begin(short *buffer, uint_least32_t count);
122 
126  void clearSids();
127 
133  void addSid(sidemu *chip);
134 
141  sidemu* getSid(unsigned int i) const { return (i < m_chips.size()) ? m_chips[i] : 0; }
142 
149  bool setFastForward(int ff);
150 
157  void setVolume(int_least32_t left, int_least32_t right);
158 
164  void setStereo(bool stereo);
165 
169  bool notFinished() const { return m_sampleIndex != m_sampleCount; }
170 
174  uint_least32_t samplesGenerated() const { return m_sampleIndex; }
175 };
176 
177 #endif // MIXER_H
static const int_least32_t VOLUME_MAX
Definition: mixer.h:46
Definition: mixer.h:39
void setStereo(bool stereo)
Definition: mixer.cpp:174
Definition: sidemu.h:45
bool notFinished() const
Definition: mixer.h:169
Definition: event.h:101
bool setFastForward(int ff)
Definition: mixer.cpp:186
void setVolume(int_least32_t left, int_least32_t right)
Definition: mixer.cpp:195
uint_least32_t samplesGenerated() const
Definition: mixer.h:174
void addSid(sidemu *chip)
Definition: mixer.cpp:160
sidemu * getSid(unsigned int i) const
Definition: mixer.h:141
void clearSids()
Definition: mixer.cpp:154
void reset()
Definition: mixer.cpp:135
Mixer(EventContext *context)
Definition: mixer.h:94
void begin(short *buffer, uint_least32_t count)
Definition: mixer.cpp:140
Definition: event.h:50
void event()
Definition: mixer.cpp:64