Swift Solid Metronome System

前端 未结 3 490
北恋
北恋 2020-12-28 23:30

I am trying to build a reliable solid system to build a metronome in my app using SWIFT.

I Have built what seems to be a solid system using NSTimer so far.. The only

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 00:29

    A metronome built purely with NSTimer will not be very accurate, as Apple explains in their documentation.

    Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer.

    I would suggest using an NSTimer that fires on the order of 50 times per desired tick (for example, if you would like a 60 ticks per minute, you would have the NSTimeInterval to be about 1/50 of a second.

    You should then store a CFAbsoluteTime which stores the "last tick" time, and compare it to the current time. If the absolute value of the difference between the current time and the "last tick" time is less than some tolerance (I would make this about 4 times the number of ticks per interval, for example, if you chose 1/50 of a second per NSTimer fire, you should apply a tolerance of around 4/50 of a second), you can play the "tick."

    You may need to calibrate the tolerances to get to your desired accuracy, but this general concept will make your metronome a lot more accurate.

    Here is some more information on another SO post. It also includes some code that uses the theory I discussed. I hope this helps!

    Update The way you are calculating your tolerances is incorrect. In your calculations, notice that the tolerance is inversely proportional to the square of the bpm. The problem with this is that the tolerance will eventually be less than the number of times the timer fires per second. Take a look at this graph to see what I mean. This will generate problems at high BPMs. The other potential source of error is your top bounding condition. You really don't need to check an upper limit on your tolerance, because theoretically, the timer should have already fired by then. Therefore, if the elapsed time is greater than the theoretical time, you can fire it regardless. (For example if the elapsed time is 0.1s and and the actual time with the true BPM should be 0.05s, you should go ahead and fire the timer anyways, no matter what your tolerance is).

    Here is my timer "tick" function, which seems to work fine. You need to tweak it to fit your needs (with the downbeats, etc.) but it works in concept.

    func tick(timer:NSTimer) {
        let elapsedTime:CFAbsoluteTime = CFAbsoluteTimeGetCurrent() - lastTick
        let targetTime:Double = 60/timer.userInfo!.objectForKey("bpm")!.doubleValue!
        if (elapsedTime > targetTime) || (abs(elapsedTime - targetTime) < 0.003) {
            lastTick = CFAbsoluteTimeGetCurrent()  
            # Play the click here
        }
    }
    

    My timer is initialized like so: nextTimer = NSTimer(timeInterval: (60.0/Double(bpm)) * 0.01, target: self, selector: "tick:", userInfo: ["bpm":bpm], repeats: true)

提交回复
热议问题