#include "Midi.h" MidiPort::MidiPort(void) { int n, i; hours = 0; minutes = 0; seconds = 0; frames = 0; lastSeg = -1; client = NULL; inPort = NULL; outPort = NULL; MIDIClientCreate(CFSTR("AudioEngine"), NULL, NULL, &client); MIDIInputPortCreate(client, CFSTR("Input port"), MidiReceiveProc, NULL, &inPort); MIDIOutputPortCreate(client, CFSTR("Output port"), &outPort); // open connections from all sources n = MIDIGetNumberOfSources(); for (i = 0; i < n; ++i) { MIDIEndpointRef src = MIDIGetSource(i); MIDIPortConnectSource(inPort, src, NULL); } } MidiPort::~MidiPort(void) { MIDIPortDispose(outPort); MIDIPortDispose(inPort); MIDIClientDispose(client); } void MidiPort::MidiSendtoAll(Byte *data, int size) { int n, i; Byte buffer[256]; MIDIEndpointRef dest = NULL; MIDIPacketList *pktlist; MIDIPacket *pkt; // set up the midi packet list pktlist = (MIDIPacketList *)buffer; pkt = MIDIPacketListInit(pktlist); MIDIPacketListAdd(pktlist, sizeof(buffer), pkt, 0, size, data); // 0 = the time stamp for right now // cycle through all destinations and send the packet list to each n = MIDIGetNumberOfDestinations(); for(i = 0; i < n; i++){ dest = MIDIGetDestination(i); MIDISend(outPort, dest, pktlist); } } static void MidiPort::MidiReceiveProc(const MIDIPacketList *pktlist, void *refCon, void *connRefCon) { char val, segment; MidiPort *parent; MIDIPacket *packet; parent = (MidiPort*)refCon; packet = (MIDIPacket *)pktlist->packet; // remove const (!) for (unsigned int j = 0; j < pktlist->numPackets; ++j) { // if it's MTC quarter fram packet if ((packet->data[0] == 0xF1) && (packet->length > 1)){ segment = (packet->data[1] & 0x70); val = (packet->data[1] & 0x0F); switch(segment){ case 0x00: if(lastSeg == 0x70) // forward nibble = val; else //backward frames = val + nibble; break; case 0x10: if(lastSeg == 0x00) // forward frames = nibble + (val << 4) else //backward nibble = (val << 4); break; case 0x20: if(lastSeg == 0x10) // forward nibble = val; else //backward seconds = val + nibble; break; case 0x30: if(lastSeg == 0x20) // forward seconds = nibble + (val << 4) else //backward nibble = (val << 4); break; case 0x40: if(lastSeg == 0x30) // forward nibble = val; else //backward minutes = val + nibble; break; case 0x50: if(lastSeg == 0x40) // forward minutes = nibble + (val << 4) else //backward nibble = (val << 4); break; case 0x60: if(lastSeg == 0x50) // forward nibble = val; else //backward hours = val + nibble; break; case 0x70: val = (val & 0x01); if(lastSeg == 0x50) // forward hours = nibble + (val << 4) else //backward nibble = (val << 4); break; } lastSeg = segment; } packet = MIDIPacketNext(packet); } }