Does anyone have any ideas about setting up a callback for physically adding/removing a given MIDI device (as in, “when a MIDI device is added, do this, and when it’s removed, do that”)? My initial thought was to set up a task that would check the MIDI device list every so often, and if it changed, respond accordingly, like so:
scLang
Task({
var delta;
loop {
delta = 0.1;
~listA = MIDIClient.list;
delta.yield;
~listB = MIDIClient.list;
(~listA != ~listB).if(
128.do{
arg i;
if(
MIDIClient.destinations.asString.contains("Midi Fighter Twister"),
{
AppClock.sched(0.5,{~comp[0].children[i].visible_(false)});
},
{
AppClock.sched(0.5,{~comp[0].children[i].visible_(true)})
}
)
}
{};
delta.yield;
)
}},
AppClock
).start;
This kind of works! The GUI objects appear when the device is unplugged and disappear when it’s plugged. When I tried to get any more complicated than “make these GUI objects appear/disappear,” though, stuff got messy. For one thing, I wanted the values of the GUI objects (knobs) to translate over to the MIDI device (MFT), so I added a line to send those values over when the device was added, like so:
more scLang
Task({
var delta;
loop {
delta = 0.1;
~listA = MIDIClient.list;
delta.yield;
~listB = MIDIClient.list;
(~listA != ~listB).if(
128.do{
arg i;
if(
MIDIClient.destinations.asString.contains("Midi Fighter Twister"),
{
AppClock.sched(0.5,{~comp[0].children[i].visible_(false)});
if(
i<64,
{
~mft.control(0,i,~comp[0].children[i].value)
},
{
~mft.control(4,i,~comp[0].children[i].value)
}
)
},
{
AppClock.sched(0.5,{~comp[0].children[i].visible_(true)})
}
)
}
{};
delta.yield;
)
}},
AppClock
).start;
But I found this was updating the values on the MFT constantly, such that if I changed them (i.e., by physically turning one of the encoders), they would immediately jump back to whatever value the GUI knob had left for them! I don’t really get why this was happening, since it’s only supposed to send those values if the MIDI device list changes, but for some reason, it was happening. This makes me wonder if there’s a better way to do this than a loop. I’m not aware of a MIDI device add/remove callback that already exists in scLang, but if there is one, of if anyone can think of a better way to build one, please let me know!