fixes to oscillators article

This commit is contained in:
veclav talica 2023-05-25 19:05:34 +05:00
parent 3dc8c60a99
commit 1a18af8baf

View File

@ -14,6 +14,8 @@ Inspirations are taken from [musicdsp](https://www.musicdsp.org/).
### Sine/Cosine ###
![](/articles/oscillators/sine.gif)
```c
/* Intended to be executed offline with values then embedded in the binary.
* By having usage of glibc sin and cos functions strictly offline it's easier to have it freestanding
@ -38,6 +40,8 @@ void pump_sinewave(struct sinewave *wave) {
### Square ###
![](/articles/oscillators/sqrt.gif)
```c
/* Implemented over sinewave */
struct sqrtwave {
@ -49,7 +53,9 @@ struct sqrtwave {
} init_sqrtwave(float frequency, float phase, float amplitude) {
struct sqrtwave r;
r.w = init_sinewave(frequency, phase, 1.f);
r.v.f = amplitude;
v.f = r.w.s;
a.f = amplitude;
r.v.u = (a.u & 0x7fffffff) | (v.u & 0x80000000);
return r;
}
@ -68,6 +74,8 @@ void pump_sqrtwave(struct sqrtwave *wave) {
### Saw ###
![](/articles/oscillators/sawt.gif)
```c
struct sawtwave {
float v, a, i;
@ -75,7 +83,7 @@ struct sawtwave {
struct sawtwave r;
r.v = sinf(phase) * amplitude;
r.a = amplitude;
r.i = frequency / AUDIO_FRAME_RATE * amplitude;
r.i = 2.f * frequency / AUDIO_FRAME_RATE * amplitude;
return r;
}
@ -83,7 +91,11 @@ struct sawtwave {
void pump_sawtwave(struct sawtwave *wave) {
wave->v += wave->i;
if (wave->v > wave->a)
wave->v -= wave->a;
wave->v -= wave->a * 2.f;
}
```
### Edits ###
- Fixed initial value based on phase in sqrtwave, proper range for sawtwave.
- Added waveform gifs.