-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathringmod.cpp
More file actions
42 lines (33 loc) · 982 Bytes
/
ringmod.cpp
File metadata and controls
42 lines (33 loc) · 982 Bytes
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
#include "ringmod.h"
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846f
#endif
namespace daisysp {
void RingModulator::Init(float sample_rate) {
sample_rate_ = sample_rate;
freq_ = 440.0f;
depth_ = 1.0f;
phase_ = 0.0f;
RecalculatePhaseIncrement();
}
void RingModulator::RecalculatePhaseIncrement() {
// Calculate phase increment for sine wave oscillator
// phase_increment = 2 * pi * frequency / sample_rate
phase_increment_ = 2.0f * M_PI * freq_ / sample_rate_;
}
float RingModulator::Process(const float &in) {
// Generate sine wave for modulation
float mod = std::sin(phase_);
// Update phase
phase_ += phase_increment_;
if (phase_ >= 2.0f * M_PI) {
phase_ -= 2.0f * M_PI;
}
// Ring modulation: multiply input by sine wave
// depth controls blend between dry (in) and ring-modulated (in * mod)
float ring_out = in * mod;
float out = (1.0f - depth_) * in + depth_ * ring_out;
return out;
}
} // namespace daisysp