-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtri.c
78 lines (66 loc) · 1.58 KB
/
tri.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "synth.h"
#include "rate.inc"
/* tri: generate a triangle wave */
static float triangle(float input)
{
input = fmod(input, 360.0f);
input /= 90.0f;
if (input < 1.0f)
return input;
else if (input < 3.0f)
return -input + 2.0f;
else
return input - 4.0f;
}
int main(int argc, char *argv[])
{
float freq = 1000, amp = 0.5f, phase = 0.0f;
float period;
long len;
long n;
int i;
get_rate();
len = RATE;
/* read options */
for (i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-freq") && i+1 < argc)
freq = atof(argv[++i]); /* frequency of the wave */
else if (!strcmp(argv[i], "-amp") && i+1 < argc)
amp = atof(argv[++i]); /* amplitude from 0 to 1 */
else if (!strcmp(argv[i], "-len") && i+1 < argc)
len = atof(argv[++i]) / 1000.0f * RATE; /* length in ms */
else if (!strcmp(argv[i], "-phase") && i+1 < argc)
phase = atof(argv[++i]); /* phase in degrees */
else if (!strcmp(argv[i], "-help"))
{
fprintf(stderr, "options: -freq arg, -amp arg, -len "
"arg, -phase arg\n");
exit(0);
}
}
/* check options */
freq = CLAMP(0.01f, freq, RATE / 22.0f * 10.f);
amp = CLAMP(0.0f, amp, 1.0f);
phase = CLAMP(0.0f, phase, 359.99f);
/* convert options */
period = RATE / freq;
phase *= period;
SET_BINARY_MODE
for (n = 0; n < len; n++)
{
float f;
f = triangle((n + phase) * 360.0f / period) * amp;
/* left channel */
if (fwrite(&f, sizeof f, 1, stdout) < 1)
break;
/* right channel */
if (fwrite(&f, sizeof f, 1, stdout) < 1)
break;
}
return 0;
}