0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-27 11:33:54 +01:00
This commit is contained in:
2025-04-27 13:11:42 +02:00
parent ca82168784
commit c4d5c39ed5
17 changed files with 82 additions and 149 deletions

42
dsp/oscillator.c Normal file
View File

@@ -0,0 +1,42 @@
#include "oscillator.h"
void init_oscillator(Oscillator *osc, float frequency, float sample_rate) {
osc->phase = 0.0f;
osc->phase_increment = (M_2PI * frequency) / sample_rate;
osc->sample_rate = sample_rate;
}
void change_oscillator_frequency(Oscillator *osc, float frequency) {
osc->phase_increment = (M_2PI * frequency) / osc->sample_rate;
}
float get_oscillator_sin_sample(Oscillator *osc) {
float sample = sinf(osc->phase);
advance_oscillator(osc);
return sample;
}
float get_oscillator_cos_sample(Oscillator *osc) {
float sample = cosf(osc->phase);
advance_oscillator(osc);
return sample;
}
float get_oscillator_sin_multiplier_ni(Oscillator *osc, float multiplier) {
float new_phase = osc->phase * multiplier;
new_phase -= (new_phase >= M_2PI) ? M_2PI : 0.0f;
return sinf(new_phase);
}
float get_oscillator_cos_multiplier_ni(Oscillator *osc, float multiplier) {
float new_phase = osc->phase * multiplier;
new_phase -= (new_phase >= M_2PI) ? M_2PI : 0.0f;
return cosf(new_phase);
}
void advance_oscillator(Oscillator *osc) {
osc->phase += osc->phase_increment;
if (osc->phase >= M_2PI) {
osc->phase -= M_2PI;
}
}