arngment/synth.c

46 lines
1.1 KiB
C
Raw Normal View History

2020-05-13 01:54:27 +02:00
/*
* arngment - random music generator
*
* Written in 2020 by Lucas
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <math.h>
/*
* Apparently, it's too hard for OSes to expose M_PI in a portable way.
* Definition taken from
* $OpenBSD: math.h,v 1.36 2018/03/10 20:52:58 kettenis Exp $
*/
#define JUST_PI ((double)3.14159265358979323846)
2020-05-13 01:54:27 +02:00
#define CLAMP(x, a, b) ((x) < (a) ? (a) : (x) > (b) ? (b) : (x))
double
synth_distortion(double gain, double x)
{
double xg = CLAMP(x * gain, -1.0, 1.0);
return (x + xg) / 2.0;
}
double
synth_sinewave(double t, double a)
{
return sin(2.0 * JUST_PI * t * a);
2020-05-13 01:54:27 +02:00
}
double
synth_trianglewave(double t, double a)
{
double x = t * a;
return 2.0 * fabs(2.0 * (x - floor(0.5 + x))) - 1.0;
}