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

后端 未结 9 598
北荒
北荒 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
        }
    }
    
    0 讨论(0)
  • 2021-01-30 03:26

    im doing something similar using remoteIO output. i do not rely on NSTimer. i use the timestamp provided in the render callback to calculate all of my timing. i dont know how acurate the iphone's hz rate is but im sure its pretty close to 44100hz, so i just calculate when i should be loading the next beat based on what the current sample number is.

    an example project that uses remote io can be found here have a look at the render callback inTimeStamp argument.

    EDIT : Example of this approach working (and on the app store, can be found here)

    0 讨论(0)
  • 2021-01-30 03:26

    Really the most precise way to approach timing is to count audio samples and do whatever you need to do when a certain number of samples has passed. Your output sample rate is the basis for all things related to sound anyway so this is the master clock.

    You don't have to check on each sample, doing this every couple of msec will suffice.

    0 讨论(0)
提交回复
热议问题