1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2010 Christian Schoenebeck * |
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., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#include "MidiInputDevice.h" |
25 |
|
26 |
#include "../../common/global_private.h" |
27 |
#include "../../Sampler.h" |
28 |
|
29 |
namespace LinuxSampler { |
30 |
|
31 |
// *************** ParameterActive *************** |
32 |
// * |
33 |
|
34 |
MidiInputDevice::ParameterActive::ParameterActive() : DeviceCreationParameterBool() { |
35 |
InitWithDefault(); |
36 |
} |
37 |
|
38 |
MidiInputDevice::ParameterActive::ParameterActive(String active) : DeviceCreationParameterBool(active) { |
39 |
} |
40 |
|
41 |
String MidiInputDevice::ParameterActive::Description() { |
42 |
return "Enable / disable device"; |
43 |
} |
44 |
|
45 |
bool MidiInputDevice::ParameterActive::Fix() { |
46 |
return false; |
47 |
} |
48 |
|
49 |
bool MidiInputDevice::ParameterActive::Mandatory() { |
50 |
return false; |
51 |
} |
52 |
|
53 |
std::map<String,DeviceCreationParameter*> MidiInputDevice::ParameterActive::DependsAsParameters() { |
54 |
return std::map<String,DeviceCreationParameter*>(); |
55 |
} |
56 |
|
57 |
optional<bool> MidiInputDevice::ParameterActive::DefaultAsBool(std::map<String,String> Parameters) { |
58 |
return true; |
59 |
} |
60 |
|
61 |
void MidiInputDevice::ParameterActive::OnSetValue(bool b) throw (Exception) { |
62 |
if (b) ((MidiInputDevice*)pDevice)->Listen(); |
63 |
else ((MidiInputDevice*)pDevice)->StopListen(); |
64 |
} |
65 |
|
66 |
String MidiInputDevice::ParameterActive::Name() { |
67 |
return "ACTIVE"; |
68 |
} |
69 |
|
70 |
|
71 |
|
72 |
// *************** ParameterPorts *************** |
73 |
// * |
74 |
|
75 |
MidiInputDevice::ParameterPorts::ParameterPorts() : DeviceCreationParameterInt() { |
76 |
InitWithDefault(); |
77 |
} |
78 |
|
79 |
MidiInputDevice::ParameterPorts::ParameterPorts(String val) : DeviceCreationParameterInt(val) { |
80 |
} |
81 |
|
82 |
String MidiInputDevice::ParameterPorts::Description() { |
83 |
return "Number of ports"; |
84 |
} |
85 |
|
86 |
bool MidiInputDevice::ParameterPorts::Fix() { |
87 |
return false; |
88 |
} |
89 |
|
90 |
bool MidiInputDevice::ParameterPorts::Mandatory() { |
91 |
return false; |
92 |
} |
93 |
|
94 |
std::map<String,DeviceCreationParameter*> MidiInputDevice::ParameterPorts::DependsAsParameters() { |
95 |
return std::map<String,DeviceCreationParameter*>(); |
96 |
} |
97 |
|
98 |
optional<int> MidiInputDevice::ParameterPorts::DefaultAsInt(std::map<String,String> Parameters) { |
99 |
return 1; |
100 |
} |
101 |
|
102 |
optional<int> MidiInputDevice::ParameterPorts::RangeMinAsInt(std::map<String,String> Parameters) { |
103 |
return 1; |
104 |
} |
105 |
|
106 |
optional<int> MidiInputDevice::ParameterPorts::RangeMaxAsInt(std::map<String,String> Parameters) { |
107 |
return optional<int>::nothing; |
108 |
} |
109 |
|
110 |
std::vector<int> MidiInputDevice::ParameterPorts::PossibilitiesAsInt(std::map<String,String> Parameters) { |
111 |
return std::vector<int>(); |
112 |
} |
113 |
|
114 |
void MidiInputDevice::ParameterPorts::OnSetValue(int i) throw (Exception) { |
115 |
MidiInputDevice* dev = static_cast<MidiInputDevice*> (pDevice); |
116 |
Sampler* s = static_cast<Sampler*> (dev->pSampler); |
117 |
std::map<uint, SamplerChannel*> channels = s->GetSamplerChannels(); |
118 |
std::map<uint, SamplerChannel*>::iterator iter = channels.begin(); |
119 |
for (; iter != channels.end(); iter++) { |
120 |
SamplerChannel* chn = iter->second; |
121 |
if (chn->GetMidiInputDevice() == NULL || chn->GetMidiInputDevice() != pDevice) { |
122 |
continue; |
123 |
} |
124 |
|
125 |
int port = chn->GetMidiInputPort(); |
126 |
if (port >= i) { |
127 |
String err = "Sampler channel " + ToString(iter->first); |
128 |
err += " is still connected to MIDI port " + ToString(port); |
129 |
throw Exception(err); |
130 |
} |
131 |
} |
132 |
|
133 |
((MidiInputDevice*)pDevice)->AcquirePorts(i); |
134 |
} |
135 |
|
136 |
String MidiInputDevice::ParameterPorts::Name() { |
137 |
return "PORTS"; |
138 |
} |
139 |
|
140 |
|
141 |
|
142 |
// *************** MidiInputDevice *************** |
143 |
// * |
144 |
|
145 |
MidiInputDevice::MidiInputDevice(std::map<String,DeviceCreationParameter*> DriverParameters, void* pSampler) { |
146 |
this->Parameters = DriverParameters; |
147 |
this->pSampler = pSampler; |
148 |
} |
149 |
|
150 |
MidiInputDevice::~MidiInputDevice() { |
151 |
std::map<String,DeviceCreationParameter*>::iterator iter = Parameters.begin(); |
152 |
while (iter != Parameters.end()) { |
153 |
delete iter->second; |
154 |
iter++; |
155 |
} |
156 |
Parameters.clear(); |
157 |
} |
158 |
|
159 |
MidiInputPort* MidiInputDevice::GetPort(uint iPort) throw (MidiInputException) { |
160 |
if (iPort >= Ports.size()) throw MidiInputException("There is no port " + ToString(iPort)); |
161 |
return Ports[iPort]; |
162 |
} |
163 |
|
164 |
uint MidiInputDevice::PortCount() { |
165 |
return Ports.size(); |
166 |
} |
167 |
|
168 |
std::map<String,DeviceCreationParameter*> MidiInputDevice::DeviceParameters() { |
169 |
return Parameters; |
170 |
} |
171 |
|
172 |
void MidiInputDevice::AddMidiPortCountListener(MidiPortCountListener* l) { |
173 |
portCountListeners.AddListener(l); |
174 |
} |
175 |
|
176 |
void MidiInputDevice::RemoveMidiPortCountListener(MidiPortCountListener* l) { |
177 |
portCountListeners.RemoveListener(l); |
178 |
} |
179 |
|
180 |
void MidiInputDevice::fireMidiPortCountChanged(int NewCount) { |
181 |
for (int i = 0; i < portCountListeners.GetListenerCount(); i++) { |
182 |
portCountListeners.GetListener(i)->MidiPortCountChanged(NewCount); |
183 |
} |
184 |
} |
185 |
|
186 |
void MidiInputDevice::fireMidiPortToBeRemoved(MidiInputPort* pPort) { |
187 |
for (int i = 0; i < portCountListeners.GetListenerCount(); i++) { |
188 |
portCountListeners.GetListener(i)->MidiPortToBeRemoved(pPort); |
189 |
} |
190 |
} |
191 |
|
192 |
void MidiInputDevice::fireMidiPortAdded(MidiInputPort* pPort) { |
193 |
for (int i = 0; i < portCountListeners.GetListenerCount(); i++) { |
194 |
portCountListeners.GetListener(i)->MidiPortAdded(pPort); |
195 |
} |
196 |
} |
197 |
|
198 |
void MidiInputDevice::AcquirePorts(uint newPorts) { |
199 |
//FIXME: hooo, this looks scary, no synchronization AT ALL yet! |
200 |
int diff = this->Ports.size() - newPorts; |
201 |
if (!diff) |
202 |
return; // number of ports matches already, nothing to do |
203 |
|
204 |
while (diff != 0) { |
205 |
if (diff > 0) { // we've got too many ports, remove one |
206 |
std::map<int,MidiInputPort*>::iterator portsIter = Ports.end(); |
207 |
--portsIter; |
208 |
|
209 |
fireMidiPortToBeRemoved(portsIter->second); |
210 |
delete portsIter->second; |
211 |
Ports.erase(portsIter); |
212 |
diff--; |
213 |
} |
214 |
if (diff < 0) { // we don't have enough ports, create one |
215 |
MidiInputPort* midiPort = this->CreateMidiPort(); |
216 |
Ports[midiPort->portNumber] = midiPort; |
217 |
diff++; |
218 |
fireMidiPortAdded(midiPort); |
219 |
} |
220 |
} |
221 |
fireMidiPortCountChanged(Ports.size()); |
222 |
} |
223 |
|
224 |
} // namespace LinuxSampler |