libsidplayfp 1.8.8
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
32class sidemu;
33
37class Mixer
38{
39public:
41 static const unsigned int MAX_SIDS = 3;
42
43 static const int_least32_t SCALE_FACTOR = 1 << 16;
44 static const int_least32_t C1 = static_cast<int_least32_t>(1.0 / (1.0 + 0.7071067812) * SCALE_FACTOR);
45 static const int_least32_t C2 = static_cast<int_least32_t>(0.7071067812 / (1.0 + 0.7071067812) * SCALE_FACTOR);
46
47private:
48 typedef int_least32_t (Mixer::*mixer_func_t)() const;
49
50public:
52 static const int_least32_t VOLUME_MAX = 1024;
53
54private:
55 std::vector<sidemu*> m_chips;
56 std::vector<short*> m_buffers;
57
58 std::vector<int_least32_t> m_iSamples;
59 std::vector<int_least32_t> m_volume;
60
61 std::vector<mixer_func_t> m_mix;
62
63 int oldRandomValue;
64 int m_fastForwardFactor;
65
66 // Mixer settings
67 short *m_sampleBuffer;
68 uint_least32_t m_sampleCount;
69 uint_least32_t m_sampleIndex;
70
71 bool m_stereo;
72
73private:
74 void updateParams();
75
76 int triangularDithering()
77 {
78 const int prevValue = oldRandomValue;
79 oldRandomValue = rand() & (VOLUME_MAX-1);
80 return oldRandomValue - prevValue;
81 }
82
83 /*
84 * Channel matrix
85 *
86 * C1
87 * L 1.0
88 * R 1.0
89 *
90 * C1 C2
91 * L 1.0 0.0
92 * R 0.0 1.0
93 *
94 * C1 C2 C3
95 * L 1/1.707 0.707/1.707 0.0
96 * R 0.0 0.707/1.707 1/1.707
97 */
98
99 // Mono mixing
100 int_least32_t mono_OneChip() const { return m_iSamples[0]; }
101 int_least32_t mono_TwoChips() const { return (m_iSamples[0] + m_iSamples[1]) / 2; }
102 int_least32_t mono_ThreeChips() const { return (m_iSamples[0] + m_iSamples[1] + m_iSamples[2]) / 3; }
103
104 // Stereo mixing
105 int_least32_t stereo_OneChip() const { return m_iSamples[0]; }
106
107 int_least32_t stereo_ch1_TwoChips() const { return m_iSamples[0]; }
108 int_least32_t stereo_ch2_TwoChips() const { return m_iSamples[1]; }
109
110 int_least32_t stereo_ch1_ThreeChips() const { return (C1*m_iSamples[0] + C2*m_iSamples[1]) / SCALE_FACTOR; }
111 int_least32_t stereo_ch2_ThreeChips() const { return (C2*m_iSamples[1] + C1*m_iSamples[2]) / SCALE_FACTOR; }
112
113
114public:
119 oldRandomValue(0),
120 m_fastForwardFactor(1),
121 m_sampleCount(0),
122 m_stereo(false)
123 {
124 m_mix.push_back(&Mixer::mono_OneChip);
125 }
126
130 void doMix();
131
135 void clockChips();
136
140 void resetBufs();
141
148 void begin(short *buffer, uint_least32_t count);
149
153 void clearSids();
154
160 void addSid(sidemu *chip);
161
168 sidemu* getSid(unsigned int i) const { return (i < m_chips.size()) ? m_chips[i] : 0; }
169
176 bool setFastForward(int ff);
177
184 void setVolume(int_least32_t left, int_least32_t right);
185
191 void setStereo(bool stereo);
192
196 bool notFinished() const { return m_sampleIndex != m_sampleCount; }
197
201 uint_least32_t samplesGenerated() const { return m_sampleIndex; }
202};
203
204#endif // MIXER_H
Definition: mixer.h:38
void doMix()
Definition: mixer.cpp:69
void clearSids()
Definition: mixer.cpp:150
void setStereo(bool stereo)
Definition: mixer.cpp:170
void setVolume(int_least32_t left, int_least32_t right)
Definition: mixer.cpp:191
bool setFastForward(int ff)
Definition: mixer.cpp:182
uint_least32_t samplesGenerated() const
Definition: mixer.h:201
void addSid(sidemu *chip)
Definition: mixer.cpp:156
void resetBufs()
Definition: mixer.cpp:64
void begin(short *buffer, uint_least32_t count)
Definition: mixer.cpp:125
sidemu * getSid(unsigned int i) const
Definition: mixer.h:168
void clockChips()
Definition: mixer.cpp:59
bool notFinished() const
Definition: mixer.h:196
static const int_least32_t VOLUME_MAX
Maximum allowed volume, must be a power of 2.
Definition: mixer.h:52
static const unsigned int MAX_SIDS
Maximum number of supported SIDs.
Definition: mixer.h:41
Mixer()
Definition: mixer.h:118
Definition: sidemu.h:40