user393964
user393964

Reputation:

Update the UI while playing audio

My app basically plays notes after each other and I want to show which note is being played at each time. Each note is a square in the UI and I have a 1-pixel view that moves from left to right to the note that is being played.

The problem is that I'm not sure how to do these two things at the same time. I can't send a message from my sample-rendering function because that could be to slow and could cause glitches in my audio playback. Does anyone have any suggestions how I should update my UI to reflect what is being played?

My music playing code is based on this example and I think that the RenderTone method is run in a different thread. I want to move my 1-pixel view 1 pixel every time 1000 samples have been played (just an example, could also be more pr less) but I don't know how to message the UI and send it updates on how many samples have been played.

So while a variation of the code below is being run I have to somehow update my UI.

OSStatus RenderTone(
    void *inRefCon, 
    AudioUnitRenderActionFlags *ioActionFlags, 
    const AudioTimeStamp *inTimeStamp, 
    UInt32 inBusNumber, 
    UInt32 inNumberFrames, 
    AudioBufferList *ioData)

{
    // Fixed amplitude is good enough for our purposes
    const double amplitude = 0.25;

    // Get the tone parameters out of the view controller
    ToneGeneratorViewController *viewController =
        (ToneGeneratorViewController *)inRefCon;
    double theta = viewController->theta;
    double theta_increment =
        2.0 * M_PI * viewController->frequency / viewController->sampleRate;

    // This is a mono tone generator so we only need the first buffer
    const int channel = 0;
    Float32 *buffer = (Float32 *)ioData->mBuffers[channel].mData;

    // Generate the samples
    for (UInt32 frame = 0; frame < inNumberFrames; frame++) 
    {
        buffer[frame] = sin(theta) * amplitude;

        theta += theta_increment;
        if (theta > 2.0 * M_PI)
        {
            theta -= 2.0 * M_PI;
        }
    }

    // Store the updated theta back in the view controller
    viewController->theta = theta;

    return noErr;
}

Upvotes: 1

Views: 602

Answers (2)

hotpaw2
hotpaw2

Reputation: 70733

On iOS devices, the UI can only update with respect to the display's 60 Hz refresh rate. So polling at the display refresh rate is a reasonable alternative, and does not risk doing something inappropriate that violates real-time constraints inside the audio unit callback thread (such as any Obj C messaging that potentially involves memory management.)

You can request the audio session buffer duration to be something around 1/60th of a second or less. Then have the audio unit buffer callback set or increment some audio sample position counter (make it 1 aligned long int variable so that this write is atomic).

Then, in the UI run loop, poll this audio position counter at some rate that is 60 times per second (frame sync), or less, depending on the UI responsiveness required. Calculate the music position from the audio sample position, and update the UI if the music position indicates a new note is being played, or is about to be played.

Upvotes: 1

David Hoerl
David Hoerl

Reputation: 41652

I would suggest that as various times in this code you use a simple block back to the main thread:

dispatch_async(dispatch_get_main_queue(), ^{ someViewController makeSomeChange:withParam; } );

You can send those infrequently. You can run your RenderTone code in the highPriority queue so the mainQueue is only run when you have no essential work to do.

Upvotes: 1

Related Questions