wip: filter lfo

This commit is contained in:
2023-09-08 15:40:31 +04:00
parent 42f9be5dba
commit 4057bf5d42
7 changed files with 20 additions and 6 deletions

View File

@@ -13,7 +13,7 @@ struct Adder {
for (size_t i = 0; i < sample_count; i++) { for (size_t i = 0; i < sample_count; i++) {
float sample = 0.0f; float sample = 0.0f;
for (Oscillator* osc : oscillators) { for (Oscillator* osc : oscillators) {
sample += osc->GenerateSample(1.f); sample += osc->GenerateSample();
} }
signal[i] = sample; signal[i] = sample;

View File

@@ -31,5 +31,5 @@ class Oscillator {
float GetFreq() { return m_freq; } float GetFreq() { return m_freq; }
void SetFreq(float freq); void SetFreq(float freq);
void Reset(); void Reset();
float GenerateSample(float duration); float GenerateSample();
}; };

View File

@@ -15,13 +15,14 @@ class Synth {
std::vector<Oscillator*> m_oscillators; std::vector<Oscillator*> m_oscillators;
std::vector<Effect*> m_effects; std::vector<Effect*> m_effects;
std::vector<float> m_out_signal; std::vector<float> m_out_signal;
Oscillator* m_lfo;
void zero_signal(); void zero_signal();
void get_note(); void get_note();
void trigger_note_on_effects(); void trigger_note_on_effects();
void untrigger_note_on_effects(); void untrigger_note_on_effects();
void apply_effects(); void apply_effects();
void add_oscillator(); void add_oscillator();
void apply_filter_lfo();
public: public:
Synth(/* args */); Synth(/* args */);
~Synth(); ~Synth();

View File

@@ -18,6 +18,7 @@ void Filter::Trigger() {}
void Filter::Release() {} void Filter::Release() {}
float Filter::Process(float in) { float Filter::Process(float in) {
calculate_coefficients();
float out = in * m_a0 + m_z1; float out = in * m_a0 + m_z1;
m_z1 = in * m_a1 + m_z2 - m_b1 * out; m_z1 = in * m_a1 + m_z2 - m_b1 * out;
m_z2 = in * m_a2 - m_b2 * out; m_z2 = in * m_a2 - m_b2 * out;
@@ -28,7 +29,7 @@ void Filter::Process(std::vector<float>& samples) {
// todo: that will not work for ADSR-controlled filter. So, let's calculate // todo: that will not work for ADSR-controlled filter. So, let's calculate
// all the possible frequency values into array/dictionary then just lookup // all the possible frequency values into array/dictionary then just lookup
// for each sample // for each sample
calculate_coefficients();
for (std::size_t i = 0; i < samples.size(); i++) { for (std::size_t i = 0; i < samples.size(); i++) {
samples[i] = Process(samples[i]); samples[i] = Process(samples[i]);
} }

View File

@@ -4,7 +4,7 @@
LowPassFilter::LowPassFilter() { LowPassFilter::LowPassFilter() {
// todo: defaults // todo: defaults
m_freq = 200.f / SAMPLE_RATE; m_freq = 200.f / SAMPLE_RATE;
m_q = 0.707f; m_q = 1.0f;//0.707f;
m_order = 0; m_order = 0;
} }

View File

@@ -45,7 +45,7 @@ void Oscillator::SetFreq(float freq) {
m_phase_dt = (this->*m_dt_function)(freq); m_phase_dt = (this->*m_dt_function)(freq);
} }
float Oscillator::GenerateSample(float duration) { float Oscillator::GenerateSample() {
return (this->*m_osc_function)() * m_volume; return (this->*m_osc_function)() * m_volume;
} }

View File

@@ -6,6 +6,7 @@
#include "LowPassFilter.h" #include "LowPassFilter.h"
Synth::Synth(/* args */) { Synth::Synth(/* args */) {
m_lfo = new Oscillator(OscillatorType::Sine, 5.f, VOLUME);
add_oscillator(); add_oscillator();
add_oscillator(); add_oscillator();
AddEffect(new ADSR()); AddEffect(new ADSR());
@@ -69,7 +70,18 @@ void Synth::Trigger(Note input) {
trigger_note_on_effects(); trigger_note_on_effects();
} }
void Synth::apply_filter_lfo() {
float dt = m_lfo->GenerateSample();
Filter* filter = (Filter*)m_effects[1];
float freq = filter->GetFreq();
//todo: check formula
//filter->SetParameters(freq + dt * 0.2f, filter->GetRes(), filter->GetPeakGain());
}
void Synth::Process() { void Synth::Process() {
//todo: on each sample.
//in order to do that, we need to move to per-sample processing
apply_filter_lfo();
get_note(); get_note();
apply_effects(); apply_effects();
} }