Skip to content

Commit

Permalink
Moved some parts of the algorithm from arrays needing lots of data mo…
Browse files Browse the repository at this point in the history
…ved around to lighted circular buffers
  • Loading branch information
adamstark committed Mar 4, 2016
1 parent 758e62a commit ed96a61
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 39 deletions.
80 changes: 44 additions & 36 deletions src/BTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <algorithm>
#include "BTrack.h"
#include "samplerate.h"
#include <iostream>

//=======================================================================
BTrack::BTrack() : odf(512,1024,ComplexSpectralDifferenceHWR,HanningWindow)
Expand Down Expand Up @@ -229,14 +230,15 @@ void BTrack::processOnsetDetectionFunctionSample(double newSample)
beatCounter--;
beatDueInFrame = false;

// move all samples back one step
for (int i=0;i < (onsetDFBufferSize-1);i++)
{
onsetDF[i] = onsetDF[i+1];
}
// // move all samples back one step
// for (int i=0;i < (onsetDFBufferSize-1);i++)
// {
// onsetDF[i] = onsetDF[i+1];
// }

// add new sample at the end
onsetDF[onsetDFBufferSize-1] = newSample;
//onsetDF[onsetDFBufferSize-1] = newSample;
onsetDF.addSampleToEnd(newSample);

// update cumulative score
updateCumulativeScore(newSample);
Expand Down Expand Up @@ -366,35 +368,37 @@ void BTrack::doNotFixTempo()
void BTrack::resampleOnsetDetectionFunction()
{
float output[512];


float input[onsetDFBufferSize];

for (int i = 0;i < onsetDFBufferSize;i++)
{
input[i] = (float) onsetDF[i];
}
double src_ratio = 512.0/((double) onsetDFBufferSize);
int BUFFER_LEN = onsetDFBufferSize;
int output_len;
SRC_DATA src_data ;
//output_len = (int) floor (((double) BUFFER_LEN) * src_ratio) ;
output_len = 512;
src_data.data_in = input;
src_data.input_frames = BUFFER_LEN;
src_data.src_ratio = src_ratio;
src_data.data_out = output;
src_data.output_frames = output_len;
src_simple (&src_data, SRC_SINC_BEST_QUALITY, 1);
for (int i = 0;i < output_len;i++)
{
resampledOnsetDF[i] = (double) src_data.data_out[i];
}
double src_ratio = 512.0/((double) onsetDFBufferSize);
int BUFFER_LEN = onsetDFBufferSize;
int output_len;
SRC_DATA src_data ;
//output_len = (int) floor (((double) BUFFER_LEN) * src_ratio) ;
output_len = 512;
src_data.data_in = input;
src_data.input_frames = BUFFER_LEN;
src_data.src_ratio = src_ratio;
src_data.data_out = output;
src_data.output_frames = output_len;
src_simple (&src_data, SRC_SINC_BEST_QUALITY, 1);
for (int i = 0;i < output_len;i++)
{
resampledOnsetDF[i] = (double) src_data.data_out[i];
}
}

//=======================================================================
Expand Down Expand Up @@ -682,16 +686,20 @@ void BTrack::updateCumulativeScore(double odfSample)
}


// shift cumulative score back one
for (int i = 0;i < (onsetDFBufferSize-1);i++)
{
cumulativeScore[i] = cumulativeScore[i+1];
}
// // shift cumulative score back one
// for (int i = 0;i < (onsetDFBufferSize-1);i++)
// {
// cumulativeScore[i] = cumulativeScore[i+1];
// }

latestCumulativeScoreValue = ((1-alpha)*odfSample) + (alpha*max);

cumulativeScore.addSampleToEnd(latestCumulativeScoreValue);

// add new value to cumulative score
cumulativeScore[onsetDFBufferSize-1] = ((1-alpha)*odfSample) + (alpha*max);
//cumulativeScore[onsetDFBufferSize-1] = ((1-alpha)*odfSample) + (alpha*max);

latestCumulativeScoreValue = cumulativeScore[onsetDFBufferSize-1];
//latestCumulativeScoreValue = cumulativeScore[onsetDFBufferSize-1];

}

Expand Down
5 changes: 3 additions & 2 deletions src/BTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define __BTRACK_H

#include "OnsetDetectionFunction.h"
#include "CircularBuffer.h"
#include <vector>

//=======================================================================
Expand Down Expand Up @@ -184,8 +185,8 @@ class BTrack {
//=======================================================================
// buffers

std::vector<double> onsetDF; /**< to hold onset detection function */
std::vector<double> cumulativeScore; /**< to hold cumulative score */
CircularBuffer onsetDF; /**< to hold onset detection function */
CircularBuffer cumulativeScore; /**< to hold cumulative score */

double resampledOnsetDF[512]; /**< to hold resampled detection function */

Expand Down
9 changes: 9 additions & 0 deletions src/CircularBuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// CircularBuffer.cpp
// BTrack Tests
//
// Created by Adam Stark on 04/02/2016.
// Copyright © 2016 Adam Stark. All rights reserved.
//

#include "CircularBuffer.h"
46 changes: 46 additions & 0 deletions src/CircularBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// CircularBuffer.hpp
// BTrack Tests
//
// Created by Adam Stark on 04/02/2016.
// Copyright © 2016 Adam Stark. All rights reserved.
//

#ifndef CircularBuffer_h
#define CircularBuffer_h

#include <vector>

class CircularBuffer
{
public:
CircularBuffer() : writeIndex(0)
{

}

double &operator[](int i)
{
int index = (i + writeIndex) % buffer.size();
return buffer[index];
}

void addSampleToEnd (double v)
{
buffer[writeIndex] = v;
writeIndex = (writeIndex + 1) % buffer.size();
}

void resize(int size)
{
buffer.resize(size);
writeIndex = 0;
}

private:

std::vector<double> buffer;
int writeIndex;
};

#endif /* CircularBuffer_hpp */
6 changes: 6 additions & 0 deletions unit-tests/BTrack Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
E38214F2188E7AED00DDD7C8 /* BTrack_Tests.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = E38214F1188E7AED00DDD7C8 /* BTrack_Tests.1 */; };
E3A45DB9188E7BCD00B48CE4 /* BTrack.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E3A45DB5188E7BCD00B48CE4 /* BTrack.cpp */; };
E3A45DBA188E7BCD00B48CE4 /* OnsetDetectionFunction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E3A45DB7188E7BCD00B48CE4 /* OnsetDetectionFunction.cpp */; };
E3A5E1DA1C63CE83007A17B0 /* CircularBuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E3A5E1D81C63CE83007A17B0 /* CircularBuffer.cpp */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -36,6 +37,8 @@
E3A45DB6188E7BCD00B48CE4 /* BTrack.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BTrack.h; sourceTree = "<group>"; };
E3A45DB7188E7BCD00B48CE4 /* OnsetDetectionFunction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = OnsetDetectionFunction.cpp; sourceTree = "<group>"; };
E3A45DB8188E7BCD00B48CE4 /* OnsetDetectionFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OnsetDetectionFunction.h; sourceTree = "<group>"; };
E3A5E1D81C63CE83007A17B0 /* CircularBuffer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CircularBuffer.cpp; sourceTree = "<group>"; };
E3A5E1D91C63CE83007A17B0 /* CircularBuffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CircularBuffer.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -92,6 +95,8 @@
E3A45DB6188E7BCD00B48CE4 /* BTrack.h */,
E3A45DB7188E7BCD00B48CE4 /* OnsetDetectionFunction.cpp */,
E3A45DB8188E7BCD00B48CE4 /* OnsetDetectionFunction.h */,
E3A5E1D81C63CE83007A17B0 /* CircularBuffer.cpp */,
E3A5E1D91C63CE83007A17B0 /* CircularBuffer.h */,
);
name = src;
path = ../../src;
Expand Down Expand Up @@ -149,6 +154,7 @@
buildActionMask = 2147483647;
files = (
E31C50041891302D006530ED /* Test_BTrack.cpp in Sources */,
E3A5E1DA1C63CE83007A17B0 /* CircularBuffer.cpp in Sources */,
E3A45DBA188E7BCD00B48CE4 /* OnsetDetectionFunction.cpp in Sources */,
E3A45DB9188E7BCD00B48CE4 /* BTrack.cpp in Sources */,
E38214F0188E7AED00DDD7C8 /* main.cpp in Sources */,
Expand Down
22 changes: 21 additions & 1 deletion unit-tests/BTrack Tests/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE BTrackTests
#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test.hpp>

//#include "CircularBuffer.h"
//#include <iostream>
//
//int main()
//{
// CircularBuffer buffer;
//
// buffer.resize(10);
//
// buffer.addSampleToEnd(10);
// buffer.addSampleToEnd(8);
//
// for (int i = 0;i < 10;i++)
// {
// std::cout << buffer[i] << std::endl;
// }
//
// return 0;
//}

0 comments on commit ed96a61

Please sign in to comment.