Debian Codegraph @

android/app/jni/extern/rtmidi/tests/cmidiin.cpp

raw
1 //*****************************************//
2 // cmidiin.cpp
3 // by Gary Scavone, 2003-2004.
4 //
5 // Simple program to test MIDI input and
6 // use of a user callback function.
7 //
8 //*****************************************//
9
10 #include <iostream>
11 #include <cstdlib>
12 #include "RtMidi.h"
13
14 void usage( void ) {
15 // Error function in case of incorrect command-line
16 // argument specifications.
17 std::cout << "\nuseage: cmidiin <port>\n";
18 std::cout << " where port = the device to use (first / default = 0).\n\n";
19 exit( 0 );
20 }
21
22 void mycallback( double deltatime, std::vector< unsigned char > *message, void */*userData*/ )
23 {
24 unsigned int nBytes = message->size();
25 for ( unsigned int i=0; i<nBytes; i++ )
26 std::cout << "Byte " << i << " = " << (int)message->at(i) << ", ";
27 if ( nBytes > 0 )
28 std::cout << "stamp = " << deltatime << std::endl;
29 }
30
31 // This function should be embedded in a try/catch block in case of
32 // an exception. It offers the user a choice of MIDI ports to open.
33 // It returns false if there are no ports available.
34 bool chooseMidiPort( RtMidiIn *rtmidi );
35
36 int main( int argc, char ** /*argv[]*/ )
37 {
38 RtMidiIn *midiin = 0;
39
40 // Minimal command-line check.
41 if ( argc > 2 ) usage();
42
43 try {
44
45 // RtMidiIn constructor
46 midiin = new RtMidiIn();
47
48 // Call function to select port.
49 if ( chooseMidiPort( midiin ) == false ) goto cleanup;
50
51 // Set our callback function. This should be done immediately after
52 // opening the port to avoid having incoming messages written to the
53 // queue instead of sent to the callback function.
54 midiin->setCallback( &mycallback );
55
56 // Don't ignore sysex, timing, or active sensing messages.
57 midiin->ignoreTypes( false, false, false );
58
59 std::cout << "\nReading MIDI input ... press <enter> to quit.\n";
60 char input;
61 std::cin.get(input);
62
63 } catch ( RtMidiError &error ) {
64 error.printMessage();
65 }
66
67 cleanup:
68
69 delete midiin;
70
71 return 0;
72 }
73
74 bool chooseMidiPort( RtMidiIn *rtmidi )
75 {
76 std::cout << "\nWould you like to open a virtual input port? [y/N] ";
77
78 std::string keyHit;
79 std::getline( std::cin, keyHit );
80 if ( keyHit == "y" ) {
81 rtmidi->openVirtualPort();
82 return true;
83 }
84
85 std::string portName;
86 unsigned int i = 0, nPorts = rtmidi->getPortCount();
87 if ( nPorts == 0 ) {
88 std::cout << "No input ports available!" << std::endl;
89 return false;
90 }
91
92 if ( nPorts == 1 ) {
93 std::cout << "\nOpening " << rtmidi->getPortName() << std::endl;
94 }
95 else {
96 for ( i=0; i<nPorts; i++ ) {
97 portName = rtmidi->getPortName(i);
98 std::cout << " Input port #" << i << ": " << portName << '\n';
99 }
100
101 do {
102 std::cout << "\nChoose a port number: ";
103 std::cin >> i;
104 } while ( i >= nPorts );
105 std::getline( std::cin, keyHit ); // used to clear out stdin
106 }
107
108 rtmidi->openPort( i );
109
110 return true;
111 }