«modulate pattern keys with arbitrary envelopes» by grirgz

on 16 Sep'12 20:49 in patternevent
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
(
SynthDef(\ctlPoint, { |outbus, value, time, curve|
       var     start = In.kr(outbus, 1);
       ReplaceOut.kr(outbus, EnvGen.kr(Env([start, value], [time], curve), doneAction: 2));
}).store;

// can't Pchain the resulting pattern, so use the "chain" argument to chain the inner pattern
~penvcontrol = { arg pat, chain=nil;
	var buskeydict = Dictionary.new;
	var respat = List.new;
	var ctlpatlist = List.new;
	var pbindpat;
	var makebusmap;

	makebusmap = { arg key;
		Pfunc { arg ev; ev[key].asMap }
	};
	
	if(pat.class == EventPatternProxy) {
		pbindpat = pat.source;
	} {
		pbindpat = pat
	};

	pbindpat.patternpairs.pairsDo { arg key,val;
		var buskey;
		var env;
		var cbus;
		var ctlpat;
		if(val.class == Ref) {
			buskey = "bus_" ++ key;
			respat.add(key);
			respat.add(makebusmap.(buskey));
			env = val.value;
			buskeydict[buskey] = env.levels[0];
			cbus.set(env.levels[0]);
			ctlpat = Pbind(
				\instrument, \ctlPoint,
				\value, Pseq(env.levels[1..],inf),
				\time, Pseq(env.times,inf) / Pfunc({thisThread.clock.tempo}),
				\group, Pkey(\busgroup),
				\outbus, Pfunc { arg ev; ev[buskey].index },
				\curve, env.curves,
				\dur, Pseq(env.times,inf)
			);
			ctlpatlist.add(ctlpat);
		} 
	};

	Pfset({
			buskeydict.keysValuesDo { arg key, val;
				currentEnvironment[key] = Bus.control(s, 1);
				currentEnvironment[key].set(val);
			};
			currentEnvironment[\busgroup] = Group.new;
		},
		Pfpar(
			[
				if(chain.notNil) {
					chain <> Pbind(*respat) <> pat;
				} {
					Pbind(*respat) <> pat;
				}
			]
			++ ctlpatlist
		),
		{
			buskeydict.keysValuesDo { arg key, val;
				currentEnvironment[key].free;
			};
			currentEnvironment[\busgroup].freeAll;
			currentEnvironment[\busgroup].free;
		}
	)
};

)


(
~pb = ~penvcontrol.(Pbind(
 \instrument, \default,
 \freq, Ref(Env([100,400,500],[1,0.5],0)),  
 // the Ref is used by ~penvcontrol to detect envelopes
 \amp, Ref(Env([0.1,1,0.1],[2,2],0)),
 \sustain, 1,
 //\dur, 1,
 \dur, Pn(1,2) // use finite pattern to restart env in sync
));
//~pb.play
Pn(~pb).play

)
raw 2143 chars (focus & ctrl+a+c to copy)
reception
comments