Change Slider Value via MIDI + Velocity
-
@trillbilly I have a custom GPT that i fed with HISE source and hise docs and told it a bit about hise.
nothing fancy. it still makes a lot of mistakes as it forgets this isn't exactly javascript., but I guess I should upload more code examples.
I entered your post in there https://forum.hise.audio//post/75262 hoping that maybe the magical talking computer will do a better job of explaining it to you than we did
and this is what i got
The issue you're encountering seems to stem from a couple of misunderstandings about how to check if a note is in your
keySwitchesC
array and how to properly use thecAnimation
function. Here's a breakdown of what needs to be fixed:- Checking for Key Presses: You are using
Message.getVelocity()
to check if a key is in yourkeySwitchesC
array, butMessage.getVelocity()
returns the velocity of the note being played, not its pitch. You should useMessage.getNoteNumber()
to check if the played note's pitch matches any of the Cs in your array. - Using
indexOf
Properly: TheindexOf
method returns-1
if the element is not found in the array. Any other returned value (i.e.,0
or above) means the element was found. Therefore, you need to check if the result ofindexOf
is not equal to-1
to confirm the presence of the note number in your array. - The
cAnimation
Function: YourcAnimation
function seems to be designed to set the slider's value, but it's missing thevalue
parameter. You need to decide what value you want to set the slider to when a C note is pressed. Since the function takes anindex
, you might want to use this index or the note number itself to determine the slider's value.
Here's a corrected version of your script:
const var slider = Content.getComponent("slider"); const var keySwitchesC = [24, 36, 48, 60, 72, 84, 96, 108, 120]; // Cs inline function cAnimation(value) { slider.setValue(value); } function onNoteOn() { var noteNumber = Message.getNoteNumber(); // Get the note number var index = keySwitchesC.indexOf(noteNumber); // Find the index of the note in the array if (index != -1) // Check if the note is a C { // Decide what value you want to set. For example, setting the slider to the note's velocity: var velocity = Message.getVelocity(); cAnimation(velocity); // Call the animation function with the determined value } }
In this revised version, the script correctly identifies when a C note is played by checking its note number against the
keySwitchesC
array. When a match is found, it uses the note's velocity (or any other logic you define) as the value for thecAnimation
function to set the slider's value. This approach ensures that the animation only happens for the specified C notes.Obviously declaring variables inside the realtime callbacks is a nono so once I reminded it about it, I got
const var slider = Content.getComponent("slider"); const var keySwitchesC = [24, 36, 48, 60, 72, 84, 96, 108, 120]; // C notes // Use 'reg' for variables involved in real-time callbacks reg noteNumber = -1; reg index = -1; reg velocity = 0; inline function cAnimation(value) { slider.setValue(value); } function onNoteOn() { noteNumber = Message.getNoteNumber(); // Use the 'reg' variable index = keySwitchesC.indexOf(noteNumber); // Update the 'reg' variable based on the note number if (index != -1) // Check if the note is a C { velocity = Message.getVelocity(); // Use the 'reg' variable for velocity cAnimation(velocity); // Call the animation function with the velocity } }
and when paste this, it sets slider to the value of velocity whenever the note is C.
Also, a much simpler way to check for C is
if ((Message.getNoteNumber() % 12) == 0)
.And if you ask the GPT about it, it'll tell you the same thing:
Yes, indeed! Since you're dealing with MIDI note numbers where middle C is 60, and each octave >spans 12 notes, you can simplify the process of checking for C notes by leveraging this pattern. >Here's how you can do it:
const var slider = Content.getComponent("slider"); reg noteNumber = 0; reg velocity = 0; function onNoteOn() { noteNumber = Message.getNoteNumber(); // Check if the note is a C (where middle C is 60) if (noteNumber % 12 == 0) { velocity = Message.getVelocity(); slider.setValue(velocity); } }
In this version of the script:
- We calculate the difference between the note number and 60, representing middle C.
- We then use the modulo operator
%
to check if this difference is a multiple of 12, indicating >that the note is a C. - If the condition is met, we proceed with setting the slider value based on the note's velocity.
This approach simplifies the code by removing the need for a separate array to store C notes and directly computes whether the note is a C using arithmetic operations.
And do a much better job of explaining these very simple snippets, mentioning all the important things so even at the base level of skill you should be able to understand.
- Checking for Key Presses: You are using
-
@aaronventure said in Change Slider Value via MIDI + Velocity:
will do a better job of explaining it to you than we did
I was intentionally vague to try and make him work it out for himself :)
-
@d-healey it seems the
.contains
may have done the trick. And yes, I appreciate you allowing me to work it through,@aaronventure Thats cool what you did with GPT. I've only ever tried to use it once or twice with HISE and got lost faster than without it.
-
@d-healey Well, me too, but sometimes it can get a bit much and lost in too many replies.
I wanted to point out that ChatGPT will not only write the code, but will explain all the reasoning behind all the decisions it made and thoroughly comment the code itself which is far better than just handing down a clear solution without context and is often beyond the scope of forum posts for such simple questions, at least based on what I've seen in years of browsing VI-C and in the last half a year or so, here.
The GPT generation contains everything one needs to know about why the initial snippet didn't work, and what to do in order to make it work.
-
@aaronventure said in Change Slider Value via MIDI + Velocity:
wanted to point out that ChatGPT will not only write the code, but will explain all the reasoning behind all the decisions it made
I think this is great if you know that what it's giving you is correct. The problem is as a beginner you don't know if the info is accurate or not. It could provide a wonderful explanation that is totally wrong and code doesn't work.
-
@d-healey Yeah absolutely, it's also still not following HISEScript coding practices (maybe I should hardcode that in the settings).
I guess what I'm trying to say is, we don't deserve the robots; we could've pasted the post into it, and just copied back the response without the code
The issue you're encountering seems to stem from a couple of misunderstandings about how to check if a note is in your keySwitchesC array and how to properly use the cAnimation function. Here's a breakdown of what needs to be fixed:
The issue you're encountering seems to stem from a couple of misunderstandings about how to check if a note is in your
keySwitchesC
array and how to properly use thecAnimation
function. Here's a breakdown of what needs to be fixed:-
Checking for Key Presses: You are using
Message.getVelocity()
to check if a key is in yourkeySwitchesC
array, butMessage.getVelocity()
returns the velocity of the note being played, not its pitch. You should useMessage.getNoteNumber()
to check if the played note's pitch matches any of the Cs in your array. -
Using
indexOf
Properly: TheindexOf
method returns-1
if the element is not found in the array. Any other returned value (i.e.,0
or above) means the element was found. Therefore, you need to check if the result ofindexOf
is not equal to-1
to confirm the presence of the note number in your array. -
The
cAnimation
Function: YourcAnimation
function seems to be designed to set the slider's value, but it's missing thevalue
parameter. You need to decide what value you want to set the slider to when a C note is pressed. Since the function takes anindex
, you might want to use this index or the note number itself to determine the slider's value.
which we basically did write, a lot more sparsely and over a bunch of posts where it appears scattered and might be tough to navigate, just because we're lazy to go in depth in the first place, ultimately wasting more time than necessary.
The response still requires the OP to take a look at their own code, check the docs etc. to figure out the proper syntax for things if they don't know.
Just musing; my fever might be coming back.
-
-
@d-healey One last issue. When releasing a key, it forces all animations back to 0. I'd like if a key is released, only that animation is returned to 0 while the others stay out.
I tried using an "if/else" statement in the onNoteOff but it throw an error on the "else".
Could you point me in the right direction?
-
@trillbilly said in Change Slider Value via MIDI + Velocity:
I tried using an "if/else" statement in the onNoteOff but it throw an error on the "else".
Snippet
-
@d-healey
Here is snippet. This one is broken because of the if/else in onNoteOff. If you remove that, everything works but when you Play C & D together, then release one, both animations return to 0.HiseSnippet 1341.3oc4XE2SaaDE+bRbWi6FpcpaZ+4Iz9ifTFMFn.UnoFHAphVCDgoroUsgNrujbB667ru.jMUo8QaeD1Gk9MX6c1Nw1fAxhJUSpQBTdu6987u68d268b5EHroggh.jV0iF6SQZett0XtbXqgDFG0oMRaA8tjPIM.GqZmw9jvPpCRSq7qTJzpVAE848ubGhKgaSSUgPGKX1zWy7XxTs8Z9CLW28HNziXdY18ZM6XK3sDthQ.eJq2.4SrOiLftOQssR5HsGrqCSJBrjDIMDoUYGgyXqghK3w6+XVH6TWpRvDYAFJV8dBWGEiUeG0ZHy0o2jycHBoo2K0KTN1K7T8tLG1T8odiGGs.NEQV+gVo7zqbN5YdSzq.JokgRUhozSzsrCX9xzUT74Q5c3PvoOAb6YoR7dQkbpn2R.6fKW1ibFcu.PXJhZq2nQcL7uk1xv.b8gR74j.bnKyAh2eOdBxATYKgmufCB0VLd4EALWEh4cfwbw7OnynistfIsGRCaAPeKPFyUpiWYs53UWuNdsMUjqNdCP2lftW.5LaroZSM9ksvO6YsBK1XsUFC.YBfVA.sJf44pSJnaCP2lftWnriYzSbkHi0NzvfwcYbJt+HtsjI3X6s4LOh5q0XbG5kKY7GXL1.9K4DubHUdLwcDMY8sxtl8PBe.0oFn8cW21N2ksMuMialy5SMpfuuPROfWCrlQUvCYXTk0GWKqmdYvmIg74vZcg7H35kJVofs+HuSoA0VZokLpBnyd5ygOhMGz+lguE.VcVpVjiJCpiotBalbbsHHuKBgh0suFoaOyjF93THuaOW71b1HNFhv3qFE52ONLv5W6bkE.FZTclcGy9y2nJ0Mjlw34R7xRK0ky.gqq53pX1UVVUNt3UR.ViG4qpiSNOIaDpQkuv2ClsBe1w0JxrQAuCmIOvmlHmV41rfxjnjhMPAyRILD1pLpb4BIkKshbIHFXjGpG6ePQrGp6qE01YFPVMAo4Uf9lNsIRhpdcBS.14SCjLkSPqM8bn4Wb06p5sogmIE9QTMotHn9tI6koMH+0limJ7mus4ELG4PEW9YPdHkMXnTIcIH4QRg8WmzD5e6aw9cEw0MWtAxi433R6IBYp3apMOnIZlHWp+HyiwMC6.pduvtq2VDZHKbF4Rj46RqFMIYAkMx1ZT09iC1db1QWtZq6J2bFbiBmrnv7yYjtOQumpFUw7sTA7UkveOy2jAg9B8c62mZKSIaE889oacpmO3TYgXp7HcKnAZzzoQD4qijw+H4bJ9UTNMP4TMukAT+6Yc.U+Yd.0Crkvi+n.BOzWDlyvVTO1QvMovrJeSHLBF82NTEkypukfDT3Ru+k6AGxBwXQjiBhRW11SLhKykdTdliIYKHm+VSl8WvHpUtulZ9i5Uu60JE+GdGjR8t1ah8XH+1y2ktK+bncOnQw2uD5izmLxUNQa9D9tBtvenfyrylMbHUFvFLfFj8bT3gaaoDdkqTMOs4gTWJIal82170PFII.7Yzayu7A8keJL18M5wzEqtkh+eU09OKl1F5SlTKhve0TQ7zii4cyVnnwQjH5jFR6.g+.oBTpt1Ta1oT27Alq2pXtCLePZmU9Sy1YGJFIY7AcIvMQX3Gc3kNrftH1TfIbNL+tZ1nRpNAwxMlLioEk6DI7OvmjEMUxZIKZNYwOJOCOhcf3D63QlUWVeXjF3byi9QZpp2UIiSmTVuQ7HcrSrsUt6uyDgJFyJyAlUmCLqMGXd9bfY84.yFyAlMuULpNuaORJheiXHf1s2tQu0hl1tbUYknrdz+B+FZVW
-
@trillbilly Your if statement is missing curly braces
-
@d-healey I get the same error
! Line 8, column 2: Found 'else' when expecting a statement
-
@trillbilly Show me your if statement
-
@d-healey This is where Im starting and trying to work my way out.
function onNoteOff() { if (keySwitchesC.contains(Message.getNoteNumber())) { slider.setValue(Message.getVelocity()); slider.changed(); slider1.setValue(Message.getVelocity()); slider1.changed(); } }
-
@trillbilly Show me the one that doesn't work
-
@d-healey Any else statement I do like this.
function onNoteOff() { if (keySwitchesC.contains(Message.getNoteNumber())) { slider.setValue(index); slider1.setValue(index); else slider.setValue(Message.getVelocity()); slider1.setValue(Message.getVelocity()); } }
I get the error:
! Line 8, column 2: Found 'else' when expecting a statement
-
@trillbilly I just had a similar conversation with another forum member a few minutes ago :)
Code is written in blocks. Blocks are defined by opening and closing curly braces
{}
Your
if
statement is a block, and so requires{}
if (something) { }
Your else is also a block, so it also needs curly braces.
else { }
-
@d-healey ahhhh, yes. Now I see.
Im confusing myself on the order I should be doing this in.
*Example:
If slider changes valueelse
these slider stay same value*
Im confused which way I call for the slider in the IF statement. Do I call just the slider or do I call the "cKeySwitch" Variable or "cAnimation" Function. I've tried them all and cant seem to figure it out. The animation compiles and still works but the issue is not fixed,
-
@trillbilly Write it out as a list, in English.
Do this,
Then do this,
If this, do this,
Otherwise do this
etc.
etc.It might make it easier to figure out the flow of the program.