-
Notifications
You must be signed in to change notification settings - Fork 408
/
effect_envelope.cpp
217 lines (205 loc) · 5.8 KB
/
effect_envelope.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Audio Library for Teensy 3.X
* Copyright (c) 2017, Paul Stoffregen, paul@pjrc.com
*
* 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.
*/
#include <Arduino.h>
#include "effect_envelope.h"
#define STATE_IDLE 0
#define STATE_DELAY 1
#define STATE_ATTACK 2
#define STATE_HOLD 3
#define STATE_DECAY 4
#define STATE_SUSTAIN 5
#define STATE_RELEASE 6
#define STATE_FORCED 7
void AudioEffectEnvelope::noteOn(void)
{
__disable_irq();
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_RELEASE && state != STATE_IDLE && state != STATE_FORCED) {
state = STATE_RELEASE;
count = release_count;
inc_hires = (-mult_hires) / (int32_t)count;
}
__enable_irq();
}
void AudioEffectEnvelope::update(void)
{
audio_block_t *block;
uint32_t *p, *end;
uint32_t sample12, sample34, sample56, sample78, tmp1, tmp2;
block = receiveWritable();
if (block)
{
if (state == STATE_IDLE) {
AudioStream::release(block);
return;
}
p = (uint32_t *)(block->data);
}
else
p = NULL;
end = p + AUDIO_BLOCK_SAMPLES/2;
// need to run the envelope process even with silent data, or
// it gets stuck and never goes idle:
while (p < end)
{
// we only care about the state when completing a region
if (count == 0) {
if (state == STATE_ATTACK) {
count = hold_count;
if (count > 0) {
state = STATE_HOLD;
mult_hires = 0x40000000;
inc_hires = 0;
} else {
state = STATE_DECAY;
count = decay_count;
inc_hires = (sustain_mult - 0x40000000) / (int32_t)count;
}
continue;
} else if (state == STATE_HOLD) {
state = STATE_DECAY;
count = decay_count;
inc_hires = (sustain_mult - 0x40000000) / (int32_t)count;
continue;
} else if (state == STATE_DECAY) {
state = STATE_SUSTAIN;
count = 0xFFFF;
mult_hires = sustain_mult;
inc_hires = 0;
} else if (state == STATE_SUSTAIN) {
count = 0xFFFF;
} else if (state == STATE_RELEASE) {
state = STATE_IDLE;
while (p < end) {
if (nullptr != block) // because p doesn't stay null!
{
*p++ = 0;
*p++ = 0;
*p++ = 0;
*p++ = 0;
}
else
p += 4;
}
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;
inc_hires = 0x40000000 / count;
continue;
}
}
if (nullptr != block)
{
int32_t mult = mult_hires >> 14;
int32_t inc = inc_hires >> 17;
// process 8 samples, using only mult and inc (16 bit resolution)
sample12 = *p++;
sample34 = *p++;
sample56 = *p++;
sample78 = *p++;
p -= 4;
mult += inc;
tmp1 = signed_multiply_32x16b(mult, sample12);
mult += inc;
tmp2 = signed_multiply_32x16t(mult, sample12);
sample12 = pack_16b_16b(tmp2, tmp1);
mult += inc;
tmp1 = signed_multiply_32x16b(mult, sample34);
mult += inc;
tmp2 = signed_multiply_32x16t(mult, sample34);
sample34 = pack_16b_16b(tmp2, tmp1);
mult += inc;
tmp1 = signed_multiply_32x16b(mult, sample56);
mult += inc;
tmp2 = signed_multiply_32x16t(mult, sample56);
sample56 = pack_16b_16b(tmp2, tmp1);
mult += inc;
tmp1 = signed_multiply_32x16b(mult, sample78);
mult += inc;
tmp2 = signed_multiply_32x16t(mult, sample78);
sample78 = pack_16b_16b(tmp2, tmp1);
*p++ = sample12;
*p++ = sample34;
*p++ = sample56;
*p++ = sample78;
// adjust the long-term gain using 30 bit resolution (fix #102)
// https://github.com/PaulStoffregen/Audio/issues/102
}
else
p += 4;
mult_hires += inc_hires;
count--;
}
if (nullptr != block)
{
transmit(block);
AudioStream::release(block);
}
}
bool AudioEffectEnvelope::isActive()
{
uint8_t current_state = *(volatile uint8_t *)&state;
if (current_state == STATE_IDLE) return false;
return true;
}
bool AudioEffectEnvelope::isSustain()
{
uint8_t current_state = *(volatile uint8_t *)&state;
if (current_state == STATE_SUSTAIN) return true;
return false;
}