-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenvelope.c
98 lines (85 loc) · 1.83 KB
/
envelope.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "synth.h"
#include "rate.inc"
/* env.c: apply or generate an envelope */
typedef struct
{
int len;
float end;
} envpoint_t;
static void envelope(float start, envpoint_t *envs, int numenvs, int apply);
#define MAXPTS 400
int main(int argc, char *argv[])
{
float start = 1.0f;
int apply;
envpoint_t envs[MAXPTS];
int numenvs = 0;
int i;
int startset = 0;
get_rate();
/* by default apply env if stdin is not tty */
apply = !isatty(STDIN_FILENO);
for (i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-generate")) /* generate envelope */
apply = 0;
else if (!strcmp(argv[i], "-help"))
{
fprintf(stderr, "options: -generate\n");
fprintf(stderr, "args: start len level "
"[len level]... -- len is in seconds\n");
exit(0);
}
else if (!startset)
{
startset = 1;
start = atof(argv[i]);
}
else if (numenvs == MAXPTS)
{
fprintf(stderr, "warning: too many points "
"(hit limit %d), ignoring rest\n", MAXPTS);
}
else if (i + 1 < argc)
{
envs[numenvs].len = (int)(RATE * atof(argv[i]));
i++;
envs[numenvs].end = atof(argv[i]);
numenvs++;
}
}
SET_BINARY_MODE
envelope(start, envs, numenvs, apply);
return 0;
}
static void envelope(float start, envpoint_t *envs, int numenvs, int apply)
{
float amp = start, d_amp, end = start;
float f[2] = {1.0f, 1.0f};
int stage, pos, len;
for (stage = 0; stage < numenvs; stage++)
{
amp = end; /* prevent it from getting a little off */
end = envs[stage].end;
len = envs[stage].len;
d_amp = (end - amp) / len;
for (pos = 0; pos < len; pos++)
{
if (!apply)
f[0] = f[1] = amp;
else if (fread(f, sizeof f[0], 2, stdin) < 2)
return;
else
{
f[0] *= amp;
f[1] *= amp;
}
amp += d_amp;
if (fwrite(f, sizeof f[0], 2, stdout) < 2)
return;
}
}
}