Simple TapTempo class for Swift

This is a Swift class that provides the ability to average a series of button taps and return a tempo in beats per minute.

Implementation

To instantiate, provide a value for timeOutInterval  (the amount of idle time between taps that will reset analysis) and a value for minimumTaps (the number of taps required to start analysis). Then, repeatedly call the addTap() function (typically in an @IBAction handler), which will return the tempo, or nil if the minimum number of taps has not been reached.

Notes

  • Currently, this class averages all taps between time out resets. Line 24 could easily be rewritten to provide an average of only the last x taps, a weighted average, or some other type of analysis.
  • A value of 1.0 for timeOutInterval will effectively limit the slowest tempo detected to 60 bpm. A more reasonable value would be between 2.0 (30 bpm) and 3.0 (20 bpm).

2 thoughts on “Simple TapTempo class for Swift

Leave a Reply