-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshape.c
65 lines (53 loc) · 1.15 KB
/
shape.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "synth.h"
/* shape.c: waveshape a waveform */
static void shape(float range, float gradation);
int main(int argc, char *argv[])
{
float range = 1.0f;
float gradation = 3.0f;
int i;
/* read options */
for (i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-range") && i+1 < argc)
range = atof(argv[++i]);
else if (!strcmp(argv[i], "-gradation") && i+1 < argc)
gradation = atof(argv[++i]);
else if (!strcmp(argv[i], "-help"))
{
fprintf(stderr, "options: -range arg, -gradation "
"arg\n");
exit(0);
}
}
/* check options */
range = CLAMP(-2.0f, range, 2.0f);
gradation = CLAMP(0.1f, gradation, 15.0f);
SET_BINARY_MODE
shape(range, gradation);
return 0;
}
static float shapeval(float val, float r, float g)
{
float x = val;
if (x < -r/(g+1))
x = x/g - r/(g+1) * (g-1/g);
else if (x > r/(g+1))
x = x/g + r/(g+1) * (g-1/g);
else
x *= g;
return x;
}
static void shape(float range, float gradation)
{
float f;
while (fread(&f, sizeof f, 1, stdin) == 1)
{
f = shapeval(f, range, gradation);
if (fwrite(&f, sizeof f, 1, stdout) < 1)
return;
}
}