-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PakettiAudioProcessing.lua
1468 lines (1212 loc) · 52.2 KB
/
PakettiAudioProcessing.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
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- TODO: Phase Shift + Pitch Shift invert mix
local vb = renoise.ViewBuilder()
local buffer = nil
local current_name = nil
local current_rate = nil
local current_length = nil
local current_bit_depth = nil
local destination_rate = nil
local destination_bit_depth = nil
local pitch_shift_amount = 0
local dialog = nil
local content = nil
local slider_value=vb:text{text="1"} -- initial text showing the slider value
local sample_name_text=vb:text{id="sample_name_text",text="Name: No valid sample selected"}
local details_text=vb:text{id="details_text",text="Details: No valid sample selected"}
-- Utility function to copy sample settings
local function copy_sample_settings(from_sample, to_sample)
to_sample.volume = from_sample.volume
to_sample.panning = from_sample.panning
to_sample.transpose = from_sample.transpose
to_sample.fine_tune = from_sample.fine_tune
to_sample.beat_sync_enabled = from_sample.beat_sync_enabled
to_sample.beat_sync_lines = from_sample.beat_sync_lines
to_sample.beat_sync_mode = from_sample.beat_sync_mode
to_sample.oneshot = from_sample.oneshot
to_sample.loop_release = from_sample.loop_release
to_sample.loop_mode = from_sample.loop_mode
to_sample.mute_group = from_sample.mute_group
to_sample.new_note_action = from_sample.new_note_action
to_sample.autoseek = from_sample.autoseek
to_sample.autofade = from_sample.autofade
to_sample.oversample_enabled = from_sample.oversample_enabled
to_sample.interpolation_mode = from_sample.interpolation_mode
to_sample.name = from_sample.name
end
-- Function to limit the sample to avoid clipping
local function limit_sample(buffer)
local max_value = 0
for c = 1, buffer.number_of_channels do
for f = 1, buffer.number_of_frames do
local value = math.abs(buffer:sample_data(c, f))
if value > max_value then max_value = value end
end
end
if max_value > 0 then
local normalization_factor = 1 / max_value
buffer:prepare_sample_data_changes()
for c = 1, buffer.number_of_channels do
for f = 1, buffer.number_of_frames do
local normalized_value = buffer:sample_data(c, f) * normalization_factor
buffer:set_sample_data(c, f, normalized_value)
end
end
buffer:finalize_sample_data_changes()
end
end
-- Function to create an audio diff sample
local function create_audio_diff_sample()
local song = renoise.song()
local sample = song.selected_sample
local duplicate = duplicate_sample()
if not duplicate then return end
local buffer1 = sample.sample_buffer
local buffer2 = duplicate.sample_buffer
if not buffer1.has_sample_data or not buffer2.has_sample_data then
renoise.app():show_status("The Sample Buffer has no data.")
return
end
buffer1:prepare_sample_data_changes()
for c = 1, buffer1.number_of_channels do
for f = 1, buffer1.number_of_frames do
local diff_value = buffer1:sample_data(c, f) - buffer2:sample_data(c, f)
buffer1:set_sample_data(c, f, diff_value)
end
end
buffer1:finalize_sample_data_changes()
-- Copy sample settings and name
copy_sample_settings(sample, duplicate)
-- Limit the output to avoid clipping
limit_sample(buffer1)
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_INSTRUMENT_SAMPLE_EDITOR
renoise.app():show_status("Audio Diff applied.")
end
-- Function to duplicate a sample
local function duplicate_sample()
local song = renoise.song()
local instrument = song.selected_instrument
if not instrument then renoise.app():show_status("There is no Instrument selected.") return nil end
local sample = song.selected_sample
if not sample then renoise.app():show_status("There is no Sample selected.") return nil end
-- Create a duplicate of the selected sample
local duplicate = instrument:insert_sample_at(#instrument.samples + 1)
duplicate:copy_from(sample)
return duplicate
end
-- Ensure focus returns to the middle frame after each button press
local function set_middle_frame_focus()
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_INSTRUMENT_SAMPLE_EDITOR
end
-- Function to mix and process samples with various operations
local function mix_and_process_samples(operation, mod_function)
local song = renoise.song()
local sample = song.selected_sample
local duplicate = duplicate_sample()
if not duplicate then return end
local buffer1 = sample.sample_buffer
local buffer2 = duplicate.sample_buffer
if not buffer1.has_sample_data or not buffer2.has_sample_data then
renoise.app():show_status("The Sample Buffer has no data.")
return
end
buffer1:prepare_sample_data_changes()
for c = 1, buffer1.number_of_channels do
for f = 1, buffer1.number_of_frames do
local original_value = buffer1:sample_data(c, f)
local processed_value = buffer2:sample_data(c, f)
-- Apply operation with possible modulation
local new_value = 0
if operation == "diff" then
new_value = original_value - processed_value
elseif operation == "modulate" then
new_value = original_value * (mod_function and mod_function(processed_value) or processed_value)
elseif operation == "sum" then
new_value = original_value + processed_value
end
buffer1:set_sample_data(c, f, new_value)
end
end
buffer1:finalize_sample_data_changes()
-- Limit the output to avoid clipping
limit_sample(buffer1)
-- Copy sample settings and name
copy_sample_settings(sample, duplicate)
-- Mute the duplicate sample
duplicate.volume = 0.0
-- Return to the original sample
song.selected_sample_index = 1
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_INSTRUMENT_SAMPLE_EDITOR
renoise.app():show_status("Sample " .. operation .. " applied and mixed.")
end
-- Function to perform phase inversion (subtraction) on a sample
local function phase_invert_sample()
local song = renoise.song()
local sample = song.selected_sample
local duplicate = duplicate_sample()
if not duplicate then return end
local buffer1 = sample.sample_buffer
local buffer2 = duplicate.sample_buffer
if not buffer1.has_sample_data or not buffer2.has_sample_data then
renoise.app():show_status("The Sample Buffer has no data.")
return
end
buffer1:prepare_sample_data_changes()
for c = 1, buffer1.number_of_channels do
for f = 1, buffer1.number_of_frames do
local original_value = buffer1:sample_data(c, f)
local duplicate_value = buffer2:sample_data(c, f)
buffer1:set_sample_data(c, f, original_value - duplicate_value) -- Direct subtraction
end
end
buffer1:finalize_sample_data_changes()
-- Copy sample settings and name
copy_sample_settings(sample, duplicate)
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_INSTRUMENT_SAMPLE_EDITOR
renoise.app():show_status("Phase Inversion (Subtraction) applied.")
end
-- Function to handle Phase Inversion and Audio Diff
local function phase_invert_and_diff_sample()
phase_invert_sample()
create_audio_diff_sample()
end
-- Function to perform inversion of right channel and summing to mono
local function invert_right_sum_mono()
local song = renoise.song()
local sample = song.selected_sample
local buffer = sample.sample_buffer
if buffer.number_of_channels ~= 2 then
renoise.app():show_status("The sample needs to be stereo")
return
end
local sample_rate = buffer.sample_rate
local bit_depth = buffer.bit_depth
local num_frames = buffer.number_of_frames
-- Create a new mono sample
local instrument = song.selected_instrument
local mono_sample = instrument:insert_sample_at(#instrument.samples + 1)
mono_sample.sample_buffer:create_sample_data(sample_rate, bit_depth, 1, num_frames)
local mono_buffer = mono_sample.sample_buffer
mono_buffer:prepare_sample_data_changes()
for f = 1, num_frames do
local left = buffer:sample_data(1, f)
local right = buffer:sample_data(2, f) * -1 -- Invert Right Channel
local sum = left + right
mono_buffer:set_sample_data(1, f, sum)
end
mono_buffer:finalize_sample_data_changes()
-- Copy sample settings and name
copy_sample_settings(sample, mono_sample)
mono_sample.name = sample.name .. " (InvertRight&Mono)"
-- Delete the original stereo sample
local original_index = song.selected_sample_index
instrument:delete_sample_at(original_index)
-- Set the new mono sample as the selected sample
song.selected_sample_index = #instrument.samples
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_INSTRUMENT_SAMPLE_EDITOR
renoise.app():show_status("Invert Right, Sum Mono applied.")
end
-- Function to perform pitch shifting and subtraction
local function pitch_shift_sample(shift_amount)
if shift_amount == 0 then
renoise.app():show_status("Set pitch valuebox to something other than 0, otherwise nothing happens.")
return
end
local song = renoise.song()
local duplicate = duplicate_sample()
if not duplicate then return end
local buffer = duplicate.sample_buffer
if not buffer.has_sample_data then renoise.app():show_status("The Sample Buffer has no data.") return end
buffer:prepare_sample_data_changes()
local num_frames = buffer.number_of_frames
local new_buffer = {}
for c = 1, buffer.number_of_channels do
new_buffer[c] = {}
for f = 1, num_frames do
local pos = f + shift_amount
if pos < 1 or pos > num_frames then new_buffer[c][f] = 0 else new_buffer[c][f] = buffer:sample_data(c, math.floor(pos)) end
end
end
for c = 1, buffer.number_of_channels do
for f = 1, num_frames do
buffer:set_sample_data(c, f, new_buffer[c][f])
end
end
buffer:finalize_sample_data_changes()
-- Copy sample settings and name
copy_sample_settings(song.selected_sample, duplicate)
mix_and_process_samples("diff")
end
-- Function to handle Pitch Shift and Audio Diff
local function pitch_shift_and_diff_sample()
if pitch_shift_amount == 0 then
renoise.app():show_status("Set pitch valuebox to something other than 0, otherwise nothing happens.")
return
end
pitch_shift_sample(pitch_shift_amount)
create_audio_diff_sample()
end
-- Function to perform modulation
local function modulate_samples()
mix_and_process_samples("modulate")
end
-- Function to handle Modulation and Audio Diff
local function modulate_and_diff_sample()
modulate_samples()
create_audio_diff_sample()
end
-- Function to create an audio diff sample
local function create_audio_diff_sample()
local song = renoise.song()
local sample = song.selected_sample
local duplicate = duplicate_sample()
if not duplicate then return end
local buffer1 = sample.sample_buffer
local buffer2 = duplicate.sample_buffer
if not buffer1.has_sample_data or not buffer2.has_sample_data then
renoise.app():show_status("The Sample Buffer has no data.")
return
end
buffer1:prepare_sample_data_changes()
for c = 1, buffer1.number_of_channels do
for f = 1, buffer1.number_of_frames do
local diff_value = buffer1:sample_data(c, f) - buffer2:sample_data(c, f)
buffer1:set_sample_data(c, f, diff_value)
end
end
buffer1:finalize_sample_data_changes()
-- Copy sample settings and name
copy_sample_settings(sample, duplicate)
-- Limit the output to avoid clipping
limit_sample(buffer1)
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_INSTRUMENT_SAMPLE_EDITOR
renoise.app():show_status("Audio Diff applied.")
end
-- Function to render the sample at a new sample rate without changing its sound
local function RenderSampleAtNewRate(target_sample_rate, target_bit_depth)
local song = renoise.song()
local instrument = song.selected_instrument
local sample_index = song.selected_sample_index
local sample = instrument:sample(sample_index)
local buffer = sample.sample_buffer
if buffer.has_sample_data then
local original_sample_rate = buffer.sample_rate
local original_frame_count = buffer.number_of_frames
local ratio = target_sample_rate / original_sample_rate
local new_frame_count = math.floor(original_frame_count * ratio)
-- Create a new sample with the target rate and bit depth
local new_sample = instrument:insert_sample_at(sample_index + 1)
copy_sample_settings(sample, new_sample)
new_sample.sample_buffer:create_sample_data(target_sample_rate, target_bit_depth, buffer.number_of_channels, new_frame_count)
local new_sample_buffer = new_sample.sample_buffer
new_sample_buffer:prepare_sample_data_changes()
-- Render the original sample into the new sample buffer, adjusting frame count
for c=1, buffer.number_of_channels do
for i=1, new_frame_count do
local original_index = math.floor(i / ratio)
original_index = math.max(1, math.min(original_frame_count, original_index))
new_sample_buffer:set_sample_data(c, i, buffer:sample_data(c, original_index))
end
end
new_sample_buffer:finalize_sample_data_changes()
-- Delete the original sample and select the new one
instrument:delete_sample_at(sample_index)
song.selected_sample_index = #instrument.samples -- Select the new sample
renoise.app():show_status("Sample resampled to " .. target_sample_rate .. " Hz and " .. target_bit_depth .. " bit.")
else
renoise.app():show_status("Sample buffer is either not loaded or is not at the correct sample rate.")
end
end
-- Function to destructively resample the selected sample to a specified sample rate
local function DestructiveResample(target_sample_rate, target_bit_depth)
local song = renoise.song()
local instrument = song.selected_instrument
local sample_index = song.selected_sample_index
local sample = instrument:sample(sample_index)
local buffer = sample.sample_buffer
if buffer.has_sample_data then
local original_sample_rate = buffer.sample_rate
local original_frame_count = buffer.number_of_frames
local ratio = target_sample_rate / original_sample_rate
local new_frame_count = math.floor(original_frame_count * ratio)
-- Pre-calculate all required information before deleting the sample
local original_sample_data = {}
for c=1, buffer.number_of_channels do
original_sample_data[c] = {}
for i=1, new_frame_count do
local original_index = math.floor(i / ratio)
original_index = math.max(1, math.min(original_frame_count, original_index))
original_sample_data[c][i] = buffer:sample_data(c, original_index)
end
end
local new_sample = instrument:insert_sample_at(sample_index + 1)
copy_sample_settings(sample, new_sample)
-- Create the new sample buffer with the selected rate and bit depth
new_sample.sample_buffer:create_sample_data(target_sample_rate, target_bit_depth, buffer.number_of_channels, new_frame_count)
local new_sample_buffer = new_sample.sample_buffer
new_sample_buffer:prepare_sample_data_changes()
-- Apply the precalculated sample data to the new buffer
for c=1, buffer.number_of_channels do
for i=1, new_frame_count do
new_sample_buffer:set_sample_data(c, i, original_sample_data[c][i])
end
end
-- Finalize changes and delete the original sample
new_sample_buffer:finalize_sample_data_changes()
instrument:delete_sample_at(sample_index)
song.selected_sample_index = #instrument.samples -- Select the new sample
renoise.app():show_status("Sample processed with " .. target_sample_rate .. " Hz and " .. target_bit_depth .. " bit.")
else
renoise.app():show_status("Sample buffer is either not loaded or is not at the correct sample rate.")
end
end
-- Function to create combined dialog content with full resampling functionality
local function create_combined_dialog_content()
local vb = renoise.ViewBuilder() -- Create a fresh ViewBuilder instance every time the dialog is opened
local sample = renoise.song().selected_sample
if not sample or not sample.sample_buffer.has_sample_data then
renoise.app():show_status("Please select a sample with data.")
return
end
local buffer = sample.sample_buffer
local current_name = sample.name
local current_rate = buffer.sample_rate
local current_length = buffer.number_of_frames
local current_bit_depth = buffer.bit_depth
local destination_rate = current_rate
local destination_bit_depth = current_bit_depth
local threshold_label=vb:text{text=string.format("%.3f%%", preferences.PakettiStripSilenceThreshold.value*100),width=60}
local begthreshold_label=vb:text{text=string.format("%.3f%%", preferences.PakettiMoveSilenceThreshold.value*100),width=60}
-- Create the dialog content
local dialog_content = vb:column { width=375,
margin = 5,vb:column{style="group",margin=5,width=365,
vb:row {
vb:text {text = "Silence Threshold:"},
vb:slider {
min = 0,
max = 1,
value = preferences.PakettiStripSilenceThreshold.value,
width = 200,
notifier = function(value)
threshold_label.text = string.format("%.3f%%", value * 100)
preferences.PakettiStripSilenceThreshold.value = value
end
},
threshold_label
},
vb:button {
text = "Strip Silence using Threshold",
notifier = function()
if preferences.PakettiStripSilenceThreshold.value == 1 then
local choice = renoise.app():show_prompt("Warning", "Are you sure you want to remove this sample?", {"Yes", "Cancel"})
if choice == "Yes" then
local song = renoise.song()
local sample_index = song.selected_sample_index
local instrument = song.selected_instrument
if instrument and sample_index > 0 and instrument:sample(sample_index) then
instrument:delete_sample_at(sample_index)
renoise.app():show_status("Sample removed.")
end
end
else
PakettiStripSilence()
renoise.app():show_status("Removed Silence from beginning + end of sample at threshold " .. (preferences.PakettiStripSilenceThreshold.value * 100) .. "%")
end
renoise.app().window.active_middle_frame = renoise.app().window.active_middle_frame
end
},
vb:row {
vb:text {text = "Silence Threshold:"},
vb:slider {
min = 0,
max = 1,
value = preferences.PakettiMoveSilenceThreshold.value,
width = 200,
notifier = function(value)
begthreshold_label.text = string.format("%.3f%%", value * 100)
preferences.PakettiMoveSilenceThreshold.value = value
end
},
begthreshold_label
},
vb:button {
text = "Move Beginning Silence to End",
notifier = function()
PakettiMoveSilence()
renoise.app().window.active_middle_frame = renoise.app().window.active_middle_frame
end
},
vb:button{ text="15 Frame Fade In & Fade Out",
notifier=function()
apply_fade_in_out()
renoise.app().window.active_middle_frame = renoise.app().window.active_middle_frame
end
}},
vb:column{style="group",margin=5,width=365,
vb:row{vb:button{text="Recursive DC Offset", notifier= function() remove_dc_offset_recursive() end}},
vb:row{
vb:button{
text = "Run Recursive DC Offset x times",
notifier = function()
local count = tonumber(vb.views.slider_value.text)
for i = 1, count do
remove_dc_offset_recursive()
end
renoise.app():show_status("Ran DC Offset " .. count .. " times")
end
}
},
vb:row{
vb:slider{
min = 1,
max = 500,
value = 1,
width = 200,
notifier = function(value)
vb.views.slider_value.text = tostring(math.floor(value))
end
},
vb:text{
id = "slider_value",
text = "1",
width=40, -- initial value
}
},
vb:row{vb:button{text="Run Recursive DC Offset 1-50 times (randomized)", notifier= function() remove_dc_offset_recursive_1to50() end}},
vb:row{vb:button{text="Max Amp DC Offset Kick Generator", notifier= function() pakettiMaxAmplitudeDCOffsetKickCreator()
renoise.song().selected_sample.name="Max Amp DC Offset Kick"
renoise.song().selected_instrument.name="Max Amp DC Offset Kick"
update_sample_details(sample_name_text, details_text)
end}},
},
vb:column{style="group",margin=5,width=365,
vb:row{vb:button{text="Invert Left Channel", notifier=function() PakettiSampleInvertLeftChannel() end},
vb:button{text="Invert Right Channel", notifier=function() PakettiSampleInvertRightChannel()end},
vb:button{text="Invert Sample", notifier=function() PakettiSampleInvertEntireSample() end},
}},
vb:column{style="group",margin=5,width=365,
vb:row{vb:button{text="Mono->Stereo", notifier=function() convert_mono_to_stereo() end},
vb:button{text="Mono->Stereo (Blank L)", notifier=function() mono_to_blank(0,1) end},
vb:button{text="Mono->Stereo (Blank R)", notifier=function() mono_to_blank(1,0) end},
}},
vb:column{style="group",margin=5,width=365,
vb:row{vb:button{text="Normalize Sample",notifier=function() normalize_selected_sample() end},
}},
vb:column{style="group",margin=5,width=365,
-- Phase Inversion Buttons
vb:row{vb:button {text = "Phase Inversion", notifier = function() phase_invert_sample() set_middle_frame_focus() end},
vb:button {text = "Phase Inversion & Audio Diff", notifier = function() phase_invert_and_diff_sample() set_middle_frame_focus() end},},
vb:row {vb:button {text = "Invert Right, Sum Mono", notifier = function() invert_right_sum_mono() set_middle_frame_focus() end},},
-- Pitch Shift Buttons
vb:row {
vb:valuebox {min = -100, max = 100, value = pitch_shift_amount, notifier = function(value) pitch_shift_amount = value end},
vb:button {text = "Pitch Shift", notifier = function() pitch_shift_sample(pitch_shift_amount) set_middle_frame_focus() end},
vb:button {text = "Pitch Shift & Audio Diff", notifier = function() pitch_shift_and_diff_sample() set_middle_frame_focus() end},
},
vb:row {vb:button {text = "Clip Bottom of Waveform", notifier = function() modulate_samples() set_middle_frame_focus() end},
vb:button {text = "Modulate & Audio Diff", notifier = function() modulate_and_diff_sample() set_middle_frame_focus() end},},
-- Audio Diff Button
vb:row {vb:button {text = "Audio Diff", notifier = function() create_audio_diff_sample() set_middle_frame_focus() end},}},
-- Resampling Section
vb:column{style="group", margin=5,
vb:text{text="Resample", style="strong", font="bold"},
sample_name_text,
details_text,
vb:space{height=10},
vb:slider{
id="rate_slider",
min=225,
max=192000,
value=current_rate,
width=354,
notifier=function(value)
destination_rate=math.floor(value)
vb.views.rate_label.text="Destination Sample Rate: "..destination_rate.."Hz" end},
vb:text{id="rate_label",text="Destination Sample Rate: "..current_rate.."Hz"},
vb:row{width="100%",
vb:button{
text="Halve Sample Rate",
notifier=function()
destination_rate=math.max(225,math.floor(vb.views.rate_slider.value/2))
vb.views.rate_slider.value=destination_rate
end,},
vb:button{
text="Double Sample Rate",
notifier=function()
destination_rate=math.min(192000,math.floor(vb.views.rate_slider.value*2))
vb.views.rate_slider.value=destination_rate
end,},
vb:button{
text="Resample to 44.1 kHz",
notifier=function()
destination_rate=44100
vb.views.rate_slider.value=destination_rate
RenderSampleAtNewRate(destination_rate, vb.views.bitdepth_switch.value*8)
end}},
vb:row{vb:text{text="Bit Depth:"},
vb:switch{
width=300,
id="bitdepth_switch",
items={"8bit","16bit","24bit","32bit"},
value=(current_bit_depth/8),
notifier=function(idx) destination_bit_depth=idx*8 end,},},
vb:space{height=5},
vb:button{
text="Process",
notifier=function()
local song=renoise.song()
local instrument=song.selected_instrument
local sample_index=song.selected_sample_index
local sample=instrument:sample(sample_index)
local buffer=sample.sample_buffer
if destination_rate==buffer.sample_rate and destination_bit_depth==buffer.bit_depth then
renoise.app():show_status("Sample rate and bit depth are already set to the selected values.")
elseif destination_rate>=buffer.sample_rate then
RenderSampleAtNewRate(destination_rate,destination_bit_depth)
else
DestructiveResample(destination_rate,destination_bit_depth)
end
update_sample_details(sample_name_text,details_text)
end}},
vb:button{text="Close",
notifier=function()
PakettiAudioProcessingToolsDialogClose()
update_sample_details(sample_name_text, details_text)
end}
},
update_sample_details(sample_name_text, details_text)
update_sample_details(details_text, sample_name_text)
return dialog_content
end
local function update_dialog_on_selection_change()
-- if dialog and dialog.visible then
-- Access the UI elements directly from vb.views and pass them to update_sample_details
-- if vb.views.sample_name_text and vb.views.details_text then
local song = renoise.song()
if song then
local instrument = song.selected_instrument
if instrument then
local sample_index = song.selected_sample_index
if sample_index > 0 and sample_index <= #instrument.samples then
local sample = instrument:sample(sample_index)
if sample then
local buffer = sample.sample_buffer
if buffer and buffer.has_sample_data then
-- Update the UI elements with valid sample data
details_text.text = string.format("Details: %dHz, %dbit, %d frames",
buffer.sample_rate, buffer.bit_depth, buffer.number_of_frames)
sample_name_text.text = "Name: " .. sample.name
renoise.app():show_status("Sample details and name updated.")
return -- Exit after successful update
end
end
end
end
end
-- Fallback if no valid sample is available
details_text.text = "Details: No valid sample selected"
sample_name_text.text = "Name: No valid sample selected"
update_sample_details(details_text, sample_name_text)
-- else
-- print("details_text or sample_name_text view is not initialized!")
-- end
-- end
end
-- Function to show or hide the combined dialog
function PakettiAudioProcessingToolsDialogShow()
if renoise.song().selected_instrument_observable:has_notifier(update_dialog_on_selection_change) then
renoise.song().selected_instrument_observable:remove_notifier(update_dialog_on_selection_change)
end
if renoise.song().selected_sample_observable:has_notifier(update_dialog_on_selection_change) then
renoise.song().selected_sample_observable:remove_notifier(update_dialog_on_selection_change)
end
-- Add the observers
renoise.song().selected_instrument_observable:add_notifier(update_dialog_on_selection_change)
renoise.song().selected_sample_observable:add_notifier(update_dialog_on_selection_change)
-- Close the dialog if it's already open
if dialog and dialog.visible then
dialog:close()
dialog = nil
if renoise.song().selected_instrument_observable:has_notifier(update_dialog_on_selection_change) then
renoise.song().selected_instrument_observable:remove_notifier(update_dialog_on_selection_change)
end
if renoise.song().selected_sample_observable:has_notifier(update_dialog_on_selection_change) then
renoise.song().selected_sample_observable:remove_notifier(update_dialog_on_selection_change)
end
return
end
-- Create the dialog content
local content = create_combined_dialog_content()
if content then
-- Show the dialog with the created content and key handler
dialog = renoise.app():show_custom_dialog("Paketti Audio Processing Tools", content, my_AudioProckeyhandler_func)
else
renoise.app():show_status("A sample must be selected.")
end
renoise.app().window.active_middle_frame=renoise.ApplicationWindow.MIDDLE_FRAME_INSTRUMENT_SAMPLE_EDITOR
end
function PakettiAudioProcessingToolsDialogClose()
-- Remove the observers
if renoise.song().selected_instrument_observable:has_notifier(update_dialog_on_selection_change) then
renoise.song().selected_instrument_observable:remove_notifier(update_dialog_on_selection_change)
end
if renoise.song().selected_sample_observable:has_notifier(update_dialog_on_selection_change) then
renoise.song().selected_sample_observable:remove_notifier(update_dialog_on_selection_change)
end
if dialog and dialog.visible then
dialog:close()
end
end
-- Keyhandler function for dialog
function my_AudioProckeyhandler_func(dialog, key)
local closer = preferences.pakettiDialogClose.value
if key.modifiers == "" and key.name == closer then
dialog:close()
dialog = nil
return nil
else
return key
end
end
-- Ensure focus returns to the middle frame after each button press
local function set_middle_frame_focus()
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_INSTRUMENT_SAMPLE_EDITOR
end
-- Keybindings for various operations
renoise.tool():add_keybinding {name = "Sample Editor:Paketti:Phase Inversion", invoke = function() phase_invert_sample() end}
renoise.tool():add_keybinding {name = "Sample Editor:Paketti:Phase Inversion & Audio Diff", invoke = function() phase_invert_and_diff_sample() end}
renoise.tool():add_keybinding {name = "Sample Editor:Paketti:Pitch Shift", invoke = function() pitch_shift_sample(20) end}
renoise.tool():add_keybinding {name = "Sample Editor:Paketti:Pitch Shift & Audio Diff", invoke = function() pitch_shift_and_diff_sample() end}
renoise.tool():add_keybinding {name = "Sample Editor:Paketti:Clip bottom of waveform", invoke = function() modulate_samples() end}
renoise.tool():add_keybinding {name = "Sample Editor:Paketti:Modulate & Audio Diff", invoke = function() modulate_and_diff_sample() end}
renoise.tool():add_keybinding {name = "Sample Editor:Paketti:Invert Right, Sum Mono", invoke = function() invert_right_sum_mono() end}
renoise.tool():add_keybinding {name = "Sample Editor:Paketti:Audio Diff", invoke = function() create_audio_diff_sample() end}
renoise.tool():add_keybinding {name = "Global:Paketti:Paketti Audio Processing Tools Dialog...", invoke = function() PakettiAudioProcessingToolsDialogShow() end}
function update_sample_details(details_text, sample_name_text)
-- If UI elements are not properly initialized, exit early
-- if not details_text or not sample_name_text then
-- print("UI elements are not initialized yet.")
-- return
-- end
local song = renoise.song()
if song then
local instrument = song.selected_instrument
if instrument then
local sample_index = song.selected_sample_index
if sample_index > 0 and sample_index <= #instrument.samples then
local sample = instrument:sample(sample_index)
if sample then
local buffer = sample.sample_buffer
if buffer and buffer.has_sample_data then
-- Update the UI elements with valid sample data
details_text.text = string.format("Details: %dHz, %dbit, %d frames",
buffer.sample_rate, buffer.bit_depth, buffer.number_of_frames)
sample_name_text.text = "Name: " .. sample.name
renoise.app():show_status("Sample details and name updated.")
return -- Exit after successful update
end
end
end
end
end
-- Fallback if no valid sample is available
details_text.text = "Details: No valid sample selected"
sample_name_text.text = "Name: No valid sample selected"
end
function PakettiStripSilence()
local song = renoise.song()
local sample_index = song.selected_sample_index
local instrument = song.selected_instrument
local threshold = renoise.tool().preferences.PakettiStripSilenceThreshold.value
if not instrument or sample_index == 0 or not instrument:sample(sample_index) then
renoise.app():show_status("No valid instrument/sample/sample buffer.")
return
end
local sample = instrument:sample(sample_index)
local buffer = sample.sample_buffer
if not buffer.has_sample_data then
renoise.app():show_status("Sample buffer is empty.")
return
end
buffer:prepare_sample_data_changes()
local num_channels = buffer.number_of_channels
local num_frames = buffer.number_of_frames
local start_frame, end_frame = 1, num_frames
-- Find start frame
for frame = 1, num_frames do
local is_silent = true
for channel = 1, num_channels do
if math.abs(buffer:sample_data(channel, frame)) > threshold then
is_silent = false
break
end
end
if not is_silent then
start_frame = frame
break
end
end
-- Find end frame
for frame = num_frames, 1, -1 do
local is_silent = true
for channel = 1, num_channels do
if math.abs(buffer:sample_data(channel, frame)) > threshold then
is_silent = false
break
end
end
if not is_silent then
end_frame = frame
break
end
end
local new_num_frames = end_frame - start_frame + 1
if new_num_frames < 1 then
renoise.app():show_status("No non-silent data found.")
buffer:finalize_sample_data_changes()
return
end
-- Create a new sample buffer with the trimmed data
local new_sample = instrument:insert_sample_at(sample_index + 1)
local new_buffer = new_sample.sample_buffer
new_buffer:create_sample_data(buffer.sample_rate, buffer.bit_depth, num_channels, new_num_frames)
new_buffer:prepare_sample_data_changes()
for frame = 1, new_num_frames do
for channel = 1, num_channels do
new_buffer:set_sample_data(channel, frame, buffer:sample_data(channel, start_frame + frame - 1))
end
end
new_buffer:finalize_sample_data_changes()
buffer:prepare_sample_data_changes()
buffer:finalize_sample_data_changes()
-- Copy properties from the old sample to the new sample
new_sample.name = sample.name
new_sample.volume = sample.volume
new_sample.panning = sample.panning
new_sample.transpose = sample.transpose
new_sample.fine_tune = sample.fine_tune
new_sample.beat_sync_enabled = sample.beat_sync_enabled
new_sample.beat_sync_lines = sample.beat_sync_lines
new_sample.beat_sync_mode = sample.beat_sync_mode
new_sample.oneshot = sample.oneshot
new_sample.loop_release = sample.loop_release
new_sample.loop_mode = sample.loop_mode
new_sample.mute_group = sample.mute_group
new_sample.new_note_action = sample.new_note_action
new_sample.autoseek = sample.autoseek
new_sample.autofade = sample.autofade
new_sample.oversample_enabled = sample.oversample_enabled
new_sample.interpolation_mode = sample.interpolation_mode
if sample.loop_start < new_buffer.number_of_frames then
new_sample.loop_start = sample.loop_start
end
if sample.loop_end < new_buffer.number_of_frames then
new_sample.loop_end = sample.loop_end
end
-- Delete the old sample
instrument:delete_sample_at(sample_index)
song.selected_sample_index = sample_index
renoise.app():show_status("Removed Silence from beginning + end of sample at threshold " .. (threshold * 100) .. "%")
end
function PakettiMoveSilence()
local song = renoise.song()
local sample_index = song.selected_sample_index
local instrument = song.selected_instrument
local threshold = renoise.tool().preferences.PakettiMoveSilenceThreshold.value
if not instrument or sample_index == 0 or not instrument:sample(sample_index) then
renoise.app():show_status("No valid instrument/sample/sample buffer.")
return
end
local sample = instrument:sample(sample_index)
local buffer = sample.sample_buffer
if not buffer.has_sample_data then
renoise.app():show_status("Sample buffer is empty.")
return
end
buffer:prepare_sample_data_changes()
local num_channels = buffer.number_of_channels
local num_frames = buffer.number_of_frames
local start_frame = 1
-- Find start frame (end of initial silence)
for frame = 1, num_frames do
local is_silent = true
for channel = 1, num_channels do