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;
73 unsigned int new_exponential_counter_period;
75 unsigned int state_pipeline;
78 unsigned int envelope_pipeline;
80 unsigned int exponential_pipeline;
96 unsigned char envelope_counter;
105 unsigned char sustain;
108 unsigned char release;
121 static const unsigned int adsrtable[16];
124 void set_exponential_counter();
148 float output()
const {
return dac[envelope_counter]; }
156 exponential_counter(0),
157 exponential_counter_period(1),
158 new_exponential_counter_period(0),
160 envelope_pipeline(0),
161 exponential_pipeline(0),
164 counter_enabled(true),
167 envelope_counter(0xaa),
209 unsigned char readENV()
const {
return env3; }
214 #if RESID_INLINING || defined(ENVELOPEGENERATOR_CPP)
222 env3 = envelope_counter;
224 if (unlikely(new_exponential_counter_period > 0))
226 exponential_counter_period = new_exponential_counter_period;
227 new_exponential_counter_period = 0;
230 if (unlikely(state_pipeline))
235 if (unlikely(envelope_pipeline != 0) && (--envelope_pipeline == 0))
237 if (likely(counter_enabled))
241 if (++envelope_counter==0xff)
243 next_state = DECAY_SUSTAIN;
247 else if ((state == DECAY_SUSTAIN) || (state == RELEASE))
249 if (--envelope_counter==0x00)
251 counter_enabled =
false;
255 set_exponential_counter();
258 else if (unlikely(exponential_pipeline != 0) && (--exponential_pipeline == 0))
260 exponential_counter = 0;
262 if (((state == DECAY_SUSTAIN) && (envelope_counter != sustain))
263 || (state == RELEASE))
270 envelope_pipeline = 1;
273 else if (unlikely(resetLfsr))
282 exponential_counter = 0;
289 envelope_pipeline = 2;
293 if (counter_enabled && (++exponential_counter == exponential_counter_period))
294 exponential_pipeline = exponential_counter_period != 1 ? 2 : 1;
306 if (likely(lfsr != rate))
310 const unsigned int feedback = ((lfsr << 14) ^ (lfsr << 13)) & 0x4000;
311 lfsr = (lfsr >> 1) | feedback;
359 void EnvelopeGenerator::state_change()
366 if (state_pipeline == 1)
369 rate = adsrtable[decay];
371 else if (state_pipeline == 0)
375 rate = adsrtable[attack];
376 counter_enabled =
true;
380 if (state_pipeline == 0)
382 state = DECAY_SUSTAIN;
383 rate = adsrtable[decay];
387 if (((state == ATTACK) && (state_pipeline == 0))
388 || ((state == DECAY_SUSTAIN) && (state_pipeline == 1)))
391 rate = adsrtable[release];
398 void EnvelopeGenerator::set_exponential_counter()
404 switch (envelope_counter)
408 new_exponential_counter_period = 1;
412 new_exponential_counter_period = 2;
416 new_exponential_counter_period = 4;
420 new_exponential_counter_period = 8;
424 new_exponential_counter_period = 16;
428 new_exponential_counter_period = 30;
Definition: EnvelopeGenerator.h:44
void writeATTACK_DECAY(unsigned char attack_decay)
Definition: EnvelopeGenerator.cpp:137
void reset()
Definition: EnvelopeGenerator.cpp:77
void writeSUSTAIN_RELEASE(unsigned char sustain_release)
Definition: EnvelopeGenerator.cpp:152
float output() const
Definition: EnvelopeGenerator.h:148
void writeCONTROL_REG(unsigned char control)
Definition: EnvelopeGenerator.cpp:102
unsigned char readENV() const
Definition: EnvelopeGenerator.h:209
void setChipModel(ChipModel chipModel)
Definition: EnvelopeGenerator.cpp:66
void clock()
Definition: EnvelopeGenerator.h:220
EnvelopeGenerator()
Definition: EnvelopeGenerator.h:153