Created
August 9, 2023 07:07
-
-
Save fredrb/d11131cffc951563104e671474ef10b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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