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 |
namespace LinuxSampler { |
27 |
|
28 |
// *************** ParameterName *************** |
29 |
// * |
30 |
|
31 |
MidiInputPort::ParameterName::ParameterName(MidiInputPort* pPort) : DeviceRuntimeParameterString("Port " + ToString(pPort->GetPortNumber())) { |
32 |
this->pPort = pPort; |
33 |
} |
34 |
|
35 |
MidiInputPort::ParameterName::ParameterName(MidiInputPort* pPort, String val) : DeviceRuntimeParameterString(val) { |
36 |
this->pPort = pPort; |
37 |
} |
38 |
|
39 |
String MidiInputPort::ParameterName::Description() { |
40 |
return "Name for this port"; |
41 |
} |
42 |
|
43 |
bool MidiInputPort::ParameterName::Fix() { |
44 |
return false; |
45 |
} |
46 |
|
47 |
std::vector<String> MidiInputPort::ParameterName::PossibilitiesAsString() { |
48 |
return std::vector<String>(); |
49 |
} |
50 |
|
51 |
void MidiInputPort::ParameterName::OnSetValue(String s) throw (LinuxSamplerException) { |
52 |
return; /* FIXME: Nothing to do here */ |
53 |
} |
54 |
|
55 |
|
56 |
|
57 |
// *************** MidiInputPort *************** |
58 |
// * |
59 |
|
60 |
MidiInputPort::~MidiInputPort() { |
61 |
std::map<String,DeviceRuntimeParameter*>::iterator iter = Parameters.begin(); |
62 |
while (iter != Parameters.end()) { |
63 |
Parameters.erase(iter); |
64 |
delete iter->second; |
65 |
iter++; |
66 |
} |
67 |
} |
68 |
|
69 |
MidiInputPort::MidiInputPort(MidiInputDevice* pDevice, int portNumber) { |
70 |
this->pDevice = pDevice; |
71 |
this->portNumber = portNumber; |
72 |
Parameters["NAME"] = new ParameterName(this); |
73 |
} |
74 |
|
75 |
MidiInputDevice* MidiInputPort::GetDevice() { |
76 |
return pDevice; |
77 |
} |
78 |
|
79 |
uint MidiInputPort::GetPortNumber() { |
80 |
return portNumber; |
81 |
} |
82 |
|
83 |
std::map<String,DeviceRuntimeParameter*> MidiInputPort::PortParameters() { |
84 |
return Parameters; |
85 |
} |
86 |
|
87 |
void MidiInputPort::DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel) { |
88 |
// dispatch event for engines listening to the same MIDI channel |
89 |
{ |
90 |
std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[MidiChannel].begin(); |
91 |
std::set<EngineChannel*>::iterator end = MidiChannelMap[MidiChannel].end(); |
92 |
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity); |
93 |
} |
94 |
// dispatch event for engines listening to ALL MIDI channels |
95 |
{ |
96 |
std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[midi_chan_all].begin(); |
97 |
std::set<EngineChannel*>::iterator end = MidiChannelMap[midi_chan_all].end(); |
98 |
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity); |
99 |
} |
100 |
} |
101 |
|
102 |
void MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel) { |
103 |
// dispatch event for engines listening to the same MIDI channel |
104 |
{ |
105 |
std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[MidiChannel].begin(); |
106 |
std::set<EngineChannel*>::iterator end = MidiChannelMap[MidiChannel].end(); |
107 |
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity); |
108 |
} |
109 |
// dispatch event for engines listening to ALL MIDI channels |
110 |
{ |
111 |
std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[midi_chan_all].begin(); |
112 |
std::set<EngineChannel*>::iterator end = MidiChannelMap[midi_chan_all].end(); |
113 |
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity); |
114 |
} |
115 |
} |
116 |
|
117 |
void MidiInputPort::DispatchPitchbend(int Pitch, uint MidiChannel) { |
118 |
// dispatch event for engines listening to the same MIDI channel |
119 |
{ |
120 |
std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[MidiChannel].begin(); |
121 |
std::set<EngineChannel*>::iterator end = MidiChannelMap[MidiChannel].end(); |
122 |
for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch); |
123 |
} |
124 |
// dispatch event for engines listening to ALL MIDI channels |
125 |
{ |
126 |
std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[midi_chan_all].begin(); |
127 |
std::set<EngineChannel*>::iterator end = MidiChannelMap[midi_chan_all].end(); |
128 |
for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch); |
129 |
} |
130 |
} |
131 |
|
132 |
void MidiInputPort::DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel) { |
133 |
// dispatch event for engines listening to the same MIDI channel |
134 |
{ |
135 |
std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[MidiChannel].begin(); |
136 |
std::set<EngineChannel*>::iterator end = MidiChannelMap[MidiChannel].end(); |
137 |
for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value); |
138 |
} |
139 |
// dispatch event for engines listening to ALL MIDI channels |
140 |
{ |
141 |
std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[midi_chan_all].begin(); |
142 |
std::set<EngineChannel*>::iterator end = MidiChannelMap[midi_chan_all].end(); |
143 |
for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value); |
144 |
} |
145 |
} |
146 |
|
147 |
void MidiInputPort::DispatchSysex(void* pData, uint Size) { |
148 |
// dispatch event for engines listening to the same MIDI channel |
149 |
{ |
150 |
for (uint MidiChannel = 0; MidiChannel <= 16; MidiChannel++) { |
151 |
std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[MidiChannel].begin(); |
152 |
std::set<EngineChannel*>::iterator end = MidiChannelMap[MidiChannel].end(); |
153 |
for (; engineiter != end; engineiter++) { |
154 |
Engine* pEngine = (*engineiter)->GetEngine(); |
155 |
if (pEngine) pEngine->SendSysex(pData, Size); |
156 |
} |
157 |
} |
158 |
} |
159 |
// dispatch event for engines listening to ALL MIDI channels |
160 |
{ |
161 |
for (uint MidiChannel = 0; MidiChannel <= 16; MidiChannel++) { |
162 |
std::set<EngineChannel*>::iterator engineiter = MidiChannelMap[midi_chan_all].begin(); |
163 |
std::set<EngineChannel*>::iterator end = MidiChannelMap[midi_chan_all].end(); |
164 |
for (; engineiter != end; engineiter++) { |
165 |
Engine* pEngine = (*engineiter)->GetEngine(); |
166 |
if (pEngine) pEngine->SendSysex(pData, Size); |
167 |
} |
168 |
} |
169 |
} |
170 |
} |
171 |
|
172 |
void MidiInputPort::Connect(EngineChannel* pEngineChannel, midi_chan_t MidiChannel) { |
173 |
if (MidiChannel < 0 || MidiChannel > 16) |
174 |
throw MidiInputException("MIDI channel index out of bounds"); |
175 |
Disconnect(pEngineChannel); |
176 |
MidiChannelMap[MidiChannel].insert(pEngineChannel); |
177 |
} |
178 |
|
179 |
void MidiInputPort::Disconnect(EngineChannel* pEngineChannel) { |
180 |
try { for (int i = 0; i <= 16; i++) MidiChannelMap[i].erase(pEngineChannel); } |
181 |
catch(...) { /* NOOP */ } |
182 |
} |
183 |
|
184 |
} // namespace LinuxSampler |