«Draw your waveform feat. silly pattern player» by rukano

on 29 Jan'14 04:34 in drawingwaveform

A short demonstration how to change values in a buffer from a GUI. Also some presets added, because the mouse precision is not very good if you intend to draw fast.

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
/////////////////////////////////////////////////////////////////////////////////
// Draw your waveform!
(
b = Buffer.alloc(s, 1024); // buffer, server side
a = Array.fill(1024, { 256 }); // array, language side
w = Window("Draw your waveform!", Rect(100, 100, 1024, 512)).front;
u = UserView(w, w.view.bounds);

// function that changes the array and the buffer, and makes some clipping
f = { |x, y|
	x = x.clip(0, 1023);
	y = y.clip(0, 511);
	a[x] = y;
	b.set(x, (y / 512) * 2 - 1);
};

// animate, to cope with the mouse changes
u.background_(Color.black);
u.animate_(true);
u.drawFunc = { |u|
	Pen.strokeColor = Color.white;
	Pen.moveTo( Point(0, a[0]) );
	a[1..].do{ |value, index| Pen.lineTo( Point(index+1, value) ) };
	Pen.stroke;
};

// call our function when the mouse is pressed or moved
u.mouseDownAction = { |u, x, y, mod| f.(x, y) };
u.mouseMoveAction = { |u, x, y, mod| f.(x, y) };

// call some presets (1..6), spacebar clears.
u.keyDownAction = { |u, char, mod|
	char.switch(
		$ , { a.size.do{ |i| f.(i, 256) } }, // reset waveform
		$1, { a.size.do{ |i| f.(i, 512.rand) } }, // random waveform
		$2, { Array.interpolation(1024, 0, 512).do{ |val, i| f.(i, val) } }, // linear ramp
		$3, { Array.fill(1024, { |i| sin(i/1024*2pi) * 256 + 256 }).do{ |val, i| f.(i, val) } }, // sinewave
		$4, { Array.fill(1024, { |i| (i/512).floor * 512 }).do{ |val, i| f.(i, val) } }, // squarewave
		$5, { Pbrown(0,512,16).asStream.nextN(1024).do{ |val, i| f.(i, val) } }, // brown pattern
		$6, { (Array.fill(1024, { |i| sin(i/1024*2pi) * 200 + 256 }) + Pbrown(0,32,8).asStream.nextN(1024)).do{ |val, i| f.(i, val) } }, // sinewave with brown
	);
};
// make a pattern player with a simple synth with COsc
x = Pbind(
	\type, \set,
	\id, { |freq| LeakDC.ar(COsc.ar(b, freq, 1))!2 * 0.5 }.play,
	\octave, 3,
	\scale, Scale.minorPentatonic,
	\degree, Pn(Plazy{Pseq(Pxrand((0..4),inf).asStream.nextN(4),4)}),
	\dur, 1/4
).play;

// clean up
w.onClose_({
	b.free;
	x.stop;
});
)
raw 2031 chars (focus & ctrl+a+c to copy)
reception
gui
comments