Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 197 lines (161 sloc) 4.25 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
*
* RetroArch 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.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "driver.h"
#include "general.h"
#include <stdlib.h>

#ifdef HAVE_OSS_BSD
#include <soundcard.h>
#else
#include <sys/soundcard.h>
#endif

#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>

#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)
{
   int *fd = (int*)calloc(1, sizeof(int));
   if (fd == NULL)
      return NULL;

   const char *oss_device = DEFAULT_OSS_DEV;

   if (device != NULL)
      oss_device = device;

   if ((*fd = open(oss_device, O_WRONLY)) < 0)
   {
      free(fd);
      perror("open");
      return NULL;
   }

   int frags = (latency * rate * 4) / (1000 * (1 << 10));
   int frag = (frags << 16) | 10;

   if (ioctl(*fd, SNDCTL_DSP_SETFRAGMENT, &frag) < 0)
      RARCH_WARN("Cannot set fragment sizes. Latency might not be as expected ...\n");

   int channels = 2;
   int format = is_little_endian() ?
      AFMT_S16_LE : AFMT_S16_BE;

   if (ioctl(*fd, SNDCTL_DSP_CHANNELS, &channels) < 0)
   {
      close(*fd);
      free(fd);
      perror("ioctl");
      return NULL;
   }

   if (ioctl(*fd, SNDCTL_DSP_SETFMT, &format) < 0)
   {
      close(*fd);
      free(fd);
      perror("ioctl");
      return NULL;
   }

   int new_rate = rate;
   if (ioctl(*fd, SNDCTL_DSP_SPEED, &new_rate) < 0)
   {
      close(*fd);
      free(fd);
      perror("ioctl");
      return NULL;
   }

   if (new_rate != (int)rate)
   {
      RARCH_WARN("Requested sample rate not supported. Adjusting output rate to %d Hz.\n", new_rate);
      g_settings.audio.out_rate = new_rate;
   }

   return fd;
}

static ssize_t oss_write(void *data, const void *buf, size_t size)
{
   int *fd = (int*)data;

   if (size == 0)
      return 0;

   ssize_t ret;
   if ((ret = write(*fd, buf, size)) <= 0)
   {
      if ((fcntl(*fd, F_GETFL) & O_NONBLOCK) && errno == EAGAIN)
         return 0;

      return -1;
   }

   return ret;
}

static bool oss_stop(void *data)
{
   int *fd = (int*)data;
   ioctl(*fd, SNDCTL_DSP_RESET, 0);
   return true;
}

static bool oss_start(void *data)
{
   return true;
}

static void oss_set_nonblock_state(void *data, bool state)
{
   int *fd = (int*)data;
   int rc;
   if (state)
      rc = fcntl(*fd, F_SETFL, fcntl(*fd, F_GETFL) | O_NONBLOCK);
   else
      rc = fcntl(*fd, F_SETFL, fcntl(*fd, F_GETFL) & (~O_NONBLOCK));
   if (rc != 0)
      RARCH_WARN("Could not set nonblocking on OSS file descriptor. Will not be able to fast-forward.\n");
}

static void oss_free(void *data)
{
   int *fd = (int*)data;

   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)
   {
      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)
   {
      RARCH_ERR("SNDCTL_DSP_GETOSPACE failed ...\n");
      return 1; // Return something non-zero to avoid SIGFPE.
   }

   return info.fragsize * info.fragstotal;
}

const audio_driver_t audio_oss = {
   oss_init,
   oss_write,
   oss_stop,
   oss_start,
   oss_set_nonblock_state,
   oss_free,
   NULL,
   "oss",
   oss_write_avail,
   oss_buffer_size,
};

Something went wrong with that request. Please try again.