2006-10-18 04:49:13 +02:00
|
|
|
/* jack plug in for the Music Player Daemon (MPD)
|
2007-04-09 22:18:11 +02:00
|
|
|
* (c)2006 by anarch(anarchsss@gmail.com)
|
2006-10-18 04:49:13 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2008-09-07 22:41:17 +02:00
|
|
|
#include "../output_api.h"
|
|
|
|
#include "../utils.h"
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2008-11-01 14:04:14 +01:00
|
|
|
#include <glib.h>
|
2006-10-18 04:49:13 +02:00
|
|
|
#include <jack/jack.h>
|
|
|
|
#include <jack/types.h>
|
|
|
|
#include <jack/ringbuffer.h>
|
|
|
|
|
2009-01-02 17:23:10 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2008-12-29 17:29:36 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "jack"
|
|
|
|
|
2008-08-26 08:27:16 +02:00
|
|
|
static const size_t sample_size = sizeof(jack_default_audio_sample_t);
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
struct jack_data {
|
2008-09-24 07:20:55 +02:00
|
|
|
struct audio_output *ao;
|
|
|
|
|
2008-08-26 08:27:16 +02:00
|
|
|
/* configuration */
|
2009-01-02 17:23:00 +01:00
|
|
|
char *output_ports[2];
|
2008-10-24 17:29:37 +02:00
|
|
|
int ringbuffer_size;
|
2008-08-26 08:27:16 +02:00
|
|
|
|
2008-09-24 07:20:36 +02:00
|
|
|
/* for srate() only */
|
|
|
|
struct audio_format *audio_format;
|
|
|
|
|
2008-08-26 08:27:16 +02:00
|
|
|
/* jack library stuff */
|
2006-10-18 04:49:13 +02:00
|
|
|
jack_port_t *ports[2];
|
|
|
|
jack_client_t *client;
|
|
|
|
jack_ringbuffer_t *ringbuffer[2];
|
|
|
|
int bps;
|
|
|
|
int shutdown;
|
2008-10-24 17:29:37 +02:00
|
|
|
};
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2008-11-01 14:04:14 +01:00
|
|
|
static const char *
|
|
|
|
mpd_jack_name(const struct jack_data *jd)
|
|
|
|
{
|
2009-01-18 19:17:52 +01:00
|
|
|
return audio_output_get_name(jd->ao);
|
2008-11-01 14:04:14 +01:00
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static struct jack_data *
|
|
|
|
mpd_jack_new(void)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2009-01-02 17:23:10 +01:00
|
|
|
struct jack_data *ret = g_new(struct jack_data, 1);
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
ret->ringbuffer_size = 32768;
|
2008-08-26 08:27:16 +02:00
|
|
|
|
2006-10-18 04:49:13 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static void
|
|
|
|
mpd_jack_client_free(struct jack_data *jd)
|
2008-08-26 08:27:15 +02:00
|
|
|
{
|
|
|
|
assert(jd != NULL);
|
|
|
|
|
|
|
|
if (jd->client != NULL) {
|
|
|
|
jack_deactivate(jd->client);
|
|
|
|
jack_client_close(jd->client);
|
|
|
|
jd->client = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jd->ringbuffer[0] != NULL) {
|
|
|
|
jack_ringbuffer_free(jd->ringbuffer[0]);
|
|
|
|
jd->ringbuffer[0] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jd->ringbuffer[1] != NULL) {
|
|
|
|
jack_ringbuffer_free(jd->ringbuffer[1]);
|
|
|
|
jd->ringbuffer[1] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static void
|
|
|
|
mpd_jack_free(struct jack_data *jd)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2008-08-26 08:27:15 +02:00
|
|
|
assert(jd != NULL);
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
mpd_jack_client_free(jd);
|
2008-08-26 08:27:16 +02:00
|
|
|
|
2009-01-02 17:23:10 +01:00
|
|
|
for (unsigned i = 0; i < G_N_ELEMENTS(jd->output_ports); ++i)
|
|
|
|
g_free(jd->output_ports[i]);
|
2008-08-26 08:27:16 +02:00
|
|
|
|
2008-08-26 08:27:15 +02:00
|
|
|
free(jd);
|
2007-01-20 22:04:32 +01:00
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static void
|
|
|
|
mpd_jack_finish(void *data)
|
2007-01-20 22:04:32 +01:00
|
|
|
{
|
2008-10-24 17:29:37 +02:00
|
|
|
struct jack_data *jd = data;
|
|
|
|
mpd_jack_free(jd);
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static int
|
2009-01-01 18:08:29 +01:00
|
|
|
mpd_jack_srate(G_GNUC_UNUSED jack_nframes_t rate, void *data)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2008-10-24 17:29:37 +02:00
|
|
|
struct jack_data *jd = (struct jack_data *)data;
|
2008-09-24 07:20:36 +02:00
|
|
|
struct audio_format *audioFormat = jd->audio_format;
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2008-10-10 14:40:54 +02:00
|
|
|
audioFormat->sample_rate = (int)jack_get_sample_rate(jd->client);
|
2006-10-18 04:49:13 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static int
|
|
|
|
mpd_jack_process(jack_nframes_t nframes, void *arg)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2008-10-24 17:29:37 +02:00
|
|
|
struct jack_data *jd = (struct jack_data *) arg;
|
2008-10-24 16:55:51 +02:00
|
|
|
jack_default_audio_sample_t *out;
|
2008-10-24 16:56:08 +02:00
|
|
|
size_t available;
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2008-10-24 08:44:40 +02:00
|
|
|
if (nframes <= 0)
|
2006-10-18 04:49:13 +02:00
|
|
|
return 0;
|
|
|
|
|
2008-10-24 16:55:51 +02:00
|
|
|
for (unsigned i = 0; i < 2; ++i) {
|
2008-10-24 16:56:08 +02:00
|
|
|
available = jack_ringbuffer_read_space(jd->ringbuffer[i]);
|
2008-10-24 16:56:10 +02:00
|
|
|
assert(available % sample_size == 0);
|
2008-10-24 16:56:08 +02:00
|
|
|
available /= sample_size;
|
|
|
|
if (available > nframes)
|
|
|
|
available = nframes;
|
2007-03-23 12:07:04 +01:00
|
|
|
|
2008-10-24 16:55:51 +02:00
|
|
|
out = jack_port_get_buffer(jd->ports[i], nframes);
|
|
|
|
jack_ringbuffer_read(jd->ringbuffer[i],
|
2008-10-24 16:56:08 +02:00
|
|
|
(char *)out, available * sample_size);
|
2007-03-23 12:07:04 +01:00
|
|
|
|
2008-10-24 16:56:08 +02:00
|
|
|
while (available < nframes)
|
2008-10-24 16:55:51 +02:00
|
|
|
/* ringbuffer underrun, fill with silence */
|
2008-10-24 16:56:08 +02:00
|
|
|
out[available++] = 0.0;
|
2007-04-09 22:18:11 +02:00
|
|
|
}
|
|
|
|
|
2006-10-18 04:49:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static void
|
|
|
|
mpd_jack_shutdown(void *arg)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2008-10-24 17:29:37 +02:00
|
|
|
struct jack_data *jd = (struct jack_data *) arg;
|
2006-10-18 04:49:13 +02:00
|
|
|
jd->shutdown = 1;
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static void
|
|
|
|
set_audioformat(struct jack_data *jd, struct audio_format *audio_format)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2008-10-24 17:29:37 +02:00
|
|
|
audio_format->sample_rate = jack_get_sample_rate(jd->client);
|
2008-12-29 17:29:36 +01:00
|
|
|
g_debug("samplerate = %u", audio_format->sample_rate);
|
2008-10-24 17:29:37 +02:00
|
|
|
audio_format->channels = 2;
|
2008-10-24 17:36:11 +02:00
|
|
|
|
|
|
|
if (audio_format->bits != 16 && audio_format->bits != 24)
|
|
|
|
audio_format->bits = 24;
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
jd->bps = audio_format->channels
|
2007-03-23 22:23:41 +01:00
|
|
|
* sizeof(jack_default_audio_sample_t)
|
2008-10-24 17:29:37 +02:00
|
|
|
* audio_format->sample_rate;
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static void
|
|
|
|
mpd_jack_error(const char *msg)
|
2006-12-30 01:14:45 +01:00
|
|
|
{
|
2008-12-29 17:29:36 +01:00
|
|
|
g_warning("%s", msg);
|
2006-12-30 01:14:45 +01:00
|
|
|
}
|
|
|
|
|
2008-10-24 08:44:40 +02:00
|
|
|
static void *
|
2008-10-24 17:29:37 +02:00
|
|
|
mpd_jack_init(struct audio_output *ao,
|
2009-01-01 18:08:29 +01:00
|
|
|
G_GNUC_UNUSED const struct audio_format *audio_format,
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_param *param)
|
2006-12-30 01:14:45 +01:00
|
|
|
{
|
2008-10-24 17:29:37 +02:00
|
|
|
struct jack_data *jd;
|
2009-01-17 20:23:27 +01:00
|
|
|
struct block_param *bp;
|
2006-12-30 01:14:45 +01:00
|
|
|
char *endptr;
|
|
|
|
int val;
|
2008-06-13 09:39:11 +02:00
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
jd = mpd_jack_new();
|
2008-09-24 07:20:55 +02:00
|
|
|
jd->ao = ao;
|
2006-12-30 01:14:45 +01:00
|
|
|
|
2008-12-29 17:29:36 +01:00
|
|
|
g_debug("mpd_jack_init (pid=%d)", getpid());
|
2008-09-24 07:20:55 +02:00
|
|
|
if (param == NULL)
|
|
|
|
return jd;
|
2006-12-30 01:14:45 +01:00
|
|
|
|
|
|
|
if ( (bp = getBlockParam(param, "ports")) ) {
|
2009-01-18 19:24:14 +01:00
|
|
|
char **ports = g_strsplit(bp->value, ",", 0);
|
2007-01-14 03:08:27 +01:00
|
|
|
|
2009-01-18 19:24:14 +01:00
|
|
|
if (ports[0] == NULL || ports[1] == NULL || ports[2] != NULL)
|
|
|
|
g_error("two port names expected in line %d",
|
|
|
|
bp->line);
|
2007-01-14 03:08:27 +01:00
|
|
|
|
2009-01-18 19:24:14 +01:00
|
|
|
jd->output_ports[0] = ports[0];
|
|
|
|
jd->output_ports[1] = ports[1];
|
2007-01-14 03:08:27 +01:00
|
|
|
|
2009-01-18 19:24:14 +01:00
|
|
|
g_free(ports);
|
2006-12-30 01:14:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( (bp = getBlockParam(param, "ringbuffer_size")) ) {
|
|
|
|
errno = 0;
|
|
|
|
val = strtol(bp->value, &endptr, 10);
|
|
|
|
|
|
|
|
if ( errno == 0 && endptr != bp->value) {
|
2008-10-24 17:29:37 +02:00
|
|
|
jd->ringbuffer_size = val < 32768 ? 32768 : val;
|
2008-12-29 17:29:36 +01:00
|
|
|
g_debug("ringbuffer_size=%d", jd->ringbuffer_size);
|
2006-12-30 01:14:45 +01:00
|
|
|
} else {
|
2008-12-29 17:29:36 +01:00
|
|
|
g_error("%s is not a number; ringbuf_size=%d",
|
|
|
|
bp->value, jd->ringbuffer_size);
|
2006-12-30 01:14:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-24 07:20:55 +02:00
|
|
|
return jd;
|
2006-12-30 01:14:45 +01:00
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool
|
2008-10-24 17:29:37 +02:00
|
|
|
mpd_jack_test_default_device(void)
|
2006-12-30 01:14:45 +01:00
|
|
|
{
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2006-12-30 01:14:45 +01:00
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static int
|
|
|
|
mpd_jack_connect(struct jack_data *jd, struct audio_format *audio_format)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2008-09-07 19:14:39 +02:00
|
|
|
const char **jports;
|
2006-12-30 01:14:45 +01:00
|
|
|
char *port_name;
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2008-09-24 07:20:36 +02:00
|
|
|
jd->audio_format = audio_format;
|
|
|
|
|
2008-11-01 14:04:14 +01:00
|
|
|
if ((jd->client = jack_client_new(mpd_jack_name(jd))) == NULL) {
|
2008-12-29 17:29:36 +01:00
|
|
|
g_warning("jack server not running?");
|
2006-10-18 04:49:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
jack_set_error_function(mpd_jack_error);
|
|
|
|
jack_set_process_callback(jd->client, mpd_jack_process, jd);
|
|
|
|
jack_set_sample_rate_callback(jd->client, mpd_jack_srate, jd);
|
|
|
|
jack_on_shutdown(jd->client, mpd_jack_shutdown, jd);
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2006-12-30 01:14:45 +01:00
|
|
|
if ( jack_activate(jd->client) ) {
|
2008-12-29 17:29:36 +01:00
|
|
|
g_warning("cannot activate client");
|
2006-10-18 04:49:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-30 01:14:45 +01:00
|
|
|
jd->ports[0] = jack_port_register(jd->client, "left",
|
|
|
|
JACK_DEFAULT_AUDIO_TYPE,
|
|
|
|
JackPortIsOutput, 0);
|
2006-10-18 04:49:13 +02:00
|
|
|
if ( !jd->ports[0] ) {
|
2008-12-29 17:29:36 +01:00
|
|
|
g_warning("Cannot register left output port.");
|
2006-10-18 04:49:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-30 01:14:45 +01:00
|
|
|
jd->ports[1] = jack_port_register(jd->client, "right",
|
|
|
|
JACK_DEFAULT_AUDIO_TYPE,
|
|
|
|
JackPortIsOutput, 0);
|
2006-10-18 04:49:13 +02:00
|
|
|
if ( !jd->ports[1] ) {
|
2008-12-29 17:29:36 +01:00
|
|
|
g_warning("Cannot register right output port.");
|
2006-10-18 04:49:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hay que buscar que hay */
|
2008-10-24 08:44:40 +02:00
|
|
|
if (!jd->output_ports[1] &&
|
|
|
|
(jports = jack_get_ports(jd->client, NULL, NULL,
|
|
|
|
JackPortIsPhysical | JackPortIsInput))) {
|
2009-01-02 17:23:00 +01:00
|
|
|
jd->output_ports[0] = g_strdup(jports[0]);
|
|
|
|
jd->output_ports[1] = g_strdup(jports[1] != NULL
|
|
|
|
? jports[1] : jports[0]);
|
2008-12-29 17:29:36 +01:00
|
|
|
g_debug("output_ports: %s %s",
|
2008-08-26 08:27:16 +02:00
|
|
|
jd->output_ports[0], jd->output_ports[1]);
|
2006-12-30 01:14:45 +01:00
|
|
|
free(jports);
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:16 +02:00
|
|
|
if ( jd->output_ports[1] ) {
|
2008-11-01 14:04:14 +01:00
|
|
|
const char *name = mpd_jack_name(jd);
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
jd->ringbuffer[0] = jack_ringbuffer_create(jd->ringbuffer_size);
|
|
|
|
jd->ringbuffer[1] = jack_ringbuffer_create(jd->ringbuffer_size);
|
2006-12-30 01:14:45 +01:00
|
|
|
memset(jd->ringbuffer[0]->buf, 0, jd->ringbuffer[0]->size);
|
|
|
|
memset(jd->ringbuffer[1]->buf, 0, jd->ringbuffer[1]->size);
|
|
|
|
|
2009-01-02 17:23:10 +01:00
|
|
|
port_name = g_malloc(sizeof(port_name[0]) * (7 + strlen(name)));
|
2006-12-30 01:14:45 +01:00
|
|
|
|
2008-11-01 14:04:14 +01:00
|
|
|
sprintf(port_name, "%s:left", name);
|
2007-01-20 22:04:32 +01:00
|
|
|
if ( (jack_connect(jd->client, port_name,
|
2008-08-26 08:27:16 +02:00
|
|
|
jd->output_ports[0])) != 0 ) {
|
2008-12-29 17:29:36 +01:00
|
|
|
g_warning("%s is not a valid Jack Client / Port",
|
|
|
|
jd->output_ports[0]);
|
2006-12-30 01:14:45 +01:00
|
|
|
free(port_name);
|
2006-10-18 04:49:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-11-01 14:04:14 +01:00
|
|
|
sprintf(port_name, "%s:right", name);
|
2007-01-20 22:04:32 +01:00
|
|
|
if ( (jack_connect(jd->client, port_name,
|
2008-08-26 08:27:16 +02:00
|
|
|
jd->output_ports[1])) != 0 ) {
|
2008-12-29 17:29:36 +01:00
|
|
|
g_warning("%s is not a valid Jack Client / Port",
|
|
|
|
jd->output_ports[1]);
|
2006-12-30 01:14:45 +01:00
|
|
|
free(port_name);
|
2006-10-18 04:49:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-12-30 01:14:45 +01:00
|
|
|
free(port_name);
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool
|
2008-10-24 17:29:37 +02:00
|
|
|
mpd_jack_open(void *data, struct audio_format *audio_format)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2008-10-24 17:29:37 +02:00
|
|
|
struct jack_data *jd = data;
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2008-08-26 08:27:15 +02:00
|
|
|
assert(jd != NULL);
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
if (jd->client == NULL && mpd_jack_connect(jd, audio_format) < 0) {
|
|
|
|
mpd_jack_client_free(jd);
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2008-09-24 07:20:55 +02:00
|
|
|
set_audioformat(jd, audio_format);
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static void
|
2009-01-01 18:08:29 +01:00
|
|
|
mpd_jack_close(G_GNUC_UNUSED void *data)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2008-10-24 17:29:37 +02:00
|
|
|
/*mpd_jack_finish(audioOutput);*/
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static void
|
2009-01-01 18:08:29 +01:00
|
|
|
mpd_jack_cancel (G_GNUC_UNUSED void *data)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:34:47 +02:00
|
|
|
static inline jack_default_audio_sample_t
|
|
|
|
sample_16_to_jack(int16_t sample)
|
|
|
|
{
|
|
|
|
return sample / (jack_default_audio_sample_t)(1 << (16 - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpd_jack_write_samples_16(struct jack_data *jd, const int16_t *src,
|
|
|
|
unsigned num_samples)
|
|
|
|
{
|
|
|
|
jack_default_audio_sample_t sample;
|
|
|
|
|
|
|
|
while (num_samples-- > 0) {
|
|
|
|
sample = sample_16_to_jack(*src++);
|
|
|
|
jack_ringbuffer_write(jd->ringbuffer[0], (void*)&sample,
|
|
|
|
sizeof(sample));
|
|
|
|
|
|
|
|
sample = sample_16_to_jack(*src++);
|
|
|
|
jack_ringbuffer_write(jd->ringbuffer[1], (void*)&sample,
|
|
|
|
sizeof(sample));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:36:11 +02:00
|
|
|
static inline jack_default_audio_sample_t
|
|
|
|
sample_24_to_jack(int32_t sample)
|
|
|
|
{
|
|
|
|
return sample / (jack_default_audio_sample_t)(1 << (24 - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpd_jack_write_samples_24(struct jack_data *jd, const int32_t *src,
|
|
|
|
unsigned num_samples)
|
|
|
|
{
|
|
|
|
jack_default_audio_sample_t sample;
|
|
|
|
|
|
|
|
while (num_samples-- > 0) {
|
|
|
|
sample = sample_24_to_jack(*src++);
|
|
|
|
jack_ringbuffer_write(jd->ringbuffer[0], (void*)&sample,
|
|
|
|
sizeof(sample));
|
|
|
|
|
|
|
|
sample = sample_24_to_jack(*src++);
|
|
|
|
jack_ringbuffer_write(jd->ringbuffer[1], (void*)&sample,
|
|
|
|
sizeof(sample));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:34:47 +02:00
|
|
|
static void
|
|
|
|
mpd_jack_write_samples(struct jack_data *jd, const void *src,
|
|
|
|
unsigned num_samples)
|
|
|
|
{
|
|
|
|
switch (jd->audio_format->bits) {
|
|
|
|
case 16:
|
|
|
|
mpd_jack_write_samples_16(jd, (const int16_t*)src,
|
|
|
|
num_samples);
|
|
|
|
break;
|
|
|
|
|
2008-10-24 17:36:11 +02:00
|
|
|
case 24:
|
|
|
|
mpd_jack_write_samples_24(jd, (const int32_t*)src,
|
|
|
|
num_samples);
|
|
|
|
break;
|
|
|
|
|
2008-10-24 17:34:47 +02:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool
|
2008-10-24 17:29:37 +02:00
|
|
|
mpd_jack_play(void *data, const char *buff, size_t size)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2008-10-24 17:29:37 +02:00
|
|
|
struct jack_data *jd = data;
|
2008-10-24 17:34:47 +02:00
|
|
|
const size_t frame_size = audio_format_frame_size(jd->audio_format);
|
2008-10-24 16:39:43 +02:00
|
|
|
size_t space, space1;
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2008-10-24 08:44:40 +02:00
|
|
|
if (jd->shutdown) {
|
2008-12-29 17:29:36 +01:00
|
|
|
g_warning("Refusing to play, because there is no client thread.");
|
2008-10-24 17:29:37 +02:00
|
|
|
mpd_jack_client_free(jd);
|
2008-09-24 07:20:55 +02:00
|
|
|
audio_output_closed(jd->ao);
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2008-10-24 16:56:10 +02:00
|
|
|
assert(size % frame_size == 0);
|
2008-10-24 16:39:43 +02:00
|
|
|
size /= frame_size;
|
|
|
|
while (size > 0 && !jd->shutdown) {
|
2008-10-24 08:44:40 +02:00
|
|
|
space = jack_ringbuffer_write_space(jd->ringbuffer[0]);
|
2008-10-24 16:39:43 +02:00
|
|
|
space1 = jack_ringbuffer_write_space(jd->ringbuffer[1]);
|
|
|
|
if (space > space1)
|
|
|
|
/* send data symmetrically */
|
|
|
|
space = space1;
|
|
|
|
|
|
|
|
space /= sample_size;
|
|
|
|
if (space > 0) {
|
|
|
|
if (space > size)
|
|
|
|
space = size;
|
|
|
|
|
2008-10-24 17:34:47 +02:00
|
|
|
mpd_jack_write_samples(jd, buff, space);
|
2007-03-23 22:23:41 +01:00
|
|
|
|
2008-10-24 17:34:47 +02:00
|
|
|
buff += space * frame_size;
|
|
|
|
size -= space;
|
2008-10-24 08:44:40 +02:00
|
|
|
} else {
|
2008-10-24 16:55:51 +02:00
|
|
|
/* XXX do something more intelligent to
|
|
|
|
synchronize */
|
|
|
|
my_usleep(10000);
|
2007-04-11 19:41:10 +02:00
|
|
|
}
|
2006-12-30 01:14:45 +01:00
|
|
|
|
2007-04-09 22:18:11 +02:00
|
|
|
}
|
2008-10-24 08:44:40 +02:00
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2008-09-08 11:43:38 +02:00
|
|
|
const struct audio_output_plugin jackPlugin = {
|
2008-09-29 15:55:17 +02:00
|
|
|
.name = "jack",
|
2008-10-24 17:29:37 +02:00
|
|
|
.test_default_device = mpd_jack_test_default_device,
|
|
|
|
.init = mpd_jack_init,
|
|
|
|
.finish = mpd_jack_finish,
|
|
|
|
.open = mpd_jack_open,
|
|
|
|
.play = mpd_jack_play,
|
|
|
|
.cancel = mpd_jack_cancel,
|
|
|
|
.close = mpd_jack_close,
|
2006-10-18 04:49:13 +02:00
|
|
|
};
|