This is a function that I created to reverse the sample:
--reverse sample
local function reverse_sample()
--renoise.song().instruments[].samples[].sample_buffer:sample_data (channel_index, frame_index)
--renoise.song().instruments[].samples[].sample_buffer:set_sample_data (channel_index, frame_index, sample_value)
local x=os.clock()
local sam=renoise.song().selected_sample
if (sam) then
local buf=sam.sample_buffer
if (buf.has_sample_data) then
--define empty tables
local TBL_L_SAM_VAL,REV_TBL_L_SAM_VAL={},{}
local TBL_R_SAM_VAL,REV_TBL_R_SAM_VAL={},{}
--load sample_value(s)
for f=1,buf.number_of_frames do
TBL_L_SAM_VAL[f]=buf:sample_data(1,f)
TBL_R_SAM_VAL[f]=buf:sample_data(2,f)
end
--reverse tables
for _,v in ripairs(TBL_L_SAM_VAL) do
REV_TBL_L_SAM_VAL[#REV_TBL_L_SAM_VAL+1]=v
end
for _,v in ripairs(TBL_R_SAM_VAL) do
REV_TBL_R_SAM_VAL[#REV_TBL_R_SAM_VAL+1]=v
end
--[[
print("================")
print(TBL_L_SAM_VAL[1])
print(TBL_L_SAM_VAL[2])
print("----------------")
print(REV_TBL_L_SAM_VAL[1])
print(REV_TBL_L_SAM_VAL[2])
]]
--reverse wave
buf:prepare_sample_data_changes()
for f=1,buf.number_of_frames do
buf:set_sample_data(1,f,REV_TBL_L_SAM_VAL[f])
buf:set_sample_data(2,f,REV_TBL_R_SAM_VAL[f])
end
buf:finalize_sample_data_changes()
end
end
print(string.format("reverse_sample: %.2f ms\n",(os.clock()- x)*1000))
end
reverse_sample()
A sample with the following characteristics:
- 16.065 frames
- 0.364 s
- 44100hz, 16 bit, stereo
It takes about 17 ms to reverse, with an i7 processor (6700K). ~ 20 ms is too slow to use in real time. Automate it should require some foresight. Probably, this function could be further optimized. I have needed to iterate 4 times.
Any ideas to speed up this function?
Edit: Ah, I think it is possible to save the tables already inverted. This is much more direct:
--reverse sample
local function reverse_sample()
local x=os.clock()
local sam=renoise.song().selected_sample
if (sam) then
local buf=sam.sample_buffer
if (buf.has_sample_data) then
--define empty tables
local TBL_L_SAM_VAL={}
local TBL_R_SAM_VAL={}
--load sample_value(s)
local i=1
for f=buf.number_of_frames,1,-1 do
TBL_L_SAM_VAL[i]=buf:sample_data(1,f)
TBL_R_SAM_VAL[i]=buf:sample_data(2,f)
i=i+1
end
--reverse wave
buf:prepare_sample_data_changes()
for f=1,buf.number_of_frames do
buf:set_sample_data(1,f,TBL_L_SAM_VAL[f])
buf:set_sample_data(2,f,TBL_R_SAM_VAL[f])
end
buf:finalize_sample_data_changes()
end
end
print(string.format("reverse_sample: %.2f ms\n",(os.clock()- x)*1000))
end
reverse_sample()
With only 2 iterations. It is still very slow. It takes about 13 ms. This function, with this case, is writing and reading up to 16000 values in two tables.