-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
292 lines (239 loc) · 10.8 KB
/
Copy pathmain.lua
File metadata and controls
292 lines (239 loc) · 10.8 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
-- Removes empty slots that has no sample data, loaded plugin or midi output
function instrument_is_empty(instrument)
local inst, has_sample_data = renoise.song():instrument(instrument), false
for _, sample in ipairs(inst.samples) do
has_sample_data = has_sample_data or sample.sample_buffer.has_sample_data
end
if inst.name ~= "" or inst.plugin_properties.plugin_loaded or inst.midi_output_properties.device_name ~= "" or has_sample_data then return false else return true end
end
function remove_empty_slots()
for instrument in ripairs(renoise.song().instruments) do
if instrument_is_empty(instrument) and #renoise.song().instruments > 1 then
renoise.song().delete_instrument_at(renoise.song(),instrument)
end
end
end
-- Sort instruments according to a remap table
function sort_instruments(new_mapping, status_text)
renoise.app():show_status("Moving instruments...")
table.sort(new_mapping, function(a, b) return a.original_position < b.original_position end)
local doing_pos = 1
while doing_pos < table.count(new_mapping) do
while new_mapping[doing_pos].new_position == doing_pos and doing_pos < table.count(new_mapping) do
doing_pos = doing_pos + 1
end
renoise.song().swap_instruments_at(renoise.song(), doing_pos, new_mapping[doing_pos].new_position)
new_mapping[doing_pos], new_mapping[new_mapping[doing_pos].new_position] = new_mapping[new_mapping[doing_pos].new_position], new_mapping[doing_pos]
end
renoise.app():show_status("Sorted the instrument list by " .. status_text .. ".")
end
-- Returns a remap table according to alphabetically sorted instrument names
function map_alphabetic()
local map_table = { }
for i, instrument in ipairs(renoise.song().instruments) do table.insert(map_table, { name = instrument.name, original_position = i }) end
table.sort(map_table, function(a, b)
if a.name == b.name then return a.original_position < b.original_position
else return string.upper(a.name) < string.upper(b.name) end
end)
for instrument = 1, #map_table do
map_table[instrument].new_position = instrument
end
return map_table
end
-- Returns a remap table according to reversed order of instruments
function map_reverse()
local map_table = { }
for _, instrument in ipairs(renoise.song().instruments) do
table.insert(map_table, 1, { name = instrument.name, original_position = _ })
end
for instrument = 1, #map_table do
map_table[instrument].new_position = instrument
end
return map_table
end
-- Returns a remap table according to the total sample size in an instrument
function size_of_instrument(instrument)
local bitsize = 0
for _, sample in ipairs(renoise.song():instrument(instrument).samples) do
if sample.sample_buffer.has_sample_data then
bitsize = bitsize + sample.sample_buffer.bit_depth * sample.sample_buffer.number_of_frames * sample.sample_buffer.number_of_channels
end
end
return bitsize
end
function map_size()
local map_table = { }
for _, instrument in ipairs(renoise.song().instruments) do
table.insert(map_table, { name = instrument.name, original_position = _, size = size_of_instrument(_) })
end
table.sort(map_table, function(b, a)
if a.size == b.size then return a.original_position < b.original_position
else return a.size > b.size
end
end)
for instrument = 1, #map_table do
map_table[instrument].new_position = instrument
end
return map_table
end
-- Returns a remap table according to the order of an instruments appearance in the song (scanned vertically)
function map_order_vertical()
local first_hit, note_cell, map_table, scanned_patterns = { }, 0, { }, { }
for _, instrument in ipairs(renoise.song().instruments) do table.insert(map_table, { name = instrument.name, original_position = _ }) end
renoise.app():show_status("Scanning song...")
for sequence, pattern_index in ipairs(renoise.song().sequencer.pattern_sequence) do
if scanned_patterns[pattern_index] then else
scanned_patterns[pattern_index] = true
local pattern = renoise.song():pattern(pattern_index)
for line = 1, pattern.number_of_lines do
for track = 1, renoise.song().sequencer_track_count do
if not pattern:track(track).is_empty then
if not pattern:track(track):line(line).is_empty then
for note_column = 1, renoise.song():track(track).visible_note_columns do
note_cell = note_cell + 1
local n_column = pattern:track(track):line(line):note_column(note_column)
if not n_column.is_empty then
if map_table[n_column.instrument_value+1] then
if map_table[n_column.instrument_value+1].note_cell then
map_table[n_column.instrument_value+1].note_cell = math.min(note_cell, map_table[n_column.instrument_value+1].note_cell)
else
map_table[n_column.instrument_value+1].note_cell = note_cell
end
end
end
end
end
end
end
end
end
end
for pos in ipairs(map_table) do
if not map_table[pos].note_cell then map_table[pos].note_cell = math.huge end
end
table.sort(map_table, function(a, b)
if a.note_cell == b.note_cell then return a.original_position < b.original_position
else return a.note_cell < b.note_cell end
end)
for instrument = 1, #map_table do
map_table[instrument].new_position = instrument
end
return map_table
end
-- Returns a remap table according to the order of an instruments appearance in the song (scanned horizontally)
function map_order_horizontal()
local first_hit, hit_order, map_table, scanned_patterns = { }, 0, { }, { }
for _, instrument in ipairs(renoise.song().instruments) do table.insert(map_table, { name = instrument.name, original_position = _, first_hit = math.huge }) end
renoise.app():show_status("Scanning song...")
for track_index = 1, renoise.song().sequencer_track_count do
for seq, pattern_index in ipairs(renoise.song().sequencer.pattern_sequence) do
if scanned_patterns[pattern_index] then else
scanned_patterns[pattern_index] = true
local track = renoise.song():pattern(pattern_index):track(track_index)
if not track.is_empty and renoise.song():track(track_index).type == renoise.Track.TRACK_TYPE_SEQUENCER then
for line_index, line in ipairs(track.lines) do
if not line.is_empty then
for note_column_index = 1, renoise.song():track(track_index).visible_note_columns do
local val = line:note_column(note_column_index)
if not val.is_empty then
if val.instrument_value ~= 255 then
if map_table[val.instrument_value+1] then
if map_table[val.instrument_value+1].first_hit > hit_order then
hit_order = hit_order + 1
map_table[val.instrument_value+1].first_hit = hit_order
end
end
end
end
end
end
end
end
end
end
end
table.sort(map_table, function(a, b)
if a.first_hit == b.first_hit then return a.original_position < b.original_position
else return a.first_hit < b.first_hit end
end)
for instrument = 1, #map_table do
map_table[instrument].new_position = instrument
end
return map_table
end
-- Returns a remap table according to a randomized order of instruments
function map_randomize()
local map_table = { }
for _, instrument in ipairs(renoise.song().instruments) do table.insert(map_table, { name = instrument.name, original_position = _, random = math.random() }) end
table.sort(map_table, function(a, b) return a.random < b.random end)
for instrument = 1, #map_table do
map_table[instrument].new_position = instrument
end
return map_table
end
-- Returns a remap table according to the number of times instruments are used in the song
function map_most_used()
local map_table = { }
for _, instrument in ipairs(renoise.song().instruments) do table.insert(map_table, { name = instrument.name, original_position = _, population = 0 }) end
renoise.app():show_status("Scanning song...")
for seq, pattern_index in ipairs(renoise.song().sequencer.pattern_sequence) do
for track_index = 1, renoise.song().sequencer_track_count do
local track = renoise.song():pattern(pattern_index):track(track_index)
if not track.is_empty and renoise.song():track(track_index).type == renoise.Track.TRACK_TYPE_SEQUENCER then
for line_index, line in ipairs(track.lines) do
if not line.is_empty then
for note_column_index = 1, renoise.song():track(track_index).visible_note_columns do
local val = line:note_column(note_column_index)
if not val.is_empty then
if val.instrument_value ~= 255 and map_table[val.instrument_value+1] then
map_table[val.instrument_value + 1].population = map_table[val.instrument_value + 1].population + 1
end
end
end
end
end
end
end
end
table.sort(map_table, function(a, b)
if a.population == b.population then return a.original_position < b.original_position
else return a.population > b.population end
end)
for instrument = 1, #map_table do
map_table[instrument].new_position = instrument
end
return map_table
end
-- Menu entries
renoise.tool():add_menu_entry {
name = "Instrument Box:Sort Instruments...:Name",
invoke = function() sort_instruments(map_alphabetic(), "name") end
}
renoise.tool():add_menu_entry {
name = "Instrument Box:Sort Instruments...:Size",
invoke = function() sort_instruments(map_size(), "total sample size") end
}
renoise.tool():add_menu_entry {
name = "Instrument Box:Sort Instruments...:Most Used",
invoke = function() sort_instruments(map_most_used(), "usage in song") end
}
renoise.tool():add_menu_entry {
name = "Instrument Box:Sort Instruments...:Order In Song (Horizontal)",
invoke = function() sort_instruments(map_order_horizontal(), "order in song (horizontal)") end
}
renoise.tool():add_menu_entry {
name = "Instrument Box:Sort Instruments...:Order In Song (Vertical)",
invoke = function() sort_instruments(map_order_vertical(), "order in song (vertical)") end
}
renoise.tool():add_menu_entry {
name = "Instrument Box:Sort Instruments...:Randomize",
invoke = function() sort_instruments(map_randomize(), "random order") end
}
renoise.tool():add_menu_entry {
name = "Instrument Box:Sort Instruments...:Reverse",
invoke = function() sort_instruments(map_reverse(), "reversed order") end
}
renoise.tool():add_menu_entry {
name = "---Instrument Box:Sort Instruments...:Delete Empty Instruments",
invoke = function() remove_empty_slots() end
}