Repository navigation
Expand file tree
/
Copy pathPakettiClearance.lua
More file actions
710 lines (612 loc) · 23.2 KB
/
Copy pathPakettiClearance.lua
File metadata and controls
710 lines (612 loc) · 23.2 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
function findUsedInstruments()
local song=renoise.song()
local used_instruments = {}
local used_samples = {}
-- Initialize tracking tables for each instrument and sample
for i=1, #song.instruments do
used_instruments[i] = false
used_samples[i] = {}
for s=1, #song.instruments[i].samples do
used_samples[i][s] = false
end
end
-- Scan through all patterns in the song (OPTIMIZED)
for _, pattern in ipairs(song.patterns) do
for track_idx, track in ipairs(pattern.tracks) do
-- Skip non-sequencer tracks (master, send tracks can't trigger instruments)
if song.tracks[track_idx].type == renoise.Track.TRACK_TYPE_SEQUENCER then
for _, line in ipairs(track.lines) do
-- Skip empty lines entirely
if not line.is_empty then
for _, note_column in ipairs(line.note_columns) do
-- Skip empty note columns
if not note_column.is_empty and note_column.instrument_value < 255 then
local instr_idx = note_column.instrument_value + 1
used_instruments[instr_idx] = true
if note_column.note_value < 120 then -- Valid note
local instrument = song.instruments[instr_idx]
if instrument then
for sample_idx, sample in ipairs(instrument.samples) do
if sample.sample_mapping then
local note_range = sample.sample_mapping.note_range
if note_column.note_value >= note_range[1] and
note_column.note_value <= note_range[2] then
used_samples[instr_idx][sample_idx] = true
end
end
end
end
end
end
end
end
end
end
end
end
return used_instruments, used_samples
end
function calculateTotalSize(unused_list)
local total_bytes = 0
for _, item in ipairs(unused_list) do
if item.sample_buffer and item.sample_buffer.has_sample_data then
local sample_size = item.sample_buffer.number_of_frames *
item.sample_buffer.number_of_channels *
(item.sample_buffer.bit_depth / 8)
total_bytes = total_bytes + sample_size
end
end
return total_bytes
end
function formatFileSize(bytes)
local units = {'B', 'KB', 'MB', 'GB'}
local unit_index = 1
local size = bytes
while size > 1024 and unit_index < #units do
size = size / 1024
unit_index = unit_index + 1
end
return string.format("%.2f %s", size, units[unit_index])
end
function saveUnusedSamples()
local song=renoise.song()
local used_instruments, used_samples = findUsedInstruments()
-- Build list of unused samples
local unused_list = {}
local unused_instruments = {}
for instr_idx, instrument in ipairs(song.instruments) do
local has_unused_samples = false
if not used_instruments[instr_idx] then
for sample_idx, sample in ipairs(instrument.samples) do
if sample.sample_buffer and sample.sample_buffer.has_sample_data then
table.insert(unused_list, {
instrument = instrument,
sample = sample,
instr_idx = instr_idx,
sample_idx = sample_idx
})
end
end
if #instrument.samples > 0 then
table.insert(unused_instruments, {
instrument = instrument,
instr_idx = instr_idx
})
end
else
for sample_idx, sample in ipairs(instrument.samples) do
if not used_samples[instr_idx][sample_idx] and
sample.sample_buffer and
sample.sample_buffer.has_sample_data then
table.insert(unused_list, {
instrument = instrument,
sample = sample,
instr_idx = instr_idx,
sample_idx = sample_idx
})
has_unused_samples = true
end
end
if has_unused_samples then
table.insert(unused_instruments, {
instrument = instrument,
instr_idx = instr_idx
})
end
end
end
if #unused_list == 0 then
renoise.app():show_status("No unused samples found in the song")
return
end
local dialog_title = string.format(
"Save %d unused samples - Select destination folder",
#unused_list
)
local folder_path = renoise.app():prompt_for_path(dialog_title)
if not folder_path or folder_path == "" then
renoise.app():show_status("Save operation cancelled")
return
end
-- Save samples
local saved_count = 0
local current_instrument_index = song.selected_instrument_index
local current_sample_index = song.selected_sample_index
for _, item in ipairs(unused_list) do
song.selected_instrument_index = item.instr_idx
local safe_instr_name = item.instrument.name:gsub("[^%w%s-]", "_")
local safe_sample_name = item.sample.name:gsub("[^%w%s-]", "_")
local filename = string.format(
"%s_-_%03d_-_%s.wav",
safe_instr_name,
item.sample_idx,
safe_sample_name
)
local full_path = folder_path .. "/" .. filename
song.selected_sample_index = item.sample_idx
if song.selected_sample.sample_buffer:save_as(full_path, "wav") then
saved_count = saved_count + 1
end
end
-- Save unused instruments as XRNI
local saved_instruments = 0
for _, item in ipairs(unused_instruments) do
song.selected_instrument_index = item.instr_idx
local safe_name = item.instrument.name:gsub("[^%w%s-]", "_")
local full_path = folder_path .. "/" .. safe_name .. ".xrni"
renoise.app():save_instrument(full_path)
saved_instruments = saved_instruments + 1
end
-- Restore original selection
song.selected_instrument_index = current_instrument_index
song.selected_sample_index = current_sample_index
renoise.app():show_status(string.format(
"Saved %d samples and %d instruments to %s",
saved_count,
saved_instruments,
folder_path
))
end
renoise.tool():add_keybinding{name="Global:Paketti:Save Unused Samples (.WAV&.XRNI)",invoke=saveUnusedSamples}
--------
function saveUnusedInstruments()
local song=renoise.song()
local used_instruments, _ = findUsedInstruments()
-- Build list of unused instruments
local unused_instruments = {}
for instr_idx, instrument in ipairs(song.instruments) do
if not used_instruments[instr_idx] and #instrument.samples > 0 then
table.insert(unused_instruments, {
instrument = instrument,
instr_idx = instr_idx
})
end
end
if #unused_instruments == 0 then
renoise.app():show_status("No unused instruments found in the song")
return
end
local dialog_title = string.format(
"Save %d unused instruments - Select destination folder",
#unused_instruments
)
local folder_path = renoise.app():prompt_for_path(dialog_title)
if not folder_path or folder_path == "" then
renoise.app():show_status("Save operation cancelled")
return
end
-- Save instruments
local saved_count = 0
local current_instrument_index = song.selected_instrument_index
for _, item in ipairs(unused_instruments) do
song.selected_instrument_index = item.instr_idx
local safe_name = item.instrument.name:gsub("[^%w%s-]", "_")
local filename = string.format(
"%03d_%s.xrni",
item.instr_idx,
safe_name
)
local full_path = folder_path .. "/" .. filename
renoise.app():save_instrument(full_path)
saved_count = saved_count + 1
end
-- Restore original selection
song.selected_instrument_index = current_instrument_index
renoise.app():show_status(string.format(
"Saved %d unused instruments to %s",
saved_count,
folder_path
))
end
renoise.tool():add_keybinding{name="Global:Paketti:Save Unused Instruments (.XRNI)",invoke=saveUnusedInstruments}
----
function deleteUnusedInstruments()
local song=renoise.song()
local used_instruments, _ = findUsedInstruments()
-- Build list of unused instruments
local unused_instruments = {}
for instr_idx, instrument in ipairs(song.instruments) do
if not used_instruments[instr_idx] and #instrument.samples > 0 then
table.insert(unused_instruments, {
instrument = instrument,
instr_idx = instr_idx
})
end
end
if #unused_instruments == 0 then
renoise.app():show_status("No unused instruments found in the song")
return
end
-- Ask for confirmation before deletion
local message = string.format(
"Are you sure you want to delete %d unused instruments?",
#unused_instruments
)
local ok = renoise.app():show_prompt("Delete Unused Instruments", message, {"Yes", "No"})
if ok ~= "Yes" then
renoise.app():show_status("Delete operation cancelled")
return
end
-- Delete instruments (starting from highest index to avoid reindexing issues)
table.sort(unused_instruments, function(a, b) return a.instr_idx > b.instr_idx end)
local deleted_count = 0
for _, item in ipairs(unused_instruments) do
song:delete_instrument_at(item.instr_idx)
deleted_count = deleted_count + 1
end
renoise.app():show_status(string.format(
"Deleted %d unused instruments",
deleted_count
))
end
renoise.tool():add_keybinding{name="Global:Paketti:Delete Unused Instruments",invoke=deleteUnusedInstruments}
---
function findUsedSamples()
print ("Finding used samples")
local song = renoise.song()
local used_samples = {}
local used_notes = {}
local used_velocities = {}
-- Initialize tracking tables
for instr_idx, instrument in ipairs(song.instruments) do
used_samples[instr_idx] = {}
used_notes[instr_idx] = {}
used_velocities[instr_idx] = {}
for sample_idx = 1, #instrument.samples do
used_samples[instr_idx][sample_idx] = false
end
end
-- First pass: Find notes/velocities from patterns (OPTIMIZED)
for _, pattern in ipairs(song.patterns) do
for track_idx, track in ipairs(pattern.tracks) do
-- Skip non-sequencer tracks (master, send tracks can't trigger instruments)
if song.tracks[track_idx].type == renoise.Track.TRACK_TYPE_SEQUENCER then
for _, line in ipairs(track.lines) do
-- Skip empty lines entirely
if not line.is_empty then
for _, note_col in ipairs(line.note_columns) do
-- Skip empty note columns
if not note_col.is_empty then
local instr_val = note_col.instrument_value
-- 255 means no instrument specified
if instr_val ~= 255 then
local instr_idx = instr_val + 1
if instr_idx > 0 and instr_idx <= #song.instruments then
local note_val = note_col.note_value
if note_val and note_val < 120 then
used_notes[instr_idx][note_val] = true
local vol_val = note_col.volume_value
if vol_val and vol_val ~= 255 then
used_velocities[instr_idx][vol_val] = true
end
end
end
end
end
end
end
end
end
end
end
-- Check phrases for each instrument (OPTIMIZED)
for instr_idx, instrument in ipairs(song.instruments) do
if instrument.phrases and #instrument.phrases > 0 then
for _, phrase in ipairs(instrument.phrases) do
for _, line in ipairs(phrase.lines) do
-- Skip empty lines entirely
if not line.is_empty then
for _, note_col in ipairs(line.note_columns) do
-- Skip empty note columns
if not note_col.is_empty then
local note_val = note_col.note_value
if note_val and note_val < 120 then
used_notes[instr_idx][note_val] = true
local vol_val = note_col.volume_value
if vol_val and vol_val ~= 255 then
used_velocities[instr_idx][vol_val] = true
end
end
end
end
end
end
end
end
end
-- Second pass: Check each instrument's type and handle accordingly (OPTIMIZED with early exits)
for instr_idx, instrument in ipairs(song.instruments) do
-- Skip instruments with no samples
if #instrument.samples == 0 then
-- Nothing to check, skip to next instrument
else
-- First check for phrases - if instrument has phrases, we need to be more careful
local has_phrases = instrument.phrases and #instrument.phrases > 0
-- Check for slice markers
local has_slices = instrument.samples[1].slice_markers and
#instrument.samples[1].slice_markers > 0
if has_slices then
-- Mark all samples as used in sliced instruments
for sample_idx = 1, #instrument.samples do
used_samples[instr_idx][sample_idx] = true
end
else
-- Check if this is a velocity-mapped instrument
local is_velocity_mapped = false
if instrument.sample_mappings and #instrument.sample_mappings > 0 then
for sample_idx = 1, #instrument.samples do
local mapping = instrument.sample_mappings[1][sample_idx]
if mapping and mapping.velocity_range and
(mapping.velocity_range[1] > 0 or mapping.velocity_range[2] < 127) then
is_velocity_mapped = true
break
end
end
end
-- Handle based on instrument type
if is_velocity_mapped then
-- For velocity-mapped instruments, check both note and velocity ranges
for sample_idx = 1, #instrument.samples do
-- EARLY EXIT: Skip if sample already marked as used
if not used_samples[instr_idx][sample_idx] then
local mapping = instrument.sample_mappings[1][sample_idx]
if mapping and mapping.note_range then
local sample_marked = false
for note in pairs(used_notes[instr_idx]) do
if sample_marked then break end
if note >= mapping.note_range[1] and note <= mapping.note_range[2] then
-- For velocity-mapped samples, check velocity range
if has_phrases then
-- If instrument has phrases, be more conservative with velocity-mapped samples
used_samples[instr_idx][sample_idx] = true
sample_marked = true
elseif mapping.velocity_range then
for vel in pairs(used_velocities[instr_idx]) do
if vel >= mapping.velocity_range[1] and vel <= mapping.velocity_range[2] then
used_samples[instr_idx][sample_idx] = true
sample_marked = true
break
end
end
end
end
end
end
end
end
else
-- For regular instruments, only check note ranges
for sample_idx = 1, #instrument.samples do
-- EARLY EXIT: Skip if sample already marked as used
if not used_samples[instr_idx][sample_idx] then
local mapping = instrument.sample_mappings[1][sample_idx]
if mapping and mapping.note_range then
for note in pairs(used_notes[instr_idx]) do
if note >= mapping.note_range[1] and note <= mapping.note_range[2] then
used_samples[instr_idx][sample_idx] = true
break
end
end
end
end
end
end
end
end
end
return used_samples
end
function deleteUnusedSamples(skip_confirmation)
-- Temporarily disable AutoSamplify monitoring to prevent interference
local AutoSamplifyMonitoringState = PakettiTemporarilyDisableNewSampleMonitoring()
local song = renoise.song()
local used_samples = findUsedSamples()
local total_unused = 0
-- Count unused samples and build a list of what will be deleted
local samples_to_delete = {}
for instr_idx, instrument in ipairs(song.instruments) do
if #instrument.samples > 0 then
samples_to_delete[instr_idx] = {}
for sample_idx = 1, #instrument.samples do
if not used_samples[instr_idx][sample_idx] then
total_unused = total_unused + 1
table.insert(samples_to_delete[instr_idx], sample_idx)
end
end
end
end
if total_unused == 0 then
renoise.app():show_status("No unused samples found in the song")
return
end
-- If confirmation is needed, show the dialog
if not skip_confirmation then
local message = string.format(
"Found %d samples that appear to be unused.\n" ..
"This will only remove samples that are unmapped and in instruments that are never triggered.\n" ..
"Are you sure you want to continue?",
total_unused
)
local ok = renoise.app():show_prompt("Delete Unused Samples", message, {"Yes", "No"})
if ok ~= "Yes" then
renoise.app():show_status("Delete operation cancelled")
return
end
end
-- Clear unused sample data (keep slots to preserve mappings)
local deleted_count = 0
for instr_idx = 1, #song.instruments do
local instrument = song.instruments[instr_idx]
for sample_idx = 1, #instrument.samples do
if not used_samples[instr_idx][sample_idx] then
local sample = instrument.samples[sample_idx]
if sample.sample_buffer.has_sample_data then
-- Clear sample data but keep the slot to preserve mappings
sample.sample_buffer:delete_sample_data()
-- Create minimal 1-frame placeholder
sample.sample_buffer:create_sample_data(44100, 16, 1, 1)
-- Mark as cleared
sample.name = "EmptiedUnused"
deleted_count = deleted_count + 1
end
end
end
end
renoise.app():show_status(string.format(
"Deleted %d unused samples",
deleted_count
))
print("Like I deleted stuff or something")
-- Restore AutoSamplify monitoring state
PakettiRestoreNewSampleMonitoring(AutoSamplifyMonitoringState)
end
renoise.tool():add_keybinding{name="Global:Paketti:Delete Unused Samples",invoke=deleteUnusedSamples}
renoise.tool():add_keybinding{name="Sample Keyzones:Paketti:Delete Unused Samples",invoke=deleteUnusedSamples}
--------
function PakettiClearanceRemoveEmptyTracks()
local song = renoise.song()
-- Check if we have tracks to work with
if #song.tracks <= 1 then
renoise.app():show_status("Cannot remove tracks - song must have at least one track")
return
end
local tracks_to_delete = {}
-- Check each track for emptiness across all patterns
for track_index = 1, #song.tracks do
local track = song.tracks[track_index]
-- Skip master track (cannot be deleted)
if track.type ~= renoise.Track.TRACK_TYPE_MASTER then
local is_empty_across_all_patterns = true
-- Check if track is empty across all patterns
for pattern_index = 1, #song.patterns do
local pattern_track = song.patterns[pattern_index].tracks[track_index]
if not pattern_track.is_empty then
is_empty_across_all_patterns = false
break
end
end
if is_empty_across_all_patterns then
table.insert(tracks_to_delete, track_index)
end
end
end
if #tracks_to_delete == 0 then
renoise.app():show_status("No empty tracks found")
return
end
-- Sort in descending order to avoid reindexing issues
table.sort(tracks_to_delete, function(a, b) return a > b end)
-- Check if we would delete all regular tracks
local remaining_regular_tracks = 0
for track_index = 1, #song.tracks do
local track = song.tracks[track_index]
if track.type == renoise.Track.TRACK_TYPE_SEQUENCER then
local will_be_deleted = false
for _, delete_index in ipairs(tracks_to_delete) do
if delete_index == track_index then
will_be_deleted = true
break
end
end
if not will_be_deleted then
remaining_regular_tracks = remaining_regular_tracks + 1
end
end
end
if remaining_regular_tracks == 0 then
renoise.app():show_status("Cannot remove all regular tracks - song must have at least one regular track")
return
end
local deleted_count = 0
for _, track_index in ipairs(tracks_to_delete) do
song:delete_track_at(track_index)
deleted_count = deleted_count + 1
end
renoise.app():show_status(string.format("Removed %d empty tracks", deleted_count))
end
function PakettiClearanceRemoveEmptyTracksAndDSP()
local song = renoise.song()
-- Check if we have tracks to work with
if #song.tracks <= 1 then
renoise.app():show_status("Cannot remove tracks - song must have at least one track")
return
end
local tracks_to_delete = {}
-- Check each track for emptiness across all patterns AND no DSP devices
for track_index = 1, #song.tracks do
local track = song.tracks[track_index]
-- Skip master track (cannot be deleted)
if track.type ~= renoise.Track.TRACK_TYPE_MASTER then
local is_empty_across_all_patterns = true
-- Check if track is empty across all patterns
for pattern_index = 1, #song.patterns do
local pattern_track = song.patterns[pattern_index].tracks[track_index]
if not pattern_track.is_empty then
is_empty_across_all_patterns = false
break
end
end
-- Check if track has only the mixer device (device count <= 1 means only mixer)
local has_no_dsp = #track.devices <= 1
if is_empty_across_all_patterns and has_no_dsp then
table.insert(tracks_to_delete, track_index)
end
end
end
if #tracks_to_delete == 0 then
renoise.app():show_status("No empty tracks (with no DSP) found")
return
end
-- Sort in descending order to avoid reindexing issues
table.sort(tracks_to_delete, function(a, b) return a > b end)
-- Check if we would delete all regular tracks
local remaining_regular_tracks = 0
for track_index = 1, #song.tracks do
local track = song.tracks[track_index]
if track.type == renoise.Track.TRACK_TYPE_SEQUENCER then
local will_be_deleted = false
for _, delete_index in ipairs(tracks_to_delete) do
if delete_index == track_index then
will_be_deleted = true
break
end
end
if not will_be_deleted then
remaining_regular_tracks = remaining_regular_tracks + 1
end
end
end
if remaining_regular_tracks == 0 then
renoise.app():show_status("Cannot remove all regular tracks - song must have at least one regular track")
return
end
local deleted_count = 0
for _, track_index in ipairs(tracks_to_delete) do
song:delete_track_at(track_index)
deleted_count = deleted_count + 1
end
renoise.app():show_status(string.format("Removed %d empty tracks (with no DSP)", deleted_count))
end
renoise.tool():add_keybinding{name="Global:Paketti:Remove Empty Tracks", invoke=PakettiClearanceRemoveEmptyTracks}
renoise.tool():add_keybinding{name="Global:Paketti:Remove Empty Tracks (No Notes, No DSP)", invoke=PakettiClearanceRemoveEmptyTracksAndDSP}
--------