/
wave.pde
81 lines (68 loc) · 1.75 KB
/
wave.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
class Wave {
float maxLoudLess = 700;
float amp = 50;
int[] lines;
int linesCount = 4;
float loudLess;
color strokeColor;
int lengthPerLine = 1;
AudioPlayer song;
Wave(AudioPlayer _song) {
song = _song;
lengthPerLine = ceil(width / float(song.bufferSize()));
lengthPerLine = 2;
initLines();
if (theme == "black") {
strokeColor = black;
} else {
strokeColor = mineShaft;
}
}
void run() {
update();
render();
}
void update() {
loudLess = min(getLoudLess(), maxLoudLess);
}
void render() {
float alpha = min(254, loudLess / 500 * 255);
stroke(strokeColor, alpha);
// we draw the waveform by connecting neighbor values with a line
// we multiply each of the values by 50
// because the values in the buffers are normalized
// this means that they have values between -1 and 1.
// If we don't scale them up our waveform
// will look more or less like a straight line.
Boolean nowLeft;
AudioBuffer wave;
for(int i = 0; i < song.bufferSize() - 1; i++) {
nowLeft = true;
for (int line : lines) {
if (nowLeft) {
wave = song.left;
} else {
wave = song.right;
}
nowLeft = !nowLeft;
line(i * lengthPerLine, line + wave.get(i)*amp,
(i+1) * lengthPerLine, line + wave.get(i+1)*amp);
}
}
}
void initLines() {
lines = new int[linesCount];
for (int i = 0; i < linesCount; ++i) {
lines[i] = height * (i + 1) / (linesCount + 1);
}
}
float getLoudLess() {
float loudLess = 0;
AudioBuffer wave = song.mix;
int ilen = song.bufferSize();
for(int i = 0; i < ilen; i++) {
loudLess += abs(wave.get(i));
}
return loudLess;
}
}