Mercurial > hg > midi-score-follower
view hackday/MidiInputStream.cpp @ 36:5a1b0c6fa1fb
Added class to read in the csv Annotation file, then write out the respective difference between the performed piece as followed here, and the annotation of RWC by Ewert and Muller
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Thu, 15 Dec 2011 02:28:49 +0000 |
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]); } }