22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
#include "MidiInputDeviceCoreMidi.h" |
#include "MidiInputDeviceCoreMidi.h" |
25 |
|
#include "MidiInputDeviceFactory.h" |
26 |
|
|
27 |
namespace LinuxSampler { |
namespace LinuxSampler { |
28 |
|
|
29 |
MidiInputDeviceCoreMidi::MidiInputDeviceCoreMidi(char* AutoConnectPortID) : MidiInputDevice(MidiInputDevice::type_core_midi) |
int MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::pPortID = 0; |
30 |
{ |
|
31 |
OSStatus err; |
MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterName::ParameterName(MidiInputPort* pPort) throw (LinuxSamplerException) : MidiInputPort::ParameterName(pPort, "Port " + ToString(pPort->GetPortNumber())) { |
32 |
|
OnSetValue(ValueAsString()); // initialize port name |
33 |
|
} |
34 |
|
|
35 |
|
void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterName::OnSetValue(String s) throw (LinuxSamplerException) { |
36 |
|
|
37 |
|
} |
38 |
|
|
39 |
err = MIDIClientCreate(CFSTR("LinuxSampler"), NotifyProc, NULL, &hCoreMidiClient); |
// *************** ParameterCoreMidiBindings *************** |
40 |
if (!hCoreMidiClient) { |
// * |
41 |
fprintf(stderr, "Cannot open CoreMidi client\n"); |
|
42 |
goto error; |
MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::ParameterCoreMidiBindings(MidiInputPortCoreMidi* pPort) : DeviceRuntimeParameterStrings( std::vector<String>() ) { |
43 |
} |
this->pPort = pPort; |
|
|
|
|
err = MIDIInputPortCreate(hCoreMidiClient, CFSTR("Input port"), ReadProc, this, &hCoreMidiInPort); |
|
|
if (!hCoreMidiInPort) { |
|
|
fprintf(stderr, "Cannot open Midi in port\n"); |
|
|
goto error; |
|
|
} |
|
|
|
|
|
if (AutoConnectPortID) ConnectToCoreMidiSource(AutoConnectPortID); |
|
|
|
|
|
// for test : to be improved |
|
|
ConnectToCoreMidiSource(NULL); |
|
|
return; |
|
|
|
|
|
error : |
|
|
if (hCoreMidiInPort){ |
|
|
MIDIPortDispose(hCoreMidiInPort); |
|
|
} |
|
|
|
|
|
if (hCoreMidiClient) { |
|
|
MIDIClientDispose(hCoreMidiClient); |
|
|
} |
|
|
|
|
|
throw MidiInputException("Error opening CoreMidi device"); |
|
44 |
} |
} |
45 |
|
|
46 |
void MidiInputDeviceCoreMidi::SetInputPort(const char * MidiSource) |
String MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::Description() { |
47 |
{ |
return "Bindings to other CoreMidi clients"; |
48 |
ConnectToCoreMidiSource(MidiSource); |
} |
49 |
|
bool MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::Fix() { |
50 |
|
return false; |
51 |
} |
} |
52 |
|
|
53 |
MidiInputDeviceCoreMidi::~MidiInputDeviceCoreMidi() |
std::vector<String> MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::PossibilitiesAsString() { |
54 |
{ |
std::vector<String> res; |
55 |
if (hCoreMidiInPort){ |
// Connections |
56 |
MIDIPortDispose(hCoreMidiInPort); |
return res; |
57 |
} |
} |
58 |
|
|
59 |
|
void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::OnSetValue(std::vector<String> vS) throw (LinuxSamplerException) { |
60 |
|
// to finish |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
|
// *************** MidiInputPortCoreMidi *************** |
65 |
|
// * |
66 |
|
|
67 |
|
MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::MidiInputPortCoreMidi(MidiInputDeviceCoreMidi* pDevice) throw (MidiInputException) : MidiInputPort(pDevice, -1) { |
68 |
|
// create CoreMidi virtual destination |
69 |
|
|
70 |
|
MIDIDestinationCreate(pDevice->hCoreMidiClient, CFSTR("LinuxSampler_in"), ReadProc, this, &pDestination); |
71 |
|
if (!pDestination) throw MidiInputException("Error creating CoreMidi virtual destination"); |
72 |
|
this->portNumber = pPortID++; |
73 |
|
|
74 |
if (hCoreMidiClient) { |
Parameters["NAME"] = new ParameterName(this); |
75 |
MIDIClientDispose(hCoreMidiClient); |
Parameters["CORE_MIDI_BINDINGS"] = new ParameterCoreMidiBindings(this); |
|
} |
|
76 |
} |
} |
77 |
|
|
78 |
void MidiInputDeviceCoreMidi::ConnectToCoreMidiSource(const char* MidiSource) |
MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::~MidiInputPortCoreMidi() { |
79 |
{ |
MIDIEndpointDispose(pDestination); |
80 |
// Open connections from all sources : to be improved |
} |
|
int n = MIDIGetNumberOfSources(); |
|
|
for (int i = 0; i < n; ++i) { |
|
|
MIDIEndpointRef src = MIDIGetSource(i); |
|
|
MIDIPortConnectSource(hCoreMidiInPort, src, NULL); |
|
|
} |
|
|
} |
|
|
|
|
|
void MidiInputDeviceCoreMidi::NotifyProc(const MIDINotification *message, void *refCon) |
|
|
{ |
|
|
if (message->messageID == kMIDIMsgSetupChanged) { |
|
|
printf("kMIDIMsgSetupChanged\n"); |
|
|
} |
|
|
} |
|
81 |
|
|
82 |
void MidiInputDeviceCoreMidi::ReadProc(const MIDIPacketList *pktlist, void *refCon, void *connRefCon) |
void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ReadProc(const MIDIPacketList* pktlist, void* refCon, void* connRefCon) |
83 |
{ |
{ |
84 |
MidiInputDeviceCoreMidi* driver = (MidiInputDeviceCoreMidi*)refCon; |
MidiInputPortCoreMidi* port = (MidiInputPortCoreMidi*)refCon; |
85 |
MIDIPacket *packet = (MIDIPacket *)pktlist->packet; |
MIDIPacket *packet = (MIDIPacket *)pktlist->packet; |
86 |
|
|
87 |
for (int i = 0; i < pktlist->numPackets; ++i) { |
for (unsigned int i = 0; i < pktlist->numPackets; ++i) { |
88 |
|
|
89 |
int cin = packet->data[0] & 0xF0; |
int cin = packet->data[0] & 0xF0; |
90 |
|
|
93 |
switch(cin) { // status byte |
switch(cin) { // status byte |
94 |
|
|
95 |
case 0xB0: |
case 0xB0: |
96 |
driver->DispatchControlChange(packet->data[1],packet->data[2],packet->data[0]&0x0F); |
port->DispatchControlChange(packet->data[1],packet->data[2],packet->data[0]&0x0F); |
97 |
break; |
break; |
98 |
|
|
99 |
case 0xE0: |
case 0xE0: |
100 |
driver->DispatchPitchbend(packet->data[1],packet->data[0]&0x0F); |
port->DispatchPitchbend(packet->data[1],packet->data[0]&0x0F); |
101 |
break; |
break; |
102 |
|
|
103 |
case 0x90: |
case 0x90: |
104 |
if (packet->data[1] < 128){ |
if (packet->data[1] < 0x80) { |
105 |
if (packet->data[2] > 0){ |
if (packet->data[2] > 0){ |
106 |
driver->DispatchNoteOn(packet->data[1],packet->data[2], packet->data[0]&0x0F); |
port->DispatchNoteOn(packet->data[1],packet->data[2], packet->data[0]&0x0F); |
107 |
}else{ |
}else{ |
108 |
driver->DispatchNoteOff(packet->data[1],packet->data[2],packet->data[0]&0x0F); |
port->DispatchNoteOff(packet->data[1],packet->data[2],packet->data[0]&0x0F); |
109 |
} |
} |
110 |
} |
} |
111 |
break; |
break; |
112 |
|
|
113 |
case 0x80: |
case 0x80: |
114 |
if (packet->data[1] < 128){ |
if (packet->data[1] < 0x80) { |
115 |
driver->DispatchNoteOff(packet->data[1],packet->data[2],packet->data[0]&0x0F); |
port->DispatchNoteOff(packet->data[1],packet->data[2],packet->data[0]&0x0F); |
116 |
} |
} |
117 |
break; |
break; |
118 |
} |
} |
119 |
|
|
120 |
packet = MIDIPacketNext(packet); |
packet = MIDIPacketNext(packet); |
121 |
} |
} |
122 |
} |
} |
123 |
|
|
124 |
|
MidiInputDeviceCoreMidi::MidiInputDeviceCoreMidi(std::map<String,DeviceCreationParameter*> Parameters) : MidiInputDevice(Parameters) |
125 |
|
{ |
126 |
|
MIDIClientCreate(CFSTR("LinuxSampler"), NotifyProc, NULL, &hCoreMidiClient); |
127 |
|
if (!hCoreMidiClient) throw MidiInputException("Error opening CoreMidi client"); |
128 |
|
AcquirePorts(((DeviceCreationParameterInt*)Parameters["PORTS"])->ValueAsInt()); |
129 |
|
} |
130 |
|
|
131 |
|
MidiInputDeviceCoreMidi::~MidiInputDeviceCoreMidi() |
132 |
|
{ |
133 |
|
if (hCoreMidiClient) { |
134 |
|
MIDIClientDispose(hCoreMidiClient); |
135 |
|
} |
136 |
|
} |
137 |
|
|
138 |
|
MidiInputDeviceCoreMidi::MidiInputPortCoreMidi* MidiInputDeviceCoreMidi::CreateMidiPort() { |
139 |
|
return new MidiInputPortCoreMidi(this); |
140 |
|
} |
141 |
|
|
142 |
|
String MidiInputDeviceCoreMidi::Name() { |
143 |
|
return "CoreMidi"; |
144 |
|
} |
145 |
|
|
146 |
|
String MidiInputDeviceCoreMidi::Driver() { |
147 |
|
return Name(); |
148 |
|
} |
149 |
|
|
150 |
|
String MidiInputDeviceCoreMidi::Description() { |
151 |
|
return "Apple CoreMidi"; |
152 |
|
} |
153 |
|
|
154 |
|
String MidiInputDeviceCoreMidi::Version() { |
155 |
|
String s = "$Revision: 1.4 $"; |
156 |
|
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
157 |
|
} |
158 |
|
|
159 |
|
void MidiInputDeviceCoreMidi::NotifyProc(const MIDINotification* message, void* refCon) |
160 |
|
{ |
161 |
|
// to be finished |
162 |
|
if (message->messageID == kMIDIMsgSetupChanged) { |
163 |
|
printf("kMIDIMsgSetupChanged\n"); |
164 |
|
} |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |