view hackday/MidiInputStream.cpp @ 25:2a025ea7c793

hackday work to get live midi input, follow the notes, output measure, read measure in with midi file
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Sat, 03 Dec 2011 21:09:13 +0000
parents 5a11b19906c7
children 179365726f07
line wrap: on
line source
/*
 *  MidiInputStream.cpp
 *  MuseScoreMidiFollower
 *
 *  Created by Andrew on 03/12/2011.
 *  Copyright 2011 QMUL. All rights reserved.
 *
 */

#include "MidiInputStream.h"


MidiInputStream::MidiInputStream(){
	reset();
}


void MidiInputStream::reset(){
	midiInputEvents.clear();
	eventTimesForNote.clear();
	eventTimesForNote.assign (127, 0.0);
	midiInputTimes.clear();
}

bool MidiInputStream::noteInReceived(ofxMidiEventArgs &args){
//	printf("midi input received port %i, channel %i status %i, byteOne %i, byteTwo %i\n", args.port,args.channel, args.status, newPitch, args.byteTwo);
	bool noteOn = false;
	int newPitch;
	switch (args.status) {
		
		case 144:
			newPitch = (int)args.byteOne;
			newPitch += (*transposeVal);
			
	//		printf("note on %i", args.byteOne);
			if (args.byteTwo){
			//	printf("volume is %i\n", args.byteTwo);
				double time = ofGetElapsedTimeMillis();
				eventTimesForNote[newPitch] = time;
				
				noteOn = true;
				
			}else{
				double time = ofGetElapsedTimeMillis();
				time -= eventTimesForNote[newPitch];
			//	printf(", OFF after %f millis %f ticks\n", time, time * (*factor));
				
				int index = endNote(newPitch);
				//correct the length of note time
				if (midiInputEvents[index].size() > 2)
				midiInputEvents[index][3] = (time * (*factor));
			}
			
			break;
		default:
			break;
	}
	
	return noteOn;
	//cout << "MIDI message [port: " << args.port << ", channel: " << args.channel << ", status: " << args.status << ", byteOne: " << newPitch << ", byteTwo: " << args.byteTwo << ", timestamp: " << args.timestamp << "]" << endl;
}


int MidiInputStream::endNote(int notePitch){
	int index = midiInputEvents.size()-1;
	while (index > 0 && midiInputEvents[index][1] != notePitch){
		index--;
	}
//	printf("found index %i\n", index);
	return index;
}

void MidiInputStream::printNotes(){
	printf("live input \n");
	for (int i = 0;i < midiInputEvents.size();i++){
		printf("ticktime %i :: pitch %i @ millis %f\n",  midiInputEvents[i][0],  midiInputEvents[i][1],  midiInputTimes[i]);
	}
}