Files

36 lines
1.2 KiB
Lua
Raw Permalink Normal View History

-- MIDI Grid: choose the root and scale that up/down navigate by
local dir = ({ reaper.get_action_context() })[2]:match("@?(.*[\\/])")
local G = dofile(dir .. "midigrid_lib.lua")
local names = {}
for i, s in ipairs(G.SCALES) do names[#names + 1] = i .. "=" .. s.name end
local ok, csv = reaper.GetUserInputs(
"MIDI Grid scale", 2,
"Root note (C, F#, Bb...),Scale number (" .. table.concat(names, " ") .. "),extrawidth=320",
G.noteNamePc(G.scaleRoot()) .. "," .. G.scaleIndex())
if not ok then return end
local rootStr, scaleStr = csv:match("^([^,]*),(.*)$")
-- Parse a root spelled with either sharps or flats.
local BASE = { c = 0, d = 2, e = 4, f = 5, g = 7, a = 9, b = 11 }
local letter, accid = tostring(rootStr):match("^%s*(%a)([#b]?)")
if not letter or not BASE[letter:lower()] then
G.say("Unrecognised root note")
return
end
local pc = BASE[letter:lower()]
if accid == "#" then pc = pc + 1 elseif accid == "b" then pc = pc - 1 end
pc = pc % 12
local si = math.floor(tonumber(scaleStr) or 0)
if si < 1 or si > #G.SCALES then
G.say("Scale number must be 1 to " .. #G.SCALES)
return
end
G.set("root", pc)
G.set("scale", si)
G.say("Scale set to " .. G.scaleName())