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
|
|
|
|
2007-03-17 06:20:52 +00:00
|
|
|
JACK version of the renderer
|
2007-03-17 03:14:41 +00:00
|
|
|
|
2007-03-17 06:20:52 +00:00
|
|
|
Copyright (C) 2007 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
|
|
|
|
|
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
|
|
|
"$Id: snd_dma.c 11380 2007-03-10 14:17:52Z taniwha $";
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
#include "snd_render.h"
|
|
|
|
|
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;
|
2007-03-17 06:20:52 +00:00
|
|
|
static jack_client_t *jack_handle;
|
|
|
|
static jack_port_t *jack_out[2];
|
|
|
|
static dma_t _snd_shm;
|
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;
|
2007-03-17 03:14:41 +00:00
|
|
|
|
2007-04-07 07:44:07 +00:00
|
|
|
static void
|
|
|
|
s_stop_all_sounds (void)
|
|
|
|
{
|
2010-12-25 11:31:14 +00:00
|
|
|
if (!sound_started)
|
|
|
|
return;
|
2007-04-07 07:44:07 +00:00
|
|
|
SND_StopAllSounds ();
|
|
|
|
SND_ScanChannels (1);
|
|
|
|
}
|
|
|
|
|
2010-08-05 07:20:50 +00:00
|
|
|
static void
|
|
|
|
s_start_sound (int entnum, int entchannel, sfx_t *sfx, const vec3_t origin,
|
|
|
|
float fvol, float attenuation)
|
|
|
|
{
|
2010-12-25 11:31:14 +00:00
|
|
|
if (!sound_started)
|
|
|
|
return;
|
2010-08-05 07:20:50 +00:00
|
|
|
if (!snd_shutdown)
|
|
|
|
SND_StartSound (entnum, entchannel, sfx, origin, fvol, attenuation);
|
|
|
|
}
|
|
|
|
|
2009-12-24 06:35:15 +00:00
|
|
|
static void
|
|
|
|
s_update (const vec3_t origin, const vec3_t forward, const vec3_t right,
|
|
|
|
const vec3_t up)
|
|
|
|
{
|
2010-12-25 11:31:14 +00:00
|
|
|
if (!sound_started)
|
|
|
|
return;
|
2009-12-24 06:35:15 +00:00
|
|
|
if (snd_shutdown) {
|
|
|
|
if (snd_shutdown == 1) {
|
|
|
|
snd_shutdown++;
|
|
|
|
Sys_Printf ("Lost connection to jackd\n");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SND_SetListener (origin, forward, right, up);
|
|
|
|
}
|
|
|
|
|
2007-03-17 03:14:41 +00:00
|
|
|
static void
|
2007-03-17 06:20:52 +00:00
|
|
|
s_extra_update (void)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-05 07:20:50 +00:00
|
|
|
static void
|
|
|
|
s_local_sound (const char *sound)
|
|
|
|
{
|
2010-12-25 11:31:14 +00:00
|
|
|
if (!sound_started)
|
|
|
|
return;
|
2010-08-05 07:20:50 +00:00
|
|
|
if (!snd_shutdown)
|
|
|
|
SND_LocalSound (sound);
|
|
|
|
}
|
|
|
|
|
2007-03-17 09:33:21 +00:00
|
|
|
static void
|
|
|
|
s_jack_activate (void)
|
|
|
|
{
|
|
|
|
const char **ports;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (jack_activate (jack_handle)) {
|
|
|
|
Sys_Printf ("Could not activate JACK\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ports = jack_get_ports (jack_handle, 0, 0,
|
|
|
|
JackPortIsPhysical | JackPortIsInput);
|
|
|
|
//for (i = 0; ports[i]; i++)
|
|
|
|
// Sys_Printf ("%s\n", ports[i]);
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
jack_connect (jack_handle, jack_port_name (jack_out[i]), ports[i]);
|
|
|
|
free (ports);
|
|
|
|
}
|
|
|
|
|
2007-03-17 03:14:41 +00:00
|
|
|
static void
|
2007-03-17 06:20:52 +00:00
|
|
|
s_block_sound (void)
|
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
|
2007-03-17 06:20:52 +00:00
|
|
|
s_unblock_sound (void)
|
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
|
|
|
}
|
|
|
|
|
2010-08-05 07:20:50 +00:00
|
|
|
static channel_t *
|
|
|
|
s_alloc_channel (void)
|
|
|
|
{
|
2010-12-25 11:31:14 +00:00
|
|
|
if (!sound_started)
|
|
|
|
return 0;
|
2010-08-05 07:20:50 +00:00
|
|
|
if (!snd_shutdown)
|
|
|
|
return SND_AllocChannel ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-19 22:20:13 +00:00
|
|
|
static void
|
|
|
|
s_snd_force_unblock (void)
|
|
|
|
{
|
2010-12-25 11:31:14 +00:00
|
|
|
if (!sound_started)
|
|
|
|
return;
|
2007-03-19 22:20:13 +00:00
|
|
|
snd_blocked = 1;
|
|
|
|
s_unblock_sound ();
|
|
|
|
}
|
|
|
|
|
2010-12-25 11:31:14 +00:00
|
|
|
static void
|
|
|
|
s_ambient_off (void)
|
|
|
|
{
|
|
|
|
if (!sound_started)
|
|
|
|
return;
|
|
|
|
SND_AmbientOff ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
s_ambient_on (void)
|
|
|
|
{
|
|
|
|
if (!sound_started)
|
|
|
|
return;
|
|
|
|
SND_AmbientOn ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
s_static_sound (sfx_t *sfx, const vec3_t origin, float vol,
|
|
|
|
float attenuation)
|
|
|
|
{
|
|
|
|
if (!sound_started)
|
|
|
|
return;
|
|
|
|
SND_StaticSound (sfx, origin, vol, attenuation);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
s_stop_sound (int entnum, int entchannel)
|
|
|
|
{
|
|
|
|
if (!sound_started)
|
|
|
|
return;
|
|
|
|
SND_StopSound (entnum, entchannel);
|
|
|
|
}
|
|
|
|
|
|
|
|
static sfx_t *
|
|
|
|
s_precache_sound (const char *name)
|
|
|
|
{
|
|
|
|
if (!sound_started)
|
|
|
|
return 0;
|
|
|
|
return SND_PrecacheSound (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static sfx_t *
|
|
|
|
s_load_sound (const char *name)
|
|
|
|
{
|
|
|
|
if (!sound_started)
|
|
|
|
return 0;
|
|
|
|
return SND_LoadSound (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
s_channel_stop (channel_t *chan)
|
|
|
|
{
|
|
|
|
if (!sound_started)
|
|
|
|
return;
|
|
|
|
SND_ChannelStop (chan);
|
|
|
|
}
|
|
|
|
|
2007-03-17 07:05:24 +00:00
|
|
|
static void
|
|
|
|
snd_jack_xfer (int endtime)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int count;
|
2010-08-11 23:44:34 +00:00
|
|
|
float snd_vol = snd_volume->value;
|
2007-03-17 07:05:24 +00:00
|
|
|
|
|
|
|
count = endtime - snd_paintedtime;
|
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. */
|
2010-08-11 23:44:34 +00:00
|
|
|
*output[0]++ = snd_vol * snd_paintbuffer[i].left;
|
|
|
|
*output[1]++ = snd_vol * snd_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
|
|
|
{
|
2007-03-17 07:05:24 +00:00
|
|
|
int i;
|
|
|
|
|
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);
|
2007-03-17 07:05:24 +00:00
|
|
|
SND_PaintChannels (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
|
|
|
{
|
2009-12-24 06:35:15 +00:00
|
|
|
int i;
|
|
|
|
channel_t *ch;
|
|
|
|
snd_shutdown = 1;
|
|
|
|
for (i = 0; i < MAX_CHANNELS; i++) {
|
|
|
|
ch = &snd_channels[i];
|
|
|
|
ch->done = ch->stop = 1;
|
|
|
|
}
|
2007-03-17 03:14:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-03-17 06:20:52 +00:00
|
|
|
s_init (void)
|
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
|
|
|
|
2007-03-17 06:20:52 +00:00
|
|
|
snd_shm = &_snd_shm;
|
2007-03-17 07:05:24 +00:00
|
|
|
snd_shm->xfer = snd_jack_xfer;
|
2007-03-17 03:14:41 +00:00
|
|
|
|
2007-03-19 22:20:13 +00:00
|
|
|
Cmd_AddCommand ("snd_force_unblock", s_snd_force_unblock,
|
|
|
|
"fix permanently blocked sound");
|
|
|
|
|
2007-03-17 06:20:52 +00:00
|
|
|
snd_volume = Cvar_Get ("volume", "0.7", CVAR_ARCHIVE, NULL,
|
|
|
|
"Set the volume for sound playback");
|
2007-07-03 10:30:13 +00:00
|
|
|
snd_jack_server = Cvar_Get ("snd_jack_server", "default", CVAR_ROM, NULL,
|
|
|
|
"The name of the JACK server to connect to");
|
2007-03-17 03:14:41 +00:00
|
|
|
|
2007-03-17 06:20:52 +00:00
|
|
|
SND_SFX_Init ();
|
|
|
|
SND_Channels_Init ();
|
2007-03-17 03:14:41 +00:00
|
|
|
|
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");
|
|
|
|
return;
|
2007-03-17 03:14:41 +00:00
|
|
|
}
|
2007-03-17 06:20:52 +00:00
|
|
|
jack_set_process_callback (jack_handle, snd_jack_process, 0);
|
|
|
|
jack_on_shutdown (jack_handle, snd_jack_shutdown, 0);
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
jack_out[i] = jack_port_register (jack_handle, va ("out_%d", i + 1),
|
|
|
|
JACK_DEFAULT_AUDIO_TYPE,
|
|
|
|
JackPortIsOutput, 0);
|
|
|
|
snd_shm->speed = jack_get_sample_rate (jack_handle);
|
2007-03-17 09:33:21 +00:00
|
|
|
s_jack_activate ();
|
2010-12-25 11:31:14 +00:00
|
|
|
sound_started = 1;
|
2007-03-17 06:20:52 +00:00
|
|
|
Sys_Printf ("Connected to JACK: %d Sps\n", snd_shm->speed);
|
2007-03-17 03:14:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
s_shutdown (void)
|
|
|
|
{
|
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);
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
jack_port_unregister (jack_handle, jack_out[i]);
|
|
|
|
jack_client_close (jack_handle);
|
|
|
|
}
|
2007-03-17 03:14:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static general_funcs_t plugin_info_general_funcs = {
|
|
|
|
s_init,
|
|
|
|
s_shutdown,
|
|
|
|
};
|
|
|
|
|
|
|
|
static snd_render_funcs_t plugin_info_render_funcs = {
|
2010-12-25 11:31:14 +00:00
|
|
|
s_ambient_off,
|
|
|
|
s_ambient_on,
|
|
|
|
s_static_sound,
|
2010-08-05 07:20:50 +00:00
|
|
|
s_start_sound,
|
2010-12-25 11:31:14 +00:00
|
|
|
s_stop_sound,
|
|
|
|
s_precache_sound,
|
2009-12-24 06:35:15 +00:00
|
|
|
s_update,
|
2007-04-07 07:44:07 +00:00
|
|
|
s_stop_all_sounds,
|
2007-03-17 03:14:41 +00:00
|
|
|
s_extra_update,
|
2010-08-05 07:20:50 +00:00
|
|
|
s_local_sound,
|
2007-03-17 03:14:41 +00:00
|
|
|
s_block_sound,
|
|
|
|
s_unblock_sound,
|
2010-12-25 11:31:14 +00:00
|
|
|
s_load_sound,
|
2010-08-05 07:20:50 +00:00
|
|
|
s_alloc_channel,
|
2010-12-25 11:31:14 +00:00
|
|
|
s_channel_stop,
|
2007-03-17 03:14:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static general_data_t plugin_info_general_data;
|
|
|
|
|
|
|
|
static plugin_funcs_t plugin_info_funcs = {
|
|
|
|
&plugin_info_general_funcs,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
&plugin_info_render_funcs,
|
|
|
|
};
|
|
|
|
|
|
|
|
static plugin_data_t plugin_info_data = {
|
|
|
|
&plugin_info_general_data,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2007-03-17 06:20:52 +00:00
|
|
|
&snd_render_data,
|
2007-03-17 03:14:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static plugin_t plugin_info = {
|
|
|
|
qfp_snd_render,
|
|
|
|
0,
|
|
|
|
QFPLUGIN_VERSION,
|
|
|
|
"0.1",
|
|
|
|
"Sound Renderer",
|
2007-03-17 06:20:52 +00:00
|
|
|
"Copyright (C) 2007 contributors of the QuakeForge "
|
|
|
|
"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,
|
|
|
|
};
|
|
|
|
|
|
|
|
PLUGIN_INFO(snd_render, jack)
|
|
|
|
{
|
|
|
|
return &plugin_info;
|
|
|
|
}
|