-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundVisualShaderBase.pde
129 lines (119 loc) · 4 KB
/
SoundVisualShaderBase.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
void setup() {
//fullScreen(P2D);
size(640, 360, P2D);
//size(960, 540, P2D);
//size(1280, 720, P2D);
startTimeMillis = System.currentTimeMillis();
final JSONObject setting = loadJSONObject("setting.json");
framePerSecond = setting.getInt("framePerSecond");
msPerFrame = 1000.0 / framePerSecond;
println("ms per frame: " + msPerFrame + "ms");
frameRate(framePerSecond);
sounds = loadSounds(setting.getJSONArray("sounds"));
repeat = setting.getBoolean("repeat", false);
shaders = loadShaders(setting.getJSONArray("shaders"));
final JSONObject backgroundSetting = setting.getJSONObject("background");
if (backgroundSetting != null) {
final String clr = backgroundSetting.getString("color");
if (clr != null && 0 < clr.length()) {
backgroundColor = Integer.decode(clr);
}
final String image = backgroundSetting.getString("image");
if (image != null && 0 < image.length()) {
backgroundImage = loadImage(image);
}
initBackground();
}
final JSONObject outputSetting = setting.getJSONObject("out");
final JSONObject connection = outputSetting.getJSONObject("connection");
if (connection != null) {
final String senderName = connection.getString("name");
if (senderName != null && 0 < senderName.length()) {
println("initialize spout...");
spout = new Spout(this);
if (spout.createSender(senderName, width, height) == false) {
println("warning: fail to create spout");
spout = null;
}
}
}
recorder = createFrameRecorder(outputSetting);
}
void draw() {
if (provider == null || provider.isPlaying() == false) {
if (provider != null) {
++soundIndex;
if (soundIndex == sounds.size()) {
if (repeat == false) {
if (recorder != null) {
recorder.finish();
recorder = null;
}
if (0 < frameDropCount) {
println("Frame drop count: ", frameDropCount, " / ", frameCount, "(", (frameDropCount * 100.0 / frameCount) ,"%)");
}
exit();
return;
}
soundIndex = 0;
}
}
provider = new SoundDataProvider(this, msPerFrame, sounds.get(soundIndex).location);
provider.play();
}
provider.update();
final float progress = provider.getProgressPercentage();
if (startFrameCount == 0 && 0.0 < progress) {
println("Leading frame count: ", frameCount);
startFrameCount = frameCount;
}
if (onlyInitialization == false) {
initBackground();
}
final long frameTimeMillis = System.currentTimeMillis();
final float time = ((frameTimeMillis - startTimeMillis) / 1000.0);
for (ShaderInfo shaderInfo: shaders) {
if (shaderInfo.disabled == false) {
final PShader shader = shaderInfo.shader;
shader.set("time", time);
shader.set("progress", progress);
shader.set("mouse", mouseX / (float)width, (height - mouseY) / (float)height);
shader.set("soundLevel",
provider.getRightLevelValue(),
provider.getLeftLevelValue(),
provider.getMixLevelValue());
shader.set("kick", provider.getKickValue());
shader.set("hihat", provider.getHihatValue());
shader.set("snare", provider.getSnareValue());
filter(shader);
}
}
if (recorder != null) {
recorder.recordFrame();
}
if (spout != null) {
spout.sendTexture();
}
final long timeTaken = System.currentTimeMillis() - frameTimeMillis;
if ((1.0 / framePerSecond * 1000) < timeTaken) {
println("Overtime: " + timeTaken + "ms(" + frameCount + ")");
++frameDropCount;
}
}
void keyReleased() {
if (key != CODED) {
if (KeyEvent.VK_0 <= keyCode && keyCode <= KeyEvent.VK_9) {
final int index = KeyEvent.VK_0 < keyCode ? keyCode - KeyEvent.VK_1 : 10;
if (index < shaders.size()) {
ShaderInfo info = shaders.get(index);
info.disabled = !info.disabled;
}
} else {
switch (keyCode) {
case 5: // PrtSc
saveFrame("scene-########.jpg");
break;
}
}
}
}