In this example, we generate a random rhythm (of 8 steps) that changes every 4 cycles.
The sequence is as follows:
- rand.range(0, 0xFFFF).round()
This generates an integer between [0-65535]. If we represent this in binary, it would be
0000000000000000-1111111111111111, giving us a pattern of up to 16 steps.
Expressing the upper limit of range in hexadecimal is completely irrelevant, but I think it's easier to remember FFFF than 65535. - .seg(n)
This divides the cycle into "n" segments to which the value is applied. Since we have set
1/4, the effect achieved is to extend the application segment to 4 cycles. - .struct(pattern)
Creates the rhythm with the received pattern (0=silence, 1=sound). - binaryN(number, length)
Converts an integer to its binary representation (0/1). Its second parameter (8) indicates the number of bits to select. It starts from right to left. If the parameter is less than the length of the binary number, it will truncate the bits on the left, keeping only those on the right. If it is greater, it will fill the missing bits on the left with zeros (until a pattern with the specified length is completed). - .add()
Adds to the mask resulting from the previous step. In the example, we ensure that, whatever the random number selected, steps 1 and 5 will always sound (in the worst case, these steps will end up having the value 2, which for struct purposes is "trigger sound"). If we want the result to be completely random, we can remove .add("1 0 0 0 1 0 0 0") from the equation.
setcpm(120/4)
let caos = rand.range(0, 0xFFFF).round().seg(1/4);
$: stack(
s("cp:3*2"),
s("bd:6").struct(binaryN(caos, 8).add("1 0 0 0 1 0 0 0"))
)
Another interesting approach would be the following:
- The generated random number will be within this range: 00000000-00001111
- This ensures that the first 4 bits will always be 0, so whatever we put in the first 4 steps of
.add(pattern)will always remain unchanged. - We will have a constant-rate start (controlled by us) and a tail that will change every 4 cycles.
setcpm(120/4)
let caos = rand.range(0, 0x0F).round().seg(1/4);
$: stack(
s("cp:3*2"),
s("bd:6").struct(binaryN(caos, 8).add("1 0 1 1 0 0 0 0"))
)