RetroArch/audio/drivers/oss.c

210 lines
4.4 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2010-05-28 16:21:33 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2010-05-28 16:21:33 +00:00
* 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.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2010-05-28 16:21:33 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2010-05-28 16:21:33 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
2010-05-28 12:33:18 +00:00
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
2011-06-16 21:20:12 +00:00
#ifdef HAVE_OSS_BSD
#include <soundcard.h>
#else
2010-05-28 12:33:18 +00:00
#include <sys/soundcard.h>
2011-06-16 21:20:12 +00:00
#endif
#include <retro_endianness.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2015-11-23 18:40:09 +00:00
#include "../audio_driver.h"
#include "../../configuration.h"
2015-11-23 11:03:38 +00:00
#include "../../verbosity.h"
2010-05-28 12:33:18 +00:00
2011-06-16 21:20:12 +00:00
#ifdef HAVE_OSS_BSD
#define DEFAULT_OSS_DEV "/dev/audio"
#else
#define DEFAULT_OSS_DEV "/dev/dsp"
#endif
2017-01-09 11:50:54 +00:00
static bool oss_is_paused = false;
static void *oss_init(const char *device, unsigned rate, unsigned latency)
2010-05-28 12:33:18 +00:00
{
2015-01-12 04:05:56 +00:00
int frags, frag, channels, format, new_rate;
2016-09-08 09:41:58 +00:00
int *fd = (int*)calloc(1, sizeof(int));
settings_t *settings = config_get_ptr();
2015-06-26 15:03:05 +00:00
const char *oss_device = device ? device : DEFAULT_OSS_DEV;
2016-09-08 09:41:58 +00:00
if (!fd)
2010-05-28 12:33:18 +00:00
return NULL;
2011-06-16 21:20:12 +00:00
if ((*fd = open(oss_device, O_WRONLY)) < 0)
2010-05-28 12:33:18 +00:00
{
free(fd);
2012-02-23 22:53:18 +00:00
perror("open");
2010-05-28 12:33:18 +00:00
return NULL;
}
2015-01-12 04:05:56 +00:00
frags = (latency * rate * 4) / (1000 * (1 << 10));
2016-09-08 09:41:58 +00:00
frag = (frags << 16) | 10;
2010-05-28 12:33:18 +00:00
2011-06-16 21:20:12 +00:00
if (ioctl(*fd, SNDCTL_DSP_SETFRAGMENT, &frag) < 0)
2012-04-21 21:25:32 +00:00
RARCH_WARN("Cannot set fragment sizes. Latency might not be as expected ...\n");
2010-05-28 12:33:18 +00:00
2015-01-12 04:05:56 +00:00
channels = 2;
2016-09-08 09:41:58 +00:00
format = is_little_endian() ? AFMT_S16_LE : AFMT_S16_BE;
2010-05-28 12:33:18 +00:00
2011-06-16 21:20:12 +00:00
if (ioctl(*fd, SNDCTL_DSP_CHANNELS, &channels) < 0)
2016-09-08 09:41:58 +00:00
goto error;
2010-05-28 12:33:18 +00:00
2011-06-16 21:20:12 +00:00
if (ioctl(*fd, SNDCTL_DSP_SETFMT, &format) < 0)
2016-09-08 09:41:58 +00:00
goto error;
2010-05-28 12:33:18 +00:00
2015-01-12 04:05:56 +00:00
new_rate = rate;
if (ioctl(*fd, SNDCTL_DSP_SPEED, &new_rate) < 0)
2016-09-08 09:41:58 +00:00
goto error;
2010-05-28 12:33:18 +00:00
if (new_rate != (int)rate)
{
2012-04-21 21:25:32 +00:00
RARCH_WARN("Requested sample rate not supported. Adjusting output rate to %d Hz.\n", new_rate);
2015-03-20 20:31:16 +00:00
settings->audio.out_rate = new_rate;
}
2010-05-28 12:33:18 +00:00
return fd;
2016-09-08 09:41:58 +00:00
error:
close(*fd);
free(fd);
perror("ioctl");
return NULL;
2010-05-28 12:33:18 +00:00
}
static ssize_t oss_write(void *data, const void *buf, size_t size)
2010-05-28 12:33:18 +00:00
{
2015-01-12 04:05:56 +00:00
ssize_t ret;
2011-12-24 12:46:12 +00:00
int *fd = (int*)data;
2010-05-28 12:33:18 +00:00
2011-06-16 21:20:12 +00:00
if (size == 0)
2010-05-28 12:33:18 +00:00
return 0;
if ((ret = write(*fd, buf, size)) < 0)
2010-08-16 17:16:03 +00:00
{
if (errno == EAGAIN && (fcntl(*fd, F_GETFL) & O_NONBLOCK))
2010-08-16 17:16:03 +00:00
return 0;
2012-04-25 17:49:11 +00:00
2010-05-28 12:33:18 +00:00
return -1;
2010-08-16 17:16:03 +00:00
}
2010-05-28 12:33:18 +00:00
return ret;
}
static bool oss_stop(void *data)
2010-05-28 12:33:18 +00:00
{
2011-12-24 12:46:12 +00:00
int *fd = (int*)data;
2015-01-12 04:05:56 +00:00
2010-05-28 12:33:18 +00:00
ioctl(*fd, SNDCTL_DSP_RESET, 0);
oss_is_paused = true;
2010-05-28 12:33:18 +00:00
return true;
}
static bool oss_start(void *data)
2010-05-28 12:33:18 +00:00
{
(void)data;
oss_is_paused = false;
2010-05-28 12:33:18 +00:00
return true;
}
static bool oss_alive(void *data)
{
(void)data;
return !oss_is_paused;
}
static void oss_set_nonblock_state(void *data, bool state)
2010-08-16 17:16:03 +00:00
{
2010-08-16 17:49:54 +00:00
int rc;
2015-01-12 04:05:56 +00:00
int *fd = (int*)data;
2010-08-16 17:16:03 +00:00
if (state)
2010-08-16 17:49:54 +00:00
rc = fcntl(*fd, F_SETFL, fcntl(*fd, F_GETFL) | O_NONBLOCK);
2010-08-16 17:16:03 +00:00
else
2010-08-16 17:49:54 +00:00
rc = fcntl(*fd, F_SETFL, fcntl(*fd, F_GETFL) & (~O_NONBLOCK));
if (rc != 0)
2012-04-21 21:25:32 +00:00
RARCH_WARN("Could not set nonblocking on OSS file descriptor. Will not be able to fast-forward.\n");
2010-08-16 17:16:03 +00:00
}
static void oss_free(void *data)
2010-05-28 12:33:18 +00:00
{
2011-12-24 12:46:12 +00:00
int *fd = (int*)data;
2010-05-28 12:33:18 +00:00
ioctl(*fd, SNDCTL_DSP_RESET, 0);
close(*fd);
free(fd);
}
static size_t oss_write_avail(void *data)
{
audio_buf_info info;
int *fd = (int*)data;
if (ioctl(*fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("SNDCTL_DSP_GETOSPACE failed ...\n");
return 0;
}
return info.bytes;
}
static size_t oss_buffer_size(void *data)
{
audio_buf_info info;
int *fd = (int*)data;
if (ioctl(*fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("SNDCTL_DSP_GETOSPACE failed ...\n");
return 1; /* Return something non-zero to avoid SIGFPE. */
}
return info.fragsize * info.fragstotal;
}
static bool oss_use_float(void *data)
{
(void)data;
return false;
}
2014-09-11 05:06:20 +00:00
audio_driver_t audio_oss = {
2011-12-24 12:46:12 +00:00
oss_init,
oss_write,
oss_stop,
oss_start,
oss_alive,
2011-12-24 12:46:12 +00:00
oss_set_nonblock_state,
oss_free,
oss_use_float,
"oss",
NULL,
NULL,
oss_write_avail,
oss_buffer_size,
2010-05-28 12:33:18 +00:00
};