Skip to content

Commit

Permalink
allow sending midi clock to all devices simultaneously (#1642)
Browse files Browse the repository at this point in the history
* Add files via upload

* toggles for targeting device clock out

builds out @tehn's feedback from #1642 :
- any currently-ported midi device will populate in the 'midi clock out' section of 'PARAMS > CLOCK'
- each visible entry has a toggle to receive norns clock
- toggles get saved/restored as part of `system.state` during clean reboots

follow the approach brian outlined actually saves us from doing any if's during each clock tick!

* longer short name

cleaned up formatting to match the SYSTEM > DEVICES > MIDI syntax, which allows for up to 20 characters to display comfortably before aliasing

Co-authored-by: dan derks <derks.dan@gmail.com>
  • Loading branch information
zjb-s and dndrks authored Jan 13, 2023
1 parent 9c4f660 commit b932b0d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
43 changes: 30 additions & 13 deletions lua/core/clock.lua
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ local function new_id()
return id
end

local send_midi_clock = {}

--- create and start a coroutine using the norns clock scheduler.
-- @tparam function f coroutine body function
-- @param[opt] ... any extra arguments will be passed to the body function
Expand Down Expand Up @@ -200,7 +202,7 @@ end


function clock.add_params()
params:add_group("CLOCK", 9)
params:add_group("CLOCK", 27)

params:add_option("clock_source", "source", {"internal", "midi", "link", "crow"},
norns.state.clock.source)
Expand Down Expand Up @@ -234,6 +236,7 @@ function clock.add_params()
if source == "internal" then clock.internal.start()
elseif source == "link" then print("link reset not supported") end
end)
params:add_separator("link_separator", "link")
params:add_number("link_quantum", "link quantum", 1, 32, norns.state.clock.link_quantum)
params:set_action("link_quantum",
function(x)
Expand All @@ -248,15 +251,31 @@ function clock.add_params()
norns.state.clock.link_start_stop_sync = x
end)
params:set_save("link_start_stop_sync", false)
local clock_table = {"off"}
params:add_separator("midi_clock_out", "midi clock out")
for i = 1,16 do
local short_name = string.len(midi.vports[i].name) < 12 and midi.vports[i].name or util.acronym(midi.vports[i].name)
clock_table[i+1] = "port "..(i)..""..(midi.vports[i].name ~= "none" and (": "..short_name) or "")
local short_name = string.len(midi.vports[i].name) <= 20 and midi.vports[i].name or util.acronym(midi.vports[i].name)
params:add_binary("clock_midi_out_"..i, i..". "..short_name, "toggle", norns.state.clock.midi_out[i])
params:set_action("clock_midi_out_"..i,
function(x)
if x == 1 then
table.insert(send_midi_clock,i)
else
if tab.contains(send_midi_clock,i) then
table.remove(send_midi_clock,tab.key(send_midi_clock, i))
end
end
norns.state.clock.midi_out[i] = x
end
)
if short_name ~= "none" and midi.vports[i].connected then
params:show("clock_midi_out_"..i)
else
params:hide("clock_midi_out_"..i)
end
params:set_save("clock_midi_out_"..i, false)
end
params:add_option("clock_midi_out", "midi out",
clock_table, norns.state.clock.midi_out)
params:set_action("clock_midi_out", function(x) norns.state.clock.midi_out = x end)
params:set_save("clock_midi_out", false)
_menu.rebuild_params()
params:add_separator("crow_clock_out", "crow clock out")
params:add_option("clock_crow_out", "crow out",
{"off", "output 1", "output 2", "output 3", "output 4"}, norns.state.clock.crow_out)
params:set_action("clock_crow_out", function(x)
Expand Down Expand Up @@ -301,11 +320,9 @@ function clock.add_params()
clock.run(function()
while true do
clock.sync(1/24)
local midi_out = params:get("clock_midi_out")-1
if midi_out > 0 then
if midi.vports[midi_out].name ~= "none" then
midi.vports[midi_out]:clock()
end
for i = 1,#send_midi_clock do
local port = send_midi_clock[i]
midi.vports[port]:clock()
end
end
end)
Expand Down
11 changes: 11 additions & 0 deletions lua/core/midi.lua
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,18 @@ function Midi.update_connected_state()
else
Midi.vports[i].connected = false
end
if params.lookup["clock_midi_out_"..i] ~= nil then
local short_name = string.len(midi.vports[i].name) <= 20 and midi.vports[i].name or util.acronym(midi.vports[i].name)
params:lookup_param("clock_midi_out_"..i).name = i..". "..short_name
if short_name ~= "none" and midi.vports[i].connected then
params:show("clock_midi_out_"..i)
else
params:set("clock_midi_out_"..i,0)
params:hide("clock_midi_out_"..i)
end
end
end
_menu.rebuild_params()
end

-- add a device.
Expand Down
12 changes: 10 additions & 2 deletions lua/core/state.lua
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ state.clock.source = 1
state.clock.tempo = 90
state.clock.link_quantum = 4
state.clock.link_start_stop_sync = 1
state.clock.midi_out = 1
state.clock.midi_out = {}
state.clock.crow_out = 1
state.clock.crow_out_div = 4
state.clock.crow_in_div = 4
Expand All @@ -62,6 +62,12 @@ state.resume = function()
dofile(_path.data..'system.state')
end

-- if previously-saved state.clock.midi_out is a number,
-- make it a table
if type(state.clock.midi_out) == 'number' then
state.clock.midi_out = {}
end

-- update vports
midi.update_devices()
grid.update_devices()
Expand Down Expand Up @@ -139,7 +145,9 @@ state.save_state = function()
io.write("norns.state.clock.tempo = " .. norns.state.clock.tempo .. "\n")
io.write("norns.state.clock.link_quantum = " .. norns.state.clock.link_quantum .. "\n")
io.write("norns.state.clock.link_start_stop_sync = " .. norns.state.clock.link_start_stop_sync .. "\n")
io.write("norns.state.clock.midi_out = " .. norns.state.clock.midi_out .. "\n")
for i = 1,16 do
io.write("norns.state.clock.midi_out["..i.."] = " .. norns.state.clock.midi_out[i] .. "\n")
end
io.write("norns.state.clock.crow_out = " .. norns.state.clock.crow_out .. "\n")
io.write("norns.state.clock.crow_out_div = " .. norns.state.clock.crow_out_div .. "\n")
io.write("norns.state.clock.crow_in_div = " .. norns.state.clock.crow_in_div .. "\n")
Expand Down

0 comments on commit b932b0d

Please sign in to comment.