-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCore.pde
494 lines (409 loc) · 11.2 KB
/
Core.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
A tool to create visuals reacting on an audiofile (c) Jan den Besten.
*/
// Global settings
boolean debug = false;
boolean doAnalyze = false;
float smoothingFactorUp = 0.8;
float smoothingFactorDown = 0.1;
// Lucida Font
PFont font;
// Audio libraries and globals
import ddf.minim.analysis.*;
import ddf.minim.*;
Minim minim;
AudioPlayer player;
FFT fft;
BeatDetect beat;
AudioAnalyzer analyzer;
boolean isPlaying = false;
/*
Shows a debugbar:
- Name and time
- Active scenes
- Waveform
*/
void drawDebugBar() {
int barHeight = 50;
noStroke();
fill(0,0,0,50);
rect(0,0,width,barHeight);
fill(0,255,0);
textSize(12);
text( audioFile + " - " + timeFormat(timePlayed()) + " / " + timeFormat(player.length()), 10, barHeight/2 );
// Scenes
String activeScenes = "Scenes: ";
for( int s=0; s<scenes.length; s++) {
if (scenes[s].isActive()) {
activeScenes += scenes[s].name +"["+percentageFormat(scenes[s].durationPercentage())+"]" + ", ";
}
}
text( activeScenes, width/3, barHeight/2 );
if (analyzer.isNormalizing()) {
text( "ANALYZING", width*2/3, barHeight/2 );
}
// draw a line to show where in the song playback is currently located
float posx = map(player.position(), 0, player.length(), 0, width);
strokeWeight(1);
stroke(255,0,0);
line(posx, 0, posx, barHeight-1);
stroke(255,255,255);
analyzer.drawWaveformsRect(0,0,width,barHeight);
}
void drawPauseButton() {
textFont(font);
textAlign(CENTER);
fill(110,110,110);
text( "press SPACEBAR to start", width/2, height/2 );
}
/*
When in debugmode, reacts on left and right cursor keys for skipping through audio
*/
void keyPressed()
{
if (debug) {
if (key==CODED) {
if (keyCode == LEFT) {
player.skip(-10000);
} else if (keyCode == RIGHT) {
player.skip(10000);
}
}
else {
if ( player.position() >= player.length() )
{
player.pause();
}
else {
if ( player.isPlaying() )
{
player.pause();
}
else
{
player.play();
}
}
}
}
if (startWithPauzeButton && !isPlaying) {
if (key==' ') {
startWithPauzeButton = false;
resetBackground(true);
player.play();
isPlaying = true;
}
}
}
/*
Returns time passed in milliseconds
*/
int timePlayed() {
return player.position();
}
/*
Returns time left in milliseconds
*/
int timeLeft() {
return player.length() - player.position();
}
/*
Format milliseconds in a nice string format
*/
String timeFormat(int millis) {
int seconds = millis / 1000;
int minutes = seconds / 60;
seconds -= minutes*60;
millis -= (minutes*60) + (seconds*1000);
return minutes +":"+ intFormat(seconds,2,"0");// +"."+ intFormat(millis,4,"0");
}
/*
Format a number with a fixed length and prefixed characters (001 instead of 1 for example)
*/
String intFormat(int value, int length, String ch) {
String format = "";
if (length>3 && value<1000) {
format += ch;
}
if (length>2 && value<100) {
format += ch;
}
if (length>1 && value<10) {
format += ch;
}
return format + value;
}
/*
Format a float (0-100) in a percentage string (45%)
*/
String percentageFormat(float value) {
return intFormat(int(value),2,"0") + "%";
}
// ================== SCENES ======================
/*
Iterates to all scenes and call setup methods
*/
void setupScenes() {
println();
println("==== Initializing. Audio file: `",audioFile,"` length: ",player.length());
println();
for( int s=0; s<scenes.length; s++) {
scenes[s].initialize();
scenes[s].setup();
}
}
/*
Iterates to all scenes, check if a scene is active and call draw method
*/
void drawScenes() {
for( int s=0; s<scenes.length; s++) {
if (scenes[s].isActive()) {
scenes[s].draw();
}
}
}
/*
SceneTime Class
A type to define the active range of a scene
*/
class SceneTime {
// Time in milliseconds from start of the audio when the scene starts. A value of 0 is start of audio. Can be negative, in that case the starttime is calculated from the end of the auidio.
public int start;
// Duration time in milliseconds of the scene. A value of 0 is calculated and set to a druation from startTime to of audio. StartTime + duration cannot exceed lenght of audio.
public int duration;
SceneTime(int start, int duration) {
this.start = start;
this.duration = duration;
}
}
/*
Scene Class
User scenes inherit from this class.
*/
class Scene {
// A user defined name, for debugging options
public String name;
private SceneTime[] times;
private int activeTime = 0;
private boolean activated = false;
/*
Constructor
*/
Scene(String name, SceneTime[] times) {
this.name = name;
this.times = times;
}
/*
Initialize method is called once at the start of the script.
Calculate startTime and duration (if set zero of negative)
*/
private void initialize() {
for(int t=0; t<times.length; t++) {
SceneTime time = times[t];
if (time.start<0) {
time.start = player.length() - abs(time.start);
}
if (time.duration<=0) {
time.duration = player.length() - abs(time.duration) - time.start;
}
println( this.name, ".", t ," - ", time.start, ":", time.duration );
}
}
/*
Setup method is called once at the start of the script. Initilize your're scene here.
*/
public void setup() {
}
/*
Draw method is called every draw iteration of the sketch. Draw you're scene here.
*/
public void draw() {
}
/*
Test if scene is active (needs to be drawn)
*/
public boolean isActive() {
if (activeTime<times.length) {
if (timePlayed()>=times[activeTime].start && timePlayed()<(times[activeTime].start+times[activeTime].duration) ) {
activated = true;
return true;
}
}
if (activated) {
activated = false;
activeTime++;
}
return false;
}
/*
Returns the duration of the scene (from the start of the scene) in milliseconds.
*/
public int durationMillis() {
return timePlayed() - times[activeTime].start;
}
/*
Return the duration of the scene as a percentage of the length of the scene. From 0.0 to 100.0.
*/
public float durationPercentage() {
return (float)durationMillis()*100 / (float)times[activeTime].duration;
}
}
/*
Audio Analyzer Class
Handles analizing, normalizing and smoothing different audio spectrums.
With tools to show waveform and equalizer.
Writes normalize data to a file after first run. And uses this data to normalize the audio spectrums for more usefull equalizer visualisations.
*/
class AudioAnalyzer {
int bands = 0;
float volume = 0.0;
float smoothVolume = 0.0;
float[] volumeSpectrum;
float[] smoothSpectrum;
float[] maxSpectrum;
float[] rmsSpectrum;
float[] normalizedFactor;
float[] maxedFactor;
long rmsSamples = 0;
boolean isNormalizing = false;
AudioAnalyzer() {
fft = new FFT( player.bufferSize(), player.sampleRate() );
fft.logAverages( 33, 1 );
bands = fft.avgSize();
volumeSpectrum = new float[bands];
smoothSpectrum = new float[bands];
maxSpectrum = new float[bands];
rmsSpectrum = new float[bands];
maxedFactor = new float[bands];
normalizedFactor = new float[bands];
for(int i = 0; i < bands; i++){
smoothSpectrum[i] = 0.0;
maxSpectrum[i] = 0.0;
rmsSpectrum[i] = 0.0;
maxedFactor[i] = 1.0;
normalizedFactor[i] = 1.0;
}
loadNormalizeData();
}
void analyze() {
volume = player.mix.level();
// Smoothing
if (volume>smoothVolume) {
smoothVolume += (volume - smoothVolume) * smoothingFactorUp;
}
else {
smoothVolume += (volume - smoothVolume) * smoothingFactorDown;
}
// FFT
fft.forward( player.mix );
for(int i = 0; i < bands; i++){
volumeSpectrum[i] = fft.getAvg(i);
// analyze
if (isNormalizing) {
if (volumeSpectrum[i] > maxSpectrum[i]) { maxSpectrum[i]=volumeSpectrum[i]; }
rmsSpectrum[i] += volumeSpectrum[i] * volumeSpectrum[i];
rmsSamples++;
}
else {
// Factor
if (volumeSpectrum[i]>=(rmsSpectrum[i]/2) ) {
volumeSpectrum[i] = volumeSpectrum[i] * maxedFactor[i];
}
else {
volumeSpectrum[i] = volumeSpectrum[i] * normalizedFactor[i];
}
}
// Smoothing
if (volumeSpectrum[i]>smoothSpectrum[i]) {
smoothSpectrum[i] += (volumeSpectrum[i] - smoothSpectrum[i]) * smoothingFactorUp;
}
else {
smoothSpectrum[i] += (volumeSpectrum[i] - smoothSpectrum[i]) * smoothingFactorDown;
}
}
}
boolean isNormalizing() {
return isNormalizing;
}
float getVolume() {
return volume;
}
float getVolumeSmooth() {
return smoothVolume;
}
float getSpectrumBand(int band) {
return volumeSpectrum[band];
}
float getSpectrumBandSmooth(int band) {
return smoothSpectrum[band];
}
void loadNormalizeData() {
if (doAnalyze) {
File file = dataFile(getNormalizeFilename());
isNormalizing = !file.isFile();
if (isNormalizing){
Table table;
table = loadTable("data/"+getNormalizeFilename(), "header");
if ( table.getRowCount() > 0 ) {
println(table.getRowCount() + " total rows in table");
for (TableRow row : table.rows()) {
int band = row.getInt("band");
float max = row.getFloat("max");
float rms = row.getFloat("rms");
rmsSpectrum[band] = rms;
maxedFactor[band] = 1/max;
normalizedFactor[band] = 1/rms;
// println("Band ",band," => ",max,rmsSpectrum[band],normalizedFactor[band],maxedFactor[band]);
}
}
}
else {
println("No normalize data => Analyzing now...");
}
}
}
void saveNormalizeData() {
float rmsFactor = 0.0;
Table table = new Table();
table.addColumn("band");
table.addColumn("max");
table.addColumn("rms");
for(int i = 0; i < bands; i++) {
rmsFactor = sqrt(rmsSpectrum[i]/rmsSamples);
TableRow newRow = table.addRow();
newRow.setInt("band", i);
newRow.setFloat("max", maxSpectrum[i]);
newRow.setFloat("rms", rmsFactor);
}
saveTable(table, "data/"+getNormalizeFilename());
println("Saved analyse");
}
String getNormalizeFilename() {
return "fft_analyze_"+bands+".csv";
}
void drawEqualizer(int x, int y, int w, int h) {
stroke(255,0,0);
int bandwidth = width/bands;
for(int i = 0; i < bands; i++)
{
noFill();
stroke(0,0,0);
strokeWeight(1);
rect( i*bandwidth, 0, bandwidth, height);
fill(255,0,0);
rect( i*bandwidth, height, bandwidth, -height * smoothSpectrum[i] );
}
}
void drawWaveformsRect(int x, int y, int w, int h) {
int y1 = y + h/4;
int y2 = y + h*3/4;
int yh = h/2;
for(int i = 0; i < player.bufferSize() - 1; i++)
{
float x1 = map( i, 0, player.bufferSize(), x, w );
float x2 = map( i+1, 0, player.bufferSize(), x, w );
line( x1, y1 + player.left.get(i)*yh, x2, y1 + player.left.get(i+1)*yh );
line( x1, y2 + player.right.get(i)*yh, x2, y2 + player.right.get(i+1)*yh );
}
}
}