Mercurial > hg > forks > pwpunix
annotate pwplib/gb.c @ 54:85671798fdb3
Various code cleanups, cosmetics, warning fixes.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 26 May 2010 01:22:51 +0300 |
parents | b9d679965320 |
children |
rev | line source |
---|---|
0 | 1 /* |
2 * PWP Generic Beep (PWPbeep) engine for digital audio. | |
3 * | |
4 */ | |
5 | |
6 #include "config.h" | |
7 | |
8 #ifdef DRIVE_GB | |
9 | |
10 #include <math.h> | |
11 #include "pwplib.h" | |
12 | |
13 struct { | |
14 struct { | |
15 int wvlgt; | |
16 int wvleft; | |
17 int wvphase; | |
18 int wvlgt1; | |
54
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
19 } ch[3]; |
0 | 20 int freq; |
21 }pwpgb; | |
22 | |
54
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
23 |
0 | 24 void gb_sound(int chan,int freq,int volume,int ratio) |
25 { | |
26 if(volume&&freq){ | |
27 int wvlgt= | |
28 ((double)(pwpgb.freq*12)/pow(1.059465,freq/256.0)); | |
29 | |
30 if(ratio<1)ratio=1;else if(ratio>255)ratio=255; | |
31 | |
32 pwpgb.ch[chan].wvlgt=(wvlgt*ratio)>>7; | |
33 pwpgb.ch[chan].wvlgt1=2*pwpgb.ch[chan].wvlgt-wvlgt; | |
34 | |
35 pwpgb.ch[chan].wvleft=pwpgb.ch[chan].wvlgt/(chan+2); | |
36 pwpgb.ch[chan].wvphase=volume; | |
37 }else{ | |
38 pwpgb.ch[chan].wvphase=0; | |
39 pwpgb.ch[chan].wvleft=pwpgb.ch[chan].wvlgt=32123; | |
40 } | |
41 } | |
42 | |
43 void gb_init(int freq) | |
44 { | |
54
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
45 int i; |
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
46 pwpgb.freq = freq; |
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
47 for(i = 0; i < 3; i++) |
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
48 gb_sound(i,0,0,0); |
0 | 49 } |
50 | |
54
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
51 static void gb_gen1chan(u8 *d, int l, int freq) |
0 | 52 { |
53 int ph=128*256; | |
54 while(l--){ | |
55 *d++=64+((ph>>8)&0x80); | |
56 ph+=freq; | |
57 } | |
58 } | |
59 | |
60 #define CH pwpgb.ch | |
61 | |
54
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
62 static void gb_beepemu(u8 *d, int l) |
0 | 63 { |
64 static int pf=0; | |
65 int | |
66 fq0=CH[0].wvlgt, | |
67 fq1=CH[1].wvlgt, | |
68 fq2=CH[2].wvlgt,fq; | |
69 if(CH[0].wvphase==0)fq0=32768; | |
70 if(CH[1].wvphase==0)fq1=32768; | |
71 if(CH[2].wvphase==0)fq2=32768; | |
72 | |
73 if(fq0>fq1){int tmp=fq0;fq0=fq1;fq1=tmp;} | |
74 if(fq0>fq2){int tmp=fq0;fq0=fq2;fq2=tmp;} | |
75 if(fq1>fq2){int tmp=fq1;fq1=fq2;fq2=tmp;} | |
76 | |
77 if(pf&1){ | |
78 if(pf&2)fq=fq1;else fq=fq2; | |
79 if(!fq)fq=fq0; | |
80 }else{ | |
81 fq=fq0; | |
82 if(fq==32768){fq=fq1; | |
83 if(fq==32768)fq=fq2;} | |
84 } | |
85 if(fq==32768)memset(d,128,l);else | |
86 { | |
87 fq=(65536*256)/fq; | |
88 gb_gen1chan(d,l,fq); | |
89 } | |
90 pf++; | |
91 } | |
92 | |
54
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
93 void gb_genwave(u8 *d, int l) |
0 | 94 { |
95 int remain=0; | |
96 | |
97 l<<=8; | |
98 while(l) | |
99 { | |
100 int min=CH[0].wvleft,w,sum; | |
101 w=CH[1].wvleft;if(w<min)min=w; | |
102 w=CH[2].wvleft;if(w<min)min=w; | |
103 | |
104 sum=CH[0].wvphase+CH[1].wvphase+CH[2].wvphase+128; | |
105 if(sum<0)sum=0;else if(sum>255)sum=255; | |
106 | |
107 if(min>l)min=l; l-=min; | |
108 memset(d,sum,(min+remain)>>8); | |
109 d+=((min+remain)>>8); | |
110 remain=(min+remain)&255; | |
111 | |
112 {int i=0; | |
113 for(;i<3;i++){ | |
114 w=CH[i].wvleft-=min; | |
115 if(!w) | |
116 { | |
117 CH[i].wvleft=CH[i].wvlgt1; | |
118 CH[i].wvlgt1=CH[i].wvlgt; | |
119 CH[i].wvlgt=CH[i].wvleft; | |
120 CH[i].wvphase=0-CH[i].wvphase; | |
121 } | |
122 } | |
123 } | |
124 } | |
125 } | |
126 | |
127 /* my_memset, ... */ | |
128 | |
129 /* | |
130 gb_genwave_opt(char*buf,int endptr) | |
131 { | |
132 #define SETTILL(a) \ | |
133 {int bord=w##a,rs;if(bord>endptr)goto last;\ | |
134 rs=sum;if(rs&~0xff){if(rs<0)rs=0;else rs=255;}\ | |
135 memset(buf+(ptr>>8),rs,2+((bord-ptr)>>8));ptr=bord;\ | |
136 CH[a].wvphase=0-CH[a].wvphase;\ | |
137 sum+=(CH[a].wvphase)<<1;\ | |
138 w##a=ptr+CH[a].wvlgt;} | |
139 #define HANDLE(s0,s1,s2) \ | |
140 {SETTILL(s0); \ | |
141 if(w##s0<=w##s1)goto w##s0##w##s1##w##s2;else \ | |
142 if(w##s0<=w##s2)goto w##s1##w##s0##w##s2;else \ | |
143 goto w##s1##w##s2##w##s0;} | |
144 | |
145 int ptr=0,sum=CH[0].wvphase+ | |
146 CH[1].wvphase+ | |
147 CH[2].wvphase; | |
148 | |
149 int w0=CH[0].wvleft, | |
150 w1=CH[1].wvleft, | |
151 w2=CH[2].wvleft; | |
152 | |
153 endptr<<=8; | |
154 | |
155 w1w2w0:HANDLE(1,2,0); | |
156 w2w1w0:HANDLE(2,1,0); | |
157 w0w2w1:HANDLE(0,2,1); | |
158 w2w0w1:HANDLE(2,0,1); | |
159 w0w1w2:HANDLE(0,1,2); | |
160 w1w0w2:HANDLE(1,0,2); | |
161 | |
162 last: | |
163 {int rs=sum; | |
164 if(rs&~0xff){if(rs<0)rs=0;else rs=255;} | |
165 memset(buf+(ptr>>8),rs,(endptr-ptr)>>8);} | |
166 | |
167 CH[0].wvleft=w0-endptr; | |
168 CH[1].wvleft=w1-endptr; | |
169 CH[2].wvleft=w2-endptr; | |
170 | |
171 #undef SETTILL | |
172 } | |
173 */ | |
174 | |
175 #if (0) | |
176 gb_genwave_opt(char*buf,int endptr) | |
177 { | |
178 int ptr=0,sum=CH[0].wvphase+ | |
179 CH[1].wvphase+ | |
180 CH[2].wvphase; | |
181 | |
182 #define w0 CH[0].wvleft | |
183 #define w1 CH[1].wvleft | |
184 #define w2 CH[2].wvleft | |
185 | |
186 /* volatile int w0=CH[0].wvleft, | |
187 w1=CH[1].wvleft, | |
188 w2=CH[2].wvleft;*/ | |
189 | |
190 endptr<<=8; | |
191 | |
192 #define SETTILL(a) \ | |
193 {int bord=w##a,rs;if(bord>endptr)goto last;\ | |
194 rs=sum;if(rs&~0xff){if(rs<0)rs=0;else rs=255;}\ | |
195 memset(buf+(ptr>>8),rs,2+((bord-(ptr&~0xff))>>8));ptr=bord;\ | |
196 CH[a].wvphase=0-CH[a].wvphase;\ | |
197 sum+=(CH[a].wvphase)<<1;\ | |
198 w##a=ptr+CH[a].wvlgt;} | |
199 | |
200 if(w0<=w1){ | |
201 if(w1<=w2)goto w0w1w2;else | |
202 if(w2<=w0)goto w2w0w1;else | |
203 goto w0w2w1; | |
204 }else{ | |
205 if(w0<=w2)goto w1w0w2;else | |
206 if(w2<=w1)goto w2w1w0; //.. else w1w2w0 (next) | |
207 } | |
208 | |
209 w1w2w0: | |
210 SETTILL(1); | |
211 if(w1<=w2)goto w1w2w0;else | |
212 if(w1<=w0)goto w2w1w0;else //.. if(w1>w0)goto w2w0w1; | |
213 goto w2w0w1; | |
214 w2w1w0: | |
215 SETTILL(2); | |
216 if(w2<=w1)goto w2w1w0;else | |
217 if(w2<=w0)goto w1w2w0;else | |
218 goto w1w0w2; | |
219 | |
220 w0w2w1: | |
221 SETTILL(0); | |
222 if(w0<=w2)goto w0w2w1;else | |
223 if(w0<=w1)goto w2w0w1;else //.. if(w0>w1)goto w2w1w0; | |
224 goto w2w1w0; | |
225 w2w0w1: | |
226 SETTILL(2); | |
227 if(w2<=w0)goto w2w0w1;else | |
228 if(w2<=w1)goto w0w2w1;else | |
229 goto w0w1w2; //.. not reqd | |
230 | |
231 w0w1w2: | |
232 SETTILL(0); | |
233 if(w0<=w1)goto w0w1w2;else | |
234 if(w0<=w2)goto w1w0w2;else //.. if(w0>w2)goto w1w2w0; | |
235 goto w1w2w0; | |
236 | |
237 w1w0w2: | |
238 SETTILL(1); | |
239 if(w1<=w0)goto w1w0w2;else | |
240 if(w1<=w2)goto w0w1w2;else | |
241 goto w0w2w1; | |
242 | |
243 last: | |
244 {int rs=sum; | |
245 if(rs&~0xff){if(rs<0)rs=0;else rs=255;} | |
246 memset(buf+(ptr>>8),rs,2+((endptr-ptr)>>8));} | |
247 | |
248 CH[0].wvleft=w0-endptr; | |
249 CH[1].wvleft=w1-endptr; | |
250 CH[2].wvleft=w2-endptr; | |
251 | |
252 #undef SETTILL | |
253 } | |
254 #endif | |
255 | |
256 #endif |