[SOLVED] Api Keybinds Controlling The Metronome?

Ok, this starts from me seeing Protman’s “Set Octave to” -autogenerator - which basically is:

  
for oct=0,9 do  
 renoise.tool():add_keybinding {  
 name = "Pattern Editor:Impulse:Set Note to Octave " .. oct,  
 invoke = function() Octave(oct) end  
 }  
end   

I get the concept. do a for oct 0 - 9 and get a total of 10 “Set Note to Octave” keybinds.
So I tried to do this:

  
for met=0,16 do  
 renoise.tool():add_keybinding {  
 name = "Global:Impulse Set:Metronome to " .. met,   
 invoke = function() met_tick2(met) end  
 }  
end   
  

.
That made the 17 keybinds, but while Protman’s octave(oct) does seem to somehow discuss what’s going on with the keyboard, or respond to the keybinding number being called, I couldn’t, for the life of me, figure out how to get mine to do what the number received by the keybinding would want to do.

I’m really fascinated by this capability to set the metronome tick amount, it’s really quite awesome. I know I can just sit down and do 17 functions and 17 shortcuts by hand and it’ll work, but this could be a load of fun to get done “like this”, faster, because then by setting

  
renoise.song().transport.metronome_beats_per_bar  
  

quickly, it’ll be a easy to set one for

renoise.song().transport.metronome_lines_per_beat  

Since the number is observable, I can see how one would eventually be able to halve and double the metronome speeds with shortcuts :)

Ok, I’m making some headway. Now I hit my head on “what if it’s 0”. I realize that I can still just limit first met to 1-16 and met2 = 0 or something like that, and just have two functions, but it’d be sweet to have both functions in the same thing, i.e. learn a bit about “routing if not 0” “routing if 0” stuff.
Here’s what I’m trying to hack at currently:

  
function met_tick2(met)  
if ((met > 0))  
then  
 renoise.song().transport.metronome_enabled = true  
 renoise.song().transport.metronome_beats_per_bar=met  
else  
 renoise.song().transport.metronome_enabled = false  
 renoise.song().transport.metronome_beats_per_bar=met  
end  
  
end  
  
renoise.tool():add_keybinding {  
name = "Global:Impulse Set: Metronome Off",  
 invoke = function() metoff()   
end  
}  
  
for met=0,16 do  
 renoise.tool():add_keybinding {  
 name = "Global:Impulse Set:Metronome to " .. met,   
 invoke = function() met_tick2(met) end  
 }  
end   
  

shift-1 - shift-9 work nicely, and shift-0 works by stopping the metronome and then crashing :D

And the solution is, of course:

  
  
function met_tick2(met)  
if ((met > 0))  
then  
 renoise.song().transport.metronome_enabled = true  
 renoise.song().transport.metronome_beats_per_bar=met  
else  
 renoise.song().transport.metronome_enabled = false  
-- renoise.song().transport.metronome_beats_per_bar=met  
end  
end  
  
  

:huh:

You could code it a little shorter…

  
function met_tick2(met)  
  
 if met then  
 renoise.song().transport.metronome_enabled = true  
 else  
 renoise.song().transport.metronome_enabled = false  
 end  
  
 renoise.song().transport.metronome_beats_per_bar=met  
end  
  
  

if met is something it is always not 0.
Ofcourse there are languages where a variable is something as long as it isn’t [nil]. But in Lua you have to specifically measure if a variable is NIL before comparing it to any content.
else “if met then” would generate a “variable is nil” error.
However the above metronome_beats_per_bar=met doesn’t work when the value is “0” because it should always start at 1.
If Renoise really crashes upon it, that is a bug and should be fixed.

1 Like

Ok, apologies for being a bit vague. In this case I meant that the keyshortcuts of that specific script stopped functioning if I hi “set to 0” before I managed to figure out how to route it to stop :)
I don’t really know how to crash Renoise itself.

Your simplification is much appreciated, btw :)

Btw, for me, the code you supplied, when implemented, killed the “to 0” feature for LPB and Bars per Beat. I started getting errors stating that LPb and Beats per Bar can’t be set to 0. That’s why I made it in the way I could, so that shift-0 would do a different feature and LPB/BarsPerBeat would not start reporting errors.

I added the “does not work” line because you didn’t mentioned you were getting those errors but said Renoise crashed instead. But you cleared that out in your previous post. So all’s cool :) (already was)

I fixed it (Metronome LPB 0-16, Metronome Bars per Beat 0-16) and added it to here

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