No more password prompts
[kugel-rb.git] / uisimulator / x11 / oss_sound.c
blobe69c403acd6c187cd6ba4bb312d221c83fcc887d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2002 Dave Chapman
11 * oss_sound - a sound driver for Linux (and others?) OSS audio
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #include <stdio.h>
22 #include <fcntl.h>
24 #include <linux/soundcard.h>
25 #include "../common/sound.h"
27 /* We want to use the "real" open in this file */
28 #undef open
30 int init_sound(sound_t* sound) {
31 sound->fd=open("/dev/dsp", O_WRONLY);
32 sound->freq=-1;
33 sound->channels=-1;
35 if (sound->fd <= 0) {
36 fprintf(stderr,"Can not open /dev/dsp - simulating sound output\n");
37 sound->fd=0;
41 int config_sound(sound_t* sound, int sound_freq, int channels) {
42 int format=AFMT_U16_LE;
43 int setting=0x000C000D; // 12 fragments size 8kb ? WHAT IS THIS?
45 sound->freq=sound_freq;
46 sound->channels=channels;
48 if (sound->fd) {
49 if (ioctl(sound->fd,SNDCTL_DSP_SETFRAGMENT,&setting)==-1) {
50 perror("SNDCTL_DSP_SETFRAGMENT");
53 if (ioctl(sound->fd,SNDCTL_DSP_CHANNELS,&channels)==-1) {
54 perror("SNDCTL_DSP_STEREO");
56 if (channels==0) { fprintf(stderr,"Warning, only mono supported\n"); }
58 if (ioctl(sound->fd,SNDCTL_DSP_SETFMT,&format)==-1) {
59 perror("SNDCTL_DSP_SETFMT");
62 if (ioctl(sound->fd,SNDCTL_DSP_SPEED,&sound_freq)==-1) {
63 perror("SNDCTL_DSP_SPEED");
68 int output_sound(sound_t* sound,const void* buf, int count) {
69 unsigned long long t;
71 if (sound->fd) {
72 return(write(sound->fd,buf,count));
73 } else {
74 t=(unsigned int)(((unsigned int)(1000000/sound->channels)*count)/sound->freq);
75 // fprintf(stderr,"writing %d bytes at %d frequency - sleeping for %u microseconds\n",count,sound->freq,t);
76 usleep(t);
77 return(count);
81 void close_sound(sound_t* sound) {
82 if (sound->fd) close(sound->fd);
83 sound->fd=-1;