How to code some intricate legato logic... use onTimer() ?
-
@tonewolf said in How to code some intricate legato logic... use onTimer() ?:
I can’t work out how to say “50ms has now passed and another noteOn event has not been triggered, so you can now go ahead and trigger sample B”.
I just wrote a script that does almost exactly this.
In the on note on callback you record the current uptime and start the timer with an interval of 10ms.
In the timer callback you check how long has passed since the last on note on by comparing the current uptime to the one you stored in on note on.
Based on the time elapsed you carry out the action you want. In my case I'm just killing the note but in yours you would play a note.
The release samples should be in a separate sampler controlled by a separate script.
-
@d-healey great, so you understand what I’m trying to achieve… surprisingly tricky to actually put into plain English!
I suppose I was worried about having loads of timers pinging off. This is a piano library, so I’d need to set a timer on every single noteOff event, and it would need to be polyphonic! Should I be worried? What’s the most CPU friendly way of doing this?
-
@tonewolf said in How to code some intricate legato logic... use onTimer() ?:
This is a piano library, so I’d need to set a timer on every single noteOff event, and it would need to be polyphonic! Should I be worried?
Why not just have one timer and store the time elapsed for each note?
-
@d-healey in my case, I’m looking to analyse the amount of time between the last note off and the new note on. But let me see if I’ve got this correct…
I would set a timer (let’s say, 10ms intervals).
I log the time of the note off event. I log the time of the new note on event. Timer callback checks if logged new note on time minus logged note off time is less than 50ms. If yes, trigger Sample A, if no, trigger Sample B.
-
@tonewolf Yes that's basically it. Start simple with just two notes before trying to make this work with many.
Use a MIDIList to store your times, it's more efficient than an array.
This whole thing should be in a separate MIDI processor to the UI script.
You must use the Synth timer (not a timer object). You only get 1 synth timer per script and (I think) 6 per container, so you should try hard to make it work with a single timer - it can be done.
-
@d-healey okay, this makes sense. I thought I read something about the timer being limited to 40ms intervals, though? Not sure if this would have the resolution I need.
That aside, I have a follow up question regarding MidiList…
I’m logging quite a bit of information per note on event, and currently must admit to having them in an array, where each array index has an object. So I might set and get Middle C meta data at noteLog[60]["noteLength"] or noteLog[60]["noteOffTime"] etc.
Is there significant advantages to separating those all out into discrete MidiLists?
const var noteOffTime = Engine.newMidiList();
const var noteOnTime = Engine.newMidiList();
etc.
etc. -
@tonewolf said in How to code some intricate legato logic... use onTimer() ?:
I thought I read something about the timer being limited to 40ms intervals, though
Looks like 4ms to me - https://github.com/christophhart/HISE/blob/develop/hi_core/hi_dsp/modules/ModulatorSynth.cpp#L410
-
@tonewolf said in How to code some intricate legato logic... use onTimer() ?:
Is there significant advantages to separating those all out into discrete MidiLists?
Yes, do this
-
@d-healey ah, great. So is this something different from…
https://docs.hise.dev/hise-modules/midi-processors/list/scriptprocessor.html#the-ontimer-callback
Which says it is limited to 40ms…?
-
@tonewolf said in How to code some intricate legato logic... use onTimer() ?:
Which says it is limited to 40ms…?
Well the code says otherwise, unless my maths is wrong and 0.004 seconds is not 4ms
-
@d-healey great! Just wanted to make sure we were talking about the same Timer.
Wonderful. That’s been super helpful, thanks! I’ll see what I can come up with using this method.
-
Perhaps missing something obvious, but how can I use MidiList to store times, when it only seems to be able to store integer values between -128 and 128?
-
@tonewolf said in How to code some intricate legato logic... use onTimer() ?:
store integer values between -128 and 128?
hmmm, integer yes, but you can store any value (well I'm sure there'll be an upper limit).
So yeah you'll probably need to use arrays for the time for the precision you need.
-
@d-healey I think I must have misunderstood the online docs (or they are out of date), as it appears to state the values must be between -128 and 128. But testing, it seems I can have 9 figure integers. Perhaps it means there are indexes between -128 and 128?
-
@tonewolf Not sure which doc but the indexes go from 0 to 127, so you get one value for each MIDI note - hence the name, MIDI list.
-
@d-healey this is the doc…
https://docs.hise.audio/scripting/scripting-api/midilist/index.html#setvalue
But in reality, I think it can store a 32bit integer
-
@tonewolf Ah yeah, that's confusing and wrong :)