New Tool (2.8): Linearly interpolate between selection in sample

This tool draws a straight line between the selection in the sampler, the starting and end point of the selection remains unchanged. Does not work when both stereo channels are selected at the same time.
Keybinding is in Sample Editor:Edit.

I needed this tool, so in case anyone else would:

EDIT: added replaced my code with dblue’s

I did some friendly fixes and clean up of your main function.

To work with both channels selected, you do of course need to take care to handle this correctly, and to calculate unique interpolated values for each channel. The selected channel value of ‘3’ is not really a valid channel index here, but merely an indication that all channels are selected.

You also had quite a lot of unnecessary repetition when accessing stuff like renoise.song():instrument(sel_inst)… over and over again. You can make your code cleaner and more efficient by taking a local copy of the object you’re interested in working with — mainly the sample buffer in this case — and then simply refer to that directly elsewhere.

:]

  
local function line_interpol()  
  
 -- Sample properties.  
 local buffer = renoise.song().selected_sample.sample_buffer  
 local sel_channel = buffer.selected_channel  
 local sel_start = buffer.selection_start  
 local sel_end = buffer.selection_end  
 local sel_length = sel_end - sel_start  
  
 -- If the selection is too short, then we have no work to do.  
 if sel_length < 3 then  
 renoise.app():show_status('Please make a larger selection.')  
 return  
 end  
  
 -- Determine which channel(s) to process.  
 local first_channel = sel_channel  
 local last_channel = sel_channel  
 if sel_channel == renoise.SampleBuffer.CHANNEL_LEFT_AND_RIGHT then  
 first_channel = 1  
 last_channel = buffer.number_of_channels  
 end  
  
 -- Calculate initial values for each channel.  
 local smp_val = {}  
 local smp_inc = {}  
 for c = first_channel, last_channel do  
 smp_val[c] = buffer:sample_data(c, sel_start)  
 smp_inc[c] = (buffer:sample_data(c, sel_end) - smp_val[c]) / sel_length  
 end  
  
 -- Process.  
 buffer:prepare_sample_data_changes()  
 for f = sel_start, sel_end do  
 for c = first_channel, last_channel do   
 buffer:set_sample_data(c, f, smp_val[c])  
 smp_val[c] = smp_val[c] + smp_inc[c]  
 end  
 end  
 buffer:finalize_sample_data_changes()   
  
end  
  

Ah, I see now, thank you.