123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- /*
- ** Copyright (C) 2014 Alexander Regueiro <alex@noldorin.com>
- ** Copyright (C) 2013 elboulangero <elboulangero@gmail.com>
- ** Copyright (C) 2007-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
- ** Copyright (C) 2007 Jonatan Liljedahl <lijon@kymatica.com>
- **
- ** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- /*#include "src/config.h"*/
- #include <stdbool.h>
- #include <stdio.h>
- #include <errno.h>
- #include <unistd.h>
- #include <getopt.h>
- #include <libgen.h>
- #include <stdlib.h>
- #include <string.h>
- #define HAVE_JACK 1
- #if HAVE_JACK
- #include <math.h>
- #include <pthread.h>
- #include <jack/jack.h>
- #include <jack/ringbuffer.h>
- #include <sndfile.h>
- #define RB_SIZE (1 << 16)
- #define SAMPLE_SIZE (sizeof (jack_default_audio_sample_t))
- #define NOT(x) (! (x))
- typedef struct
- { jack_client_t *client ;
- jack_ringbuffer_t *ringbuf ;
- jack_nframes_t pos ;
- jack_default_audio_sample_t ** outs ;
- jack_port_t ** output_port ;
- SNDFILE *sndfile ;
- unsigned int channels ;
- unsigned int samplerate ;
- volatile int can_process ;
- volatile int read_done ;
- volatile int play_done ;
- volatile unsigned int loop_count ;
- volatile unsigned int current_loop ;
- } thread_info_t ;
- static pthread_mutex_t disk_thread_lock = PTHREAD_MUTEX_INITIALIZER ;
- static pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER ;
- static int
- process_callback (jack_nframes_t nframes, void * arg)
- {
- thread_info_t *info = (thread_info_t *) arg ;
- jack_default_audio_sample_t buf [info->channels] ;
- unsigned i, n ;
- if (NOT (info->can_process))
- return 0 ;
- for (n = 0 ; n < info->channels ; n++)
- info->outs [n] = jack_port_get_buffer (info->output_port [n], nframes) ;
- for (i = 0 ; i < nframes ; i++)
- { size_t read_count ;
- /* Read one frame of audio. */
- read_count = jack_ringbuffer_read (info->ringbuf, (void *) buf, SAMPLE_SIZE * info->channels) ;
- if (read_count == 0 && info->read_done)
- { /* File is done, so stop the main loop. */
- info->play_done = 1 ;
- return 0 ;
- } ;
- /* Update play-position counter. */
- info->pos += read_count / (SAMPLE_SIZE * info->channels) ;
- /* Output each channel of the frame. */
- for (n = 0 ; n < info->channels ; n++)
- info->outs [n][i] = buf [n] ;
- } ;
- /* Wake up the disk thread to read more data. */
- if (pthread_mutex_trylock (&disk_thread_lock) == 0)
- { pthread_cond_signal (&data_ready) ;
- pthread_mutex_unlock (&disk_thread_lock) ;
- } ;
- return 0 ;
- } /* process_callback */
- static void *
- disk_thread (void *arg)
- { thread_info_t *info = (thread_info_t *) arg ;
- sf_count_t buf_avail, read_frames ;
- jack_ringbuffer_data_t vec [2] ;
- size_t bytes_per_frame = SAMPLE_SIZE * info->channels ;
- pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL) ;
- pthread_mutex_lock (&disk_thread_lock) ;
- while (1)
- { jack_ringbuffer_get_write_vector (info->ringbuf, vec) ;
- read_frames = 0 ;
- if (vec [0].len)
- { /* Fill the first part of the ringbuffer. */
- buf_avail = vec [0].len / bytes_per_frame ;
- read_frames = sf_readf_float (info->sndfile, (float *) vec [0].buf, buf_avail) ;
- if (vec [1].len)
- { /* Fill the second part of the ringbuffer? */
- buf_avail = vec [1].len / bytes_per_frame ;
- read_frames += sf_readf_float (info->sndfile, (float *) vec [1].buf, buf_avail) ;
- } ;
- } ;
- if (read_frames == 0)
- { info->current_loop ++ ;
- if (info->loop_count >= 1 && info->current_loop >= info->loop_count)
- break ; /* end of file? */
- sf_seek (info->sndfile, 0, SEEK_SET) ;
- }
- jack_ringbuffer_write_advance (info->ringbuf, read_frames * bytes_per_frame) ;
- /* Tell process_callback that we've filled the ringbuffer. */
- info->can_process = 1 ;
- /* Wait for the process_callback thread to wake us up. */
- pthread_cond_wait (&data_ready, &disk_thread_lock) ;
- } ;
- /* Tell that we're done reading the file. */
- info->read_done = 1 ;
- pthread_mutex_unlock (&disk_thread_lock) ;
- return NULL ;
- } /* disk_thread */
- static void
- jack_shutdown (void *arg)
- { (void) arg ;
- exit (1) ;
- } /* jack_shutdown */
- static inline void
- print_time (jack_nframes_t pos, int jack_sr)
- { float sec = pos / (1.0 * jack_sr) ;
- int min = sec / 60.0 ;
- fprintf (stderr, "%02d:%05.2f", min, fmod (sec, 60.0)) ;
- } /* print_time */
- static inline void
- print_status (const thread_info_t * info)
- {
- if (info->loop_count == 0)
- fprintf (stderr, "\r-> %6d ", info->current_loop) ;
- else if (info->loop_count > 1)
- fprintf (stderr, "\r-> %6d/%d ", info->current_loop, info->loop_count) ;
- else
- fprintf (stderr, "\r-> ") ;
- print_time (info->pos, info->samplerate) ;
- fflush (stdout) ;
- } /* print_status */
- static void
- usage_exit (char * argv0, int status)
- {
- printf ("\n"
- "Usage : %s [options] <input sound file>\n"
- "\n"
- " Where [options] is one of:\n"
- "\n"
- " -w --wait[=<port>] : Wait for input before starting playback; optionally auto-connect to <port> using Jack.\n"
- " -l --loop=<count> : Loop the file <count> times (0 for infinite).\n"
- " -h --help : Show this help message.\n"
- "\n"
- "Using %s.\n"
- "\n",
- basename (argv0), sf_version_string ()) ;
- exit (status) ;
- } /* usage_exit */
- static struct option const long_options [] =
- {
- { "wait", optional_argument, NULL, 'w' } ,
- { "loop", required_argument, NULL, 'l' } ,
- { "help", no_argument, NULL, 'h' } ,
- { NULL, 0, NULL, 0 }
- } ;
- int
- main (int argc, char * argv [])
- { pthread_t thread_id ;
- SNDFILE *sndfile ;
- SF_INFO sfinfo ;
- const char * filename ;
- jack_client_t *client ;
- jack_status_t status = 0 ;
- thread_info_t info ;
- char * auto_connect_str = "system:playback_%d" ;
- bool wait_before_play = false ;
- int i, jack_sr, loop_count = 1 ;
- int c ;
- /* Parse options */
- while ((c = getopt_long (argc, argv,
- "w::" /* --wait */
- "l:" /* --loop */
- "h", /* --help */
- long_options, NULL)) != EOF)
- { if (optarg != NULL && optarg [0] == '=')
- { optarg++ ;
- }
- switch (c)
- { case 'w' :
- wait_before_play = true ;
- auto_connect_str = optarg ;
- break ;
- case 'l' :
- loop_count = strtol (optarg, NULL, 10) ;
- break ;
- case 'h' :
- usage_exit (argv [0], EXIT_SUCCESS) ;
- break ;
- default :
- usage_exit (argv [0], EXIT_FAILURE) ;
- } ;
- }
- if (argc - optind != 1)
- usage_exit (argv [0], EXIT_FAILURE) ;
- filename = argv [optind] ;
- /* Create jack client */
- if ((client = jack_client_open ("jackplay", JackNullOption | JackNoStartServer, &status)) == 0)
- { if (status & JackServerFailed)
- fprintf (stderr, "Unable to connect to JACK server\n") ;
- else
- fprintf (stderr, "jack_client_open () failed, status = 0x%2.0x\n", status) ;
- exit (1) ;
- } ;
- if (status & JackServerStarted)
- fprintf (stderr, "JACK server started\n") ;
- if (status & JackNameNotUnique)
- { const char * client_name = jack_get_client_name (client) ;
- fprintf (stderr, "Unique name `%s' assigned\n", client_name) ;
- } ;
- /* Open the soundfile. */
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
- if (sndfile == NULL)
- { fprintf (stderr, "Could not open soundfile '%s'\n", filename) ;
- return 1 ;
- } ;
- fprintf (stderr, "Channels : %d\nSample rate : %d Hz\nDuration : ", sfinfo.channels, sfinfo.samplerate) ;
- print_time (loop_count * sfinfo.frames, sfinfo.samplerate) ;
- fprintf (stderr, "\n") ;
- if (loop_count < 1)
- fprintf (stderr, "Loop count : infinite\n") ;
- else if (loop_count > 1)
- fprintf (stderr, "Loop count : %d\n", loop_count) ;
- jack_sr = jack_get_sample_rate (client) ;
- if (sfinfo.samplerate != jack_sr)
- fprintf (stderr, "Warning: samplerate of soundfile (%d Hz) does not match jack server (%d Hz).\n", sfinfo.samplerate, jack_sr) ;
- /* Init the thread info struct. */
- memset (&info, 0, sizeof (info)) ;
- info.can_process = 0 ;
- info.read_done = 0 ;
- info.play_done = 0 ;
- info.sndfile = sndfile ;
- info.channels = sfinfo.channels ;
- info.samplerate = jack_sr ;
- info.client = client ;
- info.pos = 0 ;
- info.current_loop = 0 ;
- info.loop_count = loop_count ;
- /* Allocate output ports. */
- info.output_port = calloc (sfinfo.channels, sizeof (jack_port_t *)) ;
- info.outs = calloc (sfinfo.channels, sizeof (jack_default_audio_sample_t *)) ;
- for (i = 0 ; i < sfinfo.channels ; i++)
- { char name [16] ;
- snprintf (name, sizeof (name), "out_%d", i + 1) ;
- info.output_port [i] = jack_port_register (client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0) ;
- } ;
- /* Allocate and clear ringbuffer. */
- info.ringbuf = jack_ringbuffer_create (sizeof (jack_default_audio_sample_t) * RB_SIZE) ;
- memset (info.ringbuf->buf, 0, info.ringbuf->size) ;
- /* Set up callbacks. */
- jack_set_process_callback (client, process_callback, &info) ;
- jack_on_shutdown (client, jack_shutdown, 0) ;
- /* Activate client. */
- if (jack_activate (client))
- { fprintf (stderr, "Cannot activate client.\n") ;
- return 1 ;
- } ;
- if (auto_connect_str != NULL)
- { /* Auto-connect all channels. */
- for (i = 0 ; i < sfinfo.channels ; i++)
- { char name [64] ;
- snprintf (name, sizeof (name), auto_connect_str, i + 1) ;
- if (jack_connect (client, jack_port_name (info.output_port [i]), name))
- fprintf (stderr, "Cannot connect output port %d (%s).\n", i, name) ;
- } ;
- }
- if (wait_before_play)
- { /* Wait for key press before playing. */
- printf ("Press <ENTER> key to start playing...") ;
- getchar () ;
- }
- /* Start the disk thread. */
- pthread_create (&thread_id, NULL, disk_thread, &info) ;
- /* Sit in a loop, displaying the current play position. */
- while (NOT (info.play_done))
- { print_status (&info) ;
- usleep (10000) ;
- } ;
- pthread_join (thread_id, NULL) ;
- print_status (&info) ;
- /* Clean up. */
- for (i = 0 ; i < sfinfo.channels ; i++)
- jack_port_unregister (client, info.output_port [i]) ;
- jack_ringbuffer_free (info.ringbuf) ;
- jack_client_close (client) ;
- free (info.output_port) ;
- free (info.outs) ;
- sf_close (sndfile) ;
- puts ("") ;
- return 0 ;
- } /* main */
- #else
- int
- main (void)
- {
- puts (
- "Sorry this program was compiled without libjack (which probably\n"
- "only exists on Linux and Mac OSX) and hence doesn't work."
- ) ;
- return 0 ;
- } /* main */
- #endif
|