#include #include template int32_t sine(int32_t t) { // 0 <= t < period is 0 <= t_2pi < 2 pi t %= period; float t_2pi = 2 * M_PI / static_cast(period) * t; return static_cast(sinf(t_2pi) * max_val); } template int32_t square(int32_t t) { t %= period; if (t <= period / 2) { return -max_val; } else { return max_val; } } template int32_t sawtooth(int32_t t) { // f(t) = a t + y // f(0) = -max_val = y // f(period) = a * period - max_val = max_val // a = 2 * max_val / period t %= period; return 2 * max_val * t / period - max_val; }