DaisySP
Loading...
Searching...
No Matches
oscillator.h
1/*
2Copyright (c) 2020 Electrosmith, Corp
3
4Use of this source code is governed by an MIT-style
5license that can be found in the LICENSE file or at
6https://opensource.org/licenses/MIT.
7*/
8
9#pragma once
10#ifndef DSY_OSCILLATOR_H
11#define DSY_OSCILLATOR_H
12#include <stdint.h>
13#include "Utility/dsp.h"
14#ifdef __cplusplus
15
16namespace daisysp
17{
21{
22 public:
23 Oscillator() {}
24 ~Oscillator() {}
27 enum
28 {
29 WAVE_SIN,
30 WAVE_TRI,
31 WAVE_SAW,
32 WAVE_RAMP,
33 WAVE_SQUARE,
34 WAVE_POLYBLEP_TRI,
35 WAVE_POLYBLEP_SAW,
36 WAVE_POLYBLEP_SQUARE,
37 WAVE_LAST,
38 };
39
40
50 void Init(float sample_rate)
51 {
52 sr_ = sample_rate;
53 sr_recip_ = 1.0f / sample_rate;
54 freq_ = 100.0f;
55 amp_ = 0.5f;
56 pw_ = 0.5f;
57 phase_ = 0.0f;
58 phase_inc_ = CalcPhaseInc(freq_);
59 waveform_ = WAVE_SIN;
60 eoc_ = true;
61 eor_ = true;
62 }
63
64
67 inline void SetFreq(const float f)
68 {
69 freq_ = f;
70 phase_inc_ = CalcPhaseInc(f);
71 }
72
73
76 inline void SetAmp(const float a) { amp_ = a; }
79 inline void SetWaveform(const uint8_t wf)
80 {
81 waveform_ = wf < WAVE_LAST ? wf : WAVE_SIN;
82 }
85 inline void SetPw(const float pw) { pw_ = fclamp(pw, 0.0f, 1.0f); }
86
89 inline bool IsEOR() { return eor_; }
90
93 inline bool IsEOC() { return eoc_; }
94
97 inline bool IsRising() { return phase_ < 0.5f; }
98
101 inline bool IsFalling() { return phase_ >= 0.5f; }
102
105 float Process();
106
107
110 void PhaseAdd(float _phase) { phase_ += _phase; }
113 void Reset(float _phase = 0.0f) { phase_ = _phase; }
114
115 private:
116 float CalcPhaseInc(float f);
117 uint8_t waveform_;
118 float amp_, freq_, pw_;
119 float sr_, sr_recip_, phase_, phase_inc_;
120 float last_out_, last_freq_;
121 bool eor_, eoc_;
122};
123} // namespace daisysp
124#endif
125#endif
Definition delayline.h:29
Definition oscillator.h:21
bool IsEOR()
Definition oscillator.h:89
void Init(float sample_rate)
Definition oscillator.h:50
void PhaseAdd(float _phase)
Definition oscillator.h:110
bool IsRising()
Definition oscillator.h:97
void SetWaveform(const uint8_t wf)
Definition oscillator.h:79
bool IsEOC()
Definition oscillator.h:93
void SetFreq(const float f)
Definition oscillator.h:67
void Reset(float _phase=0.0f)
Definition oscillator.h:113
float Process()
Definition oscillator.cpp:7
void SetPw(const float pw)
Definition oscillator.h:85
bool IsFalling()
Definition oscillator.h:101
void SetAmp(const float a)
Definition oscillator.h:76
FIR Filter implementation, generic and ARM CMSIS DSP based.
Definition adenv.h:16
float fclamp(float in, float min, float max)
Definition dsp.h:64