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;
114 static const unsigned int adsrtable[16];
117 void set_exponential_counter();
130 unsigned int output()
const {
return envelope_counter; }
138 exponential_counter(0),
139 exponential_counter_period(1),
140 new_exponential_counter_period(0),
142 envelope_pipeline(0),
143 exponential_pipeline(0),
146 counter_enabled(true),
149 envelope_counter(0xaa),
191 unsigned char readENV()
const {
return env3; }
196#if RESID_INLINING || defined(ENVELOPEGENERATOR_CPP)
204 env3 = envelope_counter;
206 if (unlikely(new_exponential_counter_period > 0))
208 exponential_counter_period = new_exponential_counter_period;
209 new_exponential_counter_period = 0;
212 if (unlikely(state_pipeline))
217 if (unlikely(envelope_pipeline != 0) && (--envelope_pipeline == 0))
219 if (likely(counter_enabled))
223 if (++envelope_counter==0xff)
225 next_state = DECAY_SUSTAIN;
229 else if ((state == DECAY_SUSTAIN) || (state == RELEASE))
231 if (--envelope_counter==0x00)
233 counter_enabled =
false;
237 set_exponential_counter();
240 else if (unlikely(exponential_pipeline != 0) && (--exponential_pipeline == 0))
242 exponential_counter = 0;
244 if (((state == DECAY_SUSTAIN) && (envelope_counter != sustain))
245 || (state == RELEASE))
252 envelope_pipeline = 1;
255 else if (unlikely(resetLfsr))
264 exponential_counter = 0;
271 envelope_pipeline = 2;
275 if (counter_enabled && (++exponential_counter == exponential_counter_period))
276 exponential_pipeline = exponential_counter_period != 1 ? 2 : 1;
288 if (likely(lfsr != rate))
292 const unsigned int feedback = ((lfsr << 14) ^ (lfsr << 13)) & 0x4000;
293 lfsr = (lfsr >> 1) | feedback;
341void EnvelopeGenerator::state_change()
348 if (state_pipeline == 1)
351 rate = adsrtable[decay];
353 else if (state_pipeline == 0)
357 rate = adsrtable[attack];
358 counter_enabled =
true;
362 if (state_pipeline == 0)
364 state = DECAY_SUSTAIN;
365 rate = adsrtable[decay];
369 if (((state == ATTACK) && (state_pipeline == 0))
370 || ((state == DECAY_SUSTAIN) && (state_pipeline == 1)))
373 rate = adsrtable[release];
380void EnvelopeGenerator::set_exponential_counter()
386 switch (envelope_counter)
390 new_exponential_counter_period = 1;
394 new_exponential_counter_period = 2;
398 new_exponential_counter_period = 4;
402 new_exponential_counter_period = 8;
406 new_exponential_counter_period = 16;
410 new_exponential_counter_period = 30;
Definition EnvelopeGenerator.h:44
unsigned int output() const
Definition EnvelopeGenerator.h:130
void writeATTACK_DECAY(unsigned char attack_decay)
Definition EnvelopeGenerator.cpp:122
void reset()
Definition EnvelopeGenerator.cpp:62
void writeSUSTAIN_RELEASE(unsigned char sustain_release)
Definition EnvelopeGenerator.cpp:137
void writeCONTROL_REG(unsigned char control)
Definition EnvelopeGenerator.cpp:87
unsigned char readENV() const
Definition EnvelopeGenerator.h:191
void clock()
Definition EnvelopeGenerator.h:202
EnvelopeGenerator()
Definition EnvelopeGenerator.h:135