// title: Galton Board Simulator // author: Dindoléon // description: // https://scsynth.org/t/galton-board-sim/6202 // code: ( var win = Window(); var display = UserView().layout_(HLayout().margins_(30)); var availableNotes = [58, 60, 61, 63, 65, 66, 68, 70].midicps; var timesNotesWerePlayed = Array.fill(availableNotes.size, { 0 }); var timers = Array.fill(availableNotes.size, { 0 }); var myFavoriteColor = Color(1, 0.333, 0.333); var mySecondFavoriteColor = Color(1, 1, 1); var blinkTime = 0.25; var displayRatio = 0.1; var playedNote; availableNotes.size.do({ |note, index| var view = UserView(); var counterView = UserView().animate_(true).frameRate_(24); var numberView = UserView().animate_(true); var barSize = 0; counterView.drawFunc_({ |view| barSize = view.bounds.height * displayRatio * timesNotesWerePlayed[index]; Pen.fillColor_( Color( myFavoriteColor.red + (mySecondFavoriteColor.red - myFavoriteColor.red * timers[index]), myFavoriteColor.green + (mySecondFavoriteColor.green - myFavoriteColor.green * timers[index]), myFavoriteColor.blue + (mySecondFavoriteColor.blue - myFavoriteColor.blue * timers[index]), ) ); Pen.fillRect( Rect( 0, view.bounds.height - barSize, view.bounds.width, barSize ) ); if(timers[index] > 0) { timers[index] = timers[index] - (blinkTime.reciprocal / view.frameRate) } { timers[index] = 0 }; }); numberView.drawFunc = { |view| Pen.stringCenteredIn( timesNotesWerePlayed[index].asString, Rect( 0, 0, view.bounds.width, view.bounds.height ), Font.default.deepCopy.size_(view.bounds.height * 0.5), Color.white ) }; view.layout_( VLayout( [counterView, stretch: 6], [numberView, stretch: 1] ).margins_(3) ); display.layout.add(view); }); Pbind( \instrument, \default, \dur, 0.25, \amp, 0.5, \freq, Prand(availableNotes, inf), \foo, Pfunc({ |event| playedNote = availableNotes.indexOf(event.freq); timesNotesWerePlayed[playedNote] = timesNotesWerePlayed[playedNote] + 1; if((timesNotesWerePlayed[playedNote] * displayRatio) >= 1) { displayRatio = displayRatio / 2 }; timers[playedNote] = 1; 0 }) ).play; display.drawFunc_({ |view| Pen.fillColor_(myFavoriteColor); Pen.fillRect( Rect( 0, 0, view.bounds.width, view.bounds.height ) ); Pen.fillColor_(Color.black); Pen.fillRect( Rect( 5, 5, view.bounds.width - 10, view.bounds.height - 10 ) ); }); win.layout_( VLayout(display).margins_(0) ); win.front; CmdPeriod.doOnce({ win.close }); )