Eon Posted February 14 Share Posted February 14 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! Quote Link to comment Share on other sites More sharing options...
wonshu Posted February 14 Share Posted February 14 (edited) 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 February 14 by wonshu 1 Quote Link to comment Share on other sites More sharing options...
Dewdman42 Posted February 14 Share Posted February 14 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(); } } 1 Quote Link to comment Share on other sites More sharing options...
Eon Posted February 14 Author Share Posted February 14 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. Quote Link to comment Share on other sites More sharing options...
Eon Posted February 14 Author Share Posted February 14 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? Quote Link to comment Share on other sites More sharing options...
Solution Dewdman42 Posted February 14 Solution Share Posted February 14 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(); } 1 Quote Link to comment Share on other sites More sharing options...
Eon Posted February 14 Author Share Posted February 14 Thank you! Quote Link to comment Share on other sites More sharing options...
wonshu Posted February 15 Share Posted February 15 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? Quote Link to comment Share on other sites More sharing options...
David Nahmani Posted February 15 Share Posted February 15 53 minutes ago, wonshu said: Would you mind explaining? In the original code, event.send() was inside the if statement, so that only Note on events were being sent. Quote Link to comment Share on other sites More sharing options...
wonshu Posted February 15 Share Posted February 15 In my code, yes, but the two versions of @Dewdman42 are identical, no? Quote Link to comment Share on other sites More sharing options...
David Nahmani Posted February 15 Share Posted February 15 3 minutes ago, wonshu said: In my code, yes, but the two versions of @Dewdman42 are identical, no? No, I was comparing the two versions of @Dewdman42. Quote Link to comment Share on other sites More sharing options...
wonshu Posted February 15 Share Posted February 15 Ah... my god... that is what I hate about coding... sometimes you just can't see it... even though it's right there. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.