Commit | Line | Data |
---|---|---|
ef79bbde P |
1 | /***************************************************************************\r |
2 | adsr.c - description\r | |
3 | -------------------\r | |
4 | begin : Wed May 15 2002\r | |
5 | copyright : (C) 2002 by Pete Bernert\r | |
6 | email : BlackDove@addcom.de\r | |
7 | ***************************************************************************/\r | |
8 | /***************************************************************************\r | |
9 | * *\r | |
10 | * This program is free software; you can redistribute it and/or modify *\r | |
11 | * it under the terms of the GNU General Public License as published by *\r | |
12 | * the Free Software Foundation; either version 2 of the License, or *\r | |
13 | * (at your option) any later version. See also the license.txt file for *\r | |
14 | * additional informations. *\r | |
15 | * *\r | |
16 | ***************************************************************************/\r | |
17 | \r | |
18 | #include "stdafx.h"\r | |
19 | \r | |
20 | #define _IN_ADSR\r | |
21 | \r | |
22 | // will be included from spu.c\r | |
23 | #ifdef _IN_SPU\r | |
24 | \r | |
25 | ////////////////////////////////////////////////////////////////////////\r | |
26 | // ADSR func\r | |
27 | ////////////////////////////////////////////////////////////////////////\r | |
28 | \r | |
29 | unsigned long RateTable[160];\r | |
30 | \r | |
31 | void InitADSR(void) // INIT ADSR\r | |
32 | {\r | |
33 | unsigned long r,rs,rd;int i;\r | |
34 | \r | |
35 | memset(RateTable,0,sizeof(unsigned long)*160); // build the rate table according to Neill's rules (see at bottom of file)\r | |
36 | \r | |
37 | r=3;rs=1;rd=0;\r | |
38 | \r | |
39 | for(i=32;i<160;i++) // we start at pos 32 with the real values... everything before is 0\r | |
40 | {\r | |
41 | if(r<0x3FFFFFFF)\r | |
42 | {\r | |
43 | r+=rs;\r | |
44 | rd++;if(rd==5) {rd=1;rs*=2;}\r | |
45 | }\r | |
46 | if(r>0x3FFFFFFF) r=0x3FFFFFFF;\r | |
47 | \r | |
48 | RateTable[i]=r;\r | |
49 | }\r | |
50 | }\r | |
51 | \r | |
52 | ////////////////////////////////////////////////////////////////////////\r | |
53 | \r | |
54 | INLINE void StartADSR(int ch) // MIX ADSR\r | |
55 | {\r | |
56 | s_chan[ch].ADSRX.lVolume=1; // and init some adsr vars\r | |
57 | s_chan[ch].ADSRX.State=0;\r | |
58 | s_chan[ch].ADSRX.EnvelopeVol=0;\r | |
59 | }\r | |
60 | \r | |
61 | ////////////////////////////////////////////////////////////////////////\r | |
62 | \r | |
63 | INLINE int MixADSR(int ch) // MIX ADSR\r | |
64 | { \r | |
65 | if(s_chan[ch].bStop) // should be stopped:\r | |
66 | { // do release\r | |
67 | if(s_chan[ch].ADSRX.ReleaseModeExp)\r | |
68 | {\r | |
69 | switch((s_chan[ch].ADSRX.EnvelopeVol>>28)&0x7)\r | |
70 | {\r | |
71 | case 0: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +0 + 32]; break;\r | |
72 | case 1: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +4 + 32]; break;\r | |
73 | case 2: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +6 + 32]; break;\r | |
74 | case 3: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +8 + 32]; break;\r | |
75 | case 4: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +9 + 32]; break;\r | |
76 | case 5: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +10+ 32]; break;\r | |
77 | case 6: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +11+ 32]; break;\r | |
78 | case 7: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +12+ 32]; break;\r | |
79 | }\r | |
80 | }\r | |
81 | else\r | |
82 | {\r | |
83 | s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x0C + 32];\r | |
84 | }\r | |
85 | \r | |
86 | if(s_chan[ch].ADSRX.EnvelopeVol<0) \r | |
87 | {\r | |
88 | s_chan[ch].ADSRX.EnvelopeVol=0;\r | |
08cfd5e5 | 89 | // don't stop if this chan can still cause irqs\r |
90 | if(!(spuCtrl&0x40) || (s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq))\r | |
91 | //s_chan[ch].bOn=0;\r | |
92 | s_chan[ch].pCurr=(unsigned char *)-1;\r | |
ef79bbde P |
93 | //s_chan[ch].bReverb=0;\r |
94 | //s_chan[ch].bNoise=0;\r | |
95 | }\r | |
96 | \r | |
97 | s_chan[ch].ADSRX.lVolume=s_chan[ch].ADSRX.EnvelopeVol>>21;\r | |
98 | return s_chan[ch].ADSRX.lVolume;\r | |
99 | }\r | |
100 | else // not stopped yet?\r | |
101 | {\r | |
102 | if(s_chan[ch].ADSRX.State==0) // -> attack\r | |
103 | {\r | |
104 | if(s_chan[ch].ADSRX.AttackModeExp)\r | |
105 | {\r | |
106 | if(s_chan[ch].ADSRX.EnvelopeVol<0x60000000) \r | |
107 | s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.AttackRate^0x7F)-0x10 + 32];\r | |
108 | else\r | |
109 | s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.AttackRate^0x7F)-0x18 + 32];\r | |
110 | }\r | |
111 | else\r | |
112 | {\r | |
113 | s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.AttackRate^0x7F)-0x10 + 32];\r | |
114 | }\r | |
115 | \r | |
116 | if(s_chan[ch].ADSRX.EnvelopeVol<0) \r | |
117 | {\r | |
118 | s_chan[ch].ADSRX.EnvelopeVol=0x7FFFFFFF;\r | |
119 | s_chan[ch].ADSRX.State=1;\r | |
120 | }\r | |
121 | \r | |
122 | s_chan[ch].ADSRX.lVolume=s_chan[ch].ADSRX.EnvelopeVol>>21;\r | |
123 | return s_chan[ch].ADSRX.lVolume;\r | |
124 | }\r | |
125 | //--------------------------------------------------//\r | |
126 | if(s_chan[ch].ADSRX.State==1) // -> decay\r | |
127 | {\r | |
128 | switch((s_chan[ch].ADSRX.EnvelopeVol>>28)&0x7)\r | |
129 | {\r | |
130 | case 0: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+0 + 32]; break;\r | |
131 | case 1: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+4 + 32]; break;\r | |
132 | case 2: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+6 + 32]; break;\r | |
133 | case 3: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+8 + 32]; break;\r | |
134 | case 4: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+9 + 32]; break;\r | |
135 | case 5: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+10+ 32]; break;\r | |
136 | case 6: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+11+ 32]; break;\r | |
137 | case 7: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+12+ 32]; break;\r | |
138 | }\r | |
139 | \r | |
140 | if(s_chan[ch].ADSRX.EnvelopeVol<0) s_chan[ch].ADSRX.EnvelopeVol=0;\r | |
141 | if(((s_chan[ch].ADSRX.EnvelopeVol>>27)&0xF) <= s_chan[ch].ADSRX.SustainLevel)\r | |
142 | {\r | |
143 | s_chan[ch].ADSRX.State=2;\r | |
144 | }\r | |
145 | \r | |
146 | s_chan[ch].ADSRX.lVolume=s_chan[ch].ADSRX.EnvelopeVol>>21;\r | |
147 | return s_chan[ch].ADSRX.lVolume;\r | |
148 | }\r | |
149 | //--------------------------------------------------//\r | |
150 | if(s_chan[ch].ADSRX.State==2) // -> sustain\r | |
151 | {\r | |
152 | if(s_chan[ch].ADSRX.SustainIncrease)\r | |
153 | {\r | |
154 | if(s_chan[ch].ADSRX.SustainModeExp)\r | |
155 | {\r | |
156 | if(s_chan[ch].ADSRX.EnvelopeVol<0x60000000) \r | |
157 | s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.SustainRate^0x7F)-0x10 + 32];\r | |
158 | else\r | |
159 | s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.SustainRate^0x7F)-0x18 + 32];\r | |
160 | }\r | |
161 | else\r | |
162 | {\r | |
163 | s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.SustainRate^0x7F)-0x10 + 32];\r | |
164 | }\r | |
165 | \r | |
166 | if(s_chan[ch].ADSRX.EnvelopeVol<0) \r | |
167 | {\r | |
168 | s_chan[ch].ADSRX.EnvelopeVol=0x7FFFFFFF;\r | |
169 | }\r | |
170 | }\r | |
171 | else\r | |
172 | {\r | |
173 | if(s_chan[ch].ADSRX.SustainModeExp)\r | |
174 | {\r | |
175 | switch((s_chan[ch].ADSRX.EnvelopeVol>>28)&0x7)\r | |
176 | {\r | |
177 | case 0: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +0 + 32];break;\r | |
178 | case 1: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +4 + 32];break;\r | |
179 | case 2: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +6 + 32];break;\r | |
180 | case 3: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +8 + 32];break;\r | |
181 | case 4: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +9 + 32];break;\r | |
182 | case 5: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +10+ 32];break;\r | |
183 | case 6: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +11+ 32];break;\r | |
184 | case 7: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +12+ 32];break;\r | |
185 | }\r | |
186 | }\r | |
187 | else\r | |
188 | {\r | |
189 | s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x0F + 32];\r | |
190 | }\r | |
191 | \r | |
192 | if(s_chan[ch].ADSRX.EnvelopeVol<0) \r | |
193 | {\r | |
194 | s_chan[ch].ADSRX.EnvelopeVol=0;\r | |
195 | }\r | |
196 | }\r | |
197 | s_chan[ch].ADSRX.lVolume=s_chan[ch].ADSRX.EnvelopeVol>>21;\r | |
198 | return s_chan[ch].ADSRX.lVolume;\r | |
199 | }\r | |
200 | }\r | |
201 | return 0;\r | |
202 | }\r | |
203 | \r | |
204 | #endif\r | |
205 | \r | |
206 | /*\r | |
207 | James Higgs ADSR investigations:\r | |
208 | \r | |
209 | PSX SPU Envelope Timings\r | |
210 | ~~~~~~~~~~~~~~~~~~~~~~~~\r | |
211 | \r | |
212 | First, here is an extract from doomed's SPU doc, which explains the basics\r | |
213 | of the SPU "volume envelope": \r | |
214 | \r | |
215 | *** doomed doc extract start ***\r | |
216 | \r | |
217 | --------------------------------------------------------------------------\r | |
218 | Voices.\r | |
219 | --------------------------------------------------------------------------\r | |
220 | The SPU has 24 hardware voices. These voices can be used to reproduce sample\r | |
221 | data, noise or can be used as frequency modulator on the next voice.\r | |
222 | Each voice has it's own programmable ADSR envelope filter. The main volume\r | |
223 | can be programmed independently for left and right output.\r | |
224 | \r | |
225 | The ADSR envelope filter works as follows:\r | |
226 | Ar = Attack rate, which specifies the speed at which the volume increases\r | |
227 | from zero to it's maximum value, as soon as the note on is given. The\r | |
228 | slope can be set to lineair or exponential.\r | |
229 | Dr = Decay rate specifies the speed at which the volume decreases to the\r | |
230 | sustain level. Decay is always decreasing exponentially.\r | |
231 | Sl = Sustain level, base level from which sustain starts.\r | |
232 | Sr = Sustain rate is the rate at which the volume of the sustained note\r | |
233 | increases or decreases. This can be either lineair or exponential.\r | |
234 | Rr = Release rate is the rate at which the volume of the note decreases\r | |
235 | as soon as the note off is given.\r | |
236 | \r | |
237 | lvl |\r | |
238 | ^ | /\Dr __\r | |
239 | Sl _| _ / _ \__--- \\r | |
240 | | / ---__ \ Rr\r | |
241 | | /Ar Sr \ \\r | |
242 | | / \\\r | |
243 | |/___________________\________\r | |
244 | ->time\r | |
245 | \r | |
246 | The overal volume can also be set to sweep up or down lineairly or\r | |
247 | exponentially from it's current value. This can be done seperately\r | |
248 | for left and right.\r | |
249 | \r | |
250 | Relevant SPU registers:\r | |
251 | -------------------------------------------------------------\r | |
252 | $1f801xx8 Attack/Decay/Sustain level\r | |
253 | bit |0f|0e 0d 0c 0b 0a 09 08|07 06 05 04|03 02 01 00|\r | |
254 | desc.|Am| Ar |Dr |Sl |\r | |
255 | \r | |
256 | Am 0 Attack mode Linear\r | |
257 | 1 Exponential\r | |
258 | \r | |
259 | Ar 0-7f attack rate\r | |
260 | Dr 0-f decay rate\r | |
261 | Sl 0-f sustain level\r | |
262 | -------------------------------------------------------------\r | |
263 | $1f801xxa Sustain rate, Release Rate.\r | |
264 | bit |0f|0e|0d|0c 0b 0a 09 08 07 06|05|04 03 02 01 00|\r | |
265 | desc.|Sm|Sd| 0| Sr |Rm|Rr |\r | |
266 | \r | |
267 | Sm 0 sustain rate mode linear\r | |
268 | 1 exponential\r | |
269 | Sd 0 sustain rate mode increase\r | |
270 | 1 decrease\r | |
271 | Sr 0-7f Sustain Rate\r | |
272 | Rm 0 Linear decrease\r | |
273 | 1 Exponential decrease\r | |
274 | Rr 0-1f Release Rate\r | |
275 | \r | |
276 | Note: decay mode is always Expontial decrease, and thus cannot\r | |
277 | be set.\r | |
278 | -------------------------------------------------------------\r | |
279 | $1f801xxc Current ADSR volume\r | |
280 | bit |0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00|\r | |
281 | desc.|ADSRvol |\r | |
282 | \r | |
283 | ADSRvol Returns the current envelope volume when\r | |
284 | read.\r | |
285 | -- James' Note: return range: 0 -> 32767\r | |
286 | \r | |
287 | *** doomed doc extract end *** \r | |
288 | \r | |
289 | By using a small PSX proggie to visualise the envelope as it was played,\r | |
290 | the following results for envelope timing were obtained:\r | |
291 | \r | |
292 | 1. Attack rate value (linear mode)\r | |
293 | \r | |
294 | Attack value range: 0 -> 127\r | |
295 | \r | |
296 | Value | 48 | 52 | 56 | 60 | 64 | 68 | 72 | | 80 |\r | |
297 | -----------------------------------------------------------------\r | |
298 | Frames | 11 | 21 | 42 | 84 | 169| 338| 676| |2890|\r | |
299 | \r | |
300 | Note: frames is no. of PAL frames to reach full volume (100%\r | |
301 | amplitude)\r | |
302 | \r | |
303 | Hmm, noticing that the time taken to reach full volume doubles\r | |
304 | every time we add 4 to our attack value, we know the equation is\r | |
305 | of form:\r | |
306 | frames = k * 2 ^ (value / 4)\r | |
307 | \r | |
308 | (You may ponder about envelope generator hardware at this point,\r | |
309 | or maybe not... :)\r | |
310 | \r | |
311 | By substituting some stuff and running some checks, we get:\r | |
312 | \r | |
313 | k = 0.00257 (close enuf)\r | |
314 | \r | |
315 | therefore,\r | |
316 | frames = 0.00257 * 2 ^ (value / 4)\r | |
317 | If you just happen to be writing an emulator, then you can probably\r | |
318 | use an equation like:\r | |
319 | \r | |
320 | %volume_increase_per_tick = 1 / frames\r | |
321 | \r | |
322 | \r | |
323 | ------------------------------------\r | |
324 | Pete:\r | |
325 | ms=((1<<(value>>2))*514)/10000\r | |
326 | ------------------------------------\r | |
327 | \r | |
328 | 2. Decay rate value (only has log mode)\r | |
329 | \r | |
330 | Decay value range: 0 -> 15\r | |
331 | \r | |
332 | Value | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |\r | |
333 | ------------------------------------------------\r | |
334 | frames | | | | | 6 | 12 | 24 | 47 |\r | |
335 | \r | |
336 | Note: frames here is no. of PAL frames to decay to 50% volume.\r | |
337 | \r | |
338 | formula: frames = k * 2 ^ (value)\r | |
339 | \r | |
340 | Substituting, we get: k = 0.00146\r | |
341 | \r | |
342 | Further info on logarithmic nature:\r | |
343 | frames to decay to sustain level 3 = 3 * frames to decay to \r | |
344 | sustain level 9\r | |
345 | \r | |
346 | Also no. of frames to 25% volume = roughly 1.85 * no. of frames to\r | |
347 | 50% volume.\r | |
348 | \r | |
349 | Frag it - just use linear approx.\r | |
350 | \r | |
351 | ------------------------------------\r | |
352 | Pete:\r | |
353 | ms=((1<<value)*292)/10000\r | |
354 | ------------------------------------\r | |
355 | \r | |
356 | \r | |
357 | 3. Sustain rate value (linear mode)\r | |
358 | \r | |
359 | Sustain rate range: 0 -> 127\r | |
360 | \r | |
361 | Value | 48 | 52 | 56 | 60 | 64 | 68 | 72 |\r | |
362 | -------------------------------------------\r | |
363 | frames | 9 | 19 | 37 | 74 | 147| 293| 587|\r | |
364 | \r | |
365 | Here, frames = no. of PAL frames for volume amplitude to go from 100%\r | |
366 | to 0% (or vice-versa).\r | |
367 | \r | |
368 | Same formula as for attack value, just a different value for k:\r | |
369 | \r | |
370 | k = 0.00225\r | |
371 | \r | |
372 | ie: frames = 0.00225 * 2 ^ (value / 4)\r | |
373 | \r | |
374 | For emulation purposes:\r | |
375 | \r | |
376 | %volume_increase_or_decrease_per_tick = 1 / frames\r | |
377 | \r | |
378 | ------------------------------------\r | |
379 | Pete:\r | |
380 | ms=((1<<(value>>2))*450)/10000\r | |
381 | ------------------------------------\r | |
382 | \r | |
383 | \r | |
384 | 4. Release rate (linear mode)\r | |
385 | \r | |
386 | Release rate range: 0 -> 31\r | |
387 | \r | |
388 | Value | 13 | 14 | 15 | 16 | 17 |\r | |
389 | ---------------------------------------------------------------\r | |
390 | frames | 18 | 36 | 73 | 146| 292|\r | |
391 | \r | |
392 | Here, frames = no. of PAL frames to decay from 100% vol to 0% vol\r | |
393 | after "note-off" is triggered.\r | |
394 | \r | |
395 | Formula: frames = k * 2 ^ (value)\r | |
396 | \r | |
397 | And so: k = 0.00223\r | |
398 | \r | |
399 | ------------------------------------\r | |
400 | Pete:\r | |
401 | ms=((1<<value)*446)/10000\r | |
402 | ------------------------------------\r | |
403 | \r | |
404 | \r | |
405 | Other notes: \r | |
406 | \r | |
407 | Log stuff not figured out. You may get some clues from the "Decay rate"\r | |
408 | stuff above. For emu purposes it may not be important - use linear\r | |
409 | approx.\r | |
410 | \r | |
411 | To get timings in millisecs, multiply frames by 20.\r | |
412 | \r | |
413 | \r | |
414 | \r | |
415 | - James Higgs 17/6/2000\r | |
416 | james7780@yahoo.com\r | |
417 | \r | |
418 | //---------------------------------------------------------------\r | |
419 | \r | |
420 | OLD adsr mixing according to james' rules... has to be called\r | |
421 | every one millisecond\r | |
422 | \r | |
423 | \r | |
424 | long v,v2,lT,l1,l2,l3;\r | |
425 | \r | |
426 | if(s_chan[ch].bStop) // psx wants to stop? -> release phase\r | |
427 | {\r | |
428 | if(s_chan[ch].ADSR.ReleaseVal!=0) // -> release not 0: do release (if 0: stop right now)\r | |
429 | {\r | |
430 | if(!s_chan[ch].ADSR.ReleaseVol) // --> release just started? set up the release stuff\r | |
431 | {\r | |
432 | s_chan[ch].ADSR.ReleaseStartTime=s_chan[ch].ADSR.lTime;\r | |
433 | s_chan[ch].ADSR.ReleaseVol=s_chan[ch].ADSR.lVolume;\r | |
434 | s_chan[ch].ADSR.ReleaseTime = // --> calc how long does it take to reach the wanted sus level\r | |
435 | (s_chan[ch].ADSR.ReleaseTime*\r | |
436 | s_chan[ch].ADSR.ReleaseVol)/1024;\r | |
437 | }\r | |
438 | // -> NO release exp mode used (yet)\r | |
439 | v=s_chan[ch].ADSR.ReleaseVol; // -> get last volume\r | |
440 | lT=s_chan[ch].ADSR.lTime- // -> how much time is past?\r | |
441 | s_chan[ch].ADSR.ReleaseStartTime;\r | |
442 | l1=s_chan[ch].ADSR.ReleaseTime;\r | |
443 | \r | |
444 | if(lT<l1) // -> we still have to release\r | |
445 | {\r | |
446 | v=v-((v*lT)/l1); // --> calc new volume\r | |
447 | }\r | |
448 | else // -> release is over: now really stop that sample\r | |
449 | {v=0;s_chan[ch].bOn=0;s_chan[ch].ADSR.ReleaseVol=0;s_chan[ch].bNoise=0;}\r | |
450 | }\r | |
451 | else // -> release IS 0: release at once\r | |
452 | {\r | |
453 | v=0;s_chan[ch].bOn=0;s_chan[ch].ADSR.ReleaseVol=0;s_chan[ch].bNoise=0;\r | |
454 | }\r | |
455 | }\r | |
456 | else \r | |
457 | {//--------------------------------------------------// not in release phase:\r | |
458 | v=1024;\r | |
459 | lT=s_chan[ch].ADSR.lTime;\r | |
460 | l1=s_chan[ch].ADSR.AttackTime;\r | |
461 | \r | |
462 | if(lT<l1) // attack\r | |
463 | { // no exp mode used (yet)\r | |
464 | // if(s_chan[ch].ADSR.AttackModeExp)\r | |
465 | // {\r | |
466 | // v=(v*lT)/l1;\r | |
467 | // }\r | |
468 | // else\r | |
469 | {\r | |
470 | v=(v*lT)/l1;\r | |
471 | }\r | |
472 | if(v==0) v=1;\r | |
473 | }\r | |
474 | else // decay\r | |
475 | { // should be exp, but who cares? ;)\r | |
476 | l2=s_chan[ch].ADSR.DecayTime;\r | |
477 | v2=s_chan[ch].ADSR.SustainLevel;\r | |
478 | \r | |
479 | lT-=l1;\r | |
480 | if(lT<l2)\r | |
481 | {\r | |
482 | v-=(((v-v2)*lT)/l2);\r | |
483 | }\r | |
484 | else // sustain\r | |
485 | { // no exp mode used (yet)\r | |
486 | l3=s_chan[ch].ADSR.SustainTime;\r | |
487 | lT-=l2;\r | |
488 | if(s_chan[ch].ADSR.SustainModeDec>0)\r | |
489 | {\r | |
490 | if(l3!=0) v2+=((v-v2)*lT)/l3;\r | |
491 | else v2=v;\r | |
492 | }\r | |
493 | else\r | |
494 | {\r | |
495 | if(l3!=0) v2-=(v2*lT)/l3;\r | |
496 | else v2=v;\r | |
497 | }\r | |
498 | \r | |
499 | if(v2>v) v2=v;\r | |
500 | if(v2<=0) {v2=0;s_chan[ch].bOn=0;s_chan[ch].ADSR.ReleaseVol=0;s_chan[ch].bNoise=0;}\r | |
501 | \r | |
502 | v=v2;\r | |
503 | }\r | |
504 | }\r | |
505 | }\r | |
506 | \r | |
507 | //----------------------------------------------------// \r | |
508 | // ok, done for this channel, so increase time\r | |
509 | \r | |
510 | s_chan[ch].ADSR.lTime+=1; // 1 = 1.020408f ms; \r | |
511 | \r | |
512 | if(v>1024) v=1024; // adjust volume\r | |
513 | if(v<0) v=0; \r | |
514 | s_chan[ch].ADSR.lVolume=v; // store act volume\r | |
515 | \r | |
516 | return v; // return the volume factor\r | |
517 | */\r | |
518 | \r | |
519 | \r | |
520 | //-----------------------------------------------------------------------------\r | |
521 | //-----------------------------------------------------------------------------\r | |
522 | //-----------------------------------------------------------------------------\r | |
523 | \r | |
524 | \r | |
525 | /*\r | |
526 | -----------------------------------------------------------------------------\r | |
527 | Neill Corlett\r | |
528 | Playstation SPU envelope timing notes\r | |
529 | -----------------------------------------------------------------------------\r | |
530 | \r | |
531 | This is preliminary. This may be wrong. But the model described herein fits\r | |
532 | all of my experimental data, and it's just simple enough to sound right.\r | |
533 | \r | |
534 | ADSR envelope level ranges from 0x00000000 to 0x7FFFFFFF internally.\r | |
535 | The value returned by channel reg 0xC is (envelope_level>>16).\r | |
536 | \r | |
537 | Each sample, an increment or decrement value will be added to or\r | |
538 | subtracted from this envelope level.\r | |
539 | \r | |
540 | Create the rate log table. The values double every 4 entries.\r | |
541 | entry #0 = 4\r | |
542 | \r | |
543 | 4, 5, 6, 7,\r | |
544 | 8,10,12,14,\r | |
545 | 16,20,24,28, ...\r | |
546 | \r | |
547 | entry #40 = 4096...\r | |
548 | entry #44 = 8192...\r | |
549 | entry #48 = 16384...\r | |
550 | entry #52 = 32768...\r | |
551 | entry #56 = 65536...\r | |
552 | \r | |
553 | increments and decrements are in terms of ratelogtable[n]\r | |
554 | n may exceed the table bounds (plan on n being between -32 and 127).\r | |
555 | table values are all clipped between 0x00000000 and 0x3FFFFFFF\r | |
556 | \r | |
557 | when you "voice on", the envelope is always fully reset.\r | |
558 | (yes, it may click. the real thing does this too.)\r | |
559 | \r | |
560 | envelope level begins at zero.\r | |
561 | \r | |
562 | each state happens for at least 1 cycle\r | |
563 | (transitions are not instantaneous)\r | |
564 | this may result in some oddness: if the decay rate is uberfast, it will cut\r | |
565 | the envelope from full down to half in one sample, potentially skipping over\r | |
566 | the sustain level\r | |
567 | \r | |
568 | ATTACK\r | |
569 | ------\r | |
570 | - if the envelope level has overflowed past the max, clip to 0x7FFFFFFF and\r | |
571 | proceed to DECAY.\r | |
572 | \r | |
573 | Linear attack mode:\r | |
574 | - line extends upward to 0x7FFFFFFF\r | |
575 | - increment per sample is ratelogtable[(Ar^0x7F)-0x10]\r | |
576 | \r | |
577 | Logarithmic attack mode:\r | |
578 | if envelope_level < 0x60000000:\r | |
579 | - line extends upward to 0x60000000\r | |
580 | - increment per sample is ratelogtable[(Ar^0x7F)-0x10]\r | |
581 | else:\r | |
582 | - line extends upward to 0x7FFFFFFF\r | |
583 | - increment per sample is ratelogtable[(Ar^0x7F)-0x18]\r | |
584 | \r | |
585 | DECAY\r | |
586 | -----\r | |
587 | - if ((envelope_level>>27)&0xF) <= Sl, proceed to SUSTAIN.\r | |
588 | Do not clip to the sustain level.\r | |
589 | - current line ends at (envelope_level & 0x07FFFFFF)\r | |
590 | - decrement per sample depends on (envelope_level>>28)&0x7\r | |
591 | 0: ratelogtable[(4*(Dr^0x1F))-0x18+0]\r | |
592 | 1: ratelogtable[(4*(Dr^0x1F))-0x18+4]\r | |
593 | 2: ratelogtable[(4*(Dr^0x1F))-0x18+6]\r | |
594 | 3: ratelogtable[(4*(Dr^0x1F))-0x18+8]\r | |
595 | 4: ratelogtable[(4*(Dr^0x1F))-0x18+9]\r | |
596 | 5: ratelogtable[(4*(Dr^0x1F))-0x18+10]\r | |
597 | 6: ratelogtable[(4*(Dr^0x1F))-0x18+11]\r | |
598 | 7: ratelogtable[(4*(Dr^0x1F))-0x18+12]\r | |
599 | (note that this is the same as the release rate formula, except that\r | |
600 | decay rates 10-1F aren't possible... those would be slower in theory)\r | |
601 | \r | |
602 | SUSTAIN\r | |
603 | -------\r | |
604 | - no terminating condition except for voice off\r | |
605 | - Sd=0 (increase) behavior is identical to ATTACK for both log and linear.\r | |
606 | - Sd=1 (decrease) behavior:\r | |
607 | Linear sustain decrease:\r | |
608 | - line extends to 0x00000000\r | |
609 | - decrement per sample is ratelogtable[(Sr^0x7F)-0x0F]\r | |
610 | Logarithmic sustain decrease:\r | |
611 | - current line ends at (envelope_level & 0x07FFFFFF)\r | |
612 | - decrement per sample depends on (envelope_level>>28)&0x7\r | |
613 | 0: ratelogtable[(Sr^0x7F)-0x1B+0]\r | |
614 | 1: ratelogtable[(Sr^0x7F)-0x1B+4]\r | |
615 | 2: ratelogtable[(Sr^0x7F)-0x1B+6]\r | |
616 | 3: ratelogtable[(Sr^0x7F)-0x1B+8]\r | |
617 | 4: ratelogtable[(Sr^0x7F)-0x1B+9]\r | |
618 | 5: ratelogtable[(Sr^0x7F)-0x1B+10]\r | |
619 | 6: ratelogtable[(Sr^0x7F)-0x1B+11]\r | |
620 | 7: ratelogtable[(Sr^0x7F)-0x1B+12]\r | |
621 | \r | |
622 | RELEASE\r | |
623 | -------\r | |
624 | - if the envelope level has overflowed to negative, clip to 0 and QUIT.\r | |
625 | \r | |
626 | Linear release mode:\r | |
627 | - line extends to 0x00000000\r | |
628 | - decrement per sample is ratelogtable[(4*(Rr^0x1F))-0x0C]\r | |
629 | \r | |
630 | Logarithmic release mode:\r | |
631 | - line extends to (envelope_level & 0x0FFFFFFF)\r | |
632 | - decrement per sample depends on (envelope_level>>28)&0x7\r | |
633 | 0: ratelogtable[(4*(Rr^0x1F))-0x18+0]\r | |
634 | 1: ratelogtable[(4*(Rr^0x1F))-0x18+4]\r | |
635 | 2: ratelogtable[(4*(Rr^0x1F))-0x18+6]\r | |
636 | 3: ratelogtable[(4*(Rr^0x1F))-0x18+8]\r | |
637 | 4: ratelogtable[(4*(Rr^0x1F))-0x18+9]\r | |
638 | 5: ratelogtable[(4*(Rr^0x1F))-0x18+10]\r | |
639 | 6: ratelogtable[(4*(Rr^0x1F))-0x18+11]\r | |
640 | 7: ratelogtable[(4*(Rr^0x1F))-0x18+12]\r | |
641 | \r | |
642 | -----------------------------------------------------------------------------\r | |
643 | */\r | |
644 | \r |