summaryrefslogtreecommitdiff
path: root/click-finder.c
blob: 54efb0f4bf1482f827c94d2f569946df06b731c2 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "click-finder.h"
#include "version.h"
// #include <fftw3.h>

static int verbose_flag;
static long long int bytes_per_sample = 3;
static long long int channels = 1;

int
main(int argc, char **argv)
  {
    int c;
    int samples_read;
    while (1)
      {
        static struct option long_options[] =
          {
            {"help",          no_argument,       0,             0},
            {"version",       no_argument,       0,             0},
            {"verbose",       no_argument,       &verbose_flag, 1},
            {"brief",         no_argument,       &verbose_flag, 0},
            {"bitspersample", required_argument, 0,             'b'},
            {"channels",      required_argument, 0,             'c'},
            {0, 0, 0, 0}
          };
        /* getopt_long stores the option index here. */
        int option_index = 0;

        c = getopt_long (argc, argv, "b:hv",
                         long_options, &option_index);
        /* Detect the end of the options. */
        if (c == -1)
          break;

        switch (c)
          {
            case 0:
              /* If this option set a flag, do nothing else now. */
              if (long_options[option_index].flag != 0)
                break;
              switch (option_index)
                {
                  case 0:
                    printf("Print help here.\n");
                    exit(0);
                  case 1:
                    printf("This is %s, version %s\n", progname, VERSION);
                    exit(0);
                  default:
                    fprintf (stderr, "unknown option %s", long_options[option_index].name);
                    if (optarg)
                      fprintf (stderr, " with arg %s", optarg);
                    fprintf (stderr, "\n");
                    exit(1);
                }
              break;

            case 'b':
              if (atoi(optarg) % 8 != 0)
                {
                  fprintf(stderr, "%s: bits per sample (%d) is no multiple of 8", progname, atoi(optarg));
                  exit(1);
                }
              bytes_per_sample = atoi(optarg)/8;
              break;

            case 'c':
              channels = atoi(optarg);
              break;

            case 'v':
              verbose_flag=1;
              break;

            case '?':
              /* getopt_long already printed an error message. */
              exit(1);

            default:
              exit(1);
          }
      }

    if (optind != argc)
      {
        fprintf(stderr, "%s: spurious option '%s'\n", progname, argv[optind]);
        exit(1);
      }

    if (verbose_flag)
      {
        fprintf(stderr, "bps:      %d\n", bytes_per_sample*8);
        fprintf(stderr, "channels: %d\n", channels);
      }

    int8_t *content = malloc(sizeof(int8_t) * bytes_per_sample * channels * BUF_SIZE);
    long long int content_length = 0; // in bytes
    if (content == NULL)
      {
        fprintf(stderr, "%s: Failed to allocate content", progname);
        exit(1);
      }
    while (samples_read = fread(content + content_length, bytes_per_sample * channels, BUF_SIZE, stdin))
      {
        content_length += samples_read * bytes_per_sample * channels;
        content = realloc(content, content_length + sizeof(int8_t) * bytes_per_sample * channels * BUF_SIZE);
      }

    if(ferror(stdin))
      {
        free(content);
        fprintf(stderr, "%s: Error reading from stdin.", progname);
        exit(1);
      }

    fprintf(stderr, "%d", content_length);

    free(content);
    if (verbose_flag)
      fprintf(stderr, "success\n");
    return 0;
  }