willb (owner)

Revisions

gist: 155446 Download_button fork
public
Public Clone URL: git://gist.github.com/155446.git
Embed All Files: show embed
midtest.m #
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
/*
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);
}