Solved!!!
Finally I opted for a bungled solution.In order to select the first track (the T3) at the end of the process,is essential whenever the first track (T3) is empty. So, I added an empty track, which is then removedautomaticallyin the new function delete_tracks_empty.This code works perfectly!!!:
local group_settings = {
ten_octaves = {
name = "Octaves",
color = { 0x80,0x80,0x80 },
color_blend = 27,
collapsed = false,
tracks = {
{ name = "empty" }, -- to select first track always
{ name = "0 Oct", volume_column_visible = true, delay_column_visible = true, collapsed = true, color = { 0x70,0x70,0x70 }, color_blend = 10 },
{ name = "1 Oct", volume_column_visible = true, delay_column_visible = true, collapsed = true, color = { 0x40,0x40,0x40 }, color_blend = 10 },
{ name = "2 Oct", volume_column_visible = true, delay_column_visible = true, collapsed = true, color = { 0x70,0x70,0x70 }, color_blend = 10 },
{ name = "3 Oct", volume_column_visible = true, delay_column_visible = true, collapsed = true, color = { 0x40,0x40,0x40 }, color_blend = 10 },
{ name = "4 Oct", volume_column_visible = true, delay_column_visible = true, collapsed = true, color = { 0x70,0x70,0x70 }, color_blend = 10 },
{ name = "5 Oct", volume_column_visible = true, delay_column_visible = true, collapsed = true, color = { 0x40,0x40,0x40 }, color_blend = 10 },
{ name = "6 Oct", volume_column_visible = true, delay_column_visible = true, collapsed = true, color = { 0x70,0x70,0x70 }, color_blend = 10 },
{ name = "7 Oct", volume_column_visible = true, delay_column_visible = true, collapsed = true, color = { 0x40,0x40,0x40 }, color_blend = 10 },
{ name = "8 Oct", volume_column_visible = true, delay_column_visible = true, collapsed = true, color = { 0x70,0x70,0x70 }, color_blend = 10 },
{ name = "9 Oct", volume_column_visible = true, delay_column_visible = true, collapsed = true, color = { 0x40,0x40,0x40 }, color_blend = 10 },
},
},
}
local column_names = {
"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
}
function insert_track(track_index, group_index, track_setting)
renoise.song():insert_track_at(track_index)
local track = renoise.song():track(track_index)
track.visible_effect_columns, track.visible_note_columns = 0, 12, false -- track.volume_column_visible
for parameter_name, parameter_value in pairs(track_setting) do
track[parameter_name] = parameter_value
end
for note_column = 1, 12 do
track:set_column_name(note_column, column_names[note_column])
end
renoise.song():add_track_to_group(track_index, group_index)
end
function insert_gt(group_setting)
local song = renoise.song()
local i, group_index, group_setting = 1, song.selected_track_index + 1, group_settings[group_setting]
song:insert_group_at(group_index)
for parameter_name, parameter_value in pairs(group_setting) do
if not (parameter_name == "tracks") then
song:track(group_index)[parameter_name] = parameter_value
end
end
for _, track_setting in ipairs(group_setting.tracks) do
local group_index = group_index + i
i = i + 1
insert_track(song.selected_track_index, group_index, track_setting)
end
song.tracks[song.selected_track_index + 1].group_parent.collapsed = true -- collapse FX in Group
end
function explode_pitches(track_index, first_octave_track_index)
local song = renoise.song()
local track = song:track(song.selected_track_index)
local pattern_iterator = song.pattern_iterator:note_columns_in_track(track_index, true)
local notes = { }
local sort_per_column = function(a,b)
if a.column < b.column then return true
elseif a.column > b.column then return false
elseif a.pattern < b.pattern then return true
elseif a.pattern > b.pattern then return false
elseif a.line < b.line then return true
elseif a.line > b.line then return false
end
end
for pos, note_column in pattern_iterator do
if not note_column.is_empty then
table.insert(notes, { note_column = note_column, pattern = pos.pattern, track = pos.track, column = pos.column, line = pos.line })
end
end
table.sort(notes, sort_per_column)
for i, note in ipairs(notes) do
if not (note.note_column.note_value == 120) then
local dest_track = first_octave_track_index + math.floor(note.note_column.note_value/12)
local dest_column = (note.note_column.note_value % 12) + 1
song:pattern(note.pattern):track(dest_track):line(note.line):note_column(dest_column):copy_from(song:pattern(note.pattern):track(note.track):line(note.line):note_column(note.column))
if notes[i+1] then
if (notes[i].column == notes[i+1].column) and (song:pattern(notes[i+1].pattern):track(dest_track):line(notes[i+1].line):note_column(dest_column).note_value == 121) then
local next_note = song:pattern(notes[i+1].pattern):track(dest_track):line(notes[i+1].line):note_column(dest_column)
next_note.note_value = 120
next_note.delay_value = notes[i+1].note_column.delay_value
end
end
end
end
end
function delete_tracks_empty()
local song = renoise.song()
local tindex = song.selected_track_index
song:track(tindex).collapsed = true -- original track
song:track(tindex).color_blend = 10
song:track(tindex).color = { 0xFF,0x00,0x00 }
song.tracks[tindex]:mute()
local first_octave_track = tindex + 1
for group_index = first_octave_track + 10, first_octave_track, - 1 do
local was_empty = false
for _, pattern in ipairs(song.patterns) do
if not pattern:track(group_index).is_empty then
was_empty = true
end
end
if not was_empty then
song:delete_track_at(group_index)
end
end
song:track(tindex + 1).collapsed = false -- select first track
end
function rebuild_menu()
local song, menu_entry = renoise.song(), "Pattern Editor:Track:Explode Pitches To Octaves"
if renoise.tool():has_menu_entry(menu_entry) then
renoise.tool():remove_menu_entry(menu_entry)
end
if (song.selected_track.type == renoise.Track.TRACK_TYPE_SEQUENCER) then
renoise.tool():add_menu_entry {
name = menu_entry,
invoke = function() insert_gt("ten_octaves") explode_pitches(song.selected_track_index, song.selected_track_index + 2) delete_tracks_empty()
end }
end
end
function start()
renoise.song().selected_track_observable:add_notifier(rebuild_menu)
rebuild_menu()
end
renoise.tool().app_new_document_observable:add_notifier(start)
I do not like how are these lines:
local tindex = song.selected_track_index
song:track(tindex).collapsed = true -- original track
song:track(tindex).color_blend = 10
song:track(tindex).color = { 0xFF,0x00,0x00 }
How you could arrange better? Something like the first lines of code above.