Question: Does anyone have a good example of using CARingBuffer to buffer a large audio file and how to read it in a callback?

Should it be reading the audio file in a secondary thread? How do I pause loading the audio file until the loaded buffers have been played (how do I pre-queue the audio file)? CAPlayThrough seems close but is only streaming audio from a microphone.

Thanks!

Answer: 
You can find an example that uses this ring buffer if you download the example code of the [book](https://rads.stackoverflow.com/amzn/click/com/0321636848) *Learning Core Audio* [here](http://www.informit.com/store/learning-core-audio-a-hands-on-guide-to-audio-programming-9780321636843) (under the *downloads* tab). Jump to the chapter 8 example in a folder called CH08\_AUGraphInput.

However, if you are simply reading audio from a file, then using an (extra) ring buffer seems like an overkill.. A ring buffer comes in handy when you are having real time (or near real time) input and output (read chapter 8 in the said book for a more detailed explanation of when a ring buffer is necessary.. note that the example in chapter 8 is about playing audio immediately after recording it by a mic, which isn't what you want to do). 

The reason why I said extra ring buffer, is because in core Audio there is already an audio Queue (which can be thought of as a ring buffer.. or at least it in your case it replaces the need for a ring buffer: you populate it with data, it plays the data, then fires a callback that informs you that the data you supplied has been played). The apple [documentation](https://stackoverflow.com/a/5476017/766570) offers a good explanation on this one.

In your case, if you are simply reading audio from a file, then you can easily control the throughput of the audio from the file. You can pause it by blocking the thread that reads data from the audio file for example.

For a simple example of what I'm talking about, see this [example](https://github.com/abbood/audioSamplingToAudioQueue) I created on github. For a more advanced example, see Matt Gallagher's famous [example](https://github.com/abbood/audioSamplingToAudioQueue).
Generally for audio playback anything that can block or take an unbounded amount of time (in particular file or disk IO) should be done in a secondary thread. So you want to read the audio file's data in a producer thread, and consume the data in your IOProc or RemoteIO callback.

Synchronization becomes an issue with multiple threads, but if you have only one reader and one writer generally it isn't too hard. In fact, CARingBuffer is thread safe for this case.

The general flow should look like:

From the main thread:

1. Create the producer thread
2. Tell it which file to process

From the producer thread:

1. Open the specified file
2. Fill the empty space in the ring buffer with audio data
3. Wait until signaled or a timeout happens, and go back to #2

In your IOProc/callback:

1. Read data from the ring buffer
2. Signal the producer that more data is needed

Posting code to do this here would be much too long to read, but here are a few pointers to get you started. None of these are for the iPhone, but the principles are the same.

* <https://github.com/sbooth/SFBAudioEngine/blob/master/Player/AudioPlayer.cpp>
* <http://www.snoize.com/>