How to program a real-time accurate audio sequencer on the iphone?

后端 未结 9 612
北荒
北荒 2021-01-30 02:56

I want to program a simple audio sequencer on the iphone but I can\'t get accurate timing. The last days I tried all possible audio techniques on the iphone, starting from Audio

9条回答
  •  遇见更好的自我
    2021-01-30 03:23

    By measuring the time elapsed for the "Do some work" part in the loop and subtracting this duration from the nextTime greatly improves accuracy:

    while (loop == YES)
    {
        timerInterval = adjustedTimerInterval ;
    
        startTime = CFAbsoluteTimeGetCurrent() ;
    
        if (delegate != nil)
        {
            [delegate timerFired] ; // do some work
        }
    
        endTime = CFAbsoluteTimeGetCurrent() ;
    
        diffTime = endTime - startTime ; // measure how long the call took. This result has to be subtracted from the interval!
    
        endTime = CFAbsoluteTimeGetCurrent() + timerInterval-diffTime ;
    
        while (CFAbsoluteTimeGetCurrent() < endTime)
        {
            // wait until the waiting interval has elapsed
        }
    }
    

提交回复
热议问题