1 /*---------------------------------------------------------------------------*\
2 
3   FILE........: tcontphase.c
4   AUTHOR......: David Rowe
5   DATE CREATED: 11/9/09
6 
7   Test program for developing continuous phase track synthesis algorithm.
8   However while developing this it was discovered that synthesis_mixed()
9   worked just as well.
10 
11 \*---------------------------------------------------------------------------*/
12 
13 /*
14   Copyright (C) 2009 David Rowe
15 
16   All rights reserved.
17 
18   This program is free software; you can redistribute it and/or modify
19   it under the terms of the GNU Lesser General Public License version 2.1, as
20   published by the Free Software Foundation.  This program is
21   distributed in the hope that it will be useful, but WITHOUT ANY
22   WARRANTY; without even the implied warranty of MERCHANTABILITY or
23   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
24   License for more details.
25 
26   You should have received a copy of the GNU Lesser General Public License
27   along with this program; if not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #define N  80		/* frame size          */
31 #define F 160           /* frames to synthesis */
32 #define P  10           /* LPC order           */
33 
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <math.h>
38 #include "sine.h"
39 #include "dump.h"
40 #include "synth.h"
41 #include "phase.h"
42 
43 int   frames;
44 
45 float ak[] = {
46  1.000000,
47 -1.455836,
48  1.361841,
49 -0.879267,
50  0.915985,
51 -1.002202,
52  0.944103,
53 -0.743094,
54  1.053356,
55 -0.817491,
56  0.431222
57 };
58 
59 
60 /*---------------------------------------------------------------------------*\
61 
62   switch_present()
63 
64   Searches the command line arguments for a "switch".  If the switch is
65   found, returns the command line argument where it ws found, else returns
66   NULL.
67 
68 \*---------------------------------------------------------------------------*/
69 
switch_present(sw,argc,argv)70 int switch_present(sw,argc,argv)
71   char sw[];     /* switch in string form */
72   int argc;      /* number of command line arguments */
73   char *argv[];  /* array of command line arguments in string form */
74 {
75   int i;       /* loop variable */
76 
77   for(i=1; i<argc; i++)
78     if (!strcmp(sw,argv[i]))
79       return(i);
80 
81   return 0;
82 }
83 
84 /*---------------------------------------------------------------------------*\
85 
86                                     MAIN
87 
88 \*---------------------------------------------------------------------------*/
89 
main(argc,argv)90 int main(argc,argv)
91 int argc;
92 char *argv[];
93 {
94     FILE *fout;
95     short buf[N];
96     int   i,j;
97     int   dump;
98     float phi_prev[MAX_AMP];
99     float Wo_prev, ex_phase, G;
100     //float ak[P+1];
101     COMP  H[MAX_AMP];
102     float f0;
103 
104     if (argc < 3) {
105 	printf("\nusage: %s OutputRawSpeechFile F0\n", argv[0]);
106         exit(1);
107     }
108 
109     /* Output file */
110 
111     if ((fout = fopen(argv[1],"wb")) == NULL) {
112       printf("Error opening output speech file: %s\n",argv[1]);
113       exit(1);
114     }
115 
116     f0 = atof(argv[2]);
117 
118     dump = switch_present("--dump",argc,argv);
119     if (dump)
120       dump_on(argv[dump+1]);
121 
122     init_decoder();
123 
124     for(i=0; i<MAX_AMP; i++)
125 	phi_prev[i] = 0.0;
126     Wo_prev = 0.0;
127 
128     model.Wo = PI*(f0/4000.0);
129     G = 1000.0;
130     model.L = floor(PI/model.Wo);
131 
132     //aks_to_H(&model, ak, G , H, P);
133     //for(i=1; i<=model.L; i++)
134 	model.A[i] = sqrt(H[i].real*H[i].real + H[i].imag*H[i].imag);
135     //printf("L = %d\n", model.L);
136     //model.L = 10;
137     for(i=1; i<=model.L; i++) {
138       model.A[i]   = 1000/model.L;
139       model.phi[i] = 0;
140       H[i].real = 1.0; H[i].imag = 0.0;
141     }
142 
143     //ak[0] = 1.0;
144     //for(i=1; i<=P; i++)
145     //  ak[i] = 0.0;
146 
147     frames = 0;
148     for(j=0; j<F; j++) {
149 	frames++;
150 
151 	#ifdef SWAP
152 	/* lets make phases bounce around from frame to frame.  This
153 	   could happen if H[m] is varying, for example due to frame
154 	   to frame Wo variations, or non-stationary speech.
155 	   Continous model generally results in smooth phase track
156 	   under these circumstances. */
157 	if (j%2){
158 	    H[1].real = 1.0; H[1].imag = 0.0;
159 	    model.phi[1] = 0.0;
160 	}
161 	else {
162 	    H[1].real = 0.0; H[1].imag = 1.0;
163 	    model.phi[1] = PI/2;
164 	}
165 	#endif
166 
167 	//#define CONT
168 	#ifdef CONT
169 	synthesise_continuous_phase(Pn, &model, Sn_, 1, &Wo_prev, phi_prev);
170 	#else
171 	phase_synth_zero_order(5.0, H, &Wo_prev, &ex_phase);
172 	synthesise_mixed(Pn,&model,Sn_,1);
173 	#endif
174 
175 	for(i=0; i<N; i++)
176 	    buf[i] = Sn_[i];
177 	fwrite(buf,sizeof(short),N,fout);
178     }
179 
180     fclose(fout);
181     if (dump) dump_off();
182 
183     return 0;
184 }
185 
186 
187