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

key triggering different loops

Discussion in 'Scripting Workshop' started by Bartolomeo Amati, Nov 12, 2021.

  1. Bartolomeo Amati

    Bartolomeo Amati New Member

    Messages:
    16
    Hello,

    How can i make a key play 2 different loops each time it's triggered?? i tried to explain it here :

    on init

    declare const $KEY_SWITCH_drumloop := 40
    declare $note_id

    end on
    on note

    First key trigger:
    if ($EVENT_NOTE = $KEY_SWITCH_drumloop)
    $note_id := play_note(30,126,0,0)

    Second key trigger:
    '' stop play_note(30,126,0,0)''
    '' and play_note (50,126,0,0)''

    end if
    end on
    Any help is highly appreciated!!
     
    Last edited: Nov 14, 2021
  2. Chromo Fonic

    Chromo Fonic New Member

    Messages:
    10
    I am not sure how the scripting would look for that.
    I know how you can go about it without scripting: use voice groups and the round-robin function of the group editor.
     
    • Like Like x 1
  3. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Pretty easy:

    Code:
    on init
        declare const $LOOP_KS := 40
        declare $flipflop
    end on
    
    on note
        if ($EVENT_NOTE = $LOOP_KS)
            play_note(30 + (20 * $flipflop), 127, 0, -1)
            $flipflop := 1 - $flipflop
        end if
    end on
     
    • Like Like x 1
  4. Bartolomeo Amati

    Bartolomeo Amati New Member

    Messages:
    16

    Thanks!
    For some reason the sample 30 is not looping anymore when using this code ??
    btw i love that ''$flipflop'' :D
     
  5. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Well, I guess then you replace that -1 with 0. But you get an endless loop then, unless you handle note release separately...
     
  6. Bartolomeo Amati

    Bartolomeo Amati New Member

    Messages:
    16
    Thanks for your answer!
    Yes i want the sample 30 to loop on release but once the key is trigger for the second time it should stop and play sample 50
     
  7. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Ah in that case:

    Code:
    on init
       declare const $LOOP_KS := 40
       declare $flipflop
       declare $ID
    end on
    
    on note
       if ($EVENT_NOTE = $LOOP_KS)
           note_off($ID)
           $ID := play_note(30 + (20 * $flipflop), 127, 0, 0)
           $flipflop := 1 - $flipflop
       end if
    end on
     
  8. Bartolomeo Amati

    Bartolomeo Amati New Member

    Messages:
    16
    Thank you so much that worked perfectly! i really thought that was gonna be using on release..
    Is there a way to synchronize it with the button on the gui ??


    on init
    declare ui_button $padbutton
    declare $note_id

    end on
    on ui_control($padbutton)
    if ($padbutton=1)
    $note_id := play_note(30,126,0,0)
    end if

    if ($padbutton=0)
    play_note(50,126,0,0)
    fade_out($note_id,10000,1)
    end if
    end on
     
  9. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    You just do the same thing that from note callback inside the ui_control callback.

    Code:
    on ui_control ($button)
       if ($button = 1)
          note_off($ID)
          $ID := play_note(30 + (20 * $flipflop), 127, 0, 0)
          $flipflop := 1 - $flipflop
       else
          note_off($ID)
       end if
    end on
     
  10. Bartolomeo Amati

    Bartolomeo Amati New Member

    Messages:
    16
    When i click on the button for the second time i doesn't play anything until i click it for the third time it plays note 50 but it mess up the synchronization with the key.
     
  11. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    That's exactly what the code does - pressing the button after it was enabled just stops the loop. How else would you want it? Try stuff out :)
     
  12. Bartolomeo Amati

    Bartolomeo Amati New Member

    Messages:
    16
    I'm trying to make the button react like the key when the button = 1 play note 30 and when button = 0 play note 50
    I tried to explain the problem on the attached screenshot video :
     
  13. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Please use CODE tags to retain indentation in the code you're posting.
     
  14. Bartolomeo Amati

    Bartolomeo Amati New Member

    Messages:
    16
    My bad..

    Code:
    on init
    
    make_perfview
    
    declare ui_switch $button
    set_text($button," ")
    set_control_par(get_ui_id($button),$CONTROL_PAR_WIDTH,50)
    set_control_par(get_ui_id($button),$CONTROL_PAR_HEIGHT,40)
    hide_part($button, $HIDE_PART_BG .or. $HIDE_PART_MOD_LIGHT .or. $HIDE_PART_TITLE .or. $HIDE_PART_VALUE)
    move_control_px($button, 223, 1)
    
    declare const $LOOP_KS := 40
    declare $flipflop
    declare $ID
    
    end on
    on ui_control ($button)
    if ($button = 0)
    note_off($ID)
    $ID := play_note(30,126,0,0)
    $flipflop := 1 - $flipflop
    else
    note_off($ID)
    
    end if
    if ($button = 1)
    note_off($ID)
    $ID := play_note(50,126,0,0)
    $flipflop := 1 - $flipflop
    end if
    end on
    
    on note
    if ($EVENT_NOTE = $LOOP_KS)
    note_off($ID)
    $ID := play_note(30 + (20 * $flipflop), 127, 0, 0)
    $flipflop := 1 - $flipflop
    end if
    end on
     
  15. medusa

    medusa NI Product Owner

    Messages:
    239
    Much better indentation. :-D

    Maybe try just this in the button code :
    Code:
    on ui_control(Pad)
        note_off($ID)
        $ID := play_note(30 + (20 * $flipflop), 127, 0, 0)
        $flipflop := 1 - $flipflop
    end on
     
    • Like Like x 1
  16. Bartolomeo Amati

    Bartolomeo Amati New Member

    Messages:
    16
    Thanks for your reply, that did work!
    But the key doesn't change the button modes (on/off) in the gui
     
  17. medusa

    medusa NI Product Owner

    Messages:
    239
    Code:
    on note
    if ($EVENT_NOTE = $LOOP_KS)
    note_off($ID)
    $ID := play_note(30 + (20 * $flipflop), 127, 0, 0)
    $flipflop := 1 - $flipflop
    pad := 1 - $pad
    end if
    end on
     
  18. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    In theory if he needs syncing between note callback and the button, $flipflop is totally not necessary and he can just use the state of the button.
     
    • Like Like x 1
  19. medusa

    medusa NI Product Owner

    Messages:
    239
    Ah yes, much tighter. This should work? No flipflops.
    Code:
    on note
        if ($EVENT_NOTE = $LOOP_KS)
            note_off($ID)
            $ID := play_note(30 + (20 * pad), 127, 0, 0)
            pad := 1 - pad
        end if
    end on
    
    on ui_control(pad)
        note_off($ID)
        $ID := play_note(30 + (20 * pad), 127, 0, 0)
    end on
     
  20. Bartolomeo Amati

    Bartolomeo Amati New Member

    Messages:
    16
    Nah i will keep that flipflop haha, thanks for your help guys !