-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitializeBeatVariables.c
More file actions
executable file
·193 lines (153 loc) · 4.87 KB
/
InitializeBeatVariables.c
File metadata and controls
executable file
·193 lines (153 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "InitializeBeatVariables.h"
#include "math.h"
#include "BeatTrackerMath.h"
#define PI (3.14159265)
void InitializeBeatVariables(void) {
//Initialize variables used throughout the tracking application
//First, declare local variables
//Variables for triangle filterbanks
float centerFreq[numSubbands];
float peakIndexPositions[numSubbands+1];
int firstSubbandPeak = 200; //Start at 200 Hz. Declare variables indicating the location of the peak centers
//Variables for tempo calculation
int sigma=20;
int tempovals[42];
int index;
//Now, initialize variables
//Initialize frame variables
fStart=1;
fHop = frameSize; //Use a full hop
fEnd = frameSize;
fNum = 1;
//Initialize the triangle filterbanks
for (i=1;i<8;i++) {
centerFreq[i] = firstSubbandPeak*pow(2,i-1); //Define each peak as being at twice the frequency of the previous one
peakIndexPositions[i] = round(centerFreq[i]/(fs/frameSize)); //Convert frequency to the index of the frame spectrums
}
centerFreq[0]=0;
peakIndexPositions[0]=0;
peakIndexPositions[numSubbands]=frameSize/2-1; //Define the first and last peaks
for (i=0;i<frameSize;i++) {
for (j=0;j<numSubbands;j++) {
triangleSubbandFilters[i][j]=0; //Initialize subbands to 0
}
}
for (j=0;j<numSubbands;j++) {
if (j==0) {
MakeSubbandFilter(triangleSubbandFilters,j,0,(int) peakIndexPositions[j],(int)peakIndexPositions[j+1],0,1,0,0);
}
else {
if (j<(numSubbands-1)) {
MakeSubbandFilter(triangleSubbandFilters,j,(int)peakIndexPositions[j-1],(int)peakIndexPositions[j],(int)peakIndexPositions[j+1],1,1,0,0);
}
else {
MakeSubbandFilter(triangleSubbandFilters,j,(int)peakIndexPositions[j-1],(int)peakIndexPositions[j],(int)peakIndexPositions[j+1],1,1,0,.22);
}
}
}
//Initialize the subband history buffers and variables that will be used to weight the subbands
for (i=0;i<autoCorrSize;i++) {
for (j=0;j<numSubbands;j++) {
onsetFunction[i][j]=0;
}
}
for (i=0;i<numSubbands;i++) {
autoCorrRatio[i]=1;
currentSubbandWeights[i]=0;
prevEnergy[i]=0;
curEnergy[i]=0;
}
for (i=0;i<30000;i++) {
weightHistory[i]= 0;
}
reweightTempoAndSubband=1;
framesUntilWeightingDone = autoCorrSize;
weights[0]=1;
weights[1] = (double) 5/9;
weights[2] = (double) 5/14;
weights[3] = (double) 5/29;
weights[4] = (double) 5/57;
weights[5] = (double) 5/114;
weights[6] = (double) 5/228;
weights[7] = (double) 5/456; //Define the weights that will be used for the subbands
weightThreshold=-.2;
weightHistoryIndex=0;
//Define variables that will be used to weight the tempo values
for (i=0;i<numPDFs;i++) {
tempoPDFCenters[i]=80+i*20; //From 80 to 160 BPM in increments of 20 BPM
}
tempoPDFChoice=3;
bPMDelayConversion = (float) fs*60/(fEnd-fStart);
twoFiftyPoint = ceil(bPMDelayConversion/250);
//printf("Two Fifty Point: %i \n",twoFiftyPoint);
fiftyPoint = floor(bPMDelayConversion/50);
for (i=twoFiftyPoint;i<=fiftyPoint;i++) {
index = i-twoFiftyPoint;
tempovals[index] = ceil(bPMDelayConversion/i);
}
for (i=0;i<5;i++) {
for (j=0;j<42;j++) {
pDF[i][j] = sqrt(1/(2.0*PI*sigma*sigma))*exp(-1/(sigma*sigma*2.0)*(tempovals[j]-tempoPDFCenters[i])*(tempovals[j]-tempoPDFCenters[i])); //Calculate the PDFs
}
}
for (i=0;i<42;i++) {
TempoHold2[i]=0;
}
//Initialize variables that will be used to find the beat locations
frameEnergy=0;
energyThreshold=.8;
shiftVec[0]=0;
shiftVec[1]=1;
shiftVec[2]=1;
shiftVec[3]=1;
for (i=0;i<1000;i++) {
beatsDetected[i]=0;
beatsPredicted[i]=0;
beatCon[i]=0;
}
beatPredicted[0] = 0;
//Initialize variables that will be used to detect beat patterns
for (i=0;i<5;i++) {
beatHistory_Pattern[i]=0;
for (j=0;j<5;j++) {
beatHistory_Tempo[i][j] = 0;
}
}
beatSpacing=0;
lastBeat=0;
//Initialize variables that will be used to detect double beating
doubleBeatDetect_Beats=0;
doubleBeatDetect_Pattern=0;
for (i=0;i<5;i++) {
doubleBeatDetect_Tempo[i]=0;
}
minBeatSpacing=16;
//Initialize FFT twiddle factors
computeTwiddleFactors(frameTwid, frameSize, 1);
computeTwiddleFactors(autoTwid, autoCorrSize*2, 1);
computeTwiddleFactors(invAutoTwid,autoCorrSize*2, -1);
Confidence = 0;
for (i=0;i<autoCorrSize+1;i++) {
diffFrameEnergy[i]=0;
}
}
void MakeSubbandFilter(float triFilterBank[][8],int band,int startPosition,int peakPosition,int endPosition,int firstHalf,int secondHalf,float startMag,float endMag) {
//Calculates the triangle filters used to separate the audio into subbands
int counter;
if (firstHalf==1) {
double ratio = (double) (1-startMag)/(peakPosition-startPosition);
counter=0;
for (i=startPosition;i<=peakPosition;i++) {
triFilterBank[i][band] = counter*ratio;
counter++;
}
}
if (secondHalf==1) {
double ratio = (double) (1-endMag)/(endPosition-peakPosition);
counter=0;
for (i=peakPosition;i<=endPosition;i++) {
triFilterBank[i][band] = 1-counter*ratio;
counter++;
}
}
}