diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2020-02-10 23:47:34 -0800 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2020-02-10 23:47:34 -0800 |
commit | 8ff41d060344af4d045bdd79bb826231d6739c7f (patch) | |
tree | e7e688c2d06d498b86838367c7e59203a58fb650 /src | |
parent | 64a0fe99038b9c58a815a92db2eb2ded1fd2801a (diff) |
libs/sig: triangle wave; sig_ops_tri / sig_new_tri()
Added a triangle wave
Diffstat (limited to 'src')
-rw-r--r-- | src/libs/sig/ops_sin.c | 23 | ||||
-rw-r--r-- | src/libs/sig/sig.c | 6 | ||||
-rw-r--r-- | src/libs/sig/sig.h | 3 |
3 files changed, 31 insertions, 1 deletions
diff --git a/src/libs/sig/ops_sin.c b/src/libs/sig/ops_sin.c index da5b371..4acc8e5 100644 --- a/src/libs/sig/ops_sin.c +++ b/src/libs/sig/ops_sin.c @@ -50,6 +50,15 @@ static float output_sqr(float rads) } +static float output_tri(float rads) +{ + /* This approximation comes from: + * https://calculushowto.com/triangle-wave-function/ + */ + return M_2_PI * asinf(fabsf(sinf(M_PI * rads))); +} + + static float output(void *context, unsigned ticks_ms, float (*output_fn)(float rads)) { ops_sin_ctxt_t *ctxt = context; @@ -87,6 +96,12 @@ static float ops_sqr_output(void *context, unsigned ticks_ms) } +static float ops_tri_output(void *context, unsigned ticks_ms) +{ + return output(context, ticks_ms, output_tri); +} + + sig_ops_t sig_ops_sin = { .size = ops_sin_size, .init = ops_sin_init, @@ -101,3 +116,11 @@ sig_ops_t sig_ops_sqr = { .destroy = ops_sin_destroy, .output = ops_sqr_output, }; + + +sig_ops_t sig_ops_tri = { + .size = ops_sin_size, + .init = ops_sin_init, + .destroy = ops_sin_destroy, + .output = ops_tri_output, +}; diff --git a/src/libs/sig/sig.c b/src/libs/sig/sig.c index 87a76c4..203a37a 100644 --- a/src/libs/sig/sig.c +++ b/src/libs/sig/sig.c @@ -240,6 +240,12 @@ sig_t * sig_new_sqr(sig_t *hz) } +sig_t * sig_new_tri(sig_t *hz) +{ + return sig_new(&sig_ops_tri, hz); +} + + sig_t * sig_new_sub(sig_t *a, sig_t *b) { return sig_new(&sig_ops_sub, a, b); diff --git a/src/libs/sig/sig.h b/src/libs/sig/sig.h index 917dbc4..9be3012 100644 --- a/src/libs/sig/sig.h +++ b/src/libs/sig/sig.h @@ -38,15 +38,16 @@ sig_t * sig_new_round(sig_t *x); sig_t * sig_new_scale(sig_t *x, sig_t *min, sig_t *max); sig_t * sig_new_sin(sig_t *hz); sig_t * sig_new_sqr(sig_t *hz); +sig_t * sig_new_tri(sig_t *hz); sig_t * sig_new_sub(sig_t *a, sig_t *b); extern sig_ops_t sig_ops_const; extern sig_ops_t sig_ops_rand; extern sig_ops_t sig_ops_sin; extern sig_ops_t sig_ops_sqr; +extern sig_ops_t sig_ops_tri; /* TODO: -extern sig_ops_t sig_ops_tri; extern sig_ops_t sig_ops_saw; */ |