-
Notifications
You must be signed in to change notification settings - Fork 0
/
mididevice.mm
60 lines (47 loc) · 1.86 KB
/
mididevice.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <mididevice.hh>
#import <CoreMIDI/CoreMIDI.h>
#import <Foundation/Foundation.h>
#import <Foundation/NSObjCRuntime.h>
#include <QDebug>
static void midiInputCallback(const MIDIPacketList *list,
void *procRef,
void *srcRef)
{
auto device = reinterpret_cast<En::MidiDevice::MidiDevice*>(procRef);
for (UInt32 i = 0; i < list->numPackets; i++) {
const MIDIPacket *packet = &list->packet[i];
for (UInt16 j = 0, size = 0; j < packet->length; j += size) {
UInt8 status = packet->data[j];
if (status < 0xC0) size = 3;
else if (status < 0xE0) size = 2;
else if (status < 0xF0) size = 3;
else if (status < 0xF3) size = 3;
else if (status == 0xF3) size = 2;
else size = 1;
QByteArray midiEvent(reinterpret_cast<const char*>(packet->data + j), size);
device->postMidiEvent(midiEvent);
}
}
}
En::MidiDevice::MidiDevice(QObject *parent) : QObject(parent)
{
OSStatus status;
status = MIDIClientCreate(CFSTR("MIDI client"), NULL, NULL, &m_midiClient);
NSCAssert1(status == noErr, @"Error creating MIDI client: %d", status);
status = MIDIInputPortCreate(m_midiClient, CFSTR("MIDI input"), midiInputCallback, this, &m_inputPort);
NSCAssert1(status == noErr, @"Error creating MIDI input port: %d", status);
ItemCount numOfDevices = MIDIGetNumberOfDevices();
for (ItemCount i = 0; i < numOfDevices; i++) {
qDebug() << "Connecting MIDI device" << i;
MIDIEndpointRef src = MIDIGetSource(i);
MIDIPortConnectSource(m_inputPort, src, NULL);
}
}
En::MidiDevice::~MidiDevice()
{
MIDIClientDispose(m_midiClient);
}
void En::MidiDevice::postMidiEvent(const QByteArray& event)
{
emit eventPosted(event);
}