Jump to content

Script to randomize velocity in a specific range from the current input value


Eon
Go to solution Solved by Dewdman42,

Recommended Posts

Hi, how hard is to create something like this?

In simple words i need that let's say my input velocity is 30 and i want it to be randomized in a range from 20 to 40. The built-in randomizer doesn't support relative values as i can see.

Thanks!

Link to comment
Share on other sites

Haven't tested this like crazy, but on first glance, this should do it: 


function HandleMIDI(event)
{
	event.velocity = Math.round(Math.random() * (40 - 20) + 20);
	event.trace();
	event.send();
	
}

PS: Ah: it doesn't set the note off to 0 ( which it should be ) - don't know how to do that... someone will chime in...

Edited by wonshu
  • Like 1
Link to comment
Share on other sites

That assumes a known input velocity.  If you want it to be relative to whatever is coming in, then something like this:

function HandleMIDI(event) {
  if(event instanceof NoteOn) {
    // random value between 0-20 as int
    var offset = Math.floor(Math.random() * 20);
    event.velocity = event.velocity + (offset - 10);
    // should also make sure its valid velocity
    if(event.velocity > 127) event.velocity = 127;
    if(event.velocity < 1) event.velocity = 1;
    event.send();
  }
}
  • Love 1
Link to comment
Share on other sites

13 hours ago, Dewdman42 said:

That assumes a known input velocity.  If you want it to be relative to whatever is coming in, then something like this:

function HandleMIDI(event) {
  if(event instanceof NoteOn) {
    // random value between 0-20 as int
    var offset = Math.floor(Math.random() * 20);
    event.velocity = event.velocity + (offset - 10);
    // should also make sure its valid velocity
    if(event.velocity > 127) event.velocity = 127;
    if(event.velocity < 1) event.velocity = 1;
    event.send();
  }
}

Wow! That was fast! Thank you. Exactly what i need.

Link to comment
Share on other sites

13 hours ago, Dewdman42 said:

That assumes a known input velocity.  If you want it to be relative to whatever is coming in, then something like this:

function HandleMIDI(event) {
  if(event instanceof NoteOn) {
    // random value between 0-20 as int
    var offset = Math.floor(Math.random() * 20);
    event.velocity = event.velocity + (offset - 10);
    // should also make sure its valid velocity
    if(event.velocity > 127) event.velocity = 127;
    if(event.velocity < 1) event.velocity = 1;
    event.send();
  }
}

But somehow its make a note longer, like a sustain pedal...why?

Link to comment
Share on other sites

  • Solution

sorry, dumb me...here is correction

function HandleMIDI(event) {
    if(event instanceof NoteOn) {
        // random value between 0-20 as int
        var offset = Math.floor(Math.random() * 20);
        event.velocity = event.velocity + (offset - 10);
        // should also make sure its valid velocity
        if(event.velocity > 127) event.velocity = 127;
        if(event.velocity < 1) event.velocity = 1;
    }
    event.send();
}
  • Like 1
Link to comment
Share on other sites

13 hours ago, Dewdman42 said:

sorry, dumb me...here is correction

function HandleMIDI(event) {
    if(event instanceof NoteOn) {
        // random value between 0-20 as int
        var offset = Math.floor(Math.random() * 20);
        event.velocity = event.velocity + (offset - 10);
        // should also make sure its valid velocity
        if(event.velocity > 127) event.velocity = 127;
        if(event.velocity < 1) event.velocity = 1;
    }
    event.send();
}

As far as I can see that's exactly the same code as before.

Would you mind explaining?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...