-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsine.c
82 lines (69 loc) · 1.78 KB
/
sine.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
79
80
81
82
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "synth.h"
#include "rate.inc"
/* sine: generate a sine wave */
int main(int argc, char *argv[])
{
float freq = 1000.0f, amp = 0.5f, startphase = 0.0f;
double phase, inc;
long len;
long n;
int i;
int modfreq = 0; /* use input to modulate frequency */
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)
startphase = atof(argv[++i]); /* phase in degrees */
else if (!strcmp(argv[i], "-modfreq"))
modfreq = 1; /* modulate frequency with input */
else if (!strcmp(argv[i], "-help"))
{
fprintf(stderr, "options: -freq arg, -amp arg, -len "
"arg, -phase arg, -modfreq\n");
exit(0);
}
}
/* check options */
freq = CLAMP(0.01f, freq, RATE / 22.0f * 10.f);
amp = CLAMP(0.0f, amp, 1.0f);
startphase = CLAMP(0.0f, startphase, 359.99f);
/* convert options */
startphase = startphase * M_PI / 180.0f;
phase = startphase;
inc = 2*M_PI * freq / RATE;
SET_BINARY_MODE
for (n = 0; n < len; n++)
{
float f;
if (modfreq)
{
float inputs[2], avg;
if (fread(inputs, sizeof inputs[0], 2, stdin) < 2)
avg = 0.0f;
else
avg = (inputs[0] + inputs[1] / 2);
phase += avg;
}
f = amp * sin(phase);
phase += inc;
/* left channel */
if (fwrite(&f, sizeof f, 1, stdout) < 1)
break;
/* right channel */
if (fwrite(&f, sizeof f, 1, stdout) < 1)
break;
}
return 0;
}