rate: add some sanity checking
[sox.git] / lpc10 / bsynz.c
blob551538f00aae83cf5b9de5ea21965924c64c660e
1 /*
2 * Revision 1.2 1996/08/20 20:18:55 jaf
3 * Removed all static local variables that were SAVE'd in the Fortran
4 * code, and put them in struct lpc10_decoder_state that is passed as an
5 * argument.
7 * Removed init function, since all initialization is now done in
8 * init_lpc10_decoder_state().
10 * Revision 1.1 1996/08/19 22:32:58 jaf
11 * Initial revision
15 /* -- translated by f2c (version 19951025).
16 You must link the resulting object file with the libraries:
17 -lf2c -lm (in that order)
20 #include "f2c.h"
22 int bsynz_(real *coef, integer *ip, integer *iv, real *sout, real *rms, real *ratio, real *g2pass, struct lpc10_decoder_state *st);
24 /* Common Block Declarations */
26 extern struct {
27 integer order, lframe;
28 logical corrp;
29 } contrl_;
31 #define contrl_1 contrl_
33 /* ***************************************************************** */
35 /* BSYNZ Version 54 */
38 * Revision 1.2 1996/08/20 20:18:55 jaf
39 * Removed all static local variables that were SAVE'd in the Fortran
40 * code, and put them in struct lpc10_decoder_state that is passed as an
41 * argument.
43 * Removed init function, since all initialization is now done in
44 * init_lpc10_decoder_state().
46 * Revision 1.1 1996/08/19 22:32:58 jaf
47 * Initial revision
48 * */
49 /* Revision 1.4 1996/03/27 18:11:22 jaf */
50 /* Changed the range of NOISE printed out in the debugging statements, */
51 /* even though they are commented out. I didn't discover this until I */
52 /* tried comparing two different versions of the LPC-10 coder, each with */
53 /* full tracing enabled. */
55 /* Revision 1.3 1996/03/26 19:33:23 jaf */
56 /* Commented out trace statements. */
58 /* Revision 1.2 1996/03/20 17:12:54 jaf */
59 /* Added comments about which indices of array arguments are read or */
60 /* written. */
62 /* Rearranged local variable declarations to indicate which need to be */
63 /* saved from one invocation to the next. Added entry INITBSYNZ to */
64 /* reinitialize the local state variables, if desired. */
66 /* Revision 1.1 1996/02/07 14:43:15 jaf */
67 /* Initial revision */
70 /* ***************************************************************** */
72 /* Synthesize One Pitch Epoch */
74 /* Input: */
75 /* COEF - Predictor coefficients */
76 /* Indices 1 through ORDER read. */
77 /* IP - Pitch period (number of samples to synthesize) */
78 /* IV - Voicing for the current epoch */
79 /* RMS - Energy for the current epoch */
80 /* RATIO - Energy slope for plosives */
81 /* G2PASS- Sharpening factor for 2 pass synthesis */
82 /* Output: */
83 /* SOUT - Synthesized speech */
84 /* Indices 1 through IP written. */
86 /* This subroutine maintains local state from one call to the next. If */
87 /* you want to switch to using a new audio stream for this filter, or */
88 /* reinitialize its state for any other reason, call the ENTRY */
89 /* INITBSYNZ. */
91 /* Subroutine */ int bsynz_(real *coef, integer *ip, integer *iv,
92 real *sout, real *rms, real *ratio, real *g2pass,
93 struct lpc10_decoder_state *st)
95 /* Initialized data */
97 integer *ipo;
98 real *rmso;
99 static integer kexc[25] = { 8,-16,26,-48,86,-162,294,-502,718,-728,184,
100 672,-610,-672,184,728,718,502,294,162,86,48,26,16,8 };
101 real *exc;
102 real *exc2;
103 real *lpi1;
104 real *lpi2;
105 real *lpi3;
106 real *hpi1;
107 real *hpi2;
108 real *hpi3;
110 /* System generated locals */
111 integer i__1, i__2;
112 real r__1, r__2;
114 /* Builtin functions */
115 double sqrt(doublereal);
117 /* Local variables */
118 real gain, xssq;
119 integer i__, j, k;
120 real noise[166], pulse;
121 integer px;
122 real sscale;
123 extern integer random_(struct lpc10_decoder_state *);
124 real xy, sum, ssq;
125 real lpi0, hpi0;
127 /* LPC Processing control variables: */
129 /* *** Read-only: initialized in setup */
131 /* Files for Speech, Parameter, and Bitstream Input & Output, */
132 /* and message and debug outputs. */
134 /* Here are the only files which use these variables: */
136 /* lpcsim.f setup.f trans.f error.f vqsetup.f */
138 /* Many files which use fdebug are not listed, since it is only used in */
139 /* those other files conditionally, to print trace statements. */
140 /* integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
141 /* LPC order, Frame size, Quantization rate, Bits per frame, */
142 /* Error correction */
143 /* Subroutine SETUP is the only place where order is assigned a value, */
144 /* and that value is 10. It could increase efficiency 1% or so to */
145 /* declare order as a constant (i.e., a Fortran PARAMETER) instead of as
147 /* a variable in a COMMON block, since it is used in many places in the */
148 /* core of the coding and decoding routines. Actually, I take that back.
150 /* At least when compiling with f2c, the upper bound of DO loops is */
151 /* stored in a local variable before the DO loop begins, and then that is
153 /* compared against on each iteration. */
154 /* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
155 /* Similarly for quant, which is given a value of 2400 in SETUP. quant */
156 /* is used in only a few places, and never in the core coding and */
157 /* decoding routines, so it could be eliminated entirely. */
158 /* nbits is similar to quant, and is given a value of 54 in SETUP. */
159 /* corrp is given a value of .TRUE. in SETUP, and is only used in the */
160 /* subroutines ENCODE and DECODE. It doesn't affect the speed of the */
161 /* coder significantly whether it is .TRUE. or .FALSE., or whether it is
163 /* a constant or a variable, since it is only examined once per frame. */
164 /* Leaving it as a variable that is set to .TRUE. seems like a good */
165 /* idea, since it does enable some error-correction capability for */
166 /* unvoiced frames, with no change in the coding rate, and no noticeable
168 /* quality difference in the decoded speech. */
169 /* integer quant, nbits */
170 /* *** Read/write: variables for debugging, not needed for LPC algorithm
173 /* Current frame, Unstable frames, Output clip count, Max onset buffer,
175 /* Debug listing detail level, Line count on listing page */
177 /* nframe is not needed for an embedded LPC10 at all. */
178 /* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
179 /* ERROR, which is only called from RCCHK. When LPC10 is embedded into */
180 /* an application, I would recommend removing the call to ERROR in RCCHK,
182 /* and remove ERROR and nunsfm completely. */
183 /* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in
185 /* sread.f. When LPC10 is embedded into an application, one might want */
186 /* to cause it to be incremented in a routine that takes the output of */
187 /* SYNTHS and sends it to an audio device. It could be optionally */
188 /* displayed, for those that might want to know what it is. */
189 /* maxosp is never initialized to 0 in SETUP, although it probably should
191 /* be, and it is updated in subroutine ANALYS. I doubt that its value */
192 /* would be of much interest to an application in which LPC10 is */
193 /* embedded. */
194 /* listl and lincnt are not needed for an embedded LPC10 at all. */
195 /* integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
196 /* common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
197 /* common /contrl/ quant, nbits */
198 /* common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
199 /* Function return value definitions */
200 /* Parameters/constants */
201 /* KEXC is not a Fortran PARAMETER, but it is an array initialized
203 /* with a DATA statement that is never modified. */
204 /* Local variables that need not be saved */
205 /* NOISE is declared with range (1:MAXPIT+MAXORD), but only indices
207 /* ORDER+1 through ORDER+IP are ever used, and I think that IP */
208 /* .LE. MAXPIT. Why not declare it to be in the range (1:MAXPIT) */
209 /* and use that range? */
210 /* Local state */
211 /* I believe that only indices 1 through ORDER of EXC need to be */
212 /* saved from one invocation to the next, but we may as well save */
213 /* the whole array. */
214 /* None of these local variables were given initial values in the */
215 /* original code. I'm guessing that 0 is a reasonable initial */
216 /* value for all of them. */
217 /* Parameter adjustments */
218 if (coef) {
219 --coef;
221 if (sout) {
222 --sout;
225 /* Function Body */
226 ipo = &(st->ipo);
227 exc = &(st->exc[0]);
228 exc2 = &(st->exc2[0]);
229 lpi1 = &(st->lpi1);
230 lpi2 = &(st->lpi2);
231 lpi3 = &(st->lpi3);
232 hpi1 = &(st->hpi1);
233 hpi2 = &(st->hpi2);
234 hpi3 = &(st->hpi3);
235 rmso = &(st->rmso_bsynz);
237 /* MAXPIT+MAXORD=166 */
238 /* Calculate history scale factor XY and scale filter state */
239 /* Computing MIN */
240 r__1 = *rmso / (*rms + 1e-6f);
241 xy = min(r__1,8.f);
242 *rmso = *rms;
243 i__1 = contrl_1.order;
244 for (i__ = 1; i__ <= i__1; ++i__) {
245 exc2[i__ - 1] = exc2[*ipo + i__ - 1] * xy;
247 *ipo = *ip;
248 if (*iv == 0) {
249 /* Generate white noise for unvoiced */
250 i__1 = *ip;
251 for (i__ = 1; i__ <= i__1; ++i__) {
252 exc[contrl_1.order + i__ - 1] = (real) (random_(st) / 64);
254 /* Impulse doublet excitation for plosives */
255 /* (RANDOM()+32768) is in the range 0 to 2**16-1. Therefore the
257 /* following expression should be evaluated using integers with
258 at */
259 /* least 32 bits (16 isn't enough), and PX should be in the rang
260 e */
261 /* ORDER+1+0 through ORDER+1+(IP-2) .EQ. ORDER+IP-1. */
262 px = (random_(st) + 32768) * (*ip - 1) / 65536 + contrl_1.order + 1;
263 r__1 = *ratio / 4 * 1.f;
264 pulse = r__1 * 342;
265 if (pulse > 2e3f) {
266 pulse = 2e3f;
268 exc[px - 1] += pulse;
269 exc[px] -= pulse;
270 /* Load voiced excitation */
271 } else {
272 sscale = sqrt((real) (*ip)) / 6.928f;
273 i__1 = *ip;
274 for (i__ = 1; i__ <= i__1; ++i__) {
275 exc[contrl_1.order + i__ - 1] = 0.f;
276 if (i__ <= 25) {
277 exc[contrl_1.order + i__ - 1] = sscale * kexc[i__ - 1];
279 lpi0 = exc[contrl_1.order + i__ - 1];
280 r__2 = exc[contrl_1.order + i__ - 1] * .125f + *lpi1 * .75f;
281 r__1 = r__2 + *lpi2 * .125f;
282 exc[contrl_1.order + i__ - 1] = r__1 + *lpi3 * 0.f;
283 *lpi3 = *lpi2;
284 *lpi2 = *lpi1;
285 *lpi1 = lpi0;
287 i__1 = *ip;
288 for (i__ = 1; i__ <= i__1; ++i__) {
289 noise[contrl_1.order + i__ - 1] = random_(st) * 1.f / 64;
290 hpi0 = noise[contrl_1.order + i__ - 1];
291 r__2 = noise[contrl_1.order + i__ - 1] * -.125f + *hpi1 * .25f;
292 r__1 = r__2 + *hpi2 * -.125f;
293 noise[contrl_1.order + i__ - 1] = r__1 + *hpi3 * 0.f;
294 *hpi3 = *hpi2;
295 *hpi2 = *hpi1;
296 *hpi1 = hpi0;
298 i__1 = *ip;
299 for (i__ = 1; i__ <= i__1; ++i__) {
300 exc[contrl_1.order + i__ - 1] += noise[contrl_1.order + i__ - 1];
303 /* Synthesis filters: */
304 /* Modify the excitation with all-zero filter 1 + G*SUM */
305 xssq = 0.f;
306 i__1 = *ip;
307 for (i__ = 1; i__ <= i__1; ++i__) {
308 k = contrl_1.order + i__;
309 sum = 0.f;
310 i__2 = contrl_1.order;
311 for (j = 1; j <= i__2; ++j) {
312 sum += coef[j] * exc[k - j - 1];
314 sum *= *g2pass;
315 exc2[k - 1] = sum + exc[k - 1];
317 /* Synthesize using the all pole filter 1 / (1 - SUM) */
318 i__1 = *ip;
319 for (i__ = 1; i__ <= i__1; ++i__) {
320 k = contrl_1.order + i__;
321 sum = 0.f;
322 i__2 = contrl_1.order;
323 for (j = 1; j <= i__2; ++j) {
324 sum += coef[j] * exc2[k - j - 1];
326 exc2[k - 1] = sum + exc2[k - 1];
327 xssq += exc2[k - 1] * exc2[k - 1];
329 /* Save filter history for next epoch */
330 i__1 = contrl_1.order;
331 for (i__ = 1; i__ <= i__1; ++i__) {
332 exc[i__ - 1] = exc[*ip + i__ - 1];
333 exc2[i__ - 1] = exc2[*ip + i__ - 1];
335 /* Apply gain to match RMS */
336 r__1 = *rms * *rms;
337 ssq = r__1 * *ip;
338 gain = sqrt(ssq / xssq);
339 i__1 = *ip;
340 for (i__ = 1; i__ <= i__1; ++i__) {
341 sout[i__] = gain * exc2[contrl_1.order + i__ - 1];
343 return 0;
344 } /* bsynz_ */