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;
117 static const unsigned int adsrtable[16];
120 void set_exponential_counter();
132 void setDAC(
float* dac) { this->dac = dac; }
144 float output()
const {
return dac[envelope_counter]; }
152 exponential_counter(0),
153 exponential_counter_period(1),
154 new_exponential_counter_period(0),
156 envelope_pipeline(0),
157 exponential_pipeline(0),
160 counter_enabled(true),
163 envelope_counter(0xaa),
205 unsigned char readENV()
const {
return env3; }
210#if RESID_INLINING || defined(ENVELOPEGENERATOR_CPP)
218 env3 = envelope_counter;
220 if (unlikely(new_exponential_counter_period > 0))
222 exponential_counter_period = new_exponential_counter_period;
223 new_exponential_counter_period = 0;
226 if (unlikely(state_pipeline))
231 if (unlikely(envelope_pipeline != 0) && (--envelope_pipeline == 0))
233 if (likely(counter_enabled))
237 if (++envelope_counter==0xff)
239 next_state = DECAY_SUSTAIN;
243 else if ((state == DECAY_SUSTAIN) || (state == RELEASE))
245 if (--envelope_counter==0x00)
247 counter_enabled =
false;
251 set_exponential_counter();
254 else if (unlikely(exponential_pipeline != 0) && (--exponential_pipeline == 0))
256 exponential_counter = 0;
258 if (((state == DECAY_SUSTAIN) && (envelope_counter != sustain))
259 || (state == RELEASE))
266 envelope_pipeline = 1;
269 else if (unlikely(resetLfsr))
278 exponential_counter = 0;
285 envelope_pipeline = 2;
289 if (counter_enabled && (++exponential_counter == exponential_counter_period))
290 exponential_pipeline = exponential_counter_period != 1 ? 2 : 1;
302 if (likely(lfsr != rate))
306 const unsigned int feedback = ((lfsr << 14) ^ (lfsr << 13)) & 0x4000;
307 lfsr = (lfsr >> 1) | feedback;
355void EnvelopeGenerator::state_change()
362 if (state_pipeline == 1)
365 rate = adsrtable[decay];
367 else if (state_pipeline == 0)
371 rate = adsrtable[attack];
372 counter_enabled =
true;
376 if (state_pipeline == 0)
378 state = DECAY_SUSTAIN;
379 rate = adsrtable[decay];
383 if (((state == ATTACK) && (state_pipeline == 0))
384 || ((state == DECAY_SUSTAIN) && (state_pipeline == 1)))
387 rate = adsrtable[release];
394void EnvelopeGenerator::set_exponential_counter()
400 switch (envelope_counter)
404 new_exponential_counter_period = 1;
408 new_exponential_counter_period = 2;
412 new_exponential_counter_period = 4;
416 new_exponential_counter_period = 8;
420 new_exponential_counter_period = 16;
424 new_exponential_counter_period = 30;
Definition EnvelopeGenerator.h:44
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
float output() const
Definition EnvelopeGenerator.h:144
void writeCONTROL_REG(unsigned char control)
Definition EnvelopeGenerator.cpp:87
unsigned char readENV() const
Definition EnvelopeGenerator.h:205
void clock()
Definition EnvelopeGenerator.h:216
EnvelopeGenerator()
Definition EnvelopeGenerator.h:149
void setDAC(float *dac)
Definition EnvelopeGenerator.h:132