view newOFsrc/beatTempo.cpp @ 2:c49a8f33afab

adding new OF source
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Wed, 22 Feb 2012 22:16:48 +0000
parents
children
line wrap: on
line source
/*
 *  beatTempo.cpp
 *  bayesianTempo1
 *
 *  Created by Andrew Robertson on 08/05/2010.
 *  Copyright 2010 __MyCompanyName__. All rights reserved.
 *
 */

#include "beatTempo.h"

beatTempo::beatTempo(){
index = 0;
lastBeatTime = 0;
decayAmount = 0;
}

//records a loop of cpu times for the last 16 events received

void beatTempo::addBeatTime(double f, int type){
//type 1 is kick, type 2 is snare

	index++;
	startIndex++;
	if (index == 16){
	index = 0;
	}
	lastBeatTime = f;
	beatTimes[index] = f;
	
	//find the closest click time to this new cputime
	//NOT ACTUALLY USED ANYWHERE....
	//double useThisClickTime = lastClickTime;
	//int useThisClickIndex = lastClickIndex ;
	
		if (lastClickTime + (tatum/2) < f){
		//next click time
		closestClickIndexToBeat[index] = lastClickIndex + 1;
		//useThisClickTime += tatum;
		//	useThisClickIndex++;
		}
		else{
		//recent click time
		closestClickIndexToBeat[index] = lastClickIndex;
		}
		//end not used

	
	int lastBeatSegment = beatSegment;
	
	//timeDifference = f - lastClickTime;
	timeDifference = f - lastClickTime;
	//[0, 1] => [0, 2*tatum]
	beatSegment = (6*(lastClickIndex%8));
	beatSegment +=  ( ( (int)floor( ( ( (timeDifference + (tatum/12)) * 6) ) / tatum) ) );
	

	
	//this calculates the probabilities of events in the different zones
	//not yet used in the algorithm
	beatSegment = beatSegment%48;
	//wipe old onez
	while (lastBeatSegment%48 != beatSegment){
		lastBeatSegment++;
		if (lastBeatSegment%12 == 0)
	decayProbabilityDistributionRow((lastBeatSegment/12)%4);
		
	beatMap[lastBeatSegment%48] = 0;

	}
	addToProbabilityDistribution(beatSegment, type);
	//end of new addition
	
	beatMap[beatSegment] = type;
	
	
	
	double intervalCalculation;
	int otherIndex;
	for (otherIndex = 0;otherIndex < 16;otherIndex++){
		
		if (otherIndex != index){
		intervalCalculation = calculateInterval(index, otherIndex);
		relativeIntervals[otherIndex][0] = intervalCalculation;
		intervalDifferences[index][otherIndex] =  intervalCalculation;
			
		//integer multiple is relativeIntervals[otherIndex][1]
		}
		else{
		intervalDifferences[index][otherIndex] =  0;
		}
	}
	
	
	
}


void beatTempo::addToProbabilityDistribution(int beatSegment, int type){
	//printf("beat segment %i\n", beatSegment);
	int beatNumber = (int) (beatSegment + 1)/12;
	beatNumber = beatNumber%4;
	float newProbabilityContribution = 1 - decayAmount;
	switch (beatSegment%12) {
		case 0:
			beatProbabilityDistribution[beatNumber][0][type - 1] = newProbabilityContribution;
			break;
		case 1:
			beatProbabilityDistribution[beatNumber][0][type - 1] = 1*newProbabilityContribution;
			break;
		case 2:
			beatProbabilityDistribution[beatNumber][0][type - 1] = 0.5*newProbabilityContribution;
			beatProbabilityDistribution[beatNumber][1][type - 1] = 0.5*newProbabilityContribution;
			break;	
		case 3:
			beatProbabilityDistribution[beatNumber][1][type - 1] = 1*newProbabilityContribution;
			break;		
		case 4:
			beatProbabilityDistribution[beatNumber][1][type - 1] = 0.4*newProbabilityContribution;
			beatProbabilityDistribution[beatNumber][2][type - 1] = 0.6*newProbabilityContribution;
			break;	
		case 5:
			beatProbabilityDistribution[beatNumber][2][type - 1] = 0.4*newProbabilityContribution;
			beatProbabilityDistribution[beatNumber][3][type - 1] = 0.6*newProbabilityContribution;
			break;		
		case 6:
			beatProbabilityDistribution[beatNumber][3][type - 1] = 1*newProbabilityContribution;
			break;
		case 7:
			beatProbabilityDistribution[beatNumber][3][type - 1] = 1*newProbabilityContribution;
			break;	
		case 8:
			beatProbabilityDistribution[beatNumber][4][type - 1] = 0.6*newProbabilityContribution;
			beatProbabilityDistribution[beatNumber][5][type - 1] = 0.4*newProbabilityContribution;
			break;		
		case 9:
			beatProbabilityDistribution[beatNumber][5][type - 1] = 1*newProbabilityContribution;
			break;	
		case 10:
			beatProbabilityDistribution[beatNumber][5][type - 1] = 0.5*newProbabilityContribution;
			break;		
		case 11:
			beatProbabilityDistribution[(beatNumber+1)%4][0][type - 1] = 1*newProbabilityContribution;
			break;		
	}
	
	
}

void beatTempo::decayProbabilityDistributionRow(int row){
	
	for (int x = 0;x<6;x++){
		beatProbabilityDistribution[row][x][0] *= decayAmount;
		beatProbabilityDistribution[row][x][1] *= decayAmount;
	}
	
	
}

double beatTempo::calculateInterval(int newIndex, int otherIndex){

	double newTime, otherTime, interval, relativeInterval;
	relativeInterval = tatum;
	int tatumMultiple;
	newTime = beatTimes[newIndex];
	otherTime = beatTimes[otherIndex];
	if (otherTime > 0){
	interval = newTime - otherTime;
//	tatumMultiple = closestClickIndexToBeat[newIndex] - closestClickIndexToBeat[otherIndex]; - to be added
	tatumMultiple = round (interval / tatum);
		if (tatumMultiple > 0){
		relativeInterval = interval / (tatumMultiple);
		relativeIntervals[otherIndex][1] = tatumMultiple;
		tatumMultiples[newIndex][otherIndex] = tatumMultiple;
		}//end if
	}
	return relativeInterval;

}


void beatTempo::resetBeatTimeArray(){
	for (int i = 0;i < 16;i++){
	beatTimes[i] = 0;
		for (int k = 0;k < 16;k++)
			intervalDifferences[i][k] = 0;
	}
}