|
Lines 1-11
Link Here
|
| 1 |
--- sound.c.orig Tue May 22 17:45:42 2001 |
1 |
--- sound.c.orig 2014-11-18 23:26:13 UTC |
| 2 |
+++ sound.c Tue May 22 17:45:53 2001 |
2 |
+++ sound.c |
| 3 |
@@ -5,7 +5,7 @@ |
3 |
@@ -10,7 +10,13 @@ |
| 4 |
#include <unistd.h> |
|
|
| 5 |
#include <fcntl.h> |
| 6 |
#include <sys/ioctl.h> |
| 7 |
-#include <linux/soundcard.h> |
| 8 |
+#include <sys/soundcard.h> |
| 9 |
#include <sys/time.h> |
| 10 |
#include <signal.h> |
| 11 |
#include <string.h> |
4 |
#include <string.h> |
|
|
5 |
#include <errno.h> |
| 6 |
|
| 7 |
+#ifdef NO_ALSA |
| 8 |
+#include <sys/ioctl.h> |
| 9 |
+#include <sys/soundcard.h> |
| 10 |
+#define SOUNDDEV "/dev/dsp" |
| 11 |
+#else |
| 12 |
#include <alsa/asoundlib.h> |
| 13 |
+#endif |
| 14 |
|
| 15 |
|
| 16 |
#include "scav.h" |
| 17 |
@@ -42,10 +48,14 @@ sample samples[NUMSOUNDS]; |
| 18 |
|
| 19 |
int soundworking=0; |
| 20 |
int fragment; |
| 21 |
+#ifdef NO_ALSA |
| 22 |
+int dsp; |
| 23 |
+#endif |
| 24 |
int soundwrite,soundread; |
| 25 |
int *soundbuffer; |
| 26 |
int soundbufferlen; |
| 27 |
|
| 28 |
+#ifndef NO_ALSA |
| 29 |
snd_pcm_t *playback_handle; |
| 30 |
|
| 31 |
void opendsp(int samplerate) |
| 32 |
@@ -122,6 +132,7 @@ void opendsp(int samplerate) |
| 33 |
} |
| 34 |
|
| 35 |
} |
| 36 |
+#endif |
| 37 |
|
| 38 |
|
| 39 |
void soundinit(void) |
| 40 |
@@ -129,26 +140,50 @@ void soundinit(void) |
| 41 |
int fd[2]; |
| 42 |
char devname[256]; |
| 43 |
int value; |
| 44 |
+#ifndef NO_ALSA |
| 45 |
int res; |
| 46 |
+#endif |
| 47 |
|
| 48 |
sprintf(dirlist,"%s/%s,%s",localname,localdirname,libname); |
| 49 |
soundworking=0; |
| 50 |
+#ifdef NO_ALSA |
| 51 |
+ pipe(fd); |
| 52 |
+#else |
| 53 |
res=pipe(fd);res=res;//STFU |
| 54 |
+#endif |
| 55 |
soundread=fd[0]; |
| 56 |
soundwrite=fd[1]; |
| 57 |
+#ifdef NO_ALSA |
| 58 |
+ if(fork()) |
| 59 |
+#else |
| 60 |
res = fork(); |
| 61 |
if(res>0) |
| 62 |
+#endif |
| 63 |
{ |
| 64 |
close(soundread); |
| 65 |
return; |
| 66 |
} |
| 67 |
close(soundwrite); |
| 68 |
memset(samples,0,sizeof(samples)); |
| 69 |
+#ifdef NO_ALSA |
| 70 |
+ strcpy(devname,SOUNDDEV); |
| 71 |
+ dsp=open(devname,O_WRONLY); |
| 72 |
+ if(dsp<0) goto failed; |
| 73 |
+ fragment=0x20009; |
| 74 |
+ ioctl(dsp,SNDCTL_DSP_SETFRAGMENT,&fragment); |
| 75 |
+ value=10000; |
| 76 |
+ ioctl(dsp,SNDCTL_DSP_SPEED,&value); |
| 77 |
+ value=0; |
| 78 |
+ ioctl(dsp,SNDCTL_DSP_STEREO,&value); |
| 79 |
+ ioctl(dsp,SNDCTL_DSP_GETBLKSIZE,&fragment); |
| 80 |
+ if(!fragment) {close(dsp);goto failed;} |
| 81 |
+#else |
| 82 |
|
| 83 |
// 10,000 hz mono 8bit samples |
| 84 |
fragment = 256; |
| 85 |
opendsp(10000); |
| 86 |
|
| 87 |
+#endif |
| 88 |
soundbufferlen=fragment*sizeof(int); |
| 89 |
soundbuffer=malloc(soundbufferlen); |
| 90 |
if(!soundbuffer) goto failed; |
| 91 |
@@ -180,12 +215,21 @@ int i,file,size,len; |
| 92 |
} |
| 93 |
size=lseek(file,0,SEEK_END); |
| 94 |
lseek(file,0,SEEK_SET); |
| 95 |
+#ifdef NO_ALSA |
| 96 |
+ len=samples[num].len=(size+fragment-1)/fragment; |
| 97 |
+#else |
| 98 |
int expand = 1; |
| 99 |
len=samples[num].len=(size*expand+fragment-1)/fragment; |
| 100 |
+#endif |
| 101 |
len*=fragment; |
| 102 |
p1=samples[num].data=malloc(len); |
| 103 |
if(p1) |
| 104 |
{ |
| 105 |
+#ifdef NO_ALSA |
| 106 |
+ i=read(file,p1,size); |
| 107 |
+ if(len-size) memset(p1+size,0,len-size); |
| 108 |
+ while(size--) *p1++ ^= 0x80; |
| 109 |
+#else |
| 110 |
memset(p1, 0, len); |
| 111 |
int got=read(file,p1,size); |
| 112 |
for(i=got-1;i>=0;--i) |
| 113 |
@@ -194,6 +238,7 @@ int i,file,size,len; |
| 114 |
for(j=expand-1;j>=0;--j) |
| 115 |
p1[i*expand+j] = p1[i] - 0x80; |
| 116 |
} |
| 117 |
+#endif |
| 118 |
} else |
| 119 |
samples[num].data=0; |
| 120 |
close(file); |
| 121 |
@@ -207,8 +252,12 @@ signed char *p; |
| 122 |
int *ip; |
| 123 |
int playing[MIXMAX],position[MIXMAX]; |
| 124 |
int which; |
| 125 |
+#ifdef NO_ALSA |
| 126 |
+unsigned char clip[8192]; |
| 127 |
+#else |
| 128 |
int *mixbuffer; |
| 129 |
short *outbuffer; |
| 130 |
+#endif |
| 131 |
|
| 132 |
while(!soundworking) |
| 133 |
{ |
| 134 |
@@ -223,12 +272,21 @@ short *outbuffer; |
| 135 |
com=*commands; |
| 136 |
if(com==SOUND_EXIT) exit(0); |
| 137 |
} |
| 138 |
+#ifdef NO_ALSA |
| 139 |
+ for(i=0;i<8192;i++) |
| 140 |
+ { |
| 141 |
+ j=i-4096; |
| 142 |
+ clip[i]=j > 127 ? 255 : (j<-128 ? 0 : j+128); |
| 143 |
+ } |
| 144 |
+#endif |
| 145 |
for(i=0;i<NUMSOUNDS;++i) |
| 146 |
readsound(i); |
| 147 |
memset(playing,0,sizeof(playing)); |
| 148 |
memset(position,0,sizeof(position)); |
| 149 |
+#ifndef NO_ALSA |
| 150 |
mixbuffer = malloc(fragment * sizeof(*mixbuffer)); |
| 151 |
outbuffer = malloc(fragment * sizeof(*outbuffer)); |
| 152 |
+#endif |
| 153 |
for(;;) |
| 154 |
{ |
| 155 |
commandlen=read(soundread,commands,64); |
| 156 |
@@ -238,10 +296,18 @@ short *outbuffer; |
| 157 |
commandlen=0; |
| 158 |
if(errno==EPIPE) exit(0); |
| 159 |
} else if(commandlen==0) exit(0); |
| 160 |
+#ifdef NO_ALSA |
| 161 |
+ p=commands; |
| 162 |
+#else |
| 163 |
signed char *comp=commands; |
| 164 |
+#endif |
| 165 |
while(commandlen--) |
| 166 |
{ |
| 167 |
+#ifdef NO_ALSA |
| 168 |
+ com=*p++; |
| 169 |
+#else |
| 170 |
com=*comp++; |
| 171 |
+#endif |
| 172 |
if(com==SOUND_QUIET) {memset(position,0,sizeof(position));continue;} |
| 173 |
if(com==SOUND_EXIT) exit(0); |
| 174 |
if(com<NUMSOUNDS) |
| 175 |
@@ -261,7 +327,11 @@ short *outbuffer; |
| 176 |
position[i]=0; |
| 177 |
} |
| 178 |
} |
| 179 |
+#ifdef NO_ALSA |
| 180 |
+ memset(soundbuffer,0,soundbufferlen); |
| 181 |
+#else |
| 182 |
memset(mixbuffer, 0, fragment * sizeof(*mixbuffer)); |
| 183 |
+#endif |
| 184 |
for(i=0;i<MIXMAX;++i) |
| 185 |
{ |
| 186 |
if(!position[i]) continue; |
| 187 |
@@ -274,6 +344,17 @@ short *outbuffer; |
| 188 |
p=samples[which].data; |
| 189 |
if(!p) continue; |
| 190 |
p+=fragment*(position[i]++ -1); |
| 191 |
+#ifdef NO_ALSA |
| 192 |
+ ip=soundbuffer; |
| 193 |
+ j=fragment; |
| 194 |
+ while(j--) *ip++ += *p++; |
| 195 |
+ } |
| 196 |
+ j=fragment; |
| 197 |
+ ip=soundbuffer; |
| 198 |
+ p=(char *) soundbuffer; |
| 199 |
+ while(j--) *p++ = clip[4096+*ip++]; |
| 200 |
+ write(dsp,(char *)soundbuffer,fragment); |
| 201 |
+#else |
| 202 |
|
| 203 |
for(j=0;j<fragment;++j) |
| 204 |
mixbuffer[j] += 255*p[j]; |
| 205 |
@@ -289,15 +370,22 @@ short *outbuffer; |
| 206 |
res = snd_pcm_writei(playback_handle, outbuffer, fragment); |
| 207 |
//printf("res=%d\n", res); |
| 208 |
|
| 209 |
+#endif |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
void playsound(int n) |
| 214 |
{ |
| 215 |
char c; |
| 216 |
+#ifndef NO_ALSA |
| 217 |
int res; |
| 218 |
+#endif |
| 219 |
c=n; |
| 220 |
+#ifdef NO_ALSA |
| 221 |
+ write(soundwrite,&c,1); |
| 222 |
+#else |
| 223 |
res=write(soundwrite,&c,1);res=res;//STFU |
| 224 |
+#endif |
| 225 |
} |
| 226 |
|
| 227 |
void endsound(void) |