1 |
schoenebeck |
53 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
schoenebeck |
56 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
schoenebeck |
53 |
* * |
7 |
|
|
* This program is free software; you can redistribute it and/or modify * |
8 |
|
|
* it under the terms of the GNU General Public License as published by * |
9 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
10 |
|
|
* (at your option) any later version. * |
11 |
|
|
* * |
12 |
|
|
* This program is distributed in the hope that it will be useful, * |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 |
|
|
* GNU General Public License for more details. * |
16 |
|
|
* * |
17 |
|
|
* You should have received a copy of the GNU General Public License * |
18 |
|
|
* along with this program; if not, write to the Free Software * |
19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
20 |
|
|
* MA 02111-1307 USA * |
21 |
|
|
***************************************************************************/ |
22 |
|
|
|
23 |
|
|
#include "MidiInputDevice.h" |
24 |
|
|
|
25 |
|
|
namespace LinuxSampler { |
26 |
|
|
|
27 |
senkov |
153 |
MidiInputDevice::MidiInputDevice(std::map<String,DeviceCreationParameter*> DriverParameters) { |
28 |
|
|
this->Parameters = DriverParameters; |
29 |
schoenebeck |
64 |
} |
30 |
|
|
|
31 |
senkov |
153 |
MidiInputDevice::~MidiInputDevice() { |
32 |
|
|
std::map<String,DeviceCreationParameter*>::iterator iter = Parameters.begin(); |
33 |
|
|
while (iter != Parameters.end()) { |
34 |
|
|
Parameters.erase(iter); |
35 |
|
|
delete iter->second; |
36 |
|
|
iter++; |
37 |
|
|
} |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
std::map<String,DeviceCreationParameter*> MidiInputDevice::AvailableParameters() { |
41 |
|
|
static const std::map<String,DeviceCreationParameter*> available_parameters = CreateAvailableParameters(); |
42 |
|
|
return available_parameters; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
std::map<String,DeviceCreationParameter*> MidiInputDevice::CreateAvailableParameters() { |
46 |
|
|
static ParameterActive param_active(NULL); |
47 |
|
|
static ParameterPorts param_ports(NULL); |
48 |
|
|
std::map<String,DeviceCreationParameter*> result; |
49 |
|
|
result["active"] = ¶m_active; |
50 |
|
|
result["ports"] = ¶m_ports; |
51 |
|
|
return result; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
MidiInputDevice::MidiInputPort::~MidiInputPort() { |
55 |
|
|
std::map<String,DeviceCreationParameter*>::iterator iter = Parameters.begin(); |
56 |
|
|
while (iter != Parameters.end()) { |
57 |
|
|
Parameters.erase(iter); |
58 |
|
|
delete iter->second; |
59 |
|
|
iter++; |
60 |
|
|
} |
61 |
|
|
} |
62 |
|
|
|
63 |
senkov |
157 |
MidiInputDevice::MidiInputPort::MidiInputPort(MidiInputDevice* pDevice, int portNumber) { |
64 |
|
|
this->pDevice = pDevice; |
65 |
|
|
this->portNumber = portNumber; |
66 |
|
|
Parameters["name"] = new ParameterName(this); |
67 |
|
|
} |
68 |
|
|
|
69 |
senkov |
153 |
MidiInputDevice* MidiInputDevice::MidiInputPort::GetDevice() { |
70 |
|
|
return pDevice; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
uint MidiInputDevice::MidiInputPort::GetPortNumber() { |
74 |
|
|
return portNumber; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
std::map<String,DeviceCreationParameter*> MidiInputDevice::DeviceParameters() { |
78 |
|
|
return Parameters; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
std::map<String,DeviceCreationParameter*> MidiInputDevice::MidiInputPort::DeviceParameters() { |
82 |
|
|
return Parameters; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
void MidiInputDevice::MidiInputPort::DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel) { |
86 |
schoenebeck |
53 |
std::set<Engine*>::iterator engineiter = MidiChannelMap[MidiChannel].begin(); |
87 |
|
|
std::set<Engine*>::iterator end = MidiChannelMap[MidiChannel].end(); |
88 |
|
|
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity); |
89 |
|
|
} |
90 |
|
|
|
91 |
senkov |
153 |
void MidiInputDevice::MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel) { |
92 |
schoenebeck |
53 |
std::set<Engine*>::iterator engineiter = MidiChannelMap[MidiChannel].begin(); |
93 |
|
|
std::set<Engine*>::iterator end = MidiChannelMap[MidiChannel].end(); |
94 |
|
|
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity); |
95 |
|
|
} |
96 |
|
|
|
97 |
senkov |
153 |
void MidiInputDevice::MidiInputPort::DispatchPitchbend(int Pitch, uint MidiChannel) { |
98 |
schoenebeck |
53 |
std::set<Engine*>::iterator engineiter = MidiChannelMap[MidiChannel].begin(); |
99 |
|
|
std::set<Engine*>::iterator end = MidiChannelMap[MidiChannel].end(); |
100 |
|
|
for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch); |
101 |
|
|
} |
102 |
|
|
|
103 |
senkov |
153 |
void MidiInputDevice::MidiInputPort::DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel) { |
104 |
schoenebeck |
53 |
std::set<Engine*>::iterator engineiter = MidiChannelMap[MidiChannel].begin(); |
105 |
|
|
std::set<Engine*>::iterator end = MidiChannelMap[MidiChannel].end(); |
106 |
|
|
for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value); |
107 |
|
|
} |
108 |
|
|
|
109 |
senkov |
153 |
void MidiInputDevice::MidiInputPort::Connect(Engine* pEngine, midi_chan_t MidiChannel) { |
110 |
schoenebeck |
53 |
if (MidiChannel < 0 || MidiChannel > 16) |
111 |
|
|
throw MidiInputException("MIDI channel index out of bounds"); |
112 |
|
|
Disconnect(pEngine); |
113 |
|
|
MidiChannelMap[MidiChannel].insert(pEngine); |
114 |
|
|
} |
115 |
|
|
|
116 |
senkov |
153 |
void MidiInputDevice::MidiInputPort::Disconnect(Engine* pEngine) { |
117 |
schoenebeck |
53 |
try { for (int i = 0; i <= 16; i++) MidiChannelMap[i].erase(pEngine); } |
118 |
|
|
catch(...) { /* NOOP */ } |
119 |
|
|
} |
120 |
|
|
|
121 |
senkov |
153 |
void MidiInputDevice::AcquirePorts(uint newPorts) { |
122 |
|
|
int diff = this->Ports.size() - newPorts; |
123 |
|
|
if (!diff) |
124 |
|
|
return; //Number of ports matches already, nothing to do. |
125 |
|
|
|
126 |
|
|
while (diff != 0) { |
127 |
|
|
if (diff > 0) { //We've got too many ports, remove one |
128 |
|
|
std::map<int,MidiInputPort*>::iterator portsIter = Ports.end(); |
129 |
|
|
Ports.erase(--portsIter); |
130 |
|
|
delete portsIter->second; |
131 |
|
|
diff--; |
132 |
|
|
} |
133 |
|
|
if (diff < 0) { //We don't have enough ports, create one |
134 |
|
|
MidiInputPort* midiPort = this->CreateMidiPort(); |
135 |
|
|
Ports[midiPort->portNumber] = midiPort; |
136 |
|
|
diff++; |
137 |
|
|
} |
138 |
|
|
} |
139 |
schoenebeck |
64 |
} |
140 |
|
|
|
141 |
schoenebeck |
53 |
} // namespace LinuxSampler |