/[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 775 - (hide annotations) (download)
Wed Sep 21 14:30:43 2005 UTC (18 years, 7 months ago) by schoenebeck
File size: 10209 byte(s)
* src/drivers/MidiInputPort.cpp: fixed legacy SysEx code which caused
  dispatching of MIDI SysEx messages several times instead of once
* src/engines/gig/Engine.cpp: fixed bug in SysEx handling
  (patch by Juan Linietsky)

1 schoenebeck 221 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 411 * Copyright (C) 2005 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     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     Parameters.erase(iter);
67     delete iter->second;
68     iter++;
69     }
70     }
71    
72     MidiInputPort::MidiInputPort(MidiInputDevice* pDevice, int portNumber) {
73     this->pDevice = pDevice;
74     this->portNumber = portNumber;
75     Parameters["NAME"] = new ParameterName(this);
76 schoenebeck 551 pPreviousProgramChangeEngineChannel = NULL;
77 schoenebeck 221 }
78    
79     MidiInputDevice* MidiInputPort::GetDevice() {
80     return pDevice;
81     }
82    
83     uint MidiInputPort::GetPortNumber() {
84     return portNumber;
85     }
86    
87     std::map<String,DeviceRuntimeParameter*> MidiInputPort::PortParameters() {
88     return Parameters;
89     }
90    
91     void MidiInputPort::DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel) {
92 schoenebeck 274 // dispatch event for engines listening to the same MIDI channel
93     {
94 schoenebeck 411 std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[MidiChannel].begin();
95     std::set<EngineChannel*>::iterator end = MidiChannelMap[MidiChannel].end();
96 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity);
97     }
98     // dispatch event for engines listening to ALL MIDI channels
99     {
100 schoenebeck 411 std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[midi_chan_all].begin();
101     std::set<EngineChannel*>::iterator end = MidiChannelMap[midi_chan_all].end();
102 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity);
103     }
104 schoenebeck 221 }
105    
106     void MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel) {
107 schoenebeck 274 // dispatch event for engines listening to the same MIDI channel
108     {
109 schoenebeck 411 std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[MidiChannel].begin();
110     std::set<EngineChannel*>::iterator end = MidiChannelMap[MidiChannel].end();
111 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity);
112     }
113     // dispatch event for engines listening to ALL MIDI channels
114     {
115 schoenebeck 411 std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[midi_chan_all].begin();
116     std::set<EngineChannel*>::iterator end = MidiChannelMap[midi_chan_all].end();
117 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity);
118     }
119 schoenebeck 221 }
120    
121     void MidiInputPort::DispatchPitchbend(int Pitch, uint MidiChannel) {
122 schoenebeck 274 // dispatch event for engines listening to the same MIDI channel
123     {
124 schoenebeck 411 std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[MidiChannel].begin();
125     std::set<EngineChannel*>::iterator end = MidiChannelMap[MidiChannel].end();
126 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch);
127     }
128     // dispatch event for engines listening to ALL MIDI channels
129     {
130 schoenebeck 411 std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[midi_chan_all].begin();
131     std::set<EngineChannel*>::iterator end = MidiChannelMap[midi_chan_all].end();
132 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch);
133     }
134 schoenebeck 221 }
135    
136     void MidiInputPort::DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel) {
137 schoenebeck 274 // dispatch event for engines listening to the same MIDI channel
138     {
139 schoenebeck 411 std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[MidiChannel].begin();
140     std::set<EngineChannel*>::iterator end = MidiChannelMap[MidiChannel].end();
141 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value);
142     }
143     // dispatch event for engines listening to ALL MIDI channels
144     {
145 schoenebeck 411 std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[midi_chan_all].begin();
146     std::set<EngineChannel*>::iterator end = MidiChannelMap[midi_chan_all].end();
147 schoenebeck 274 for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value);
148     }
149 schoenebeck 221 }
150    
151 schoenebeck 244 void MidiInputPort::DispatchSysex(void* pData, uint Size) {
152 schoenebeck 775 // dispatch event to all engine instances
153     std::set<Engine*>::iterator engineiter = EngineFactory::EngineInstances().begin();
154     std::set<Engine*>::iterator end = EngineFactory::EngineInstances().end();
155     for (; engineiter != end; engineiter++) (*engineiter)->SendSysex(pData, Size);
156 schoenebeck 244 }
157    
158 schoenebeck 670 void MidiInputPort::DispatchProgramChange(uint8_t Program, uint MidiChannel) {
159 schoenebeck 551 if (!pDevice || !pDevice->pSampler) {
160     std::cerr << "MidiInputPort: ERROR, no sampler instance to handle program change."
161     << "This is a bug, please report it!\n" << std::flush;
162     return;
163     }
164    
165     Sampler* pSampler = (Sampler*) pDevice->pSampler;
166     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(Program);
167     if (!pSamplerChannel) return;
168    
169     EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
170     if (!pEngineChannel) return;
171    
172     // disconnect from the engine channel which was connected by the last PC event
173     if (pPreviousProgramChangeEngineChannel)
174     Disconnect(pPreviousProgramChangeEngineChannel);
175 schoenebeck 670
176 schoenebeck 551 // now connect to the new engine channel and remember it
177     try {
178     Connect(pEngineChannel, (midi_chan_t) MidiChannel);
179     pPreviousProgramChangeEngineChannel = pEngineChannel;
180     }
181     catch (...) { /* NOOP */ }
182     }
183    
184 schoenebeck 411 void MidiInputPort::Connect(EngineChannel* pEngineChannel, midi_chan_t MidiChannel) {
185 schoenebeck 221 if (MidiChannel < 0 || MidiChannel > 16)
186     throw MidiInputException("MIDI channel index out of bounds");
187 schoenebeck 670
188 schoenebeck 675 // firt check if desired connection is already established
189     MidiChannelMapMutex.Lock();
190     bool bAlreadyDone = MidiChannelMap[MidiChannel].count(pEngineChannel);
191     MidiChannelMapMutex.Unlock();
192     if (bAlreadyDone) return;
193    
194     // remove all other connections of that engine channel (if any)
195 schoenebeck 411 Disconnect(pEngineChannel);
196 schoenebeck 670
197 schoenebeck 675 // register engine channel on the desired MIDI channel
198 schoenebeck 551 MidiChannelMapMutex.Lock();
199 schoenebeck 411 MidiChannelMap[MidiChannel].insert(pEngineChannel);
200 schoenebeck 551 MidiChannelMapMutex.Unlock();
201 schoenebeck 670
202 schoenebeck 675 // inform engine channel about this connection
203     pEngineChannel->Connect(this, MidiChannel);
204    
205 schoenebeck 670 // mark engine channel as changed
206     pEngineChannel->StatusChanged(true);
207 schoenebeck 221 }
208    
209 schoenebeck 411 void MidiInputPort::Disconnect(EngineChannel* pEngineChannel) {
210 schoenebeck 675 if (!pEngineChannel) return;
211    
212     bool bChannelFound = false;
213    
214     // unregister engine channel from all MIDI channels
215 schoenebeck 551 MidiChannelMapMutex.Lock();
216 schoenebeck 675 try {
217     for (int i = 0; i <= 16; i++) {
218     bChannelFound |= MidiChannelMap[i].count(pEngineChannel);
219     MidiChannelMap[i].erase(pEngineChannel);
220     }
221     }
222 schoenebeck 221 catch(...) { /* NOOP */ }
223 schoenebeck 551 MidiChannelMapMutex.Unlock();
224 schoenebeck 675
225     // inform engine channel about the disconnection (if there is one)
226     if (bChannelFound) pEngineChannel->DisconnectMidiInputPort();
227    
228 schoenebeck 670 // mark engine channel as changed
229     pEngineChannel->StatusChanged(true);
230 schoenebeck 221 }
231    
232     } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC