Debian Codegraph @

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

raw
1 //*****************************************//
2 // midiclock.cpp
3 //
4 // Simple program to test MIDI clock sync. Run midiclock_in in one
5 // console and midiclock_out in the other, make sure to choose
6 // options that connect the clocks between programs on your platform.
7 //
8 // (C)2016 Refer to README.md in this archive for copyright.
9 //
10 //*****************************************//
11
12 #include <iostream>
13 #include <cstdlib>
14 #include "RtMidi.h"
15
16 // Platform-dependent sleep routines.
17 #if defined(WIN32)
18 #include <windows.h>
19 #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
20 #else // Unix variants
21 #include <unistd.h>
22 #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
23 #endif
24
25 // These functions should be embedded in a try/catch block in case of
26 // an exception. It offers the user a choice of MIDI ports to open.
27 // It returns false if there are no ports available.
28 bool chooseInputPort( RtMidiIn *rtmidi );
29 bool chooseOutputPort( RtMidiOut *rtmidi );
30
31 void mycallback( double deltatime, std::vector< unsigned char > *message, void *user )
32 {
33 unsigned int *clock_count = reinterpret_cast<unsigned int*>(user);
34
35 // Ignore longer messages
36 if (message->size() != 1)
37 return;
38
39 unsigned int msg = message->at(0);
40 if (msg == 0xFA)
41 std::cout << "START received" << std::endl;
42 if (msg == 0xFB)
43 std::cout << "CONTINUE received" << std::endl;
44 if (msg == 0xFC)
45 std::cout << "STOP received" << std::endl;
46 if (msg == 0xF8) {
47 if (++*clock_count == 24) {
48 double bpm = 60.0 / 24.0 / deltatime;
49 std::cout << "One beat, estimated BPM = " << bpm <<std::endl;
50 *clock_count = 0;
51 }
52 }
53 else
54 *clock_count = 0;
55 }
56
57 int clock_in()
58 {
59 RtMidiIn *midiin = 0;
60 unsigned int clock_count = 0;
61
62 try {
63
64 // RtMidiIn constructor
65 midiin = new RtMidiIn();
66
67 // Call function to select port.
68 if ( chooseInputPort( midiin ) == false ) goto cleanup;
69
70 // Set our callback function. This should be done immediately after
71 // opening the port to avoid having incoming messages written to the
72 // queue instead of sent to the callback function.
73 midiin->setCallback( &mycallback, &clock_count );
74
75 // Don't ignore sysex, timing, or active sensing messages.
76 midiin->ignoreTypes( false, false, false );
77
78 std::cout << "\nReading MIDI input ... press <enter> to quit.\n";
79 char input;
80 std::cin.get(input);
81
82 } catch ( RtMidiError &error ) {
83 error.printMessage();
84 }
85
86 cleanup:
87
88 delete midiin;
89
90 return 0;
91 }
92
93 int clock_out()
94 {
95 RtMidiOut *midiout = 0;
96 std::vector<unsigned char> message;
97 int sleep_ms = 0, k = 0, j = 0;
98
99 // RtMidiOut constructor
100 try {
101 midiout = new RtMidiOut();
102 }
103 catch ( RtMidiError &error ) {
104 error.printMessage();
105 exit( EXIT_FAILURE );
106 }
107
108 // Call function to select port.
109 try {
110 if ( chooseOutputPort( midiout ) == false ) goto cleanup;
111 }
112 catch ( RtMidiError &error ) {
113 error.printMessage();
114 goto cleanup;
115 }
116
117 // Period in ms = 100 BPM
118 // 100*24 ticks / 1 minute, so (60*1000) / (100*24) = 25 ms / tick
119 sleep_ms = 25;
120 std::cout << "Generating clock at "
121 << (60.0 / 24.0 / sleep_ms * 1000.0)
122 << " BPM." << std::endl;
123
124 // Send out a series of MIDI clock messages.
125 // MIDI start
126 message.clear();
127 message.push_back( 0xFA );
128 midiout->sendMessage( &message );
129 std::cout << "MIDI start" << std::endl;
130
131 for (j=0; j < 8; j++)
132 {
133 if (j > 0)
134 {
135 // MIDI continue
136 message.clear();
137 message.push_back( 0xFB );
138 midiout->sendMessage( &message );
139 std::cout << "MIDI continue" << std::endl;
140 }
141
142 for (k=0; k < 96; k++) {
143 // MIDI clock
144 message.clear();
145 message.push_back( 0xF8 );
146 midiout->sendMessage( &message );
147 if (k % 24 == 0)
148 std::cout << "MIDI clock (one beat)" << std::endl;
149 SLEEP( sleep_ms );
150 }
151
152 // MIDI stop
153 message.clear();
154 message.push_back( 0xFC );
155 midiout->sendMessage( &message );
156 std::cout << "MIDI stop" << std::endl;
157 SLEEP( 500 );
158 }
159
160 // MIDI stop
161 message.clear();
162 message.push_back( 0xFC );
163 midiout->sendMessage( &message );
164 std::cout << "MIDI stop" << std::endl;
165
166 SLEEP( 500 );
167
168 std::cout << "Done!" << std::endl;
169
170 // Clean up
171 cleanup:
172 delete midiout;
173
174 return 0;
175 }
176
177 int main( int, const char *argv[] )
178 {
179 std::string prog(argv[0]);
180 if (prog.find("midiclock_in") != prog.npos) {
181 clock_in();
182 }
183 else if (prog.find("midiclock_out") != prog.npos) {
184 clock_out();
185 }
186 else {
187 std::cout << "Don't know what to do as " << prog << std::endl;
188 }
189 return 0;
190 }
191
192 template<typename RT>
193 bool choosePort( RT *rtmidi, const char *dir )
194 {
195 std::string portName;
196 unsigned int i = 0, nPorts = rtmidi->getPortCount();
197 if ( nPorts == 0 ) {
198 std::cout << "No " << dir << " ports available!" << std::endl;
199 return false;
200 }
201
202 if ( nPorts == 1 ) {
203 std::cout << "\nOpening " << rtmidi->getPortName() << std::endl;
204 }
205 else {
206 for ( i=0; i<nPorts; i++ ) {
207 portName = rtmidi->getPortName(i);
208 std::cout << " " << dir << " port #" << i << ": " << portName << '\n';
209 }
210
211 do {
212 std::cout << "\nChoose a port number: ";
213 std::cin >> i;
214 } while ( i >= nPorts );
215 }
216
217 std::cout << "\n";
218 rtmidi->openPort( i );
219
220 return true;
221 }
222
223 bool chooseInputPort( RtMidiIn *rtmidi )
224 {
225 return choosePort<RtMidiIn>( rtmidi, "input" );
226 }
227
228 bool chooseOutputPort( RtMidiOut *rtmidi )
229 {
230 return choosePort<RtMidiOut>( rtmidi, "output" );
231 }