furnace/extern/rtmidi/tests/midiout.cpp

147 lines
3.1 KiB
C++
Raw Normal View History

2022-02-13 20:02:43 +00:00
//*****************************************//
// midiout.cpp
// by Gary Scavone, 2003-2004.
//
// Simple program to test MIDI output.
//
//*****************************************//
#include <iostream>
#include <cstdlib>
#include "RtMidi.h"
// Platform-dependent sleep routines.
#if defined(WIN32)
#include <windows.h>
#define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
#else // Unix variants
#include <unistd.h>
#define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
#endif
// 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( RtMidiOut *rtmidi );
int main( void )
{
RtMidiOut *midiout = 0;
std::vector<unsigned char> message;
// RtMidiOut constructor
try {
midiout = new RtMidiOut();
}
catch ( RtMidiError &error ) {
error.printMessage();
exit( EXIT_FAILURE );
}
// Call function to select port.
try {
if ( chooseMidiPort( midiout ) == false ) goto cleanup;
}
catch ( RtMidiError &error ) {
error.printMessage();
goto cleanup;
}
// Send out a series of MIDI messages.
// Program change: 192, 5
message.push_back( 192 );
message.push_back( 5 );
midiout->sendMessage( &message );
SLEEP( 500 );
message[0] = 0xF1;
message[1] = 60;
midiout->sendMessage( &message );
// Control Change: 176, 7, 100 (volume)
message[0] = 176;
message[1] = 7;
message.push_back( 100 );
midiout->sendMessage( &message );
// Note On: 144, 64, 90
message[0] = 144;
message[1] = 64;
message[2] = 90;
midiout->sendMessage( &message );
SLEEP( 500 );
// Note Off: 128, 64, 40
message[0] = 128;
message[1] = 64;
message[2] = 40;
midiout->sendMessage( &message );
SLEEP( 500 );
// Control Change: 176, 7, 40
message[0] = 176;
message[1] = 7;
message[2] = 40;
midiout->sendMessage( &message );
SLEEP( 500 );
// Sysex: 240, 67, 4, 3, 2, 247
message[0] = 240;
message[1] = 67;
message[2] = 4;
message.push_back( 3 );
message.push_back( 2 );
message.push_back( 247 );
midiout->sendMessage( &message );
// Clean up
cleanup:
delete midiout;
return 0;
}
bool chooseMidiPort( RtMidiOut *rtmidi )
{
std::cout << "\nWould you like to open a virtual output 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 output 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 << " Output port #" << i << ": " << portName << '\n';
}
do {
std::cout << "\nChoose a port number: ";
std::cin >> i;
} while ( i >= nPorts );
}
std::cout << "\n";
rtmidi->openPort( i );
return true;
}