Jump to content

Script to "compress" velocity? [SOLVED]


Recommended Posts

UPDATE: The final code I'm using that's working is here: viewtopic.php?f=45&t=162689#p853869

 

As a newbie when it comes to Javascript, would it be easy to create a script that reduces the range of the velocity?

So instead of having the velocity going from 1 to 127, it would go from 1 to 100, for example.

Any tips on what to look for to see if I can do it myself? Or is this not something that a beginner would be able to accomplish?

Link to comment
Share on other sites

I think I was able to do it. Can someone check if everything's good or if there's another way to approach it?

Here's the code and the preset itself for test:

// Compression only on C1

function HandleMIDI(event) {

if (event instanceof Note && event.pitch == 36) {
	var originalVelocity = event.velocity;
	var percentage = GetParameter (0)*100/originalVelocity;
	event.velocity = event.velocity - (event.velocity*(percentage/100));
}
event.trace();
event.send();
 
}


var PluginParameters = [
{
name:"Velocity Compression",
type:"lin",
minValue:0,
maxValue:127,
numberOfSteps:127,
defaultValue:0,
}
]

 

Velocity Compression.pst

Link to comment
Share on other sites

For context, this is my eDrum rig in Logic. As always, I'd approach it differently, for several reasons:

 

- I like to change things into the shape I need before I record them which is not impossible but difficult with Scripter

- I need several, drastically different setups during a show, including silent metronome and correct lead sheet pages in a PDF reader

- Scripter is fun, but its interface is pretty much not customizable and very wasteful on screen resources. This makes it ok for set-and-forget-processors, but very impractical if you need access to or even just visual feedback from its controls on a single (small) screen setup (i.e. Macbook)

 

So the Environment (plus a healthy amount of assisting Scripters) it is for me.

 

edrum.gif.50b6f3ef0471396baf915fc50b2f87f3.gif

- received drum notes light the switches, so I see what I'm (not) hitting

- I can see what I'm about to hit before I embarrass myself with the 28" tamtam when I thought it was a 6" splash.

- everything is stored in memory patches, so I can step through a set list with hitting a pad or a footswitch

 

Cut to the chase - every drum note has a min and max velocity number (black squares). These define the target range the incoming range of 1-127 will get compressed to:

 

edrum2.gif.60ebe286728d21dcd69412e42c10fabb.gif

This way I can compress towards the quiet range, towards the loud range or limit everything to a single, specific value.

 

So the calculation I would suggest is:

 

vcompressed = voriginal * (max - min) / 127 + min

 

You can implement this in javascript just as well. I have added some safety rails to ensure max is never less than min, but that's just for convenience.

Link to comment
Share on other sites

For context, this is my eDrum rig in Logic. As always, I'd approach it differently, for several reasons:

 

- I like to change things into the shape I need before I record them which is not impossible but difficult with Scripter

- I need several, drastically different setups during a show, including silent metronome and correct lead sheet pages in a PDF reader

- Scripter is fun, but its interface is pretty much not customizable and very wasteful on screen resources. This makes it ok for set-and-forget-processors, but very impractical if you need access to or even just visual feedback from its controls on a single (small) screen setup (i.e. Macbook)

 

So the Environment (plus a healthy amount of assisting Scripters) it is for me.

 

edrum.gif

- received drum notes light the switches, so I see what I'm (not) hitting

- I can see what I'm about to hit before I embarrass myself with the 28" tamtam when I thought it was a 6" splash.

- everything is stored in memory patches, so I can step through a set list with hitting a pad or a footswitch

 

Cut to the chase - every drum note has a min and max velocity number (black squares). These define the target range the incoming range of 1-127 will get compressed to:

 

edrum2.gif

This way I can compress towards the quiet range, towards the loud range or limit everything to a single, specific value.

 

So the calculation I would suggest is:

 

vcompressed = voriginal * (max - min) / 127 + min

 

You can implement this in javascript just as well. I have added some safety rails to ensure max is never less than min, but that's just for convenience.

 

 

Damn that's some advanced stuff you have right there. I don't think I would be able to even understand what's happening under the hood :P

It looks very useful though!

 

I'm using the Scripter, just because I'm learning a little bit more about it and also because this is just to control the velocity range of the sidestick in general when I use the DKD. Nothing too fancy and this would be saved with the patch itself.

 

So the vcompressed = voriginal * (max - min) / 127 + min would work on my script? If so, how would I implement that in the already finished script?

 

 

Also, how do you create that last "Feature" where you have the min and max velocity and are able to change it by dragging the value up and down?

The Environment is a mystery to me as well as the Transformer window. Can you clarify what's happening and how to recreate that? :)

I need that to only be applied to the rim, not the whole snare.

Link to comment
Share on other sites

I'm using the Environment merely to illustrate the concept. You can do it in Scripter just as well, if not easier. You set up two Sliders, or numerical boxes, just like you did, and fetch them with GetParameter and put it into the calculation as max and min. Then you push your incoming velocity through that calculation and output a note with the result of the calculation, just like you did with your first version of the script.
Link to comment
Share on other sites

I'm using the Environment merely to illustrate the concept. You can do it in Scripter just as well, if not easier. You set up two Sliders, or numerical boxes, just like you did, and fetch them with GetParameter and put it into the calculation as max and min. Then you push your incoming velocity through that calculation and output a note with the result of the calculation, just like you did with your first version of the script.

 

That seems like a more advanced level... I'm not sure I understand how that is accomplished.

Would you mind adding that to my script (modify it) so I can see what you mean? I've already added the 2 sliders.

 

// Compression only on C1

function HandleMIDI(event) {

if (event instanceof Note && event.pitch == 36) {
	var originalVelocity = event.velocity;
	var percentage = GetParameter (0)*100/originalVelocity;
	event.velocity = event.velocity - (event.velocity*(percentage/100));
}
event.trace();
event.send();
 
}


var PluginParameters = [
{
name:"Min Velocity",
type:"lin",
minValue:0,
maxValue:127,
numberOfSteps:127,
defaultValue:0,
},
{
name:"Max Velocity",
type:"lin",
minValue:0,
maxValue:127,
numberOfSteps:127,
defaultValue:127,
}
]

Link to comment
Share on other sites

You have a quite laborious three-lines set of calculations in your if-condition. Replace that with the calculation I gave you, substituting your parameters:

event.velocity = event.velocity * (GetParameter (1) - GetParameter (0)) / 127 + GetParameter (0);

I'd also suggest to swap the order of your controls, so they make more sense visually. Here it is in action, feeding it a constant stream of notes with rising velocity:

 

compress.gif.d770a4e390e055801c4d6b309fcf77f4.gif

Link to comment
Share on other sites

For context, this is my eDrum rig in Logic. As always, I'd approach it differently, for several reasons:

 

- I like to change things into the shape I need before I record them which is not impossible but difficult with Scripter

- I need several, drastically different setups during a show, including silent metronome and correct lead sheet pages in a PDF reader

- Scripter is fun, but its interface is pretty much not customizable and very wasteful on screen resources. This makes it ok for set-and-forget-processors, but very impractical if you need access to or even just visual feedback from its controls on a single (small) screen setup (i.e. Macbook)

 

So the Environment (plus a healthy amount of assisting Scripters) it is for me.

 

edrum.gif

- received drum notes light the switches, so I see what I'm (not) hitting

- I can see what I'm about to hit before I embarrass myself with the 28" tamtam when I thought it was a 6" splash.

- everything is stored in memory patches, so I can step through a set list with hitting a pad or a footswitch

 

Cut to the chase - every drum note has a min and max velocity number (black squares). These define the target range the incoming range of 1-127 will get compressed to:

 

edrum2.gif

This way I can compress towards the quiet range, towards the loud range or limit everything to a single, specific value.

 

So the calculation I would suggest is:

 

vcompressed = voriginal * (max - min) / 127 + min

 

You can implement this in javascript just as well. I have added some safety rails to ensure max is never less than min, but that's just for convenience.

Stunning!
Link to comment
Share on other sites

As a newbie when it comes to Javascript, would it be easy to create a script that reduces the range of the velocity?

So instead of having the velocity going from 1 to 127, it would go from 1 to 100, for example.

Any tips on what to look for to see if I can do it myself? Or is this not something that a beginner would be able to accomplish?

Have you considered the Velocity Processor MIDI Fx and /or the related track parmeters? Edited by Atlas007
Link to comment
Share on other sites

You have a quite laborious three-lines set of calculations in your if-condition. Replace that with the calculation I gave you, substituting your parameters:

event.velocity = event.velocity * (GetParameter (1) - GetParameter (0)) / 127 + GetParameter (0);

I'd also suggest to swap the order of your controls, so they make more sense visually. Here it is in action, feeding it a constant stream of notes with rising velocity:

 

compress.gif

 

Ok I tried the one you suggested. So that was assuming I was using Min Velocity first (Parameter 0) and Max Velocity second (Parameter 1), right? Because when I changed the order, it seemed to invert the effect. Just making sure...

 

Also, it makes more sense to me when I see min at the top, just cause it's a lower number, so in my mind it comes first ;)

But thanks for the code. I need to look at it again and see what you did there compared to mine.

Link to comment
Share on other sites

I think he's asking the question because he needs to control the velocity of just certain pitches.

 

Yes, I only need that applied to a single piece on my drum kit, the snare's rim, because the sidestick is super loud.

I'm using this code now, with fizzfilth's suggestion:

 

// Compression only on sidestick

function HandleMIDI(event) {

if (event instanceof Note && event.pitch == 37) {
	event.velocity = event.velocity * (GetParameter (1) - GetParameter (0)) / 127 + GetParameter (0);
}
event.trace();
event.send();
 
}


var PluginParameters = [
{
name:"Min Velocity",
type:"lin",
minValue:0,
maxValue:127,
numberOfSteps:127,
defaultValue:0,
},
{
name:"Max Velocity",
type:"lin",
minValue:0,
maxValue:127,
numberOfSteps:127,
defaultValue:127,
}
]

Link to comment
Share on other sites

I mean if you use the curve thing I mentioned earlier..you'll set up with something like this that uses the curve instead of a calculation formula and only makes the change for pitch 37:

 

var velocities = [0, 2, 3, 5, 7, 8, 10, 11, 13, 15, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 39, 41, 43, 46, 49, 51, 54, 56, 59, 62, 64, 67, 69, 72, 74, 77, 79, 81, 84, 86, 88, 89, 91, 93, 94, 95, 96, 97, 98, 99, 99, 100, 100, 100, 101, 101, 101, 100, 100, 100, 99, 99, 98, 98, 97, 96, 95, 94, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 78, 77, 76, 76, 75, 75, 74, 74, 74, 73, 73, 73, 74, 74, 74, 74, 75, 75, 76, 77, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 95, 96, 97, 98, 100, 100];

function HandleMIDI(event) {
   if(event instanceof NoteOn && event.pitch == 37) {
       event.velocity = velocities[event.velocity];
   }
   event.send();
}
Link to comment
Share on other sites

I mean if you use the curve thing I mentioned earlier..you'll set up with something like this that uses the curve instead of a calculation formula and only makes the change for pitch 37:

 

var velocities = [0, 2, 3, 5, 7, 8, 10, 11, 13, 15, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 39, 41, 43, 46, 49, 51, 54, 56, 59, 62, 64, 67, 69, 72, 74, 77, 79, 81, 84, 86, 88, 89, 91, 93, 94, 95, 96, 97, 98, 99, 99, 100, 100, 100, 101, 101, 101, 100, 100, 100, 99, 99, 98, 98, 97, 96, 95, 94, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 78, 77, 76, 76, 75, 75, 74, 74, 74, 73, 73, 73, 74, 74, 74, 74, 75, 75, 76, 77, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 95, 96, 97, 98, 100, 100];

function HandleMIDI(event) {
   if(event instanceof NoteOn && event.pitch == 37) {
       event.velocity = velocities[event.velocity];
   }
   event.send();
}

 

 

Oh ok. I thought you were still talking about the original script to adjust the velocity of the sidestick. I figured it out for that one.

For the curve, it's actually a different pitch, it's 38, which is the snare head. So yeah, I will check that too.

I'm doing my best to tweak this drum kit until it sound as realistic as possible, because I was able to achieve a pretty good drum sound right now and I'm loving the fact that I am able to add of these pieces of code and make it as natural as possible :)

Link to comment
Share on other sites

What I'm suggestion is you can use the curve for both pitches. So if you wanted a different curve for two different pitches and you don't have to worry about whether you got the calculation formula correct or not and you handle both 37 and 38 notes in the same script. You could do that several different ways, I'll give you two and then I'm off the thread as you seem to be on your way...

 

Example 1

 

This uses the curve generator to generate a linear response for 0-100 instead of 0-127. The curve looks like this in the webpage:

 

1118535697_ScreenShot2022-02-16at3_40_29PM.thumb.jpg.16b6d4b8ace0bed7f21f2c97a44c607a.jpg

 

That generates a complete script but just copy this one section:

 

var velocities = [0, 1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, 16, 17, 18, 19, 20, 20, 21, 22, 23, 24, 24, 25, 26, 27, 27, 28, 29, 30, 31, 31, 32, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53, 54, 54, 55, 56, 57, 57, 58, 59, 60, 61, 61, 62, 63, 64, 65, 65, 66, 67, 68, 69, 69, 70, 71, 72, 72, 73, 74, 75, 76, 76, 77, 78, 79, 80, 80, 81, 82, 83, 84, 84, 85, 86, 87, 87, 88, 89, 90, 91, 91, 92, 93, 94, 95, 95, 96, 97, 98, 99, 99, 100];

 

Then you modify the HandleMIDI function as follows:

 

function HandleMIDI(event) {
   if(event instanceof NoteOn && event.pitch == 37) {
       event.velocity = velocities[event.velocity];
   }
   event.send();
}

 

Then you need a curve for 38. I don't know what curve you need or want, but here is a simple one:

 

688678391_ScreenShot2022-02-16at3_43_37PM.thumb.jpg.f85405eb757313b767203beb51c9b4e0.jpg

 

That is similar as the one above, going to 100, but with a non-linear curve. But you can make it whatever you want. Copy and paste the same little bit as the last one:

 

var velocities38 = [0, 0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 31, 31, 32, 33, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99, 100];

But notice I changed the name of the variable in this second one from velocities to velocities38.

 

Add an if statement for catching pitch 38 that uses the second velocities38 array variable. Now the script looks like this:

 

var velocities = [0, 1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, 16, 17, 18, 19, 20, 20, 21, 22, 23, 24, 24, 25, 26, 27, 27, 28, 29, 30, 31, 31, 32, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53, 54, 54, 55, 56, 57, 57, 58, 59, 60, 61, 61, 62, 63, 64, 65, 65, 66, 67, 68, 69, 69, 70, 71, 72, 72, 73, 74, 75, 76, 76, 77, 78, 79, 80, 80, 81, 82, 83, 84, 84, 85, 86, 87, 87, 88, 89, 90, 91, 91, 92, 93, 94, 95, 95, 96, 97, 98, 99, 99, 100];

var velocities38 = [0, 0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 31, 31, 32, 33, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99, 100];

function HandleMIDI(event) {
   if(event instanceof NoteOn && event.pitch == 37) {
       event.velocity = velocities[event.velocity];
   }
   else if(event instanceof NoteOn && event.pitch == 38) {
       event.velocity = velocities38[event.velocity];
   }
   event.send();
}

 

So the above handles both pitches independently with a seperate compression and or velocity scaling curve as you wish....from one single script.

 

Example 2

 

This next version of the script might make your nose bleed, but anyway its probably how I would do it for those that want to explore more javascript possibilities. Main advantage is easier to expand in the future if desired:

 

// initialize velocities lookup array as two dimensional (per pitch)
var velocities = new Array(128);
for( let p=0; p<128; p++) {
   velocities[p] = new Array(128);
   for( let v=0; v<128; v++) {
       velocities[p][v] = v;
   }
}

// Handle Pitch 37
velocities[37] = [0, 1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, 16, 17, 18, 19, 20, 20, 21, 22, 23, 24, 24, 25, 26, 27, 27, 28, 29, 30, 31, 31, 32, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53, 54, 54, 55, 56, 57, 57, 58, 59, 60, 61, 61, 62, 63, 64, 65, 65, 66, 67, 68, 69, 69, 70, 71, 72, 72, 73, 74, 75, 76, 76, 77, 78, 79, 80, 80, 81, 82, 83, 84, 84, 85, 86, 87, 87, 88, 89, 90, 91, 91, 92, 93, 94, 95, 95, 96, 97, 98, 99, 99, 100];
// Handle Pitch 38
velocities[38] = [0, 0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 31, 31, 32, 33, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99, 100];


function HandleMIDI(event) {
   if(event instanceof NoteOn) {
       event.velocity = velocities[event.pitch][event.velocity];
   }
   event.send();
}

 

Good luck.

Link to comment
Share on other sites

What I'm suggestion is you can use the curve for both pitches. So if you wanted a different curve for two different pitches and you don't have to worry about whether you got the calculation formula correct or not and you handle both 37 and 38 notes in the same script. You could do that several different ways, I'll give you two and then I'm off the thread as you seem to be on your way...

 

Example 1

 

This uses the curve generator to generate a linear response for 0-100 instead of 0-127. The curve looks like this in the webpage:

 

Screen Shot 2022-02-16 at 3.40.29 PM.jpg

 

That generates a complete script but just copy this one section:

 

var velocities = [0, 1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, 16, 17, 18, 19, 20, 20, 21, 22, 23, 24, 24, 25, 26, 27, 27, 28, 29, 30, 31, 31, 32, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53, 54, 54, 55, 56, 57, 57, 58, 59, 60, 61, 61, 62, 63, 64, 65, 65, 66, 67, 68, 69, 69, 70, 71, 72, 72, 73, 74, 75, 76, 76, 77, 78, 79, 80, 80, 81, 82, 83, 84, 84, 85, 86, 87, 87, 88, 89, 90, 91, 91, 92, 93, 94, 95, 95, 96, 97, 98, 99, 99, 100];

 

Then you modify the HandleMIDI function as follows:

 

function HandleMIDI(event) {
   if(event instanceof NoteOn && event.pitch == 37) {
       event.velocity = velocities[event.velocity];
   }
   event.send();
}

 

Then you need a curve for 38. I don't know what curve you need or want, but here is a simple one:

 

Screen Shot 2022-02-16 at 3.43.37 PM.jpg

 

That is similar as the one above, going to 100, but with a non-linear curve. But you can make it whatever you want. Copy and paste the same little bit as the last one:

 

var velocities38 = [0, 0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 31, 31, 32, 33, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99, 100];

But notice I changed the name of the variable in this second one from velocities to velocities38.

 

Add an if statement for catching pitch 38 that uses the second velocities38 array variable. Now the script looks like this:

 

var velocities = [0, 1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, 16, 17, 18, 19, 20, 20, 21, 22, 23, 24, 24, 25, 26, 27, 27, 28, 29, 30, 31, 31, 32, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53, 54, 54, 55, 56, 57, 57, 58, 59, 60, 61, 61, 62, 63, 64, 65, 65, 66, 67, 68, 69, 69, 70, 71, 72, 72, 73, 74, 75, 76, 76, 77, 78, 79, 80, 80, 81, 82, 83, 84, 84, 85, 86, 87, 87, 88, 89, 90, 91, 91, 92, 93, 94, 95, 95, 96, 97, 98, 99, 99, 100];

var velocities38 = [0, 0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 31, 31, 32, 33, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99, 100];

function HandleMIDI(event) {
   if(event instanceof NoteOn && event.pitch == 37) {
       event.velocity = velocities[event.velocity];
   }
   else if(event instanceof NoteOn && event.pitch == 38) {
       event.velocity = velocities38[event.velocity];
   }
   event.send();
}

 

So the above handles both pitches independently with a seperate compression and or velocity scaling curve as you wish....from one single script.

 

Example 2

 

This next version of the script might make your nose bleed, but anyway its probably how I would do it for those that want to explore more javascript possibilities. Main advantage is easier to expand in the future if desired:

 

// initialize velocities lookup array as two dimensional (per pitch)
var velocities = new Array(128);
for( let p=0; p<128; p++) {
   velocities[p] = new Array(128);
   for( let v=0; v<128; v++) {
       velocities[p][v] = v;
   }
}

// Handle Pitch 37
velocities[37] = [0, 1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, 16, 17, 18, 19, 20, 20, 21, 22, 23, 24, 24, 25, 26, 27, 27, 28, 29, 30, 31, 31, 32, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53, 54, 54, 55, 56, 57, 57, 58, 59, 60, 61, 61, 62, 63, 64, 65, 65, 66, 67, 68, 69, 69, 70, 71, 72, 72, 73, 74, 75, 76, 76, 77, 78, 79, 80, 80, 81, 82, 83, 84, 84, 85, 86, 87, 87, 88, 89, 90, 91, 91, 92, 93, 94, 95, 95, 96, 97, 98, 99, 99, 100];
// Handle Pitch 38
velocities[38] = [0, 0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 31, 31, 32, 33, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99, 100];


function HandleMIDI(event) {
   if(event instanceof NoteOn) {
       event.velocity = velocities[event.pitch][event.velocity];
   }
   event.send();
}

 

Good luck.

 

 

 

Oh I see what you mean. Instead of using that first "velocity compression" script I created and then one for the curve, adding both to the same script, right?

That is a good option, if that's what you mean. Thanks.

 

And yes, the second example is waaaaaay to complicated for me. I'm sure other people will probably find it useful, though ;)

 

Thanks again for sharing that link and that extra code!

Link to comment
Share on other sites

 Share

×
×
  • Create New...