1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2011 Grigor Iliev * |
6 |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
8 |
* it under the terms of the GNU General Public License as published by * |
9 |
* the Free Software Foundation; either version 2 of the License, or * |
10 |
* (at your option) any later version. * |
11 |
* * |
12 |
* This program is distributed in the hope that it will be useful, * |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 |
* GNU General Public License for more details. * |
16 |
* * |
17 |
* You should have received a copy of the GNU General Public License * |
18 |
* along with this program; if not, write to the Free Software * |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
20 |
* MA 02111-1307 USA * |
21 |
***************************************************************************/ |
22 |
|
23 |
#ifndef __LS_SIGNALUNITRACK_H__ |
24 |
#define __LS_SIGNALUNITRACK_H__ |
25 |
|
26 |
#include "Event.h" |
27 |
#include "SignalUnit.h" |
28 |
#include "../../common/Pool.h" |
29 |
|
30 |
|
31 |
namespace LinuxSampler { |
32 |
|
33 |
class EqSupport { |
34 |
private: |
35 |
int BandCount; |
36 |
int* GainIdxs; ///< the indices of the gain controls |
37 |
int* FreqIdxs; ///< the indices of the frequency controls |
38 |
int* BandwidthIdxs; ///< the indices of the bandwidth controls |
39 |
Effect* pEffect; |
40 |
Effect* pEffect2; // If the effect is mono we'll need two effects |
41 |
|
42 |
inline float check(optional<float> minimum, optional<float> maximum, float value) { |
43 |
if (minimum) { |
44 |
float min = *minimum; |
45 |
if (value < min) value = min; |
46 |
} |
47 |
if (maximum) { |
48 |
float max = *maximum; |
49 |
if (value > max) value = max; |
50 |
} |
51 |
|
52 |
return value; |
53 |
} |
54 |
|
55 |
public: |
56 |
EqSupport(); |
57 |
~EqSupport(); |
58 |
|
59 |
/** |
60 |
* Searches for know EQ effects and create one if the search succeed. |
61 |
* If the initialization is successful and EQ effect is selected, |
62 |
* HasSupport() returns true; |
63 |
*/ |
64 |
void Install(); |
65 |
|
66 |
void Uninstall(); |
67 |
|
68 |
/** Returns true if an EQ is created and is ready for use. */ |
69 |
bool HasSupport() { return pEffect != NULL; } |
70 |
|
71 |
/** Reset the gains of all bands to 0dB. */ |
72 |
void Reset() { |
73 |
if (!HasSupport()) return; |
74 |
for (int i = 0; i < BandCount; i++) { |
75 |
pEffect->InputControl(GainIdxs[i])->SetValue(0); // 0dB |
76 |
if (pEffect2 != NULL) pEffect2->InputControl(GainIdxs[i])->SetValue(0); // 0dB |
77 |
} |
78 |
} |
79 |
|
80 |
void InitEffect(AudioOutputDevice* pDevice) { |
81 |
if (pEffect != NULL) pEffect->InitEffect(pDevice); |
82 |
if (pEffect2 != NULL) pEffect2->InitEffect(pDevice); |
83 |
} |
84 |
|
85 |
int GetBandCount() { return BandCount; } |
86 |
|
87 |
void SetGain(int band, float gain); |
88 |
void SetFreq(int band, float freq); |
89 |
void SetBandwidth(int band, float octaves); |
90 |
|
91 |
AudioChannel* GetInChannelLeft() { |
92 |
return pEffect->InputChannel(0); |
93 |
} |
94 |
|
95 |
AudioChannel* GetInChannelRight() { |
96 |
return pEffect2 != NULL ? pEffect2->InputChannel(0) : pEffect->InputChannel(1); |
97 |
} |
98 |
|
99 |
AudioChannel* GetOutChannelLeft() { |
100 |
return pEffect->OutputChannel(0); |
101 |
} |
102 |
|
103 |
AudioChannel* GetOutChannelRight() { |
104 |
return pEffect2 != NULL ? pEffect2->OutputChannel(0) : pEffect->OutputChannel(1); |
105 |
} |
106 |
|
107 |
void RenderAudio(uint Samples) { |
108 |
pEffect->RenderAudio(Samples); |
109 |
if (pEffect2 != NULL) pEffect2->RenderAudio(Samples); |
110 |
} |
111 |
}; |
112 |
|
113 |
|
114 |
|
115 |
class SignalUnitRack { |
116 |
protected: |
117 |
uint CurrentStep; // The current time step |
118 |
bool bHasEq, releaseStageEntered; |
119 |
|
120 |
public: |
121 |
FixedArray<SignalUnit*> Units; // A list of all signal units in this rack |
122 |
|
123 |
/** |
124 |
* @param maxUnitCount We are using fixed size array because of the real-time safe requirements. |
125 |
*/ |
126 |
SignalUnitRack(int maxUnitCount): CurrentStep(0), bHasEq(false), |
127 |
releaseStageEntered(false), Units(maxUnitCount) { } |
128 |
|
129 |
uint GetCurrentStep() { return CurrentStep; } |
130 |
|
131 |
virtual EndpointSignalUnit* GetEndpointUnit() = 0; |
132 |
|
133 |
virtual void EnterFadeOutStage() = 0; |
134 |
|
135 |
/** |
136 |
* Will be called to increment the time with one sample point. |
137 |
* Each unit should recalculate its current level on every call of this function. |
138 |
*/ |
139 |
virtual void Increment() { |
140 |
CurrentStep++; |
141 |
|
142 |
for (int i = 0; i < Units.size(); i++) { |
143 |
Units[i]->Increment(); |
144 |
} |
145 |
} |
146 |
|
147 |
virtual void ProcessCCEvent(RTList<Event>::Iterator& itEvent) { |
148 |
if ( !(itEvent->Type == Event::type_control_change && itEvent->Param.CC.Controller) ) return; |
149 |
for (int i = 0; i < Units.size(); i++) { |
150 |
Units[i]->ProcessCCEvent(itEvent->Param.CC.Controller, itEvent->Param.CC.Value); |
151 |
} |
152 |
} |
153 |
|
154 |
/** Initializes and triggers the rack. */ |
155 |
virtual void Trigger() { |
156 |
releaseStageEntered = false; |
157 |
CurrentStep = 0; |
158 |
for (int i = 0; i < Units.size(); i++) { |
159 |
Units[i]->Trigger(); |
160 |
} |
161 |
} |
162 |
|
163 |
/** |
164 |
* When the rack belongs to a voice, this method is |
165 |
* called when the voice enter the release stage. |
166 |
*/ |
167 |
virtual void EnterReleaseStage() { |
168 |
releaseStageEntered = true; |
169 |
for (int i = 0; i < Units.size(); i++) { |
170 |
Units[i]->EnterReleaseStage(); |
171 |
} |
172 |
} |
173 |
|
174 |
bool isReleaseStageEntered() { return releaseStageEntered; } |
175 |
|
176 |
/** |
177 |
* When the rack belongs to a voice, this method is |
178 |
* called when the voice is of type which ignore note off. |
179 |
*/ |
180 |
virtual void CancelRelease() { |
181 |
for (int i = 0; i < Units.size(); i++) { |
182 |
Units[i]->CancelRelease(); |
183 |
} |
184 |
} |
185 |
|
186 |
/** |
187 |
* Determines whether an equalization is applied to the voice. |
188 |
* Used for optimization. |
189 |
*/ |
190 |
bool HasEq() { return bHasEq; } |
191 |
|
192 |
virtual void UpdateEqSettings(EqSupport* pEqSupport) = 0; |
193 |
}; |
194 |
} // namespace LinuxSampler |
195 |
|
196 |
#endif /* __LS_SIGNALUNITRACK_H__ */ |