OTYPElearn1.1scripting for the octatrackoctatrack

The studies
  1. Learn OType
  2. 01Quickstart
  3. 02Numbers and expressions
  4. 03Decisions and loops
  5. 04Variables and memory
  6. 05Tracks and steps
  7. 06Functions
  8. 07Asking the player
  9. 08Chance you can repeat
  10. 09Live scripts
  11. 10When something is wrong
  12. 11Appendix: scope at a glance

study 08 of 10

Chance you can repeat

Random choices are where scripts surprise you. They are also where you want control: keep the variation you liked, come back to it tomorrow. This study covers OType's random values, its lists, and the seeds that make chance repeatable.

Random values

writegives
rnd n0 to n − 1
rrand a ba to b, both included
toss0 or 1
chance p1 with p % chance, else 0
prob p:the modifier you know: run the line with p % chance

Lists

A list is a few numbers between braces, up to sixteen: {60 90 127}. Lists are constants, written in the script; usually you name them with const. Three operations read them: pick list (one element at random), at list n (the n-th, from 1) and len list (how many).

Listing 8-1 · Random hats, with volumes from a list
const vols {60 90 127}

def rnd_hats
  menu "LEARN" >> "RND HATS"
  tr.clr
  l 1 16:
    if chance 60:
      tr.add i
      pl.set i amp.vol pick vols
Output · what the simulator prints for $ otype.py run rnd_hats -v --seed 5
rnd_hats  seed 5 (--seed)  pattern A01 len 16 (default)  track 1
T1  1   5   9   13
    ...XXX..XXXXXX.X
    step 4: amp.vol 90   step 5: amp.vol 60   step 6: amp.vol 60   step 9: amp.vol 60
    step 10: amp.vol 90   step 11: amp.vol 60   step 12: amp.vol 90   step 13: amp.vol 60
    step 14: amp.vol 127   step 16: amp.vol 60
      X trig  ! one-shot  o trigless trig  - trigless lock  l locks only  . none
part:  tempo 120
cost: 244/304   random draws: rand 10  toss 16  prob 0  drunk 0  file 0

Some operations draw a value for every step of the context at once: pl.rnd p locks parameter p at random on each trig, smp.rnd a b locks a sample slot between a and b. They save you a loop.

Seeds

Each press draws new numbers. seed n fixes the draws: from that line on, the same n always gives the same results, on the unit as in the simulator. Make the seed a setting, and the player can browse variations and come back to the one they liked:

Listing 8-2 · Variation 1, 2, 3…: each one always the same
def variations
  menu "LEARN" >> "VARIATIONS"
  param pick_no 1 1 99
  seed pick_no
  tr.clr
  l 1 16: prob 40: tr.add i
Output · what the simulator prints for $ otype.py run variations
variations  seed 1 (--seed)  pattern A01 len 16 (default)  track 1
T1  1   5   9   13
    ...X...XX.....XX
      X trig  ! one-shot  o trigless trig  - trigless lock  l locks only  . none
part:  tempo 120
cost: 149/193   random draws: rand 0  toss 0  prob 16  drunk 0  file 0

A seed holds until the end of the block it is in; outside it, the draws go on as before. That lets you fix one part of a function and leave the rest free.

Chance in OType comes from five separate streams: one for rnd rrand pick, one for toss chance, one for prob, one for drunk, and one for the files on the card. Adding a prob line therefore does not change what the next rrand gives. seed sets all five; rand.seed, toss.seed, prob.seed, drunk.seed and file.seed set one each.

A/B with a seed

Combine a seed with flip from variables and memory: one press gives variation 7, the next one the pattern without randomness, and so on.

Listing 8-3 · One press a variation, the next the reference
def ab_locks
  menu "LEARN" >> "A/B FX"
  seed ? flip 7 0
  steps has.trig:
    pl.rnd fx1 1

What you learned

rnd · rrand · toss · chance
random values
{…} · pick · at · len
constant lists and their reading
pl.rnd · smp.rnd
a random lock on every trig of the context
seed n
the same draws for the same n, until the end of the block
streams
five independent sources; rand.seed and the others seed one

Try it

  • Run variations on the computer with --param pick_no=2, then 3: each pattern is always the same.
  • Put the seed of rnd_hats in a block around the trigs only, so the volumes stay free.
  • Use at vols rrand 1 3 instead of pick vols. Is it the same?