Skip to content

Instantly share code, notes, and snippets.

@JRHeaton
Last active December 15, 2015 08:09
Show Gist options
  • Save JRHeaton/5228935 to your computer and use it in GitHub Desktop.
Save JRHeaton/5228935 to your computer and use it in GitHub Desktop.
10 minute hack to get the launchpad to give me sexy feedback
//
// main.c
// lpfeedback
//
// Created by John Heaton on 3/23/13.
// Copyright (c) 2013 John Heaton. All rights reserved.
//
#include <CoreFoundation/CoreFoundation.h>
#include <CoreMIDI/CoreMIDI.h>
#define BG_COLOR_VELOCITY 13 // dark red
#define NOTE_COLOR_VELOCITY 127 // bright amber
// ************ NOTE **************
//
// this doesn't play with with DAW's yet because it doesn't properly check
// the message type incoming from the LP
//
// therefore it's impossible to switch modes. easy fix though if you want to do it
// ********************************
static struct {
MIDIClientRef client;
MIDIDeviceRef launchpad;
MIDIPortRef input_port, output_port;
MIDIEndpointRef dest, source;
} midi_ctx;
void lp_send_msg(unsigned char msg[3]) {
unsigned char buf[1024];
bzero(buf, 1024);
MIDIPacketList *list = (MIDIPacketList *)buf;
MIDIPacket *p = MIDIPacketListInit(list);
p = MIDIPacketListAdd(list, 1024, p, 0, 3, msg);
printf("x\n");
MIDISend(midi_ctx.output_port, midi_ctx.dest, list);
}
void lp_input_msg(const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon) {
printf("msg\n");
// @textblock
// MIDIPacket *packet = &packetList->packet[0];
// for (int i = 0; i < packetList->numPackets; ++i) {
// ...
// packet = MIDIPacketNext(packet);
// }
MIDIPacket *p = &pktlist->packet[0];
for(int i=0;i<pktlist->numPackets;++i) {
unsigned char msg[3];
msg[1] = p->data[1];
msg[2] = (p->data[0] == 0x90 && p->data[2] > 0 ? 127 : 13);
msg[0] = p->data[0];
lp_send_msg(msg);
p = MIDIPacketNext(p);
}
}
int is_launchpad(MIDIDeviceRef device) {
CFStringRef name;
MIDIObjectGetStringProperty(device, kMIDIPropertyName, &name);
if(CFStringCompare(name, CFSTR("Launchpad"), kCFCompareCaseInsensitive) != 0)
return 0;
return 1;
}
int launchpad_init() {
int c = MIDIGetNumberOfDevices();
int ret_found = 0;
for(int i=0;i<c;++i) {
MIDIDeviceRef d = MIDIGetDevice(i);
if(!is_launchpad(d))
continue;
else
ret_found = 1;
MIDIEntityRef e = MIDIDeviceGetEntity(d, 0);
midi_ctx.source = MIDIEntityGetSource(e, 0);
midi_ctx.dest = MIDIEntityGetDestination(e, 0);
MIDIClientCreate(CFSTR("lpfeedback"), NULL/*change this*/, NULL, &midi_ctx.client);
MIDIInputPortCreate(midi_ctx.client, CFSTR("lpfeedback-in"), lp_input_msg, NULL, &midi_ctx.input_port);
MIDIOutputPortCreate(midi_ctx.client, CFSTR("lpfeedback-out"), &midi_ctx.output_port);
MIDIPortConnectSource(midi_ctx.input_port, midi_ctx.source, NULL);
}
return ret_found;
}
void set_lp_grid_color(int velocity) {
for(int i=35;i<108;++i) {
unsigned char msg[3] = { 0x90, i, velocity };
lp_send_msg(msg);
}
}
int main(int argc, const char * argv[])
{
if(!launchpad_init()) {
fprintf(stderr, "fuck\n");
return 1;
}
printf("got lp\n");
set_lp_grid_color(13);
CFRunLoopRun();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment