WE Core
Loading...
Searching...
No Matches
ModulationSource.h
Go to the documentation of this file.
1/*
2 * File: ModulationSource.h
3 *
4 * Created: 22/11/2020
5 *
6 * This file is part of WECore.
7 *
8 * WECore is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * WECore is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with WECore. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24namespace WECore {
25
33 template <typename SampleType>
35 static_assert(std::is_floating_point<SampleType>::value,
36 "Must be provided with a floating point template type");
37
38 public:
40 virtual ~ModulationSource() = default;
41
46 inline SampleType getNextOutput(SampleType inSample);
47
51 virtual SampleType getLastOutput() const { return _cachedOutput; }
52
56 inline void reset();
57
58 protected:
59 SampleType _cachedOutput;
60
67 virtual SampleType _getNextOutputImpl(SampleType inSample) = 0;
68
72 virtual void _resetImpl() = 0;
73 };
74
75 template <typename SampleType>
76 SampleType ModulationSource<SampleType>::getNextOutput(SampleType inSample) {
77 _cachedOutput = _getNextOutputImpl(inSample);
78 return _cachedOutput;
79 }
80
81 template <typename SampleType>
83 _resetImpl();
84 _cachedOutput = 0;
85 }
86
87 template <typename SampleType>
89 std::shared_ptr<ModulationSource<SampleType>> source;
90 double amount;
91 };
92
93 template <typename SampleType>
94 SampleType calcModValue(const std::vector<ModulationSourceWrapper<SampleType>>& sources) {
95 SampleType retVal {0};
96
97 for (const ModulationSourceWrapper<SampleType>& source : sources) {
98 retVal += source.source->getLastOutput() * source.amount;
99 }
100
101 return retVal;
102 }
103}
virtual SampleType _getNextOutputImpl(SampleType inSample)=0
SampleType getNextOutput(SampleType inSample)
virtual SampleType getLastOutput() const
virtual void _resetImpl()=0
virtual ~ModulationSource()=default
SampleType calcModValue(const std::vector< ModulationSourceWrapper< SampleType > > &sources)
std::shared_ptr< ModulationSource< SampleType > > source