monome / norns

norns is many sound instruments.
http://monome.org
GNU General Public License v3.0
633 stars 147 forks source link

add script-definable action for when clock tempo changes #1575

Closed dndrks closed 2 years ago

dndrks commented 2 years ago

during scripting, sometimes it's helpful to push changes to PARAMETERS > CLOCK > tempo across other parts of the script which work within the bounds of timing but perhaps don't use clock.sync. currently, this requires re-patching the system tempo change handler inside of the script, as described here: https://llllllll.co/t/norns-scripting/14120/1010

to avoid development pitfalls / weird states if a script doesn't reset the system handler, this PR adds clock.tempo_change_handler, which is tied into the system-defined clock_tempo parameter action, and passes the bpm and source into a user script, eg:

function clock.tempo_change_handler(bpm,source)
  print(bpm,source)
  engine.delay_time(clock.get_beat_sec() * bpm/128) -- imagining a clock-synced delay mechanism inside of an engine
end

the above will print 91 internal (for example) and send the engine the correct timing value.

the clock.tempo_change_handler function is reset during clock cleanup, so there's a clean slate in between scripts. please lmk if there's anything else to consider on this! tyty!

dndrks commented 2 years ago

rad, thank you @artfwo !! folded the changes in -- much clearer, appreciate your eyes + insights!!

awwaiid commented 2 years ago

https://github.com/monome/norns/pull/1573 might be another approach, it allows chaining on top of existing param actions. The example on the lines forum would be something like:

params:append_action("clock_tempo", function ()
  -- Do some other stuff
  -- or even here call do_some_other_stuff()
end)
dndrks commented 2 years ago

@artfwo , finally got the brain space to make these proposed changes -- please lmk if there's anything i'm misunderstanding?

artfwo commented 2 years ago

@artfwo , finally got the brain space to make these proposed changes -- please lmk if there's anything i'm misunderstanding?

approved, looks good!