2026-08-14 18:13:45 +02:00
|
|
|
--[[
|
|
|
|
|
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
|
|
|
|
|
|
2026-08-24 01:21:21 +02:00
|
|
|
-- 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()
|
|
|
|
|
|
2026-08-14 18:13:45 +02:00
|
|
|
-- 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
|
|
|
|
|
|
2026-08-24 01:21:21 +02:00
|
|
|
take = reaper.MIDIEditor_GetTake(hwnd) or take
|
|
|
|
|
|
2026-08-14 18:13:45 +02:00
|
|
|
G.set("active", 1)
|
|
|
|
|
|
2026-08-24 00:55:25 +02:00
|
|
|
-- Preview must sound through this track's instrument, whatever else is armed.
|
2026-08-24 01:21:21 +02:00
|
|
|
G.routePreview(take)
|
2026-08-24 00:55:25 +02:00
|
|
|
|
2026-08-24 01:21:21 +02:00
|
|
|
-- Stay where you were listening, on the nearest cell boundary, rather than
|
|
|
|
|
-- jumping back to the top of the item.
|
|
|
|
|
G.snapCursorToCell(take, cur)
|
2026-08-14 18:13:45 +02:00
|
|
|
|
|
|
|
|
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)))
|