view ofxPreciseOnsetDetectorOffline/PreciseOnsetVisualiser.cpp @ 8:184a7c232049 tip

changed files since updating computer
author Venetian
date Thu, 14 Aug 2014 17:53:57 +0100
parents b1c13e8bec26
children
line wrap: on
line source
/*
 *  PreciseOnsetVisualiser.cpp
 *  ofxPreciseOnsetDetectionOffline
 *
 *  Created by Andrew Robertson on 28/12/2013.
 *  Copyright 2013 QMUL. All rights reserved.
 *
 */

#include "PreciseOnsetVisualiser.h"

PreciseOnsetVisualiser::PreciseOnsetVisualiser(){

}

PreciseOnsetVisualiser::~PreciseOnsetVisualiser(){
	printf("deleting pointer in visualiser\n");
	pod = NULL;
	delete pod;

}

void PreciseOnsetVisualiser::newFile(){
	resetWindow();
	
	//problem example
	//soundPlay.loadSound("/Users/andrew/Music/Logic/GreenOnionsEvaluationThree/GreenOnionsEvaluationMetronome3/Audio Files/SM57_Metronome#13.aif");//.c_str());
	
	windowPress = 0;
	printf("Precise onset, loading...'%s'\n", pod->loadedFilename.c_str());
	soundPlay.loadSound(pod->loadedFilename, false);
	stop();//sets it not playing at beginning
}

double PreciseOnsetVisualiser::windowWidth(){
	return windowEnd - windowStart;
}

void PreciseOnsetVisualiser::update(){
	currentPlayPosition = positionSeconds();
	if (currentPlayPosition > windowEnd){
		double tmp = windowWidth();
		windowEnd += tmp;
		windowStart += tmp;
		//printf("Scrolling\n");
	}

}


void PreciseOnsetVisualiser::draw(){
	ofSetLineWidth(1);
	//if plotting use this, else comment out

	ofSetColor(ofColor::white);
	window.drawBackground();
	ofSetColor(ofColor::black);
	window.drawOutline();
	
	ofDrawBitmapString(title, window.x+60, window.y - 10);
	
	//draw df function
/*
	int startIndex = 0;
	while (startIndex < (int)pod->dfValues.size() && pod->frameIndexToSeconds(startIndex) < windowStart)
		startIndex++;
	
	int endIndex = 0;
	while (endIndex < (int)pod->dfValues.size()-1  && pod->frameIndexToSeconds(endIndex) < windowEnd)
		endIndex++;
*/	
	ofSetColor(ofColor::tan);
//	plotter.drawVector(pod->dfValues, startIndex, endIndex, window);
	plotter.drawVector(pod->dfValues, round(pod->secondsToFrameIndex(windowStart)), round(pod->secondsToFrameIndex(windowEnd)), window);
	
	ofSetColor(ofColor::black);
	ofDrawBitmapString(ofToString(round(pod->secondsToFrameIndex(windowStart)), 1), window.x, window.y-10);
	ofDrawBitmapString(ofToString(round(pod->secondsToFrameIndex(windowEnd)), 1), window.x+window.width, window.y-10);
	
	ofSetColor(ofColor::blue);
//	plotter.drawBeatStripes(pod->onsetLocations, window, windowStart, windowEnd);
	plotter.drawBeatStripes(pod->onsetList, window, windowStart, windowEnd);
	drawOnsetInfo(pod->onsetList, window, windowStart, windowEnd);
	
	//play position
	ofSetColor(ofColor::red);
	plotter.drawStripe(positionSeconds(), window, windowStart, windowEnd);
	
	ofSetColor(ofColor::black);
	ofDrawBitmapString(ofToString(windowStart, 1), window.x, window.y+window.height+10);
	ofDrawBitmapString(ofToString(windowEnd, 1), window.x+window.width, window.y+window.height+10);
	
}

void PreciseOnsetVisualiser::drawOnsetInfo(std::vector<OnsetInfo> onsetList, ofxWindowRegion& window, double startTime, double endTime){
	ofSetColor(ofColor::darkBlue);
	if (endTime > startTime){
		int index = 0;
		while (index < onsetList.size() && onsetList[index].onsetLocation < startTime) {
			index++;
		}
		
		int pos = max(0, window.x - 45);
		ofDrawBitmapString("onsetLocation", pos, window.y+window.height+10);
		ofDrawBitmapString("beatPosition", pos, window.y+window.height+30);
		ofDrawBitmapString("onsetType", pos, window.y+window.height+50);
		if (pod->isBass){
			ofDrawBitmapString("pitch", pos, window.y+window.height+70);
			ofDrawBitmapString("midiPitch", pos, window.y+window.height+90);
			ofDrawBitmapString("midiPrediction", pos, window.y+window.height+110);
			ofDrawBitmapString("midiName", pos, window.y+window.height+130);
			ofDrawBitmapString("exp timing", pos, window.y+window.height+150);	
		}
		
		while (index < onsetList.size() && onsetList[index].onsetLocation <= endTime){
			pos = window.width*((onsetList[index].onsetLocation - startTime)/(endTime - startTime));
			pos += window.x;
			int lineWidth = 20;
			ofDrawBitmapString(ofToString(onsetList[index].onsetLocation, 2), pos, window.y+window.height+10);
			ofDrawBitmapString(ofToString(onsetList[index].beatPosition, 0), pos, window.y+window.height+30);
			ofDrawBitmapString(ofToString(onsetList[index].onsetType, 0), pos, window.y+window.height+50);
			if (pod->isBass){
				ofDrawBitmapString(ofToString(onsetList[index].pitch, 1), pos, window.y+window.height+70);
				ofDrawBitmapString(ofToString(onsetList[index].midiPitch, 0), pos, window.y+window.height+90);
				if (onsetList[index].midiPrediction == onsetList[index].roundedPitch)
					ofSetColor(ofColor::darkGreen);
				else if (abs(onsetList[index].midiPrediction - onsetList[index].roundedPitch) == 12)
					ofSetColor(ofColor::orange);
				else 
					ofSetColor(ofColor::darkRed);
				
				ofDrawBitmapString(ofToString(onsetList[index].midiPrediction, 0), pos, window.y+window.height+110);
				ofSetColor(ofColor::darkBlue);
				ofDrawBitmapString(onsetList[index].midiName, pos, window.y+window.height+130);
				if (onsetList[index].expressiveTiming)
					ofDrawBitmapString(ofToString(1000.0*onsetList[index].expressiveTiming, 1), pos, window.y+window.height+150);	
			}
			
//			ofLine(pos, window.y, pos, window.y+window.height);
			index++;
		}
	}
}



double PreciseOnsetVisualiser::positionSeconds(){
	return soundPlay.getPosition()*pod->samples/44100.;
}

//void PreciseOnsetVisualiser::drawOnsets(DoubleVector& onsetTimesSeconds, ofxWindowregion& window, double startTime, double endTime){
//}

void PreciseOnsetVisualiser::resetWindow(){
	windowStart = 0;
	windowEnd = 8;
//	while (windowEnd < pod->samples/44100.)
//		windowEnd *= 2;
	
//	windowEnd = pod->samples/44100.;
		
	printf("reset: start %.1f end %.1f\n", windowStart, windowEnd);
}

void PreciseOnsetVisualiser::cropStart(){
	if (soundPlay.getPositionMS()/1000. < windowEnd){
		windowStart = soundPlay.getPositionMS()/1000.;
		printf("s: start %.1f end %.1f\n", windowStart, windowEnd);
	}
}

void PreciseOnsetVisualiser::cropStartSeconds(double& val){
	if (val < windowEnd)
		windowStart = val;
}

void PreciseOnsetVisualiser::cropEnd(){//crops to play position
	
	if (soundPlay.getPositionMS()/1000. > windowStart){
		windowEnd = soundPlay.getPositionMS()/1000.;
		printf("crop end: start %.1f end %.1f\n", windowStart, windowEnd);
	}
}


void PreciseOnsetVisualiser::cropEndSeconds(double& val){
	if (val > windowStart)
		windowEnd = val;
	
	printf("crop end: start %.1f end %.1f\n", windowStart, windowEnd);
}


void PreciseOnsetVisualiser::mousePressed(int& x, int& y){
	if (window.tapped(x, y)){
		windowPress = windowStart + (windowEnd-windowStart)*(x - window.x)/window.width;
		double newPos = windowPress/(double)(pod->samples/44100.);
		printf("window position is %.1f new pos %f\n", windowPress, newPos);
		soundPlay.setPositionMS(windowPress*1000.0);
	}
}

void PreciseOnsetVisualiser::togglePlay(){
	if (!paused ){
		soundPlay.setPaused(true);
		paused = true;
		//printf("was playing\n");
	}
	else {
		soundPlay.setPaused(false);//
		paused = false;
		//printf("was not playing\n");
	}
}

void PreciseOnsetVisualiser::stop(){
	soundPlay.stop();
	//then get set to be played
	soundPlay.play();
	soundPlay.setPaused(true);
	soundPlay.setPositionMS(windowStart*1000.0);
	paused = true;
}

void PreciseOnsetVisualiser::zoomIn(){
	double positionNow = positionSeconds();
	if (positionNow > windowEnd){
		double tmp = windowWidth();
		windowEnd += windowWidth();
		windowStart += tmp;
	}
	
	windowStart = positionNow - (windowEnd-windowStart)/4;
	if (windowStart < 0)
		windowStart = 0;
	windowEnd = positionNow + (windowEnd-windowStart)/4;
}


void PreciseOnsetVisualiser::zoomOut(){
	double positionNow = positionSeconds();
	if (positionNow > windowEnd){
		double tmp = windowWidth();
		windowEnd += tmp;
		windowStart += tmp;
	}

	windowStart = positionNow - (windowEnd-windowStart);
	windowEnd = positionNow + (windowEnd-windowStart);
	if (windowStart < 0)
		windowStart = 0;
}

void PreciseOnsetVisualiser::scrollLeft(){
	double tmp = windowWidth();
	windowStart = max(0., windowStart - windowWidth()/2.);
	windowEnd = windowStart + tmp;
	checkPosition();
}


void PreciseOnsetVisualiser::scrollRight(){
	double tmp = windowWidth();
	windowStart = min( windowStart+tmp/2., pod->samples/44100.);
	windowEnd = windowStart + tmp;
	checkPosition();
}

void PreciseOnsetVisualiser::scrollRightSecs(double secs){
	double positionNow = positionSeconds();
	double newPosition = min(positionNow+secs, pod->samples/44100.);
	soundPlay.setPositionMS(newPosition*1000.);
	double tmp = windowWidth()/2.;
	while (windowEnd < newPosition){
		windowEnd += tmp;
		windowStart += tmp;
	}
}


void PreciseOnsetVisualiser::scrollLeftSecs(double secs){
	double positionNow = positionSeconds();
	double newPosition = max(positionNow-secs, 0.);
	soundPlay.setPositionMS(newPosition*1000.);
	double tmp = windowWidth()/2.;
	while (windowStart > newPosition){
		windowEnd -= tmp;
		windowStart -= tmp;
	}
}


void PreciseOnsetVisualiser::checkPosition(){
double positionNow = positionSeconds();
	if (positionNow < windowStart || positionNow > windowEnd)
		soundPlay.setPositionMS(windowStart*1000.);
}


void PreciseOnsetVisualiser::setPositionSeconds(double secs){
	soundPlay.setPositionMS(secs*1000.);
	double tmp = windowEnd - windowStart;
	while (windowStart > max(0. , secs)){
		windowStart -= tmp;
		windowEnd -= tmp;
	}
	while (windowEnd < min(pod->samples/44100. , secs)){
		windowStart += tmp;
		windowEnd += tmp;
	}
}

double PreciseOnsetVisualiser::lengthSeconds(){
	return pod->samples/44100.;
}


void PreciseOnsetVisualiser::keyPressed(int key){
	switch (key) {
		case 'r':
			resetWindow();
			break;
		case 's':
			cropStart();
			break;
		case 'e':
			cropEnd();
			break;
		case ' ':
			togglePlay();
			break;
		case OF_KEY_RETURN:
			stop();
			
			break;
			
		case OF_KEY_UP: case 'u':
			zoomIn();
			break;
		case OF_KEY_DOWN:
			zoomOut();
			break;
		case OF_KEY_RIGHT:
			scrollRight();
			break;
		case OF_KEY_LEFT:
			scrollLeft();
			break;	
			
		default:
			break;
	}
}