1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2004, 2005 Grame * |
4 |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This program is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this program; if not, write to the Free Software * |
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
18 |
* MA 02111-1307 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#include "MidiInputDeviceCoreMidi.h" |
22 |
#include "MidiInputDeviceFactory.h" |
23 |
|
24 |
namespace LinuxSampler { |
25 |
|
26 |
int MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::pPortID = 0; |
27 |
|
28 |
MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterName::ParameterName(MidiInputPort* pPort) throw (LinuxSamplerException) : MidiInputPort::ParameterName(pPort, "Port " + ToString(pPort->GetPortNumber())) { |
29 |
OnSetValue(ValueAsString()); // initialize port name |
30 |
} |
31 |
|
32 |
void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterName::OnSetValue(String s) throw (LinuxSamplerException) { |
33 |
|
34 |
} |
35 |
|
36 |
// *************** ParameterCoreMidiBindings *************** |
37 |
// * |
38 |
|
39 |
MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::ParameterCoreMidiBindings(MidiInputPortCoreMidi* pPort) : DeviceRuntimeParameterStrings( std::vector<String>() ) { |
40 |
this->pPort = pPort; |
41 |
} |
42 |
|
43 |
String MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::Description() { |
44 |
return "Bindings to other CoreMidi clients"; |
45 |
} |
46 |
bool MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::Fix() { |
47 |
return false; |
48 |
} |
49 |
|
50 |
std::vector<String> MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::PossibilitiesAsString() { |
51 |
std::vector<String> res; |
52 |
// Connections |
53 |
return res; |
54 |
} |
55 |
|
56 |
void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::OnSetValue(std::vector<String> vS) throw (LinuxSamplerException) { |
57 |
// to finish |
58 |
} |
59 |
|
60 |
|
61 |
// *************** MidiInputPortCoreMidi *************** |
62 |
// * |
63 |
|
64 |
MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::MidiInputPortCoreMidi(MidiInputDeviceCoreMidi* pDevice) throw (MidiInputException) : MidiInputPort(pDevice, -1) { |
65 |
// create CoreMidi virtual destination |
66 |
|
67 |
MIDIDestinationCreate(pDevice->hCoreMidiClient, CFSTR("LinuxSampler_in"), ReadProc, this, &pDestination); |
68 |
if (!pDestination) throw MidiInputException("Error creating CoreMidi virtual destination"); |
69 |
this->portNumber = pPortID++; |
70 |
|
71 |
Parameters["NAME"] = new ParameterName(this); |
72 |
Parameters["CORE_MIDI_BINDINGS"] = new ParameterCoreMidiBindings(this); |
73 |
} |
74 |
|
75 |
MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::~MidiInputPortCoreMidi() { |
76 |
MIDIEndpointDispose(pDestination); |
77 |
} |
78 |
|
79 |
void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ReadProc(const MIDIPacketList* pktlist, void* refCon, void* connRefCon) |
80 |
{ |
81 |
MidiInputPortCoreMidi* port = (MidiInputPortCoreMidi*)refCon; |
82 |
MIDIPacket *packet = (MIDIPacket *)pktlist->packet; |
83 |
|
84 |
for (unsigned int i = 0; i < pktlist->numPackets; ++i) { |
85 |
|
86 |
int cin = packet->data[0] & 0xF0; |
87 |
|
88 |
// To be checked : several events per packet |
89 |
|
90 |
switch(cin) { // status byte |
91 |
|
92 |
case 0xB0: |
93 |
if (packet->data[1] == 0) |
94 |
port->DispatchBankSelectMsb(packet->data[2],packet->data[0]&0x0F); |
95 |
else if (packet->data[1] == 32) |
96 |
port->DispatchBankSelectLsb(packet->data[2],packet->data[0]&0x0F); |
97 |
else |
98 |
port->DispatchControlChange(packet->data[1],packet->data[2],packet->data[0]&0x0F); |
99 |
break; |
100 |
|
101 |
case 0xD0: |
102 |
port->DispatchControlChange(128,packet->data[1],packet->data[0]&0x0F); |
103 |
break; |
104 |
|
105 |
case 0xE0: |
106 |
port->DispatchPitchbend(packet->data[1],packet->data[0]&0x0F); |
107 |
break; |
108 |
|
109 |
case 0x90: |
110 |
if (packet->data[1] < 0x80) { |
111 |
if (packet->data[2] > 0){ |
112 |
port->DispatchNoteOn(packet->data[1],packet->data[2], packet->data[0]&0x0F); |
113 |
}else{ |
114 |
port->DispatchNoteOff(packet->data[1],packet->data[2],packet->data[0]&0x0F); |
115 |
} |
116 |
} |
117 |
break; |
118 |
|
119 |
case 0x80: |
120 |
if (packet->data[1] < 0x80) { |
121 |
port->DispatchNoteOff(packet->data[1],packet->data[2],packet->data[0]&0x0F); |
122 |
} |
123 |
break; |
124 |
|
125 |
case 0xC0: |
126 |
if (packet->data[1] < 0x80) { |
127 |
port->DispatchProgramChange(packet->data[1], packet->data[0] & 0x0F); |
128 |
} |
129 |
break; |
130 |
} |
131 |
|
132 |
packet = MIDIPacketNext(packet); |
133 |
} |
134 |
} |
135 |
|
136 |
|
137 |
// *************** MidiInputDeviceCoreMidi *************** |
138 |
// * |
139 |
|
140 |
MidiInputDeviceCoreMidi::MidiInputDeviceCoreMidi(std::map<String,DeviceCreationParameter*> Parameters, void* pSampler) : MidiInputDevice(Parameters, pSampler) |
141 |
{ |
142 |
MIDIClientCreate(CFSTR("LinuxSampler"), NotifyProc, NULL, &hCoreMidiClient); |
143 |
if (!hCoreMidiClient) throw MidiInputException("Error opening CoreMidi client"); |
144 |
AcquirePorts(((DeviceCreationParameterInt*)Parameters["PORTS"])->ValueAsInt()); |
145 |
} |
146 |
|
147 |
MidiInputDeviceCoreMidi::~MidiInputDeviceCoreMidi() |
148 |
{ |
149 |
if (hCoreMidiClient) { |
150 |
MIDIClientDispose(hCoreMidiClient); |
151 |
} |
152 |
} |
153 |
|
154 |
MidiInputDeviceCoreMidi::MidiInputPortCoreMidi* MidiInputDeviceCoreMidi::CreateMidiPort() { |
155 |
return new MidiInputPortCoreMidi(this); |
156 |
} |
157 |
|
158 |
String MidiInputDeviceCoreMidi::Name() { |
159 |
return "COREMIDI"; |
160 |
} |
161 |
|
162 |
String MidiInputDeviceCoreMidi::Driver() { |
163 |
return Name(); |
164 |
} |
165 |
|
166 |
String MidiInputDeviceCoreMidi::Description() { |
167 |
return "Apple CoreMidi"; |
168 |
} |
169 |
|
170 |
String MidiInputDeviceCoreMidi::Version() { |
171 |
String s = "$Revision: 1.8 $"; |
172 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
173 |
} |
174 |
|
175 |
void MidiInputDeviceCoreMidi::NotifyProc(const MIDINotification* message, void* refCon) |
176 |
{ |
177 |
// to be finished |
178 |
if (message->messageID == kMIDIMsgSetupChanged) { |
179 |
printf("kMIDIMsgSetupChanged\n"); |
180 |
} |
181 |
} |
182 |
|
183 |
|
184 |
} // namespace LinuxSampler |