onTimer swap between samplers
-
Hey guys,
I am trying to switch between two notes that trigger simultaneously, depending on how quickly the note-off happens. Here is my logic.Sampler A (plays regular notes as long as the note is held, simple envelope release time set up 1166ms)
On note on: start timer
On note off: stop timer
If timer is less than 50ms, set simple envelope release to 100msSampler B (plays one-shots, has a fade in)
On note on: start timer and set the volume output to -100
On note off: stop timer
If timer is less than 50ms, set the volume output to 100 (full)So, what this should result in (in theory) is that if you quickly press the note on/off, the Sampler A will play the sample's attack portion and quickly fade out, while the Sampler B will crossfade and play the decay of the muted note.
If you press the key and hold the note, then only Sampler A will sound and the sampler B will be ignored.For starters, I am just trying to get and print the timer value when pressing the key, but I am not having much success. Here is what I have in my Midi Processor:
onInit var t = Synth.getTimerInterval(); onNoteOn function onNoteOn() { startTimer; } onNoteOff function onNoteOff() { stopTimer; Console.print("open surdo timer is "+t); }
No matter how I trigger the note, the console print always says 0.
Any help is much appreciated. Thank you!
-
@gorangrooves
You have to create a timer first withEngine.createTimerObject()
Then the calls aremyTimer.startTimer(int ms)
&myTimer.stopTimer()
-
@ustk Thank you. Let me give that a shot :)
-
I wouldn't use a timer for that. You get a precise timing with Engine.getUptime() that you can use in the onNoteOff callback to get the duration of the note:
// This is only monophonic so for a real use case you might want to // use another data storage type. reg start = 0.0; function onNoteOn() { start = Engine.getUptime(); } function onNoteOff() { local durationMs = (Engine.getUptime() - start) * 1000.0; // do something with the duration }
-
@Christoph-Hart Thank you. That's interesting. I'll play around with it and see if I can make it work :)
-
@Christoph-Hart That works well and gives me precise values. Excellent. I'm off to the next steps :) Thank you!
-
@Christoph-Hart I was wondering if you could give me a little nudge here. It seems that I am missing something. Everything compiles ok, but the quick fade-out I am trying to achieve is not happening. It seems to be getting ignored. I get no errors. This is what I have:
onInit reg start = 0.0; const var evtList = []; evtList.reserve(64000); function onNoteOn() { start = Engine.getUptime(); } function onNoteOff() { local durationMs = (Engine.getUptime() - start) * 1000.0; // do something with the duration if (durationMs < 96) { for(eventId in evtList) { Synth.addVolumeFade(eventId, 50, -100); } // Clear all notes evtList.clear(); } Console.print("open surdo timer is "+durationMs); }
-
@gorangrooves Is it possible for durationMs to ever be less than 1000 ?
Console.print(durationMs);
Also evtList isn't being populated. And why are you reserving 64000 elements, that seems like a lot? -
@d-healey Yes, the duration is between 45-95ms when quickly striking the keys.
I guess I could lower the even list reserve since it is getting cleared all the time. I did it a while ago on a hi-hat script fearing that it wouldn't be enough, but now I realized that it gets cleared.Why isn't the event list isn't being populated?
-
Because you didn't push anything in there :)
-
@Christoph-Hart "That's what she said!" Ha!
Let me see if I can stick that in there.
-
Yes, I suck at coding, but I am good at recycling! I reused this nice little bit of your code @Christoph-Hart and made it work :)
onNoteOn start = Engine.getUptime(); Message.makeArtificial(); evtList.push(Message.getEventId());
Thank you kindly, gentlemen!
-
@gorangrooves That's not a good way to do it. You're essentially just adding an endless number of event IDs into the array, the more notes played the less efficient your instrument will become. You should use a MIDI list instead.
-
@d-healey I have no idea how to go about that. Isn't that array getting emptied every time with
evtList.clear();
?
-
@gorangrooves Oh yeah so it is, so you're just wasting memory and CPU cycles with your
evtList.reserve(64000);
:pYou should use a midi list.
I have no idea how to go about that.
Now is a good time to learn. https://docs.hise.audio/scripting/scripting-api/midilist/index.html
-
@d-healey Thanks, Dave. I'll try to make some sense of it and see if I can make it work.