[SOLVED] Lua Math - How To Define Space

Here’s a quick tip from me regarding the code that restricts the value ranges.

math.min(a, b) will return whichever value is the lowest. For example: math.min(16, 17) will return 16.

math.max(a, b) will return whichever value is the highest. For example: math.max(2, 1) will return 2.

So instead of this bit of code: (which is perfectly functional but not very elegant)

  
if value > max then  
 value = max  
elseif value < min then  
 value = min  
end  
  

You can do this instead, which is much more sexy:

  
value = math.max(min, math.min(max, value))  
  

:slight_smile:

Finally, here’s something a bit more compact, just for fun:

  
-- +1 / -1 on Metronome LPB and Metronome BPB  
function adjust_metronome(lpb_delta, bpb_delta)  
  
 -- Local reference to transport  
 local t = renoise.song().transport  
  
 -- Adjust LPB  
 t.metronome_lines_per_beat = math.max(1, math.min(16, t.metronome_lines_per_beat + lpb_delta))  
  
 -- Adjust BPB  
 t.metronome_beats_per_bar = math.max(1, math.min(16, t.metronome_beats_per_bar + bpb_delta))  
  
 -- Show status  
 renoise.app():show_status(  
 "Metronome : " .. t.metronome_lines_per_beat .. " LPB : " .. t.metronome_beats_per_bar .. " BPB"  
 )  
  
end  
  
renoise.tool():add_keybinding {  
 name = "Global:Impulse:Metronome LPB -1",  
 invoke = function() adjust_metronome(-1, 0) end  
}  
  
renoise.tool():add_keybinding {  
 name = "Global:Impulse:Metronome LPB +1",  
 invoke = function() adjust_metronome(1, 0) end  
}  
renoise.tool():add_keybinding {  
 name = "Global:Impulse:Metronome BPB -1",  
 invoke = function() adjust_metronome(0, -1) end  
}  
  
renoise.tool():add_keybinding {  
 name = "Global:Impulse:Metronome BPB +1",  
 invoke = function() adjust_metronome(0, 1) end  
}  
  
1 Like