1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
7 |
* * |
8 |
* This library 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 library 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 library; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#include "FxSend.h" |
25 |
|
26 |
#include "../drivers/audio/AudioOutputDevice.h" |
27 |
#include "../common/RTMath.h" |
28 |
|
29 |
#include <map> |
30 |
|
31 |
#define DEFAULT_FX_SEND_LEVEL 0.0f |
32 |
|
33 |
namespace LinuxSampler { |
34 |
|
35 |
FxSend::FxSend(EngineChannel* pEngineChannel, uint8_t MidiCtrl, String Name) throw (Exception) { |
36 |
this->pEngineChannel = pEngineChannel; |
37 |
AudioOutputDevice* pDevice = pEngineChannel->GetAudioOutputDevice(); |
38 |
const int iChanOffset = (pDevice) ? pDevice->ChannelCount() - pEngineChannel->Channels() : 0; |
39 |
for (int i = 0; i < pEngineChannel->Channels(); i++) { |
40 |
const int iDestination = iChanOffset + i; |
41 |
Routing.push_back(iDestination); |
42 |
} |
43 |
SetMidiController(MidiCtrl); |
44 |
sName = Name; |
45 |
|
46 |
// create an EngineChannel unique ID for this FxSend instance |
47 |
if (!pEngineChannel->GetFxSendCount()) iId = 0; |
48 |
else { |
49 |
// get the highest existing map ID |
50 |
uint highestIndex = 0; |
51 |
for (uint i = 0; i < pEngineChannel->GetFxSendCount(); i++) |
52 |
highestIndex = RTMath::Max(highestIndex, pEngineChannel->GetFxSend(i)->Id()); |
53 |
// check if we reached the index limit |
54 |
if (highestIndex + 1 < highestIndex) { |
55 |
// search for an unoccupied map ID starting from 0 |
56 |
for (uint i = 0; i < highestIndex; i++) { |
57 |
bool bOccupied = false; |
58 |
for (uint j = 0; j < pEngineChannel->GetFxSendCount(); j++) { |
59 |
if (pEngineChannel->GetFxSend(j)->Id() == i) { |
60 |
bOccupied = true; |
61 |
break; |
62 |
} |
63 |
} |
64 |
if (!bOccupied) { |
65 |
iId = i; |
66 |
goto __done; |
67 |
} |
68 |
} |
69 |
throw Exception("Internal error: could not find unoccupied FxSend ID."); |
70 |
} |
71 |
iId = highestIndex + 1; |
72 |
} |
73 |
__done: |
74 |
|
75 |
fLevel = DEFAULT_FX_SEND_LEVEL; |
76 |
} |
77 |
|
78 |
int FxSend::DestinationChannel(int SrcChan) { |
79 |
if (SrcChan >= pEngineChannel->Channels()) return -1; |
80 |
return Routing[SrcChan]; |
81 |
} |
82 |
|
83 |
void FxSend::SetDestinationChannel(int SrcChan, int DstChan) throw (Exception) { |
84 |
if (SrcChan < 0 || SrcChan >= pEngineChannel->Channels()) |
85 |
throw Exception("Cannot alter FxSend routing, source channel out of bounds"); |
86 |
AudioOutputDevice* pDevice = pEngineChannel->GetAudioOutputDevice(); |
87 |
if (pDevice) { |
88 |
if (DstChan < 0 || DstChan >= pDevice->ChannelCount()) |
89 |
throw Exception("Cannot alter FxSend routing, destination channel out of bounds"); |
90 |
} else { // no audio device assigned yet |
91 |
if (DstChan < 0 || DstChan >= pEngineChannel->Channels()) |
92 |
throw Exception( |
93 |
"there is no audio device yet, so you cannot set a " |
94 |
"FxSend destination channel higher than the engine " |
95 |
"channel's amount of channels" |
96 |
); |
97 |
} |
98 |
Routing[SrcChan] = DstChan; |
99 |
} |
100 |
|
101 |
void FxSend::UpdateChannels() { |
102 |
if (Routing.size() > pEngineChannel->Channels()) { |
103 |
// add routings with default destinations |
104 |
AudioOutputDevice* pDevice = pEngineChannel->GetAudioOutputDevice(); |
105 |
const int iChanOffset = (pDevice) ? pDevice->ChannelCount() - pEngineChannel->Channels() : 0; |
106 |
for (int i = Routing.size(); i < pEngineChannel->Channels(); i++) { |
107 |
const int iDestination = iChanOffset + i; |
108 |
Routing.push_back(iDestination); |
109 |
} |
110 |
} else if (Routing.size() < pEngineChannel->Channels()) { |
111 |
// shrink routing vector |
112 |
Routing.resize(pEngineChannel->Channels()); |
113 |
} |
114 |
} |
115 |
|
116 |
float FxSend::Level() { |
117 |
return fLevel; |
118 |
} |
119 |
|
120 |
void FxSend::SetLevel(float f) { |
121 |
if(fLevel == f) return; |
122 |
fLevel = f; |
123 |
SetInfoChanged(true); |
124 |
} |
125 |
|
126 |
void FxSend::SetLevel(uint8_t iMidiValue) { |
127 |
fLevel = float(iMidiValue & 0x7f) / 127.0f; |
128 |
SetInfoChanged(true); |
129 |
} |
130 |
|
131 |
void FxSend::Reset() { |
132 |
SetLevel(DEFAULT_FX_SEND_LEVEL); |
133 |
} |
134 |
|
135 |
uint8_t FxSend::MidiController() { |
136 |
return MidiFxSendController; |
137 |
} |
138 |
|
139 |
void FxSend::SetMidiController(uint8_t MidiCtrl) throw (Exception) { |
140 |
if (MidiCtrl >> 7) |
141 |
throw Exception("Invalid MIDI controller " + ToString((int)MidiCtrl)); |
142 |
MidiFxSendController = MidiCtrl; |
143 |
} |
144 |
|
145 |
String FxSend::Name() { |
146 |
return sName; |
147 |
} |
148 |
|
149 |
void FxSend::SetName(String Name) { |
150 |
sName = Name; |
151 |
} |
152 |
|
153 |
uint FxSend::Id() { |
154 |
return iId; |
155 |
} |
156 |
|
157 |
void FxSend::SetInfoChanged(bool b) { |
158 |
bInfoChanged = b; |
159 |
} |
160 |
|
161 |
bool FxSend::IsInfoChanged() { |
162 |
return bInfoChanged; |
163 |
} |
164 |
|
165 |
} // namespace LinuxSampler |