-
Notifications
You must be signed in to change notification settings - Fork 404
/
effect_envelope.h
106 lines (100 loc) · 3.71 KB
/
effect_envelope.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Audio Library for Teensy 3.X
* Copyright (c) 2017, Paul Stoffregen, [email protected]
*
* Development of this audio library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef effect_envelope_h_
#define effect_envelope_h_
#include <Arduino.h> // github.com/PaulStoffregen/cores/blob/master/teensy4/Arduino.h
#include <AudioStream.h> // github.com/PaulStoffregen/cores/blob/master/teensy4/AudioStream.h
#include "utility/dspinst.h"
#define SAMPLES_PER_MSEC (AUDIO_SAMPLE_RATE_EXACT/1000.0f)
class AudioEffectEnvelope : public AudioStream
{
public:
AudioEffectEnvelope() : AudioStream(1, inputQueueArray) {
state = 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();
void delay(float milliseconds) {
delay_count = milliseconds2count(milliseconds);
}
void attack(float milliseconds) {
attack_count = milliseconds2count(milliseconds);
if (attack_count == 0) attack_count = 1;
}
void hold(float milliseconds) {
hold_count = milliseconds2count(milliseconds);
}
void decay(float milliseconds) {
decay_count = milliseconds2count(milliseconds);
if (decay_count == 0) decay_count = 1;
}
void sustain(float level) {
if (level < 0.0f) level = 0;
else if (level > 1.0f) level = 1.0f;
sustain_mult = level * 1073741824.0f;
}
void release(float milliseconds) {
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;
}
bool isActive();
bool isSustain();
virtual void update(void);
private:
uint16_t milliseconds2count(float milliseconds) {
if (milliseconds < 0.0f) milliseconds = 0.0f;
uint32_t c = ((uint32_t)(milliseconds*SAMPLES_PER_MSEC)+7)>>3;
if (c > 65535) c = 65535; // allow up to 11.88 seconds
return c;
}
audio_block_t *inputQueueArray[1];
// state
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
// settings
uint16_t delay_count;
uint16_t attack_count;
uint16_t hold_count;
uint16_t decay_count;
int32_t sustain_mult;
uint16_t release_count;
uint16_t release_forced_count;
};
#undef SAMPLES_PER_MSEC
#endif