Dave Chapman | ef5f97b | 2002-05-12 12:30:14 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 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 Feltzing | 4242a34 | 2004-07-08 10:12:39 +0000 | [diff] [blame] | 24 | #include <sys/soundcard.h> |
Dave Chapman | f07bfd0 | 2002-05-12 14:58:41 +0000 | [diff] [blame] | 25 | #include "../common/sound.h" |
Dave Chapman | ef5f97b | 2002-05-12 12:30:14 +0000 | [diff] [blame] | 26 | |
Dave Chapman | 553c256 | 2002-05-15 07:27:48 +0000 | [diff] [blame] | 27 | /* We want to use the "real" open in this file */ |
Dave Chapman | ef5f97b | 2002-05-12 12:30:14 +0000 | [diff] [blame] | 28 | #undef open |
| 29 | |
| 30 | int init_sound(sound_t* sound) { |
Dave Chapman | 553c256 | 2002-05-15 07:27:48 +0000 | [diff] [blame] | 31 | sound->fd=open("/dev/dsp", O_WRONLY); |
| 32 | sound->freq=-1; |
| 33 | sound->channels=-1; |
Dave Chapman | ef5f97b | 2002-05-12 12:30:14 +0000 | [diff] [blame] | 34 | |
Dave Chapman | 553c256 | 2002-05-15 07:27:48 +0000 | [diff] [blame] | 35 | if (sound->fd <= 0) { |
| 36 | fprintf(stderr,"Can not open /dev/dsp - simulating sound output\n"); |
| 37 | sound->fd=0; |
Dave Chapman | ef5f97b | 2002-05-12 12:30:14 +0000 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
| 41 | int config_sound(sound_t* sound, int sound_freq, int channels) { |
Linus Nielsen Feltzing | 4242a34 | 2004-07-08 10:12:39 +0000 | [diff] [blame] | 42 | int format=AFMT_S16_NE; |
Dave Chapman | ef5f97b | 2002-05-12 12:30:14 +0000 | [diff] [blame] | 43 | int setting=0x000C000D; // 12 fragments size 8kb ? WHAT IS THIS? |
| 44 | |
Dave Chapman | 553c256 | 2002-05-15 07:27:48 +0000 | [diff] [blame] | 45 | sound->freq=sound_freq; |
| 46 | sound->channels=channels; |
Dave Chapman | ef5f97b | 2002-05-12 12:30:14 +0000 | [diff] [blame] | 47 | |
Dave Chapman | 553c256 | 2002-05-15 07:27:48 +0000 | [diff] [blame] | 48 | if (sound->fd) { |
| 49 | if (ioctl(sound->fd,SNDCTL_DSP_SETFRAGMENT,&setting)==-1) { |
| 50 | perror("SNDCTL_DSP_SETFRAGMENT"); |
| 51 | } |
Dave Chapman | ef5f97b | 2002-05-12 12:30:14 +0000 | [diff] [blame] | 52 | |
Dave Chapman | 553c256 | 2002-05-15 07:27:48 +0000 | [diff] [blame] | 53 | 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 Chapman | ef5f97b | 2002-05-12 12:30:14 +0000 | [diff] [blame] | 57 | |
Dave Chapman | 553c256 | 2002-05-15 07:27:48 +0000 | [diff] [blame] | 58 | 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 Chapman | ef5f97b | 2002-05-12 12:30:14 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | int output_sound(sound_t* sound,const void* buf, int count) { |
Dave Chapman | 553c256 | 2002-05-15 07:27:48 +0000 | [diff] [blame] | 69 | 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 Chapman | ef5f97b | 2002-05-12 12:30:14 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void close_sound(sound_t* sound) { |
Dave Chapman | 553c256 | 2002-05-15 07:27:48 +0000 | [diff] [blame] | 82 | if (sound->fd) close(sound->fd); |
| 83 | sound->fd=-1; |
Dave Chapman | ef5f97b | 2002-05-12 12:30:14 +0000 | [diff] [blame] | 84 | } |