| 1 |
//*****************************************// |
| 2 |
// qmidiin.cpp |
| 3 |
// by Gary Scavone, 2003-2004. |
| 4 |
// |
| 5 |
// Simple program to test MIDI input and |
| 6 |
// retrieval from the queue. |
| 7 |
// |
| 8 |
//*****************************************// |
| 9 |
|
| 10 |
#include <iostream> |
| 11 |
#include <cstdlib> |
| 12 |
#include <signal.h> |
| 13 |
#include "RtMidi.h" |
| 14 |
|
| 15 |
// Platform-dependent sleep routines. |
| 16 |
#if defined(WIN32) |
| 17 |
#include <windows.h> |
| 18 |
#define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds ) |
| 19 |
#else // Unix variants |
| 20 |
#include <unistd.h> |
| 21 |
#define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) ) |
| 22 |
#endif |
| 23 |
|
| 24 |
bool done; |
| 25 |
static void finish( int /*ignore*/ ){ done = true; } |
| 26 |
|
| 27 |
void usage( void ) { |
| 28 |
// Error function in case of incorrect command-line |
| 29 |
// argument specifications. |
| 30 |
std::cout << "\nusage: qmidiin <port>\n"; |
| 31 |
std::cout << " where port = the device to use (first / default = 0).\n\n"; |
| 32 |
exit( 0 ); |
| 33 |
} |
| 34 |
|
| 35 |
int main( int argc, char *argv[] ) |
| 36 |
{ |
| 37 |
RtMidiIn *midiin = 0; |
| 38 |
std::vector<unsigned char> message; |
| 39 |
int nBytes, i; |
| 40 |
double stamp; |
| 41 |
|
| 42 |
// Minimal command-line check. |
| 43 |
if ( argc > 2 ) usage(); |
| 44 |
|
| 45 |
// RtMidiIn constructor |
| 46 |
try { |
| 47 |
midiin = new RtMidiIn(); |
| 48 |
} |
| 49 |
catch ( RtMidiError &error ) { |
| 50 |
error.printMessage(); |
| 51 |
exit( EXIT_FAILURE ); |
| 52 |
} |
| 53 |
|
| 54 |
// Check available ports vs. specified. |
| 55 |
unsigned int port = 0; |
| 56 |
unsigned int nPorts = midiin->getPortCount(); |
| 57 |
if ( argc == 2 ) port = (unsigned int) atoi( argv[1] ); |
| 58 |
if ( port >= nPorts ) { |
| 59 |
delete midiin; |
| 60 |
std::cout << "Invalid port specifier!\n"; |
| 61 |
usage(); |
| 62 |
} |
| 63 |
|
| 64 |
try { |
| 65 |
midiin->openPort( port ); |
| 66 |
} |
| 67 |
catch ( RtMidiError &error ) { |
| 68 |
error.printMessage(); |
| 69 |
goto cleanup; |
| 70 |
} |
| 71 |
|
| 72 |
// Don't ignore sysex, timing, or active sensing messages. |
| 73 |
midiin->ignoreTypes( false, false, false ); |
| 74 |
|
| 75 |
// Install an interrupt handler function. |
| 76 |
done = false; |
| 77 |
(void) signal(SIGINT, finish); |
| 78 |
|
| 79 |
// Periodically check input queue. |
| 80 |
std::cout << "Reading MIDI from port " << midiin->getPortName() << " ... quit with Ctrl-C.\n"; |
| 81 |
while ( !done ) { |
| 82 |
stamp = midiin->getMessage( &message ); |
| 83 |
nBytes = message.size(); |
| 84 |
for ( i=0; i<nBytes; i++ ) |
| 85 |
std::cout << "Byte " << i << " = " << (int)message[i] << ", "; |
| 86 |
if ( nBytes > 0 ) |
| 87 |
std::cout << "stamp = " << stamp << std::endl; |
| 88 |
|
| 89 |
// Sleep for 10 milliseconds. |
| 90 |
SLEEP( 10 ); |
| 91 |
} |
| 92 |
|
| 93 |
// Clean up |
| 94 |
cleanup: |
| 95 |
delete midiin; |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |