Skip to content

Instantly share code, notes, and snippets.

@fredrb
Created August 9, 2023 07:07
Show Gist options
  • Save fredrb/d11131cffc951563104e671474ef10b1 to your computer and use it in GitHub Desktop.
Save fredrb/d11131cffc951563104e671474ef10b1 to your computer and use it in GitHub Desktop.
typedef struct {
float current_step;
float step_size;
float volume;
} oscillator;
oscillator oscillate(float rate, float volume) {
oscillator o = {
.current_step = 0,
.volume = volume,
.step_size = (2 * M_PI) / rate,
};
return o;
}
float next(oscillator *os) {
os->current_step += os->step_size;
return sinf(os->current_step) * os->volume;
}
// using the oscillator:
float A4_freq = (float)SAMPLE_RATE / 440.00f;
a4 = oscillate(A4_freq, 0.8f)
next(a4); // returns the next sample point in the A4 sine wave
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment