-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioHandler.pde
218 lines (190 loc) · 4.4 KB
/
AudioHandler.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import beads.*;
class AudioHandler
{
AudioContext ac;
//Add BeatDetector
PeakDetector od;
boolean beat;
boolean debug = false;
float[] volume, average;
int buffer;
int portAudio;
float[] maxima, normalized, smoothed;
AudioHandler(int numPorts, boolean debug)
{
this.debug = debug;
setup(numPorts);
}
AudioHandler(int numPorts)
{
setup(numPorts);
}
void setup(int numPorts)
{
portAudio = numPorts;
maxima = new float[portAudio];
volume = new float[portAudio];
average = new float[portAudio];
normalized = new float[portAudio];
smoothed = new float[portAudio];
for (int p = 0; p < portAudio; p++)
{
volume[p] = 0;
average[p] = 0;
maxima[p] = 100;
smoothed[p] = 0;
}
if (!debug)
{
println("starting audio...");
try
{
// ac = new AudioContext(AudioContext.defaultAudioFormat(portAudio));
ac = new AudioContext();
ac.out.setGain(100);
UGen inputs = ac.getAudioInput(new int[] {
0, 1, 2, 3, 4, 5, 6, 7
}
);
ac.out.addInput(inputs);
// Add BeatDetection for all inputs
ShortFrameSegmenter sfs = new ShortFrameSegmenter(ac);
sfs.setChunkSize(2048);
sfs.setHopSize(441);
sfs.addInput(ac.out);
FFT fft = new FFT();
PowerSpectrum ps = new PowerSpectrum();
sfs.addListener(fft);
fft.addListener(ps);
SpectralDifference sd = new SpectralDifference(ac.getSampleRate());
ps.addListener(sd);
od = new PeakDetector();
sd.addListener(od);
od.setThreshold(threshold);
od.setAlpha(alpha);
od.addMessageListener(
new Bead() {
protected void messageReceived(Bead b)
{
beat = true;
}
}
);
ac.out.addDependent(sfs);
ac.start();
println(ac.getBufferSize());
buffer = 6;
println("Number of INs : "+ ac.getAudioInput().getOuts());
println("Number of OUTs : "+ ac.out.getIns());
println("success!");
}
catch (Throwable e)
{
println(e);
debug = true;
}
}
}
void update()
{
if (!debug)
{
// Einstellungesslider werte für beatDetection
od.setThreshold(threshold);
od.setAlpha(alpha);
try
{
for (int p = 0; p < portAudio; p++)
{
volume[p] = 0;
}
for (int p = 0; p < portAudio; p++)
{
for (int i = 0; i < ac.getBufferSize (); i++)
{
volume[p] += abs(ac.out.getValue(p, i));
}
}
}
catch(Throwable e)
{
println("AudioHandler broken: " + e);
debug = true;
}
} else
{
for (int p = 0; p < portAudio; p++)
{
volume[p] = noise(p, frameCount*0.02 * p) * 100;
}
// debug "noise" beat
if (millis() % 300 < 20) {
beat = true;
} else {
beat = false;
}
}
for (int p = 0; p < portAudio; p++)
{
average[p] = ((average[p] * buffer) + volume[p])/(buffer + 1);
}
for (int i = 0; i < maxima.length; i++)
{
if (volume[i] != 0)
maxima[i] += (volume[i] - maxima[i])*0.05;
normalized[i] = constrain(map(volume[i], 0, maxima[i], 0.0, 1.0), 0.0, 1.0);
}
for (int i = 0; i < smoothed.length; i++)
{
smoothed[i] += (normalized[i] - smoothed[i])*0.1;
}
}
void drawInput()
{
fill(255);
noStroke();
float[] n = getSmoothed();
for (int i = 0; i < portAudio; i++)
{
text(i + " " + nfc(n[i], 2), 30, 14 + i * 35);
stroke(0);
noFill();
rect(30, 22 + i*35, 100, 15);
noStroke();
fill(255);
rect(30, 22 + i*35, 100 * n[i], 15);
}
// beat
fill(255);
if(beat == true){
rect(30, 350, 100, 15);
}
text("beat", 30, 340);
}
// volume values, as received by Beads library
float[] getVolume()
{
return volume;
}
// average volume, unnormalized, unsmoothed
float[] getAverage()
{
return average;
}
// normalized volume, unsmoothed
float[] getNormalized()
{
return normalized;
}
// smoothed, normalized volume
float[] getSmoothed()
{
return smoothed;
}
boolean getBeat()
{
boolean _beat = beat;
beat = false;
return _beat;
}
}