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 ### ### Sine/Cosine ###
![](/articles/oscillators/sine.gif)
```c ```c
/* Intended to be executed offline with values then embedded in the binary. /* 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 * 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 ### ### Square ###
![](/articles/oscillators/sqrt.gif)
```c ```c
/* Implemented over sinewave */ /* Implemented over sinewave */
struct sqrtwave { struct sqrtwave {
@ -49,7 +53,9 @@ struct sqrtwave {
} init_sqrtwave(float frequency, float phase, float amplitude) { } init_sqrtwave(float frequency, float phase, float amplitude) {
struct sqrtwave r; struct sqrtwave r;
r.w = init_sinewave(frequency, phase, 1.f); 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; return r;
} }
@ -68,6 +74,8 @@ void pump_sqrtwave(struct sqrtwave *wave) {
### Saw ### ### Saw ###
![](/articles/oscillators/sawt.gif)
```c ```c
struct sawtwave { struct sawtwave {
float v, a, i; float v, a, i;
@ -75,7 +83,7 @@ struct sawtwave {
struct sawtwave r; struct sawtwave r;
r.v = sinf(phase) * amplitude; r.v = sinf(phase) * amplitude;
r.a = amplitude; r.a = amplitude;
r.i = frequency / AUDIO_FRAME_RATE * amplitude; r.i = 2.f * frequency / AUDIO_FRAME_RATE * amplitude;
return r; return r;
} }
@ -83,7 +91,11 @@ struct sawtwave {
void pump_sawtwave(struct sawtwave *wave) { void pump_sawtwave(struct sawtwave *wave) {
wave->v += wave->i; wave->v += wave->i;
if (wave->v > wave->a) 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.