Sample Range In Lua Question

having trouble scripting the following…

  1. I select a sample range and I want to copy into new instrument
  2. I select a sample range and I wnt to trim (like right click on sample and trim from menu)

I have been trying for 2 days with no success

I even pulled apart vvoois.RenderSlicesToNewInstrument TOOL and could not figure this one out… all help is welcome!

The for() loop seems a tad slow, maybe someone else can pitch in with an optimization.

local rs =  
local original = rs.selected_sample.sample_buffer  
local my_range = original.selection_range  
local num_frames = my_range[2] - my_range[1] + 1  
local new_inst = rs:insert_instrument_at(#rs.instruments + 1)  
new_inst.samples[1].name = "My Test"  
original.sample_rate, original.bit_depth, original.number_of_channels, num_frames  
for channel = 1, original.number_of_channels do  
 for frame = my_range[1], my_range[2] do  
 local value = original:sample_data(channel, frame)  
 new_inst.samples[1].sample_buffer:set_sample_data(channel, frame - my_range[1] + 1, value)  

thanks any idea why the naming doesn’t work?

new_inst.samples[1].name = "My Test"  

Works for me?

This is the sample name (Bottom pane, Instrument Settings, Sample). Did you mean you wanted to change the instrument name? Try: = "My Test"  

You’re trying to name a sample that does not really exist yet. Try setting the name after you’ve called create_sample_data() instead.

PS: if you run the above code without selecting a real sample you will get:

*** std::logic_error: 'can not access properties of a sample buffer with no sample data.'  

You will have to compensate. E.g. wrap in something like

if original.has_sample_data then   

thanks for all the help everybody!