-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAudioDriver
204 lines (199 loc) · 5.59 KB
/
AudioDriver
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
import processing.core.PApplet;
import beads.*;
import org.jaudiolibs.beads.*;
import java.util.Arrays;
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import java.io.File;
String audioType = "file";
String pathName = "/Users/johnwaldo/Downloads/beep-01a.wav";
String desiredAction = "play";
AudioContext ac;
Frequency f;
Glide fGlide;
WavePlayer wPlayer;
PowerSpectrum ps;
float averageFrequency = 400.0;
color fore = color(255, 255, 255);
color back = color(0,0,0);
/*public AudioDriver(String at, String pn, String da) {
audioType = at;
pathName = pn;
desiredAction = da;
} */
void setup() {
if (audioType == "microphone" && desiredAction == "frequency") {
size(600,600);
ac = new AudioContext();
Gain g = new Gain(ac, 2, .01);
ac.out.addInput(g);
UGen micIn = ac.getAudioInput();
fGlide = new Glide(ac, 50, 10);
wPlayer = new WavePlayer(ac, fGlide, Buffer.SINE);
g.addInput(wPlayer);
ShortFrameSegmenter sfs = new ShortFrameSegmenter(ac);
sfs.addInput(micIn);
FFT fft = new FFT();
sfs.addListener(fft);
ps = new PowerSpectrum();
fft.addListener(ps);
f = new Frequency(44100.0f);
ps.addListener(f);
ac.out.addDependent(sfs);
ac.start();
}
else if (audioType == "file" && desiredAction == "play") {
frameRate(200);
size(400,400);
ac = new AudioContext();
File inputFile;
try {
inputFile = new File(pathName);
Noise n = new Noise(ac);
String audioFileName = inputFile.getAbsolutePath();
SamplePlayer player = new SamplePlayer(ac, SampleManager.sample(audioFileName));
Gain g = new Gain(ac, 2,2);
g.addInput(player);
ac.out.addInput(g);
ac.start();}
catch (Exception e) {System.out.println("Not a valid pathName"); }
}
else if (audioType == "file" && desiredAction == "compression") {
frameRate(200);
size(400,400);
ac = new AudioContext();
File inputFile;
try {
inputFile = new File(pathName);
Noise n = new Noise(ac);
String audioFileName = inputFile.getAbsolutePath();
SamplePlayer player = new SamplePlayer(ac, SampleManager.sample(audioFileName));
Gain g = new Gain(ac, 2,2);
g.addInput(player);
ac.out.addInput(g);
ac.start();}
catch (Exception e) {System.out.println("Not a valid pathName"); }
}
else if (audioType == "file" && desiredAction == "frequency") {
frameRate(200);
size(600,600);
ac = new AudioContext();
File inputFile;
try {
inputFile = new File(pathName);
Noise n = new Noise(ac);
String audioFileName = inputFile.getAbsolutePath();
SamplePlayer player = new SamplePlayer(ac, SampleManager.sample(audioFileName));
Gain g = new Gain(ac, 2,2);
g.addInput(player);
ac.out.addInput(g);
ShortFrameSegmenter sfs = new ShortFrameSegmenter(ac);
sfs.addInput(ac.out);
FFT fft = new FFT();
sfs.addListener(fft);
ps = new PowerSpectrum();
fft.addListener(ps);
ac.out.addDependent(sfs);
ac.start(); }
catch (Exception e) {System.out.println("Not a valid pathName"); }
}
}
void draw() {
if (audioType == "microphone" && desiredAction == "frequency") {
background(back);
stroke(fore);
if(ps == null) return;
float[] features = ps.getFeatures();
if(features != null) {
//scan across the pixels
for(int i = 0; i < width; i++) {
int featureIndex = i * features.length / width;
int vOffset = height - 1 - Math.min((int)(features[featureIndex] * height), height - 1);
line(i,height,i,vOffset);
}
}
}
else if (audioType == "file" && desiredAction == "play") {
loadPixels();
//set the background
Arrays.fill(pixels, back);
//scan across the pixels
double addVar = 1;
double multVar = 2;
for(int i = 0; i < width; i++) {
//for each pixel work out where in the current audio buffer we are
int buffIndex = i * ac.getBufferSize() / width;
//then work out the pixel height of the audio data at that point
float vOff = ac.out.getValue(0,buffIndex);
int vOffset = (int)((addVar + ac.out.getValue(0,buffIndex)) * height / multVar);
vOffset = min(vOffset, height);
try {
pixels[vOffset * height + i] = fore;
}
catch (Exception e) {
if (vOffset*height+i < pixels.length) {
multVar*= 1.75;
addVar*=1.75;
}
else {
multVar/= 1.75;
addVar/=1.75;
}
}
updatePixels();
}
}
else if (audioType == "file" && desiredAction == "compression") {
loadPixels();
//set the background
Arrays.fill(pixels, back);
//scan across the pixels
double multVar = 2;
double addVar = 1;
for(int i = 0; i < width; i++) {
//for each pixel work out where in the current audio buffer we are
int buffIndex = i * 4 / width;
//then work out the pixel height of the audio data at that point
float vOff = ac.out.getValue(0,buffIndex);
if (vOff < .15) {
ac.out.setGain(ac.out.getGain()+.01);
ac.out.setValue(ac.out.getValue(0,buffIndex)+.01);
}
else if(vOff > .25) {
ac.out.setGain(ac.out.getGain()-.01);
ac.out.setValue(ac.out.getValue(0,buffIndex)-.01);
}
int vOffset = (int)((addVar + ac.out.getValue(0,buffIndex)) * height / multVar);
vOffset = min(vOffset, height);
try {
pixels[vOffset * height + i] = fore;
}
catch (Exception e) {
if (vOffset*height+i < pixels.length) {
multVar*= 1.75;
addVar*=1.75;
}
else {
multVar/= 1.75;
addVar/=1.75;
}
}
}
updatePixels();
}
else if (audioType == "file" && desiredAction == "frequency") {
background(back);
stroke(fore);
if(ps == null) return;
float[] features = ps.getFeatures();
if(features != null) {
//scan across the pixels
for(int i = 0; i < width; i++) {
int featureIndex = i * features.length / width;
int vOffset = height - 1 - Math.min((int)(features[featureIndex] * height), height - 1);
line(i,height,i,vOffset);
}
}
}
}