Wednesday, November 16, 2011

Tutorial: JACK Ringbuffers

Hey All,

A quick tutorial on basic Jack ringbuffer usage. Ringbuffers are an easy way to exchange data from one thread to another in a realtime safe way. This means that no thread will block when reading or writing, and hence you use ringbuffers in a real-time thread.

For this tutorial I'll be using the "standard" linux audio ringbuffer that comes with JACK. Its docs are available here:

So what were going to do is:
1. Setup a ring buffer
2. Register a JACK client, and give it a process callback
3. Write data in the "local" thread, ie: our main()
4. Make JACK print out any data it recieves in its RT thread

Note that here the JACK thread is our READ thread , and the main() thread is the WRITE thread. This is important, because a ringbuffer like the JACK one will only work in ONE direction.

Find the well commented source here:

Note the compile command is: g++ main.cpp `pkg-config --cflags --libs jack` (its also in the source.. but just to make sure :)

Responses welcome, -Harry

PS: Note there are many different implementations of ringbuffers, all with benefits of their own. The JACK ringbuffer is a simple and IMO easily usable one, and that's the reason I like it. I don't want a fancy impossible multi-read multi-write templated 13 class derived special oval ringbuffer, just something that does what it says on the tin :)