chenlh
2026-03-20 f761b6198e2e98f02adb84c83dbf59529e51b2ef
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
#pragma once 
 
#include <iostream>
#include <cmath>
 
#define PI 3.14159265358979323846
 
/**
 * Low Frequency Oscillator Class
 * 
 */
class LFO
{
public:
 
    /**
     * @brief Contructor
     * 
     */
    LFO();
    
    /**
     * @brief Constructor
     * 
     * Sets waveform type during object declaration
     * 
     * @param waveformType Waveform type index
     */
    LFO(int waveformType);
 
    /**
     * @brief Prepares object for processing
     * 
     * @param sampleRate Current sample rate
     */
    void prepare(double sampleRate);
 
    /**
     * @brief Generates and returns the next oscillator sample
     * 
     * @param phaseType Oscillator phase
     */
    double getNextOutputSample(int phaseType);
 
    /**
     * @brief Sets oscillator waveform type
     * 
     * @param waveformType Waveform type index
     */
    void setWaveformType(int waveformType);
 
    /**
     * @brief Returns waveform type index
     */
    int getWaveformType();
 
    /**
     * @brief Sets oscillation rate
     * 
     * @param freq New Rate
     */
    void setFrequency(double freq);
 
    /**
     * @brief Returns oscillation rate
     */
    double getFrequency();
 
    /**
     * @brief Sets the Sample Rate
     * 
     * @param sampleRate New sample rate
     */
    void setSampleRate(double sampleRate);
 
    /**
     * @brief Returns the current sample rate the object is operating on
     */
    double getSampleRate();
 
    /**
     * @brief Converts unipolar oscilation type to bipolar
     * 
     * @param value Value to convert
     */
    double unipolarToBipolar(double value);
 
    enum Waveforms
    {
        Triangle = 0,
        Sine,
        Saw
    };
 
    enum LFOPhase
    {
        Normal = 0,
        Inverted,
        QuadPhase,
        QuadPhaseInverted
    };
 
 
private:
    void generateNextOutputSample();
 
    // Advabce the mod counter
    void moduloAdvance(double& modCounter, double phaceInc);
 
    // Check the mod counter and wrap
    void moduloWrap(double& modCounter, double phaceInc);
    // Advance the mod counter and wrap
    void moduloAdvanceAndWrap(double& modCounter, double phaceInc);
 
    // Parabolic Sine Calculation Function (Angle ragnes from -pi to pi)
    double parabolicSine(double angle);
 
    // Oscillator Parameters
    double modCounter, modCounter90, phaseInc;
 
 
    double frequency {1.0}, sampleRate;
    int waveformType {Waveforms::Sine};
 
    // Output Values
    double outNormal, outInverted, outQuadPhase, outQuadPhaseInverted;
};