forked from skooter500/OOP-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudio1.java
More file actions
140 lines (117 loc) · 3.72 KB
/
Audio1.java
File metadata and controls
140 lines (117 loc) · 3.72 KB
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
package ie.tudublin;
import ddf.minim.AudioBuffer;
import ddf.minim.AudioInput;
import ddf.minim.AudioPlayer;
import ddf.minim.Minim;
import processing.core.PApplet;
public class Audio1 extends PApplet
{
Minim minim;
AudioPlayer ap;
AudioInput ai;
AudioBuffer ab;
int mode = 0;
float y = 0;
float smoothedY = 0;
float smoothedAmplitude = 0;
public void keyPressed() {
if (key >= '0' && key <= '9') {
mode = key - '0';
}
if (keyCode == ' ') {
if (ap.isPlaying()) {
ap.pause();
} else {
ap.rewind();
ap.play();
}
}
}
public void settings()
{
size(1024, 1000, P3D);
//fullScreen(P3D, SPAN);
}
public void setup()
{
minim = new Minim(this);
// Uncomment this to use the microphone
// ai = minim.getLineIn(Minim.MONO, width, 44100, 16);
// ab = ai.mix;
// And comment the next two lines out
ap = minim.loadFile("heroplanet.mp3", 1024);
ap.play();
ab = ap.mix;
colorMode(HSB);
y = height / 2;
smoothedY = y;
}
float off = 0;
float lerpedBuffer[] = new float[1024];
public void draw()
{
//background(0);
float halfH = height / 2;
float average = 0;
float sum = 0;
off += 1;
// Calculate sum and average of the samples
// Also lerp each element of buffer;
for(int i = 0 ; i < ab.size() ; i ++)
{
sum += abs(ab.get(i));
lerpedBuffer[i] = lerp(lerpedBuffer[i], ab.get(i), 0.1f);
}
average= sum / (float) ab.size();
smoothedAmplitude = lerp(smoothedAmplitude, average, 0.1f);
float cx = width / 2;
float cy = height / 2;
switch (mode) {
case 0:
background(0);
for(int i = 0 ; i < ab.size() ; i ++)
{
//float c = map(ab.get(i), -1, 1, 0, 255);
float c = map(i, 0, ab.size(), 0, 255);
stroke(c, 255, 255);
float f = lerpedBuffer[i] * halfH * 4.0f;
line(halfH + f, i, halfH - f, i);
}
break;
case 1:
background(0);
for(int i = 0 ; i < ab.size() ; i ++)
{
//float c = map(ab.get(i), -1, 1, 0, 255);
float c = map(i, 0, ab.size(), 0, 255);
stroke(c, 255, 255);
float f = lerpedBuffer[i] * halfH * 4.0f;
line(i, halfH + f, halfH - f, i);
}
break;
case 2:
background(0);
for(int i = 0 ; i < ab.size() ; i ++)
{
float c = map(i, 0, ab.size(), mouseX /2, mouseY/ 2);
stroke(c, 255, 255);
float f = lerpedBuffer[i] * halfH * 4.0f;
line(0, i, f, i);
line(width, i, width - f, i);
line(i, 0, i, f);
line(i, height, i, height - f);
}
break;
}
// Other examples we made in the class
/*
stroke(255);
fill(100, 255, 255);
circle(width / 2, halfH, lerpedA * 100);
circle(100, y, 50);
y += random(-10, 10);
smoothedY = lerp(smoothedY, y, 0.1f);
circle(200, smoothedY, 50);
*/
}
}