forked from damellis/EncodeAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncodeAudio.pde
More file actions
61 lines (45 loc) · 1.41 KB
/
EncodeAudio.pde
File metadata and controls
61 lines (45 loc) · 1.41 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
import java.awt.datatransfer.*;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import ddf.minim.*;
int SAMPLES = 30000;
Minim minim;
AudioSample sample;
void setup()
{
size(512, 200);
String file = selectInput("Select audio file to encode.");
if (file == null) {
exit();
return;
}
try {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
minim = new Minim(this);
sample = minim.loadSample(file);
float[] samples = sample.getChannel(BufferedAudio.LEFT);
float maxval = 0;
for (int i = 0; i < samples.length; i++) {
if (abs(samples[i]) > maxval) maxval = samples[i];
}
int start;
for (start = 0; start < samples.length; start++) {
if (abs(samples[start]) / maxval > 0.01) break;
}
String result = "";
for (int i = start; i < samples.length && i - start < SAMPLES; i++) {
result += constrain(int(map(samples[i], -maxval, maxval, 0, 256)), 0, 255) + ", ";
}
clipboard.setContents(new StringSelection(result), null);
JOptionPane.showMessageDialog(null, "Audio data copied to the clipboard.", "Success!", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Maybe you didn't pick a valid audio file?\n" + e, "Error!", JOptionPane.ERROR_MESSAGE);
}
exit();
}
void stop()
{
sample.close();
minim.stop();
super.stop();
}