forked from pichenettes/eurorack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvactrol.cc
186 lines (160 loc) · 5.66 KB
/
vactrol.cc
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
// Copyright 2014 Emilie Gillet.
//
// Author: Emilie Gillet ([email protected])
//
// 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 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.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
//
// -----------------------------------------------------------------------------
//
// Vactrol.
#include "streams/vactrol.h"
#include "stmlib/utils/dsp.h"
#include "streams/gain.h"
#include "streams/resources.h"
namespace streams {
using namespace stmlib;
void Vactrol::Init() {
state_[0] = 0;
state_[1] = 0;
state_[2] = 0;
state_[3] = 0;
excite_ = 0;
}
void Vactrol::Process(
int16_t audio,
int16_t excite,
uint16_t* gain,
uint16_t* frequency) {
// Smooth frequency amount parameters.
frequency_amount_ += (target_frequency_amount_ - frequency_amount_) >> 8;
frequency_offset_ += (target_frequency_offset_ - frequency_offset_) >> 8;
int32_t input;
int32_t error;
int64_t coefficient = 0;
if (excite < 0) {
excite = 0;
}
// Simple plucked mode.
if (plucked_) {
if (gate_ == false) {
if (excite > kSchmittTriggerThreshold) {
gate_ = true;
state_[0] = 32767 << 16;
state_[1] = 32767 << 16;
}
} else {
if (excite < (kSchmittTriggerThreshold >> 1)) {
gate_ = false;
}
}
// Filter the excitation pulses.
state_[0] -= static_cast<int64_t>(
state_[0]) * fast_decay_coefficient_ >> 31;
state_[1] -= static_cast<int64_t>(
state_[1]) * decay_coefficient_ >> 31;
// VCF envelope.
error = state_[0] - state_[2];
coefficient = error > 0
? fast_attack_coefficient_ : fast_decay_coefficient_;
state_[2] += static_cast<int64_t>(error) * coefficient >> 31;
// VCA envelope.
error = state_[1] - state_[3];
coefficient = error > 0 ? fast_attack_coefficient_ : decay_coefficient_;
// Increase the duration of the tail
int64_t strength = error > 0 ? error : -error;
coefficient = (coefficient >> 1) + (coefficient * strength >> 31);
state_[3] += static_cast<int64_t>(error) * coefficient >> 31;
uint16_t vcf_amount = state_[2] >> 16;
uint16_t vca_mount = Interpolate1022(wav_gompertz, (state_[3] >> 2) * 3);
*gain = kAboveUnityGain * vca_mount >> 15;
*frequency = frequency_offset_ + \
(frequency_amount_ * vcf_amount >> 15);
return;
}
// Low-pass filter the negative edges to prevent fast pulse to immediately
// decay before the vactrol has started reacting. This allows the EXCITE
// input to be used for both controlling the vactrol or just plucking it
// from a trigger.
error = excite - excite_;
coefficient = error > 0 ? (1 << 30) : (decay_coefficient_ << 1);
excite_ += static_cast<int64_t>(error) * coefficient >> 31;
excite = excite_;
input = frequency_offset_;
input += frequency_amount_ >> 1;
input = (65535 + input) >> 1;
input *= excite;
state_[3] += static_cast<int64_t>(input - state_[3]) * 67976239 >> 31;
error = input - state_[0];
coefficient = 0;
if (error > 0) {
if (state_[1] > 0) {
coefficient = attack_coefficient_;
// Increase attack time when the photocell has been desensitized.
coefficient += coefficient * (255 - (state_[2] >> 23)) >> 6;
} else {
coefficient = fast_attack_coefficient_;
}
} else {
if (state_[1] < 0) {
coefficient = decay_coefficient_;
} else {
coefficient = fast_decay_coefficient_;
}
}
// First order.
state_[0] += static_cast<int64_t>(error) * coefficient >> 31;
// Second order.
state_[1] += static_cast<int64_t>(error - state_[1]) * coefficient >> 31;
// Memory effect.
int32_t sensitivity = state_[0];
if (sensitivity > (1 << 28)) {
sensitivity = 1 << 31;
} else {
sensitivity <<= 3;
}
error = sensitivity - state_[2];
if (error > 0) {
// Get into the "sensitized" state in 1s.
state_[2] += static_cast<int64_t>(error) * 138132 >> 31;
} else {
// Get out of the "sensitized" state in 60s.
state_[2] += static_cast<int64_t>(error) * 1151 >> 31;
}
// Apply non-linearity.
int32_t index = state_[0] >> 1;
// A little hack to add overshoot...
index += (state_[3] >> 15) * (state_[1] >> 15) >> 1;
if (index < 0) {
index = 0;
} else if (index >= (1 << 30)) {
index = (1 << 30) - 1;
}
uint16_t amplitude = index < 536870912
? Interpolate1022(wav_gompertz, static_cast<uint32_t>(index) << 3)
: 32767;
uint16_t cutoff = index >> 14;
if (cutoff >= 32767) cutoff = 32767;
cutoff = cutoff * cutoff >> 15;
*gain = kAboveUnityGain * amplitude >> 15;
*frequency = frequency_offset_ + \
(frequency_amount_ * cutoff >> 15);
}
} // namespace streams