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