MidiCPP

MidiCPP is a C++ midi library who's aim is to make midi programming fun and easy, and to establish a cross platfrom midi library. the following were compiled with g++ foo.cpp -lmidicpp

//The following simply sends 4 notes. I'm sending to port 65, you can send to anywhere you like

#include <midistream.hpp>

#include <midifile.hpp>

#include <events.hpp>

using namespace std;

int main(){

//midistream(input port, output port)

midistream mstream(65,65);

//midi_event(type,time,channel,note,velocity)

midi_event m1(flags::note_on,0,0,50,70);

midi_event m2(flags::note_on,100,0,55,70);

midi_event m3(flags::note_on,200,0,60,70);

midi_event m4(flags::note_on,300,0,60,70);

mstream << m1 << m2 << m3 << m4;

sleep(2);

}

//The following captures 4 notes from the keyboard, and plays them on my computer. my keyboard is at port 64, and my soundcard is at port 65.

#include <midistream.hpp>

#include <midifile.hpp>

#include <events.hpp>

using namespace std;

int main(){

midistream mstream(64,65);

mstream << flags::absolute;

for(int i =0 ;i < 8 ; i++){

midi_event m;

mstream >> m;

mstream << m;

}

}

//The following records 10 notes onto a midi file called test.mid (Note that it reconds from port 64, the input port in this case)

#include <midistream.hpp>

#include <midifile.hpp>

#include <events.hpp>

int main(){

midistream mstream(64,65);

midifile mfile;

for(int i =0 ;i < 20 ; i++){

midi_event m;

mstream >> m;

mfile.insert(m);

}

mfile.write("test.mid");

}

//The following plays the midi file we just recorded:

#include <midistream.hpp>

#include <midifile.hpp>

#include <events.hpp>

int main(){

midistream mstream(64,65);

midifile mfile("test.mid");

mstream << mfile;

}

//The only fancy example: this program takes notes from port 64(for me, my keyboard), adds a random delay, adds a half step to the key and randomly changes the velocity, plays it on port 65(for me, my computer)

#include <midistream.hpp>

#include <midifile.hpp>

#include <events.hpp>

#include <stdlib.h>

int main(){

midistream mstream(64,65);

mstream << flags::absolute;

while(1){

midi_event m;

mstream >> m;

m.set_note(m.get_note()+1);

m.set_time(m.get_time()+rand()%100);

m.set_velocity((m.get_velocity()+(rand()%10))%0x7f);

mstream << m;

}

}

Enjoy!

Jacques Le normand