24#ifndef ENVELOPEGENERATOR_H
25#define ENVELOPEGENERATOR_H
27#include "siddefs-fp.h"
52 ATTACK, DECAY_SUSTAIN, RELEASE
57 unsigned int lfsr = 0x7fff;
60 unsigned int rate = 0;
66 unsigned int exponential_counter = 0;
72 unsigned int exponential_counter_period = 1;
73 unsigned int new_exponential_counter_period = 0;
75 unsigned int state_pipeline = 0;
78 unsigned int envelope_pipeline = 0;
80 unsigned int exponential_pipeline = 0;
83 State state = State::RELEASE;
84 State next_state = State::RELEASE;
87 bool counter_enabled =
true;
93 bool resetLfsr =
false;
96 unsigned char envelope_counter = 0xaa;
99 unsigned char attack = 0;
102 unsigned char decay = 0;
105 unsigned char sustain = 0;
108 unsigned char release = 0;
111 unsigned char env3 = 0;
114 static const unsigned int adsrtable[16];
117 void set_exponential_counter();
130 unsigned int output()
const {
return envelope_counter; }
166 unsigned char readENV()
const {
return env3; }
171#if RESID_INLINING || defined(ENVELOPEGENERATOR_CPP)
179 env3 = envelope_counter;
181 if (unlikely(new_exponential_counter_period > 0))
183 exponential_counter_period = new_exponential_counter_period;
184 new_exponential_counter_period = 0;
187 if (unlikely(state_pipeline))
192 if (unlikely(envelope_pipeline != 0) && (--envelope_pipeline == 0))
194 if (likely(counter_enabled))
196 if (state == State::ATTACK)
198 if (++envelope_counter==0xff)
200 next_state = State::DECAY_SUSTAIN;
204 else if ((state == State::DECAY_SUSTAIN) || (state == State::RELEASE))
206 if (--envelope_counter==0x00)
208 counter_enabled =
false;
212 set_exponential_counter();
215 else if (unlikely(exponential_pipeline != 0) && (--exponential_pipeline == 0))
217 exponential_counter = 0;
219 if (((state == State::DECAY_SUSTAIN) && (envelope_counter != sustain))
220 || (state == State::RELEASE))
227 envelope_pipeline = 1;
230 else if (unlikely(resetLfsr))
235 if (state == State::ATTACK)
239 exponential_counter = 0;
246 envelope_pipeline = 2;
250 if (counter_enabled && (++exponential_counter == exponential_counter_period))
251 exponential_pipeline = exponential_counter_period != 1 ? 2 : 1;
263 if (likely(lfsr != rate))
267 const unsigned int feedback = ((lfsr << 14) ^ (lfsr << 13)) & 0x4000;
268 lfsr = (lfsr >> 1) | feedback;
316void EnvelopeGenerator::state_change()
323 if (state_pipeline == 1)
326 rate = adsrtable[decay];
328 else if (state_pipeline == 0)
330 state = State::ATTACK;
332 rate = adsrtable[attack];
333 counter_enabled =
true;
336 case State::DECAY_SUSTAIN:
337 if (state_pipeline == 0)
339 state = State::DECAY_SUSTAIN;
340 rate = adsrtable[decay];
344 if (((state == State::ATTACK) && (state_pipeline == 0))
345 || ((state == State::DECAY_SUSTAIN) && (state_pipeline == 1)))
347 state = State::RELEASE;
348 rate = adsrtable[release];
355void EnvelopeGenerator::set_exponential_counter()
361 switch (envelope_counter)
365 new_exponential_counter_period = 1;
369 new_exponential_counter_period = 2;
373 new_exponential_counter_period = 4;
377 new_exponential_counter_period = 8;
381 new_exponential_counter_period = 16;
385 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:166
void clock()
Definition EnvelopeGenerator.h:177