aboutsummaryrefslogtreecommitdiffstats
path: root/fgen.c
blob: c4539c364beaf1e27515328168b79423d14b9369 (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
31
32
33
34
35
36
37
38
39
40
/*
 * Copyright (C) 2014  Tomasz Kramkowski <tk@the-tk.com>
 *
 * This program is free software. It is licensed under version 3 of the
 * GNU General Public License.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
 */

#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;
}