I don’t mind too much if you make edits, but if you do plan to release it publicly then please make it very obvious that it’s a modified version by you, and not the original version by me. People should be aware which exact version they are using, just to avoid confusion. If you want to incorporate my pattern resizing functions into your own tool, that’s also totally fine as long as proper credit is given
Regarding your line index bug, the pattern lines are indexed starting from 1 in the Lua API, not from 0.
To compensate for that in your calculations, first subtract 1 from the selected index, then multiply or divide it, then add 1 back to the result.
-- Expand
local sli = ((renoise.song().selected_line_index - 1) * 2) + 1
-- Shrink
local sli = ((renoise.song().selected_line_index - 1) / 2) + 1
You’re also missing quite a bunch of sanity/safety checks in your modified code, so it’s very easy to expand/shrink everything to some invalid LPB, invalid line index, and so on. Exactly how you prefer to handle this is up to you, but you can simply wrap your calculations with math.min() and math.max() to apply some crude range limiting.
local sli = math.max(1, math.min(renoise.song().selected_pattern.number_of_lines, ((renoise.song().selected_line_index - 1) * 2) + 1 ))
I took the liberty of modifying your functions to include these changes.
renoise.tool():add_keybinding {
name = "Pattern Editor:Pattern:Resize x2",
invoke = function()
-- Reference to song
local rs = renoise.song()
-- Resize pattern
resize_pattern(rs.selected_pattern, adjust_pattern_length(2))
-- Adjust selected line index
local sli = rs.selected_line_index
local max_lines = rs.selected_pattern.number_of_lines
local new_sli = math.max(1, math.min(max_lines, ((sli - 1) * 2) + 1 ))
rs.selected_line_index = new_sli
-- Adjust LPB
local t = rs.transport
t.lpb = math.max(1, math.min(256, t.lpb * 2))
-- Move transport to the new position and resume/stop playback
local playing = t.playing
t:start_at(new_sli)
if (not playing) then t:stop() end
end
}
renoise.tool():add_keybinding {
name = "Pattern Editor:Pattern:Resize x0.5",
invoke = function()
-- Reference to song
local rs = renoise.song()
-- Adjust selected line index
local sli = rs.selected_line_index
local max_lines = rs.selected_pattern.number_of_lines
local new_sli = math.max(1, math.min(max_lines, ((sli - 1) / 2) + 1 ))
rs.selected_line_index = new_sli
-- Adjust LPB
local t = rs.transport
t.lpb = math.max(1, math.min(256, t.lpb / 2))
-- Move transport to the new position and resume/stop playback
local playing = t.playing
t:start_at(new_sli)
if (not playing) then t:stop() end
-- Resize pattern
resize_pattern(rs.selected_pattern, adjust_pattern_length(1/2))
end
}
There are other things you might consider, like preventing the entire expand/shrink operation if the LPB or other values would theoretically go out of bounds, but I leave that up to you.
All in all, these are pretty good additions, so I will likely include some variation of them in my original tool… if I ever get around to updating the damn thing!