«Simple Harmonic Motion» by rukano

on 28 Oct'14 15:44 in sinesinewavessimple harmonic motion

Was just trying to reproduce the simple harmonic motion videos i've been watching. I think this is a compact and yet flexible way where you are able to change the scale, octave, frequency, etc very easily.

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
(
Server.default.waitForBoot({
	SynthDef(\SimpleHarmonicPing, { |out, freq, amp, sustain, pan|
		var snd = BLowPass4.ar(Saw.ar(freq), freq*Line.kr(2, 1, sustain/4), 1);
		snd = snd * EnvGen.ar(Env.perc(0.01, sustain), doneAction:2);
		OffsetOut.ar(out, Pan2.ar(snd, pan, amp))
	}).add;
});
~scale = Scale.chinese;
~octave = 4;
~sustain = 1.25;
~root = 0;
~mtranspose = 0;
~ctranspose = 0;
)

(
var n = 7*2;
var w = 1024, h = 600;
var win = Window("", Rect(100, 100, w, h)).front;
var uv = UserView(win, win.view.bounds);
var sinFunc = { |i, periode=2pi| sin((i/n)*periode) };
var lastValues = { 0 }.dup(n);
uv.background = Color.black;
uv.animate = true;
uv.frameRate = 120;
uv.drawFunc = { |uv|
	var values = Array.fill(n, { |i| sinFunc.((i-1) + (uv.frame/100), uv.frame.linexp(1,60*(60*12), pi, 24pi)) }); // for 2 min
	QPen.strokeColor = Color.white;
	QPen.line( Point(0, h/2), Point(w, h/2) );
	QPen.stroke;
	QPen.fillColor = Color.red;
	QPen.use{
		QPen.translate(w/n/2,0);
		values.do{ |value, i|
			var lastValue = lastValues[i];
			var center = Point( (i/n) * w,  (h/2) + (value * (h/3)));
			QPen.fillOval(Rect.aboutPoint( center, 3, 3 ));
			if( ((lastValue < 0) and: { value > 0 }) or: { (lastValue > 0) and: { value < 0 } } ) {
				QPen.strokeColor = Color.white;
				QPen.strokeOval(Rect.aboutPoint( center, 5, 5) );
				QPen.strokeOval(Rect.aboutPoint( center, 10, 10) );
				(
					scale: ~scale,
					instrument: \SimpleHarmonicPing,
					octave: ~octave,
					degree:i,
					amp:0.1,
					mtranspose: ~mtranspose,
					ctranspose: ~ctranspose,
					root: ~root,
					sustain: ~sustain,
					pan:i.linlin(0,n,-1, 1),
					detune:2.0.rand2,
				).play;
				"trig %".format(i).postln;
			};
		};
	};
	lastValues = values;
};
)
raw 1801 chars (focus & ctrl+a+c to copy)
reception
comments