-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbach_stress_test.js
executable file
·145 lines (118 loc) · 3.66 KB
/
bach_stress_test.js
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
/*
bach stress test
code: http://www.giovannibedetti.com/
illustration: https://www.behance.net/monocolo
README!
This is a stress test for Processing and Web Audio API.
The canvas is drawn using Processing and the sound is made with Web Audio API.
At mouse click the image will be divided in 4 images.
Also the audio file is split in 4 parts and these are played together. Parts 1 and 3 are played forward, while parts 2 and 4 are played backwards.
You'll see and hear 1, 4, 16, 64, 256, 1024 images and sounds together.
The theme is the famous J.S. Bach canon 1 à 2 from the "Musical Offering" (1747)
This piece is a "crab canon", so it can be played at the same time forward and backward.
See here (www2.nau.edu/tas3/MOcancrizans.pdf) for the full story.
This test was made just to find out if it can be combined in other ways.
Check out the magic of math.
Desktop only:
for your pleasure you can stop the sounds pressing 'p',
and enable/disable the random image change pressing 's'.
Warning: as I said, this is a stress test. Try it at your own risk! (your browser can hang a little bit)
If your browser supports Web Audio API, you will hear the full Bach theme on load. On iOS devices, this could not be true, because autoplay is disabled.
02/2015
*/
var maxRecursion = 5; //max subdivision of rects, change it at your own risk
var fullRect;
var allRects;
var imgCount = 8;
var images = [imgCount];
var still = false;
//how many divisions are done
var times = 0;
//the div containing this canvas
var sketchParent;
function preload(){
loadImages();
}
function windowResized() {
resizeCanvas(sketchParent.offsetWidth, sketchParent.offsetHeight);
stopSound(allRects.length);
initRects();
times = 0;
still = false;
createBuffers(1);
playAtIndex(0);
}
function setup() {
initAudio();
sketchParent = document.getElementById("sketch-holder");
var canvas = createCanvas(sketchParent.offsetWidth, sketchParent.offsetHeight);
canvas.parent('sketch-holder');
initRects();
textSize(12);
canvas.mousePressed(onMousePressed);
}
function draw() {
background(255);
for (var i = 0; i < allRects.length; i++) {
allRects[i].display();
}
//draw some info about the test
fill(0);
rect(0, height-20, width, height);
fill(255, 0, 0);
text(allRects.length+" buffer(s) playing, Processing running at "+nf(frameRate(), 2, 2)+" fps", width-300, height-5);
}
function onMousePressed() {/*mobile chrome fires mousePressed twice, wait for p5.js fix*/
if (times >= 0) {
still = true;
}
if (times < maxRecursion) {
stopSound(allRects.length);
divideRects();
times++;
createBuffers(allRects.length);
for (var i=0; i < allRects.length; i++) {
playAtIndex(i);
}
} else {
background(255);
stopSound(allRects.length);
initRects();
times = 0;
still = false;
createBuffers(1);
playAtIndex(0);
}
}
function keyPressed() {
if (key=='s'||key=='S') {
still = !still;
for (var i = 0; i < allRects.length; i++) {
allRects[i].setStill(still);
}
}
if (key=='p'||key=='P') {
stopSound(allRects.length);
}
}
function divideRects() {
var tempRects = [4];
var newRects = [allRects.length*4];
for (var i = 0; i < allRects.length; i++) {
tempRects = allRects[i].divide();
//copy tempRects to newRects
for (var j = 0; j < tempRects.length; j++) {
newRects[(i * 4) + j] = tempRects[j];
}
}
allRects = newRects;
}
function loadImages() {
for (var i=0; i<imgCount; i++) {
images[i] = loadImage("data/canone_V2_0"+i+".png");
}
}
function initRects() {
allRects = [1];
allRects[0] = new DrawingRect(images, 0, 0, width, height-20);
}