| 1 | /* FCE Ultra - NES/Famicom Emulator |
| 2 | * |
| 3 | * Copyright notice for this file: |
| 4 | * Copyright (C) 2002 Ben Parnell |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include <unistd.h> |
| 24 | #include <string.h> |
| 25 | #include <ctype.h> |
| 26 | #include <errno.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sched.h> |
| 33 | #include <sys/soundcard.h> |
| 34 | |
| 35 | #include "../../types.h" |
| 36 | |
| 37 | static int format; |
| 38 | static int dspfd; |
| 39 | |
| 40 | // fsize is in samples, not bytes(gets translated before ioctl()) |
| 41 | int InitUNIXDSPSound(int *rate, int bits, int fsize, int nfrags) |
| 42 | { |
| 43 | int x; |
| 44 | |
| 45 | printf(" Opening /dev/dsp..."); |
| 46 | dspfd=open("/dev/dsp",O_WRONLY); |
| 47 | if(dspfd==-1) goto __disperror; |
| 48 | |
| 49 | if(!bits) goto skip16check; |
| 50 | x=AFMT_S16_LE; |
| 51 | format=0; |
| 52 | printf("\n Setting format to 16-bit, signed, LSB first..."); |
| 53 | if(ioctl(dspfd,SNDCTL_DSP_SETFMT,&x)==-1) |
| 54 | { |
| 55 | skip16check: |
| 56 | x=AFMT_U8; |
| 57 | printf("\n Setting format to 8-bit, unsigned..."); |
| 58 | if(ioctl(dspfd,SNDCTL_DSP_SETFMT,&x)==-1) goto __disperror; |
| 59 | format=1; |
| 60 | } |
| 61 | |
| 62 | printf("\n Setting fragment size to %d samples and number of fragments to %d...",1<<fsize,nfrags); |
| 63 | |
| 64 | if(!format) |
| 65 | fsize++; |
| 66 | x=fsize|(nfrags<<16); |
| 67 | |
| 68 | if(ioctl(dspfd,SNDCTL_DSP_SETFRAGMENT,&x)==-1) |
| 69 | printf("ERROR (continuing anyway)\n"); |
| 70 | x=0; |
| 71 | printf("\n Setting mono sound..."); |
| 72 | if(ioctl(dspfd,SNDCTL_DSP_STEREO,&x)==-1) goto __disperror; |
| 73 | printf("\n Setting playback rate of %d hz...",*rate); |
| 74 | if(ioctl(dspfd,SNDCTL_DSP_SPEED,rate)==-1) goto __disperror; |
| 75 | printf("Set to %d hz\n",*rate); |
| 76 | if(*rate<8192 || *rate>65535) |
| 77 | { |
| 78 | printf(" Sample rate is out of the acceptable range(8192-65535).\n"); |
| 79 | close(dspfd); |
| 80 | return(0); |
| 81 | } |
| 82 | return 1; |
| 83 | __disperror: |
| 84 | printf("ERROR\n"); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | void KillUNIXDSPSound(void) |
| 89 | { |
| 90 | close(dspfd); |
| 91 | } |
| 92 | |
| 93 | static int16 MBuffer[2048]; |
| 94 | void WriteUNIXDSPSound(int32 *Buffer, int Count, int noblocking) |
| 95 | { |
| 96 | int P,c; |
| 97 | int32 *src=Buffer; |
| 98 | |
| 99 | if(format) |
| 100 | { |
| 101 | uint8 *dest=(uint8 *)MBuffer; |
| 102 | for(P=Count;P;P--,dest++,src++) |
| 103 | *dest=(uint8)((*src)>>8)^128; |
| 104 | c=Count; |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | int16 *dest=MBuffer; |
| 109 | for(P=Count;P;P--,dest++,src++) |
| 110 | *dest=*src; |
| 111 | c=Count<<1; |
| 112 | } |
| 113 | |
| 114 | // noblocking=!noblocking; // speed testing |
| 115 | if(noblocking) |
| 116 | { |
| 117 | struct audio_buf_info ai; |
| 118 | if(!ioctl(dspfd,SNDCTL_DSP_GETOSPACE,&ai)) |
| 119 | if(ai.bytes<c) |
| 120 | return; |
| 121 | } |
| 122 | write(dspfd,(uint8 *)MBuffer,c); |
| 123 | } |