1 /*************************************************************************/
2 /*                                                                       */
3 /*                  Language Technologies Institute                      */
4 /*                     Carnegie Mellon University                        */
5 /*                        Copyright (c) 2015                             */
6 /*                        All Rights Reserved.                           */
7 /*                                                                       */
8 /*  Permission is hereby granted, free of charge, to use and distribute  */
9 /*  this software and its documentation without restriction, including   */
10 /*  without limitation the rights to use, copy, modify, merge, publish,  */
11 /*  distribute, sublicense, and/or sell copies of this work, and to      */
12 /*  permit persons to whom this work is furnished to do so, subject to   */
13 /*  the following conditions:                                            */
14 /*   1. The code must retain the above copyright notice, this list of    */
15 /*      conditions and the following disclaimer.                         */
16 /*   2. Any modifications must be clearly marked as such.                */
17 /*   3. Original authors' names are not deleted.                         */
18 /*   4. The authors' names are not used to endorse or promote products   */
19 /*      derived from this software without specific prior written        */
20 /*      permission.                                                      */
21 /*                                                                       */
22 /*  CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK         */
23 /*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
24 /*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
25 /*  SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE      */
26 /*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
27 /*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
28 /*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
29 /*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
30 /*  THIS SOFTWARE.                                                       */
31 /*                                                                       */
32 /*************************************************************************/
33 /*             Author:  Alan W Black (awb@cs.cmu.edu)                    */
34 /*               Date:  August 2015                                      */
35 /*************************************************************************/
36 /*                                                                       */
37 /*  subtract window average from waveform                                */
38 /*************************************************************************/
39 #include <stdio.h>
40 #include "flite.h"
41 #include "cst_wave.h"
42 #include "cst_args.h"
43 
main(int argc,char ** argv)44 int main(int argc, char **argv)
45 {
46     cst_wave *in, *out;
47     cst_val *files;
48     cst_features *args;
49     int i,j;
50     int w, a, t;
51 
52     args = new_features();
53     files =
54         cst_args(argv,argc,
55                  "usage: dcoffset_wave OPTIONS\n"
56                  "Subtract window average from waveform\n"
57 		 "-i <string>  Input waveform\n"
58 		 "-o <string>  Output waveform\n"
59 		 "-w <int>     Window size (in samples)\n",
60                  args);
61 
62     w = flite_get_param_int(args,"-w",20);
63     in = new_wave();
64     cst_wave_load_riff(in,flite_get_param_string(args,"-i","-"));
65 
66     out = copy_wave(in);
67 
68     for (i=0; i<=out->num_samples; i++)
69     {
70         for (t=a=0,j=i-w/2; j < i+w/2; j++)
71         {
72             if ((j > 0) && (j < out->num_samples))
73             {
74                 t += 1;
75                 a+=in->samples[j];
76             }
77         }
78         /*        printf("%d %d %d %d %d\n",i,out->samples[i],a/t,t,out->samples[i]-a/t); */
79         out->samples[i] -= a/t;
80     }
81 
82     cst_wave_save_riff(out,flite_get_param_string(args,"-o","-"));
83 
84     return 0;
85 }
86