-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmidi.lua
70 lines (63 loc) · 1.41 KB
/
midi.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
local midi_out = {}
midi_out.devices = {}
midi_out.device = 1
midi_out.channel = 1
midi_out.active_notes = {}
for d = 1, 4 do
local channels = {}
for c = 1, 16 do
notes = {}
for n = 0, 127 do
notes[n] = 0
end
channels[c] = notes
end
midi_out.active_notes[d] = channels
end
function midi_out:trigger(shape, vel)
local device = self.device or shape.midi_device
local channel = self.channel or shape.midi_channel
local note = shape.midi_note
local length = self.trigger_length
local active_notes = self.active_notes[device][channel]
midi.vports[device]:note_on(note, vel, channel)
active_notes[note] = active_notes[note] + 1
clock.run(function()
clock.sleep(length)
active_notes[note] = active_notes[note] - 1
-- if active_notes[note] < 1 then
midi.vports[device]:note_off(note, 0, channel)
-- end
end)
end
function midi_out:connect()
for d = 1, 4 do
self.devices[d] = midi.connect(d)
end
end
-- clear all notes on all devices, right now
function midi_out:clear_sync()
for d = 1, 4 do
for c = 1, 16 do
for n = 0, 127 do
midi_out.devices[d]:note_off(n, 0, c)
end
end
end
end
-- clear all notes on all devices, with throttling
function midi_out:clear_async()
clock.run(function()
for d = 1, 4 do
for c = 1, 16 do
for n = 0, 127 do
midi_out.devices[d]:note_off(n, 0, c)
if n % 64 == 0 then
clock.sleep(0.01)
end
end
end
end
end)
end
return midi_out