-
Notifications
You must be signed in to change notification settings - Fork 0
/
waves.c
48 lines (38 loc) · 1.05 KB
/
waves.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <math.h>
#include <stdint.h>
/*
** "steps" = sampling frequency / note frequency
** number of samples per oscillation/period
*/
int16_t triangle(double steps, uint32_t pos) {
double quarter, inc;
pos = pos % (uint32_t)steps;
quarter = steps / 4.0;
inc = (double)INT16_MAX / quarter;
if (pos < quarter) {
return inc * pos;
} else if (pos < 3 * quarter) {
return INT16_MAX - (inc * (pos - quarter));
}
return -INT16_MAX + (inc * (pos - 3 * quarter));
}
int16_t sawtooth(double steps, uint32_t pos) {
double half, inc;
pos = pos % (uint32_t)steps;
half = steps / 2.0;
inc = (double)INT16_MAX / half;
if (pos < half) {
return inc * pos;
}
return inc * (pos - half) - INT16_MAX;
}
int16_t sine(double steps, uint32_t pos) {
double inc;
pos = pos % (uint32_t)steps;
inc = (2.0 * M_PI) / steps;
return sin(inc * pos) * INT16_MAX;
}
int16_t square(double steps, uint32_t pos) {
pos = pos % (uint32_t)steps;
return pos < steps / 2 ? -INT16_MAX : INT16_MAX;
}