-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSoundAnalyzer.pde
107 lines (86 loc) · 1.96 KB
/
SoundAnalyzer.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
class SoundAnalyzer
{
int ellipseSize;
int baseSize = 10;
float tInc;
float tIncBase = 0.003;
float offSetSize;
boolean varyingSize;
boolean varyingSpeed;
boolean varyingOffset;
AudioPlayer player;
float avgPower;
float bassPower;
float amplifyFactor;
SoundAnalyzer(String fileName)
{
player = maxim.loadFile(fileName);
player.setAnalysing(true);
varyingOffset = false;
varyingSize = false;
varyingSpeed = true;
amplifyFactor = 0;
}
void updateVisuals()
{
player.play();
avgPower = player.getAveragePower();
avgPower = map(avgPower, 0.15, 0.3, 0, 1);
avgPower = constrain(avgPower,-1,2);
//println(avgPower);
int numSpectralBands = 0;
bassPower = 0;
float[] spec = player.getPowerSpectrum();
//println("spec length:"+spec.length);
for(int i = 0; i<(spec.length/50); i++)
{
numSpectralBands++;
bassPower += spec[i];
}
bassPower /= numSpectralBands;
bassPower = map(bassPower, 0.25, 0.5, 0, 1);
bassPower = constrain(bassPower,-1,2);
println("BASS:"+bassPower);
float largeEffectBassPower = map(bassPower, 0.3, 0.7, 0, 1);
largeEffectBassPower = constrain(largeEffectBassPower,-1,2);
bassPower = bassPower*(1-amplifyFactor)+largeEffectBassPower*amplifyFactor;
updateSize();
updateSpeed();
updateOffset();
}
void updateSize()
{
if (varyingSize)
{
ellipseSize = (int) (baseSize*bassPower + baseSize/2);
} else
{
ellipseSize = baseSize;
}
}
void updateSpeed()
{
if (varyingSpeed)
{
tInc = tIncBase*bassPower;
} else
{
tInc = tIncBase/3.0;
}
}
void updateOffset()
{
if (varyingOffset)
{
offSetSize = avgPower*0.05;
//println(offSetSize);
} else
{
offSetSize = 0;
}
}
int getOffset(float seed)
{
return (int)(200.0*(noise(seed+sound.offSetSize)-noise(seed)));
}
}