-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtk.lua
More file actions
982 lines (831 loc) · 27.7 KB
/
btk.lua
File metadata and controls
982 lines (831 loc) · 27.7 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
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
local _, path = reaper.get_action_context()
local folder_path = path:match('^.+[\\/]')
package.path = folder_path .. '?.lua;'
local rpr = require 'rpr'
local function Log(...)
local consoleMsg = ""
for _, arg in ipairs(table.pack(...)) do
if consoleMsg ~= "" then
consoleMsg = consoleMsg .. " "
end
if arg == nil then
consoleMsg = consoleMsg .. "(nil)"
else
consoleMsg = consoleMsg .. tostring(arg)
end
end
reaper.ShowConsoleMsg(consoleMsg .. "\n")
end
local function bool2num(bool)
if bool then
return 1
else
return 0
end
end
local function num2bool(number)
if number == 0 then
return false
else
return true
end
end
local function GetAllItems()
local count = reaper.CountMediaItems(0)
local items = {}
for i = 0, count - 1 do
items[i + 1] = reaper.GetMediaItem(0, i)
end
return items
end
local function GetProjectMarkers()
local projectMarkers = {}
local projectMarkersCount = reaper.CountProjectMarkers()
for i = 0, projectMarkersCount - 1 do
local _, isRegion, position, regionEnd, name, regionNumber, color = reaper.EnumProjectMarkers3(0, i)
projectMarkers[#projectMarkers + 1] = {
regionIndex = i,
regionNumber = regionNumber,
position = position,
isRegion = isRegion,
regionEnd = regionEnd,
name = name,
color = color
}
end
return projectMarkers
end
local function GetSelectedItems()
local selectedItems = {}
for _, item in ipairs(GetAllItems()) do
if reaper.IsMediaItemSelected(item) then
selectedItems[#selectedItems + 1] = item
end
end
return selectedItems
end
local function SelectOnlyItems(items)
for _, item in ipairs(GetAllItems()) do
reaper.SetMediaItemSelected(item, false)
end
for _, item in ipairs(items) do
reaper.SetMediaItemSelected(item, true)
end
end
local function SelectOnlyItem(item)
SelectOnlyItems({item})
end
local function GetAllTracks()
local count = reaper.CountTracks(0)
local tracks = {}
for i = 0, count - 1 do
tracks[i + 1] = reaper.GetTrack(0, i)
end
return tracks
end
local function SelectOnlyTracks(tracks)
for _, track in ipairs(GetAllTracks()) do
reaper.SetTrackSelected(track, false)
end
for _, track in ipairs(tracks) do
reaper.SetTrackSelected(track, true)
end
end
local function SelectOnlyTrack(track)
SelectOnlyTracks({track})
end
local function GetSelectedTracks()
local count = reaper.CountSelectedTracks(0)
local tracks = {}
for i = 0, count - 1 do
tracks[i + 1] = reaper.GetSelectedTrack(0, i)
end
return tracks
end
local function IterSelectedTracks()
local i = 0
local selectedTracks = GetSelectedTracks()
return function()
i = i + 1
if i <= #selectedTracks then
return selectedTracks[i]
end
end
end
local function main(name, func)
reaper.Undo_BeginBlock()
func()
reaper.Undo_EndBlock(name, -1)
end
local function GetItemTakeInfo(itemTake)
local _, name = reaper.GetSetMediaItemTakeInfo_String(itemTake, "P_NAME", "", false)
local pcmSourceAddr = reaper.GetMediaItemTakeInfo_Value(itemTake, "P_SOURCE")
local pcmSource = reaper.JS_Window_HandleFromAddress(pcmSourceAddr)
local length, _ = reaper.GetMediaSourceLength(pcmSource)
return {
-- 0=normal, 1=reverse stereo, 2=downmix, 3=left, 4=right
channelMode = reaper.GetMediaItemTakeInfo_Value(itemTake, "I_CHANMODE"),
name = name,
playrate = reaper.GetMediaItemTakeInfo_Value(itemTake, "D_PLAYRATE"),
length = length,
startOffset = reaper.GetMediaItemTakeInfo_Value(itemTake, "D_STARTOFFS")
}
end
local function GetItemInfo(item)
local track = reaper.GetMediaItemInfo_Value(item, "P_TRACK")
local take = reaper.GetActiveTake(item)
return {
track = track,
trackGUID = reaper.GetTrackGUID(track),
position = reaper.GetMediaItemInfo_Value(item, "D_POSITION"),
length = reaper.GetMediaItemInfo_Value(item, "D_LENGTH"),
snapOffset = reaper.GetMediaItemInfo_Value(item, "D_SNAPOFFSET"),
mute = num2bool(reaper.GetMediaItemInfo_Value(item, "B_MUTE")),
muteActual = num2bool(reaper.GetMediaItemInfo_Value(item, "B_MUTE_ACTUAL")),
take = take,
takeInfo = GetItemTakeInfo(take),
fadeInLength = reaper.GetMediaItemInfo_Value(item, "D_FADEINLEN"),
fadeOutLength = reaper.GetMediaItemInfo_Value(item, "D_FADEOUTLEN"),
fadeInCurvature = reaper.GetMediaItemInfo_Value(item, "D_FADEINDIR"),
fadeOutCurvature = reaper.GetMediaItemInfo_Value(item, "D_FADEOUTDIR"),
fadeInShape = reaper.GetMediaItemInfo_Value(item, "C_FADEINSHAPE"),
fadeOutShape = reaper.GetMediaItemInfo_Value(item, "C_FADEOUTSHAPE")
}
end
local function GetItemsInfo(items)
local itemsInfo = {}
for i, item in ipairs(items) do
itemsInfo[i] = GetItemInfo(item)
end
return itemsInfo
end
local function SetItemInfo(item, info)
if info.position ~= nil then
reaper.SetMediaItemInfo_Value(item, "D_POSITION", info.position)
end
if info.length ~= nil then
reaper.SetMediaItemInfo_Value(item, "D_LENGTH", info.length)
end
if info.mute ~= nil then
reaper.SetMediaItemInfo_Value(item, "B_MUTE", bool2num(info.mute))
end
if info.snapOffset ~= nil then
reaper.SetMediaItemInfo_Value(item, "D_SNAPOFFSET", info.snapOffset)
end
if info.fadeInLength ~= nil then
reaper.SetMediaItemInfo_Value(item, "D_FADEINLEN", info.fadeInLength)
end
if info.fadeOutLength ~= nil then
reaper.SetMediaItemInfo_Value(item, "D_FADEOUTLEN", info.fadeOutLength)
end
if info.fadeInCurvature ~= nil then
reaper.SetMediaItemInfo_Value(item, "D_FADEINDIR", info.fadeInCurvature)
end
if info.fadeOutCurvature ~= nil then
reaper.SetMediaItemInfo_Value(item, "D_FADEOUTDIR", info.fadeOutCurvature)
end
if info.fadeInShape ~= nil then
reaper.SetMediaItemInfo_Value(item, "C_FADEINSHAPE", info.fadeInShape)
end
if info.fadeOutShape ~= nil then
reaper.SetMediaItemInfo_Value(item, "C_FADEOUTSHAPE", info.fadeOutShape)
end
end
local function SetItemTakeInfo(itemTake, info)
if info.startOffset ~= nil then
reaper.SetMediaItemTakeInfo_Value(itemTake, "D_STARTOFFS", info.startOffset)
end
end
local function GetTrackInfo(track)
local _, name = reaper.GetSetMediaTrackInfo_String(track, "P_NAME", "", false)
local trackNumber = reaper.GetMediaTrackInfo_Value(track, "IP_TRACKNUMBER")
return {
mute = num2bool(reaper.GetMediaTrackInfo_Value(track, "B_MUTE")),
name = name,
trackNumber1Based = trackNumber,
index = trackNumber - 1,
folderCompact = reaper.GetMediaTrackInfo_Value(track, "I_FOLDERCOMPACT"),
folderDepth = reaper.GetMediaTrackInfo_Value(track, "I_FOLDERDEPTH")
}
end
local function SetTrackInfo(track, info)
if info.mute ~= nil then
reaper.SetMediaTrackInfo_Value(track, "B_MUTE", bool2num(info.mute))
end
if info.name ~= nil then
reaper.GetSetMediaTrackInfo_String(track, "P_NAME", info.name, true)
end
if info.folderCompact ~= nil then
reaper.SetMediaTrackInfo_Value(track, "I_FOLDERCOMPACT", info.folderCompact)
end
end
local function MoveItemToTrack(item, track)
local itemTrack = GetItemInfo(item).track
if reaper.GetTrackGUID(itemTrack) ~= reaper.GetTrackGUID(track) then
local _, itemChunk = reaper.GetItemStateChunk(item, '')
reaper.DeleteTrackMediaItem(itemTrack, item)
local newItem = reaper.AddMediaItemToTrack(track)
reaper.SetItemStateChunk(newItem, itemChunk)
end
end
local function GetLoopTimeRange()
return reaper.GetSet_LoopTimeRange2(0, false, false, 0, 0, false)
end
local function HasLoopTimeRange()
local loopStart, loopEnd = GetLoopTimeRange()
return loopStart ~= loopEnd
end
local function GetLoopTimeRangeOrCursor()
local loopStart, loopEnd = GetLoopTimeRange()
if loopStart == loopEnd then
loopStart = reaper.GetCursorPosition()
loopEnd = loopStart
end
return loopStart, loopEnd
end
local function SetLoopTimeRange(loopStart, loopEnd)
reaper.GetSet_LoopTimeRange2(0, true, true, loopStart, loopEnd, true)
end
local function SetLoopTimeRangeAndCursor(loopStart, loopEnd)
reaper.GetSet_LoopTimeRange2(0, true, true, loopStart, loopEnd, true)
local cursor = reaper.GetCursorPosition()
reaper.MoveEditCursor(loopEnd - cursor, false)
end
local function ExtendTimeSelection(seconds)
local loopStart, loopEnd = GetLoopTimeRange()
SetLoopTimeRange(loopStart, math.max(loopStart, loopEnd + seconds))
end
local function GetChildTracks(track)
local trackInfo = GetTrackInfo(track)
local trackDepth = reaper.GetTrackDepth(track)
local childTracks = {}
for i = trackInfo.trackNumber1Based, reaper.CountTracks(0) - 1 do
local nextTrack = reaper.GetTrack(0, i)
local nextTrackDepth = reaper.GetTrackDepth(nextTrack)
if nextTrackDepth <= trackDepth then
break
end
if nextTrackDepth == trackDepth + 1 then
childTracks[#childTracks + 1] = nextTrack
end
end
return childTracks
end
local function GetDescendantTracks(track)
local trackInfo = GetTrackInfo(track)
local trackDepth = reaper.GetTrackDepth(track)
local descendantTracks = {}
for i = trackInfo.trackNumber1Based, reaper.CountTracks(0) - 1 do
local nextTrack = reaper.GetTrack(0, i)
if reaper.GetTrackDepth(nextTrack) <= trackDepth then
break
end
descendantTracks[#descendantTracks + 1] = nextTrack
end
return descendantTracks
end
local function GetItemsInTrack(track)
local count = reaper.CountMediaItems(0)
local items = {}
for i = 0, count - 1 do
local item = reaper.GetMediaItem(0, i)
local itemInfo = GetItemInfo(item)
if reaper.GetTrackGUID(itemInfo.track) == reaper.GetTrackGUID(track) then
items[#items + 1] = item
end
end
return items
end
local function GetMaxItemLength(items)
local maxLength = 0
for _, item in ipairs(items) do
local itemInfo = GetItemInfo(item)
maxLength = math.max(maxLength, itemInfo.length)
end
return maxLength
end
local function Approximately(x, y)
return math.abs(x - y) < 1e-6
end
local function GetMarkerSnapPoints()
local snapPoints = {0}
local function InsertIfUnique(num)
if #snapPoints == 0 or not Approximately(num, snapPoints[#snapPoints]) then
snapPoints[#snapPoints + 1] = num
end
end
for _, marker in ipairs(GetProjectMarkers()) do
InsertIfUnique(marker.position)
if marker.isRegion then
InsertIfUnique(marker.regionEnd)
end
end
local loopStart, loopEnd = GetLoopTimeRange()
if loopStart ~= loopEnd then
InsertIfUnique(loopStart)
end
table.sort(snapPoints)
return snapPoints
end
local function FindClosestNumber(numbers, target)
local closestIndex = 1
local closestPoint = numbers[1]
for i, snapPoint in ipairs(numbers) do
if math.abs(snapPoint - target) < math.abs(closestPoint - target) then
closestIndex = i
closestPoint = snapPoint
end
end
return closestIndex, closestPoint
end
local function RGB(r, g, b)
return reaper.ColorToNative(math.ceil(r), math.ceil(g), math.ceil(b)) | 0x1000000
end
local function Clamp(x, min, max)
if x < min then
return min
elseif x > max then
return max
else
return x
end
end
local function HSL(h, s, l)
-- From https://stackoverflow.com/questions/68317097/how-to-properly-convert-hsl-colors-to-rgb-colors-in-lua
h = Clamp(h, 0, 360) / 360
s = Clamp(s, 0, 1)
l = Clamp(l, 0, 1)
local r, g, b;
if s == 0 then
r, g, b = l, l, l; -- achromatic
else
local function hue2rgb(p, q, t)
if t < 0 then
t = t + 1
end
if t > 1 then
t = t - 1
end
if t < 1 / 6 then
return p + (q - p) * 6 * t
end
if t < 1 / 2 then
return q
end
if t < 2 / 3 then
return p + (q - p) * (2 / 3 - t) * 6
end
return p;
end
local q = l < 0.5 and l * (1 + s) or l + s - l * s;
local p = 2 * l - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
end
return RGB(r * 255, g * 255, b * 255)
end
local function Avg(values)
if #values == 0 then
reaper.ShowConsoleMsg("Error: no values to calculate average")
end
local total = 0
for _, value in ipairs(values) do
total = total + value
end
return total / #values
end
local function Contains(values, containsValue)
for _, value in ipairs(values) do
if value == containsValue then
return true
end
end
return false
end
local function GenerateMarkerColors(regenerate)
local markers = GetProjectMarkers()
local foundRegionMatrixEntry = false
for _, marker in ipairs(markers) do
if marker.isRegion and reaper.EnumRegionRenderMatrix(0, marker.regionNumber, 0) ~= nil then
foundRegionMatrixEntry = true
break
end
end
if not foundRegionMatrixEntry then
return
end
local function RandomHue(prevHue)
local hue = math.random(40, 300)
while math.abs(hue - prevHue) < 50 do
hue = math.random(40, 300)
end
return hue
end
local function BlackTint(tint)
local r, g, b = reaper.ColorFromNative(tint)
return reaper.ColorToNative(math.ceil(r / 8), math.ceil(g / 8), math.ceil(b / 8))
end
local hue = nil
local regionedTracks = {}
local defaultColor = RGB(255, 0, 0)
local function SetTrackColor(track, color)
reaper.SetTrackColor(track, color)
local trackGUID = reaper.GetTrackGUID(track)
regionedTracks[trackGUID] = true
end
for _, track in ipairs(GetAllTracks()) do
reaper.SetMediaTrackInfo_Value(track, "I_CUSTOMCOLOR", 0)
defaultColor = reaper.GetTrackColor(track)
end
local previousMarkerName = nil
for _, marker in ipairs(markers) do
local markerColor = marker.color
if regenerate or markerColor == 0 then
if previousMarkerName == nil or previousMarkerName ~= marker.name then
hue = RandomHue(hue or 0, 50)
previousMarkerName = marker.name
end
markerColor = HSL(hue, 1, 0.5)
reaper.SetProjectMarker4(0, marker.regionNumber, marker.isRegion, marker.position, marker.regionEnd, "",
markerColor, 0)
end
if marker.isRegion then
for i = 0, 1000 do
local track = reaper.EnumRegionRenderMatrix(0, marker.regionNumber, i)
if track == nil then
break
end
SetTrackColor(track, markerColor)
for _, descTrack in ipairs(GetDescendantTracks(track)) do
SetTrackColor(descTrack, markerColor)
end
end
end
end
end
local function CopyTrackRegionRenderMatrix(fromTrack, toTrack)
for _, marker in ipairs(GetProjectMarkers()) do
if marker.isRegion then
for i = 0, 100 do
local track = reaper.EnumRegionRenderMatrix(0, marker.regionIndex, i)
if track == nil then
break
elseif track == fromTrack then
reaper.SetRegionRenderMatrix(0, marker.regionIndex, toTrack, 1)
end
end
end
end
end
local function GetRenderedTracks()
local trackGUIDSet = {}
local tracks = {}
for _, marker in ipairs(GetProjectMarkers()) do
if marker.isRegion then
for i = 0, 100 do
local track = reaper.EnumRegionRenderMatrix(0, marker.regionIndex, i)
if track == nil then
break
end
local trackGUID = reaper.GetTrackGUID(track)
if trackGUID[trackGUID] == nil then
trackGUIDSet[trackGUID] = track
tracks[#tracks + 1] = track
end
end
end
end
return tracks, trackGUIDSet
end
local function GetAllItemsInRange(rangeStart, rangeEnd)
local itemsInRange = {}
for _, item in ipairs(GetAllItems()) do
local info = GetItemInfo(item)
if info.position < rangeEnd and info.position + info.length > rangeStart then
itemsInRange[#itemsInRange + 1] = item
end
end
return itemsInRange
end
local function GetTrackFolderHierarchy(track)
local info = GetTrackInfo(track)
local hierarchy = {}
local trackCursor = track
while true do
local cursorDepth = reaper.GetTrackDepth(trackCursor)
if cursorDepth == 0 then
break
end
while reaper.GetTrackDepth(trackCursor) >= cursorDepth do
trackCursor = reaper.GetTrack(0, GetTrackInfo(trackCursor).index - 1)
end
hierarchy[#hierarchy + 1] = trackCursor
end
return hierarchy
end
local function ShowTrackInFolderHierarchy(track)
local hierarchy = GetTrackFolderHierarchy(track)
for _, folder in ipairs(hierarchy) do
SetTrackInfo(folder, {
folderCompact = 0
})
end
end
local function ShowTracksInFolderHierarchy(tracks)
for _, track in ipairs(tracks) do
ShowTrackInFolderHierarchy(track)
end
end
local function Reverse(list)
for i = 1, math.floor(#list / 2) do
local t = list[i]
list[i] = list[#list - i + 1]
list[#list - i + 1] = t
end
end
local function AnyCollapsed(folders)
for _, folder in ipairs(folders) do
if GetTrackInfo(folder).folderCompact ~= 0 then
return true
end
end
return false
end
local function GetAllFolders()
local allFolders = {}
for _, track in ipairs(GetAllTracks()) do
if GetTrackInfo(track).folderDepth == 1 then
allFolders[#allFolders + 1] = track
end
end
return allFolders
end
local function GetSelectedFolders()
local selectedFolders = {}
for _, track in ipairs(GetSelectedTracks()) do
if GetTrackInfo(track).folderDepth == 1 then
selectedFolders[#selectedFolders + 1] = track
end
end
return selectedFolders
end
local function ToggleFolderCollapsedStateAtDepth(depth)
local allFolders = GetAllFolders()
local folders1 = {}
for _, folder in ipairs(allFolders) do
if reaper.GetTrackDepth(folder) < depth then
folders1[#folders1 + 1] = folder
end
end
for _, folder in ipairs(allFolders) do
SetTrackInfo(folder, {
folderCompact = 2
})
end
for _, folder in ipairs(folders1) do
SetTrackInfo(folder, {
folderCompact = 0
})
end
end
local function Concat(l, r)
local result = {}
for _, v in ipairs(l) do
result[#result + 1] = v
end
for _, v in ipairs(r) do
result[#result + 1] = v
end
return result
end
local function RenderSelectionOrSelectedItems(stereo, intoPreviousTrack, intoSelf)
local selectedTracks = GetSelectedTracks()
if #selectedTracks == 0 then
return
end
local track = selectedTracks[1]
reaper.SetOnlyTrackSelected(selectedTracks[1])
local trackInfo = GetTrackInfo(track)
local loopStart, loopEnd = reaper.GetSet_LoopTimeRange2(0, false, false, 0, 0, false)
local loopItems = nil
if loopStart == loopEnd then
loopItems = GetSelectedItems()
if #loopItems == 0 then
return
end
rpr.sws_save_edit_cursor()
rpr.time_selection_set_to_items()
ExtendTimeSelection(1)
end
if stereo then
rpr.track_render_selected_area_to_stereo()
else
rpr.track_render_selected_area_to_mono()
end
local renderedTrack = GetSelectedTracks()[1]
if intoPreviousTrack and trackInfo.trackNumber1Based > 1 then
-- GetTrack is 0-based, trackNumber1Based is 1-based.
local originalPreviousTrack = reaper.GetTrack(0, trackInfo.trackNumber1Based - 2)
for _, item in ipairs(GetItemsInTrack(renderedTrack)) do
MoveItemToTrack(item, originalPreviousTrack)
end
reaper.DeleteTrack(renderedTrack)
elseif intoSelf then
reaper.SetOnlyTrackSelected(track)
rpr.item_select_all_in_track()
rpr.item_remove()
for _, item in ipairs(GetItemsInTrack(renderedTrack)) do
MoveItemToTrack(item, track)
end
reaper.DeleteTrack(renderedTrack)
end
if loopItems then
for _, item in ipairs(loopItems) do
reaper.SetMediaItemInfo_Value(item, "B_MUTE", 1)
end
end
if loopStart == loopEnd then
reaper.GetSet_LoopTimeRange2(0, true, true, loopStart, loopEnd, false)
rpr.sws_restore_edit_cursor()
end
SetTrackInfo(track, {
mute = trackInfo.mute
})
SelectOnlyTracks(selectedTracks)
end
local function DeleteTrackRecursive(track)
local selectedTracks = GetSelectedTracks()
local function Inner(deleteTrack)
for _, child in ipairs(GetChildTracks(deleteTrack)) do
Inner(child)
end
SelectOnlyTrack(deleteTrack)
reaper.ReorderSelectedTracks(1, 0)
reaper.DeleteTrack(deleteTrack)
end
Inner(track)
SelectOnlyTracks(selectedTracks)
end
local function SplitItemsByTrack(items)
local itemsByTrack = {}
local splitItems = {}
for _, item in ipairs(items) do
local trackGUID = GetItemInfo(item).trackGUID
local trackItems = itemsByTrack[trackGUID]
if trackItems == nil then
trackItems = {}
itemsByTrack[trackGUID] = trackItems
splitItems[#splitItems + 1] = trackItems
end
trackItems[#trackItems + 1] = item
end
return splitItems
end
local function GlueItemsPreserveFade(items)
local firstItem = items[1]
local firstItemInfo = GetItemInfo(firstItem)
local lastItem = firstItem
local lastItemInfo = firstItemInfo
for i = 2, #items do
local item = items[i]
local itemInfo = GetItemInfo(item)
if itemInfo.position < firstItemInfo.position then
firstItem = item
firstItemInfo = itemInfo
end
if itemInfo.position + itemInfo.length > lastItemInfo.position + lastItemInfo.length then
lastItem = item
lastItemInfo = itemInfo
end
end
SetItemInfo(firstItem, {
fadeInLength = 0
})
SetItemInfo(lastItem, {
fadeOutLength = 0
})
SelectOnlyItems(items)
rpr.item_glue()
local gluedItem = GetSelectedItems()[1]
SetItemInfo(gluedItem, {
fadeInLength = firstItemInfo.fadeInLength,
fadeInShape = firstItemInfo.fadeInShape,
fadeInCurvature = firstItemInfo.fadeInCurvature,
fadeOutLength = lastItemInfo.fadeOutLength,
fadeOutShape = lastItemInfo.fadeOutShape,
fadeOutCurvature = lastItemInfo.fadeOutCurvature
})
return gluedItem
end
local function FirstItemPosition(items)
local firstItemPosition = -1
for _, item in ipairs(items) do
local info = GetItemInfo(item)
if firstItemPosition == -1 or info.position < firstItemPosition then
firstItemPosition = info.position
end
end
return firstItemPosition
end
local function InsertRegionForItemsAndAddToMatrix(items)
if #items == 0 then
return
end
local itemsInfo = GetItemsInfo(items)
local startPosition = -1
local endPosition = -1
for _, info in ipairs(itemsInfo) do
if startPosition == -1 or info.position < startPosition then
startPosition = info.position
end
if endPosition == -1 or info.position + info.length > endPosition then
endPosition = info.position + info.length
end
end
local defaultName = GetTrackInfo(itemsInfo[1].track).name
defaultName = string.gsub(defaultName, "%(%d%)", "")
local _, regionName = reaper.GetUserInputs("Region name [" .. defaultName .. "]", 1, "", "")
if regionName == "" then
regionName = defaultName
end
local regionIndex = reaper.AddProjectMarker(0, true, startPosition, endPosition, regionName, -1)
for _, info in ipairs(itemsInfo) do
reaper.SetRegionRenderMatrix(0, regionIndex, info.track, 1)
end
end
local function Redraw()
-- The view doesn't update until the cursor is moved for some reason
rpr.view_move_cursor_right_one_pixel()
rpr.view_move_cursor_left_one_pixel()
end
local function MoveItemToRelativeTrackNumber(item, delta)
local trackInfo = GetTrackInfo(GetItemInfo(item).track)
local newTrackIndex = trackInfo.index + delta
newTrackIndex = math.max(0, math.min(reaper.CountTracks(0) - 1, newTrackIndex))
reaper.MoveMediaItemToTrack(item, reaper.GetTrack(0, newTrackIndex))
Redraw()
end
return {
AnyCollapsed = AnyCollapsed,
Approximately = Approximately,
Avg = Avg,
bool2num = bool2num,
Clamp = Clamp,
Concat = Concat,
Contains = Contains,
CopyTrackRegionRenderMatrix = CopyTrackRegionRenderMatrix,
DeleteTrackRecursive = DeleteTrackRecursive,
ExtendTimeSelection = ExtendTimeSelection,
FindClosestNumber = FindClosestNumber,
FirstItemPosition = FirstItemPosition,
GenerateMarkerColors = GenerateMarkerColors,
GetAllFolders = GetAllFolders,
GetAllItems = GetAllItems,
GetAllItemsInRange = GetAllItemsInRange,
GetAllTracks = GetAllTracks,
GetChildTracks = GetChildTracks,
GetDescendantTracks = GetDescendantTracks,
GetItemInfo = GetItemInfo,
GetItemsInfo = GetItemsInfo,
GetItemsInTrack = GetItemsInTrack,
GetItemTakeInfo = GetItemTakeInfo,
GetLoopTimeRange = GetLoopTimeRange,
GetLoopTimeRangeOrCursor = GetLoopTimeRangeOrCursor,
GetMarkerSnapPoints = GetMarkerSnapPoints,
GetMaxItemLength = GetMaxItemLength,
GetProjectMarkers = GetProjectMarkers,
GetRenderedTracks = GetRenderedTracks,
GetSelectedFolders = GetSelectedFolders,
GetSelectedItems = GetSelectedItems,
GetSelectedTracks = GetSelectedTracks,
GetTrackFolderHierarchy = GetTrackFolderHierarchy,
GetTrackInfo = GetTrackInfo,
GlueItemsPreserveFade = GlueItemsPreserveFade,
HasLoopTimeRange = HasLoopTimeRange,
HSL = HSL,
InsertRegionForItemsAndAddToMatrix = InsertRegionForItemsAndAddToMatrix,
IterSelectedTracks = IterSelectedTracks,
Log = Log,
main = main,
MoveItemToRelativeTrackNumber = MoveItemToRelativeTrackNumber,
MoveItemToTrack = MoveItemToTrack,
num2bool = num2bool,
Redraw = Redraw,
RenderSelectionOrSelectedItems = RenderSelectionOrSelectedItems,
Reverse = Reverse,
RGB = RGB,
SelectOnlyItem = SelectOnlyItem,
SelectOnlyItems = SelectOnlyItems,
SelectOnlyTrack = SelectOnlyTrack,
SelectOnlyTracks = SelectOnlyTracks,
SetItemInfo = SetItemInfo,
SetItemTakeInfo = SetItemTakeInfo,
SetLoopTimeRange = SetLoopTimeRange,
SetLoopTimeRangeAndCursor = SetLoopTimeRangeAndCursor,
SetTrackInfo = SetTrackInfo,
ShowTrackInFolderHierarchy = ShowTrackInFolderHierarchy,
ShowTracksInFolderHierarchy = ShowTracksInFolderHierarchy,
SplitItemsByTrack = SplitItemsByTrack,
ToggleFolderCollapsedStateAtDepth = ToggleFolderCollapsedStateAtDepth
}