-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal_inharm.h
198 lines (169 loc) · 4.15 KB
/
modal_inharm.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
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
#pragma once
#ifndef DSY_MODAL_INHARM_H
#define DSY_MODAL_INHARM_H
// Maximum resonance - let's try and keep things stable
#define RES_MAX 0.99999
#define GAIN_MAX 10.0f
#include <stdint.h>
#include "arm_math.h"
#include "iir_reson.h"
#include "iir_1p_lp.h"
#include "inharm_presets.h"
#ifdef __cplusplus
namespace daisysp
{
/*
* An inharmonic modal voice is constructed from a set of modes
* Specify the fundamental frequency and the mode multiples,
* gain and resonance factors
*
* Jared Anderson June 2021
*/
class modal_inharm
{
public:
modal_inharm(int n) :n_modes_{n}, modes{new iir_reson[n]} {}
~modal_inharm() { delete[] modes; }
void init(float fs, float fc, inharm_preset *preset)
{
fs_ = fs;
fc_ = fc;
mgf_ = DEFAULT_MGF;
n_modes_ = preset->num_modes;
for (int i = 0; i < n_modes_; i++) {
modes_.push_back(preset->modes[i]);
gains_.push_back(preset->gains[i]);
res_.push_back(preset->res[i]);
float mode_f;
if (modes_.at(i) > 0) {
mode_f = modes_.at(i) * fc_;
} else {
mode_f = -modes_.at(i);
}
// dont alias
if (mode_f > (fs_ / 2)) {
n_modes_ = i;
break;
}
float mode_g = gains_.at(i) / pow((i + 1), mgf_);
float mode_r = res_.at(i);
modes[i].init(fs_, mode_f, CLAMP(mode_r, 0, RES_MAX), mode_g);
}
input_filt.init(fs_, DEFAULT_IFC);
}
void load_preset(inharm_preset *preset)
{
int i;
n_modes_ = preset->num_modes;
for (i = 0; i < n_modes_; i++) {
modes_.at(i) = preset->modes[i];
gains_.at(i) = preset->gains[i];
res_.at(i) = preset->res[i];
float mode_f;
if (modes_.at(i) > 0) {
mode_f = modes_.at(i) * fc_;
} else {
mode_f = -modes_.at(i);
}
// dont alias
if (mode_f > (fs_ / 2)) {
n_modes_ = i;
break;
}
float mode_g = gains_.at(i) / pow((i + 1), mgf_);
float mode_r = res_.at(i);
modes[i].update_fc(mode_f);
modes[i].update_r(CLAMP(mode_r, 0, RES_MAX));
modes[i].update_g(mode_g);
}
}
float Process(float in)
{
float out = 0;
float in_filt = input_filt.Process(in);
for (int i = 0; i < n_modes_; i++) {
out += modes[i].Process(in_filt) / n_modes_;
}
// Let's do clamping after summing in the top level
return out;
}
void update_fc(float fc)
{
int i;
if (fc != fc_) {
fc_ = fc;
for (i = 0; i < n_modes_; i++) {
float mode_f;
if (modes_.at(i) > 0) {
mode_f = modes_.at(i) * fc_;
} else {
mode_f = -modes_.at(i);
}
// dont alias
if (mode_f > (fs_ / 2)) {
n_modes_ = i;
break;
}
modes[i].update_fc(mode_f);
}
}
}
void update_r(float *res)
{
for (int i = 0; i < n_modes_; i++) {
float r = res[i];
if (r != res_.at(i)) {
res_.at(i) = r;
modes[i].update_r(res_.at(i));
}
}
}
// keeps res_.at(i) as the baseline and increases
// amt should be between 0 and 1 where 0 is baseline and 1 is RES_MAX
void modulate_r(float amt)
{
for (int i = 0; i < n_modes_; i++) {
float r = res_.at(i) + amt * (RES_MAX - res_.at(i));
modes[i].update_r(r);
}
}
// keeps gains_.at(i) as the baseline and increases
// amt should be between 0 and 1 where 0 is baseline and 1 is GAIN_MAX
void modulate_g(float amt)
{
for (int i = 0; i < n_modes_; i++) {
//float g = gains_.at(i) + amt * (GAIN_MAX - gains_.at(i));
float g = gains_.at(i) + amt * (GAIN_MAX * gains_.at(i) - gains_.at(i));
modes[i].update_g(g);
}
}
/*
void update_mgf(float mgf)
{
if (mgf != mgf_) {
mgf_ = mgf;
int calculated_modes = 0;
for (int i = 0; calculated_modes < n_modes_; i++) {
// skip modes defined by beta
if (fmod(i, beta_) == 0) continue;
float mode_g = g_ / pow((i + 1), mgf_);
modes[calculated_modes].update_g(mode_g);
calculated_modes++;
}
}
}
*/
void update_ifc(float ifc)
{
input_filt.update_fc(ifc);
}
private:
int n_modes_;
iir_reson *modes;
iir_1p_lp input_filt;
float fs_, fc_, mgf_;
std::vector<float> modes_, gains_, res_;
};
} // namespace daisysp
#endif
#endif