1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2004 Grame * |
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 "MidiInputDeviceCoreMidi.h" |
25 |
|
26 |
namespace LinuxSampler { |
27 |
|
28 |
MidiInputDeviceCoreMidi::MidiInputDeviceCoreMidi(char* AutoConnectPortID) : MidiInputDevice(MidiInputDevice::type_core_midi) |
29 |
{ |
30 |
OSStatus err; |
31 |
|
32 |
err = MIDIClientCreate(CFSTR("LinuxSampler"), NotifyProc, NULL, &hCoreMidiClient); |
33 |
if (!hCoreMidiClient) { |
34 |
fprintf(stderr, "Cannot open CoreMidi client\n"); |
35 |
goto error; |
36 |
} |
37 |
|
38 |
err = MIDIInputPortCreate(hCoreMidiClient, CFSTR("Input port"), ReadProc, this, &hCoreMidiInPort); |
39 |
if (!hCoreMidiInPort) { |
40 |
fprintf(stderr, "Cannot open Midi in port\n"); |
41 |
goto error; |
42 |
} |
43 |
|
44 |
if (AutoConnectPortID) ConnectToCoreMidiSource(AutoConnectPortID); |
45 |
|
46 |
// for test : to be improved |
47 |
ConnectToCoreMidiSource(NULL); |
48 |
return; |
49 |
|
50 |
error : |
51 |
if (hCoreMidiInPort){ |
52 |
MIDIPortDispose(hCoreMidiInPort); |
53 |
} |
54 |
|
55 |
if (hCoreMidiClient) { |
56 |
MIDIClientDispose(hCoreMidiClient); |
57 |
} |
58 |
|
59 |
throw MidiInputException("Error opening CoreMidi device"); |
60 |
} |
61 |
|
62 |
void MidiInputDeviceCoreMidi::SetInputPort(const char * MidiSource) |
63 |
{ |
64 |
ConnectToCoreMidiSource(MidiSource); |
65 |
} |
66 |
|
67 |
MidiInputDeviceCoreMidi::~MidiInputDeviceCoreMidi() |
68 |
{ |
69 |
if (hCoreMidiInPort){ |
70 |
MIDIPortDispose(hCoreMidiInPort); |
71 |
} |
72 |
|
73 |
if (hCoreMidiClient) { |
74 |
MIDIClientDispose(hCoreMidiClient); |
75 |
} |
76 |
} |
77 |
|
78 |
void MidiInputDeviceCoreMidi::ConnectToCoreMidiSource(const char* MidiSource) |
79 |
{ |
80 |
// Open connections from all sources : to be improved |
81 |
int n = MIDIGetNumberOfSources(); |
82 |
for (int i = 0; i < n; ++i) { |
83 |
MIDIEndpointRef src = MIDIGetSource(i); |
84 |
MIDIPortConnectSource(hCoreMidiInPort, src, NULL); |
85 |
} |
86 |
} |
87 |
|
88 |
void MidiInputDeviceCoreMidi::NotifyProc(const MIDINotification *message, void *refCon) |
89 |
{ |
90 |
if (message->messageID == kMIDIMsgSetupChanged) { |
91 |
printf("kMIDIMsgSetupChanged\n"); |
92 |
} |
93 |
} |
94 |
|
95 |
void MidiInputDeviceCoreMidi::ReadProc(const MIDIPacketList *pktlist, void *refCon, void *connRefCon) |
96 |
{ |
97 |
MidiInputDeviceCoreMidi* driver = (MidiInputDeviceCoreMidi*)refCon; |
98 |
MIDIPacket *packet = (MIDIPacket *)pktlist->packet; |
99 |
|
100 |
for (int i = 0; i < pktlist->numPackets; ++i) { |
101 |
|
102 |
int cin = packet->data[0] & 0xF0; |
103 |
|
104 |
// To be checked : several events per packet |
105 |
|
106 |
switch(cin) { // status byte |
107 |
|
108 |
case 0xB0: |
109 |
driver->DispatchControlChange(packet->data[1],packet->data[2],packet->data[0]&0x0F); |
110 |
break; |
111 |
|
112 |
case 0xE0: |
113 |
driver->DispatchPitchbend(packet->data[1],packet->data[0]&0x0F); |
114 |
break; |
115 |
|
116 |
case 0x90: |
117 |
if (packet->data[1] < 128){ |
118 |
if (packet->data[2] > 0){ |
119 |
driver->DispatchNoteOn(packet->data[1],packet->data[2], packet->data[0]&0x0F); |
120 |
}else{ |
121 |
driver->DispatchNoteOff(packet->data[1],packet->data[2],packet->data[0]&0x0F); |
122 |
} |
123 |
} |
124 |
break; |
125 |
|
126 |
case 0x80: |
127 |
if (packet->data[1] < 128){ |
128 |
driver->DispatchNoteOff(packet->data[1],packet->data[2],packet->data[0]&0x0F); |
129 |
} |
130 |
break; |
131 |
} |
132 |
|
133 |
packet = MIDIPacketNext(packet); |
134 |
} |
135 |
} |
136 |
|
137 |
|
138 |
} // namespace LinuxSampler |