24 #ifndef ENVELOPEGENERATOR_H
25 #define ENVELOPEGENERATOR_H
27 #include "siddefs-fp.h"
52 ATTACK, DECAY_SUSTAIN, RELEASE
66 unsigned int exponential_counter;
72 unsigned int exponential_counter_period;
74 unsigned int state_pipeline;
77 unsigned int envelope_pipeline;
79 unsigned int exponential_pipeline;
95 unsigned char envelope_counter;
104 unsigned char sustain;
107 unsigned char release;
120 static const unsigned int adsrtable[16];
123 void set_exponential_counter();
147 float output()
const {
return dac[envelope_counter]; }
155 exponential_counter(0),
156 exponential_counter_period(1),
158 envelope_pipeline(0),
159 exponential_pipeline(0),
162 counter_enabled(true),
165 envelope_counter(0xaa),
207 unsigned char readENV()
const {
return env3; }
212 #if RESID_INLINING || defined(ENVELOPEGENERATOR_CPP)
220 env3 = envelope_counter;
222 if (unlikely(state_pipeline))
227 if (unlikely(envelope_pipeline != 0) && (--envelope_pipeline == 0))
229 if (likely(counter_enabled))
233 if (++envelope_counter==0xff)
235 state = DECAY_SUSTAIN;
236 rate = adsrtable[decay];
239 else if ((state == DECAY_SUSTAIN) || (state == RELEASE))
241 if (--envelope_counter==0x00)
243 counter_enabled =
false;
247 set_exponential_counter();
250 else if (unlikely(exponential_pipeline != 0) && (--exponential_pipeline == 0))
252 exponential_counter = 0;
254 if (((state == DECAY_SUSTAIN) && (envelope_counter != sustain))
255 || (state == RELEASE))
262 envelope_pipeline = 1;
265 else if (unlikely(resetLfsr))
274 exponential_counter = 0;
281 envelope_pipeline = 2;
285 if (counter_enabled && (++exponential_counter == exponential_counter_period))
286 exponential_pipeline = exponential_counter_period != 1 ? 2 : 1;
298 if (likely(lfsr != rate))
302 const unsigned int feedback = ((lfsr << 14) ^ (lfsr << 13)) & 0x4000;
303 lfsr = (lfsr >> 1) | feedback;
351 void EnvelopeGenerator::state_change()
358 if (state_pipeline == 0)
362 rate = adsrtable[attack];
363 counter_enabled =
true;
369 if (((state == ATTACK) && (state_pipeline == 0))
370 || ((state == DECAY_SUSTAIN) && (state_pipeline == 1)))
373 rate = adsrtable[release];
380 void EnvelopeGenerator::set_exponential_counter()
386 switch (envelope_counter)
389 exponential_counter_period = 1;
393 exponential_counter_period = 2;
397 exponential_counter_period = 4;
401 exponential_counter_period = 8;
405 exponential_counter_period = 16;
409 exponential_counter_period = 30;
413 exponential_counter_period = 1;