{
   "name" : "Confetti2",
   "author" : "Henk Lasschuit",
   "ancestor_list" : [],
   "description" : "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.",
   "labels" : [
      "chance",
      "numer to pitch",
      "distribution",
      "sound and image"
   ],
   "is_private" : null,
   "id" : "1-31X",
   "code" : "//Confetti 2\r\n/*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*/\r\n\r\nServer.default.boot;\r\n(\r\n~maxValue=31; //change maxValue to 255 for the complete experience\r\n(\r\n/*There is a decreasing linear connection between log(freq) and value.\r\nlog(freq)=log(maxfreq)-((log(maxfreq)-log(minfreq))*value/maxValue), so\r\nfreq=exp(log(maxfreq)-((log(maxfreq)-log(minfreq))*value/maxValue))*/\r\n\r\nSynthDef(\\beeper, { arg value=150, pan=0;\r\n\tvar maxfreq=10000, minfreq=50;\r\n\tOut.ar(0, Pan2.ar(Pulse.ar(exp(log(maxfreq)-((log(maxfreq)-log(minfreq))*value/~maxValue)), 0.5, 0.3), pan, 1));\r\n}).send(s);\r\n);\r\n)\r\n(\r\n~maxIndex=10; \r\n~vhighest=0; ~hhighest=0;\r\n~vlowest = 16; ~hlowest = 16;\r\nz=40;\r\n\r\nw=Window(\"Confetti2\", Rect(400, 300, z*~maxIndex, z*~maxIndex)).front;\r\nw.view.background_(Color.white);\r\nw.front;\r\n\r\n//function for drawing array on screen\r\nt={\r\n\tw.drawHook = {\r\n\t\tfor(0, ~maxIndex-1, { arg v;\r\n\t\t\tfor(0, ~maxIndex-1, {arg h;\r\n\t\t\t\tr = Rect(h*z+10, v*z, z,z);  \r\n\t\t\t\tif((v == ~vhighest) && (h == ~hhighest), { //highest value will be colored red\r\n\t\t\t\t\tPen.color=Color.red;\r\n\t\t\t\t\tPen.addRect(Rect(h*z, v*z, z, z));\r\n\t\t\t\t\tPen.fill;\r\n\t\t\t\t});\r\n\t\t\t\tif (( v == ~vlowest) && (h == ~hlowest), {  //lowest value will be colored green\r\n\t\t\t\t\tPen.color = Color.green;\r\n\t\t\t\t\tPen.addRect(Rect(h*z, v*z, z, z));\r\n\t\t\t\t\tPen.fill;\r\n\t\t\t\t});\r\n\t\t\t\t(~tabel[v,h]+32).asAscii.asString.drawInRect(r, Font(\"Times\", 24));\r\n\t\t\t\t// remove +32 if maxValue > 222; this may initially cause a blank window\r\n\t\t\t});\r\n\t\t});\r\n\t\tw.refresh;\r\n\t};\r\n};\r\n\r\n//initialize array and put on screen\r\n(\r\nRoutine{\r\n~tabel=Array2D(~maxIndex, ~maxIndex);\r\nfor(0, ~maxIndex-1, { arg v;\r\n\tfor(0, ~maxIndex-1, {arg h;\t\r\n\t\t~tabel.put(v, h,0);\r\n\t});\t\t\r\n});\r\nt.value;\r\n}.play;);\r\n\r\np=Synth(\\beeper, [\\value, 150, \\pan, 0]);\r\n\r\nRoutine({\r\n\t//make distribution\r\n\tvar highest=0, lowest=255;\r\n\tv= ~maxIndex.rand;   //choose random element of array\r\n\th= ~maxIndex.rand;\r\n\twhile ({~tabel.at(v,h) <= ~maxValue}, {\r\n\t\t~tabel.put(v, h, ~tabel.at(v, h) + 1); //increase element by 1\r\n\t\tif (~tabel.at(v,h) > highest , {highest = ~tabel.at(v,h); ~vhighest=v; ~hhighest=h;}); //find highest value\r\n\t\tt.value;   //put on screen\r\n\t\tp.set(\\value, ~tabel.at(v,h));  //make sound\r\n\t\tv= ~maxIndex.rand;\t\t//choose next element\r\n\t\th= ~maxIndex.rand;\r\n\t\t0.125.wait\r\n\t});\r\n\t//show what was made before\r\n\thighest=0;\r\n\tlowest=255;\r\n\tfor(0, ~maxIndex-1, { arg v;\r\n\t\tfor(0, ~maxIndex-1, {arg h;\r\n\t\t\tif (~tabel.at(v,h) > highest, {   //find highest value\r\n\t\t\t\thighest = ~tabel.at(v,h);\r\n\t\t\t\t~vhighest = v;\r\n\t\t\t\t~hhighest = h;\r\n\t\t\t});\r\n\t\t\tif(~tabel.at(v,h) < lowest, {   //find lowest value\r\n\t\t\t\tlowest = ~tabel.at(v,h);\r\n\t\t\t\t~vlowest = v;\r\n\t\t\t\t~hlowest = h;\r\n\t\t\t});\t\t\t\t\r\n\t\t\tp.set(\\value, ~tabel.at(v,h)); //make sound\r\n\t\t\t0.1.wait;\r\n\t\t});\r\n\t});\r\n\tt.value; //put on screen and color highest and lowest value\r\n\tp.set(\\value, ~tabel.at(~vhighest, ~hhighest), \\pan, -1); //sound for highest vakue\r\n\tq=Synth(\\beeper, [\\value, ~tabel.at(~vlowest, ~hlowest), \\pan, 1]); //sound for lowest value\r\n\t10.wait;\r\n\tp.free;\r\n\tq.free;\r\n}).play;\r\n\t\r\n)"
}
