RetroArch/audio/oss.c

197 lines
4.1 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2012-01-08 00:08:18 +00:00
* Copyright (C) 2010-2012 - 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/>.
*/
2011-06-16 21:20:12 +00:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2010-05-28 16:21:33 +00:00
2010-05-28 12:33:18 +00:00
#include "driver.h"
2011-07-25 22:53:24 +00:00
#include "general.h"
2010-05-28 12:33:18 +00:00
#include <stdlib.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
2010-05-28 12:33:18 +00:00
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
2010-08-16 17:16:03 +00:00
#include <errno.h>
2010-08-16 17:52:52 +00:00
#include <stdio.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
static void *oss_init(const char *device, unsigned rate, unsigned latency)
2010-05-28 12:33:18 +00:00
{
2011-12-24 12:46:12 +00:00
int *fd = (int*)calloc(1, sizeof(int));
2011-06-16 21:20:12 +00:00
if (fd == NULL)
2010-05-28 12:33:18 +00:00
return NULL;
2011-06-16 21:20:12 +00:00
const char *oss_device = DEFAULT_OSS_DEV;
2010-05-28 12:33:18 +00:00
2011-06-16 21:20:12 +00:00
if (device != NULL)
2010-05-28 12:33:18 +00:00
oss_device = device;
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;
}
2012-02-23 22:53:18 +00:00
int frags = (latency * rate * 4) / (1000 * (1 << 10));
2012-02-20 17:30:26 +00:00
int 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
int channels = 2;
2011-07-25 22:53:24 +00:00
int 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)
2010-05-28 12:33:18 +00:00
{
close(*fd);
free(fd);
2012-02-23 22:53:18 +00:00
perror("ioctl");
2010-05-28 12:33:18 +00:00
return NULL;
}
2011-06-16 21:20:12 +00:00
if (ioctl(*fd, SNDCTL_DSP_SETFMT, &format) < 0)
2010-05-28 12:33:18 +00:00
{
close(*fd);
free(fd);
2012-02-23 22:53:18 +00:00
perror("ioctl");
2010-05-28 12:33:18 +00:00
return NULL;
}
int new_rate = rate;
if (ioctl(*fd, SNDCTL_DSP_SPEED, &new_rate) < 0)
2010-05-28 12:33:18 +00:00
{
close(*fd);
free(fd);
2012-02-23 22:53:18 +00:00
perror("ioctl");
2010-05-28 12:33:18 +00:00
return NULL;
}
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);
g_settings.audio.out_rate = new_rate;
}
2010-05-28 12:33:18 +00:00
return fd;
}
static ssize_t oss_write(void *data, const void *buf, size_t size)
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
2011-06-16 21:20:12 +00:00
if (size == 0)
2010-05-28 12:33:18 +00:00
return 0;
ssize_t ret;
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;
2010-05-28 12:33:18 +00:00
ioctl(*fd, SNDCTL_DSP_RESET, 0);
return true;
}
static bool oss_start(void *data)
2010-05-28 12:33:18 +00:00
{
return true;
}
static void oss_set_nonblock_state(void *data, bool state)
2010-08-16 17:16:03 +00:00
{
2011-12-24 12:46:12 +00:00
int *fd = (int*)data;
2010-08-16 17:49:54 +00:00
int rc;
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)
{
int *fd = (int*)data;
audio_buf_info info;
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)
{
int *fd = (int*)data;
audio_buf_info info;
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;
}
2010-05-28 12:33:18 +00:00
const audio_driver_t audio_oss = {
2011-12-24 12:46:12 +00:00
oss_init,
oss_write,
oss_stop,
oss_start,
oss_set_nonblock_state,
oss_free,
NULL,
"oss",
oss_write_avail,
oss_buffer_size,
2010-05-28 12:33:18 +00:00
};
2011-12-24 12:46:12 +00:00