WE Core
Loading...
Searching...
No Matches
CarveDSPUnit.h
Go to the documentation of this file.
1/*
2 * File: CarveDSPUnit.h
3 *
4 * Version: 2.0.0
5 *
6 * Created: 09/09/2015
7 *
8 * This file is part of WECore.
9 *
10 * WECore is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * WECore is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with WECore. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#pragma once
26
27#include "CarveParameters.h"
28#include "General/CoreMath.h"
29
55namespace WECore::Carve {
56
57 template <typename T>
59 static_assert(std::is_floating_point<T>::value,
60 "Must be provided with a floating point template type");
61
62 public:
66 CarveDSPUnit() : _preGain(Parameters::PREGAIN.defaultValue),
67 _postGain(Parameters::POSTGAIN.defaultValue),
68 _tweak(Parameters::TWEAK.defaultValue),
69 _mode(Parameters::MODE.defaultValue) { }
70
71 virtual ~CarveDSPUnit() {}
72
83 void setMode(int val) { _mode = Parameters::MODE.BoundsCheck(val); }
84
93 void setPreGain(double val) { _preGain = Parameters::PREGAIN.BoundsCheck(val); }
94
103 void setPostGain(double val) { _postGain = Parameters::POSTGAIN.BoundsCheck(val); }
104
114 void setTweak(double val) { _tweak = Parameters::TWEAK.BoundsCheck(val); }
115
124 int getMode() { return _mode; }
125
129 double getPreGain() { return _preGain; }
130
134 double getPostGain() { return _postGain; }
135
139 double getTweak() { return _tweak; }
140
151 T process(T inSample) const;
152
153 private:
154 double _preGain,
157
158 int _mode;
159
160 T _processSine(T inSample) const;
161
162 T _processParabolicSoft(T inSample) const;
163
164 T _processParabolicHard(T inSample) const;
165
166 T _processAsymmetricSine(T inSample) const;
167
168 T _processExponent(T inSample) const;
169
170 T _processClipper(T inSample) const;
171 };
172
173 template <typename T>
174 T CarveDSPUnit<T>::process(T inSample) const {
175 switch (_mode) {
177 return 0;
178
180 return _processSine(inSample);
181
183 return _processParabolicSoft(inSample);
184
186 return _processParabolicHard(inSample);
187
189 return _processAsymmetricSine(inSample);
190
192 return _processExponent(inSample);
193
195 return _processClipper(inSample);
196
197 default:
198 return _processSine(inSample);
199 }
200 }
201
202 template <typename T>
203 T CarveDSPUnit<T>::_processSine(T inSample) const {
204 return (
205 (((1 - std::abs(_tweak/2)) * sin(CoreMath::DOUBLE_PI * inSample * _preGain)))
206 + ((_tweak/2) * sin(4 * CoreMath::DOUBLE_PI * inSample * _preGain))
207 )
208 * _postGain;
209 }
210
211 template <typename T>
213 return (
214 CoreMath::DOUBLE_PI * inSample * _preGain * ((4 * _tweak)
215 - sqrt(4 * pow(inSample * CoreMath::DOUBLE_PI * _preGain, 2))) * 0.5
216 )
217 * _postGain;
218 }
219
220 template <typename T>
222 return (
223 ((1 - std::abs(_tweak/10)) * (atan(_preGain * 4 * CoreMath::DOUBLE_PI * inSample) / 1.5))
224 + ((_tweak/5) * sin(CoreMath::DOUBLE_PI * inSample * _preGain))
225 )
226 * _postGain;
227 }
228
229 template <typename T>
231 return (
232 cos(CoreMath::DOUBLE_PI * inSample * (_tweak + 1))
233 * atan(4 * CoreMath::DOUBLE_PI * inSample * _preGain)
234 )
235 * _postGain;
236 }
237
238 template <typename T>
240 return (
241 sin(-0.25 * pow(2 * CoreMath::DOUBLE_E, (inSample * _preGain + 1.5)))
242 )
243 * _postGain;
244 }
245
246 template <typename T>
248 inSample *= CoreMath::DOUBLE_PI * _preGain;
249
250 const T tweakInverted {1 - _tweak};
251
252 return (
253 sin(inSample) +
254 0.3 * sin(3 * inSample) * tweakInverted +
255 0.15 * sin(5 * inSample) * tweakInverted +
256 0.075 * sin(7 * inSample) * tweakInverted +
257 0.0375 * sin(9 * inSample) * tweakInverted +
258 0.01875 * sin(11 * inSample) * tweakInverted
259 )
260 * _postGain / 1.5 * -1;
261 }
262}
T process(T inSample) const
T _processAsymmetricSine(T inSample) const
void setPostGain(double val)
T _processClipper(T inSample) const
T _processSine(T inSample) const
T _processExponent(T inSample) const
void setPreGain(double val)
T _processParabolicHard(T inSample) const
T _processParabolicSoft(T inSample) const
const ModeParameter MODE
const ParameterDefinition::RangedParameter< double > TWEAK(0, 1, 0)
const ParameterDefinition::RangedParameter< double > POSTGAIN(0, 2, 0.5)
const ParameterDefinition::RangedParameter< double > PREGAIN(0, 2, 1)
constexpr double DOUBLE_PI
Definition CoreMath.h:36
constexpr double DOUBLE_E
Definition CoreMath.h:42