Norns - reassign MIDI device via params?

Hello! I’m having some trouble reassigning a MIDI device via a parameter I’ve set up. Long story short…it doesn’t work. Here’s what I’m working with at the moment:

I define two variables - one for each MIDI device I’m trying to use - and assign them like so:

local m_out = midi.connect()
local m_in = midi.connect()

I’ve declared the parameter for each like so:

params:add{type = "number", 
           id = "midi_in_device",
           name = "midi in device",
           min = 1,
           max = 4,
           default = 2,
           action = function(value) m_in = midi.connect(value) end

params:add{type = "number",
           id = "midi_out_device",
           name = "midi out device",
           min = 1,
           max = 4,
           default = 1,
           action = function(value) m_out = midi.connect(value) end

Now when I try to change which device each variable is assigned to, nothing happens. Norns doesn’t complain about anything, it just doesn’t seem to work.

I did employ some very sophisticated debugging in the params action function via:

print("Hello, the value is: "..value)

…and am definitely seeing the right values in there.

Also, I tried declaring each variable with a device number like this and it works fine:

local m_out = midi.connect(1)
local m_in = midi.connect(2)

…but trying to change the devices just doesn’t do anything. The values change but the devices aren’t being reassigned. :broken_heart:

Oh and I noticed in study 4 there are references to m:disconnect() and m:reconnect(n) but I think those are deprecated? No mention of them in the API doc and Norns was displeased with my attempts to call them.

I feel like I’m missing something really simple here and a new pair of eyes might help. Thanks for your time!

try midi.update_devices() in your param action

1 Like

Hmm tried that (thank you!) but that didn’t seem to change anything. I made that call after calling midi.connect(newvalue)…not sure if that’s how it’s intended to be used?

Not sure if this is the best way, but this is how I handled it:

function init()
    action=function(val) setup_midi(val) end

function setup_midi(input)
  midi_device = midi.connect(input)
  midi_device.event = midi_to_synth

The plot thickens…

I added in the call to midi.cleanup() (thank you @midouest) in the action function for my m_in variable, but I was worried that calling that method would also remove the handler I have set up for the other device assigned to the m_out variable. So I tried changing the device assigned to m_out…and it totally worked fine?

I’m beginning to suspect that I’m too hot and tired to think through this clearly at the moment. :frowning: Taking a break and will report back later.

1 Like

you might take a look at my code for Midi Monitor and see if it tells you anything.

In that - I am doing a bit of a hack to regenerate the vports listing, but I’m not sure if that’s the same problem you’re having.

1 Like

Ok it seems that I can succesfully change the output device, but not the input device. Or rather, if I change the input device, I can’t set it BACK to where it was…I just kind of lose my input device.

Could this be because of the way I’m handling input? Further down the script I’m just doing something like this:

m_in.event = function (data)
               --do some input handling stuff

I see in the example that @midouest posted that .event is being reassigned after cleanup() is called and I’m wondering if that’s where I’m going wrong now. It’s certainly acting that way…

Aha yep, that was the problem! When I changed the device I was losing the event handler. Reassigning it when the parameter changes is what I needed.

Working now. Thank you all so much for your help!