Skip to content

Commit e423056

Browse files
author
eritiro
committed
Handle non midi items gracefully
1 parent d64eb02 commit e423056

File tree

2 files changed

+86
-77
lines changed

2 files changed

+86
-77
lines changed

Modules/MIDI.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ function rippleFollowingItems(track, item, originalLength)
103103
end
104104

105105
function getStepVelocity(item, stepIdx, pitch)
106-
if not item then return false end
107-
local take = reaper.GetActiveTake(item); if not take or not reaper.TakeIsMIDI(take) then return false end
106+
local take = reaper.GetActiveTake(item)
108107

109108
local itemPos = reaper.GetMediaItemInfo_Value(item,'D_POSITION')
110109
local secPerBt = reaper.TimeMap2_beatsToTime(0,1)
@@ -126,9 +125,7 @@ function getStepVelocity(item, stepIdx, pitch)
126125
end
127126

128127
function addMidiNote(item, stepIdx, note, velocity)
129-
if not item then return end
130-
local take = reaper.GetActiveTake(item); if not take then return end
131-
if not reaper.TakeIsMIDI(take) then return end
128+
local take = reaper.GetActiveTake(item)
132129

133130
local itemPos = reaper.GetMediaItemInfo_Value(item, 'D_POSITION')
134131
local secPerBt = reaper.TimeMap2_beatsToTime(0, 1)
@@ -144,9 +141,7 @@ function addMidiNote(item, stepIdx, note, velocity)
144141
end
145142

146143
function deleteMidiNote(item, stepIdx, note)
147-
if not item then return end
148-
local take = reaper.GetActiveTake(item); if not take then return end
149-
if not reaper.TakeIsMIDI(take) then return end
144+
local take = reaper.GetActiveTake(item)
150145

151146
local itemPos = reaper.GetMediaItemInfo_Value(item, 'D_POSITION')
152147
local secPerBt = reaper.TimeMap2_beatsToTime(0, 1)
@@ -166,3 +161,8 @@ function deleteMidiNote(item, stepIdx, note)
166161
end
167162
reaper.MIDI_Sort(take)
168163
end
164+
165+
function isMidi(item)
166+
take = reaper.GetActiveTake(item)
167+
return take and reaper.TakeIsMIDI(take)
168+
end

brute-seq.lua

Lines changed: 78 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,79 @@ local loopPattern = reaper.GetExtState("BruteSeq", "LoopPattern") == "1"
8888
local loopSong = reaper.GetExtState("BruteSeq", "LoopSong") == "1"
8989
local ripple = reaper.GetExtState("BruteSeq", "Ripple") == "1"
9090

91+
local function processPattern(currentPattern)
92+
-- Navigation Step Bar
93+
local currentStepTotal = getCurrentStep(currentPattern.item)
94+
local currentStep = currentStepTotal and (currentStepTotal % currentPattern.steps) + 1 or -1
95+
local currentTime = currentStepTotal and (currentStepTotal // currentPattern.steps) + 1 or -1
96+
97+
reaper.ImGui_PushStyleVar(ctx, reaper.ImGui_StyleVar_ItemSpacing(), 2, 2) -- (x,y)
98+
99+
drawTrackLabel(ctx, images.Channel_button_on, "Sequencer")
100+
for s=1, currentPattern.steps do
101+
reaper.ImGui_SameLine(ctx)
102+
local isCurrent = currentStep and s == currentStep
103+
drawStepCursor(isCurrent)
104+
105+
if reaper.ImGui_IsItemClicked(ctx) then
106+
jumpToStep(currentPattern.item, s - 1)
107+
end
108+
end
109+
110+
if currentPattern.times > 1 then
111+
drawTimesSeparator()
112+
for s=1, currentPattern.times do
113+
reaper.ImGui_SameLine(ctx)
114+
local isCurrent = currentTime and s == currentTime
115+
drawStepCursor(isCurrent)
116+
117+
if reaper.ImGui_IsItemClicked(ctx) then
118+
jumpToStep(currentPattern.item, (s - 1) * currentPattern.steps)
119+
end
120+
end
121+
end
122+
123+
-- Step Grid
124+
125+
for ti,trk in ipairs(tracks) do
126+
local id = '##ch' .. ti
127+
local selected = false
128+
local sprite = selected and images.Channel_button_on
129+
or images.Channel_button_off
130+
131+
drawTrackLabel(ctx, sprite, trk.name)
132+
reaper.ImGui_SameLine(ctx)
133+
134+
for s=1, currentPattern.steps do
135+
local stepVelocity = getStepVelocity(currentPattern.item, s, trk.note)
136+
local active = stepVelocity ~= nil
137+
local odd = ((s-1)//4)%2==0
138+
local accent = active and (stepVelocity > accentThreshold)
139+
140+
drawStepButton(active, odd, accent)
141+
142+
if s < currentPattern.steps then reaper.ImGui_SameLine(ctx) end
143+
144+
local clicked = reaper.ImGui_IsItemClicked(ctx)
145+
if clicked then
146+
local shift = reaper.ImGui_IsKeyDown(ctx, reaper.ImGui_Key_LeftShift())
147+
148+
local shouldDelete = active
149+
local shouldCreate = not active or (shift ~= accent)
150+
151+
if shouldDelete then
152+
deleteMidiNote(currentPattern.item, s, trk.note)
153+
end
154+
if shouldCreate then
155+
local newVelocity = shift and accentVelocity or normalVelocity
156+
addMidiNote(currentPattern.item, s, trk.note, newVelocity)
157+
end
158+
end
159+
end
160+
end
161+
reaper.ImGui_PopStyleVar(ctx)
162+
end
163+
91164
local function loop()
92165
passThroughShortcuts(ctx)
93166
reaper.ImGui_SetNextWindowSize(ctx,900,420,reaper.ImGui_Cond_FirstUseEver())
@@ -204,76 +277,12 @@ local function loop()
204277

205278
reaper.ImGui_Separator(ctx)
206279

207-
-- Navigation Step Bar
208-
local currentStepTotal = getCurrentStep(currentPattern.item)
209-
local currentStep = currentStepTotal and (currentStepTotal % currentPattern.steps) + 1 or -1
210-
local currentTime = currentStepTotal and (currentStepTotal // currentPattern.steps) + 1 or -1
211-
212-
reaper.ImGui_PushStyleVar(ctx, reaper.ImGui_StyleVar_ItemSpacing(), 2, 2) -- (x,y)
213-
214-
drawTrackLabel(ctx, images.Channel_button_on, "Sequencer")
215-
for s=1, currentPattern.steps do
216-
reaper.ImGui_SameLine(ctx)
217-
local isCurrent = currentStep and s == currentStep
218-
drawStepCursor(isCurrent)
219-
220-
if reaper.ImGui_IsItemClicked(ctx) then
221-
jumpToStep(currentPattern.item, s - 1)
222-
end
223-
end
224-
225-
if currentPattern.times > 1 then
226-
drawTimesSeparator()
227-
for s=1, currentPattern.times do
228-
reaper.ImGui_SameLine(ctx)
229-
local isCurrent = currentTime and s == currentTime
230-
drawStepCursor(isCurrent)
231-
232-
if reaper.ImGui_IsItemClicked(ctx) then
233-
jumpToStep(currentPattern.item, (s - 1) * currentPattern.steps)
234-
end
235-
end
280+
if isMidi(currentPattern.item) then
281+
processPattern(currentPattern)
282+
else
283+
reaper.ImGui_AlignTextToFramePadding(ctx)
284+
reaper.ImGui_Text(ctx, "Non MIDI items are not supported in the sequencer track")
236285
end
237-
238-
-- Step Grid
239-
240-
for ti,trk in ipairs(tracks) do
241-
local id = '##ch' .. ti
242-
local selected = false
243-
local sprite = selected and images.Channel_button_on
244-
or images.Channel_button_off
245-
246-
drawTrackLabel(ctx, sprite, trk.name)
247-
reaper.ImGui_SameLine(ctx)
248-
249-
for s=1, currentPattern.steps do
250-
local stepVelocity = getStepVelocity(currentPattern.item, s, trk.note)
251-
local active = stepVelocity ~= nil
252-
local odd = ((s-1)//4)%2==0
253-
local accent = active and (stepVelocity > accentThreshold)
254-
255-
drawStepButton(active, odd, accent)
256-
257-
if s < currentPattern.steps then reaper.ImGui_SameLine(ctx) end
258-
259-
local clicked = reaper.ImGui_IsItemClicked(ctx)
260-
if clicked then
261-
local shift = reaper.ImGui_IsKeyDown(ctx, reaper.ImGui_Key_LeftShift())
262-
263-
local shouldDelete = active
264-
local shouldCreate = not active or (shift ~= accent)
265-
266-
if shouldDelete then
267-
deleteMidiNote(currentPattern.item, s, trk.note)
268-
end
269-
if shouldCreate then
270-
local newVelocity = shift and accentVelocity or normalVelocity
271-
addMidiNote(currentPattern.item, s, trk.note, newVelocity)
272-
end
273-
end
274-
end
275-
end
276-
reaper.ImGui_PopStyleVar(ctx)
277286
else
278287
reaper.ImGui_SameLine(ctx)
279288
reaper.ImGui_AlignTextToFramePadding(ctx)

0 commit comments

Comments
 (0)