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
|
/* osssound.c: OSS (e.g. Linux) sound I/O
Copyright (c) 2000-2004 Russell Marks, Matan Ziv-Av, Philip Kendall
$Id: osssound.c 3115 2007-08-19 02:49:14Z fredm $
This program 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 Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/soundcard.h>
#include "settings.h"
#include "sound.h"
#include "spectrum.h"
#include "ui/ui.h"
/* using (8) 64 byte frags for 8kHz, scale up for higher */
#define BASE_SOUND_FRAG_PWR 6
static int soundfd=-1;
static int sixteenbit=1;
/* returns 0 on *success*, and adjusts freq/stereo args to reflect
* what we've actually got.
*/
int sound_lowlevel_init(const char *device,int *freqptr,int *stereoptr)
{
int frag,tmp,flags;
/* select a default device if we weren't explicitly given one */
if(device==NULL) device = "/dev/dsp";
/* Open the sound device non-blocking to avoid hangs if it is being
* used by something else, but then set it blocking again as that's what
* we actually want
*/
if((soundfd=open(device,O_WRONLY|O_NONBLOCK))==-1)
{
settings_current.sound = 0;
ui_error(UI_ERROR_ERROR,"couldn't open sound device '%s': %s",device,strerror(errno));
return 1;
}
if((flags=fcntl(soundfd,F_GETFL))==-1)
{
settings_current.sound = 0;
ui_error(UI_ERROR_ERROR,"couldn't get flags from sound device: %s",strerror(errno));
close(soundfd);
return 1;
}
flags &= ~O_NONBLOCK;
if(fcntl(soundfd,F_SETFL,flags)==-1)
{
settings_current.sound = 0;
ui_error(UI_ERROR_ERROR,"couldn't set sound device non-blocking: %s",strerror(errno));
close(soundfd);
return 1;
}
tmp=AFMT_S16_NE;
sixteenbit=1;
if(settings_current.sound_force_8bit || ioctl(soundfd,SNDCTL_DSP_SETFMT,&tmp)==-1)
{
/* try 8-bit - may be an 8-bit-only device */
tmp=AFMT_U8;
if((ioctl(soundfd,SNDCTL_DSP_SETFMT,&tmp))==-1)
{
settings_current.sound = 0;
ui_error(UI_ERROR_ERROR,"couldn't set sound device into 8-bit mode: %s",strerror(errno));
close(soundfd);
return 1;
}
sixteenbit=0;
}
/* XXX should it warn if it didn't get the stereoness it wanted? */
tmp=(*stereoptr)?1:0;
if(ioctl(soundfd,SNDCTL_DSP_STEREO,&tmp)<0)
{
/* if it failed, make sure the opposite is ok */
tmp=(*stereoptr)?0:1;
if(ioctl(soundfd,SNDCTL_DSP_STEREO,&tmp)<0)
{
settings_current.sound = 0;
ui_error(UI_ERROR_ERROR,"couldn't set sound device into either mono or stereo mode: %s",strerror(errno));
close(soundfd);
return 1;
}
*stereoptr=tmp;
}
frag=(0x80000|BASE_SOUND_FRAG_PWR);
if(ioctl(soundfd,SNDCTL_DSP_SPEED,freqptr)<0)
{
settings_current.sound = 0;
ui_error(UI_ERROR_ERROR,"couldn't set sound device speed to %d: %s",*freqptr, strerror(errno));
close(soundfd);
return 1;
}
if(*freqptr>8250) frag++;
if(*freqptr>16500) frag++;
if(*freqptr>33000) frag++;
if(*stereoptr) frag++;
if(sixteenbit) frag++;
/* FIXME: OSS API docs say you should write to the soundcard in frag size
blocks */
if(ioctl(soundfd,SNDCTL_DSP_SETFRAGMENT,&frag)<0)
{
settings_current.sound = 0;
ui_error(UI_ERROR_ERROR,"couldn't set sound device fragment size to %d: %s",frag,strerror(errno));
close(soundfd);
return 1;
}
return 0; /* success */
}
void sound_lowlevel_end(void)
{
if(soundfd!=-1)
close(soundfd);
}
void sound_lowlevel_frame(libspectrum_signed_word *data,int len)
{
static unsigned char buf8[4096];
unsigned char *data8=(unsigned char *)data;
int ret=0,ofs=0;
len<<=1; /* now in bytes */
if(!sixteenbit)
{
libspectrum_signed_word *src;
unsigned char *dst;
int f;
src=data; dst=buf8;
len>>=1;
for(f=0;f<len;f++)
*dst++=128+(int)((*src++)/256);
data8=buf8;
}
while(len)
{
ret=write(soundfd,data8+ofs,len);
if(ret>0)
ofs+=ret,len-=ret;
}
}
|