summaryrefslogtreecommitdiffstats
path: root/sinewave.c
blob: 6e6180f0193ad6e3fbfd0de3b3433f9e6911551e (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
#include <inttypes.h>
#include <stdio.h>
#include <netinet/in.h>
#include <math.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  double samplerate = 44100.0;
  double frequency  = atof(argv[1]);
  int samples = atoi(argv[2]);
  double increment = 2*M_PI*frequency/samplerate;
  double x = 0.0;
  int16_t sample[2];

  fprintf(stderr, "increment = %g\n", increment);

  while ( samples-- ) {
    int16_t s = htons((int16_t)(sin(x)*32767.0));
    sample[0] = sample[1] = s;
    fwrite(sample, 2, 2, stdout);
    
    x = fmod(x+increment, 2*M_PI);
  }

  return 0;
}