/[svn]/linuxsampler/trunk/src/drivers/midi/MidiInputPort.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/drivers/midi/MidiInputPort.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 846 - (show annotations) (download)
Sun Mar 19 16:38:22 2006 UTC (18 years ago) by persson
File size: 12092 byte(s)
* more thread safety fixes: another fix for lscp "load engine" and
  midi thread. Sysex midi protected against lscp. Instrument loader
  thread protected against lscp thread.

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005 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 "MidiInputPort.h"
25
26 #include "../../Sampler.h"
27 #include "../../engines/EngineFactory.h"
28
29 namespace LinuxSampler {
30
31 // *************** ParameterName ***************
32 // *
33
34 MidiInputPort::ParameterName::ParameterName(MidiInputPort* pPort) : DeviceRuntimeParameterString("Port " + ToString(pPort->GetPortNumber())) {
35 this->pPort = pPort;
36 }
37
38 MidiInputPort::ParameterName::ParameterName(MidiInputPort* pPort, String val) : DeviceRuntimeParameterString(val) {
39 this->pPort = pPort;
40 }
41
42 String MidiInputPort::ParameterName::Description() {
43 return "Name for this port";
44 }
45
46 bool MidiInputPort::ParameterName::Fix() {
47 return false;
48 }
49
50 std::vector<String> MidiInputPort::ParameterName::PossibilitiesAsString() {
51 return std::vector<String>();
52 }
53
54 void MidiInputPort::ParameterName::OnSetValue(String s) throw (LinuxSamplerException) {
55 return; /* FIXME: Nothing to do here */
56 }
57
58
59
60 // *************** MidiInputPort ***************
61 // *
62
63 MidiInputPort::~MidiInputPort() {
64 std::map<String,DeviceRuntimeParameter*>::iterator iter = Parameters.begin();
65 while (iter != Parameters.end()) {
66 delete iter->second;
67 iter++;
68 }
69 Parameters.clear();
70 }
71
72 MidiInputPort::MidiInputPort(MidiInputDevice* pDevice, int portNumber)
73 : MidiChannelMapReader(MidiChannelMap),
74 SysexListenersReader(SysexListeners) {
75 this->pDevice = pDevice;
76 this->portNumber = portNumber;
77 Parameters["NAME"] = new ParameterName(this);
78 pPreviousProgramChangeEngineChannel = NULL;
79 }
80
81 MidiInputDevice* MidiInputPort::GetDevice() {
82 return pDevice;
83 }
84
85 uint MidiInputPort::GetPortNumber() {
86 return portNumber;
87 }
88
89 std::map<String,DeviceRuntimeParameter*> MidiInputPort::PortParameters() {
90 return Parameters;
91 }
92
93 void MidiInputPort::DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel) {
94 const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
95 // dispatch event for engines listening to the same MIDI channel
96 {
97 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
98 std::set<EngineChannel*>::iterator end = midiChannelMap[MidiChannel].end();
99 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity);
100 }
101 // dispatch event for engines listening to ALL MIDI channels
102 {
103 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
104 std::set<EngineChannel*>::iterator end = midiChannelMap[midi_chan_all].end();
105 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity);
106 }
107 MidiChannelMapReader.Unlock();
108 }
109
110 void MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel) {
111 const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
112 // dispatch event for engines listening to the same MIDI channel
113 {
114 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
115 std::set<EngineChannel*>::iterator end = midiChannelMap[MidiChannel].end();
116 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity);
117 }
118 // dispatch event for engines listening to ALL MIDI channels
119 {
120 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
121 std::set<EngineChannel*>::iterator end = midiChannelMap[midi_chan_all].end();
122 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity);
123 }
124 MidiChannelMapReader.Unlock();
125 }
126
127 void MidiInputPort::DispatchPitchbend(int Pitch, uint MidiChannel) {
128 const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
129 // dispatch event for engines listening to the same MIDI channel
130 {
131 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
132 std::set<EngineChannel*>::iterator end = midiChannelMap[MidiChannel].end();
133 for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch);
134 }
135 // dispatch event for engines listening to ALL MIDI channels
136 {
137 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
138 std::set<EngineChannel*>::iterator end = midiChannelMap[midi_chan_all].end();
139 for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch);
140 }
141 MidiChannelMapReader.Unlock();
142 }
143
144 void MidiInputPort::DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel) {
145 const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
146 // dispatch event for engines listening to the same MIDI channel
147 {
148 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
149 std::set<EngineChannel*>::iterator end = midiChannelMap[MidiChannel].end();
150 for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value);
151 }
152 // dispatch event for engines listening to ALL MIDI channels
153 {
154 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
155 std::set<EngineChannel*>::iterator end = midiChannelMap[midi_chan_all].end();
156 for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value);
157 }
158 MidiChannelMapReader.Unlock();
159 }
160
161 void MidiInputPort::DispatchSysex(void* pData, uint Size) {
162 const std::set<Engine*> allEngines = SysexListenersReader.Lock();
163 // dispatch event to all engine instances
164 std::set<Engine*>::iterator engineiter = allEngines.begin();
165 std::set<Engine*>::iterator end = allEngines.end();
166 for (; engineiter != end; engineiter++) (*engineiter)->SendSysex(pData, Size);
167 SysexListenersReader.Unlock();
168 }
169
170 void MidiInputPort::DispatchProgramChange(uint8_t Program, uint MidiChannel) {
171 if (!pDevice || !pDevice->pSampler) {
172 std::cerr << "MidiInputPort: ERROR, no sampler instance to handle program change."
173 << "This is a bug, please report it!\n" << std::flush;
174 return;
175 }
176
177 Sampler* pSampler = (Sampler*) pDevice->pSampler;
178 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(Program);
179 if (!pSamplerChannel) return;
180
181 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
182 if (!pEngineChannel) return;
183
184 // disconnect from the engine channel which was connected by the last PC event
185 if (pPreviousProgramChangeEngineChannel)
186 Disconnect(pPreviousProgramChangeEngineChannel);
187
188 // now connect to the new engine channel and remember it
189 try {
190 Connect(pEngineChannel, (midi_chan_t) MidiChannel);
191 pPreviousProgramChangeEngineChannel = pEngineChannel;
192 }
193 catch (...) { /* NOOP */ }
194 }
195
196 void MidiInputPort::Connect(EngineChannel* pEngineChannel, midi_chan_t MidiChannel) {
197 if (MidiChannel < 0 || MidiChannel > 16)
198 throw MidiInputException("MIDI channel index out of bounds");
199
200 // first check if desired connection is already established
201 MidiChannelMapMutex.Lock();
202 MidiChannelMap_t& midiChannelMap = MidiChannelMap.GetConfigForUpdate();
203 bool bAlreadyDone = midiChannelMap[MidiChannel].count(pEngineChannel);
204 MidiChannelMapMutex.Unlock();
205 if (bAlreadyDone) return;
206
207 // remove all other connections of that engine channel (if any)
208 Disconnect(pEngineChannel);
209
210 // register engine channel on the desired MIDI channel
211 MidiChannelMapMutex.Lock();
212 MidiChannelMap.GetConfigForUpdate()[MidiChannel].insert(pEngineChannel);
213 MidiChannelMap.SwitchConfig()[MidiChannel].insert(pEngineChannel);
214 MidiChannelMapMutex.Unlock();
215
216 // inform engine channel about this connection
217 pEngineChannel->Connect(this, MidiChannel);
218
219 // mark engine channel as changed
220 pEngineChannel->StatusChanged(true);
221 }
222
223 void MidiInputPort::Disconnect(EngineChannel* pEngineChannel) {
224 if (!pEngineChannel) return;
225
226 bool bChannelFound = false;
227
228 // unregister engine channel from all MIDI channels
229 MidiChannelMapMutex.Lock();
230 try {
231 {
232 MidiChannelMap_t& midiChannelMap = MidiChannelMap.GetConfigForUpdate();
233 for (int i = 0; i <= 16; i++) {
234 bChannelFound |= midiChannelMap[i].count(pEngineChannel);
235 midiChannelMap[i].erase(pEngineChannel);
236 }
237 }
238 // do the same update again, after switching to the other config
239 {
240 MidiChannelMap_t& midiChannelMap = MidiChannelMap.SwitchConfig();
241 for (int i = 0; i <= 16; i++) {
242 bChannelFound |= midiChannelMap[i].count(pEngineChannel);
243 midiChannelMap[i].erase(pEngineChannel);
244 }
245 }
246 }
247 catch(...) { /* NOOP */ }
248 MidiChannelMapMutex.Unlock();
249
250 // inform engine channel about the disconnection (if there is one)
251 if (bChannelFound) pEngineChannel->DisconnectMidiInputPort();
252
253 // mark engine channel as changed
254 pEngineChannel->StatusChanged(true);
255 }
256
257 SynchronizedConfig<std::set<LinuxSampler::Engine*> > MidiInputPort::SysexListeners;
258
259 void MidiInputPort::AddSysexListener(Engine* engine) {
260 std::pair<std::set<Engine*>::iterator, bool> p = SysexListeners.GetConfigForUpdate().insert(engine);
261 if (p.second) SysexListeners.SwitchConfig().insert(engine);
262 }
263
264 bool MidiInputPort::RemoveSysexListener(Engine* engine) {
265 int count = SysexListeners.GetConfigForUpdate().erase(engine);
266 if (count) SysexListeners.SwitchConfig().erase(engine);
267 return count;
268 }
269
270 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC