iphone BPM tempo button
开发者_开发问答i want to create a button that allows the user to tap on it and thereby set a beats per minute. i will also have touches moved up and down on it to adjust faster and slower. (i have already worked out this bit).
what are some appropriate ways to get the times that the user has clicked on the button to get an average time between presses and thereby work out a tempo.
Overall
You best use
time()
fromtime.h
instead of anNSDate
. At the rate of beats the overhead of creating anNSDate
could result in an important loss of precision.I believe
time_t
is guaranteed to be of double precision, therefore you're safe to usetime()
in combination withdifftime()
.Use the whole screen for this, don't just give the user 1 small button.
Two idea
Post-process
Store all times in an array.
Trim the result. Remove elements from the start and end that are more than a threshold from the average.
Get the average from the remaining values. That's your speed.
If it's close to a common value, use that.
Adaptive
Use 2 variables. One is called
speed
and the othererror
.After the first 2 beats calculate the estimated speed, set
error
tospeed
.After each beat
queue = Fifo(5) # First-in, first-out queue. Try out # different values for the length currentBeat = now - timeOflastBeat currentError = |speed - currentBeat| # adapt error = (error + currentError) / 2 # you have to experiment how much # weight currentError should have queue.push(currentBeat) # push newest speed on queue # automatically removes the oldest speed = average(queue)
As soon as
error
gets smaller than a certain threshold you can stop and tell the user you've determined the speed.Go crazy with the interface. Make the screen flash whenever the user taps. Extra sparks for a tap that is nearly identical to the expected time.
Make the background color correspond to the error. Make it brighter the smaller the error gets.
Each time the button is pressed, store the current date/time (with [NSDate date]
). Then, the next time it's pressed, you can calculate the difference with -[previousDate timeIntervalSinceNow]
(negative because it's subtracting the current date from the previous), which will give you the number of seconds.
精彩评论