Skip to content

Commit

Permalink
Add AudioEffectEnvelope releaseNoteOn
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulStoffregen committed Jul 29, 2017
1 parent dc82582 commit 6aab072
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
38 changes: 28 additions & 10 deletions effect_envelope.cpp
Expand Up @@ -33,27 +33,34 @@
#define STATE_DECAY 4
#define STATE_SUSTAIN 5
#define STATE_RELEASE 6
#define STATE_FORCED 7

void AudioEffectEnvelope::noteOn(void)
{
__disable_irq();
mult_hires = 0;
count = delay_count;
if (count > 0) {
state = STATE_DELAY;
inc_hires = 0;
} else {
state = STATE_ATTACK;
count = attack_count;
inc_hires = 0x40000000 / (int32_t)count;
if (state == STATE_IDLE || state == STATE_DELAY || release_forced_count == 0) {
mult_hires = 0;
count = delay_count;
if (count > 0) {
state = STATE_DELAY;
inc_hires = 0;
} else {
state = STATE_ATTACK;
count = attack_count;
inc_hires = 0x40000000 / (int32_t)count;
}
} else if (state != STATE_FORCED) {
state = STATE_FORCED;
count = release_forced_count;
inc_hires = (-mult_hires) / (int32_t)count;
}
__enable_irq();
}

void AudioEffectEnvelope::noteOff(void)
{
__disable_irq();
if (state != STATE_IDLE) {
if (state != STATE_IDLE && state != STATE_FORCED) {
state = STATE_RELEASE;
count = release_count;
inc_hires = (-mult_hires) / (int32_t)count;
Expand Down Expand Up @@ -112,6 +119,17 @@ void AudioEffectEnvelope::update(void)
*p++ = 0;
}
break;
} else if (state == STATE_FORCED) {
mult_hires = 0;
count = delay_count;
if (count > 0) {
state = STATE_DELAY;
inc_hires = 0;
} else {
state = STATE_ATTACK;
count = attack_count;
inc_hires = 0x40000000 / (int32_t)count;
}
} else if (state == STATE_DELAY) {
state = STATE_ATTACK;
count = attack_count;
Expand Down
20 changes: 13 additions & 7 deletions effect_envelope.h
Expand Up @@ -37,12 +37,13 @@ class AudioEffectEnvelope : public AudioStream
public:
AudioEffectEnvelope() : AudioStream(1, inputQueueArray) {
state = 0;
delay(0.0); // default values...
attack(1.5);
hold(0.5);
decay(15.0);
sustain(0.667);
release(30.0);
delay(0.0f); // default values...
attack(10.5f);
hold(2.5f);
decay(35.0f);
sustain(0.5f);
release(300.0f);
releaseNoteOn(5.0f);
}
void noteOn();
void noteOff();
Expand All @@ -69,6 +70,10 @@ class AudioEffectEnvelope : public AudioStream
release_count = milliseconds2count(milliseconds);
if (release_count == 0) release_count = 1;
}
void releaseNoteOn(float milliseconds) {
release_forced_count = milliseconds2count(milliseconds);
if (release_count == 0) release_count = 1;
}
using AudioStream::release;
virtual void update(void);
private:
Expand All @@ -80,7 +85,7 @@ class AudioEffectEnvelope : public AudioStream
}
audio_block_t *inputQueueArray[1];
// state
uint8_t state; // idle, delay, attack, hold, decay, sustain, release
uint8_t state; // idle, delay, attack, hold, decay, sustain, release, forced
uint16_t count; // how much time remains in this state, in 8 sample units
int32_t mult_hires; // attenuation, 0=off, 0x40000000=unity gain
int32_t inc_hires; // amount to change mult_hires every 8 samples
Expand All @@ -92,6 +97,7 @@ class AudioEffectEnvelope : public AudioStream
uint16_t decay_count;
int32_t sustain_mult;
uint16_t release_count;
uint16_t release_forced_count;

};

Expand Down
16 changes: 12 additions & 4 deletions gui/index.html
Expand Up @@ -2383,21 +2383,29 @@ <h3>Functions</h3>
default is zero, for no delay.
</p>
<p class=func><span class=keyword>attack</span>(milliseconds);</p>
<p class=desc>Set the attack time. The default is 1.5 milliseconds.
<p class=desc>Set the attack time. The default is 10.5 milliseconds.
</p>
<p class=func><span class=keyword>hold</span>(milliseconds);</p>
<p class=desc>Set the hold time. The default is 0.5 milliseconds.
<p class=desc>Set the hold time. The default is 2.5 milliseconds.
</p>
<p class=func><span class=keyword>decay</span>(milliseconds);</p>
<p class=desc>Set the decay time. The default is 15 milliseconds.
<p class=desc>Set the decay time. The default is 35 milliseconds.
</p>
<p class=func><span class=keyword>sustain</span>(level);</p>
<p class=desc>Set the sustain level. The range is 0 to 1.0. The
gain will be maintained at this level after the decay phase,
until noteOff() is called.
</p>
<p class=func><span class=keyword>release</span>(milliseconds);</p>
<p class=desc>Set the release time. The default is 30 millisecond.
<p class=desc>Set the release time. The default is 300 millisecond.
</p>
<p class=func><span class=keyword>releaseNoteOn</span>(milliseconds);</p>
<p class=desc>Set a quick release time to be used when a new note is
started while the envelop is in any state passing the signal.
This will add latency before your new attack phase begins, so
short times are recommended. Zero may be used to completely
disable this feature (never extra latency). Longer times help
reduce clicks or pops. The default is 5 millisecond.
</p>
<h3>Examples</h3>
<p class=exam>File &gt; Examples &gt; Audio &gt; Synthesis &gt; PlaySynthMusic
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Expand Up @@ -116,6 +116,7 @@ hold KEYWORD2
decay KEYWORD2
sustain KEYWORD2
release KEYWORD2
releaseNoteOn KEYWORD2
inputLevel KEYWORD2
inputSelect KEYWORD2
muteHeadphone KEYWORD2
Expand Down

0 comments on commit 6aab072

Please sign in to comment.