forked from ReaTeam/ReaScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_Autoincrement MIDI send channels of selected tracks.lua
130 lines (110 loc) · 5.36 KB
/
js_Autoincrement MIDI send channels of selected tracks.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
--[[
* ReaScript Name: js_Autoincrement MIDI send channels of selected tracks
* Description: Autoincrements the MIDI send channels of selected tracks.
* Particularly useful when opening large MIDI files, to ensure that each track's MIDI is 1) sent to a
* single channel, and 2) sent to a different channel than other tracks' MIDI.
* The script starts with a dialog box in which the user can select the following:
* - The starting channel from which to increment.
* - Whether to skip channel 10, which is a dedicated percussion channel in the General MIDI standard.
* - Whether to remove audio sends from the selected tracks, which makes the routing matrix easier to read.
*
* HINT: The following two scripts are useful for setting up the track names of MIDI files:
* "X-Raym_Rename tracks with first VSTi and its preset name.lua"
* "spk77_Rename MIDI tracks.eel"
* Instructions:
*
* Screenshot:
* Notes:
* Category:
* Author: juliansader
* Licence: GPL v3
* Forum Thread:
* Forum Thread URL: http://forum.cockos.com/showthread.php?t=176878
* Version: 1.1
* REAPER: 5.20
* Extensions:
]]
--[[
Changelog:
* v1.0
+ Initial Release
* v1.1 (2016-07-15)
+ Audio send to parent track will also be removed if "Remove audio sends" is selected
]]
-- USER AREA
-- Settings that the user can customize
-- End of USER AREA
---------------------------
-- Is there anything to do?
numSelTracks = reaper.CountSelectedTracks(0)
if (numSelTracks == 0)
then return(0)
end
---------------------------------------------------------------------
-- Part 1: Get user inputs (channel from which to start incrementing)
---------------------------------------------------------------------
-- Repeat until we get usable inputs
repeat
retval, userInputsCSV = reaper.GetUserInputs("Autoincrement MIDI send channels",
3,
"Starting channel (1-16),Skip channel 10? (y/n),Remove audio sends? (y/n)",
"1,y,y")
if retval == false then
return(0)
else -- retval == true
gotUserInputs = true -- temporary, will be changed to false if anything is wrong
startChan, skip10, removeAudio = userInputsCSV:match("([^,]+),([^,]+),([^,]+)")
startChan = tonumber(startChan)
if startChan == nil or startChan < 1 or startChan > 16 then
gotUserInputs = false
end
if skip10 ~= "y" and skip10 ~= "Y" and skip10 ~= "n" and skip10 ~= "N" then
gotUserInputs = false
end
if removeAudio ~= "y" and removeAudio ~= "Y" and removeAudio ~= "n" and removeAudio ~= "N" then
gotUserInputs = false
end
end
until gotUserInputs == true
------------------------------------------------------
-- Now autoincrement all MIDI sends of selected tracks
------------------------------------------------------
-- Note:
-- MIDI hardware cannot be accessed via GetTrackNumSends(selTrack, 1)!
-- 1) Do MIDI hardware output not count as "hardware outputs"?
-- 2) Does REAPER only allow a single MIDI hardware output?
-- Iterate through all selected tracks
chan = startChan
for trackIndex = 0, numSelTracks-1 do
selTrack = reaper.GetSelectedTrack(0, trackIndex)
-- Iterate through all internal track sends
for sendIndex = 0, reaper.GetTrackNumSends(selTrack, 0)-1 do
MIDIflags = reaper.GetTrackSendInfo_Value(selTrack, 0, sendIndex, "I_MIDIFLAGS")
-- I_MIDIFLAGS : returns int *, low 5 bits=source channel 0=all, 1-16,
-- next 5 bits=dest channel, 0=orig, 1-16=chan
-- The following sets orig (bits 1-5) to "all" and inserts channel into bits 6-10.
-- 0xFFFFFC00 = b1111 1111 1111 1111 1111 1100 0000 0000
MIDIflags = (MIDIflags & 0xFFFFFC00)
MIDIflags = MIDIflags | (chan << 5)
-- SetTrackSendInfo_Value(MediaTrack tr, int category, int sendidx, "parmname", newvalue)
-- category is <0 for receives, 0=sends, >0 for hardware outputs
reaper.SetTrackSendInfo_Value(selTrack, 0, sendIndex, "I_MIDIFLAGS", MIDIflags)
-- Remove audio sends (I_SRCCHAN : -1 for none)
if removeAudio == "y" or removeAudio == "Y" then
reaper.SetTrackSendInfo_Value(selTrack, 0, sendIndex, "I_SRCCHAN", -1)
reaper.SetMediaTrackInfo_Value(selTrack, "B_MAINSEND", 0)
end
end
-- Set MIDI hardware output channel
MIDIflags = reaper.GetMediaTrackInfo_Value(selTrack, "I_MIDIHWOUT")
-- I_MIDIHWOUT : int * : track midi hardware output index (<0 for disabled,
-- low 5 bits are which channels (0=all, 1-16),
-- next 5 bits are output device index (0-31))
-- 4294967264 = b11111111111111111111111111100000
MIDIflags = MIDIflags & 4294967264
MIDIflags = MIDIflags | chan
reaper.SetMediaTrackInfo_Value(selTrack, "I_MIDIHWOUT", MIDIflags)
-- Increment channel and skip channel 10 if user so selected
if chan == 16 then chan = 1 else chan = chan + 1 end
if chan == 10 and (skip10 == "Y" or skip10 == "y") then chan = chan + 1 end
end -- for trackIndex = 0, numSelTracks-1