andrew@28: /* andrew@28: * drawMidiNotes.cpp andrew@28: * midiCannamReader andrew@28: * andrew@28: * Created by Andrew on 17/07/2011. andrew@28: * Copyright 2011 QMUL. All rights reserved. andrew@28: * andrew@28: */ andrew@28: andrew@28: #include "drawMidiNotes.h" andrew@28: andrew@28: drawMidiNotes::drawMidiNotes(){ andrew@28: andrew@28: andrew@28: ticksPerScreen = 8000; andrew@28: tickLocation = 0; andrew@28: pulsesPerQuarternote = 240; andrew@28: noteArrayIndex = 0; andrew@28: noteMinimum = 30; andrew@28: noteMaximum = 96; andrew@28: screenWidth = ofGetWidth(); andrew@28: screenHeight = ofGetHeight(); andrew@28: noteHeight = screenHeight / (float)(noteMaximum - noteMinimum); andrew@28: } andrew@28: andrew@28: void drawMidiNotes::reset(){ andrew@28: noteArrayIndex = 0; andrew@28: tickLocation = 0; andrew@28: lastPeriodUpdateTime = ofGetElapsedTimeMillis(); andrew@28: andrew@28: } andrew@28: andrew@28: void drawMidiNotes::updatePlayPosition(){ andrew@28: double timeDifference = ofGetElapsedTimeMillis() - lastPeriodUpdateTime; andrew@28: //this is time diff in milliseconds andrew@28: //then we have andrew@28: double quarterNoteIntervals = (timeDifference / period); andrew@28: tickLocation = quarterNoteIntervals * pulsesPerQuarternote; andrew@28: andrew@28: } andrew@28: andrew@28: void drawMidiNotes::drawFile(const IntMatrix& noteOnMatrix){ andrew@28: int size = noteOnMatrix.size(); andrew@28: if (size > 0){ andrew@28: andrew@28: int numberOfScreensIn = tickLocation / ticksPerScreen; andrew@28: andrew@28: while (noteArrayIndex < noteOnMatrix.size() && tickLocation > noteOnMatrix[noteArrayIndex][0] ) andrew@28: noteArrayIndex++; andrew@28: andrew@28: while (noteArrayIndex > 0 && noteArrayIndex < size && tickLocation < noteOnMatrix[noteArrayIndex][0]) andrew@28: noteArrayIndex--; andrew@28: andrew@28: //need to start where we currently are in file andrew@28: int maxNoteIndexToPrint = noteArrayIndex; andrew@28: int minNoteIndexToPrint = noteArrayIndex; andrew@28: andrew@28: andrew@28: while (maxNoteIndexToPrint < noteOnMatrix.size() && noteOnMatrix[maxNoteIndexToPrint][0] < (numberOfScreensIn+1)*ticksPerScreen ) andrew@28: maxNoteIndexToPrint++; andrew@28: andrew@28: while (minNoteIndexToPrint > 0 && minNoteIndexToPrint < size && noteOnMatrix[minNoteIndexToPrint][0] > numberOfScreensIn*ticksPerScreen) andrew@28: minNoteIndexToPrint--; andrew@28: andrew@28: andrew@28: for (int tmpIndex = minNoteIndexToPrint;tmpIndex < maxNoteIndexToPrint;tmpIndex++){ andrew@28: int xLocation = (float)(noteOnMatrix[tmpIndex][0] - numberOfScreensIn*ticksPerScreen)*screenWidth/(float)ticksPerScreen; andrew@28: int duration = (float)(noteOnMatrix[tmpIndex][3]*screenWidth)/(float)ticksPerScreen; andrew@28: andrew@28: andrew@28: int yLocation = screenHeight - ((noteOnMatrix[tmpIndex][1] - noteMinimum )*screenHeight/ (float)(noteMaximum - noteMinimum)); andrew@28: ofRect(xLocation,yLocation, duration, noteHeight); andrew@28: andrew@28: } andrew@28: andrew@28: int xLocation = (float)(tickLocation - numberOfScreensIn*ticksPerScreen)*screenWidth/(float)ticksPerScreen; andrew@28: ofLine(xLocation, 0, xLocation, screenHeight); andrew@28: andrew@28: // if (noteArrayIndex < size ) andrew@28: // printf("tick %i :: note array :%i: %i\n", tickLocation, noteArrayIndex, noteOnMatrix[noteArrayIndex][0]); andrew@28: // else andrew@28: // printf("end of file\n"); andrew@28: andrew@28: andrew@28: } andrew@28: andrew@28: }