[Fixed 2.6] Problem With Undo On Sample Buffer Operations

there must be something wrong with my brain recently, but I can’t see what I am doing wrong in this script which swaps stereo channels of sample (I have removed some idiot-proof stuff just to make it clearer):

  
  
local sample_buffer = nil  
  
function main()  
  
 sample_buffer = renoise.song().selected_sample.sample_buffer  
  
 local sample_data_buffer = 0  
  
 for sample_number = sample_buffer.selection_start, sample_buffer.selection_end do  
  
 sample_data_buffer = sample_buffer:sample_data(1, sample_number)  
  
 sample_buffer:set_sample_data(1, sample_number, sample_buffer:sample_data(2, sample_number))  
 sample_buffer:set_sample_data(2, sample_number, sample_data_buffer)  
  
 end  
  
 sample_buffer:finalize_sample_data_changes()  
  
end  
  

the problem: the channel swapping works, but undo seems not to update the sample data, while redo seems to work fine if done after two undo’s.

this is a tipical consequence of a missing finalize_sample_data_changes() call, but it is there…

attached is the complete version of the script.

1303 com_renoise_SampleChannelsSwapper_V0_1_0.xrnx

ok, looks like I forgot that the good way to perform such kind of actions is creating a whole new buffer from scratch

Ehm no. This should work without creating a new buffer.

Actually we’ll need a
sample_buffer:prepare_sample_data_changes()
as counterpart to
sample_buffer:finalize_sample_data_changes()

Sorry for not thinking about this before.

I’ll add “prepare_sample_data_changes” for the next update. NOT calling it, will do what is done now in order to be backwards compatible, but all tools should be changed to make use of both properly:

->

  
  
local sample_buffer = renoise.song().selected_sample.sample_buffer  
  
-- check if sample data is preset at all first  
if (sample_buffer.has_sample_data) then  
  
 -- before touching any sample, let renoise create undo data, when necessary  
 sample_buffer:prepare_sample_data_changes()  
  
 -- modify sample data in the selection (defaults to the whole sample)  
 for channel = 1, sample_buffer.number_of_channels do  
 for frame = sample_buffer.selection_start, sample_buffer.selection_end do  
 local value = sample_buffer:sample_data(channel, frame)  
 value = -value -- do something with the value  
 sample_buffer:set_sample_data(channel, frame, value)  
 end  
 end  
  
 -- let renoise update sample overviews and caches. apply bit depth   
 -- quantization, finalize undo/redo data when needed...  
 sample_buffer:finalize_sample_data_changes()  
  
end  
  
  

oh, thanks, the need for a new buffer is indeed confusing and already caused other of my scripts (add-remove silence, for example) to be more complex than necessary.

on the same topic, I have tried to use my swapper script with undo disabled on sample editor; the results are quite unpredictable: the status messages say “was undone”, “was redone” and undo initially seems to work, which shouldn’t be, but redoing causes bad behaviours

Well, as soon as you are changing the length of the buffer, you have to recreate it. So this won’t help here.