1 /*
2  * pvffilter.c
3  *
4  * pvffilter can filter a range of frequencies out of a given voice signal.
5  * Input and output is in the pvf (portable voice format) format.
6  *
7  * $Id: pvffilter.c,v 1.2 1999/03/16 09:59:23 marcs Exp $
8  *
9  */
10 
11 #include "../include/voice.h"
12 
13 char *program_name;
14 
usage(void)15 static void usage (void)
16      {
17      fprintf(stderr, "\n%s %s\n\n", program_name, vgetty_version);
18      fprintf(stderr, "usage:\n");
19      fprintf(stderr, "\t%s [options] [<pvffile in> [<pvffile out>]]\n",
20       program_name);
21      fprintf(stderr, "\noptions:\n");
22      fprintf(stderr, "\t-h     this help message\n");
23      fprintf(stderr, "\t-F <n> frequency of the filter in Hz (default is 1000)\n");
24      fprintf(stderr, "\t-W <n> width of the filter in Hz (default is 100)\n");
25      fprintf(stderr, "\t-a     output pvf ascii format\n");
26      fprintf(stderr, "\t-b     output pvf binary format (default)\n");
27      fprintf(stderr, "\t-8     output 8 bit samples\n");
28      fprintf(stderr, "\t-16    output 16 bit samples\n");
29      fprintf(stderr, "\t-32    output 32 bit samples (default)\n\n");
30      exit(ERROR);
31      }
32 
main(int argc,char * argv[])33 int main (int argc, char *argv[])
34      {
35      int option;
36      FILE *fd_in = stdin;
37      FILE *fd_out = stdout;
38      char *name_in = "stdin";
39      char *name_out = "stdout";
40      pvf_header header_in = init_pvf_header;
41      pvf_header header_out = init_pvf_header;
42      double fwidth = 100.0;
43      double ffrequency = 1000.0;
44 
45      check_system();
46      program_name = argv[0];
47 
48      while ((option = getopt(argc, argv, "abh12368F:W:")) != EOF)
49           {
50 
51           switch (option)
52                {
53                case 'a':
54                     header_out.ascii = TRUE;
55                     break;
56                case 'b':
57                     header_out.ascii = FALSE;
58                     break;
59                case '8':
60                     header_out.nbits = 8;
61                     break;
62                case '1':
63                case '6':
64                     header_out.nbits = 16;
65                     break;
66                case '3':
67                case '2':
68                     header_out.nbits = 32;
69                     break;
70                case 'F':
71                     ffrequency = atof(optarg);
72                     break;
73                case 'W':
74                     fwidth = atof(optarg);
75                     break;
76                default:
77                     usage();
78                };
79 
80           };
81 
82      if (optind < argc)
83           {
84           name_in = argv[optind];
85 
86           if ((fd_in = fopen(name_in, "r")) == NULL)
87                {
88                fprintf(stderr, "%s: Could not open file %s\n", program_name,
89                 name_in);
90                exit(FAIL);
91                };
92 
93           optind++;
94           };
95 
96      if (read_pvf_header(fd_in, &header_in) != OK)
97           exit(ERROR);
98 
99      header_out.speed = header_in.speed;
100      fwidth /= header_in.speed;
101      ffrequency /= header_in.speed;
102 
103      if (optind < argc)
104           {
105           name_out = argv[optind];
106 
107           if ((fd_out = fopen(name_out, "w")) == NULL)
108                {
109                fprintf(stderr, "%s: Could not open file %s\n", program_name,
110                 name_out);
111                exit(FAIL);
112                };
113 
114           };
115 
116      if (write_pvf_header(fd_out, &header_out) != OK)
117           {
118           fclose(fd_out);
119 
120           if (fd_out != stdout)
121                unlink(name_out);
122 
123           exit(ERROR);
124           }
125 
126      {
127      int p;
128      #define N 50
129      double c[2 * N + 1];
130 /*     double c[2 * N + 1] = {7.0, 24.0, 34.0, 24.0, 7.0}; */
131      double u[2 * N + 1];
132 
133      for (p = -N; p <= N; p++)
134           {
135 
136           if (p != 0)
137                c[p + N] = 2.0 / p / M_PI * cos(2.0 * M_PI * p * ffrequency) *
138                 sin(M_PI * p * fwidth);
139           else
140                c[p + N] = 2.0 * fwidth;
141 
142           u[p + N] = 0.0;
143           };
144 
145      for (p = 0; p < N; p++)
146           u[p] = header_in.read_pvf_data(fd_in) / ONE;
147 
148      p = N;
149 
150      while (1)
151           {
152           static int k;
153           static double y;
154 
155           u[p] = header_in.read_pvf_data(fd_in) / ONE;
156           if (feof(fd_in))
157                break;
158           y = 0.0;
159 
160           for (k = -N; k <= N; k++)
161                y += c[k + N] * u[(k + (p - N) + (2 * N + 1)) % (2 * N + 1)];
162 
163 /*          y = u[((p - N) + (2 * N + 1)) % (2 * N + 1)] - y; */
164           header_out.write_pvf_data(fd_out, (int) y * ONE);
165           p++;
166           p %= (2 * N + 1);
167           }
168 
169      }
170 
171      fclose(fd_in);
172      fclose(fd_out);
173      exit(OK);
174      }
175