RetroArch/audio/jack.c

335 lines
8.2 KiB
C
Raw Normal View History

2011-01-17 19:54:58 +00:00
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
2012-01-08 00:08:18 +00:00
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
2011-01-01 02:53:30 +00:00
*
* Some code herein may be based on code found in BSNES.
*
* SSNES 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES 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 SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
2011-12-24 23:59:46 +00:00
#include "../driver.h"
2011-01-01 02:53:30 +00:00
#include <stdlib.h>
2011-12-24 23:59:46 +00:00
#include "../general.h"
2011-01-01 02:53:30 +00:00
#include <jack/jack.h>
#include <jack/types.h>
#include <jack/ringbuffer.h>
#include <pthread.h>
#include <stdint.h>
2011-12-24 12:46:12 +00:00
#include "../boolean.h"
2011-01-01 02:53:30 +00:00
#include <string.h>
#include <assert.h>
#define FRAMES(x) (x / (sizeof(float) * 2))
2011-01-01 02:53:30 +00:00
typedef struct jack
{
jack_client_t *client;
jack_port_t *ports[2];
jack_ringbuffer_t *buffer[2];
volatile bool shutdown;
bool nonblock;
pthread_cond_t cond;
pthread_mutex_t cond_lock;
2012-02-20 17:39:50 +00:00
size_t buffer_size;
2011-01-01 02:53:30 +00:00
} jack_t;
static int process_cb(jack_nframes_t nframes, void *data)
{
2011-12-24 12:46:12 +00:00
jack_t *jd = (jack_t*)data;
2011-01-01 02:53:30 +00:00
if (nframes <= 0)
{
pthread_cond_signal(&jd->cond);
return 0;
}
jack_nframes_t avail[2];
avail[0] = jack_ringbuffer_read_space(jd->buffer[0]);
avail[1] = jack_ringbuffer_read_space(jd->buffer[1]);
2011-01-01 03:50:35 +00:00
jack_nframes_t min_avail = ((avail[0] < avail[1]) ? avail[0] : avail[1]) / sizeof(jack_default_audio_sample_t);
2011-01-01 02:53:30 +00:00
if (min_avail > nframes)
min_avail = nframes;
for (int i = 0; i < 2; i++)
{
2011-12-24 12:46:12 +00:00
jack_default_audio_sample_t *out = (jack_default_audio_sample_t*)jack_port_get_buffer(jd->ports[i], nframes);
2011-01-01 02:53:30 +00:00
assert(out);
jack_ringbuffer_read(jd->buffer[i], (char*)out, min_avail * sizeof(jack_default_audio_sample_t));
for (jack_nframes_t f = min_avail; f < nframes; f++)
{
out[f] = 0.0f;
}
}
pthread_cond_signal(&jd->cond);
return 0;
}
static void shutdown_cb(void *data)
{
2011-12-24 12:46:12 +00:00
jack_t *jd = (jack_t*)data;
2011-01-01 02:53:30 +00:00
jd->shutdown = true;
pthread_cond_signal(&jd->cond);
}
static int parse_ports(char **dest_ports, const char **jports)
2011-01-01 02:53:30 +00:00
{
int parsed = 0;
const char *con = strtok(g_settings.audio.device, ",");
if (con)
dest_ports[parsed++] = strdup(con);
2011-01-01 02:53:30 +00:00
con = strtok(NULL, ",");
if (con)
dest_ports[parsed++] = strdup(con);
2011-01-01 02:53:30 +00:00
for (int i = parsed; i < 2; i++)
dest_ports[i] = strdup(jports[i]);
return 2;
}
static size_t find_buffersize(jack_t *jd, int latency)
{
int frames = latency * g_settings.audio.out_rate / 1000;
2011-03-06 15:58:33 +00:00
jack_latency_range_t range;
int jack_latency = 0;
for (int i = 0; i < 2; i++)
{
jack_port_get_latency_range(jd->ports[i], JackPlaybackLatency, &range);
2011-12-24 12:46:12 +00:00
if ((int)range.max > jack_latency)
2011-03-06 15:58:33 +00:00
jack_latency = range.max;
}
SSNES_LOG("JACK: Jack latency is %d frames.\n", jack_latency);
int buffer_frames = frames - jack_latency;
int min_buffer_frames = jack_get_buffer_size(jd->client) * 2;
SSNES_LOG("JACK: Minimum buffer size is %d frames.\n", min_buffer_frames);
if (buffer_frames < min_buffer_frames)
buffer_frames = min_buffer_frames;
return buffer_frames * sizeof(jack_default_audio_sample_t);
2011-01-01 02:53:30 +00:00
}
static void *ja_init(const char *device, unsigned rate, unsigned latency)
2011-01-01 02:53:30 +00:00
{
2011-12-24 12:46:12 +00:00
jack_t *jd = (jack_t*)calloc(1, sizeof(jack_t));
if (!jd)
2011-01-01 02:53:30 +00:00
return NULL;
2011-08-14 14:24:42 +00:00
pthread_cond_init(&jd->cond, NULL);
pthread_mutex_init(&jd->cond_lock, NULL);
2011-01-01 02:53:30 +00:00
const char **jports = NULL;
2011-12-24 12:46:12 +00:00
char *dest_ports[2];
size_t bufsize = 0;
int parsed = 0;
2011-01-01 02:53:30 +00:00
jd->client = jack_client_open("SSNES", JackNullOption, NULL);
if (jd->client == NULL)
goto error;
g_settings.audio.out_rate = jack_get_sample_rate(jd->client);
jack_set_process_callback(jd->client, process_cb, jd);
jack_on_shutdown(jd->client, shutdown_cb, jd);
jd->ports[0] = jack_port_register(jd->client, "left", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
jd->ports[1] = jack_port_register(jd->client, "right", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (jd->ports[0] == NULL || jd->ports[1] == NULL)
{
SSNES_ERR("Failed to register ports.\n");
goto error;
}
jports = jack_get_ports(jd->client, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
if (jports == NULL)
{
SSNES_ERR("Failed to get ports.\n");
goto error;
}
2011-12-24 12:46:12 +00:00
bufsize = find_buffersize(jd, latency);
2012-02-20 17:39:50 +00:00
jd->buffer_size = bufsize;
SSNES_LOG("JACK: Internal buffer size: %d frames.\n", (int)(bufsize / sizeof(jack_default_audio_sample_t)));
for (int i = 0; i < 2; i++)
{
jd->buffer[i] = jack_ringbuffer_create(bufsize);
if (jd->buffer[i] == NULL)
{
SSNES_ERR("Failed to create buffers.\n");
goto error;
}
}
2011-12-24 12:46:12 +00:00
parsed = parse_ports(dest_ports, jports);
2011-01-01 02:53:30 +00:00
if (jack_activate(jd->client) < 0)
{
SSNES_ERR("Failed to activate Jack...\n");
goto error;
}
for (int i = 0; i < 2; i++)
{
if (jack_connect(jd->client, jack_port_name(jd->ports[i]), dest_ports[i]))
{
SSNES_ERR("Failed to connect to Jack port.\n");
goto error;
}
}
for (int i = 0; i < parsed; i++)
free(dest_ports[i]);
2011-01-01 02:53:30 +00:00
jack_free(jports);
return jd;
error:
if (jports != NULL)
jack_free(jports);
return NULL;
}
static size_t write_buffer(jack_t *jd, const float *buf, size_t size)
2011-01-01 02:53:30 +00:00
{
2011-12-24 12:46:12 +00:00
jack_default_audio_sample_t out_deinterleaved_buffer[2][AUDIO_CHUNK_SIZE_NONBLOCKING];
2011-01-01 02:53:30 +00:00
for (int i = 0; i < 2; i++)
for (size_t j = 0; j < FRAMES(size); j++)
out_deinterleaved_buffer[i][j] = buf[j * 2 + i];
2011-01-01 02:53:30 +00:00
size_t frames = FRAMES(size);
size_t written = 0;
while (written < frames)
2011-01-01 02:53:30 +00:00
{
if (jd->shutdown)
return 0;
2011-12-24 12:46:12 +00:00
size_t avail[2] = {
jack_ringbuffer_write_space(jd->buffer[0]),
jack_ringbuffer_write_space(jd->buffer[1]),
};
2011-01-01 02:53:30 +00:00
size_t min_avail = avail[0] < avail[1] ? avail[0] : avail[1];
min_avail /= sizeof(float);
2011-01-01 02:53:30 +00:00
size_t write_frames = frames - written > min_avail ? min_avail : frames - written;
if (write_frames > 0)
2011-01-01 03:50:35 +00:00
{
for (int i = 0; i < 2; i++)
2011-12-24 12:46:12 +00:00
{
jack_ringbuffer_write(jd->buffer[i], (const char*)&out_deinterleaved_buffer[i][written],
write_frames * sizeof(jack_default_audio_sample_t));
}
written += write_frames;
2011-01-01 03:50:35 +00:00
}
else
{
pthread_mutex_lock(&jd->cond_lock);
pthread_cond_wait(&jd->cond, &jd->cond_lock);
pthread_mutex_unlock(&jd->cond_lock);
2011-01-01 03:50:35 +00:00
}
2011-01-01 02:53:30 +00:00
if (jd->nonblock)
break;
2011-01-01 02:53:30 +00:00
}
return written * sizeof(float) * 2;
2011-01-01 02:53:30 +00:00
}
static ssize_t ja_write(void *data, const void *buf, size_t size)
2011-01-01 02:53:30 +00:00
{
2011-12-24 12:46:12 +00:00
jack_t *jd = (jack_t*)data;
2011-01-01 02:53:30 +00:00
2011-12-24 12:46:12 +00:00
return write_buffer(jd, (const float*)buf, size);
2011-01-01 02:53:30 +00:00
}
static bool ja_stop(void *data)
2011-01-01 02:53:30 +00:00
{
(void)data;
return true;
}
static void ja_set_nonblock_state(void *data, bool state)
2011-01-01 02:53:30 +00:00
{
2011-12-24 12:46:12 +00:00
jack_t *jd = (jack_t*)data;
2011-01-01 02:53:30 +00:00
jd->nonblock = state;
}
static bool ja_start(void *data)
2011-01-01 02:53:30 +00:00
{
(void)data;
return true;
}
static void ja_free(void *data)
2011-01-01 02:53:30 +00:00
{
2011-12-24 12:46:12 +00:00
jack_t *jd = (jack_t*)data;
2011-01-01 02:53:30 +00:00
2011-01-01 03:50:35 +00:00
jd->shutdown = true;
2011-01-01 02:53:30 +00:00
if (jd->client != NULL)
{
jack_deactivate(jd->client);
jack_client_close(jd->client);
}
for (int i = 0; i < 2; i++)
if (jd->buffer[i] != NULL)
jack_ringbuffer_free(jd->buffer[i]);
pthread_mutex_destroy(&jd->cond_lock);
pthread_cond_destroy(&jd->cond);
free(jd);
}
static bool ja_use_float(void *data)
{
(void)data;
return true;
}
2012-02-20 17:39:50 +00:00
static size_t ja_write_avail(void *data)
{
jack_t *jd = (jack_t*)data;
return jack_ringbuffer_write_space(jd->buffer[0]);
}
static size_t ja_buffer_size(void *data)
{
jack_t *jd = (jack_t*)data;
return jd->buffer_size;
}
2011-01-01 02:53:30 +00:00
const audio_driver_t audio_jack = {
2011-12-24 12:46:12 +00:00
ja_init,
ja_write,
ja_stop,
ja_start,
ja_set_nonblock_state,
ja_free,
ja_use_float,
2012-02-20 17:39:50 +00:00
"jack",
ja_write_avail,
ja_buffer_size,
2011-01-01 02:53:30 +00:00
};