1. IMPORTANT:
    We launched a new online community and this space is now closed. This community will be available as a read-only resources until further notice.
    JOIN US HERE

Solved Group activation depending on the current note's pitch relative to the previous one

Discussion in 'Scripting Workshop' started by Chromo Fonic, Nov 10, 2021.

  1. Chromo Fonic

    Chromo Fonic New Member

    Messages:
    10
    Any help will be much appreciated!
    I am trying to allow (disallow) groups depending on whether the current note is lower (or higher) in pitch than the last note played.
    I found out how to allow specific groups based on the "if" and "else".
    However, I can not figure how to declare "$pitch_of_last_note" (probably such a thing does not exist) and determine whether the current note comes after a lower or a higher pitch. I imagined something like the lines :


    on init
    declare $pitch_of_last_note := 0
    end on

    on note
    disallow_group($ALL_GROUPS)
    inc($pitch_of_last_note)
    if ($pitch_of_last_note <= 1)
    allow_group(0)
    else
    allow_group(1)
    end if
    message($pitch_of_last_note)
    end on

    on release
    dec($pitch_of_last_note)
    if ($pitch_of_last_note < 0)
    $pitch_of_last_note := 0
    end if
    end on
     
  2. soyfabi

    soyfabi NI Product Owner

    Messages:
    26
    You could store your last played Note in a variable like $last_note and then allow your desired group whether your new played $EVENT_NOTE is lower or higher than $last_note
     
    • Like Like x 1
  3. Chromo Fonic

    Chromo Fonic New Member

    Messages:
    10
    Thank you for the reply. I can't figure a way to express the last $last_note variable as a function of the internal variable $EVENT_NOTE. Do I need to use the increment function somehow. It's my first script.
     
  4. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    No need for any increments. Just use $last_note := $EVENT_NOTE at the end of note callback. Make sure to set it to default value of -1 in init callback, and in note callback don't do any calculation if it's -1. So, something like:

    Code:
    on init
        declare $last_note := -1
    end on
    
    on note
        if ($last_note > -1)
            < do your group allowing here for the case when a new note is either higher or lower than last one >
        else
            < do your group allowing for the first note ever case >
        end if   
    
        $last_note := $EVENT_NOTE
    end on
     
    • Like Like x 1
  5. Chromo Fonic

    Chromo Fonic New Member

    Messages:
    10
    You are evil..... and a legend! It worked!
    You guys are good. You point in the right direction and provide the right amount of help to facilitate learning. There's a sort of "high" associated when the code finally works. I got a buzz :) (even if it's a trivial code for you). Thank you.

    I wanted to have three groups. The first group (5) plays when an incoming note comes from a lower tonality, the second group (6) when it comes from a higher tonality, and the third group (4) plays on note repetitions.

    For any future users that might have a similar question, I am pasting the code below.



    Code:
    on init
     declare $last_note := -1
    end on
    
    on note
    disallow_group($ALL_GROUPS)
        if ($last_note - $EVENT_NOTE > 0)
                allow_group(5)
            end if
                
                if ($last_note - $EVENT_NOTE < 0)
                     allow_group(6)
            end if   
                if  ($last_note = $EVENT_NOTE)
                    allow_group(4)
        end if
    
         $last_note := $EVENT_NOTE
    
    end on
     
  6. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    That last condition should probably be: if ($last_note = $EVENT_NOTE or $last_note = -1).
     
  7. Chromo Fonic

    Chromo Fonic New Member

    Messages:
    10
    Evil, you are somehow right. Attention to detail is paramount.
    In the case the last condition being: if(if $last_note = $EVENT_NOTE),
    the first note that sounds when first loading the instrument (or applying the script) is group 6 (I wished it was group 4).
    When I change the last condition to: if ($last_note = $EVENT_NOTE or $last_note = -1) both groups 4 and 6 sound on the first instance of applying the script.
    The solution I settled on was for the last condition to be:
    if ($last_note = $EVENT_NOTE or $last_note = -1)
    disallow_group(6)
    allow_group(4)

    The result is that when you first load the instrument or apply the script, only group 4 sounds, whereas when moving up and down the keyboard, group 6 and group 5 sound, respectively, and when you repeat the same note, group 4 plays.