-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
409 lines (364 loc) · 18 KB
/
Copy pathmain.lua
File metadata and controls
409 lines (364 loc) · 18 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
local _, filename = reaper.get_action_context()
local rootFolder = filename:match("(.*[/\\])")
--add root folder to path so that lua can resolve the modules correctly
package.path = package.path .. ";" .. rootFolder .. "?.lua"
--Imported functions and variables
require("utils")
local NOTES2MIDI = require("notesToMidi")
require("notesData")
require("renderer/drawZones")
require("renderer/drawCrossfades")
require("renderer/drawTaps")
require("renderer/drawScratches")
require("renderer/drawScratchZones")
require("renderer/drawEuphoria")
require("renderer/drawEffects")
require("renderer/drawSections")
require("renderer/drawFSCrossfade")
require("renderer/drawBeatIndicators")
require("renderer/drawFSSamples")
require("renderer/drawFSScratches")
require("renderer/drawOther")
--Other globals
local notesTracks = {}
local effectsTracks = {}
local chosenNotesTrackIndex = 0
local chosenEffectsTrackIndex = 0
local freestyleSampleToLane = {}
--number of beats visible
local pixelsPerBeat = 2*UNIT
local lastFrame = reaper.time_precise()
--should not do any drawing, just analysis
local function getNotesInFrame(track, startPPQ, endPPQ)
if track == nil then
return nil
else
local result = {
crossfades = {},
spikes = {},
taps = {},
scratches = {},
scratchZones = {},
euphoria = {},
effects = {},
sections = {},
freestyle = {},
fsCrossfadeMarkers = {},
other = {}
}
local midiTake = reaper.GetMediaItemTake(reaper.GetTrackMediaItem(track, 0), 0)
if reaper.TakeIsMIDI(midiTake) then
local _retval, noteCount, _ccEventCount, textEventCount = reaper.MIDI_CountEvts(midiTake)
local crossfadeHistory = { [1] = nil, [2] = nil, [3] = nil}
for i=0, noteCount - 1 do
local retval, isNoteSelected, isNoteMuted, noteStartPPQ, noteEndPPQ, noteChannel, notePitch, noteVelocity = reaper.MIDI_GetNote(midiTake, i)
--check for crossfades
if notePitch == NOTES2MIDI.CROSS_G then
crossfadeHistory[3] = crossfadeHistory[2]
crossfadeHistory[2] = crossfadeHistory[1]
crossfadeHistory[1] = CrossfadeEvent(noteStartPPQ, noteEndPPQ, CrossfadePos.LEFT)
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.crossfades, crossfadeHistory[1])
end
elseif notePitch == NOTES2MIDI.CROSS_R then
crossfadeHistory[3] = crossfadeHistory[2]
crossfadeHistory[2] = crossfadeHistory[1]
crossfadeHistory[1] = CrossfadeEvent(noteStartPPQ, noteEndPPQ, CrossfadePos.CENTER)
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.crossfades, crossfadeHistory[1])
end
elseif notePitch == NOTES2MIDI.CROSS_B then
crossfadeHistory[3] = crossfadeHistory[2]
crossfadeHistory[2] = crossfadeHistory[1]
crossfadeHistory[1] = CrossfadeEvent(noteStartPPQ, noteEndPPQ, CrossfadePos.RIGHT)
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.crossfades, crossfadeHistory[1])
end
end
--check for spikes
if notePitch == NOTES2MIDI.SPIKE_G then
crossfadeHistory[3] = crossfadeHistory[2]
crossfadeHistory[2] = crossfadeHistory[1]
--by default outwards spikes have crossfade center as base position
crossfadeHistory[1] = CFSpikeEvent(noteStartPPQ, noteEndPPQ, CrossfadePos.CENTER, CrossfadePos.LEFT)
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.spikes, crossfadeHistory[1])
end
elseif notePitch == NOTES2MIDI.SPIKE_R then
crossfadeHistory[3] = crossfadeHistory[2]
crossfadeHistory[2] = crossfadeHistory[1]
crossfadeHistory[1] = CFSpikeEvent(noteStartPPQ, noteEndPPQ, crossfadeHistory[2].position, CrossfadePos.CENTER)
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.spikes, crossfadeHistory[1])
end
elseif notePitch == NOTES2MIDI.SPIKE_B then
crossfadeHistory[3] = crossfadeHistory[2]
crossfadeHistory[2] = crossfadeHistory[1]
--by default outwards spikes have crossfade center as base position
crossfadeHistory[1] = CFSpikeEvent(noteStartPPQ, noteEndPPQ, CrossfadePos.CENTER, CrossfadePos.RIGHT)
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.spikes, crossfadeHistory[1])
end
end
--adjust spikes if needed
if crossfadeHistory[1] ~= nil and crossfadeHistory[2] ~= nil and crossfadeHistory[3] ~= nil then
--if we have a situation like CROSS_G, SPIKE_B, CROSS_G then the middle spike has to be adjusted in order to have base position CROSS_G
if crossfadeHistory[2].type == EventType.SPIKE and crossfadeHistory[1].position == crossfadeHistory[3].position then
crossfadeHistory[2].position = crossfadeHistory[1].position
end
end
--check for taps
if notePitch == NOTES2MIDI.TAP_G then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.taps, TapEvent(noteStartPPQ, noteEndPPQ, Lane.GREEN))
end
elseif notePitch == NOTES2MIDI.TAP_R then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.taps, TapEvent(noteStartPPQ, noteEndPPQ, Lane.RED))
end
elseif notePitch == NOTES2MIDI.TAP_B then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.taps, TapEvent(noteStartPPQ, noteEndPPQ, Lane.BLUE))
end
end
--check for scratches
if notePitch == NOTES2MIDI.SCRATCH_G_UP then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.scratches, ScratchEvent(noteStartPPQ, noteEndPPQ, Lane.GREEN, ScratchDir.UP))
end
elseif notePitch == NOTES2MIDI.SCRATCH_G_DOWN then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.scratches, ScratchEvent(noteStartPPQ, noteEndPPQ, Lane.GREEN, ScratchDir.DOWN))
end
elseif notePitch == NOTES2MIDI.SCRATCH_G_ANY then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.scratches, ScratchEvent(noteStartPPQ, noteEndPPQ, Lane.GREEN, ScratchDir.ANYDIR))
end
elseif notePitch == NOTES2MIDI.SCRATCH_B_UP then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.scratches, ScratchEvent(noteStartPPQ, noteEndPPQ, Lane.BLUE, ScratchDir.UP))
end
elseif notePitch == NOTES2MIDI.SCRATCH_B_DOWN then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.scratches, ScratchEvent(noteStartPPQ, noteEndPPQ, Lane.BLUE, ScratchDir.DOWN))
end
elseif notePitch == NOTES2MIDI.SCRATCH_B_ANY then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.scratches, ScratchEvent(noteStartPPQ, noteEndPPQ, Lane.BLUE, ScratchDir.ANYDIR))
end
end
--check for scratch zones
if notePitch == NOTES2MIDI.SCRATCH_G_ZONE then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.scratchZones, ScratchZoneEvent(noteStartPPQ, noteEndPPQ, Lane.GREEN))
end
elseif notePitch == NOTES2MIDI.SCRATCH_B_ZONE then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.scratchZones, ScratchZoneEvent(noteStartPPQ, noteEndPPQ, Lane.BLUE))
end
end
--check for euphoria
if notePitch == NOTES2MIDI.EUPHORIA then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.euphoria, EuphoriaEvent(noteStartPPQ, noteEndPPQ))
end
end
--check for effects
if notePitch == NOTES2MIDI.EFFECTS_G then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.effects, EffectEvent(noteStartPPQ, noteEndPPQ, EffectMask.GREEN))
end
elseif notePitch == NOTES2MIDI.EFFECTS_R then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.effects, EffectEvent(noteStartPPQ, noteEndPPQ, EffectMask.RED))
end
elseif notePitch == NOTES2MIDI.EFFECTS_B then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.effects, EffectEvent(noteStartPPQ, noteEndPPQ, EffectMask.BLUE))
end
elseif notePitch == NOTES2MIDI.EFFECTS_ALL then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.effects, EffectEvent(noteStartPPQ, noteEndPPQ, EffectMask.ALL))
end
end
--check for freestyle crossfade
if notePitch == NOTES2MIDI.FS_CROSS_ZONE then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.freestyle, FSCrossfadeEvent(noteStartPPQ, noteEndPPQ))
end
elseif notePitch == NOTES2MIDI.FS_SAMPLES_SCRATCHES then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
local lane = freestyleSampleToLane[noteVelocity]
if lane == Lane.GREEN then
table.insert(result.freestyle, FSScratchEvent(noteStartPPQ, noteEndPPQ, Lane.GREEN))
elseif lane == Lane.BLUE then
table.insert(result.freestyle, FSScratchEvent(noteStartPPQ, noteEndPPQ, Lane.BLUE))
else
table.insert(result.freestyle, FSSampleEvent(noteStartPPQ, noteEndPPQ))
end
end
end
--check for freestyle crossfade markers
if notePitch == NOTES2MIDI.FS_CROSS_G_MARKER then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.fsCrossfadeMarkers, FSCrossMarkerEvent(noteStartPPQ, noteEndPPQ, Lane.GREEN))
end
elseif notePitch == NOTES2MIDI.FS_CROSS_B_MARKER then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.fsCrossfadeMarkers, FSCrossMarkerEvent(noteStartPPQ, noteEndPPQ, Lane.BLUE))
end
end
--check for other notes
if notePitch == NOTES2MIDI.MEGAMIX_TRANSITION then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.other, MegamixTransitionEvent(noteStartPPQ))
end
elseif notePitch == NOTES2MIDI.BATTLE_CHUNKREMIX then
if isVisible(noteStartPPQ, noteEndPPQ, startPPQ, endPPQ) then
table.insert(result.other, BattleChunkRemixEvent(noteStartPPQ))
end
end
end
--get sections
for i=1, textEventCount do
local _retval, _selected, _muted, ppqpos, type, msg = reaper.MIDI_GetTextSysexEvt(midiTake, i)
if type == 1 then
if isVisible(ppqpos,ppqpos, startPPQ, endPPQ) then
table.insert(result.sections, SectionEvent(ppqpos, msg))
end
end
end
end
return result
end
end
--[CrossfadeEvent], [CFSpikeEvent] -> sorted [CrossfadeEvent | CFSpikeEvent]
local function mergeCrossfadeEvents(crossfades, spikes)
local result = {}
for _, crossfade in ipairs(crossfades) do
table.insert(result, crossfade)
end
for _, spike in ipairs(spikes) do
table.insert(result, spike)
end
table.sort(result, PPQComparator)
return result
end
local function handleKey(key)
if key == 0 then
return
end
local FACTOR = 1.1
if key == 45 then -- Minus key
pixelsPerBeat = pixelsPerBeat / FACTOR
elseif key == 61 then -- Equals key
pixelsPerBeat = pixelsPerBeat * FACTOR
elseif key == 1919379572 then -- Right Arrow Key
if chosenNotesTrackIndex + 1 <= #notesTracks then
chosenNotesTrackIndex = chosenNotesTrackIndex + 1
end
elseif key == 1818584692 then -- Left Arrow Key
if chosenNotesTrackIndex - 1 >= 1 then
chosenNotesTrackIndex = chosenNotesTrackIndex - 1
end
else
--reaper.ShowMessageBox(tostring(key),"AAA",0)
end
end
-- This function has to be without arguments because it gets called by reaper itself
-- so the tracks has to be global vars
local function update()
startGlog()
notes = notesTracks[chosenNotesTrackIndex]
local thisFrame = reaper.time_precise()
if notes == nil then
glog("ERROR: Notes track not found")
else
updateOrigin(gfx.w, gfx.h)
local startPPQ, endPPQ, PPQresolution = getPPQTimes(notes,pixelsPerBeat, ORIGIN_Y)
local deltaTime = thisFrame - lastFrame
glog(string.format("%f FPS", 1 / deltaTime))
local measure, beat = PPQToMeasureBeats(startPPQ, PPQresolution)
glog(string.format("Time: %d.%d (%s)", measure, beat, startPPQ))
local _, trackName = reaper.GetTrackName(notes)
glog(string.format("Track %d of %d:%s", chosenNotesTrackIndex, #notesTracks, trackName))
local notesInFrame = getNotesInFrame(notes, startPPQ, endPPQ)
if notesInFrame == nil then
glog("ERROR: Could not find compatible midi take")
else
local mergedCross = mergeCrossfadeEvents(notesInFrame.crossfades, notesInFrame.spikes)
--draw stuff
drawBeatIndicators(startPPQ, endPPQ, PPQresolution)
drawEuphoriaZones(startPPQ, endPPQ, notesInFrame.euphoria)
drawEffectsZones(startPPQ, endPPQ, notesInFrame.effects)
drawZones(startPPQ, mergedCross)
drawFSCrossfades(startPPQ, endPPQ, notesInFrame.freestyle)
drawCrossfades(startPPQ, endPPQ, mergedCross, notesInFrame.freestyle)
drawScratchZones(startPPQ, endPPQ, notesInFrame.scratchZones, mergedCross)
drawTaps(startPPQ, endPPQ, PPQresolution, notesInFrame.taps, mergedCross)
drawScratches(startPPQ, endPPQ, PPQresolution, notesInFrame.scratches, mergedCross)
drawEffectsHandle(startPPQ, endPPQ, notesInFrame.effects)
drawFSCrossfadeMarkers(startPPQ, endPPQ, notesInFrame.fsCrossfadeMarkers)
drawFSSamples(startPPQ, endPPQ, notesInFrame.freestyle)
drawFSScratches(startPPQ, endPPQ, notesInFrame.freestyle, mergedCross)
drawOther(startPPQ, endPPQ, notesInFrame.other)
drawSections(startPPQ, endPPQ, notesInFrame.sections)
end
end
gfx.update()
lastFrame = thisFrame
-- gfx.getchar() returns -1 if the window is closed
local key = gfx.getchar()
if key ~= -1 then
handleKey(key)
--think of this as JS's RequestAnimationFrame
reaper.defer(update)
end
end
local function parseSampleMap(file)
local result = {}
local line = file:read("*line")
while line ~= nil do
startI, endI, data = string.find(line,"green%s*=([%s%d]*)")
if startI ~= nil then
--we have a match
for vel in string.gmatch(data, "%d*") do
if #vel > 0 then
result[tonumber(vel)] = Lane.GREEN
end
end
end
startI, endI, data = string.find(line,"blue%s*=([%s%d]*)")
if startI ~= nil then
--we have a match
for vel in string.gmatch(data, "%d*") do
if #vel > 0 then
result[tonumber(vel)] = Lane.BLUE
end
end
end
line = file:read("*line")
end
return result
end
local function main()
local WIDTH = 800
local HEIGHT = 600
gfx.init("DJH-Chart-Visualizer v20260121", WIDTH, HEIGHT)
gfx.setfont(1, "Arial", 20)
local sampleMap = reaper.GetProjectPath().."/sampleMap.txt"
local file = io.open(sampleMap, "r")
if file ~= nil then
freestyleSampleToLane = parseSampleMap(file)
end
--[[
for k, v in pairs(freestyleSampleToLane) do
reaper.ShowMessageBox(string.format("%s -> %d", k, v), "", 0)
end
]]
notesTracks, effectsTracks = getDJHTracks()
chosenNotesTrackIndex = 1
chosenEffectsTrackIndex = 1
update()
end
main()