1

First time posting here so sorry if the question is too vague or missing info. Anyway:

I'm trying to send OSC (open sound control) or MQTT messages when I hit specific notes on my midi controller. When I try to send a OSC message when I get a midi input, I get this unpredictable high latency, varying from about 200-300 milliseconds up to 10 seconds. Here is my test-code:

var easymidi = require('easymidi');
var osc = require("osc");


easymidi.getInputs().forEach((midiDevice)=> {

    if (midiDevice.includes('Maschine')){
        inputMidi = midiDevice
        console.log(`Midi device input found: ${inputMidi}`)
    }

})


var inputMikro = new easymidi.Input(inputMidi)


var rmeOSC = new osc.UDPPort ({

    remoteAddress: "192.168.10.148",
    remotePort: 9001

});

rmeOSC.open();

inputMikro.on('cc', function (msg) {

    if (msg.controller == 7){
        
        // This is instant
        console.log(msg)

        // This is what becomes unpredictable and slow
        rmeOSC.send({
            address: "/1/mastervolume",
            args: [
                {
                    type: "f",
                    value: msg.value / 127
                }
            ]
        });
        
    }
}

I've tried to zero in what makes it slow. When just logging to console inside inputMikro.on('cc', ... ) event listener, it's instant. If I send that midi data to another midi device in that same event listener (also using easymidi library), it's also instant. But sending OSC or MQTT messages there creates that unpredictable latency. I've also tried setting an interval every 20 to send OSC just to make sure it's not a limitation of the OSC library in itself:

var vol = 0

var sendOsc = (()=>{

    vol++

    if (vol == 127){
        vol = 0
    }

    rmeOSC.send({
        address: "/1/mastervolume",
        args: [
            {
                type: "f",
                value: vol / 127
            }
        ]
    });
})

setInterval(sendOsc, 20)

That works great, same with MQTT. I also tried creating a separate volume variable with an event listener that listens for changes in that variable, and update that variable inside the inputMikro.on('cc', ... ) listener. Same there, if i just log to console whenever that variable changes, it's instant, but if I try to send OSC or MQTT messages when the variable changes, I get that latency.

I'm out of ideas and have no idea what's going on. I'd very much appreciate any insight on how I can fix this. I hope my question is clear enough.

Or Assayag
  • 5,662
  • 13
  • 57
  • 93
  • there can be a bunch of factors to this - first thing to eliminate is network issues, is it behaving this way when sending to `localhost` ? If it isn't you can try sniffing the packets on the other end and profiling them with hexler's [protokol](https://hexler.net/products/protokol) or better yet with [wireshark](https://www.wireshark.org/). If you're spamming OSC messages you're also likely to flood the receiver - even 20ms can clog sometimes. what's on the other end? – lys Dec 13 '20 at 14:14
  • 1
    This is all local on the same computer. Never heard of hexlers protokol though, have to check that out! Anyway since I'm able to send osc from my script with low consistent latency as long as it's sent from a "setInterval(sendOsc, 20)" it seems to me that the problem should be the interaction between the midi library and it's eventlistener and the osc library somehow – Stefan Stistrup Dec 17 '20 at 08:57

0 Answers0