-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzaibuyidao_cencyte_Split Note (fast).lua
413 lines (396 loc) · 13.8 KB
/
zaibuyidao_cencyte_Split Note (fast).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
--[[
@Modified by: Cencyte
@Original Author(s): Zaibuyidao
@Date Created: 11-17-23
@Description: Selects Note Under Mouse Cursor
@Version: 1.0
@License: MIT
]]
EVENT_NOTE_START = 9
EVENT_NOTE_END = 8
EVENT_ARTICULATION = 15
local events
local midi
function print(...)
local params = {...}
for i = 1, #params do
if i ~= 1 then reaper.ShowConsoleMsg(" ") end
reaper.ShowConsoleMsg(tostring(params[i]))
end
reaper.ShowConsoleMsg("\n")
end
function table.print(t)
local print_r_cache = {}
local function sub_print_r(t, indent)
if (print_r_cache[tostring(t)]) then
print(indent .. "*" .. tostring(t))
else
print_r_cache[tostring(t)] = true
if (type(t) == "table") then
for pos, val in pairs(t) do
if (type(val) == "table") then
print(indent .. "[" .. tostring(pos) .. "] => " .. tostring(t) .. " {")
sub_print_r(val, indent .. string.rep(" ", string.len(tostring(pos)) + 8))
print(indent .. string.rep(" ", string.len(tostring(pos)) + 6) .. "}")
elseif (type(val) == "string") then
print(indent .. "[" .. tostring(pos) .. '] => "' .. val .. '"')
else
print(indent .. "[" .. tostring(pos) .. "] => " .. tostring(val))
end
end
else
print(indent .. tostring(t))
end
end
end
if (type(t) == "table") then
print(tostring(t) .. " {")
sub_print_r(t, " ")
print("}")
else
sub_print_r(t, " ")
end
end
function Open_URL(url)
if not OS then local OS = reaper.GetOS() end
if OS=="OSX32" or OS=="OSX64" then
os.execute("open ".. url)
else
os.execute("start ".. url)
end
end
if not reaper.SN_FocusMIDIEditor then
local retval = reaper.ShowMessageBox("This script needs SWS to expand, do you want to download it now?", "Warning", 1)
if retval == 1 then
Open_URL("http://www.sws-extension.org/download/pre-release/")
end
end
local function clone(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for key, value in pairs(object) do
new_table[_copy(key)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end
local EventMeta = {
__index = function (event, key)
if (key == "selected") then
return event.flags & 1 == 1
elseif key == "pitch" then
return event.msg:byte(2)
elseif key == "Velocity" then
return event.msg:byte(3)
elseif key == "type" then
return event.msg:byte(1) >> 4
elseif key == "articulation" then
return event.msg:byte(1) >> 4
end
end,
__newindex = function (event, key, value)
if key == "pitch" then
event.msg = string.pack("BBB", event.msg:byte(1), value or event.msg:byte(2), event.msg:byte(3))
elseif key == "Velocity" then
event.msg = string.pack("BBB", event.msg:byte(1), event.msg:byte(2), value or event.msg:byte(3))
end
end
}
function setAllEvents(take, events)
local lastPos = 0
for _, event in pairs(events) do
event.offset = event.pos - lastPos
lastPos = event.pos
end
local tab = {}
for _, event in pairs(events) do
table.insert(tab, string.pack("i4Bs4", event.offset, event.flags, event.msg))
end
reaper.MIDI_SetAllEvts(take, table.concat(tab))
end
function getAllEvents(take, onEach)
local events = {}
local _, MIDIstring = reaper.MIDI_GetAllEvts(take, "")
local stringPos = 1
local lastPos = 0
while stringPos <= MIDIstring:len() do
local offset, flags, msg
offset, flags, msg, stringPos = string.unpack("i4Bs4", MIDIstring, stringPos)
local event = setmetatable({
offset = offset,
pos = lastPos + offset,
flags = flags,
msg = msg
}, EventMeta)
table.insert(events, event)
onEach(event)
lastPos = lastPos + offset
end
return events
end
function getAllTakes()
tTake = {}
if reaper.MIDIEditor_EnumTakes then
local editor = reaper.MIDIEditor_GetActive()
for i = 0, math.huge do
take = reaper.MIDIEditor_EnumTakes(editor, i, false)
if take and reaper.ValidatePtr2(0, take, "MediaItem_Take*") then
tTake[take] = true
tTake[take] = {item = reaper.GetMediaItemTake_Item(take)}
else
break
end
end
else
for i = 0, reaper.CountMediaItems(0)-1 do
local item = reaper.GetMediaItem(0, i)
local take = reaper.GetActiveTake(item)
if reaper.ValidatePtr2(0, take, "MediaItem_Take*") and reaper.TakeIsMIDI(take) and reaper.MIDI_EnumSelNotes(take, -1) == 0 then -- Get potential takes that contain notes. NB == 0
tTake[take] = true
end
end
for take in next, tTake do
if reaper.MIDI_EnumSelNotes(take, -1) ~= -1 then tTake[take] = nil end
end
end
if not next(tTake) then return end
return tTake
end
local sourceLengthTicks = reaper.BR_GetMidiSourceLenPPQ(take)
local notes = {}
local noteLastEventAtPitch = {}
local articulationEventAtPitch = {}
local switch = false
function main(div, take)
if div == nil then return end
reaper.MIDI_DisableSort(take)
if div == 1 and switch == false then
events = getAllEvents(take, function(event)
if event.type == EVENT_NOTE_START then
noteLastEventAtPitch[event.pitch] = event
elseif event.type == EVENT_NOTE_END then
local head = noteLastEventAtPitch[event.pitch]
if head == nil then error("The notes overlap and cannot be analyzed") end
local tail = event
if event.selected and div <= tail.pos - head.pos then
table.insert(notes, {
head = head,
tail = tail,
articulation = articulationEventAtPitch[event.pitch],
pitch = event.pitch
})
end
noteLastEventAtPitch[event.pitch] = nil
articulationEventAtPitch[event.pitch] = nil
elseif event.type == EVENT_ARTICULATION then
if event.msg:byte(1) == 0xFF and not (event.msg:byte(2) == 0x0F) then
-- text event
elseif event.msg:find("articulation") then
local chan, pitch = event.msg:match("NOTE (%d+) (%d+) ")
articulationEventAtPitch[tonumber(pitch)] = event
end
end
end)
switch = true
elseif div >= 1 and switch == true then
end
local skipEvents = {}
local replacementForEvent = {}
local copyAritulationForEachNote = false -- If it is true, each piece of the cut will be accompanied by the original symbol information
for _, note in ipairs(notes) do
local replacement = {}
skipEvents[note.head] = true
skipEvents[note.tail] = true
local len = note.tail.pos - note.head.pos
local len_div = math.floor(len / div)
local mult_len = note.head.pos + div * len_div
local first = true -- Is it the first note after cutting
for j = 1, div do
local newNote = clone(note)
newNote.head.pos = note.head.pos + (j - 1) * len_div
newNote.tail.pos = note.head.pos + (j - 1) * len_div + len_div
if newNote.articulation then newNote.articulation.pos = newNote.head.pos end
table.insert(replacement, newNote.head)
if first or copyAritulationForEachNote then
table.insert(replacement, newNote.articulation)
end
table.insert(replacement, newNote.tail)
first = false
end
if mult_len < note.tail.pos then
local newNote = clone(note)
newNote.head.pos = note.head.pos + div * len_div
newNote.tail.pos = note.tail.pos
if newNote.articulation then newNote.articulation.pos = newNote.head.pos end
table.insert(replacement, newNote.head)
if first or copyAritulationForEachNote then
table.insert(replacement, newNote.articulation)
end
table.insert(replacement, newNote.tail)
first = false
end
replacementForEvent[note.tail] = replacement
end
local newEvents = {}
local last = events[#events]
table.remove(events, #events) -- All-Note-Off
for _, event in ipairs(events) do
if replacementForEvent[event] then
for _, e in ipairs(replacementForEvent[event]) do table.insert(newEvents, e) end
end
if not skipEvents[event] then
table.insert(newEvents, event)
end
end
table.insert(newEvents, last) -- All-Note-Off
table.insert(events, last) -- All-Note-Off
setAllEvents(take, newEvents)
reaper.MIDI_Sort(take)
end
local dy_prev
local div_prev
local yprev
local time
local time2
local p_time
local p_time2
local next_Int
local div = 1
local y_origin
local midiview = reaper.JS_Window_Find('midiview', true)
local Hwnd_At_Cursor = reaper.JS_Window_FromPoint(reaper.GetMousePosition())
local cursor_Int = 10
local mouseMsg = {}
local EventMeta2 = {
__index = function(mouseMsg, key)
if string.match(key, "^0x%d%d%d%d$") then
local v1, v2, v3, v4, v5, v6, v7 = reaper.JS_WindowMessage_Peek(Hwnd_At_Cursor, key) --passthrough = true
local mouseMsg = { OK = v1, pt = v2 , time = v3, ID = v4, wHigh = v5, x = v6, y = v7 }
mouseMsg[key] = key
return mouseMsg
end
if key == "OK" then
return mouseMsg[key].OK
elseif key == "pt" then
return mouseMsg[key].pt
elseif key == "time" then
return mouseMsg[key].time
elseif key == "ID" then
return mouseMsg[key].ID
elseif key == "wHigh" then
return mouseMsg[key].wHigh
elseif key == "x" then
return mouseMsg[key].x
elseif key == "y" then
return mouseMsg[key].y
end
end,
__newIndex = function(mouseMsg, key, value)
if key == "intercept" then
reaper.JS_WindowMessage_Intercept(Hwnd_At_Cursor, value, true)
elseif key == "release" then
reaper.JS_WindowMessage_Release(Hwnd_At_Cursor, value, true)
end
end,
__call = function(self, args)
reaper.JS_WindowMessage_Intercept(Hwnd_At_Cursor, tostring(args), true)
local v1, v2, v3, v4, v5, v6, v7 = reaper.JS_WindowMessage_Peek(Hwnd_At_Cursor, tostring(args)) --passthrough = true
local mouseMsg = { OK = v1, pt = v2 , time = v3, ID = v4, wHigh = v5, x = v6, y = v7 }
for k, v in pairs(mouseMsg) do
if mouseMsg[v].OK then
print(args)
return { OK = v1, pt = v2 , time = v3, ID = v4, wHigh = v5, x = v6, y = v7 }
end
end
end
}
local KeyCodes = {
WM_LBUTTONDOWN = '0x0201',
WM_LBUTTONUP = '0x0202',
WM_LBUTTONDBLCLK = '0x0203',
WM_MOUSEMOVE = '0x0200'
}
setmetatable(mouseMsg, EventMeta2)
y_origin = mouseMsg['0x0201'].y
reaper.DeleteExtState("CC Script", "Released", true)
reaper.DeleteExtState("CC Script", "Pressed", true)
reaper.atexit(reaper.JS_WindowMessage_ReleaseAll())
for take, _ in pairs(getAllTakes()) do
local _, MIDIstring = reaper.MIDI_GetAllEvts(take, "")
local sourceLengthTicks = reaper.BR_GetMidiSourceLenPPQ(take)
if not (sourceLengthTicks == reaper.BR_GetMidiSourceLenPPQ(take)) then
reaper.MIDI_SetAllEvts(take, MIDIstring)
reaper.ShowMessageBox("The script caused the event to move, and the original MIDI data has been restored", "Error", 0)
end
local function OnRelease()
local time2 = mouseMsg['0x0202'].time
if p_time2 then print("p_time2 is: " .. p_time2) end
if mouseMsg['0x0202'].OK and time2 ~= 0.0 and time2 ~= p_time2 and reaper.GetExtState("CC Script", "Pressed") then
p_time2 = time2
reaper.SetExtState("CC Script", "Released", "true", true)
reaper.SetExtState("CC Script", "Pressed", "false", true)
else
local ypos = mouseMsg['0x0200'].y
if ypos ~= 0 then
if ypos ~= yprev and mouseMsg['0x0200'].time ~= time then
local _dy = y_origin - ypos
if _dy <= 0 then
reaper.MIDI_SetAllEvts(take, MIDIstring)
else
local dy = math.abs(ypos - y_origin)
local div = ((dy // cursor_Int) or 1); if div == 0 then div = 1 end
if not next_Int or (div >= 1 and ((dy > next_Int) or (dy < (next_Int - cursor_Int)))) then
local dely = dy - (dy_prev or 0)
local del_div = div - (div_prev or 0)
local int_y = dely // cursor_Int
next_Int = ((div + 1) * cursor_Int)
main(div, take)
end
div_prev = div
dy_prev = dy
yprev = ypos
end
end
end
if reaper.GetExtState("CC Script", "Released") == "" then
reaper.defer(OnRelease)
end
end
end
function InnerMain()
local Hwnd_At_Cursor = reaper.JS_Window_FromPoint(reaper.GetMousePosition())
local midiview = reaper.JS_Window_Find('midiview', true)
if Hwnd_At_Cursor ~= midiview then
error("Cursor outside boundary: midiview")
return
else
for k, v in pairs(KeyCodes) do
if mouseMsg[v].OK == false then
reaper.JS_WindowMessage_Intercept(Hwnd_At_Cursor, v, true)
end
end
local time = mouseMsg['0x0201'].time
if mouseMsg['0x0201'].OK and time ~= p_time then
if reaper.GetExtState("CC Script", "Pressed") == "" then
reaper.SetExtState("CC Script", "Pressed", "true", true)
p_time = time
end
OnRelease()
end
if reaper.GetExtState("CC Script", "Released") == "true" then
reaper.SetExtState("CC Script", "Released", "false", true)
return
end
end
reaper.defer(InnerMain)
end
end
InnerMain()