1 /*
2  * Copyright (C) 2008 by Jonathan Duddington
3  * email: jonsd@users.sourceforge.net
4  * Copyright (C) 2015-2017 Reece H. Dunn
5  *
6  * Based on a re-implementation by:
7  * (c) 1993,94 Jon Iles and Nick Ing-Simmons
8  * of the Klatt cascade-parallel formant synthesizer
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see: <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "synthesize.h"
25 
26 #ifdef __cplusplus
27 extern "C"
28 {
29 #endif
30 
31 #define CASCADE_PARALLEL 1         /* Type of synthesis model */
32 #define ALL_PARALLEL     2
33 
34 #define IMPULSIVE        1         /* Type of voicing source */
35 #define NATURAL          2
36 #define SAMPLED          3
37 #define SAMPLED2         4
38 
39 /* typedef's that need to be exported */
40 
41 typedef long flag;
42 
43 /* Resonator Structure */
44 
45 typedef struct {
46 	double a;
47 	double b;
48 	double c;
49 	double p1;
50 	double p2;
51 	double a_inc;
52 	double b_inc;
53 	double c_inc;
54 } resonator_t, *resonator_ptr;
55 
56 /* Structure for Klatt Globals */
57 
58 typedef struct {
59 	flag synthesis_model; /* cascade-parallel or all-parallel */
60 	flag outsl;     /* Output waveform selector                      */
61 	long samrate;   /* Number of output samples per second           */
62 	long FLPhz;     /* Frequeny of glottal downsample low-pass filter */
63 	long BLPhz;     /* Bandwidth of glottal downsample low-pass filter */
64 	flag glsource;  /* Type of glottal source */
65 	int f0_flutter; /* Percentage of f0 flutter 0-100 */
66 	long nspfr;     /* number of samples per frame */
67 	long nper;      /* Counter for number of samples in a pitch period */
68 	long ns;
69 	long T0;        /* Fundamental period in output samples times 4 */
70 	long nopen;     /* Number of samples in open phase of period    */
71 	long nmod;      /* Position in period to begin noise amp. modul */
72 	long nrand;     /* Varible used by random number generator      */
73 	double pulse_shape_a; /* Makes waveshape of glottal pulse when open   */
74 	double pulse_shape_b; /* Makes waveshape of glottal pulse when open   */
75 	double minus_pi_t;
76 	double two_pi_t;
77 	double onemd;
78 	double decay;
79 	double amp_bypas; /* AB converted to linear gain              */
80 	double amp_voice; /* AVdb converted to linear gain            */
81 	double par_amp_voice; /* AVpdb converted to linear gain       */
82 	double amp_aspir; /* AP converted to linear gain              */
83 	double amp_frica; /* AF converted to linear gain              */
84 	double amp_breth; /* ATURB converted to linear gain           */
85 	double amp_gain0; /* G0 converted to linear gain              */
86 	int num_samples; /* number of glottal samples */
87 	double sample_factor; /* multiplication factor for glottal samples */
88 	short *natural_samples; /* pointer to an array of glottal samples */
89 	long original_f0; /* original value of f0 not modified by flutter */
90 
91 	int fadeout;       // set to 64 to cause fadeout over 64 samples
92 	int scale_wav;     // depends on the voicing source
93 
94 #define N_RSN 20
95 #define Rnz  0   // nasal zero, anti-resonator
96 #define R1c  1
97 #define R2c  2
98 #define R3c  3
99 #define R4c  4
100 #define R5c  5
101 #define R6c  6
102 #define R7c  7
103 #define R8c  8
104 #define Rnpc 9   // nasal pole
105 
106 #define Rparallel 10
107 #define Rnpp 10
108 #define R1p  11
109 #define R2p  12
110 #define R3p  13
111 #define R4p  14
112 #define R5p  15
113 #define R6p  16
114 
115 #define RGL  17
116 #define RLP  18
117 #define Rout 19
118 
119 	resonator_t rsn[N_RSN];  // internal storage for resonators
120 	resonator_t rsn_next[N_RSN];
121 
122 } klatt_global_t, *klatt_global_ptr;
123 
124 /* Structure for Klatt Parameters */
125 
126 #define F_NZ   0  // nasal zero formant
127 #define F1     1
128 #define F2     2
129 #define F3     3
130 #define F4     4
131 #define F5     5
132 #define F6     6
133 #define F_NP   9  // nasal pole formant
134 
135 
136 typedef struct {
137 	int F0hz10; /* Voicing fund freq in Hz                          */
138 	int AVdb;   /* Amp of voicing in dB,            0 to   70       */
139 	int Fhz[10];  // formant Hz, F_NZ to F6 to F_NP
140 	int Bhz[10];
141 	int Ap[10];   /* Amp of parallel formants in dB,    0 to   80       */
142 	int Bphz[10]; /* Parallel formants bw in Hz,       40 to 1000      */
143 
144 	int ASP;    /* Amp of aspiration in dB,         0 to   70       */
145 	int Kopen;  /* # of samples in open period,     10 to   65      */
146 	int Aturb;  /* Breathiness in voicing,          0 to   80       */
147 	int TLTdb;  /* Voicing spectral tilt in dB,     0 to   24       */
148 	int AF;     /* Amp of frication in dB,          0 to   80       */
149 	int Kskew;  /* Skewness of alternate periods,   0 to   40 in sample#/2  */
150 
151 	int AB;     /* Amp of bypass fric. in dB,       0 to   80       */
152 	int AVpdb;  /* Amp of voicing,  par in dB,      0 to   70       */
153 	int Gain0;  /* Overall gain, 60 dB is unity,    0 to   60       */
154 
155 	int AVdb_tmp;      // copy of AVdb, which is changed within parwave()
156 	int Fhz_next[10];    // Fhz for the next chunk, so we can do interpolation of resonator (a,b,c) parameters
157 	int Bhz_next[10];
158 } klatt_frame_t, *klatt_frame_ptr;
159 
160 
161 typedef struct {
162 	int freq;     // Hz
163 	int bw;   // klatt bandwidth
164 	int ap;   // parallel amplitude
165 	int bp;   // parallel bandwidth
166 	DOUBLEX freq1; // floating point versions of the above
167 	DOUBLEX bw1;
168 	DOUBLEX ap1;
169 	DOUBLEX bp1;
170 	DOUBLEX freq_inc;    // increment by this every 64 samples
171 	DOUBLEX bw_inc;
172 	DOUBLEX ap_inc;
173 	DOUBLEX bp_inc;
174 }  klatt_peaks_t;
175 
176 
177 #ifdef __cplusplus
178 }
179 #endif
180