Skip to content

Instantly share code, notes, and snippets.

@aalin
Created August 30, 2009 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalin/178042 to your computer and use it in GitHub Desktop.
Save aalin/178042 to your computer and use it in GitHub Desktop.
#include <CoreMIDI/MIDIServices.h>
#include <iostream>
#include <vector>
#include "midi_driver.hpp"
MidiDriver::MidiDriver()
{
CFStringRef client_name = CFStringCreateWithCString(0, "Drummer", 0);
MIDIClientCreate(client_name, 0, 0, &_client);
CFStringRef port_name = CFStringCreateWithCString(0, "Output", 0);
MIDIOutputPortCreate(_client, port_name, &_outport);
int number_of_destinations = MIDIGetNumberOfDestinations();
if(number_of_destinations < 1)
throw "No midi destinations";
_destination = MIDIGetDestination(0);
}
MidiDriver::~MidiDriver()
{
MIDIClientDispose(_client);
}
void
MidiDriver::message(std::vector<unsigned char> args)
{
std::vector<unsigned char> buffer(256, 0);
MIDIPacketList* packet_list = reinterpret_cast<MIDIPacketList*>(&buffer[0]);
for(int i = 0; i < args.size(); i++)
printf("%2x", args[i]);
std::cout << std::endl;
MIDIPacket* packet_ptr = MIDIPacketListInit(packet_list);
MIDIPacketListAdd(packet_list, 256, packet_ptr, 0, args.size(), &args[0]);
MIDISend(_outport, _destination, packet_list);
}
#ifndef MIDI_DRIVER_HPP
#define MIDI_DRIVER_HPP
class MidiDriver
{
public:
enum Command
{
ON = 0x90, // Note on
OFF = 0x80, // Note off
PA = 0xa0, // Polyphonic aftertouch
CC = 0xb0, // Control change
PC = 0xc0, // Program change
CA = 0xd0, // Channel aftertouch
PB = 0xe0, // Pitch bend
};
MidiDriver();
~MidiDriver();
void noteOn(unsigned char note, unsigned char channel, unsigned char velocity)
{
message(ON | channel, note, velocity);
}
void noteOff(unsigned char note, unsigned char channel, unsigned char velocity = 100)
{
message(OFF | channel, note, velocity);
}
void aftertouch(unsigned char note, unsigned char channel, unsigned char pressure)
{
message(PA | channel, note, pressure);
}
void controlChange(unsigned char number, unsigned char channel, unsigned char value)
{
message(CC | channel, number, value);
}
void programChange(unsigned char channel, unsigned char program)
{
message(PC | channel, program);
}
void channelAftertouch(unsigned char channel, unsigned char pressure)
{
message(CA | channel, pressure);
}
void pitchBend(unsigned char channel, unsigned char value)
{
message(PB | channel, value);
}
protected:
// driver api
void message(std::vector<unsigned char> args);
void message(unsigned char a, unsigned char b)
{
std::vector<unsigned char> v;
v.push_back(a);
v.push_back(b);
message(v);
}
void message(unsigned char a, unsigned char b, unsigned char c)
{
std::vector<unsigned char> v;
v.push_back(a);
v.push_back(b);
v.push_back(c);
message(v);
}
MIDIClientRef _client;
MIDIPortRef _outport;
MIDIEndpointRef _destination;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment