blob: 51d85470e7ed833507705a32c8cd707869892d3d [file] [log] [blame]
Dave Chapmanef5f97b2002-05-12 12:30:14 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2002 Dave Chapman
10 *
11 * oss_sound - a sound driver for Linux (and others?) OSS audio
12 *
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.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
21#include <stdio.h>
22#include <fcntl.h>
23
Linus Nielsen Feltzing4242a342004-07-08 10:12:39 +000024#include <sys/soundcard.h>
Dave Chapmanf07bfd02002-05-12 14:58:41 +000025#include "../common/sound.h"
Dave Chapmanef5f97b2002-05-12 12:30:14 +000026
Dave Chapman553c2562002-05-15 07:27:48 +000027/* We want to use the "real" open in this file */
Dave Chapmanef5f97b2002-05-12 12:30:14 +000028#undef open
29
30int init_sound(sound_t* sound) {
Dave Chapman553c2562002-05-15 07:27:48 +000031 sound->fd=open("/dev/dsp", O_WRONLY);
32 sound->freq=-1;
33 sound->channels=-1;
Dave Chapmanef5f97b2002-05-12 12:30:14 +000034
Dave Chapman553c2562002-05-15 07:27:48 +000035 if (sound->fd <= 0) {
36 fprintf(stderr,"Can not open /dev/dsp - simulating sound output\n");
37 sound->fd=0;
Dave Chapmanef5f97b2002-05-12 12:30:14 +000038 }
39}
40
41int config_sound(sound_t* sound, int sound_freq, int channels) {
Linus Nielsen Feltzing4242a342004-07-08 10:12:39 +000042 int format=AFMT_S16_NE;
Dave Chapmanef5f97b2002-05-12 12:30:14 +000043 int setting=0x000C000D; // 12 fragments size 8kb ? WHAT IS THIS?
44
Dave Chapman553c2562002-05-15 07:27:48 +000045 sound->freq=sound_freq;
46 sound->channels=channels;
Dave Chapmanef5f97b2002-05-12 12:30:14 +000047
Dave Chapman553c2562002-05-15 07:27:48 +000048 if (sound->fd) {
49 if (ioctl(sound->fd,SNDCTL_DSP_SETFRAGMENT,&setting)==-1) {
50 perror("SNDCTL_DSP_SETFRAGMENT");
51 }
Dave Chapmanef5f97b2002-05-12 12:30:14 +000052
Dave Chapman553c2562002-05-15 07:27:48 +000053 if (ioctl(sound->fd,SNDCTL_DSP_CHANNELS,&channels)==-1) {
54 perror("SNDCTL_DSP_STEREO");
55 }
56 if (channels==0) { fprintf(stderr,"Warning, only mono supported\n"); }
Dave Chapmanef5f97b2002-05-12 12:30:14 +000057
Dave Chapman553c2562002-05-15 07:27:48 +000058 if (ioctl(sound->fd,SNDCTL_DSP_SETFMT,&format)==-1) {
59 perror("SNDCTL_DSP_SETFMT");
60 }
61
62 if (ioctl(sound->fd,SNDCTL_DSP_SPEED,&sound_freq)==-1) {
63 perror("SNDCTL_DSP_SPEED");
64 }
Dave Chapmanef5f97b2002-05-12 12:30:14 +000065 }
66}
67
68int output_sound(sound_t* sound,const void* buf, int count) {
Dave Chapman553c2562002-05-15 07:27:48 +000069 unsigned long long t;
70
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);
78 }
Dave Chapmanef5f97b2002-05-12 12:30:14 +000079}
80
81void close_sound(sound_t* sound) {
Dave Chapman553c2562002-05-15 07:27:48 +000082 if (sound->fd) close(sound->fd);
83 sound->fd=-1;
Dave Chapmanef5f97b2002-05-12 12:30:14 +000084}