The short answer:

#include <jack/jack.h>
#include <jack/ringbuffer.h>
#include <semaphore.h>

sem_t block_wait;
jack_ringbuffer_t ringbuf;

init()
{
   int max_blocks = buffer_size_in_samples/jack_block_size
   sem_init(&block_wait, 0, max_blocks);
}

jack_process()
{
   float *out = jack_port_get_buffer(...);
   jack_ringbuffer_read(ringbuf, out, block_size*sizeof(float));
   sem_post(&block_wait);
}

int exit = 0;
worker_thread()
{
   while( !exit) {
       sem_wait(&block_wait);
       read_from_disk_to_ring_buffer()
   }
}

correct program should use jack_default_audio_sample_t instead of float
and should deal with buffer size changes (as well as sample rate) ;)

I'll try to write a working example tonight (using libsndfile of course).
Or take a look at examples/capture_client.c in jackd distribution. It
uses mutex/cond but the idea is the same (semaphores are better,
actually. Jack example needs a fix).

Regards,
Dmitry.

Reply via email to