2007-03-17 03:14:41 +00:00
|
|
|
/*
|
2007-03-17 06:20:52 +00:00
|
|
|
snd_jack.c
|
2007-03-17 03:14:41 +00:00
|
|
|
|
2021-06-25 09:32:48 +00:00
|
|
|
JACK output driver
|
2007-03-17 03:14:41 +00:00
|
|
|
|
2021-06-25 09:32:48 +00:00
|
|
|
Copyright (C) 2007-2021 Bill Currie <bill@taniwha.org>
|
2007-03-17 03:14:41 +00: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:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
2007-03-17 06:20:52 +00:00
|
|
|
Boston, MA 02111-1307, USA
|
2007-03-17 03:14:41 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
2007-03-17 06:20:52 +00:00
|
|
|
#include <jack/jack.h>
|
2007-03-17 03:14:41 +00:00
|
|
|
|
2007-03-19 22:20:13 +00:00
|
|
|
#include "QF/cmd.h"
|
2007-03-17 03:14:41 +00:00
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/hash.h"
|
|
|
|
#include "QF/plugin.h"
|
|
|
|
#include "QF/sound.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/va.h"
|
|
|
|
|
2011-07-23 06:56:22 +00:00
|
|
|
#include "snd_internal.h"
|
2007-03-17 03:14:41 +00:00
|
|
|
|
2010-12-25 11:31:14 +00:00
|
|
|
static int sound_started = 0;
|
2007-03-17 03:14:41 +00:00
|
|
|
static int snd_blocked = 0;
|
2009-12-24 06:35:15 +00:00
|
|
|
static int snd_shutdown = 0;
|
2011-09-07 06:13:47 +00:00
|
|
|
static int snd_alive = 0;
|
|
|
|
static double snd_alive_time = 0;
|
2007-03-17 06:20:52 +00:00
|
|
|
static jack_client_t *jack_handle;
|
|
|
|
static jack_port_t *jack_out[2];
|
2007-03-17 07:05:24 +00:00
|
|
|
static float *output[2];
|
2007-07-03 10:30:13 +00:00
|
|
|
static cvar_t *snd_jack_server;
|
2021-06-29 11:03:13 +00:00
|
|
|
static cvar_t *snd_jack_ports;
|
2007-03-17 03:14:41 +00:00
|
|
|
|
2021-06-25 09:32:48 +00:00
|
|
|
static int s_jack_connect (snd_t *snd);
|
2010-08-05 07:20:50 +00:00
|
|
|
|
2009-12-24 06:35:15 +00:00
|
|
|
static void
|
2021-06-25 09:32:48 +00:00
|
|
|
s_update (snd_t *snd)
|
2009-12-24 06:35:15 +00:00
|
|
|
{
|
2011-09-07 06:13:47 +00:00
|
|
|
double now = Sys_DoubleTime ();
|
|
|
|
|
2010-12-25 11:31:14 +00:00
|
|
|
if (!sound_started)
|
|
|
|
return;
|
2011-09-07 06:13:47 +00:00
|
|
|
if (snd_alive) {
|
|
|
|
snd_alive = 0;
|
|
|
|
snd_alive_time = now;
|
|
|
|
} else {
|
|
|
|
if (!snd_shutdown) {
|
|
|
|
if (now - snd_alive_time > 1.0) {
|
|
|
|
Sys_Printf ("jackd client thread seems to have died\n");
|
2021-06-26 07:18:18 +00:00
|
|
|
snd->finish_channels ();
|
2011-09-07 06:13:47 +00:00
|
|
|
snd_shutdown = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-24 06:35:15 +00:00
|
|
|
if (snd_shutdown) {
|
|
|
|
if (snd_shutdown == 1) {
|
|
|
|
snd_shutdown++;
|
|
|
|
Sys_Printf ("Lost connection to jackd\n");
|
2021-06-25 09:32:48 +00:00
|
|
|
s_jack_connect (snd);
|
2009-12-24 06:35:15 +00:00
|
|
|
}
|
2012-05-27 22:46:22 +00:00
|
|
|
if (snd_shutdown)
|
|
|
|
return;
|
2009-12-24 06:35:15 +00:00
|
|
|
}
|
2010-08-05 07:20:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 11:03:13 +00:00
|
|
|
static const char **
|
|
|
|
s_jack_parse_ports (const char *ports_str)
|
|
|
|
{
|
|
|
|
int num_ports = 1;
|
|
|
|
int ind = 0;
|
|
|
|
char **ports;
|
|
|
|
|
|
|
|
for (const char *s = ports_str; *s; s++) {
|
|
|
|
num_ports += *s == ';';
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t len = strlen (ports_str) + 1;
|
|
|
|
ports = malloc ((num_ports + 1) * sizeof (*ports) + len + 1);
|
|
|
|
ports[num_ports] = 0;
|
|
|
|
ports[0] = (char *) &ports[num_ports + 1];
|
|
|
|
ports[0][len] = 0;
|
|
|
|
strcpy (ports[0], ports_str);
|
|
|
|
|
|
|
|
for (char *s = ports[0]; *s; s++) {
|
|
|
|
if (s > ports[0]) {
|
|
|
|
s[-1] = 0;
|
|
|
|
}
|
|
|
|
for (ports[ind++] = s; *s && *s != ';'; s++) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (const char **) ports;
|
|
|
|
}
|
|
|
|
|
2007-03-17 09:33:21 +00:00
|
|
|
static void
|
|
|
|
s_jack_activate (void)
|
|
|
|
{
|
|
|
|
const char **ports;
|
|
|
|
int i;
|
|
|
|
|
2012-05-27 22:46:22 +00:00
|
|
|
snd_shutdown = 0;
|
2021-06-29 11:03:13 +00:00
|
|
|
if (!*snd_jack_ports->string) {
|
|
|
|
ports = jack_get_ports (jack_handle, 0, 0,
|
|
|
|
JackPortIsPhysical | JackPortIsInput);
|
|
|
|
} else {
|
|
|
|
ports = s_jack_parse_ports (snd_jack_ports->string);
|
|
|
|
}
|
2021-03-29 10:58:00 +00:00
|
|
|
if (developer->int_val & SYS_snd) {
|
2021-06-29 11:03:13 +00:00
|
|
|
for (i = 0; ports[i]; i++) {
|
2011-09-07 06:13:47 +00:00
|
|
|
Sys_Printf ("%s\n", ports[i]);
|
2021-06-29 11:03:13 +00:00
|
|
|
}
|
2011-09-07 06:13:47 +00:00
|
|
|
}
|
2021-06-29 11:03:13 +00:00
|
|
|
if (jack_activate (jack_handle)) {
|
|
|
|
Sys_Printf ("Could not activate JACK\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 2; i++) {
|
2007-03-17 09:33:21 +00:00
|
|
|
jack_connect (jack_handle, jack_port_name (jack_out[i]), ports[i]);
|
2021-06-29 11:03:13 +00:00
|
|
|
}
|
2007-03-17 09:33:21 +00:00
|
|
|
free (ports);
|
|
|
|
}
|
|
|
|
|
2007-03-17 03:14:41 +00:00
|
|
|
static void
|
2021-06-25 09:32:48 +00:00
|
|
|
s_block_sound (snd_t *t)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
2010-12-25 11:31:14 +00:00
|
|
|
if (!sound_started)
|
|
|
|
return;
|
2007-03-17 06:20:52 +00:00
|
|
|
if (++snd_blocked == 1) {
|
2007-03-18 11:20:47 +00:00
|
|
|
//Sys_Printf ("jack_deactivate: %d\n", jack_deactivate (jack_handle));
|
2007-03-17 06:20:52 +00:00
|
|
|
}
|
2007-03-17 03:14:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-06-25 09:32:48 +00:00
|
|
|
s_unblock_sound (snd_t *t)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
2010-12-25 11:31:14 +00:00
|
|
|
if (!sound_started)
|
|
|
|
return;
|
2007-03-17 06:20:52 +00:00
|
|
|
if (!snd_blocked)
|
|
|
|
return;
|
2007-03-17 03:14:41 +00:00
|
|
|
|
2007-03-17 06:20:52 +00:00
|
|
|
if (!--snd_blocked) {
|
2007-03-18 11:20:47 +00:00
|
|
|
//s_jack_activate ();
|
|
|
|
//Sys_Printf ("jack_activate\n");
|
2007-03-17 06:20:52 +00:00
|
|
|
}
|
2007-03-17 03:14:41 +00:00
|
|
|
}
|
|
|
|
|
2007-03-17 07:05:24 +00:00
|
|
|
static void
|
2021-06-23 15:04:02 +00:00
|
|
|
snd_jack_xfer (snd_t *snd, portable_samplepair_t *paintbuffer, int count,
|
|
|
|
float volume)
|
2007-03-17 07:05:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-03-21 11:39:01 +00:00
|
|
|
if (snd_blocked) {
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
*output[0]++ = 0;
|
|
|
|
*output[1]++ = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2007-03-17 07:05:24 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2010-08-06 01:58:12 +00:00
|
|
|
/* max is +/- 1.0. need to implement clamping. */
|
2021-06-26 07:18:18 +00:00
|
|
|
*output[0]++ = volume * paintbuffer[i].left;
|
|
|
|
*output[1]++ = volume * paintbuffer[i].right;
|
2007-03-17 07:05:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-17 06:20:52 +00:00
|
|
|
static int
|
|
|
|
snd_jack_process (jack_nframes_t nframes, void *arg)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
2021-06-25 09:32:48 +00:00
|
|
|
snd_t *snd = arg;
|
2007-03-17 07:05:24 +00:00
|
|
|
int i;
|
|
|
|
|
2011-09-07 06:13:47 +00:00
|
|
|
snd_alive = 1;
|
2007-03-18 11:58:54 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
output[i] = (float *) jack_port_get_buffer (jack_out[i], nframes);
|
2021-06-26 07:18:18 +00:00
|
|
|
snd->paint_channels (snd, snd->paintedtime + nframes);
|
2007-03-17 06:20:52 +00:00
|
|
|
return 0;
|
2007-03-17 03:14:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-03-17 06:20:52 +00:00
|
|
|
snd_jack_shutdown (void *arg)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
2021-06-26 07:18:18 +00:00
|
|
|
snd_t *snd = arg;
|
|
|
|
|
2009-12-24 06:35:15 +00:00
|
|
|
snd_shutdown = 1;
|
2021-06-26 07:18:18 +00:00
|
|
|
snd->finish_channels ();
|
2011-09-07 06:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
snd_jack_error (const char *desc)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "snd_jack: %s\n", desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
snd_jack_xrun (void *arg)
|
|
|
|
{
|
2021-03-29 10:58:00 +00:00
|
|
|
if (developer->int_val & SYS_snd) {
|
2020-03-23 06:19:05 +00:00
|
|
|
fprintf (stderr, "snd_jack: xrun\n");
|
|
|
|
}
|
2011-09-07 06:13:47 +00:00
|
|
|
return 0;
|
2007-03-17 03:14:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 09:32:48 +00:00
|
|
|
static int
|
|
|
|
s_jack_connect (snd_t *snd)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
2007-03-17 06:20:52 +00:00
|
|
|
int i;
|
2007-03-17 03:14:41 +00:00
|
|
|
|
2011-09-07 06:13:47 +00:00
|
|
|
jack_set_error_function (snd_jack_error);
|
2007-07-03 10:30:13 +00:00
|
|
|
if ((jack_handle = jack_client_open ("QuakeForge",
|
|
|
|
JackServerName | JackNoStartServer, 0,
|
|
|
|
snd_jack_server->string)) == 0) {
|
2007-03-17 06:20:52 +00:00
|
|
|
Sys_Printf ("Could not connect to JACK\n");
|
2021-06-25 09:32:48 +00:00
|
|
|
return 0;
|
2007-03-17 03:14:41 +00:00
|
|
|
}
|
2011-09-07 06:13:47 +00:00
|
|
|
if (jack_set_xrun_callback (jack_handle, snd_jack_xrun, 0))
|
|
|
|
Sys_Printf ("Could not set xrun callback\n");
|
2021-06-25 09:32:48 +00:00
|
|
|
jack_set_process_callback (jack_handle, snd_jack_process, snd);
|
2021-06-26 07:18:18 +00:00
|
|
|
jack_on_shutdown (jack_handle, snd_jack_shutdown, snd);
|
2007-03-17 06:20:52 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
2021-01-31 07:01:20 +00:00
|
|
|
jack_out[i] = jack_port_register (jack_handle, va (0, "out_%d", i + 1),
|
2007-03-17 06:20:52 +00:00
|
|
|
JACK_DEFAULT_AUDIO_TYPE,
|
|
|
|
JackPortIsOutput, 0);
|
2021-06-25 09:32:48 +00:00
|
|
|
snd->speed = jack_get_sample_rate (jack_handle);
|
2021-06-26 07:18:18 +00:00
|
|
|
snd->channels = 2;
|
2007-03-17 09:33:21 +00:00
|
|
|
s_jack_activate ();
|
2010-12-25 11:31:14 +00:00
|
|
|
sound_started = 1;
|
2021-06-25 09:32:48 +00:00
|
|
|
Sys_Printf ("Connected to JACK: %d Sps\n", snd->speed);
|
|
|
|
return 1;
|
2007-03-17 03:14:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 09:32:48 +00:00
|
|
|
static int
|
|
|
|
s_init (snd_t *snd)
|
2012-05-27 22:46:22 +00:00
|
|
|
{
|
2021-06-25 09:32:48 +00:00
|
|
|
snd->xfer = snd_jack_xfer;
|
|
|
|
return s_jack_connect (snd);
|
2012-05-27 22:46:22 +00:00
|
|
|
}
|
|
|
|
|
2007-03-17 03:14:41 +00:00
|
|
|
static void
|
2021-06-25 09:32:48 +00:00
|
|
|
s_shutdown (snd_t *snd)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
2007-03-17 09:33:21 +00:00
|
|
|
int i;
|
2007-03-18 22:09:51 +00:00
|
|
|
if (jack_handle) {
|
|
|
|
jack_deactivate (jack_handle);
|
2012-01-27 10:58:03 +00:00
|
|
|
if (!snd_shutdown) {
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
jack_port_unregister (jack_handle, jack_out[i]);
|
|
|
|
}
|
2007-03-18 22:09:51 +00:00
|
|
|
jack_client_close (jack_handle);
|
|
|
|
}
|
2007-03-17 03:14:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 09:32:48 +00:00
|
|
|
static void
|
|
|
|
s_init_cvars (void)
|
|
|
|
{
|
|
|
|
snd_jack_server = Cvar_Get ("snd_jack_server", "default", CVAR_ROM, NULL,
|
|
|
|
"The name of the JACK server to connect to");
|
2021-06-29 11:03:13 +00:00
|
|
|
snd_jack_ports = Cvar_Get ("snd_jack_ports", "", CVAR_ROM, NULL,
|
|
|
|
"; separated list of port names to which QF "
|
|
|
|
"will connect. Defaults to the first two "
|
|
|
|
"physical ports. Currently only two ports "
|
|
|
|
"are supported.");
|
2021-06-25 09:32:48 +00:00
|
|
|
}
|
|
|
|
|
2007-03-17 03:14:41 +00:00
|
|
|
static general_funcs_t plugin_info_general_funcs = {
|
2021-06-25 09:32:48 +00:00
|
|
|
.init = s_init_cvars,
|
2007-03-17 03:14:41 +00:00
|
|
|
};
|
|
|
|
|
2021-06-25 09:32:48 +00:00
|
|
|
static snd_output_funcs_t plugin_info_output_funcs = {
|
|
|
|
.init = s_init,
|
|
|
|
.shutdown = s_shutdown,
|
|
|
|
.on_update = s_update,
|
2021-06-22 06:35:37 +00:00
|
|
|
.block_sound = s_block_sound,
|
|
|
|
.unblock_sound = s_unblock_sound,
|
2021-06-25 09:32:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static snd_output_data_t plugin_info_output_data = {
|
|
|
|
.model = som_pull,
|
2007-03-17 03:14:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static general_data_t plugin_info_general_data;
|
|
|
|
|
|
|
|
static plugin_funcs_t plugin_info_funcs = {
|
2021-06-25 09:32:48 +00:00
|
|
|
.general = &plugin_info_general_funcs,
|
|
|
|
.snd_output = &plugin_info_output_funcs,
|
2007-03-17 03:14:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static plugin_data_t plugin_info_data = {
|
2021-06-25 09:32:48 +00:00
|
|
|
.general = &plugin_info_general_data,
|
|
|
|
.snd_output = &plugin_info_output_data,
|
2007-03-17 03:14:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static plugin_t plugin_info = {
|
2021-06-25 09:32:48 +00:00
|
|
|
qfp_snd_output,
|
2007-03-17 03:14:41 +00:00
|
|
|
0,
|
|
|
|
QFPLUGIN_VERSION,
|
|
|
|
"0.1",
|
|
|
|
"Sound Renderer",
|
2021-06-25 09:32:48 +00:00
|
|
|
"Copyright (C) 2007-2021 contributors of the QuakeForge "
|
2007-03-17 06:20:52 +00:00
|
|
|
"project\n"
|
|
|
|
"Please see the file \"AUTHORS\" for a list of contributors",
|
2007-03-17 03:14:41 +00:00
|
|
|
&plugin_info_funcs,
|
|
|
|
&plugin_info_data,
|
|
|
|
};
|
|
|
|
|
2021-06-25 09:32:48 +00:00
|
|
|
PLUGIN_INFO(snd_output, jack)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
|
|
|
return &plugin_info;
|
|
|
|
}
|