File oscillator.h¶
File List > DaisySP > Source > Synthesis > oscillator.h
Go to the documentation of this file
Source Code¶
/*
Copyright (c) 2020 Electrosmith, Corp
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
#pragma once
#ifndef DSY_OSCILLATOR_H
#define DSY_OSCILLATOR_H
#include <stdint.h>
#include "Utility/dsp.h"
#ifdef __cplusplus
namespace daisysp
{
class Oscillator
{
public:
Oscillator() {}
~Oscillator() {}
enum
{
WAVE_SIN,
WAVE_TRI,
WAVE_SAW,
WAVE_RAMP,
WAVE_SQUARE,
WAVE_POLYBLEP_TRI,
WAVE_POLYBLEP_SAW,
WAVE_POLYBLEP_SQUARE,
WAVE_LAST,
};
void Init(float sample_rate)
{
sr_ = sample_rate;
sr_recip_ = 1.0f / sample_rate;
freq_ = 100.0f;
amp_ = 0.5f;
pw_ = 0.5f;
phase_ = 0.0f;
phase_inc_ = CalcPhaseInc(freq_);
waveform_ = WAVE_SIN;
eoc_ = true;
eor_ = true;
}
inline void SetFreq(const float f)
{
freq_ = f;
phase_inc_ = CalcPhaseInc(f);
}
inline void SetAmp(const float a) { amp_ = a; }
inline void SetWaveform(const uint8_t wf)
{
waveform_ = wf < WAVE_LAST ? wf : WAVE_SIN;
}
inline void SetPw(const float pw) { pw_ = fclamp(pw, 0.0f, 1.0f); }
inline bool IsEOR() { return eor_; }
inline bool IsEOC() { return eoc_; }
inline bool IsRising() { return phase_ < 0.5f; }
inline bool IsFalling() { return phase_ >= 0.5f; }
float Process();
void PhaseAdd(float _phase) { phase_ += _phase; }
void Reset(float _phase = 0.0f) { phase_ = _phase; }
private:
float CalcPhaseInc(float f);
uint8_t waveform_;
float amp_, freq_, pw_;
float sr_, sr_recip_, phase_, phase_inc_;
float last_out_, last_freq_;
bool eor_, eoc_;
};
} // namespace daisysp
#endif
#endif