-------------------------------------------------------------------------------- --! @title TransportPattern --! @brief Rhythm Pattern That Starts and Stops With the Host --! @category Sequencing --! --! A 16-step velocity pattern (a hi-hat part, for instance) that plays whenever --! the host transport runs, with no key to hold. onTransport starts the loop on --! play and cancels it on stop. The loop schedules each step against the song --! position returned by getBeatTime, so the pattern lands on the host's grid even --! when playback starts in the middle of a bar, and it follows tempo changes and --! playhead jumps (cycle loops, clicks in the timeline) instead of drifting. --! --! @demonstrates onTransport, getBeatTime, waitBeat, beat2ms, playNote, Table -------------------------------------------------------------------------------- local NUM_STEPS = 16 local STEP_BEATS = 0.25 -- sixteenth notes local pattern = Table("Velocity", NUM_STEPS, 0, 0, 127, true) pattern.bounds = {10, 10, 480, 100} -- Default groove, accents on the beats. A saved preset overrides it: the stored -- values are restored after the main chunk has run. for i = 1, NUM_STEPS do pattern:setValue(i, (i % 4 == 1) and 110 or 60, false) end local playhead = Table("Playhead", NUM_STEPS, 0, 0, 1, true) playhead.bounds = {10, 112, 480, 8} playhead.enabled = false playhead.persistent = false -- display only, nothing to save local note = Knob{"Note", 42, 0, 127, true} -- GM closed hi-hat note.bounds = {510, 10, 90, 50} local gate = Knob{"Gate", 0.5, 0.05, 1, unit = Unit.PercentNormalized} gate.bounds = {510, 70, 90, 50} -- Bumped on every transport change: a loop only runs while it holds the -- current value, so a stop (or a fast stop/play) can never leave two loops. local generation = 0 local shownStep = nil local function showStep(step) if shownStep then playhead:setValue(shownStep, 0) end if step then playhead:setValue(step, 1) end shownStep = step end -- First step boundary at or after the given song position, in beats. The small -- epsilon keeps a position that is exactly on a boundary on that boundary. local function nextBoundary(beat) return math.ceil(beat / STEP_BEATS - 1e-6) * STEP_BEATS end local function playLoop(myGeneration) local nextBeat = nextBoundary(getBeatTime()) while myGeneration == generation do local now = getBeatTime() -- More than a step away means the playhead was relocated: resync on the -- new position rather than waiting for (or catching up with) the old one. if math.abs(nextBeat - now) > STEP_BEATS then nextBeat = nextBoundary(now) end if nextBeat > now then waitBeat(nextBeat - now) end if myGeneration ~= generation then break end -- The step comes from the song position, not from a counter, so the -- pattern is always in phase with the host's bars. local step = math.floor(nextBeat / STEP_BEATS + 0.5) % NUM_STEPS + 1 local velocity = pattern:getValue(step) if velocity > 0 then -- An explicit duration: this thread is not tied to a key, so -1 -- would release the note at once. playNote(note.value, velocity, beat2ms(STEP_BEATS * gate.value)) end showStep(step) nextBeat = nextBeat + STEP_BEATS end end -- Runs as a thread, so it may wait; it can also be called before onInit. function onTransport(playing) generation = generation + 1 -- cancels the running loop, if any if playing then playLoop(generation) else -- Notes already playing end on their own after their gate time. showStep(nil) end end setSize(620, 130) makePerformanceView()