aboutsummaryrefslogtreecommitdiffstats
path: root/fgen.c
blob: 10b9b510a6b9f1a0ce5d909558d19aef4851c14e (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char **argv)
{
        if (argc != 4) {
                fprintf(stderr, "Usage: %s <sampling-frequency> <frequency> <duration> [amplitude]\n", argv[0]);
                exit(1);
        }

        long int sfreq = strtol(argv[1], NULL, 10);
        long double freq = strtod(argv[2], NULL);
        if (freq <= 0) freq = 1;
        long double duration = strtod(argv[3], NULL);
        long double amplitude = 127;

        if (argc == 5) {
                amplitude = strtol(argv[4], NULL, 10);
                if (amplitude > 127) amplitude = 127;
                else if (amplitude < 0) amplitude = 0;
        }

        long double wave_length = sfreq / freq;

        for (long int i = 0; i < duration / 1000 * sfreq; i++)
                putchar((char)((sinf(i / wave_length * M_PI_2 * 4) + 1) * amplitude));

        return 0;
}