-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtri_lfo.h
87 lines (73 loc) · 1.47 KB
/
tri_lfo.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
#pragma once
#ifndef DSY_TRI_LFO_H
#define DSY_TRI_LFO_H
#ifdef __cplusplus
#include <math.h>
namespace daisysp
{
/*
* Triangle LFO lifted from Daisy DaisySP/Source/Effects/flanger.h and modified
*/
class tri_lfo
{
public:
/** Initialize the modules
\param sample_rate Audio engine sample rate.
*/
void Init(float sample_rate)
{
sample_rate_ = sample_rate;
phase_ = 0.f;
depth_ = 0.f;
range_ = 0.f;
out_ = 0.f;
SetFreq(.3);
}
void SetDepth(float depth)
{
depth_ = fclamp(depth, 0.f, 1.f);
}
void SetRange(float range)
{
range_ = range;
}
/** Set lfo frequency.
\param freq Frequency in Hz
*/
void SetFreq(float freq)
{
freq = 4.f * freq / sample_rate_;
freq *= freq_ < 0.f ? -1.f : 1.f; //if we're headed down, keep going
freq_ = fclamp(freq, -.25f, .25f); //clip at +/- .125 * sr
}
float GetOutput() {
return out_;
}
void Process()
{
phase_ += freq_;
//wrap around and flip direction
if(phase_ > 1.f)
{
phase_ = 1.f - (phase_ - 1.f);
freq_ *= -1.f;
}
else if(phase_ < -1.f)
{
phase_ = -1.f - (phase_ + 1.f);
freq_ *= -1.f;
}
out_ = phase_ * (range_ * depth_);
}
private:
float sample_rate_;
//triangle lfos
float phase_;
float freq_;
float depth_;
float range_;
float out_;
};
}
#endif
#endif