andrew@6: /* andrew@6: * PointerOnsetVisualiser.cpp andrew@6: * GreenOnionsMidiBeatApp andrew@6: * andrew@6: * Created by Andrew on 14/01/2014. andrew@6: * Copyright 2014 QMUL. All rights reserved. andrew@6: * andrew@6: */ andrew@6: andrew@6: #include "PointerOnsetVisualiser.h" andrew@6: andrew@6: andrew@6: PointerOnsetVisualiser::PointerOnsetVisualiser(){ andrew@6: andrew@6: } andrew@6: andrew@6: PointerOnsetVisualiser::~PointerOnsetVisualiser(){ andrew@6: printf("deleting pointer in visualiser\n"); andrew@6: pod = NULL; andrew@6: delete pod; andrew@6: andrew@6: // delete windowStart; andrew@6: // delete windowEnd; andrew@6: // delete windowPress; andrew@6: } andrew@6: andrew@6: void PointerOnsetVisualiser::newFile(){ andrew@6: resetWindow(); andrew@6: // windowPress = 0; Venetian@7: printf("loading sound %s\n", pod->loadedFilename.c_str()); andrew@6: soundPlay.loadSound(pod->loadedFilename, false); andrew@6: paused = true; andrew@6: soundPlay.play(); andrew@6: soundPlay.setPaused(true); andrew@6: } andrew@6: andrew@6: double PointerOnsetVisualiser::windowWidth(){ andrew@6: return (*windowEnd) - (*windowStart); andrew@6: } andrew@6: andrew@6: void PointerOnsetVisualiser::update(){ andrew@6: double positionNow = positionSeconds(); andrew@6: if (positionNow > (*windowEnd)){ andrew@6: double tmp = windowWidth(); andrew@6: (*windowEnd) += tmp; andrew@6: (*windowStart) += tmp; andrew@6: //printf("Scrolling\n"); andrew@6: } andrew@6: andrew@6: } andrew@6: andrew@6: andrew@6: void PointerOnsetVisualiser::draw(){ andrew@6: //if plotting use this, else comment out andrew@6: andrew@6: ofSetColor(ofColor::white); andrew@6: window.drawBackground(); andrew@6: ofSetColor(ofColor::black); andrew@6: window.drawOutline(); andrew@6: andrew@6: //draw df function andrew@6: /* andrew@6: int startIndex = 0; andrew@6: while (startIndex < (int)pod->dfValues.size() && pod->frameIndexToSeconds(startIndex) < (*windowStart)) andrew@6: startIndex++; andrew@6: andrew@6: int endIndex = 0; andrew@6: while (endIndex < (int)pod->dfValues.size()-1 && pod->frameIndexToSeconds(endIndex) < (*windowEnd)) andrew@6: endIndex++; andrew@6: */ andrew@6: ofSetColor(ofColor::tan); andrew@6: // plotter.drawVector(pod->dfValues, startIndex, endIndex, window); andrew@6: plotter.drawVector(pod->dfValues, round(pod->secondsToFrameIndex((*windowStart))), round(pod->secondsToFrameIndex((*windowEnd))), window); andrew@6: ofSetColor(ofColor::black); andrew@6: ofDrawBitmapString(ofToString(round(pod->secondsToFrameIndex((*windowStart))), 1), window.x, window.y-10); andrew@6: ofDrawBitmapString(ofToString(round(pod->secondsToFrameIndex((*windowEnd))), 1), window.x+window.width, window.y-10); andrew@6: andrew@6: ofSetColor(ofColor::blue); Venetian@7: plotter.drawBeatStripes(pod->onsetList, window, (*windowStart), (*windowEnd)); Venetian@7: //replaced above line with a plotter that can do our onset list andrew@6: andrew@6: //play position andrew@6: ofSetColor(ofColor::red); andrew@6: plotter.drawStripe(positionSeconds(), window, (*windowStart), (*windowEnd)); andrew@6: andrew@6: ofSetColor(ofColor::black); andrew@6: ofDrawBitmapString(ofToString((*windowStart), 1), window.x, window.y+window.height+10); andrew@6: ofDrawBitmapString(ofToString((*windowEnd), 1), window.x+window.width, window.y+window.height+10); andrew@6: andrew@6: } andrew@6: andrew@6: double PointerOnsetVisualiser::positionSeconds(){ andrew@6: return soundPlay.getPosition()*pod->samples/44100.; andrew@6: } andrew@6: andrew@6: //void PointerOnsetVisualiser::drawOnsets(DoubleVector& onsetTimesSeconds, ofxWindowregion& window, double startTime, double endTime){ andrew@6: //} andrew@6: andrew@6: void PointerOnsetVisualiser::resetWindow(){ andrew@6: (*windowStart) = 0; andrew@6: (*windowEnd) = 8; andrew@6: // while ((*windowEnd) < pod->samples/44100.) andrew@6: // (*windowEnd) *= 2; andrew@6: Venetian@7: // (*windowEnd) = pod->samples/44100.; andrew@6: andrew@6: printf("reset: start %.1f end %.1f\n", (*windowStart), (*windowEnd)); andrew@6: } andrew@6: andrew@6: void PointerOnsetVisualiser::cropStart(){ andrew@6: if (soundPlay.getPositionMS()/1000. < (*windowEnd)){ andrew@6: (*windowStart) = soundPlay.getPositionMS()/1000.; andrew@6: printf("s: start %.1f end %.1f\n", (*windowStart), (*windowEnd)); andrew@6: } andrew@6: } andrew@6: andrew@6: void PointerOnsetVisualiser::cropStartSeconds(double& val){ andrew@6: if (val < (*windowEnd)) andrew@6: (*windowStart) = val; andrew@6: } andrew@6: andrew@6: void PointerOnsetVisualiser::cropEnd(){//crops to play position andrew@6: andrew@6: if (soundPlay.getPositionMS()/1000. > (*windowStart)){ andrew@6: (*windowEnd) = soundPlay.getPositionMS()/1000.; andrew@6: printf("crop end: start %.1f end %.1f\n", (*windowStart), (*windowEnd)); andrew@6: } andrew@6: } andrew@6: andrew@6: andrew@6: void PointerOnsetVisualiser::cropEndSeconds(double& val){ andrew@6: if (val > (*windowStart)) andrew@6: (*windowEnd) = val; andrew@6: andrew@6: printf("crop end: start %.1f end %.1f\n", (*windowStart), (*windowEnd)); andrew@6: } andrew@6: andrew@6: andrew@6: void PointerOnsetVisualiser::mousePressed(int& x, int& y){ andrew@6: /* andrew@6: if (window.tapped(x, y)){ andrew@6: windowPress = (*windowStart) + ((*windowEnd)-(*windowStart))*(x - window.x)/window.width; andrew@6: double newPos = windowPress/(double)(pod->samples/44100.); andrew@6: printf("window position is %.1f new pos %f\n", windowPress, newPos); andrew@6: soundPlay.setPositionMS(windowPress*1000.0); andrew@6: } andrew@6: */ andrew@6: } andrew@6: andrew@6: void PointerOnsetVisualiser::setSoundPositionSeconds(double position){ andrew@6: printf("set position, was %i, new pos %f\n", soundPlay.getPositionMS(), position); andrew@6: soundPlay.setPositionMS(position*1000.0); andrew@6: printf("new position %i\n", soundPlay.getPositionMS()); andrew@6: } andrew@6: andrew@6: void PointerOnsetVisualiser::togglePlay(){ andrew@6: if (!paused ){ andrew@6: soundPlay.setPaused(true); andrew@6: paused = true; andrew@6: printf("was playing\n"); andrew@6: } andrew@6: else { andrew@6: soundPlay.setPaused(false);// andrew@6: paused = false; andrew@6: printf("was not playing\n"); andrew@6: } andrew@6: } andrew@6: andrew@6: void PointerOnsetVisualiser::stop(){ andrew@6: soundPlay.stop(); andrew@6: //then get set to be played andrew@6: soundPlay.play(); andrew@6: soundPlay.setPaused(true); andrew@6: soundPlay.setPositionMS((*windowStart)*1000.0); andrew@6: paused = true; andrew@6: } andrew@6: andrew@6: void PointerOnsetVisualiser::zoomIn(){ andrew@6: double positionNow = positionSeconds(); andrew@6: if (positionNow > (*windowEnd)){ andrew@6: double tmp = windowWidth(); andrew@6: (*windowEnd) += windowWidth(); andrew@6: (*windowStart) += tmp; andrew@6: } andrew@6: andrew@6: (*windowStart) = positionNow - ((*windowEnd)-(*windowStart))/4; andrew@6: if ((*windowStart) < 0) andrew@6: (*windowStart) = 0; andrew@6: (*windowEnd) = positionNow + ((*windowEnd)-(*windowStart))/4; andrew@6: } andrew@6: andrew@6: andrew@6: void PointerOnsetVisualiser::zoomOut(){ andrew@6: double positionNow = positionSeconds(); andrew@6: if (positionNow > (*windowEnd)){ andrew@6: double tmp = windowWidth(); andrew@6: (*windowEnd) += tmp; andrew@6: (*windowStart) += tmp; andrew@6: } andrew@6: andrew@6: (*windowStart) = positionNow - ((*windowEnd)-(*windowStart)); andrew@6: (*windowEnd) = positionNow + ((*windowEnd)-(*windowStart)); andrew@6: if ((*windowStart) < 0) andrew@6: (*windowStart) = 0; andrew@6: } andrew@6: andrew@6: void PointerOnsetVisualiser::scrollLeft(){ andrew@6: double tmp = windowWidth(); andrew@6: (*windowStart) = max(0., (*windowStart) - windowWidth()/2.); andrew@6: (*windowEnd) = (*windowStart) + tmp; andrew@6: checkPosition(); andrew@6: } andrew@6: andrew@6: andrew@6: void PointerOnsetVisualiser::scrollRight(){ andrew@6: double tmp = windowWidth(); andrew@6: (*windowStart) = min( (*windowStart)+tmp/2., pod->samples/44100.); andrew@6: (*windowEnd) = (*windowStart) + tmp; andrew@6: checkPosition(); andrew@6: } andrew@6: andrew@6: void PointerOnsetVisualiser::checkPosition(){ andrew@6: double positionNow = positionSeconds(); andrew@6: if (positionNow < (*windowStart) || positionNow > (*windowEnd)) andrew@6: soundPlay.setPositionMS((*windowStart)*1000); andrew@6: }