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
|
#ifndef UDO_MIDI
#define UDO_MIDI ##
/*
MIDI control handler
Currently only handling one channel
This file is part of the SONICS UDO collection by Richard Knight 2022
License: GPL-2.0-or-later
http://1bpm.net
*/
gimidi_values = ftgen(0, 0, -128, -2, 0) ; scale 0 to 1 values with index correlating to MIDI CC number
/*
Handle incoming MIDI messages and write to channel with scaling if defined in gSmidimap_channels and gkmidimap_values
*/
instr midi_handler
kstatus, kchan, kdata1, kdata2 midiin
if (kstatus == 176) then ;144 is note on 128 is note off ; 208 is aftertouch
tabw scale(kdata2, 1, 0, 127, 0), kdata1, gimidi_values
#ifndef MIDI_NOTE_HANDLER_INSTRUMENT
endif
#else
elseif (kstatus == 144) then
schedulek("_midi_note_handler", 0, 1, 1, kchan, kdata1, kdata2)
elseif (kstatus == 128) then
schedulek("_midi_note_handler", 0, 1, 0, kchan, kdata1, kdata2)
endif
#end
endin
alwayson("midi_handler")
instr _midi_note_handler
ionoff = p4
ichannel = p5
inote = p6
ivelocity = p7
instrnum = nstrnum("$MIDI_NOTE_HANDLER_INSTRUMENT") + (ichannel / 100) + (inote / 100000)
if (ionoff == 0) then
turnoff2 instrnum, 4, 1
else
schedule(instrnum, 0, -1, ichannel, inote, ivelocity)
endif
turnoff
endin
opcode midi_cc, k, i
icc xin
kval tab icc, gimidi_values
xout kval
endop
opcode midi_zeroall, 0, 0
index = 0
while (index < 128) do
outic 1, index, 0, 0, 1
tabw_i 0, index, gimidi_values
index += 1
od
endop
#end
|