-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathrolandmaj7_Insert bars at cursor position (splits items).lua
More file actions
243 lines (222 loc) · 9.86 KB
/
rolandmaj7_Insert bars at cursor position (splits items).lua
File metadata and controls
243 lines (222 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
-- @description Insert bars at cursor position (splits items)
-- @author RolandMaj7
-- @version 1.0
-- @changelog Added 'selected tracks' scope to control item split condition. Code cleanup for production.
-- @provides . > RolandMaj7_Insert_bars_at_cursor_position
-- @about
-- # Insert Empty Bars at Cursor (Sibelius-style)
--
-- ## Purpose
-- This script simulates how **Avid Sibelius** inserts bars into a score:
-- everything after the edit cursor is shifted forward by whole bars, and bar numbering is preserved.
-- Unlike quarter-note math approaches, this script is **bar index–based**, so it works correctly in any local time signature.
--
-- ## What the script does
-- - Prompts the user for **barCount** (integer ≥ 1).
-- - Splits items on **selected tracks** at the edit cursor (so only the right halves move).
-- - Moves everything at or after the cursor forward by `barCount` bars:
-- - **Tempo/Time-Signature markers** are shifted by measure index, preserving BPM, TS, and linear flags.
-- - **Project markers and regions** are shifted; regions spanning the cursor grow by the inserted bars.
-- - **Items on selected tracks** are repositioned by bar index, preserving their musical alignment.
-- - Preserves all musical data: time signatures, marker options, item lengths, and stretch markers.
--
-- ## Why this is needed
-- - In REAPER, “Insert empty space” works in absolute time/beats, not in **musical bars**.
-- - In notation programs like Sibelius, “Insert Bars” respects the local meter so that bar numbers advance consistently.
-- - This script bridges that gap: it ensures that if you are on bar **91** and insert **1 bar**, the next downbeat is **92.1.00**, no matter whether the section is in 4/4, 3/4, 6/8, or a partial measure.
--
-- ## Usage
-- 1. Place the **edit cursor** at the exact bar position where the new bars should be inserted.
-- The cursor’s bar index becomes the anchor: inserted bars start *at that bar*, and everything after shifts forward.
-- 2. Select the **tracks/items** you want affected (only items on selected tracks will move).
-- 3. Run the script and enter the number of bars to insert.
-- 4. The cursor bar is preserved; all later bars are renumbered by the inserted amount.
--
-- ## Notes
-- - Items are always **split at the cursor** first; left portions remain fixed, right portions move.
-- - Project markers/regions at or after the cursor are shifted by bar index; spanning regions grow.
-- - Undo is supported: the whole operation is wrapped in a single undo point.
-- - To affect all tracks, select them all before running.
-- RolandMaj7_Insert_Bars_At_Cursor_Position_By_Measure.lua
-- REAPER v7.45+
-- Inserts N empty bars at the edit cursor, using MEASURE/BEAT remapping
-- (not QN math) so the next bar number becomes current_bar+N regardless
-- of local time signatures or partial measures.
--
-- Behavior
-- • Prompts for barCount (integer ≥ 1)
-- • Splits items on SELECTED TRACKS at the cursor (only right halves move)
-- • Moves:
-- - Tempo/TS markers at/after cursor by +barCount measures
-- - Project markers/regions at/after cursor by +barCount measures
-- * Regions spanning the cursor: end moves, start stays → region length grows
-- - Items on SELECTED TRACKS with start ≥ cursor by +barCount measures
-- • Preserves: BPM, TS (num/den), linear flag, marker names/colors, item lengths
-- • One undo point, clear summary
local r = reaper
local EPS = 1e-9
local function log(s) r.ShowConsoleMsg(tostring(s).."\n") end
-- ---- Input ----
local ok, sval = r.GetUserInputs("Insert empty bars at cursor", 1, "Number of bars (integer ≥ 1)", "1")
if not ok then return end
local barCount = tonumber(sval or "")
if not barCount or barCount < 1 or math.floor(barCount) ~= barCount then
r.MB("Please enter an integer ≥ 1.","Invalid input",0); return
end
-- ---- Helpers (measure/beat mapping) ----
-- Time -> (measures, beat_in_measure, cml, cdenom)
local function time_to_measbeat(t)
local beat_in_measure, measures, cml, fullbeats, cdenom = r.TimeMap2_timeToBeats(0, t)
return measures, beat_in_measure, cml, cdenom
end
-- (measures, beat_in_measure) -> time
local function measbeat_to_time(measures, beat_in_measure)
return r.TimeMap2_beatsToTime(0, beat_in_measure, measures)
end
local function get_cursor() return r.GetCursorPosition() end
-- ---- Capture + shift: Tempo/TS markers ----
local function capture_tempo_from(t0)
local list = {}
local n = r.CountTempoTimeSigMarkers(0)
for i=0, n-1 do
local ok, t, meas, beat, bpm, tsn, tsd, linear = r.GetTempoTimeSigMarker(0, i)
if ok and t >= t0 - EPS then
-- Derive precise measure/beat at *current* t via TimeMap2 (robust to partial measures)
local M, B = time_to_measbeat(t)
list[#list+1] = {
idx=i, t=t, M=M, B=B,
bpm=bpm, tsn=tsn, tsd=tsd, linear=linear
}
end
end
return list
end
local function shift_tempo_markers_by_measures(captured, deltaMeasures)
if #captured == 0 then return 0 end
-- Delete affected markers first (from end) to avoid reindex issues
local toDel = {}
for _, m in ipairs(captured) do toDel[m.idx] = true end
for i = r.CountTempoTimeSigMarkers(0)-1, 0, -1 do
if toDel[i] then r.DeleteTempoTimeSigMarker(0, i) end
end
-- Re-add using MEASURE/BEAT addressing so partial-measure semantics are preserved
local added = 0
for _, m in ipairs(captured) do
local newM = m.M + deltaMeasures
-- Place by measure/beat (timepos = -1; measurepos/newM, beatpos/m.B)
local ok = r.SetTempoTimeSigMarker(0, -1, -1, newM, m.B, m.bpm, m.tsn, m.tsd, m.linear or 0)
if ok then added = added + 1 end
end
return added
end
-- ---- Capture + shift: Project markers/regions ----
local function capture_proj_markers_regions(t0)
local starts, ends = {}, {}
local nm, nr = r.CountProjectMarkers(0)
for i=0, nm+nr-1 do
local ok, isrgn, pos, rgnend, name, idx, color = r.EnumProjectMarkers3(0, i)
if ok then
if pos >= t0 - EPS then
local M, B = time_to_measbeat(pos)
starts[#starts+1] = { idx=idx, isrgn=isrgn, M=M, B=B, name=name, color=color }
end
if isrgn and rgnend >= t0 - EPS then
local Me, Be = time_to_measbeat(rgnend)
ends[#ends+1] = { idx=idx, Me=Me, Be=Be }
end
end
end
return starts, ends
end
local function apply_proj_marker_region_shifts(starts, ends, deltaMeasures)
local movedS, movedE = 0, 0
-- Move starts
for _, s in ipairs(starts) do
local newStart = measbeat_to_time(s.M + deltaMeasures, s.B)
local ok
if s.isrgn then
local _, _, _, rgnend = r.EnumProjectMarkers3(0, s.idx)
ok = r.SetProjectMarker3(0, s.idx, true, newStart, rgnend, s.name or "", s.color or 0)
else
ok = r.SetProjectMarker3(0, s.idx, false, newStart, 0, s.name or "", s.color or 0)
end
if ok then movedS = movedS + 1 end
end
-- Move region ends (so regions spanning the cursor grow by the insert)
for _, e in ipairs(ends) do
local curOK, _, pos, _, name, idx, color = r.EnumProjectMarkers3(0, e.idx)
local newEnd = measbeat_to_time(e.Me + deltaMeasures, e.Be)
local ok = r.SetProjectMarker3(0, e.idx, true, pos, newEnd, name or "", color or 0)
if ok then movedE = movedE + 1 end
end
return movedS, movedE
end
-- ---- Items on selected tracks ----
local function split_selected_track_items_at_cursor()
-- 40012: Item: Split items at edit cursor
r.Main_OnCommand(40012, 0)
end
local function capture_items_on_selected_tracks_from(t0)
local cap = {}
local trc = r.CountSelectedTracks(0)
for ti=0, trc-1 do
local tr = r.GetSelectedTrack(0, ti)
if tr then
local n = r.CountTrackMediaItems(tr)
for i=0, n-1 do
local it = r.GetTrackMediaItem(tr, i)
if it then
local pos = r.GetMediaItemInfo_Value(it, "D_POSITION")
if pos >= t0 - EPS then
local M, B = time_to_measbeat(pos)
cap[#cap+1] = { it=it, M=M, B=B }
end
end
end
end
end
return cap
end
local function move_items_by_measures(cap, deltaMeasures)
local moved = 0
for _, c in ipairs(cap) do
if r.ValidatePtr2(0, c.it, "MediaItem*") then
local newPos = measbeat_to_time(c.M + deltaMeasures, c.B)
r.SetMediaItemInfo_Value(c.it, "D_POSITION", newPos)
moved = moved + 1
end
end
return moved
end
-- ---- MAIN ----
r.Undo_BeginBlock2(0)
r.PreventUIRefresh(1)
-- r.ClearConsole()
local tCursor = get_cursor()
-- 1) Split selected-track items at cursor
split_selected_track_items_at_cursor()
-- 2) Snapshot affected things under the *current* map
local tempoCap = capture_tempo_from(tCursor)
local pmStarts, pmEnds = capture_proj_markers_regions(tCursor)
local itemsCap = capture_items_on_selected_tracks_from(tCursor)
-- 3) Apply shifts by +barCount MEASURES
local tempoMoved = shift_tempo_markers_by_measures(tempoCap, barCount)
local markMovedS, markMovedE = apply_proj_marker_region_shifts(pmStarts, pmEnds, barCount)
local itemsMoved = move_items_by_measures(itemsCap, barCount)
r.UpdateArrange()
r.UpdateTimeline()
r.PreventUIRefresh(-1)
r.Undo_EndBlock2(0, string.format("Insert %d bar(s) at cursor (by measure)", barCount), -1)
-- ---- Summary ----
r.ShowConsoleMsg("")
log("=== Insert Empty Bars (Measure-accurate) ===")
do
local Mcur, Bcur = time_to_measbeat(tCursor)
log(string.format("Cursor: time=%.6f | measure=%d | beat-in-measure=%.6f", tCursor, Mcur, Bcur))
end
log(string.format("Bars inserted: %d (everything at/after cursor moved by +%d measures)", barCount, barCount))
log(string.format("Tempo/TS markers moved: %d", tempoMoved))
log(string.format("Project markers moved (starts): %d, (region ends): %d", markMovedS, markMovedE))
log(string.format("Items moved (selected tracks): %d", itemsMoved))
log("Note: Items were split at the cursor; left parts stayed. BPM/TS/linear flags preserved.")
log("===========================================")