User:Army1987/terzosuono.c

From Wikipedia, the free encyclopedia

#define _ISOC99_SOURCE
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define FREQ0 600.
#define AMP 10000.
#define R 5.
#define SRATE 44.1e3
#define LEN 20.
#define TWOPI 6.283185307179586476925286766559
#define DITHER(x) nearbyint(x + (double)(rand())/RAND_MAX - (double)(rand()) / RAND_MAX)
int main(void)
{
        int i;
        int16_t sample;
        double t, a;
        for (i=0; i < LEN * SRATE; i++) {
                t = i / SRATE;
                a = AMP * sin(TWOPI * FREQ0 * t);
                sample = DITHER(a);
                fwrite(&sample, sizeof sample, 1, stdout);
                a = AMP * sin(TWOPI * (FREQ0 + (R/2)*t) * t);
/* The argument is the integral of 2*pi*f over t, where f is FREQ0 + R*t */
                sample = DITHER(a);
                fwrite(&sample, sizeof sample, 1, stdout);
        }
        return 0;
}