# About sustain pedal script



## tomaslobosk (Mar 17, 2016)

Hey folks, I've been fooling around with a custom sustain pedal script for quite some time.

The script is working now for all tasks except when I'm sending two consecutive note-on messages (same-key, no note-off message, so active voices: 2)

It should trigger 2 release samples, and it's triggering only the newest voice release sample.


```
on release
  if(%CC[64]<64)
    get_event_ids(%ids)
    count:=0
    while (%ids[count]#0)
      if (get_event_par(%ids[count],EVENT_PAR_NOTE)=EVENT_NOTE)
        note_off(%ids[count])
        wait(1)
      end if
      inc(count)
    end while
    disallow_group(ALL_GROUPS)
    {allow release samples groups}
  else
    ignore_event(EVENT_ID)
  end if
end on

on controller
  if(CC_NUM=64)
    if (%CC[64]<64 and lastcc64value>=64)
      get_event_ids(%ids)
      count:=0
      while (%ids[count]#0)
        if(KEY_DOWN[get_event_par(%ids[count],$EVENT_PAR_NOTE)]=0)
          note_off(%ids[count])
        end if
        inc(count)
      end while
    end if
    lastcc64value:=%CC[64]
  end if
end on
```

Any help will be extremely appreciated.


----------



## Mike Greene (Mar 17, 2016)

First, I have a question about your while loop. Simplifying a bit, you have:

```
while $count # 0
  do stuff
  inc($count)
end while
```

Shouldn’t that while loop go on forever, since $count will never be 0? I’d be interested to know if there’s some trick that I didn’t know about.

Anyway, to your problem - the issue is that your %ids array has only one entry per note ($EVENT_NOTE.) So if you play a note twice, only the newest note id is stored and the previous one is erased. There are a couple ways to fix this:

*Method 1:* If you will never have more than two active notes playing on the same key, you could have two arrays. One would be %ids and the other would be %Previousids. Then each time a new note is played, just do:

```
%Previousids[$EVENT_NOTE] := %ids[$EVENT_NOTE]
%ids[$EVENT_NOTE] := $EVENT_ID
```
Then release notes for both of these arrays, instead of just the %ids array.

*Method 2:* Instead of making the %ids array based on the 127 notes on the keyboard, I would make it so that it contains all the recently played notes, up to however many notes you anticipate being active at once. So at any given time, the array would be filled with:
%ids[0] = Newest note id
%ids[1] = Second newest note id
%ids[2] = Third newest note id
%ids[3] = Fourth newest note id
…
%ids[100] = 99th newest note id

Of course, each time a new note is played, you move each entry back one, like this:

```
$count := 100
while ($count > 0)
  %ids[$count] := %ids[$count - 1]
  dec($count)
end while
%ids[0] := $EVENT_ID
```


----------



## KrisY (Jan 21, 2019)

tomaslobosk said:


> Hey folks, I've been fooling around with a custom sustain pedal script for quite some time.
> 
> The script is working now for all tasks except when I'm sending two consecutive note-on messages (same-key, no note-off message, so active voices: 2)
> 
> ...




Hello, did you manage to solve this issue? I would love a solution to this! In a similar situation.


----------



## KrisY (Mar 8, 2019)

Mike Greene said:


> ... the issue is that your %ids array has only one entry per note ($EVENT_NOTE.) So if you play a note twice, only the newest note id is stored and the previous one is erased.



I´m doing a similar script at the moment, and this thing above seems inaccurate. The %ids array is storing the result of *get_event_ids*, all active event id:s. It stores all note on events, so if a note is pressed repeatedly, all events of that note and all other notes active is stored in the %ids array, upon using get_event_ids. 

Your 2 arrays came in handy, when checking on controller for the latest 2 items for every key. In my case it is for making the latest key release at a different fade_out time from the one being released when releasing the pedal, while holding the same key (_for repeating the keys_). Fading that one out slower than the absolute last seems key for smoothness. And the native script is rigid, follows only the envelope setting. But changing it seems to work on all timings / fade_out speeds less than the maximum _Env release setting of the group_.

My current problem is similar to that of thomas´s one, how to get the right id´s to do the correct thing? On controller. In this case cc64. The RCB seems to be entirely oblivious to what the type of event is, it is always *KEY_DOWN = 0 and NOTE_HELD = 0 and CC64 < 64*. All else are only _key-off messages_, that do not release the note. Not if the _native_ pedal script is active. 

I´m using arrays like the ones above to intercept the type of on note variable or array-store on controller. Does anyone have a proper "matrix" for all release situations? *Is there a good pedal-repedal-release-etc script out there for most uses?* (_excluding calculations or change-vol etc_)


----------

