Unexpected Midi port closing


I am developing a tool to midi map the Launch Control XL (I know it exist Duplex, but my aim is to learn how to do it).
I 've done the complicate thing, but I don’t know why, after creating the midi_callback with : renoise.Midi.create_input_device(device_name, IncomingMidi) I can receive incoming midi but only for a couple of second, then the device close itself (I see it on the Renoise message bar at the bottom of the main window), even no “midi_device_in:close()” instruction is present in my code.
I don’t know if it is the ‘garbage collector’ of lua or Renoise itself, but it my “midi_device_in” object is closed by something… Any Idea to figure out why (the hell) ?

Note that I am new with lua, but not new as a hobbist programmer.

My code:

local function IncomingMidi(message)
assert(#message == 3)
assert(message[1] >= 0 and message[1] <= 0xff)
assert(message[2] >= 0 and message[2] <= 0xff)
assert(message[3] >= 0 and message[3] <= 0xff)

if math.floor(message[1]/16) == 0x09 then
print(“Note - Y:”,IncomingNote(message[2])[1]," - X:", IncomingNote(message[2])[2])
midi_device_out:send {message[1], message[2], red_full}

if math.floor(message[1]/16) == 0x08 then
midi_device_out:send {message[1], message[2], off}

if math.floor(message[1]/16) == 0x0B then
IncomingPot(message[2], message[3])[1],
IncomingPot(message[2], message[3])[2])

  if IncomingPot(message[2], message[3])[1] ~= 0 then
    SetValueToParameter(1, 1, IncomingPot(message[2], message[3]))



local midi_device_out = renoise.Midi.create_output_device(device_name)
local midi_device_in = renoise.Midi.create_input_device(device_name, IncomingMidi)

Yes, that will be the problem. As soon as the “midi_device_in” object is garbage collected by Lua it will also close its device.

In this simple example you can avoid this by making the object global (remove the local keyword).

1 Like

Ok. :sweat_smile:
I was trying to deactivate the garbage collector to be sure…

But yeah, I am an old school programmer: a variable exist or not (is accessible in some ‘part’ or not). This garbage thing is like a ‘2’ on a binary system…

One day perhaps, I will offer you a beer.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.