[code Trick]
I share this fairly fast way of converting the typical range of numbers from 0 to 119 to strings from C-0 to B-9 , using tostring and tonumber for a valuebox.
--tostring to valuebox
function note_tostring ( number ) --return a string, range number: 0 to 119)
local note_name = { "C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-" }
return ("%s%s"):format( note_name[number % 12 + 1], math.floor( number/12 ) )
end
--tonumber to valuebox
function note_tonumber( string ) --return a number
local note_name_1 = { "C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-" }
local note_name_2 = { "c-", "c#", "d-", "d#", "e-", "f-", "f#", "g-", "g#", "a-", "a#", "b-" }
local note_name_3 = { "C", "C#", "D", "D#", "E", "F" , "F#", "G", "G#", "A", "A#", "B" }
local note_name_4 = { "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b" }
for i = 1, 12 do
for octave = 0, 9 do
if ( string == ("%s%s"):format( note_name_1[i], octave ) ) or
( string == ("%s%s"):format( note_name_2[i], octave ) ) or
( string == ("%s%s"):format( note_name_3[i], octave ) ) or
( string == ("%s%s"):format( note_name_4[i], octave ) ) then
return i + ( octave * 12 ) - 1
end
end
end
end
--the valuebox
vb:valuebox {
id = "CONVERT_NOTE_NAME",
height = 25,
width = 59,
min = 0,
max = 119,
value = 36,
tostring = function( number ) return note_tostring ( number ) end,
tonumber = function( string ) return note_tonumber ( string ) end,
tooltip = "Tooltip text\n[Range = C-0 to B-9]"
}
This case is equal from the first function (but little optimized):
function note_tostring_( value ) --return a string, Range number: 0 to 119)
if ( value < 12 ) then
return ("%s0"):format( pht_table_notes[value + 1] )
elseif ( value < 24 ) then
return ("%s1"):format( pht_table_notes[value - 11] )
elseif ( value < 36 ) then
return ("%s2"):format( pht_table_notes[value - 23] )
elseif ( value < 48 ) then
return ("%s3"):format( pht_table_notes[value - 35] )
elseif ( value < 60 ) then
return ("%s4"):format( pht_table_notes[value - 47] )
elseif ( value < 72 ) then
return ("%s5"):format( pht_table_notes[value - 59] )
elseif ( value < 84 ) then
return ("%s6"):format( pht_table_notes[value - 71] )
elseif ( value < 96 ) then
return ("%s7"):format( pht_table_notes[value - 83] )
elseif ( value < 108 ) then
return ("%s8"):format( pht_table_notes[value - 95] )
else
return ("%s9"):format( pht_table_notes[value - 107] )
end
end
The second function will allow you to manually write the strings (C-0, C#0 … ) in the valuebox and the function will convert it to its corresponding number.It also allows you to write the letters in miniscule and forget the " - ".
…
Please, someone who has code of this kind, could you share it?It would be interesting to have a compilation of “advanced code” for this kind of details, value conversions and so on …