andrew@0
|
1 /*
|
andrew@0
|
2 * PreciseOnsetLocator.cpp
|
andrew@0
|
3 * peakOnsetDetector
|
andrew@0
|
4 *
|
andrew@2
|
5 * Created by Andrew Robertson on 21/09/2012.
|
andrew@0
|
6 * Copyright 2012 QMUL. All rights reserved.
|
andrew@0
|
7 *
|
andrew@0
|
8 */
|
andrew@0
|
9
|
andrew@0
|
10 #include "PreciseOnsetLocator.h"
|
andrew@0
|
11
|
andrew@4
|
12 const bool printingOn = false;//true;
|
andrew@2
|
13
|
andrew@0
|
14 PreciseOnsetLocator::PreciseOnsetLocator(){
|
andrew@0
|
15 setup(512);
|
andrew@0
|
16 }
|
andrew@0
|
17
|
andrew@0
|
18 PreciseOnsetLocator::~PreciseOnsetLocator(){
|
andrew@0
|
19 onsetSamples.clear();
|
andrew@0
|
20 recentBufferSamples.clear();
|
andrew@0
|
21 }
|
andrew@0
|
22
|
andrew@0
|
23
|
andrew@0
|
24 void PreciseOnsetLocator::setup(const int& size){
|
andrew@0
|
25 onsetSamples.clear();
|
andrew@0
|
26 recentBufferSamples.clear();
|
andrew@0
|
27 bufferSize = size;
|
andrew@0
|
28 onsetSamples.assign(bufferSize, 0.0);
|
andrew@0
|
29 recentBufferSamples.assign(bufferSize, 0.0);
|
andrew@2
|
30 if (printingOn)
|
andrew@2
|
31 printf("Precise onset locator - using %i samples as framesize\n", size);
|
andrew@0
|
32 }
|
andrew@0
|
33
|
andrew@0
|
34 void PreciseOnsetLocator::storeSamples(double* newSamples){
|
andrew@0
|
35
|
andrew@0
|
36 for (int i = 0;i < bufferSize;i++)
|
andrew@0
|
37 recentBufferSamples[i] = newSamples[i];
|
andrew@0
|
38
|
andrew@0
|
39 }
|
andrew@0
|
40
|
Venetian@8
|
41 int PreciseOnsetLocator::findExactOnset(float* frame){
|
Venetian@8
|
42 //store the samples - mainly for viewing actually
|
Venetian@8
|
43 onsetSamples.clear();
|
Venetian@8
|
44 for (int i = 0; i < bufferSize;i++){
|
Venetian@8
|
45 onsetSamples.push_back(frame[i]);
|
Venetian@8
|
46 }
|
Venetian@8
|
47 return findExactOnset();
|
Venetian@8
|
48 }
|
Venetian@8
|
49
|
andrew@0
|
50 int PreciseOnsetLocator::findExactOnset(double* frame){
|
andrew@0
|
51 //store the samples - mainly for viewing actually
|
andrew@0
|
52 onsetSamples.clear();
|
andrew@2
|
53 for (int i = 0; i < bufferSize;i++){
|
andrew@0
|
54 onsetSamples.push_back(frame[i]);
|
andrew@0
|
55 }
|
Venetian@8
|
56 return findExactOnset();
|
Venetian@8
|
57 }
|
Venetian@8
|
58
|
Venetian@8
|
59 int PreciseOnsetLocator::findExactOnset(){
|
andrew@0
|
60 double energySum = 0;
|
andrew@0
|
61 double lastEnergySum, hopsizeLastEnergySum;
|
andrew@0
|
62 double energyDifference;
|
andrew@0
|
63 int bestEnergyIndex = 0;
|
andrew@0
|
64 double bestEnergyDifference = 0;
|
andrew@0
|
65 int endIndex = bufferSize;
|
andrew@0
|
66 int hopSize;
|
andrew@0
|
67
|
andrew@2
|
68 for (int resolution = bufferSize/2; resolution > 1; resolution/=2){
|
andrew@2
|
69 if (printingOn)
|
andrew@2
|
70 printf("resolution %i\n", resolution);
|
andrew@0
|
71
|
andrew@0
|
72 bestEnergyDifference = 0;
|
andrew@0
|
73 // printf("previous energy %f", lastEnergySum);
|
andrew@0
|
74 //initialise last energySum
|
andrew@0
|
75 hopSize = resolution/2;
|
andrew@0
|
76
|
andrew@0
|
77 lastEnergySum = getLastEnergySum(bestEnergyIndex, resolution);
|
andrew@0
|
78 hopsizeLastEnergySum = getLastEnergySum(bestEnergyIndex + hopSize, resolution);
|
andrew@0
|
79
|
andrew@0
|
80 for (int startIndex = bestEnergyIndex;startIndex + resolution <= endIndex;startIndex += hopSize){
|
andrew@2
|
81 if (printingOn)
|
andrew@2
|
82 printf("index %i last energy %f hop energy %f ", startIndex, lastEnergySum, hopsizeLastEnergySum);
|
andrew@0
|
83
|
andrew@0
|
84 //sum the energy for this new frame
|
andrew@0
|
85 energySum = 0;
|
andrew@0
|
86 for (int i = 0;i < resolution;i++){
|
andrew@0
|
87 energySum += onsetSamples[startIndex + i] * onsetSamples[startIndex + i];
|
andrew@0
|
88 }
|
andrew@0
|
89
|
andrew@2
|
90 if (printingOn)
|
andrew@2
|
91 printf("energysum %f\n", energySum);
|
andrew@0
|
92 //check if new max difference
|
andrew@0
|
93 energyDifference = energySum - lastEnergySum;
|
andrew@0
|
94 if (energyDifference > bestEnergyDifference){
|
andrew@0
|
95 bestEnergyDifference = energyDifference;
|
andrew@0
|
96 bestEnergyIndex = startIndex;
|
andrew@0
|
97 }
|
andrew@0
|
98
|
andrew@0
|
99 //store the values for checking in two loops time (because proceeding at resolution/2 each step)
|
andrew@0
|
100 //eg 0_to_128 compared to -128_to_0, 64_to_196 compared to -64_to_64, then 128_256 compared with 0_to_128,
|
andrew@0
|
101 lastEnergySum = hopsizeLastEnergySum;// energySum;
|
andrew@0
|
102 hopsizeLastEnergySum = energySum;
|
andrew@0
|
103
|
andrew@0
|
104 }
|
andrew@2
|
105 if (printingOn)
|
andrew@2
|
106 printf("winning index is %i\n", bestEnergyIndex);
|
andrew@0
|
107 endIndex = bestEnergyIndex + resolution;
|
andrew@0
|
108
|
andrew@0
|
109 }
|
andrew@2
|
110 if (printingOn)
|
andrew@2
|
111 printf("TOTAL WINNER %i\n", bestEnergyIndex);
|
andrew@0
|
112 return bestEnergyIndex;
|
andrew@0
|
113
|
andrew@0
|
114 }
|
andrew@0
|
115
|
andrew@0
|
116 double PreciseOnsetLocator::getLastEnergySum(const int& startIndex, const int& vectorSize){
|
andrew@0
|
117 double lastEnergySum = 0;
|
andrew@6
|
118 if (bufferSize + startIndex < recentBufferSamples.size()){
|
andrew@6
|
119 for (int i = startIndex - vectorSize;i < startIndex;i++){
|
andrew@6
|
120 if (i > 0)
|
andrew@6
|
121 lastEnergySum += onsetSamples[i] * onsetSamples[i];
|
andrew@6
|
122 else {
|
andrew@6
|
123 lastEnergySum += recentBufferSamples[bufferSize + i] * recentBufferSamples[bufferSize + i];
|
andrew@6
|
124 }
|
andrew@0
|
125 }
|
andrew@0
|
126 }
|
andrew@0
|
127 return lastEnergySum;
|
andrew@0
|
128
|
andrew@0
|
129 }
|