1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2008 - 2013 Christian Schoenebeck * |
4 |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This program is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this program; if not, write to the Free Software * |
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
18 |
* MA 02111-1307 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#include "EffectChain.h" |
22 |
|
23 |
#include "../common/global_private.h" |
24 |
|
25 |
namespace LinuxSampler { |
26 |
|
27 |
EffectChain::EffectChain(AudioOutputDevice* pDevice, int iEffectChainId) { |
28 |
this->pDevice = pDevice; |
29 |
iID = iEffectChainId; |
30 |
} |
31 |
|
32 |
void EffectChain::AppendEffect(Effect* pEffect) { |
33 |
pEffect->InitEffect(pDevice); |
34 |
_ChainEntry entry = { pEffect, true }; |
35 |
vEntries.push_back(entry); |
36 |
pEffect->SetParent(this); |
37 |
} |
38 |
|
39 |
void EffectChain::InsertEffect(Effect* pEffect, int iChainPos) throw (Exception) { |
40 |
if (iChainPos < 0 || iChainPos >= vEntries.size()) |
41 |
throw Exception( |
42 |
"Cannot insert effect at chain position " + |
43 |
ToString(iChainPos) + ", index out of bounds." |
44 |
); |
45 |
pEffect->InitEffect(pDevice); // might throw Exception ! |
46 |
std::vector<_ChainEntry>::iterator iter = vEntries.begin(); |
47 |
for (int i = 0; i < iChainPos; ++i) ++iter; |
48 |
_ChainEntry entry = { pEffect, true }; |
49 |
vEntries.insert(iter, entry); |
50 |
pEffect->SetParent(this); |
51 |
} |
52 |
|
53 |
void EffectChain::RemoveEffect(int iChainPos) throw (Exception) { |
54 |
if (iChainPos < 0 || iChainPos >= vEntries.size()) |
55 |
throw Exception( |
56 |
"Cannot remove effect at chain position " + |
57 |
ToString(iChainPos) + ", index out of bounds." |
58 |
); |
59 |
std::vector<_ChainEntry>::iterator iter = vEntries.begin(); |
60 |
for (int i = 0; i < iChainPos; ++i) ++iter; |
61 |
Effect* pEffect = (*iter).pEffect; |
62 |
vEntries.erase(iter); |
63 |
pEffect->SetParent(NULL); // mark effect as not in use anymore |
64 |
} |
65 |
|
66 |
void EffectChain::RenderAudio(uint Samples) { |
67 |
for (int i = 0; i < vEntries.size(); ++i) { |
68 |
Effect* pCurrentEffect = vEntries[i].pEffect; |
69 |
if (i) { // import signal from previous effect |
70 |
Effect* pPrevEffect = vEntries[i - 1].pEffect; |
71 |
for (int iChan = 0; iChan < pPrevEffect->OutputChannelCount() && iChan < pCurrentEffect->InputChannelCount(); ++iChan) { |
72 |
pPrevEffect->OutputChannel(iChan)->MixTo( |
73 |
pCurrentEffect->InputChannel(iChan), |
74 |
Samples |
75 |
); |
76 |
} |
77 |
} |
78 |
if (IsEffectActive(i)) pCurrentEffect->RenderAudio(Samples); |
79 |
else { //TODO: lazy, suboptimal implementation of inactive, bypassed effects |
80 |
for (int iChan = 0; iChan < pCurrentEffect->OutputChannelCount() && iChan < pCurrentEffect->InputChannelCount(); ++iChan) { |
81 |
pCurrentEffect->InputChannel(iChan)->MixTo( |
82 |
pCurrentEffect->OutputChannel(iChan), |
83 |
Samples |
84 |
); |
85 |
} |
86 |
} |
87 |
} |
88 |
} |
89 |
|
90 |
Effect* EffectChain::GetEffect(int iChainPos) const { |
91 |
if (iChainPos < 0 || iChainPos >= vEntries.size()) return NULL; |
92 |
return vEntries[iChainPos].pEffect; |
93 |
} |
94 |
|
95 |
int EffectChain::EffectCount() const { |
96 |
return vEntries.size(); |
97 |
} |
98 |
|
99 |
void EffectChain::Reconnect(AudioOutputDevice* pDevice) { |
100 |
for (int i = 0; i < vEntries.size(); ++i) { |
101 |
Effect* pEffect = vEntries[i].pEffect; |
102 |
pEffect->InitEffect(pDevice); |
103 |
} |
104 |
} |
105 |
|
106 |
void EffectChain::SetEffectActive(int iChainPos, bool bOn) throw (Exception) { |
107 |
if (iChainPos < 0 || iChainPos >= vEntries.size()) |
108 |
throw Exception( |
109 |
"Cannot change active state of effect at chain position " + |
110 |
ToString(iChainPos) + ", index out of bounds." |
111 |
); |
112 |
vEntries[iChainPos].bActive = bOn; |
113 |
} |
114 |
|
115 |
bool EffectChain::IsEffectActive(int iChainPos) const { |
116 |
if (iChainPos < 0 || iChainPos >= vEntries.size()) return false; |
117 |
return vEntries[iChainPos].bActive; |
118 |
} |
119 |
|
120 |
void EffectChain::ClearAllChannels() { |
121 |
for (int iEffect = 0; iEffect < vEntries.size(); ++iEffect) { |
122 |
Effect* pEffect = vEntries[iEffect].pEffect; |
123 |
for (int i = 0; i < pEffect->InputChannelCount(); ++i) |
124 |
pEffect->InputChannel(i)->Clear(); // zero out buffers |
125 |
for (int i = 0; i < pEffect->OutputChannelCount(); ++i) |
126 |
pEffect->OutputChannel(i)->Clear(); // zero out buffers |
127 |
} |
128 |
} |
129 |
|
130 |
int EffectChain::ID() const { |
131 |
return iID; |
132 |
} |
133 |
|
134 |
} // namespace LinuxSampler |