playback client

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

playback client

Doug McLain
Attatched is a simple libsndfile compatable audio file playback client,
based on capture_client.c.  I think it would be a nice addition to
example-clients. Capture client provides all of the same principles
obviously, but I think a lot of new jack users/developers/hackers  like
the idea of seeing jack in action playing back their favorite song via a
simple, single file client.  I'm using it extensively, sticking in
little code chunks all over the place to study the inner workings of
jack, setting up back n forth transfers with capture client, etc.

Also, I wonder if anyone can explain this behavior I'm seeing.  I play a
stereo wav file, and if I let it play to EOF, then the next time I play
it all is fine.  If I interrupt it mid stream via ctrl-c, then upon re
playing it, i get a small chunk of sound before the song starts again.
In my copy of playback_client.c I have added a signal handler that
cancels and joins the disk thread, then proceeds to cleanup everything
(close the file, the jack client; free the ringbuffer) just like main
does at end of file, but yet still a small chirp of audio if i don't let
it finish.

Doug
--
http://nostar.net/

/*
    Copyright (C) 2001 Paul Davis
    Copyright (C) 2003 Jack O'Quin
    Copyright (C) 2005 Doug McLain
   
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    * 2002/08/23 - modify for libsndfile 1.0.0 <[hidden email]>
    * 2003/05/26 - use ringbuffers - joq
    * 2005/05/19 - used capture_client as a template for playback_client
   
    $Id: playback_client.c,v 1.2 2004/06/04 07:32:50 nostar Exp $
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sndfile.h>
#include <pthread.h>
#include <getopt.h>
#include <jack/jack.h>
#include <jack/ringbuffer.h>

#include <sys/types.h>
#include <unistd.h>
#include <signal.h>

typedef struct _thread_info {
    pthread_t thread_id;
    SNDFILE *sf;
    jack_nframes_t duration;
    jack_nframes_t rb_size;
    jack_client_t *client;
    unsigned int channels;
    int bitdepth;
    char *path;
    volatile int can_playback;
    volatile int can_process;
    volatile int status;
} jack_thread_info_t;

/* JACK data */

jack_client_t *client;
unsigned int nports;
jack_port_t **ports;
jack_default_audio_sample_t **out;
jack_nframes_t nframes;
const size_t sample_size = sizeof(jack_default_audio_sample_t);

/* Synchronization between process thread and disk thread. */
#define DEFAULT_RB_SIZE 16384 /* ringbuffer size in frames */
jack_ringbuffer_t *rb;
pthread_mutex_t disk_thread_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  data_ready = PTHREAD_COND_INITIALIZER;
pthread_cond_t  xrun_count = PTHREAD_COND_INITIALIZER;
long underruns = 0;


void* disk_thread (void *arg)
{
        jack_thread_info_t *info = (jack_thread_info_t *) arg;
        static jack_nframes_t total_read = 0;
        jack_nframes_t samples_per_frame = info->channels;
        size_t bytes_per_frame = samples_per_frame * sample_size;
        void *framebuf = malloc (bytes_per_frame * info->rb_size);
        size_t write_frames, frames_read;
        long last_underrun_count = 0;

        pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
        pthread_mutex_lock (&disk_thread_lock);

        info->status = 0;

        while (1) {
                if (underruns > last_underrun_count)
                        printf("Ringbuffer underrun #%ld\n", underruns);
                last_underrun_count = underruns;
                       
                write_frames = (jack_ringbuffer_write_space (rb) / bytes_per_frame);
                       
                if ((frames_read = sf_readf_float (info->sf, framebuf, write_frames)) < write_frames) {
                        printf ("End of file\n");
                        break;
                }

                total_read += frames_read;
                jack_ringbuffer_write (rb, framebuf, write_frames * bytes_per_frame);
               
                /* The file is open, it's the first time thru, and the ringbuffer
                   is full, so give the green light to process() */
                if (!info->can_process)
                        info->can_process = 1;
                /* wait until process() signals more data */
                pthread_cond_wait (&data_ready, &disk_thread_lock);
        }

        pthread_mutex_unlock (&disk_thread_lock);
        free (framebuf);
        return 0;
}
       
int process (jack_nframes_t nframes, void *arg)
{
        int chn;
        size_t i;
        jack_thread_info_t *info = (jack_thread_info_t *) arg;

        /* Do nothing until we're ready to begin. */
        if (!info->can_process)
                return 0;
               
       
        for (chn = 0; chn < nports; chn++)
                out[chn] = jack_port_get_buffer (ports[chn], nframes);

        for (i = 0; i < nframes; i++) {
                for (chn = 0; chn < nports; chn++) {
                        if (jack_ringbuffer_read (rb, (void *) (out[chn]+i), sample_size)  < sample_size)
                                underruns++;
                }
        }
       
        /* Tell the disk thread there is work to do.  If it is already
         * running, the lock will not be available.  We can't wait
         * here in the process() thread, but we don't need to signal
         * in that case, because the disk thread will read all the
         * data queued before waiting again. */
        if (pthread_mutex_trylock (&disk_thread_lock) == 0) {
            pthread_cond_signal (&data_ready);
            pthread_mutex_unlock (&disk_thread_lock);
        }
        return 0;
}

void jack_shutdown (void *arg)
{
        fprintf (stderr, "JACK shutdown\n");
        // exit (0);
        abort();
}

void setup_disk_thread (jack_thread_info_t *info)
{
        SF_INFO sf_info;
        sf_info.format = 0;
        int length;

        if ((info->sf = sf_open (info->path, SFM_READ, &sf_info)) == NULL) {
                char errstr[256];
                sf_error_str (0, errstr, sizeof (errstr) - 1);
                fprintf (stderr, "cannot open sndfile \"%s\" for input (%s)\n", info->path, errstr);
                jack_client_close (info->client);
                exit (1);
        }
        length = sf_info.frames / sf_info.samplerate;
        info->channels = sf_info.channels;
        info->duration = sf_info.frames;
       
        printf("frames:\t\t\t%d\nsamplerate:\t\t%dHz\nchannels:\t\t%d\nlength:\t\t\t%d seconds\n",(int)sf_info.frames, sf_info.samplerate, sf_info.channels, length );

        pthread_create (&info->thread_id, NULL, disk_thread, info);
}

void run_disk_thread (jack_thread_info_t *info)
{
        pthread_join (info->thread_id, NULL);
        sf_close (info->sf);
       
        if (underruns > 0) {
                fprintf (stderr,
                         "jackrec failed with %ld underruns.\n", underruns);
                fprintf (stderr, " try a bigger buffer than -B %"
                         PRIu32 ".\n", info->rb_size);
                info->status = EPIPE;
        }
}

void setup_ports (int sources, char *source_names[], jack_thread_info_t *info)
{
        unsigned int i;
        size_t out_size;

        /* Allocate data structures that depend on the number of ports. */
        nports = sources;
        ports = (jack_port_t **) malloc (sizeof (jack_port_t *) * nports);
        out_size =  nports * sizeof (jack_default_audio_sample_t *);
        out = (jack_default_audio_sample_t **) malloc (out_size);
        rb = jack_ringbuffer_create (nports * sample_size * info->rb_size);
        printf ("ringbuffer size:\t%d bytes\n", rb->size);
       
        /* When JACK is running realtime, jack_activate() will have
         * called mlockall() to lock our pages into memory.  But, we
         * still need to touch any newly allocated pages before
         * process() starts using them.  Otherwise, a page fault could
         * create a delay that would force JACK to shut us down. */
        memset(out, 0, out_size);
        memset(rb->buf, 0, rb->size);

        for (i = 0; i < nports; i++) {
                char name[64];

                sprintf (name, "input%d", i+1);

                if ((ports[i] = jack_port_register (info->client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == 0) {
                        fprintf (stderr, "cannot register output port \"%s\"!\n", name);
                        jack_client_close (info->client);
                        exit (1);
                }
        }

        for (i = 0; i < nports; i++) {
                if (jack_connect (info->client, jack_port_name (ports[i]), source_names[i])) {
                        fprintf (stderr, "cannot connect input port %s to %s\n", source_names[i], jack_port_name (ports[i]));
                        jack_client_close (info->client);
                        exit (1);
                }
        }
}

int main (int argc, char *argv[])
{
        jack_thread_info_t thread_info;
        int c;
        int longopt_index = 0;
        extern int optind, opterr;
        int show_usage = 0;
        char *optstring = "f:b:B:h";
        struct option long_options[] = {
                { "help", 0, 0, 'h' },
                { "file", 1, 0, 'f' },
                { "bitdepth", 1, 0, 'b' },
                { "bufsize", 1, 0, 'B' },
                { 0, 0, 0, 0 }
        };
       
        memset (&thread_info, 0, sizeof (thread_info));
        thread_info.rb_size = DEFAULT_RB_SIZE;
        opterr = 0;

        while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
                switch (c) {
                case 1:
                        /* getopt signals end of '-' options */
                        break;

                case 'h':
                        show_usage++;
                        break;
                case 'f':
                        thread_info.path = optarg;
                        break;
                case 'B':
                        thread_info.rb_size = atoi (optarg);
                        break;
                default:
                        fprintf (stderr, "error\n");
                        show_usage++;
                        break;
                }
        }

        if (show_usage || thread_info.path == NULL || optind == argc) {
                fprintf (stderr, "usage: jackplay -f filename [ -b bitdepth ] [ -B bufsize ] port1 [ port2 ... ]\n");
                exit (1);
        }

        if ((client = jack_client_open ("jplay", JackNullOption, NULL)) == 0) {
                fprintf (stderr, "jack server not running?\n");
                exit (1);
        }

        thread_info.client = client;
        thread_info.can_process = 0;

        jack_set_process_callback (client, process, &thread_info);
        jack_on_shutdown (client, jack_shutdown, &thread_info);

        if (jack_activate (client)) {
                fprintf (stderr, "cannot activate client");
        }

        setup_ports (argc - optind, &argv[optind], &thread_info);

        setup_disk_thread (&thread_info);
       
        run_disk_thread (&thread_info);
       
        jack_client_close (client);

        jack_ringbuffer_free (rb);

        exit (0);
}
Reply | Threaded
Open this post in threaded view
|

Re: playback client

Knute Knudsen
Doug McLain wrote:

>
> Also, I wonder if anyone can explain this behavior I'm seeing.  I play a
> stereo wav file, and if I let it play to EOF, then the next time I play
> it all is fine.  If I interrupt it mid stream via ctrl-c, then upon re
> playing it, i get a small chunk of sound before the song starts again.
> In my copy of playback_client.c I have added a signal handler that
> cancels and joins the disk thread, then proceeds to cleanup everything
> (close the file, the jack client; free the ringbuffer) just like main
> does at end of file, but yet still a small chirp of audio if i don't let
> it finish.
>


I'm thinking that in your process() function, try filling the input
buffer with zeros then returning instead of simply returning when
can_process is false. You don't know what's in that buffer from the last
time you ran. Just an idea.


-------------------------------------------------------
This SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first software developer in space?
Enter now for the Oracle Space Sweepstakes!
http://ads.osdn.com/?ad_id=7412&alloc_id=16344&op=click
_______________________________________________
Jackit-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jackit-devel