Reading / Writing to Global State

Continuing the discussion from Club meeting!:

We were thinking of ways to tackle mutable state within tidal (reading/writing to variables). From what I understand, setF/setI etc. can be used to store patterns in the global dictionary (Double and Integer in this case), which can later be referenced by name, as @yaxu mentioned.

Is there a way to store different types of values, like [Pattern Double] or Map String -> [Pattern Double] in a similar way? How could an implementation look like?

1 Like

These things came to mind:

  1. tidal-lazy - a package that implements lazy variable evaluation. maybe it can be used for that, too.
  2. mutable - a package that implements mutable global state.
1 Like

@mashaal I did a thing! You can change rhythms on the fly, even while playing them. Try this:

-- Code:
makertmname name idx = "rtm_" ++ name ++ "_" ++ (show idx)

getrtmvoice name idx = cF_ (makertmname name idx)

defrhythm name pats = mapM_ (\(patname, pat) -> setF patname pat)
    $ zipWith (\idx voice -> (makertmname name idx, voice)) [0..]
    $ pats

dr = defrhythm

r name ss = stack
      $ zipWith (flip (#)) (map s ss)
      $ map (n . getrtmvoice name) [0..10]

-- Usage:

dr "funk" ["0(3,8) . ~ [0 ~] ~@2" "[~ 0]*2", "[~ 0]*2"]

d1 $ r "funk" ["bd", "sn"]

A caveat: you can't store ControlPatterns (eg. you can't say dr "rtm" ["0" # gain 1]). This is for the future.
Tell me what you think! Check the file out here

2 Likes

@yaxu this is a POC of serializing/deserializing data on the global state. I'm thinking maybe generalizing this behavior in the future for ControlMaps and whatnot. Do you see any benefit for the community in developing this further?

Oh nice, I will see if I can get this working this weekend.

I had a play to see if a similar kind of helper without modifying core tidal. It's not exactly the same, but the feeling is there...

house x y z = stack [
  struct "t*2" # s x,
  struct "f t" # s y,
  struct "[f t]*2" # s z
]

-- no high hat
d1 $ house "bd" "sn" "~"

-- add high hat
d1 $ house "bd" "sn" "hh"