-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathRingModulatorPatch.hpp
153 lines (139 loc) · 4.14 KB
/
RingModulatorPatch.hpp
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
////////////////////////////////////////////////////////////////////////////////////////////////////
/*
LICENSE:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* created by the OWL team 2014 */
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "StompBox.h"
#ifndef TWOPI
#define TWOPI 6.28318530717959
#endif
namespace RingModulator {
class BaseOscillator {
public:
float frequency;
BaseOscillator() {
setSampleRate(44100);
frequency = 440;
}
void setSampleRate(float sampleRate) {
TWO_PI_BY_SAMPLERATE = TWOPI / sampleRate;
}
// impelement this to make your own oscillator.
virtual float getSampleForPhase() { return sin(phase); }
float getSample(float freq) {
this->frequency = freq;
return getSample();
}
float getSample() {
// wrap and increment phase
phase += frequency*(TWOPI / 44100.f);//TWO_PI_BY_SAMPLERATE;
if(phase>=TWOPI) phase -= TWOPI;
return getSampleForPhase();
}
float TWO_PI_BY_SAMPLERATE; // constant for calculating phase increment
float phase=0; // current phase of oscillator
};
// uber simple, not bandlimited
class TriOscillator: public BaseOscillator {
public:
float getSampleForPhase() {
if(phase<M_PI) return (-1.f + (2.f*phase/M_PI))*2.f;
else return 2.f*(1.f - (2.f*(phase-M_PI)/M_PI));
}
};
class SawOscillator: public BaseOscillator {
public:
float getSampleForPhase() {
return (1.f - (phase/M_PI))*0.5f;
}
};
class ReverseSawOscillator: public BaseOscillator {
public:
float getSampleForPhase() {
return ((phase/M_PI)-1.f)*0.5f;
}
};
class SquareOscillator: public BaseOscillator {
public:
float getSampleForPhase() {
if(phase<M_PI) return 1.f;
else return -1.f;
}
};
class SinOscillator: public BaseOscillator {
public:
float getSampleForPhase() {
return sin(phase);
}
};
};
#define ABS(X) (X>0?X:-X)
class RingModulatorPatch : public Patch {
public:
RingModulator::SinOscillator oscX;
RingModulator::SinOscillator oscY;
RingModulator::SinOscillator lfo;
RingModulatorPatch(){
registerParameter(PARAMETER_A, "Frequency");
registerParameter(PARAMETER_B, "Multiply");
registerParameter(PARAMETER_C, "LFO");
registerParameter(PARAMETER_D, "Dry/Wet");
registerParameter(PARAMETER_E, "+Frequency");
oscX.frequency = 82.405f;
oscY.frequency = 82.405f;
lfo.frequency = 0.3f;
}
void processAudio(AudioBuffer& buffer){
float dry = getParameterValue(PARAMETER_D);
float wet = 1-dry;
float mult = getParameterValue(PARAMETER_B);
float parameterC = getParameterValue(PARAMETER_C);
float freq = 0;
bool cross = false;
if (parameterC>0.95){
cross = true;
}
else {
freq = (1 - parameterC) * mult * 9000;
}
float EXP = getParameterValue(PARAMETER_E)*9000;
if(EXP>0.1) { // allow for the fact that it never goes to 0
freq += EXP - 0.1f;
}
int size = buffer.getSize();
float* x = buffer.getSamples(0);
float* y = buffer.getSamples(1);
lfo.frequency = getParameterValue(PARAMETER_C)*20;
if(lfo.frequency==0) lfo.phase = 0;
mult *= freq/2.f;
float oscSampleX;
float oscSampleY;
for(int i=0; i<size; ++i) {
if (cross){
oscSampleX = y[i];
oscSampleY = x[i];
}
else {
float lfoSample = lfo.getSample();
oscX.frequency = freq - (1 + lfoSample)*mult;
oscY.frequency = freq - (1 - lfoSample)*mult;//the lfo modulates osc0(left) and osc1(right) with opposite polarity
oscSampleX = oscX.getSample();
oscSampleY = oscY.getSample();
}
x[i] = x[i] * dry + x[i] * oscSampleX * wet;
y[i] = y[i] * dry + y[i] * oscSampleY * wet;
}
}
};