view hackday/MidiInputStream.cpp @ 52:13194a9dca77 tip

Added exporting of image and text data
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Tue, 17 Jul 2012 22:13:10 +0100
parents 179365726f07
children
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();
	midiInputTimes.clear();
	
	eventTimesForNote.clear();
	eventTimesForNote.assign (127, 0.0);

	
	totalNotesRecievedByPitch.clear();
	totalNotesRecievedByPitch.assign (127, 0);
	
}

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;
				newNoteCounted(newPitch);
				
			}else{
				double time = ofGetElapsedTimeMillis();
				time -= eventTimesForNote[newPitch];
			//	printf(", NOTE 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;
}

void  MidiInputStream::newNoteCounted(const int& pitch){
	totalNotesRecievedByPitch[pitch] += 1;
}

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;
}

double MidiInputStream::calculateTotalScore(midiEventHolder& midiEvents){
	double gameScore = 1.0;
	double totalNotes = 1.0;
	if (totalNotesRecievedByPitch.size() > 100){
		for (int i = 30;i < 90;i++){
			gameScore += abs(totalNotesRecievedByPitch[i] - midiEvents.recordedTotalNoteCounterByPitch[i]);
			totalNotes += midiEvents.recordedTotalNoteCounterByPitch[i];
		}
	}
	return 1.0 - (gameScore / totalNotes);

}

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]);
	}
}

void MidiInputStream::printTotalCount(){
	for (int i = 0;i < totalNotesRecievedByPitch.size();i++){
		printf("LIVE PLAYED TOTAL[%i] := %i\n", i, totalNotesRecievedByPitch[i]);
	}
}