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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 880 - (hide annotations) (download)
Tue Jun 27 22:57:37 2006 UTC (17 years, 9 months ago) by schoenebeck
File size: 12080 byte(s)
just some refactoring work:
- renamed class LinuxSamplerException -> Exception
- encapsulated LS API relevant files into LS namespace
- removed unnecessary header inclusions

1 schoenebeck 221 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 880 * Copyright (C) 2005, 2006 Christian Schoenebeck *
7 schoenebeck 221 * *
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 schoenebeck 551 #include "../../Sampler.h"
27 schoenebeck 775 #include "../../engines/EngineFactory.h"
28 schoenebeck 551
29 schoenebeck 221 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 schoenebeck 880 void MidiInputPort::ParameterName::OnSetValue(String s) throw (Exception) {
55 schoenebeck 221 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 persson 836 Parameters.clear();
70 schoenebeck 221 }
71    
72 persson 846 MidiInputPort::MidiInputPort(MidiInputDevice* pDevice, int portNumber)
73     : MidiChannelMapReader(MidiChannelMap),
74     SysexListenersReader(SysexListeners) {
75 schoenebeck 221 this->pDevice = pDevice;
76     this->portNumber = portNumber;
77     Parameters["NAME"] = new ParameterName(this);
78 schoenebeck 551 pPreviousProgramChangeEngineChannel = NULL;
79 schoenebeck 221 }
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 persson 846 const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
95 schoenebeck 274 // dispatch event for engines listening to the same MIDI channel
96     {
97 persson 840 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
98     std::set<EngineChannel*>::iterator end = midiChannelMap[MidiChannel].end();
99 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity);
100     }
101     // dispatch event for engines listening to ALL MIDI channels
102     {
103 persson 840 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
104     std::set<EngineChannel*>::iterator end = midiChannelMap[midi_chan_all].end();
105 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity);
106     }
107 persson 846 MidiChannelMapReader.Unlock();
108 schoenebeck 221 }
109    
110     void MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel) {
111 persson 846 const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
112 schoenebeck 274 // dispatch event for engines listening to the same MIDI channel
113     {
114 persson 840 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
115     std::set<EngineChannel*>::iterator end = midiChannelMap[MidiChannel].end();
116 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity);
117     }
118     // dispatch event for engines listening to ALL MIDI channels
119     {
120 persson 840 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
121     std::set<EngineChannel*>::iterator end = midiChannelMap[midi_chan_all].end();
122 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity);
123     }
124 persson 846 MidiChannelMapReader.Unlock();
125 schoenebeck 221 }
126    
127     void MidiInputPort::DispatchPitchbend(int Pitch, uint MidiChannel) {
128 persson 846 const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
129 schoenebeck 274 // dispatch event for engines listening to the same MIDI channel
130     {
131 persson 840 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
132     std::set<EngineChannel*>::iterator end = midiChannelMap[MidiChannel].end();
133 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch);
134     }
135     // dispatch event for engines listening to ALL MIDI channels
136     {
137 persson 840 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
138     std::set<EngineChannel*>::iterator end = midiChannelMap[midi_chan_all].end();
139 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch);
140     }
141 persson 846 MidiChannelMapReader.Unlock();
142 schoenebeck 221 }
143    
144     void MidiInputPort::DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel) {
145 persson 846 const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
146 schoenebeck 274 // dispatch event for engines listening to the same MIDI channel
147     {
148 persson 840 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
149     std::set<EngineChannel*>::iterator end = midiChannelMap[MidiChannel].end();
150 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value);
151     }
152     // dispatch event for engines listening to ALL MIDI channels
153     {
154 persson 840 std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
155     std::set<EngineChannel*>::iterator end = midiChannelMap[midi_chan_all].end();
156 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value);
157     }
158 persson 846 MidiChannelMapReader.Unlock();
159 schoenebeck 221 }
160    
161 schoenebeck 244 void MidiInputPort::DispatchSysex(void* pData, uint Size) {
162 persson 846 const std::set<Engine*> allEngines = SysexListenersReader.Lock();
163 schoenebeck 775 // dispatch event to all engine instances
164 persson 846 std::set<Engine*>::iterator engineiter = allEngines.begin();
165     std::set<Engine*>::iterator end = allEngines.end();
166 schoenebeck 775 for (; engineiter != end; engineiter++) (*engineiter)->SendSysex(pData, Size);
167 persson 846 SysexListenersReader.Unlock();
168 schoenebeck 244 }
169    
170 schoenebeck 670 void MidiInputPort::DispatchProgramChange(uint8_t Program, uint MidiChannel) {
171 schoenebeck 551 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 schoenebeck 670
188 schoenebeck 551 // 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 schoenebeck 411 void MidiInputPort::Connect(EngineChannel* pEngineChannel, midi_chan_t MidiChannel) {
197 schoenebeck 221 if (MidiChannel < 0 || MidiChannel > 16)
198     throw MidiInputException("MIDI channel index out of bounds");
199 schoenebeck 670
200 persson 840 // first check if desired connection is already established
201 schoenebeck 675 MidiChannelMapMutex.Lock();
202 persson 840 MidiChannelMap_t& midiChannelMap = MidiChannelMap.GetConfigForUpdate();
203     bool bAlreadyDone = midiChannelMap[MidiChannel].count(pEngineChannel);
204 schoenebeck 675 MidiChannelMapMutex.Unlock();
205     if (bAlreadyDone) return;
206    
207     // remove all other connections of that engine channel (if any)
208 schoenebeck 411 Disconnect(pEngineChannel);
209 schoenebeck 670
210 schoenebeck 675 // register engine channel on the desired MIDI channel
211 schoenebeck 551 MidiChannelMapMutex.Lock();
212 persson 840 MidiChannelMap.GetConfigForUpdate()[MidiChannel].insert(pEngineChannel);
213     MidiChannelMap.SwitchConfig()[MidiChannel].insert(pEngineChannel);
214 schoenebeck 551 MidiChannelMapMutex.Unlock();
215 schoenebeck 670
216 schoenebeck 675 // inform engine channel about this connection
217     pEngineChannel->Connect(this, MidiChannel);
218    
219 schoenebeck 670 // mark engine channel as changed
220     pEngineChannel->StatusChanged(true);
221 schoenebeck 221 }
222    
223 schoenebeck 411 void MidiInputPort::Disconnect(EngineChannel* pEngineChannel) {
224 schoenebeck 675 if (!pEngineChannel) return;
225    
226     bool bChannelFound = false;
227    
228     // unregister engine channel from all MIDI channels
229 schoenebeck 551 MidiChannelMapMutex.Lock();
230 schoenebeck 675 try {
231 persson 840 {
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 schoenebeck 675 }
238 persson 840 // 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 schoenebeck 675 }
247 schoenebeck 221 catch(...) { /* NOOP */ }
248 schoenebeck 551 MidiChannelMapMutex.Unlock();
249 schoenebeck 675
250     // inform engine channel about the disconnection (if there is one)
251     if (bChannelFound) pEngineChannel->DisconnectMidiInputPort();
252    
253 schoenebeck 670 // mark engine channel as changed
254     pEngineChannel->StatusChanged(true);
255 schoenebeck 221 }
256    
257 persson 846 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 schoenebeck 221 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC