# HG changeset patch # User Andrew N Robertson # Date 1388770982 0 # Node ID 50f62c48b42117063652af39469649855c3ec9a4 # Parent 7ec1ed0b2eb041bf051be5405c0d8a1f43873d99 Small change diff -r 7ec1ed0b2eb0 -r 50f62c48b421 ofxPreciseOnsetDetectorOffline/PreciseOnsetDetectorOffline.cpp --- a/ofxPreciseOnsetDetectorOffline/PreciseOnsetDetectorOffline.cpp Mon Dec 30 14:08:42 2013 +0000 +++ b/ofxPreciseOnsetDetectorOffline/PreciseOnsetDetectorOffline.cpp Fri Jan 03 17:43:02 2014 +0000 @@ -36,8 +36,7 @@ int PreciseOnsetDetectorOffline::processAudioForBeatTimes(std::string audiofile){ //originally from BeatAnnotationViewer project - - BeatWriter writer; + dfValues.clear(); onsetLocations.clear(); onsetPositionFrames.clear(); @@ -83,12 +82,6 @@ int channels = sfinfo.channels; samples = sfinfo.frames; printf("Number of channels %i, samples %i\n", channels, samples); - - //for writing output - std::string outputFilename = audiofile+"_preciseOnsets.txt"; - - if (writeOutput) - writer.openFile(outputFilename); int blocksize = hopSize;//FRAMESIZE; @@ -138,8 +131,7 @@ //as we have just added hopsize samples in, the beginning of the frame is -1 from counter exactsample += precisesample; //printf("PreciseSample %i, %i\n", precisesample, exactsample); - if (writeOutput) - writer.writeBeatTime(exactsample/44100.); + onsetLocations.push_back(exactsample/44100.); } @@ -154,9 +146,6 @@ // Close input file sf_close (infile); - if (writeOutput) - writer.closeFile(); - delete detectionFunction; detectionFunction = NULL; @@ -164,8 +153,32 @@ } +void PreciseOnsetDetectorOffline::exportOnsetTimes(){ + exportOnsetTimes(0.0, samples/44100.0);//i.e. the whole file +} +void PreciseOnsetDetectorOffline::exportOnsetTimes(double startTime, double endTime){ + //for writing output + BeatWriter writer; + std::string outputFilename = loadedFilename+"_preciseOnsets.txt"; + + if (writeOutput) + writer.openFile(outputFilename); + + + int index = 0; + while (index < onsetLocations.size() && onsetLocations[index] < startTime) + index++; + + while (index < onsetLocations.size() && onsetLocations[index] <= endTime){ + writer.writeBeatTime(onsetLocations[index]); + index++; + } + + if (writeOutput) + writer.closeFile(); +} void PreciseOnsetDetectorOffline::update(){ @@ -180,3 +193,20 @@ for (int i = 0; i < (int)onsetLocations.size(); i++) printf("Onset[%i]: %.3f\n", i, onsetLocations[i]); } + +double PreciseOnsetDetectorOffline::closestOnset(double& targetVal){ + int bestIndex = 0; + double bestDiff = 99999; + double bestVal = -1; + + for (int testIndex = 0; testIndex < (int)onsetLocations.size(); testIndex++){ + double testDiff = (onsetLocations[testIndex] - targetVal); + if (fabs(testDiff) < bestDiff){ + bestDiff = fabs(testDiff); + bestVal = onsetLocations[testIndex]; + bestIndex = testIndex; + } + } + return bestVal; +} + diff -r 7ec1ed0b2eb0 -r 50f62c48b421 ofxPreciseOnsetDetectorOffline/PreciseOnsetDetectorOffline.h --- a/ofxPreciseOnsetDetectorOffline/PreciseOnsetDetectorOffline.h Mon Dec 30 14:08:42 2013 +0000 +++ b/ofxPreciseOnsetDetectorOffline/PreciseOnsetDetectorOffline.h Fri Jan 03 17:43:02 2014 +0000 @@ -32,6 +32,10 @@ void draw(); int processAudioForBeatTimes(std::string audiofile); + void exportOnsetTimes(); + void exportOnsetTimes(double startTime, double endTime); + + double closestOnset(double& targetVal); void printOnsetLocations(); diff -r 7ec1ed0b2eb0 -r 50f62c48b421 ofxPreciseOnsetDetectorOffline/PreciseOnsetVisualiser.cpp --- a/ofxPreciseOnsetDetectorOffline/PreciseOnsetVisualiser.cpp Mon Dec 30 14:08:42 2013 +0000 +++ b/ofxPreciseOnsetDetectorOffline/PreciseOnsetVisualiser.cpp Fri Jan 03 17:43:02 2014 +0000 @@ -14,6 +14,7 @@ } PreciseOnsetVisualiser::~PreciseOnsetVisualiser(){ + printf("deleting pointer in visualiser\n"); delete pod; pod = NULL; } @@ -43,27 +44,52 @@ plotter.drawBeatStripes(pod->onsetLocations, window, windowStart, windowEnd); ofSetColor(ofColor::red); - plotter.drawStripe(soundPlay.getPosition()*pod->samples/44100., window, windowStart, windowEnd); + plotter.drawStripe(positionSeconds(), window, windowStart, windowEnd); } +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(){ - windowStart = windowPress; - printf("s: start %.1f end %.1f\n", windowStart, windowEnd); + if (soundPlay.getPositionMS()/1000. < windowEnd){ + windowStart = soundPlay.getPositionMS()/1000.; + printf("s: start %.1f end %.1f\n", windowStart, windowEnd); + } } -void PreciseOnsetVisualiser::cropEnd(){ - if (windowPress > windowStart) - windowEnd = windowPress; +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); } @@ -72,8 +98,9 @@ void PreciseOnsetVisualiser::mousePressed(int& x, int& y){ if (window.tapped(x, y)){ windowPress = windowStart + (windowEnd-windowStart)*(x - window.x)/window.width; - - printf("window position is %f\n", windowPress); + double newPos = windowPress/(double)(pod->samples/44100.); + printf("window position is %.1f new pos %f\n", windowPress, newPos); + soundPlay.setPositionMS(windowPress*1000.0); } } @@ -88,5 +115,13 @@ 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; } \ No newline at end of file diff -r 7ec1ed0b2eb0 -r 50f62c48b421 ofxPreciseOnsetDetectorOffline/PreciseOnsetVisualiser.h --- a/ofxPreciseOnsetDetectorOffline/PreciseOnsetVisualiser.h Mon Dec 30 14:08:42 2013 +0000 +++ b/ofxPreciseOnsetDetectorOffline/PreciseOnsetVisualiser.h Fri Jan 03 17:43:02 2014 +0000 @@ -23,13 +23,17 @@ void newFile(); void update(); void draw(); + double positionSeconds(); void mousePressed(int& x, int& y); void togglePlay(); + void stop(); void resetWindow(); void cropStart(); void cropEnd(); + void cropStartSeconds(double& val); + void cropEndSeconds(double& val); //vars PreciseOnsetDetectorOffline* pod; diff -r 7ec1ed0b2eb0 -r 50f62c48b421 ofxPreciseOnsetDetectorOffline/testApp.cpp --- a/ofxPreciseOnsetDetectorOffline/testApp.cpp Mon Dec 30 14:08:42 2013 +0000 +++ b/ofxPreciseOnsetDetectorOffline/testApp.cpp Fri Jan 03 17:43:02 2014 +0000 @@ -5,7 +5,7 @@ //-------------------------------------------------------------- void testApp::setup(){ pov.window.setToRelativeSize(0.1, 0.1, 0.8, 0.3); - pov.pod = &preciseOnsetDetect; + pov.pod = &preciseOnsetDetect;//set up pointer if we want to visualise the onsets in a window // preciseOnsetDetect.load("/Users/andrew/Documents/work/programming/MadMax/AudioFiles/AdamBetts/AdamBetss_1_Swing_Kick.wav"); // preciseOnsetDetect.load("/Users/andrew/Music/Logic/GreenOnionsChichester/GreenOnionsChichester/Bouncing/Snare_Sontronics#08edit.aif"); @@ -18,10 +18,6 @@ preciseOnsetDetect.load(filename); preciseOnsetDetect.printOnsetLocations(); - //windowStart = 0; - //windowEnd = preciseOnsetDetect.samples/44100.; - //windowPress = 0; - pov.newFile(); } @@ -35,18 +31,10 @@ void testApp::draw(){ pov.draw(); - /* - //if plotting use this, else comment out - ofSetColor(ofColor::white); - window.drawBackground(); - ofSetColor(ofColor::black); - window.drawOutline(); - ofSetColor(ofColor::blue); - plotter.drawBeatStripes(preciseOnsetDetect.onsetLocations, window, windowStart, windowEnd); -*/ - + } + //-------------------------------------------------------------- void testApp::keyPressed(int key){ std::string loadName; @@ -60,6 +48,10 @@ case 'e': pov.cropEnd(); break; + case 'x': + printf("Exporting between %f and %f\n", pov.windowStart, pov.windowEnd); + preciseOnsetDetect.exportOnsetTimes(pov.windowStart, pov.windowEnd); + break; case 'o': if (getFilenameFromDialogBox(&loadName)){ printf("loading %s\n", (loadName).c_str()); diff -r 7ec1ed0b2eb0 -r 50f62c48b421 ofxPreciseOnsetDetectorOffline/testApp.h --- a/ofxPreciseOnsetDetectorOffline/testApp.h Mon Dec 30 14:08:42 2013 +0000 +++ b/ofxPreciseOnsetDetectorOffline/testApp.h Fri Jan 03 17:43:02 2014 +0000 @@ -14,6 +14,7 @@ void update(); void draw(); + void keyPressed(int key); void keyReleased(int key); void mouseMoved(int x, int y); @@ -30,9 +31,6 @@ //vars PreciseOnsetDetectorOffline preciseOnsetDetect; -// ofxWindowRegion window; -// ofxPlotFunction plotter; -// double windowStart, windowEnd, windowPress; PreciseOnsetVisualiser pov; };