Icon representing a recipe

Recipe: segment index adjustment from trim

created by bravosk8erboy

Profile


Name
segment index adjustment from trim
ID
108963
Shared with
Public
Parent
None
Children
None
Created on
February 07, 2025 at 06:38 AM UTC
Updated on
February 09, 2025 at 06:01 AM UTC
Description

segment index change using the trim tool.
also a segment buffer of 1 on each side is added.

Best for


Code


--Objective-------------------------------------------------------------------------------------------------------------------- -- figuring out segment index post trim is trick. a buffer on both sides of a selection is added, then unselected index are removed. -- trying to select a specific segment index after trim is tricky the numbers all change. -- this script trys to establish a plan for handling segment index continuity through trim. --Notes ----------------------------------------------------------------------------------------------------------------------- --table.setn(selected_array, 10000) -- table.setn is obsoleted in current lua --#(selected_array) -- "table.getn()" is now "#()" -- in order to reduce the size of a table just make "table[end+1] = nil" --print ("size: " .. #selected_array) ------------------------------------------------------------------------------------------------------------------------------------- -- -- generates a table of selected segments. This is important because fold.it decides after time that these are the only segments left. -- after trim a segment index will be changed to a lower value based on how many segments with index lower were removed -- example if you have selected segments index {1,5,8} post trim these will become segment index {1,2,3} -- reselecting segment index 5 after the trim is tricky because it will become index 2 -- but finding the index of value 5 in this new array will give us the new index post trim of 2 -- it will always be more efficent to generate this array while selecting the segments then set "selected_array[selection_count+1] = nil" -- -- use this function before trim function generate_table_of_selected_segments() local selected_array = {} local selection_count = selection.GetCount() --get count of selected indexes print("selection count: " ..selection_count) selected_array[selection_count+1] = nil --make oversized arrays the correct length for this selection print("selection count2: " ..selection_count) structure_get_count = structure.GetCount() i=1 for j=1 , structure_get_count do if selection.IsSelected(j) then selected_array[i] = j i=i+1 if i>selection_count then selected_array[i] = nil -- signals end of array for length command #(selected_array) return selected_array end end end print(" test: should break before this ") return selected_array end -- the trim tool actually adds a segment index for the segment before and after the trimmed selection indexes. -- some segments will get removed from the segment index count lowering some segment index values -- if i select segment index {1,3,10} when i trim segments, 1-4 and 9-11 will remain. for a total of 7 segment index post trim -- example, segment index {1,3,10} will become {1,3,6} -- example, segment index {5,12,20,21} will become {2,5,8,9} becasue 4-6,11-13,19-22 will be included in the trim for a total of 10 segments function buffer_selection_array(selection_array) local buffer_array = {} local structure_count = structure.GetCount() buffer_array[structure_count] = nil -- make the array long to we arn't slow on a ton of resizes j=1 --the pointer for the buffer_array. if selection_array[1] == 1 then j=0 --edge case for segment index zero end for i=1 , #(selection_array) do -- this will add a buffer to both side of a selection. will create duplicates buffer_array[j] = (selection_array[i] - 1) -- one before selection index --for segment zero this will get trimmed off buffer_array[j+1] = selection_array[i] -- selection_array value buffer_array[j+2] = (selection_array[i] + 1) -- one after selection index -- for last segment this will get trimmed off later j=j+3 end buffer_array[0] = nil -- remove fake first segment (segment index zero edge case from above) while buffer_array[#(buffer_array)] > structure_count do buffer_array[#(buffer_array)] = nil -- remove edge case where we buffer past the last segment (structure.GetCount() + 1) end --remove duplicates ------ slightly stolen local hash = {} local res = {} for _,v in ipairs(buffer_array) do -- "_" used as name for unused variable if (not hash[v]) then res[#res+1] = v -- you could print array here instead hash[v] = true end end --return buffer_array return res end --find the new post-trim segment index from the pre-trim segment index function post_trim_index_of_pre_trim_segment(array, pre_trim_segment_index) for post_trim_segment_index, v in ipairs(array) do if v == pre_trim_segment_index then return post_trim_segment_index end end return nil end -- selects multip segments -- finds new index of post trim segments -- selection_array can be any array of pre trim segment index you want to select. it will look up the adjusted segment index post-trim -- post_trim_array is the fully buffered array of the trim. -- a value in post_trim_array is the segment index pre-trim -- a values index in post_trim_array is the segment index post trim after the buffer -- example "post_trim_array[5] = 200" means that pre-trim segment index was 200 and post trim segment index is 5 -- both arrays must be sorted low to high -- value in selection_array must exist in post_trim_array function select_SEGMENTS_post_trim(selection_array,post_trim_array) post_trim_array_index = 1 for selection_array_segment_index=1, #selection_array do while selection_array[selection_array_segment_index] > post_trim_array[post_trim_array_index] do post_trim_array_index = post_trim_array_index + 1 end selection.Select(post_trim_array_index) -- TODO add edge case to where pre trim segment is not in post_trim_array_index -- this currently will break the whole loop end end -- selects one segment -- finds new index of post trim segment function select_segment_post_trim(pre_trim_index,post_trim_array) selection.Select(post_trim_index_of_pre_trim_segment(post_trim_array, pre_trim_index)) end function print_array(the_array) print(table.concat(the_array,", ")) end function demo() pre_trim_index = 10 selection.Select(10) selection_array = generate_table_of_selected_segments() print_array(selection_array) post_trim_array = buffer_selection_array(selection_array) -- print_array(post_trim_array) print("pre trim index: " ..pre_trim_index) post_trim_index = post_trim_index_of_pre_trim_segment(post_trim_array, pre_trim_index) print(pre_trim_index.." post trim index: " ..post_trim_index) structure.TrimPose() -- recommend safe untrim and safe trim by LociOiling -- https://fold.it/recipes/108773 -- https://fold.it/recipes/108774 -- selects a specific pre trim segment at the post trim index select_segment_post_trim(pre_trim_index,post_trim_array) selection.DeselectAll() -- selection array can be any array of segments but right now they must exist in the post_trim_array -- selects a array of pre trim segments at the post trim index select_SEGMENTS_post_trim(selection_array,post_trim_array) end demo()

Comments