Files
midigrid/MidiGrid_ToggleMode.lua
T

40 lines
1.1 KiB
Lua
Raw Normal View History

-- MIDI Grid: toggle grid entry mode on/off
local dir = ({ reaper.get_action_context() })[2]:match("@?(.*[\\/])")
local G = dofile(dir .. "midigrid_lib.lua")
local now = not G.isActive()
G.set("active", now and 1 or 0)
if not now then
-- Hand back the record arm/monitor state grid mode borrowed for preview.
G.restoreRouting()
G.say("Grid mode off")
return
end
local hwnd, take, err = G.editor()
if not take then
-- Mode is still armed; it simply has nothing to act on yet.
G.say("Grid mode on, " .. err)
return
end
-- Preview must sound through the instrument of the track being edited.
G.routePreview(take)
-- Park the pitch cursor on a scale tone so the very first up/down press
-- moves by a sensible degree rather than off a foreign note.
local p = G.getPitch(hwnd)
if not G.inScale(p) then
local up = G.scaleStep(p, 1)
if up then p = up ; G.setPitch(hwnd, p) end
end
local g = G.gridQN(take)
G.say(("Grid mode on, %s, grid %s, %s, %s, %s"):format(
G.scaleName(),
G.gridLabel(g),
G.isHold() and "hold on" or "hold off",
G.noteName(p),
G.cellPosText(take)))