-
Notifications
You must be signed in to change notification settings - Fork 0
/
ana.pde
executable file
·159 lines (142 loc) · 4.21 KB
/
ana.pde
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
import ddf.minim.analysis.*;
import ddf.minim.*;
class BeatTrigger {
Minim minim;
AudioInput in;
FFT fft;
int bands = 256;
//float[] spectrum = new float[bands];
int bandsToAnalyze = 10;
int triggerAmount = 5;
int bandHistSize = 200;
float[][] bandHist = new float [bandsToAnalyze][bandHistSize];
float[] histSum = new float [bandsToAnalyze];
float[] histVarSum = new float [bandsToAnalyze];
float[] histAvg = new float [bandsToAnalyze];
float[] histVar = new float [bandsToAnalyze];
float[] histMax = new float [bandsToAnalyze];
int bandCutOff;
int upperBandLim;
Boolean[] triggers = new Boolean [triggerAmount];
float[] triggerBins = new float [triggerAmount];
float[][] triggerConfig = new float[triggerAmount][bandsToAnalyze];
int[] triggerConfigSums = new int[triggerAmount];
float[] sensitivity = new float [triggerAmount];
int sensCurrentSelection = 0;
float sensitivityResetVal = 1.8;
Boolean smoothing;
float smoothingFac;
BeatTrigger(PApplet p) {
minim = new Minim(p);
in = minim.getLineIn(Minim.STEREO, 1024);
fft = new FFT(in.bufferSize(), in.sampleRate());
for (int i = 0; i < bandsToAnalyze; i++)
{
this.histSum[i] = 0;
this.histVarSum[i] = 0;
this.histAvg[i] = 0;
this.histVar[i] = 0;
this.histMax[i] = 0;
for (int j = 0; j < bandHistSize; j++)
{
this.bandHist[i][j] = 0;
}
}
for (int i = 0; i < triggerAmount; i++)
{
this.triggers[i] = false;
this.triggerBins[i] = 0;
this.sensitivity[i] = sensitivityResetVal;
for (int j = 0; j < bandsToAnalyze; j++)
{
triggerConfig[i][j] = 0;
}
this.triggerConfigSums[i] = 0;
}
bandCutOff = 0;
smoothing = false;
smoothingFac = 0.2;
}
void setTriggerConfig(float[][] triggerArray) {
this.triggerConfig = triggerArray;
for (int i = 0; i < triggerAmount; i++)
{
triggerConfigSums[i] = 0;
for (int j = 0; j < bandsToAnalyze; j++)
{
triggerConfigSums[i] += triggerConfig[i][j];
}
}
}
void analyze() {
float A;
float sum, sumVar;
fft.forward(in.mix);
this.upperBandLim = this.bandHistSize - this.bandCutOff;
for (int i = 0; i < bandsToAnalyze; i++)
{
A = fft.getBand(i);
sum = 0;
sumVar = 0;
for (int j = upperBandLim-1; j > 0; j--)
{
this.bandHist[i][j] = this.bandHist[i][j-1];
sum += this.bandHist[i][j];
sumVar += pow(histAvg[i]-this.bandHist[i][j], 2);
}
for (int j = upperBandLim; j < bandHistSize; j++)
{
this.bandHist[i][j] = 0;
}
if (A > this.histAvg[i]*2) this.histMax[i] = this.histAvg[i]*3;
//forces re-maxing
//this.histMax[i] *= 0.99;
if (smoothing) this.bandHist[i][0] = this.bandHist[i][1] + smoothingFac*(A-this.bandHist[i][1]);
if (!smoothing) this.bandHist[i][0] = A;
this.histAvg[i] = sum/this.upperBandLim;
this.histVar[i] = sqrt(sumVar/this.upperBandLim);
triggerCollect();
triggerEvaluate();
}
}
Boolean [] getTriggers() {
return this.triggers;
}
void triggerEvaluate() {
for (int i = 0; i < triggerAmount; i++)
{
if (triggerBins[i] > sensitivity[i]) triggers[i] = true;
else triggers[i] = false;
}
}
void triggerCollect() {
for (int i = 0; i < triggerAmount; i++)
{
this.triggerBins[i] = 0;
for (int j = 0; j < bandsToAnalyze; j++)
{
//triggerBins[i] += triggerConfig[i][j] * max((fft.getBand(j)-(histAvg[j]+histVar[j]))/histMax[j], 0);
triggerBins[i] += triggerConfig[i][j] * max((fft.getBand(j)-(histAvg[j]+histVar[j])), 0);
}
//calculate average
triggerBins[i] = triggerBins[i]/triggerConfigSums[i];
}
}
void show() {
}
void stop()
{
minim.stop();
}
void sensMenu() {
textSize(10);
for (int i = 0; i < sensitivity.length; i++)
{
fill(255);
text(sensitivity[i], 10, 10 + 10 * i);
}
stroke(50, 200, 200);
noFill();
rect(10, 10 * sensCurrentSelection, 50, 10);
}
}