«Confetti2» by Henk Lasschuit

on 03 Apr'12 23:14 in chancenumer to pitchdistributionsound and image

Confetti2 is an old project that I wrote for my first toy computer, the Aquarius by Mattel. I liked the sound of it so much, that I rewrote it for every system that I worked with: Atari-PASCAL and a few BASIC-dialects on the Mac. Now I hope that Apple computers and SuperCollider are here to stay so I don't ever have to rewrite this again.

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
//Confetti 2
/*Imagine a box that is divided into smaller compartments. You start randomly throwing beads (or marbles or grains) into it. Wheneven one compartment is full, you stop. Check how many beads there are in a compartment whenever a bead falls and check the distribution of beads in the box after you stopped. Turn numbers into pitches. Or show numbers as ASCII-characters.For the complete experience you change maxValue to 255 and  you remove '+32' where shown. This program starts with a single tone but after a while it really starts swinging*/

Server.default.boot;
(
~maxValue=31; //change maxValue to 255 for the complete experience
(
/*There is a decreasing linear connection between log(freq) and value.
log(freq)=log(maxfreq)-((log(maxfreq)-log(minfreq))*value/maxValue), so
freq=exp(log(maxfreq)-((log(maxfreq)-log(minfreq))*value/maxValue))*/

SynthDef(\beeper, { arg value=150, pan=0;
	var maxfreq=10000, minfreq=50;
	Out.ar(0, Pan2.ar(Pulse.ar(exp(log(maxfreq)-((log(maxfreq)-log(minfreq))*value/~maxValue)), 0.5, 0.3), pan, 1));
}).send(s);
);
)
(
~maxIndex=10; 
~vhighest=0; ~hhighest=0;
~vlowest = 16; ~hlowest = 16;
z=40;

w=Window("Confetti2", Rect(400, 300, z*~maxIndex, z*~maxIndex)).front;
w.view.background_(Color.white);
w.front;

//function for drawing array on screen
t={
	w.drawHook = {
		for(0, ~maxIndex-1, { arg v;
			for(0, ~maxIndex-1, {arg h;
				r = Rect(h*z+10, v*z, z,z);  
				if((v == ~vhighest) && (h == ~hhighest), { //highest value will be colored red
					Pen.color=Color.red;
					Pen.addRect(Rect(h*z, v*z, z, z));
					Pen.fill;
				});
				if (( v == ~vlowest) && (h == ~hlowest), {  //lowest value will be colored green
					Pen.color = Color.green;
					Pen.addRect(Rect(h*z, v*z, z, z));
					Pen.fill;
				});
				(~tabel[v,h]+32).asAscii.asString.drawInRect(r, Font("Times", 24));
				// remove +32 if maxValue > 222; this may initially cause a blank window
			});
		});
		w.refresh;
	};
};

//initialize array and put on screen
(
Routine{
~tabel=Array2D(~maxIndex, ~maxIndex);
for(0, ~maxIndex-1, { arg v;
	for(0, ~maxIndex-1, {arg h;	
		~tabel.put(v, h,0);
	});		
});
t.value;
}.play;);

p=Synth(\beeper, [\value, 150, \pan, 0]);

Routine({
	//make distribution
	var highest=0, lowest=255;
	v= ~maxIndex.rand;   //choose random element of array
	h= ~maxIndex.rand;
	while ({~tabel.at(v,h) <= ~maxValue}, {
		~tabel.put(v, h, ~tabel.at(v, h) + 1); //increase element by 1
		if (~tabel.at(v,h) > highest , {highest = ~tabel.at(v,h); ~vhighest=v; ~hhighest=h;}); //find highest value
		t.value;   //put on screen
		p.set(\value, ~tabel.at(v,h));  //make sound
		v= ~maxIndex.rand;		//choose next element
		h= ~maxIndex.rand;
		0.125.wait
	});
	//show what was made before
	highest=0;
	lowest=255;
	for(0, ~maxIndex-1, { arg v;
		for(0, ~maxIndex-1, {arg h;
			if (~tabel.at(v,h) > highest, {   //find highest value
				highest = ~tabel.at(v,h);
				~vhighest = v;
				~hhighest = h;
			});
			if(~tabel.at(v,h) < lowest, {   //find lowest value
				lowest = ~tabel.at(v,h);
				~vlowest = v;
				~hlowest = h;
			});				
			p.set(\value, ~tabel.at(v,h)); //make sound
			0.1.wait;
		});
	});
	t.value; //put on screen and color highest and lowest value
	p.set(\value, ~tabel.at(~vhighest, ~hhighest), \pan, -1); //sound for highest vakue
	q=Synth(\beeper, [\value, ~tabel.at(~vlowest, ~hlowest), \pan, 1]); //sound for lowest value
	10.wait;
	p.free;
	q.free;
}).play;
	
)
raw 3535 chars (focus & ctrl+a+c to copy)
comments
aki.traar user 10 Jul'18 14:04

sorry but how do you play this?

prko user 12 Jul'18 17:33

t.value;

in line 66 and 99 must be

{ t.value }.defer;

And please replace the line 30

w.drawHook = {

as follows:

w.drawFunc = {

Then it works!

prko user 12 Jul'18 17:36

t.value in line 74 must be also enclosed by { }.defer