«Galton Board Simulator» by Dindoléon

on 10 Jul'22 12:50 in gui

https://scsynth.org/t/galton-board-sim/6202

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
(
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 });
)
raw 2538 chars (focus & ctrl+a+c to copy)
reception
comments
fellipealcateia user 04 Aug'22 23:02

This is great! Super useful for visualizing the probability distribution! Thanks for sharing!!!