RetroArch/audio/oss.c

161 lines
3.3 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.
2011-01-23 19:29:28 +00:00
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
2010-05-28 16:21:33 +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-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);
return NULL;
}
int frags = (latency * rate * 4)/(1000 * (1 << 9));
int frag = (frags << 16) | 9;
2011-06-16 21:20:12 +00:00
if (ioctl(*fd, SNDCTL_DSP_SETFRAGMENT, &frag) < 0)
2010-05-28 12:33:18 +00:00
{
close(*fd);
free(fd);
return NULL;
}
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);
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);
return NULL;
}
2011-06-16 21:20:12 +00:00
if (ioctl(*fd, SNDCTL_DSP_SPEED, &rate) < 0)
2010-05-28 12:33:18 +00:00
{
close(*fd);
free(fd);
return NULL;
}
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;
2011-06-16 21:20:12 +00:00
if ((ret = write(*fd, buf, size)) <= 0)
2010-08-16 17:16:03 +00:00
{
2011-06-16 21:20:12 +00:00
if ((fcntl(*fd, F_GETFL) & O_NONBLOCK) && errno == EAGAIN)
2010-08-16 17:16:03 +00:00
return 0;
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)
fprintf(stderr, "SSNES [ERROR]: 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);
}
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"
2010-05-28 12:33:18 +00:00
};
2011-12-24 12:46:12 +00:00