view ofxPreciseOnsetDetectorOffline/PointerOnsetVisualiser.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
/*
 *  PointerOnsetVisualiser.cpp
 *  GreenOnionsMidiBeatApp
 *
 *  Created by Andrew on 14/01/2014.
 *  Copyright 2014 QMUL. All rights reserved.
 *
 */

#include "PointerOnsetVisualiser.h"


PointerOnsetVisualiser::PointerOnsetVisualiser(){
	
}

PointerOnsetVisualiser::~PointerOnsetVisualiser(){
	printf("deleting pointer in visualiser\n");
	pod = NULL;
	delete pod;
	
//	delete windowStart;
//	delete windowEnd;
//	delete windowPress;
}

void PointerOnsetVisualiser::newFile(){
	resetWindow();
//	windowPress = 0;
	printf("loading sound %s\n", pod->loadedFilename.c_str());
	soundPlay.loadSound(pod->loadedFilename, false);
	paused = true;
	soundPlay.play();
	soundPlay.setPaused(true);
}

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

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


void PointerOnsetVisualiser::draw(){
	//if plotting use this, else comment out
	
	ofSetColor(ofColor::white);
	window.drawBackground();
	ofSetColor(ofColor::black);
	window.drawOutline();
	
	//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->onsetList, window, (*windowStart), (*windowEnd));
	//replaced above line with a plotter that can do our onset list
	
	//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);
	
}

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

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

void PointerOnsetVisualiser::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 PointerOnsetVisualiser::cropStart(){
	if (soundPlay.getPositionMS()/1000. < (*windowEnd)){
		(*windowStart) = soundPlay.getPositionMS()/1000.;
		printf("s: start %.1f end %.1f\n", (*windowStart), (*windowEnd));
	}
}

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

void PointerOnsetVisualiser::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 PointerOnsetVisualiser::cropEndSeconds(double& val){
	if (val > (*windowStart))
		(*windowEnd) = val;
	
	printf("crop end: start %.1f end %.1f\n", (*windowStart), (*windowEnd));
}


void PointerOnsetVisualiser::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 PointerOnsetVisualiser::setSoundPositionSeconds(double position){
	printf("set position, was %i, new pos %f\n", soundPlay.getPositionMS(), position);
	soundPlay.setPositionMS(position*1000.0);
	printf("new position %i\n", soundPlay.getPositionMS());
}

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

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

void PointerOnsetVisualiser::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 PointerOnsetVisualiser::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 PointerOnsetVisualiser::scrollLeft(){
	double tmp = windowWidth();
	(*windowStart) = max(0., (*windowStart) - windowWidth()/2.);
	(*windowEnd) = (*windowStart) + tmp;
	checkPosition();
}


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

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