class library: Reset has been renamed to OnError
[supercollider.git] / SCClassLibrary / JITLib / Patterns / Pxfade.sc
blobd6512d36ef3303160b19f347c78b54add611360f
1 PfadeIn : FilterPattern {
2         var <>fadeTime, <>holdTime=0, <>tolerance;
3         *new { arg pattern, fadeTime=1.0, holdTime=0.0, tolerance=0.0001;
4                 ^super.new(pattern).fadeTime_(fadeTime).holdTime_(holdTime).tolerance_(tolerance)
5         }
6         embedInStream { arg inval;
7                         var outval, elapsed=0, stream, c;
8                         stream = pattern.asStream;
11                         if(holdTime > 0.0) { Event.silent(holdTime, inval).yield };
12                         loop {
13                                 outval = stream.next(inval);
14                                 if(outval.isNil) { ^nil.yield };
16                                 elapsed = elapsed + outval.delta;
17                                 c = elapsed / fadeTime;
18                                 if(c >= 1.0) {
19                                         inval = outval.yield;
20                                         ^stream.embedInStream(inval);
21                                 } {
22                                         outval = outval.copy;
23                                         outval[\amp] = c.max(0) * outval[\amp];
24                                 //      outval[\amp] = (c.max(0) * pi * 0.25).sin * outval[\amp];
26                                         inval = outval.yield;
27                                 }
28                         }
29         }
30         storeArgs { ^[ pattern, fadeTime, holdTime, tolerance ] }
33 PfadeOut : PfadeIn {
34         asStream { | cleanup| ^Routine({ arg inval; this.embedInStream(inval, cleanup) }) }
36         embedInStream { arg inval, cleanup;
37                 var outval, elapsed=0, stream, c;
38                 stream = pattern.asStream;
39                 cleanup ?? { cleanup = EventStreamCleanup.new };
40                 loop {
41                         inval = stream.next(inval) ?? { ^cleanup.exit(inval) };
42                         cleanup.update(inval);
44                         elapsed = elapsed + inval.delta;
45                         if(elapsed.round(tolerance) <= holdTime) {
46                                 inval = inval.yield;
47                         } {
48                                 c = elapsed - holdTime / fadeTime;
49                                 if(c >= 1.0) {
50                                         ^cleanup.exit(inval);
51                                 } {
52                                         inval[\amp] = (1.0 - c.max(0)) * inval[\amp];
54                                         inval = inval.yield;
55                                 }
56                         }
57                 }
58         }
61 PfinQuant : FilterPattern {
62         var <>quant, <>clock;
64         *new { arg pattern, quant, clock;
65                 ^super.new(pattern).quant_(quant).clock_(clock)
66         }
68         embedInStream { arg inval;
69                 var value, stream = pattern.asStream;
70                 var referenceClock = clock ? thisThread.clock;
71                 var endAt = quant.nextTimeOnGrid(referenceClock);
72                 while {
73                         value = stream.next(inval);
74                         value.notNil and: { referenceClock.beats < endAt }
75                 } {
76                         inval = value.yield;
77                 };
78                 ^inval
79         }