-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrimmer.lua
131 lines (103 loc) · 3.25 KB
/
Trimmer.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
--[[
Trim content of sample
@param instr: renoise.Instrument
@param s_idx: (int) sample index
@param mode: (string)
"loop" - match the loop start/end marker
"selection" - match the selection
"custom" - provide custom start/end points
]]
function trim_sample(instr,s_idx,mode)
local s = instr.samples[s_idx]
if not s then
print("Could not locate sample")
return
end
local sbuf = s.sample_buffer
if not sbuf.has_sample_data then
print(instr.name,s.name,"Skipped empty sample")
return
end
if sbuf.read_only then
print(instr.name,s.name,"Sample is read-only (sliced?)")
return
end
local trim_start, trim_end
if (mode == "loop") then
if (s.loop_mode == renoise.Sample.LOOP_MODE_OFF) then
print(instr.name,s.name,"No loop defined in sample")
return
end
trim_start = s.loop_start-1
trim_end = s.loop_end-1
elseif (mode == "selection") then
if (s.loop_mode == renoise.Sample.LOOP_MODE_OFF) then
print(instr.name,s.name,"No selection defined in sample")
return
end
trim_start = sbuf.selection_start-1
trim_end = sbuf.selection_end-1
else
print("Unsupported 'mode'")
return
end
--print("trimming instrument:sample ",instr.name,s.name)
-- create a new sample (duplicate)
--print("creating temporary sample at index",s_idx)
local s_new = instr:insert_sample_at(s_idx)
local sbuf_new = s_new.sample_buffer
s_new:copy_from(s)
--sbuf_new:prepare_sample_data_changes()
local s_rate = sbuf.sample_rate
local b_depth = sbuf.bit_depth
local num_channels = sbuf.number_of_channels
local num_frames = 1 + trim_end - trim_start
sbuf_new:create_sample_data(s_rate,b_depth,num_channels,num_frames)
for chan_idx = 1, sbuf.number_of_channels do
for frame_idx = 1,num_frames do
sbuf_new:set_sample_data(chan_idx,frame_idx,
sbuf:sample_data(chan_idx,frame_idx + trim_start))
end
end
--sbuf_new:finalize_sample_data_changes()
-- if loop mode, update loop markers
if (mode == "loop") then
s_new.loop_start = 1
s_new.loop_end = num_frames
end
-- remove original sample
--print("deleting temporary sample at index",s_idx+1)
instr:delete_sample_at(s_idx+1)
end
local trim_samples_in_instr = function(instr,mode)
if instr then
for s_idx,s in ipairs(instr.samples) do
trim_sample(instr,s_idx,mode)
end
end
end
------------------------------------------------------
-- RUN THE SCRIPT
------------------------------------------------------
-- set the scope
-- "selection": run on selected instrument/sample
-- "instrument": all samples in selected instrument
-- "range": all instruments/samples in range
local scope = "instrument"
local range_start = 0x61
local range_end = 0xff
-- set the mode (loop,selection)
local trim_mode = "loop"
if (scope == "selected") then
local instr = renoise.song().selected_instrument
local s_idx = renoise.song().selected_sample_index
trim_sample(instr,s_idx,trim_mode)
elseif (scope == "instrument") then
local instr = renoise.song().selected_instrument
trim_samples_in_instr(instr,trim_mode)
elseif (scope == "range") then
for idx = range_start,range_end do
instr = renoise.song().instruments[idx]
trim_samples_in_instr(instr,trim_mode)
end
end