1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/***************************************************/
/*
Simple realtime MIDI to SKINI parser.
This object takes MIDI from the input stream
(via the RtMidi class), parses it, and turns it
into SKINI messages.
by Perry R. Cook and Gary P. Scavone, 1995 - 2004.
*/
/***************************************************/
#include "RtMidi.h"
#include "SKINImsg.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
void usage(void) {
std::cout << "\nuseage: Md2Skini <flag(s)>\n\n";
std::cout << " With no arguments, Md2Skini converts MIDI input to SKINI\n";
std::cout << " format and sends the output directly to stdout.\n";
std::cout << " With flag = -f <filename>, the output stream is simultaneously\n";
std::cout << " written to the file specified by the optional <filename>\n";
std::cout << " (default = test.ski).\n";
std::cout << " With flag = -c, MIDI control change messages will not be\n";
std::cout << " converted to SKINI-specific named controls.\n";
std::cout << " A MIDI input port can be specified with flag = -p portNumber.\n" << std::endl;
exit(0);
}
#include <signal.h>
static void finish( int ignore ){ std::cout << "Type 'Exit' to quit." << std::endl; }
bool parseSkiniControl = true;
void midiCallback( double deltatime, std::vector< unsigned char > *bytes, void *userData )
{
if ( bytes->size() < 2 ) return;
// Parse the MIDI bytes ... only keep MIDI channel messages.
if ( bytes->at(0) > 239 ) return;
register long type = bytes->at(0) & 0xF0;
register int channel = bytes->at(0) & 0x0F;
register long databyte1 = bytes->at(1);
register long databyte2 = 0;
if ( ( type != 0xC0 ) && ( type != 0xD0 ) ) {
if ( bytes->size() < 3 ) return;
databyte2 = bytes->at(2);
}
std::string typeName;
switch( type ) {
case __SK_NoteOn_:
if ( databyte2 == 0 ) {
typeName = "NoteOff\t\t";
databyte2 = 64;
}
else typeName = "NoteOn\t\t";
break;
case __SK_NoteOff_:
typeName = "NoteOff\t\t";
break;
case __SK_PolyPressure_:
typeName = "PolyPressure\t";
break;
case __SK_ProgramChange_:
typeName = "ProgramChange\t";
break;
case __SK_ChannelPressure_:
typeName = "ChannelPressure\t";
break;
case __SK_PitchBend_:
typeName = "PitchBend\t";
break;
case __SK_ControlChange_:
if ( parseSkiniControl != true ) {
typeName = "ControlChange\t";
goto output;
}
switch( databyte1 ) {
case __SK_PitchChange_:
typeName = "PitchChange\t";
goto output;
case __SK_Volume_:
typeName = "Volume\t";
goto output;
case __SK_ModWheel_:
typeName = "ModWheel\t";
goto output;
case __SK_Breath_:
typeName = "Breath\t\t";
goto output;
case __SK_FootControl_:
typeName = "FootControl\t";
goto output;
case __SK_Portamento_:
typeName = "Portamento\t";
goto output;
case __SK_Balance_:
typeName = "Balance\t";
goto output;
case __SK_Pan_:
typeName = "Pan\t\t";
goto output;
case __SK_Sustain_:
typeName = "Sustain\t";
goto output;
case __SK_Expression_:
typeName = "Expression\t";
goto output;
default:
typeName = "ControlChange\t";
goto output;
}
default:
typeName = "Unknown\t";
}
output:
FILE *file = (FILE *) userData;
if ( type == 0xC0 || type == 0xD0 || type == 0xE0 ) { // program change, channel pressure, or pitchbend
fprintf( stdout, "%s %.3f %d %.1f\n", typeName.c_str(), 0.0, channel, (float)databyte1 );
if ( file != NULL )
fprintf( file, "%s %.3f %d %.1f\n", typeName.c_str(), deltatime, channel, (float)databyte1 );
}
else if ( type == 0xB0 ) { // control change
if ( typeName == "ControlChange\t" ) {
fprintf( stdout, "%s %.3f %d %.1f %.1f\n", typeName.c_str(), 0.0, channel, (float)databyte1, (float)databyte2 );
if ( file != NULL )
fprintf( file, "%s %.3f %d %.1f %.1f\n", typeName.c_str(), deltatime, channel, (float)databyte1, (float)databyte2 );
}
else {
fprintf( stdout, "%s %.3f %d %.1f\n", typeName.c_str(), 0.0, channel, (float)databyte2 );
if ( file != NULL )
fprintf( file, "%s %.3f %d %.1f\n", typeName.c_str(), deltatime, channel, (float)databyte2 );
}
}
else { // noteon, noteoff, aftertouch, and unknown
fprintf( stdout, "%s %.3f %d %.1f %.1f\n", typeName.c_str(), 0.0, channel, (float)databyte1, (float)databyte2 );
if ( file != NULL )
fprintf( file, "%s %.3f %d %.1f %.1f\n", typeName.c_str(), deltatime, channel, (float)databyte1, (float)databyte2 );
}
fflush( stdout );
}
int main( int argc,char *argv[] )
{
FILE *file = NULL;
std::string fileName;
RtMidiIn *midiin = 0;
unsigned int port = 0;
std::string input;
if ( argc > 5 ) usage();
// Parse the command-line arguments.
int i = 1;
while ( i < argc ) {
if (argv[i][0] == '-') {
switch(argv[i][1]) {
case 'f':
if ( (i+1 < argc) && argv[i+1][0] != '-' ) {
i++;
fileName = argv[i];
if ( fileName.find( ".ski" ) == std::string::npos ) fileName.append( ".ski" );
}
else fileName = "test.ski";
file = fopen( fileName.c_str(), "wb" );
break;
case 'p':
if ( i++ >= argc) usage();
port = (unsigned int) atoi( argv[i] );
break;
case 'c':
parseSkiniControl = false;
break;
default:
usage();
break;
}
}
else usage();
i++;
}
try {
midiin = new RtMidiIn();
}
catch (RtMidiError &error) {
error.printMessage();
if ( file != NULL ) fclose( file );
exit(EXIT_FAILURE);
}
// Check available ports vs. specified.
unsigned int nPorts = midiin->getPortCount();
if ( nPorts == 0 ) {
std::cout << "No MIDI ports available!\n";
goto cleanup;
}
else if ( port >= nPorts ) {
std::cout << "Invalid port specifier!\n";
goto cleanup;
}
// Open the port.
try {
midiin->openPort( port );
}
catch (RtMidiError &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( &midiCallback, file );
// We'll ignore sysex, timing, and active sensing messages.
midiin->ignoreTypes( true, true, true );
// Install an interrupt handler function.
(void) signal(SIGINT, finish);
std::cout << "\nReading MIDI input ... type 'Exit' to quit.\n";
while ( input != "Exit" && input != "exit" ) {
input.erase();
std::cin >> input;
std::cout << input << std::endl;
}
cleanup:
delete midiin;
if ( file != NULL ) fclose( file );
std::cout << "Md2Skini finished ... bye!" << std::endl;
return 0;
}
|