Files

50 lines
1.6 KiB
Lua
Raw Permalink Normal View History

--[[
MIDI Grid: open the selected item in the MIDI editor and enter grid mode.
This is the counterpart to the normal "open in MIDI editor" action: it does
the same thing, then arms grid mode and announces the starting state, so a
single keystroke takes you from a selected item to writing notes.
]]
local dir = ({ reaper.get_action_context() })[2]:match("@?(.*[\\/])")
local G = dofile(dir .. "midigrid_lib.lua")
local item = reaper.GetSelectedMediaItem(0, 0)
if not item then G.say("No item selected") return end
local take = reaper.GetActiveTake(item)
if not take or not reaper.TakeIsMIDI(take) then G.say("Selected item is not MIDI") return end
-- Opening the editor parks the cursor at the item start; remember where the
-- user actually was so it can be handed back.
local cur = reaper.GetCursorPosition()
-- Item: Open in built-in MIDI editor (set default behaviour in preferences)
reaper.Main_OnCommand(40153, 0)
local hwnd = reaper.MIDIEditor_GetActive()
if not hwnd then G.say("Could not open MIDI editor") return end
take = reaper.MIDIEditor_GetTake(hwnd) or take
G.set("active", 1)
-- Preview must sound through this track's instrument, whatever else is armed.
G.routePreview(take)
-- Stay where you were listening, on the nearest cell boundary, rather than
-- jumping back to the top of the item.
G.snapCursorToCell(take, cur)
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
G.say(("Grid mode on, %s, grid %s, %s, %s, %s"):format(
G.scaleName(),
G.gridLabel(G.gridQN(take)),
G.isHold() and "hold on" or "hold off",
G.noteName(p),
G.cellPosText(take)))