| 19 | } |
| 20 | |
| 21 | void process(bool pingRecieved, float sampleTime) { |
| 22 | timer.process(sampleTime); |
| 23 | |
| 24 | bool clockRestarted = false; |
| 25 | |
| 26 | if (pingRecieved) { |
| 27 | |
| 28 | bool tempoShouldBeUpdated = true; |
| 29 | float duration = timer.getTime(); |
| 30 | |
| 31 | // if the ping was unusually different to last time |
| 32 | bool outlier = duration > (pingDuration * 2) || duration < (pingDuration / 2); |
| 33 | // if there is a previous estimate of tempo, but it's an outlier |
| 34 | if ((pingDuration && outlier)) { |
| 35 | // don't calculate tempo from this; prime so future pings will update |
| 36 | tempoShouldBeUpdated = false; |
| 37 | pingDuration = 0; |
| 38 | } |
| 39 | else { |
| 40 | pingDuration = duration; |
| 41 | } |
| 42 | timer.reset(); |
| 43 | |
| 44 | if (tempoShouldBeUpdated) { |
| 45 | // if the tempo should be updated, do so |
| 46 | tempo = pingDuration; |
| 47 | clockRestarted = true; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // we restart the clock if a) a new valid ping arrived OR b) the current clock expired |
| 52 | clockRestarted = clockExpiry.process(!clockTimer.process(sampleTime)) || clockRestarted; |
| 53 | if (clockRestarted) { |
| 54 | clockTimer.reset(); |
| 55 | clockTimer.trigger(tempo); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | bool isTempoOutHigh() { |
| 60 | // give a 1ms pulse as tempo out |