Pattrns and Lua question about note sequences

So, I want to return a sequence of notes, not a chord.
When i do it like this:

return pattern {
  unit = "1/4",
  event = sequence("c4", "off d0.1")
}

it’s okay.
as well as:

function s()
  return sequence("c4", "off d0.1")
end
return pattern {
  unit = "1/4",
  event = s()
}

but this:

return pattern {
    unit = "1/4",
    event = function(context)
        if context.step % 2 == 1 then
            return {"c4", "off d0.3"}
        else
            return {"c4", "off d0.1"}
        end
    end
}

returns a chord

I guess it sees it like
event = {{"c4", "off d0.3"}}

So how can i return a sequence based on conditions?

When you assign a function to event, it will be called at every single step and whatever it returns will be the value at that step. The context.step value tells you what step you are on.

In your first two example you assigned a pure sequence, just in the second case you’ve used a function to generate it at the time of assignment, this is different from assigning a function itself (where it is up to pattrns to call it later).

Not sure what you expect as output here though. Do you want to play one sequence after another, take notes from two sequences alternating between which one to take from, or something else?

You’ll probably have to manage looking up a specific step from a sequence table inside your function.

For example the script below plays sequence a at the beginning then just repeats b indefinitely. Note how inside the function we are returning a single element from the lists using the the .step value on the context (c here) to derive the index.

Also using math.imod to have a wrapping index (as % would give us zero when wrapping around).

local a = {"c3", "a#3", "d3", "f3"}
local b = {"g2", "g3"}

return pattern {
  unit = "1/8",
  event = function(c)
    if c.step <= #a then
      return a[c.step]
    else
      return b[math.imod(c.step,#b)]
    end
  end
}

my code returns


while i need

so, a sequence of notes, not sequence of chords

note off plays at he same time with a c4 but i need it to play after

i expected this

return pattern {
  unit = "1/4",
  event = function(context)
    return {"c4", "off d0.1"}
  end
}

to behave like this:

return pattern {
  unit = "1/4",
  event = {"c4", "off d0.1"}
}

but i want it to be in a function because i need to use “if … else …”

That’s not how it behaves because the function is called every step. As I explained above, when using functions you will have to implement the logic you want on a per-step basis. The engine expects a single step to come out of the function so if you return a list of notes, it will interpret it as a chord.

Your particular pattern could be written like this

return pattern {
  unit = "1/8",
  event = function(c)
    if math.imod(c.step, 2) == 1 then
      return "c4"
    else
      if math.imod(c.step, 4) == 2 then      
        return "off d0.3"
      else
        return "off d0.1"
      end
    end
  end
}

But obviously this kind of logic can get hard to manage fast so you might want to keep track of where different sequences are at with your own variables and so on, depending on what else you want to do later.

Or you might want to write a function that calculates an entire sequence ahead of time and give the result of that to event.

You could also use the cycle notation where expressing alternating events is simpler

return pattern {
  unit = "1/4",
  event = cycle("c4 off:d=<0.3 0.1>")
}

Yep, these actually work as i wanted. Thanks!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.