diff options
-rw-r--r-- | uisimulator/common/sound.h | 9 | ||||
-rw-r--r-- | uisimulator/x11/oss_sound.c | 55 |
2 files changed, 43 insertions, 21 deletions
diff --git a/uisimulator/common/sound.h b/uisimulator/common/sound.h index 4da5ce9f08..cb9afcb187 100644 --- a/uisimulator/common/sound.h +++ b/uisimulator/common/sound.h @@ -23,8 +23,13 @@ #ifdef LINUX -/* The "sound device type" is simply the file descriptor */ -#define sound_t int +/* The "sound device type" */ + +typedef struct { + int fd; + int freq; + int channels; +} sound_t; #else #ifdef WIN32 diff --git a/uisimulator/x11/oss_sound.c b/uisimulator/x11/oss_sound.c index 853b70d57f..e69c403acd 100644 --- a/uisimulator/x11/oss_sound.c +++ b/uisimulator/x11/oss_sound.c @@ -24,15 +24,17 @@ #include <linux/soundcard.h> #include "../common/sound.h" -/* We want to use the "real" open in some cases */ +/* We want to use the "real" open in this file */ #undef open int init_sound(sound_t* sound) { - *sound=open("/dev/dsp", O_WRONLY); + sound->fd=open("/dev/dsp", O_WRONLY); + sound->freq=-1; + sound->channels=-1; - if (sound < 0) { - fprintf(stderr,"Can not open /dev/dsp - Aborting - sound=%d\n",sound); - exit(-1); + if (sound->fd <= 0) { + fprintf(stderr,"Can not open /dev/dsp - simulating sound output\n"); + sound->fd=0; } } @@ -40,28 +42,43 @@ int config_sound(sound_t* sound, int sound_freq, int channels) { int format=AFMT_U16_LE; int setting=0x000C000D; // 12 fragments size 8kb ? WHAT IS THIS? - if (ioctl(*sound,SNDCTL_DSP_SETFRAGMENT,&setting)==-1) { - perror("SNDCTL_DSP_SETFRAGMENT"); - } + sound->freq=sound_freq; + sound->channels=channels; - if (ioctl(*sound,SNDCTL_DSP_CHANNELS,&channels)==-1) { - perror("SNDCTL_DSP_STEREO"); - } - if (channels==0) { fprintf(stderr,"Warning, only mono supported\n"); } + if (sound->fd) { + if (ioctl(sound->fd,SNDCTL_DSP_SETFRAGMENT,&setting)==-1) { + perror("SNDCTL_DSP_SETFRAGMENT"); + } - if (ioctl(*sound,SNDCTL_DSP_SETFMT,&format)==-1) { - perror("SNDCTL_DSP_SETFMT"); - } + if (ioctl(sound->fd,SNDCTL_DSP_CHANNELS,&channels)==-1) { + perror("SNDCTL_DSP_STEREO"); + } + if (channels==0) { fprintf(stderr,"Warning, only mono supported\n"); } - if (ioctl(*sound,SNDCTL_DSP_SPEED,&sound_freq)==-1) { - perror("SNDCTL_DSP_SPEED"); + if (ioctl(sound->fd,SNDCTL_DSP_SETFMT,&format)==-1) { + perror("SNDCTL_DSP_SETFMT"); + } + + if (ioctl(sound->fd,SNDCTL_DSP_SPEED,&sound_freq)==-1) { + perror("SNDCTL_DSP_SPEED"); + } } } int output_sound(sound_t* sound,const void* buf, int count) { - return(write(*sound,buf,count)); + unsigned long long t; + + if (sound->fd) { + return(write(sound->fd,buf,count)); + } else { + t=(unsigned int)(((unsigned int)(1000000/sound->channels)*count)/sound->freq); +// fprintf(stderr,"writing %d bytes at %d frequency - sleeping for %u microseconds\n",count,sound->freq,t); + usleep(t); + return(count); + } } void close_sound(sound_t* sound) { - if (*sound) close(*sound); + if (sound->fd) close(sound->fd); + sound->fd=-1; } |