// Block spurious high velocity notes // Velocities that exceed the threshold parameter are replaced with the // mean value of the past 64 keypresses var iThreshold("Threshold") : Parameter 0..127 = 112 m : MidiMessage iVelocities : integer[64] // holds the previous 64 note velocities iNewNote : integer // track the index where the newest note is stored iOldNote : integer // track the index where the oldest note is stored iRunningSum : integer // keep a running sum of velocities iCount : integer // number of velocity values in the array iTraps : integer // track number of notes whose velocities have been trapped Initialization SetInfoMessage("When active, this scriptlet traps notes with velocities that meet or exceed a threshold, and sends them with a velocity equal to the mean of the previous 64 notes.") SetDisplayMessage("Click the 'info' button for instructions") ClearArray(iVelocities) // no notes yet iNewNote = 0 // next note will be stored at this index iOldNote = 0 // oldest note is stored at this index iRunningSum = 0 // running sum of velocities starts at zero iCount = 0 // no velocity values in the array iTraps = 0 // no traps have been made yet End On NoteOnEvent(m : NoteMessage) var iVelocity : integer iVelocity = GetVelocity(m) // capture this note velocity iVelocities[iNewNote] = iVelocity // store it in the array iRunningSum = iRunningSum + iVelocity // add the velocity to the running counter If ( iCount < 64 ) Then iCount = iCount + 1 End If ( iVelocity >= iThreshold ) Then // trap this note iVelocity = iRunningSum / iCount iTraps = iTraps + 1 SetDisplayMessage("Number of trapped notes: " + iTraps) End SendNow(WithVelocity(m, iVelocity)) //Print("iNewNote:"+iNewNote + ", iVelocity:"+iVelocity + ", iOldNote:"+iOldNote + ", iRunningSum:"+iRunningSum + ", iCount:"+iCount + ", iTraps:"+iTraps) iNewNote = iNewNote + 1 // next note gets stored in next available array slot, rolling over if needed If ( iNewNote == 64 ) Then iNewNote = 0 End If ( iNewNote == iOldNote ) Then // clean up oldest note before we overwrite it iRunningSum = iRunningSum - iVelocities[iOldNote] iOldNote = iOldNote + 1 If ( iOldNote == 64 ) Then iOldNote = 0 End End End