File: Pfunc.schelp

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (85 lines) | stat: -rw-r--r-- 2,131 bytes parent folder | download | duplicates (3)
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
class:: Pfunc
summary:: Function pattern
categories:: Streams-Patterns-Events>Patterns>Function
related:: Classes/Pfuncn, Classes/FuncStream

description::
Returns a link::Classes/Stream:: that returns values from the code::nextFunc::.


classmethods::

method:: new
argument:: nextFunc
Stream function. In an event stream code::nextFunc:: receives the current link::Classes/Event:: as argument, and more generally the argument passed to the stream's code::next:: call.
argument:: resetFunc
Function that is called when the stream is reset. code::resetFunc:: receives no arguments.


examples::

Numeric stream examples

code::
(
var a, x;
a = Pfunc({ exprand(0.1, 2.0) + #[1, 2, 3, 6].choose }, { \reset.postln });
x = a.asStream;
x.nextN(20).postln;
x.reset;
)

// with argument passed to nextFunc
(
x = Pfunc({ |inval| (10 ** inval.value) * rrand(1, 9) }).asStream;
[2, 3, 2].do { |i| x.next(i).postln };
x.nextN(5, (:0..)).postln;
)
::

Event stream (sound) examples:

code::
(
SynthDef(\help_sinegrain,
	{ arg out = 0, freq = 440, sustain = 0.05;
		var env;
		env = EnvGen.kr(Env.perc(0.01, sustain, 0.2), doneAction: Done.freeSelf);
		Out.ar(out, SinOsc.ar(freq, 0, env))
	}).add;
)

(
var a;
a = Pfunc({ exprand(0.1, 0.3) + #[1, 2, 3, 6, 7].choose }).asStream;
{
	a.do { |val|
		Synth(\help_sinegrain, [\freq, val * 100 + 300]);
		0.02.wait;
	}
}.fork;
)
::

When an Event is played, if code::\freq:: is set then code::\degree:: is ignored (due to the code::Event.default:: machinery). In a chain of Patterns, a code::Pfunc:: can be used to delete a key from the Event stream; this can even be done inside a Pbind.

code::
q = Pbind(\freq, 300, \dur, Pn(0.3, 2));
q.trace.play;

// Sound-wise ineffective modification of the incoming stream
p = Pbind(\degree, 6) <> q;
p.trace.play;

// Instead
p = Pbind(\degree, 6) <> Pfunc { |ev| ev.freq = nil } <> q;
p.trace.play;

// Alternatively
p = Pbind(\degree, Pfunc { |ev| ev.freq = nil; 6 } ) <> q;
p.trace.play;

// Just setting a key to nil from a Pbind pair will end the stream
p = Pbind(\freq, nil, \degree, 6) <> q;
p.trace.play; // silent because it outputs no events
::