24#ifndef ENVELOPEGENERATOR_H
25#define ENVELOPEGENERATOR_H
27#include "siddefs-fp.h"
56 ATTACK, DECAY_SUSTAIN, RELEASE
61 uint16_t lfsr = 0x7fff;
70 unsigned int exponential_counter = 0;
76 unsigned int exponential_counter_period = 1;
77 unsigned int new_exponential_counter_period = 0;
79 unsigned int state_pipeline = 0;
82 unsigned int envelope_pipeline = 0;
84 unsigned int exponential_pipeline = 0;
87 State state = State::RELEASE;
88 State next_state = State::RELEASE;
91 bool counter_enabled =
true;
97 bool resetLfsr =
false;
100 uint8_t envelope_counter = 0xaa;
118 static const uint16_t adsrtable[16];
121 void set_exponential_counter();
134 uint8_t
output()
const {
return envelope_counter; }
175#if RESIDFP_INLINING || defined(ENVELOPEGENERATOR_CPP)
183 env3 = envelope_counter;
185 if (unlikely(new_exponential_counter_period > 0))
187 exponential_counter_period = new_exponential_counter_period;
188 new_exponential_counter_period = 0;
191 if (unlikely(state_pipeline))
196 if (unlikely(envelope_pipeline != 0) && (--envelope_pipeline == 0))
198 if (likely(counter_enabled))
200 if (state == State::ATTACK)
202 if (++envelope_counter==0xff)
204 next_state = State::DECAY_SUSTAIN;
208 else if ((state == State::DECAY_SUSTAIN) || (state == State::RELEASE))
210 if (--envelope_counter==0x00)
212 counter_enabled =
false;
216 set_exponential_counter();
219 else if (unlikely(exponential_pipeline != 0) && (--exponential_pipeline == 0))
221 exponential_counter = 0;
223 if (((state == State::DECAY_SUSTAIN) && (envelope_counter != sustain))
224 || (state == State::RELEASE))
231 envelope_pipeline = 1;
234 else if (unlikely(resetLfsr))
239 if (state == State::ATTACK)
243 exponential_counter = 0;
250 envelope_pipeline = 2;
254 if (counter_enabled && (++exponential_counter == exponential_counter_period))
255 exponential_pipeline = exponential_counter_period != 1 ? 2 : 1;
267 if (likely(lfsr != rate))
271 const unsigned int feedback = ((lfsr << 14) ^ (lfsr << 13)) & 0x4000;
272 lfsr = (lfsr >> 1) | feedback;
320void EnvelopeGenerator::state_change()
327 if (state_pipeline == 1)
330 rate = adsrtable[decay];
332 else if (state_pipeline == 0)
334 state = State::ATTACK;
336 rate = adsrtable[attack];
337 counter_enabled =
true;
340 case State::DECAY_SUSTAIN:
341 if (state_pipeline == 0)
343 state = State::DECAY_SUSTAIN;
344 rate = adsrtable[decay];
348 if (((state == State::ATTACK) && (state_pipeline == 0))
349 || ((state == State::DECAY_SUSTAIN) && (state_pipeline == 1)))
351 state = State::RELEASE;
352 rate = adsrtable[release];
359void EnvelopeGenerator::set_exponential_counter()
365 switch (envelope_counter)
369 new_exponential_counter_period = 1;
373 new_exponential_counter_period = 2;
377 new_exponential_counter_period = 4;
381 new_exponential_counter_period = 8;
385 new_exponential_counter_period = 16;
389 new_exponential_counter_period = 30;
Definition EnvelopeGenerator.h:46
void writeATTACK_DECAY(uint8_t attack_decay)
Definition EnvelopeGenerator.cpp:122
void reset()
Definition EnvelopeGenerator.cpp:62
void clock()
Definition EnvelopeGenerator.h:181
void writeCONTROL_REG(uint8_t control)
Definition EnvelopeGenerator.cpp:87
uint8_t readENV() const
Definition EnvelopeGenerator.h:170
uint8_t output() const
Definition EnvelopeGenerator.h:134
State
Definition EnvelopeGenerator.h:55
void writeSUSTAIN_RELEASE(uint8_t sustain_release)
Definition EnvelopeGenerator.cpp:137