aboutsummaryrefslogtreecommitdiff
path: root/unittest/mksine.c
blob: a77830c22969b5f0e38f90f7a36393450e0d9205 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
  mksine.c
  David Rowe
  10 Nov 2010

  Creates a file of sine wave samples.
*/

#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TWO_PI 6.283185307
#define FS 8000.0

int main(int argc, char *argv[]) {
  FILE *f;
  int i, n;
  float freq, length;
  short *buf;
  float amp = 1E4;

  if (argc < 4) {
    printf("usage: %s outputFile frequencyHz lengthSecs [PeakAmp]\n", argv[0]);
    exit(1);
  }

  if (strcmp(argv[1], "-") == 0) {
    f = stdout;
  } else if ((f = fopen(argv[1], "wb")) == NULL) {
    fprintf(stderr, "Error opening output file: %s: %s.\n", argv[3],
            strerror(errno));
    exit(1);
  }
  freq = atof(argv[2]);
  length = atof(argv[3]);
  if (argc == 5) amp = atof(argv[4]);

  n = length * FS;
  buf = (short *)malloc(sizeof(short) * n);
  assert(buf != NULL);

  for (i = 0; i < n; i++) buf[i] = amp * cos(freq * i * (TWO_PI / FS));

  fwrite(buf, sizeof(short), n, f);

  fclose(f);
  free(buf);

  return 0;
}