Mercurial > hg > midi-score-follower
view src/DynamicVector.cpp @ 5:195907bb8bb7
added purple where notes have been seen - lets you see what updates have been used. Also the chopping of midi files to the beginning was introduced recently, so when they load, you chop any white space at the beginning, then use first note to launch.
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Fri, 19 Aug 2011 16:38:30 +0100 |
parents | 4a8e6a6cd224 |
children | 75dcd1308658 |
line wrap: on
line source
/* * DynamicVector.cpp * midiCannamReader * * Created by Andrew on 18/07/2011. * Copyright 2011 QMUL. All rights reserved. * */ #include "DynamicVector.h" DynamicVector::DynamicVector(){ length = 0; arraySize = 0; maximumValue = 0; MAPestimate = 0; offset = 0; scalar = 1; gaussianLookupMean = (double) GAUSSIAN_LOOKUP_LENGTH/2; gaussianLookupStdDev = (double)(GAUSSIAN_LOOKUP_LENGTH/16); double factor = 1.0;//(1.0 / (gaussianLookupStdDev*sqrt(2*PI)) );//1.0;//-1.0/(2*PI*sqrt(gaussianLookupStdDev)); for (int i = 0;i < GAUSSIAN_LOOKUP_LENGTH;i++){ gaussianLookupTable[i] = factor*exp(-1.0*(i-gaussianLookupMean)*(i-gaussianLookupMean)/(2.0*gaussianLookupStdDev*gaussianLookupStdDev)); } } void DynamicVector::copyFromDynamicVector(const DynamicVector& dynamicVec){ if (dynamicVec.length == length){ for (int i = 0;i < length;i++) array[i] = dynamicVec.array[i]; } else{ printf("CANNOT COPY VECTORS OF NON SAME LENGTH!!\n"); } } void DynamicVector::createVector(int len){ array.clear(); for (int i = 0; i < len;i++){ array.push_back(0); } length = len; arraySize = array.size(); } double DynamicVector::getMaximum(){ int i; double max = 0; for (i=0;i < length;i++){ if (array[i] > max){ max = array[i]; MAPestimate = i; } } maximumValue = max; return max; } double DynamicVector::getIntegratedEstimate(){ //returns the index of the integrated average - where the probability distribution is centred double estimate = 0; double integratedTotal = 0; for (int i = 0;i < length;i++){ estimate += array[i]*i; integratedTotal += array[i]; } if (integratedTotal > 0){ estimate /= integratedTotal; } return estimate; } void DynamicVector::zero(){ for (int i = 0;i < array.size();i++) array[i] = 0; } void DynamicVector::renormalise(){ double tmpMax = getMaximum(); if (tmpMax > 0){ // printf("renormalise : max is %f and size is %i\n", tmpMax, arraySize); for (int i = 0;i < array.size();i++) array[i] /= tmpMax; } //printArray(); } void DynamicVector::doProduct(DynamicVector& arrayOne, DynamicVector& arrayTwo){ for (int i = 0;i < arrayOne.length;i++) array[i] = arrayOne.array[i] * arrayTwo.array[i]; } void DynamicVector::printArray(){ for (int i = 0;i < arraySize;i++){ printf("[%i] = %f\n", i, array[i]); } } void DynamicVector::translateDistribution(int translationIndex){ int tmpIndex; DoubleVector tmpArray; int i; for (i=0;i < arraySize;i++){ tmpArray.push_back(array[i]); } //translate values for (i=0;i < arraySize;i++){ tmpIndex = (i + translationIndex + arraySize)%arraySize; array[tmpIndex] = tmpArray[i]; } tmpArray.clear(); //now delete tmp array } void DynamicVector::addGaussianShape(double mean, double StdDev, double factor){ int i; factor *= (1/(StdDev*sqrt(2*PI))); for (i=0;i<array.size();i++){ array[i] += factor*exp(-1*(i-mean)*(i-mean)/(2*StdDev*StdDev)); } //addGaussianShapeByLookupTable(mean, StdDev, factor); } void DynamicVector::addGaussianShapeByLookupTable(double& mean, double& StdDev, double& factor){ int i; int lookupIndex ; factor *= (1/(StdDev*sqrt(2*PI))); for (i=0;i<array.size()-1;i++){ lookupIndex = round(getLookupIndex(i, mean, StdDev)); array[i] += factor*gaussianLookupTable[lookupIndex]; } //printf("ADDED GAUSSIAN SHAPE %i\n", (int)array.size()); } double DynamicVector::getLookupIndex(const int& i, const double& mean, const double& StdDev){ double Z = ((double)i - mean)/StdDev; double lookupIndex = Z*gaussianLookupStdDev + gaussianLookupMean; if (lookupIndex < 0) lookupIndex = 0; if (lookupIndex >= GAUSSIAN_LOOKUP_LENGTH) lookupIndex = GAUSSIAN_LOOKUP_LENGTH-1; // (i - mean)*(i-mean)*(GAUSSIAN_LOOKUP_LENGTH*GAUSSIAN_LOOKUP_LENGTH/16.0)/(StdDev*StdDev); return lookupIndex; } void DynamicVector::addTriangularShape(double mean, double width, double factor){ int i; for (i= max(0., (double)(mean - width));i < min((mean+width), (double)array.size());i++){ array[i] += factor * abs(i - mean) / mean; } } void DynamicVector::addConstant(double value){ for (int i=0;i<array.size();i++){ array[i] += value; } } void DynamicVector::addToIndex(int index, double constant){ array[index] += constant; } double DynamicVector::getIndexInRealTerms(const int& index){ if (index < arraySize) return (offset + scalar*index); else return 0; } double DynamicVector::getRealTermsAsIndex(double value){ value -= offset; value /= scalar; return value; } double DynamicVector::getValueAtMillis(const double& millis){ int index = round(getRealTermsAsIndex(millis)); if (index >= 0 && index < length) return array[index]; else return 0; } void DynamicVector::drawVector(const int& minIndex, const int& maxIndex){ double stepSize = ofGetWidth() / (double)(maxIndex - minIndex); double screenHeight = ofGetHeight(); double maxVal = getMaximum(); int startInt = max(1,minIndex+1); int endInt = min(maxIndex, (int)array.size()); double heightConstant = screenHeight / maxVal; int lastHeightPixel = heightConstant * (1 - array[startInt-1]); int newHeightPixel; for (int i = startInt;i < endInt;i++){ newHeightPixel = (int) heightConstant * (1 - array[i]); ofLine (stepSize*(i-1), lastHeightPixel, stepSize*i, newHeightPixel); lastHeightPixel = newHeightPixel; } } void DynamicVector::drawConstrainedVector(const int& minIndex, const int& maxIndex, const int& minScreenIndex, const int& maxScreenIndex){ //constrain the height and width double stepSize = (maxScreenIndex - minScreenIndex) / (double)(maxIndex - minIndex);//step size in pixels per array bin double screenHeight = ofGetHeight(); double maxVal = getMaximum(); //OPTIMIZE!! XXX could just add stepsize each time //not add minindex each time int i = max(1,minIndex+1); // ofDrawBitmapString("i = "+ofToString(i)+" :: screen min: "+ofToString(minScreenIndex + stepSize*(i-minIndex-1)), 20, 640); while ((minScreenIndex + stepSize*(i-minIndex)) < 0) i++;//only draw what is on the screen for ( ; i < min(maxIndex+1, (int)array.size());i++){ ofLine (minScreenIndex + (stepSize*(i-minIndex-1)), screenHeight * (1 - array[i-1] / maxVal), minScreenIndex + (stepSize*(i-minIndex)), screenHeight * (1 - array[i] / maxVal) ); } ofLine(minScreenIndex, screenHeight, minScreenIndex, screenHeight/2); ofLine(maxScreenIndex, screenHeight, maxScreenIndex, screenHeight/2); // ofDrawBitmapString(ofToString(stepSize, 2)+" "+ofToString(maxScreenIndex - minScreenIndex, 0), 20, 600); }