Created
July 26, 2009 06:16
-
-
Save willb/155446 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
midtest.m | |
example OS X CoreMIDI code; generates an unattractive stream of random notes | |
compile me with | |
gcc -framework Foundation -framework CoreMIDI midtest.m -o mt | |
Then run mt and set the input MIDI track for your favorite | |
synthesizer to "random notes;" enjoy (?) random notes | |
Comments/flames to Will Benton (google me); this code is in the public domain. | |
*/ | |
#include <stdlib.h> | |
#include <pthread.h> | |
#include <Cocoa/Cocoa.h> | |
#include <Foundation/Foundation.h> | |
#include <CoreMIDI/CoreMIDI.h> | |
void send_midi_note(MIDIEndpointRef midiEndpoint, Byte channel, Byte note, Byte vel) { | |
Byte buffer[128]; | |
MIDIPacketList *pktlist = (MIDIPacketList*)buffer; | |
MIDIPacket *curPacket = MIDIPacketListInit(pktlist); | |
Byte message[] = {(channel & 0x0f) | 0x90, // take either a MIDI channel (90-9f) or a small integer | |
note, | |
vel}; | |
curPacket = MIDIPacketListAdd(pktlist, sizeof(buffer), curPacket, 0, 3, message); | |
MIDIReceived(midiEndpoint, pktlist); | |
} | |
int main(int argc, char *argv[]) { | |
MIDIClientRef midiClient; | |
MIDIEndpointRef midiEndpoint; | |
MIDIClientCreate(@"miditest", NULL, NULL, &midiClient); | |
MIDISourceCreate(midiClient, @"random notes", &midiEndpoint); | |
while(true) { | |
Byte note = random() % 127; | |
send_midi_note(midiEndpoint, 0, note, 127); // note on at velocity 127 | |
sleep(1); // don't try this in a real application! | |
send_midi_note(midiEndpoint, 0, note, 0); // note off (at velocity 0) | |
} | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment