2002-05-12 12:30:14 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 Dave Chapman
|
|
|
|
*
|
|
|
|
* oss_sound - a sound driver for Linux (and others?) OSS audio
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <linux/soundcard.h>
|
2002-05-12 14:58:41 +00:00
|
|
|
#include "../common/sound.h"
|
2002-05-12 12:30:14 +00:00
|
|
|
|
2002-05-15 07:27:48 +00:00
|
|
|
/* We want to use the "real" open in this file */
|
2002-05-12 12:30:14 +00:00
|
|
|
#undef open
|
|
|
|
|
|
|
|
int init_sound(sound_t* sound) {
|
2002-05-15 07:27:48 +00:00
|
|
|
sound->fd=open("/dev/dsp", O_WRONLY);
|
|
|
|
sound->freq=-1;
|
|
|
|
sound->channels=-1;
|
2002-05-12 12:30:14 +00:00
|
|
|
|
2002-05-15 07:27:48 +00:00
|
|
|
if (sound->fd <= 0) {
|
|
|
|
fprintf(stderr,"Can not open /dev/dsp - simulating sound output\n");
|
|
|
|
sound->fd=0;
|
2002-05-12 12:30:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
2002-05-15 07:27:48 +00:00
|
|
|
sound->freq=sound_freq;
|
|
|
|
sound->channels=channels;
|
2002-05-12 12:30:14 +00:00
|
|
|
|
2002-05-15 07:27:48 +00:00
|
|
|
if (sound->fd) {
|
|
|
|
if (ioctl(sound->fd,SNDCTL_DSP_SETFRAGMENT,&setting)==-1) {
|
|
|
|
perror("SNDCTL_DSP_SETFRAGMENT");
|
|
|
|
}
|
2002-05-12 12:30:14 +00:00
|
|
|
|
2002-05-15 07:27:48 +00:00
|
|
|
if (ioctl(sound->fd,SNDCTL_DSP_CHANNELS,&channels)==-1) {
|
|
|
|
perror("SNDCTL_DSP_STEREO");
|
|
|
|
}
|
|
|
|
if (channels==0) { fprintf(stderr,"Warning, only mono supported\n"); }
|
2002-05-12 12:30:14 +00:00
|
|
|
|
2002-05-15 07:27:48 +00:00
|
|
|
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");
|
|
|
|
}
|
2002-05-12 12:30:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int output_sound(sound_t* sound,const void* buf, int count) {
|
2002-05-15 07:27:48 +00:00
|
|
|
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);
|
|
|
|
}
|
2002-05-12 12:30:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void close_sound(sound_t* sound) {
|
2002-05-15 07:27:48 +00:00
|
|
|
if (sound->fd) close(sound->fd);
|
|
|
|
sound->fd=-1;
|
2002-05-12 12:30:14 +00:00
|
|
|
}
|