«Re: sloth canons [Pattern Extension]» by tom.dugovic

on 23 Apr'22 22:21 in code forksloth canon

Save this as an extension and recompile class library. You can now use Psloth([0,1,0], 10), for example, instead of declaring a new routine for each pattern.

Update: Psloth2 created. Same output, memory usage decreased.

I didn't persist the use cases, as this should be saved as a .sc file. Please refer to the ancestor code for usage.

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
Psloth : Pattern { // Uses more memory.
    var seed, repeats;

    *new { arg seed, repeats=1;
        if(repeats==inf, {MethodError("Psloth should not be called with `inf` repeats. It grows in memory unbounded. Consider Psloth2.").throw;});
        ^super.newCopyArgs(seed, repeats);
    }

    storeArgs{^[seed, repeats]}

    embedInStream { arg inval;
        var out, temp=seed, temp2=List[], size=seed.size;
        repeats.value(inval).do({ arg i;
            out = temp.wrapAt((i/size).floor) + seed.wrapAt(i);
            out.embedInStream(inval);
            i = i+1;
            temp2.add(out);
            if((i+1) > (temp.size**2), {temp = temp2;});
        });
        ^inval
    }
}

Psloth2 : Psloth { // Uses more CPU. Memory usage severely reduced.
    *new { arg seed, repeats=1;
        ^super.newCopyArgs(seed, repeats);
    }

    embedInStream { arg inval;
        var size=seed.size;
        var pointers=List[];
        repeats.value(inval).do({ arg j;
            j = j+1;
            if(j == j.nextPowerOf(size),{ pointers.add(0); });
            pointers.collect{|i| seed.at(i)}.sum.embedInStream(inval);
            pointers.do{|x, i|
                if((i==0) || (pointers[0..(i-1)].sum==0), {pointers[i] = ((x+1)%size);})
            };
        });
        ^inval
    }
}
raw 1344 chars (focus & ctrl+a+c to copy)
reception
comments
eli.rosenkim user 28 Apr'22 01:25

this is rad!