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