Millihertz: new tool to test/help me on

Heres a tool to calculate the milliseconds(ms) and frequency(Hz) of the current bpm set in renoise, nothing major, but you might find it handy…its not quite finished as i haven’t added 3rds or 5th etc. yet, but that will come.
I could do with a hand in tidying it up abit, e.g. i can’t fathom a way to add titles/headers to each column that use a different “style” (that might be because i decided to use the dynamic method of building the dialog)…does anyone think it needs to have the floating point numbers rounded up abit? if so, how would i go about doing that? i guess it would be using a “math.something” somewhere near “:format”…

https://drive.google…dit?usp=sharing

thanks to dblue for getting the ball rolling on this…

[details=“Click to view contents”] ```

–main.lua
–============================================================================]]–

– Placeholder for the dialog
local show_custom_dialog = nil

– Placeholder to expose the ViewBuilder outside the show_dialog() function
local vb = nil

– Reload the script whenever this file is saved.
– Additionally, execute the attached function.
_AUTO_RELOAD_DEBUG = function()

end

– Read from the manifest.xml file.
class “RenoiseScriptingTool” (renoise.Document.DocumentNode)
function RenoiseScriptingTool:__init()
renoise.Document.DocumentNode.__init(self)
self:add_property(“Name”, “Untitled Tool”)
self:add_property(“Id”, “Unknown Id”)
end

local manifest = RenoiseScriptingTool()
local ok,err = manifest:load_from(“manifest.xml”)
local tool_name = manifest:property(“Name”).value
local tool_id = manifest:property(“Id”).value


– Main functions

function millihertz() -----------start the main function
local songbpm = renoise.song().transport.bpm -----------call on the songs bpm and store it
local lpb = renoise.song().transport.lpb
local divised = nil —placeholder for the calculated millisecond interval
local hertz = nil ----placeholder for the calculated frequency interval
local vb = renoise.ViewBuilder() --set the name of the viewbuilder
local NUM_DIVISIONS = 13 -----------number of iterations we are gonna run
local division_strings = { --create an array,one position for each row
"1/32 ", "1/24 ", "1/16 ", "1/12 ", "1/9 ", "1/8 ", "1/7 ", "1/6 ", "1/5 ", "1/4 ", "1/3 ", "1/2 ", "1/1 "
}

– create the main content column, but don’t add any views yet:
local dialog_content = vb:column
{
style = “plain”,
margin = 15,
vb:row { style = “panel”, vb:text {text = “Beat”}, vb:text { text = " "}, vb:text { text = “Milliseconds”}, vb:text { text = " "}, vb:text {text = "Frequency "}},
vb:row { vb:text {text = “----------------------------------------------”}}
}

for division = 1,NUM_DIVISIONS do
– create a row for each division
local division_row = vb:row {}

for divising = 1, NUM_DIVISIONS do
if (division == 1) then divised = ((60000 / songbpm) * (1/(32/lpb))) --calculates
elseif (division == 2) then divised = ((60000 / songbpm) * (1/(24/lpb))) --ms
elseif (division == 3) then divised = ((60000 / songbpm) * (1/(16/lpb))) --for
elseif (division == 4) then divised = ((60000 / songbpm) * (1/(12/lpb))) --each
elseif (division == 5) then divised = ((60000 / songbpm) * (1/(9/lpb))) --division
elseif (division == 6) then divised = ((60000 / songbpm) * (1/(8/lpb))) --(row)
elseif (division == 7) then divised = ((60000 / songbpm) * (1/(7/lpb)))
elseif (division == 8) then divised = ((60000 / songbpm) * (1/(6/lpb)))
elseif (division == 9) then divised = ((60000 / songbpm) * (1/(5/lpb)))
elseif (division == 10) then divised = ((60000 / songbpm) * (1/(4/lpb)))
elseif (division == 11) then divised = ((60000 / songbpm) * (1/(3/lpb)))
elseif (division == 12) then divised = ((60000 / songbpm) * (1/(2/lpb)))
elseif (division == 13) then divised = ((60000 / songbpm) * (1/(1/lpb)))
else divised = 0
end
end

for freqcount = 1, NUM_DIVISIONS do
if (division == 1) then hertz = (1 / ((60 / songbpm) * (1/(32/lpb)))) --calculates
elseif (division == 2) then hertz = (1 / ((60 / songbpm) * (1/(24/lpb)))) --hertz
elseif (division == 3) then hertz = (1 / ((60 / songbpm) * (1/(16/lpb)))) --for
elseif (division == 4) then hertz = (1 / ((60 / songbpm) * (1/(12/lpb)))) --each
elseif (division == 5) then hertz = (1 / ((60 / songbpm) * (1/(9/lpb)))) --division
elseif (division == 6) then hertz = (1 / ((60 / songbpm) * (1/(8/lpb)))) --(row)
elseif (division == 7) then hertz = (1 / ((60 / songbpm) * (1/(7/lpb))))
elseif (division == 8) then hertz = (1 / ((60 / songbpm) * (1/(6/lpb))))
elseif (division == 9) then hertz = (1 / ((60 / songbpm) * (1/(5/lpb))))
elseif (division == 10) then hertz = (1 / ((60 / songbpm) * (1/(4/lpb))))
elseif (division == 11) then hertz = (1 / ((60 / songbpm) * (1/(3/lpb))))
elseif (division == 12) then hertz = (1 / ((60 / songbpm) * (1/(2/lpb))))
elseif (division == 13) then hertz = (1 / ((60 / songbpm) * (1/(1/lpb))))
else hertz = 0
end
end
--------Place the boxes---------------------------------------

local division_text_box = vb:text
{
text = division_strings[division],------call on the array and use variable “division” to
} ------choose position

local division_ms_box = vb:textfield
{
text = (("%.3f ms"):format(divised)),------show the number as a float and add units----
}

local division_fs_box = vb:textfield
{
text = (("%.3f Hz"):format(hertz)), ------show the number as a float and add units
}

--------Add the boxes by “hand” into the division_row--------------------------
division_row:add_child(division_text_box)
division_row:add_child(division_ms_box)
division_row:add_child(division_fs_box)
--------Add the row------------------------------------------------------------
dialog_content:add_child(division_row)

end--------------------------------------end the initial “do for each position in the array”

renoise.app():show_custom_dialog(“MilliHertz”, dialog_content)—place the dialog

end-----------------------------------------------end the main function(millihertz)


– Menu entries

renoise.tool():add_menu_entry {
name = “Main Menu:Tools:”…tool_name…"…",
invoke = millihertz
}


– Key Binding

renoise.tool():add_keybinding {
name = “Global:Tools:” … tool_name…"…",
invoke = millihertz
}


– MIDI Mapping

–[[
renoise.tool():add_midi_mapping {
name = tool_id…":Show Dialog…",
invoke = show_dialog
}
–]]

  
edit:----29/07/2014  
Millihertz now takes the lines per beats into account. Its also possible to add a keybinding. plus theres some more beat intervals for you to play around with. :-) use the DL link above :-)

For a rounding function simply search “lua round” on the internet

If I understood you correctly, simply add a child row with your title with a different style

Lua doesn’t have a native rounding function, but it’s quite trivial to make your own. You can even extend the existing math functions if you want to keep things somewhat uniform.

math.round = function(value) return math.floor(value + 0.5) end  
  
>>> print( math.round(0.499) )  
0  
  
>>> print( math.round(0.500) )  
1  
  

Maybe not quite what you’re looking for in this case, though.

If you simply want to format the floating point numbers into something more friendly for display purposes, then you can use string.format()

The values will naturally get rounded a bit when limiting the number of decimal places to be displayed.

>>> print( string.format("%.1f", 123.456789) )  
123.5  
  
>>> print( string.format("%.2f", 123.456789) )  
123.46  
  
>>> print( string.format("%.3f", 123.456789) )  
123.457  

So in your code, you could use something like:

text = (("%.2f ms"):format(divised))  

thanks for those tips, i finally realised where to put the vb:row :-)…should be an updated version of the tool in the first post…just have to add in the other beat divisions now…its looking alot cleaner now :slight_smile:

Thanks, it’s working nicely here.

updated tool, see original post. XXX