MIDI player laggy
-
I'm trying to trigger some MIDI players on noteOn, but the behaviour is a bit strange.
The MIDI player starts immediately, but there's a delay until I hear the sounds.
Sometimes it also triggers the wrong player, and sometimes thestop
doesn't work properly.Is there anything I'm doing wrong?
Synth.deferCallbacks(false); const mp0 = Synth.getMidiPlayer("mp0"); const mp1 = Synth.getMidiPlayer("mp1"); const players = [mp0, mp1]; const noteTriggers = [60, 62]; const triggers = []; for (var i=0;i<players.length;i++) { triggers[noteTriggers[i]] = players[i]; } function onNoteOn() { const note = Message.getNoteNumber(); if (!!triggers[note]) { Message.ignoreEvent(true); triggers[note].play(0); } } function onNoteOff() { if (!!triggers[note]) { Message.ignoreEvent(true); triggers[note].stop(0); } }
-
@daniloprates said in MIDI player laggy:
for (var i=0;i<players.length;i++) {
No need for the
var
@daniloprates said in MIDI player laggy:
const note = Message.getNoteNumber();
Use a
local
here.@daniloprates said in MIDI player laggy:
!!triggers[note]
Doesn't the first
!
cancel out the second? -
@d-healey said in MIDI player laggy:
note = Message.getNoteNumber();
!!
returns atrue
instead of the value. Just a more secure way of checking if something exists.I've just realized that there's a latency even when pressing play on the MIDI player directly, so it's not related to the code. The player starts immediately, but the sound takes a while to start.
-
@daniloprates said in MIDI player laggy:
!! returns a true instead of the value. Just a more secure way of checking if something exists.
isDefined()
is probably a better way, because!!
might return the wrong value for invalid values. -
@d-healey thanks for the heads up!
-
@d-healey I think
isDefined()
is for different cases. Similar in many ways but different enough to throw you off at times.The double quote
!!
turns any truthy/falsey value into a boolean. ButisDefined()
will consider a value offalse
as defined, and therefore return true. -
@dannytaurus said in MIDI player laggy:
But isDefined() will consider a value of false as defined,
Yeah that's what I meant by he might get invalid values, because he wants to check "if something exists" not if it's true or false - or at least that's what I got from the statement.
-
@d-healey said in MIDI player laggy:
Yeah that's what I meant by he might get invalid values, because he wants to check "if something exists" not if it's true or false - or at least that's what I got from the statement.
Yeah, true (no pun intended). To check if something is defined I'd always use
isDefined()
because it's more readable and explicit.But I do use
!!
a lot in other places because it's so consistent in what it returns.And
!!
does returnfalse
forundefined
, for reference.Console.print(isDefined(somethingUndefined)); // 0 Console.print(!!somethingUndefined); // 0