I'm trying to get BPM detection working and I want the BPM value to update as each new reading comes in but I want it averaged over the last four readings.
Assuming that you have an extracted trigger, you could try:
(<your detected trigger> durationBetweenTriggers: 4) s bpm removeUnits
Or to answer the general question of a moving average of length 4:
| length updateTrig beatDur |
length := 4.
updateTrig := <your detected trigger>.
beatDur := !LocalTime - (updateTrig shiftRegister: !LocalTime length: 2).
((beatDur - (updateTrig shiftRegister: beatDur length: length + 1)) accumulateIf: updateTrig initialValue: 0 reset: 0) / length
Or, if you just want a running average of length 4 of some value:
| length updateTrig |
length := 4.
updateTrig := <triggers at the rate you want to update the average>.
((!Value - (updateTrig shiftRegister: !Value length: length + 1)) accumulateIf: updateTrig initialValue: 0 reset: 0) / length
Or could you use the trigger that you are extracting directly to trigger other Sounds (rather than converting it to !BPM?