diff options
| author | Gary Scavone <gary@music.mcgill.ca> | 2013-10-11 14:49:11 +0200 |
|---|---|---|
| committer | Stephen Sinclair <sinclair@music.mcgill.ca> | 2013-10-11 14:49:11 +0200 |
| commit | 8fec3d71d441efbada609d2139ddf8ff6aa7b914 (patch) | |
| tree | 760e2e74be6be3f4c81bc35b9e8a5d932e0338d8 /tests/cmidiin.cpp | |
| parent | 94ef7a9c9202314c5fe9de2dc24b16ded6b7d98c (diff) | |
Version 1.0.12
Diffstat (limited to 'tests/cmidiin.cpp')
| -rw-r--r-- | tests/cmidiin.cpp | 93 |
1 files changed, 61 insertions, 32 deletions
diff --git a/tests/cmidiin.cpp b/tests/cmidiin.cpp index 0556b80..f2f392b 100644 --- a/tests/cmidiin.cpp +++ b/tests/cmidiin.cpp @@ -28,6 +28,11 @@ void mycallback( double deltatime, std::vector< unsigned char > *message, void * std::cout << "stamp = " << deltatime << std::endl; } +// This function should be embedded in a try/catch block in case of +// an exception. It offers the user a choice of MIDI ports to open. +// It returns false if there are no ports available. +bool chooseMidiPort( RtMidiIn *rtmidi ); + int main( int argc, char *argv[] ) { RtMidiIn *midiin = 0; @@ -35,48 +40,72 @@ int main( int argc, char *argv[] ) // Minimal command-line check. if ( argc > 2 ) usage(); - // RtMidiIn constructor try { + + // RtMidiIn constructor midiin = new RtMidiIn(); - } - catch ( RtError &error ) { - error.printMessage(); - exit( EXIT_FAILURE ); - } - // Check available ports vs. specified. - unsigned int port = 0; - unsigned int nPorts = midiin->getPortCount(); - if ( argc == 2 ) port = (unsigned int) atoi( argv[1] ); - if ( port >= nPorts ) { - delete midiin; - std::cout << "Invalid port specifier!\n"; - usage(); - } + // Call function to select port. + if ( chooseMidiPort( midiin ) == false ) goto cleanup; - try { - midiin->openPort( port ); - } - catch ( RtError &error ) { - error.printMessage(); - goto cleanup; - } + // Set our callback function. This should be done immediately after + // opening the port to avoid having incoming messages written to the + // queue instead of sent to the callback function. + midiin->setCallback( &mycallback ); - // Set our callback function. This should be done immediately after - // opening the port to avoid having incoming messages written to the - // queue instead of sent to the callback function. - midiin->setCallback( &mycallback ); + // Don't ignore sysex, timing, or active sensing messages. + midiin->ignoreTypes( false, false, false ); - // Don't ignore sysex, timing, or active sensing messages. - midiin->ignoreTypes( false, false, false ); + std::cout << "\nReading MIDI input ... press <enter> to quit.\n"; + char input; + std::cin.get(input); - std::cout << "\nReading MIDI input ... press <enter> to quit.\n"; - char input; - std::cin.get(input); + } catch ( RtError &error ) { + error.printMessage(); + } - // Clean up cleanup: + delete midiin; return 0; } + +bool chooseMidiPort( RtMidiIn *rtmidi ) +{ + std::cout << "\nWould you like to open a virtual input port? [y/N] "; + + std::string keyHit; + std::getline( std::cin, keyHit ); + if ( keyHit == "y" ) { + rtmidi->openVirtualPort(); + return true; + } + + std::string portName; + unsigned int i = 0, nPorts = rtmidi->getPortCount(); + if ( nPorts == 0 ) { + std::cout << "No input ports available!" << std::endl; + return false; + } + + if ( nPorts == 1 ) { + std::cout << "\nOpening " << rtmidi->getPortName() << std::endl; + } + else { + for ( i=0; i<nPorts; i++ ) { + portName = rtmidi->getPortName(i); + std::cout << " Input port #" << i << ": " << portName << '\n'; + } + + do { + std::cout << "\nChoose a port number: "; + std::cin >> i; + } while ( i >= nPorts ); + } + + std::getline( std::cin, keyHit ); // used to clear out stdin + rtmidi->openPort( i ); + + return true; +} |
