Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   Tutorial


midiin.cpp

00001 // midiin.cpp
00002 //
00003 // Simple program to read MIDI input from default port.
00004 //
00005 // by Gary Scavone, 2003.
00006 //
00007 // compile: g++ -Wall -D__MACOSX_CORE__ -o midiin midiin.cpp RtMidi.cpp Stk.cpp -framework CoreMIDI -framework CoreFoundation -framework CoreAudio
00008 
00009 #include <iostream>
00010 #include <unistd.h>
00011 #include <signal.h>
00012 #include "RtMidi.h"
00013 
00014 bool done;
00015 static void finish(int ignore){ done = true; }
00016 
00017 int main()
00018 {
00019   using namespace std;
00020   RtMidiIn midi;
00021   std::vector<unsigned char> message;
00022 
00023   if ( midi.getPortCount() < 1 ) {
00024     cout << "\nThere are no MIDI input sources available!\n";
00025     exit(0);
00026   }
00027 
00028   try {
00029     midi.openPort();
00030   }
00031   catch ( RtError & ) {
00032     // You might want to do something more useful here.
00033     cout << "\nAborting program!\n";
00034     goto cleanup;
00035   }
00036 
00037   // Install an interrupt handler function.
00038   (void) signal(SIGINT, finish);
00039 
00040   int nBytes, i, value;
00041   double stamp;
00042   done = false;
00043   cout << "Reading MIDI from default port ..." << endl;
00044   while ( !done ) {
00045     stamp = midi.getMessage( &message );
00046     nBytes = message.size();
00047     for ( i=0; i<nBytes; i++ ) {
00048       value = message[i];
00049       cout << "Byte " << i << " = " << value << ", ";
00050     }
00051     if ( nBytes > 0 )
00052       cout << "stamp = " << stamp << '\n';
00053 
00054     // Sleep for 10 milliseconds.
00055     usleep( 10000 );
00056   }
00057 
00058   cout << "Goodbye!" << endl;
00059 
00060  cleanup:
00061   midi.closePort();
00062   return 0;
00063 }

The Synthesis ToolKit in C++ (STK)
©1995-2002 Perry R. Cook and Gary P. Scavone. All Rights Reserved.