~ubuntu-branches/ubuntu/quantal/linphone/quantal

« back to all changes in this revision

Viewing changes to lpc10-1.5/dyptrk.c

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2004-06-30 13:58:16 UTC
  • Revision ID: james.westby@ubuntu.com-20040630135816-wwx75gdlodkqbabb
Tags: upstream-0.12.2
ImportĀ upstreamĀ versionĀ 0.12.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
$Log: dyptrk.c,v $
 
4
Revision 1.1.1.1  2001/11/19 19:50:15  smorlat
 
5
First cvs.
 
6
 
 
7
Revision 1.1.1.1  2001/08/08 21:29:08  simon
 
8
First import
 
9
 
 
10
 * Revision 1.2  1996/08/20  20:25:29  jaf
 
11
 * Removed all static local variables that were SAVE'd in the Fortran
 
12
 * code, and put them in struct lpc10_encoder_state that is passed as an
 
13
 * argument.
 
14
 *
 
15
 * Removed init function, since all initialization is now done in
 
16
 * init_lpc10_encoder_state().
 
17
 *
 
18
 * Revision 1.1  1996/08/19  22:32:26  jaf
 
19
 * Initial revision
 
20
 *
 
21
 
 
22
*/
 
23
 
 
24
#ifdef P_R_O_T_O_T_Y_P_E_S
 
25
extern int dyptrk_(real *amdf, integer *ltau, integer *minptr, integer *voice, integer *pitch, integer *midx, struct lpc10_encoder_state *st);
 
26
/* comlen contrl_ 12 */
 
27
#endif
 
28
 
 
29
/*  -- translated by f2c (version 19951025).
 
30
   You must link the resulting object file with the libraries:
 
31
        -lf2c -lm   (in that order)
 
32
*/
 
33
 
 
34
#include "f2c.h"
 
35
 
 
36
/* Common Block Declarations */
 
37
 
 
38
extern struct {
 
39
    integer order, lframe;
 
40
    logical corrp;
 
41
} contrl_;
 
42
 
 
43
#define contrl_1 contrl_
 
44
 
 
45
/* ********************************************************************* */
 
46
 
 
47
/*      DYPTRK Version 52 */
 
48
 
 
49
/* $Log: dyptrk.c,v $
 
50
/* Revision 1.1.1.1  2001/11/19 19:50:15  smorlat
 
51
/* First cvs.
 
52
/*
 
53
/* Revision 1.1.1.1  2001/08/08 21:29:08  simon
 
54
/* First import
 
55
/*
 
56
 * Revision 1.2  1996/08/20  20:25:29  jaf
 
57
 * Removed all static local variables that were SAVE'd in the Fortran
 
58
 * code, and put them in struct lpc10_encoder_state that is passed as an
 
59
 * argument.
 
60
 *
 
61
 * Removed init function, since all initialization is now done in
 
62
 * init_lpc10_encoder_state().
 
63
 *
 
64
 * Revision 1.1  1996/08/19  22:32:26  jaf
 
65
 * Initial revision
 
66
 * */
 
67
/* Revision 1.5  1996/03/26  19:35:35  jaf */
 
68
/* Commented out trace statements. */
 
69
 
 
70
/* Revision 1.4  1996/03/19  18:03:22  jaf */
 
71
/* Replaced the initialization "DATA P/60*DEPTH*0/" with "DATA P/120*0/", */
 
72
/* because apparently Fortran (or at least f2c) can't handle expressions */
 
73
/* like that. */
 
74
 
 
75
/* Revision 1.3  1996/03/19  17:38:32  jaf */
 
76
/* Added comments about the local variables that should be saved from one */
 
77
/* invocation to the next.  None of them were given initial values in the */
 
78
/* original code, but from my testing, it appears that initializing them */
 
79
/* all to 0 works. */
 
80
 
 
81
/* Added entry INITDYPTRK to reinitialize these local variables. */
 
82
 
 
83
/* Revision 1.2  1996/03/13  16:32:17  jaf */
 
84
/* Comments added explaining which of the local variables of this */
 
85
/* subroutine need to be saved from one invocation to the next, and which */
 
86
/* do not. */
 
87
 
 
88
/* WARNING!  Some of them that should are never given initial values in */
 
89
/* this code.  Hopefully, Fortran 77 defines initial values for them, but */
 
90
/* even so, giving them explicit initial values is preferable. */
 
91
 
 
92
/* Revision 1.1  1996/02/07 14:45:14  jaf */
 
93
/* Initial revision */
 
94
 
 
95
 
 
96
/* ********************************************************************* */
 
97
 
 
98
/*   Dynamic Pitch Tracker */
 
99
 
 
100
/* Input: */
 
101
/*  AMDF   - Average Magnitude Difference Function array */
 
102
/*           Indices 1 through LTAU read, and MINPTR */
 
103
/*  LTAU   - Number of lags in AMDF */
 
104
/*  MINPTR - Location of minimum AMDF value */
 
105
/*  VOICE  - Voicing decision */
 
106
/* Output: */
 
107
/*  PITCH  - Smoothed pitch value, 2 frames delayed */
 
108
/*  MIDX   - Initial estimate of current frame pitch */
 
109
/* Compile time constant: */
 
110
/*  DEPTH  - Number of frames to trace back */
 
111
 
 
112
/* This subroutine maintains local state from one call to the next.  If */
 
113
/* you want to switch to using a new audio stream for this filter, or */
 
114
/* reinitialize its state for any other reason, call the ENTRY */
 
115
/* INITDYPTRK. */
 
116
 
 
117
/* Subroutine */ int dyptrk_(real *amdf, integer *ltau, integer *
 
118
        minptr, integer *voice, integer *pitch, integer *midx,
 
119
                               struct lpc10_encoder_state *st)
 
120
{
 
121
    /* Initialized data */
 
122
 
 
123
    real *s;
 
124
    integer *p;
 
125
    integer *ipoint;
 
126
    real *alphax;
 
127
 
 
128
    /* System generated locals */
 
129
    integer i__1;
 
130
 
 
131
    /* Local variables */
 
132
    integer pbar;
 
133
    real sbar;
 
134
    integer path[2], iptr, i__, j;
 
135
    real alpha, minsc, maxsc;
 
136
 
 
137
/*       Arguments */
 
138
/* $Log: dyptrk.c,v $
 
139
/* Revision 1.1.1.1  2001/11/19 19:50:15  smorlat
 
140
/* First cvs.
 
141
/*
 
142
/* Revision 1.1.1.1  2001/08/08 21:29:08  simon
 
143
/* First import
 
144
/*
 
145
 * Revision 1.2  1996/08/20  20:25:29  jaf
 
146
 * Removed all static local variables that were SAVE'd in the Fortran
 
147
 * code, and put them in struct lpc10_encoder_state that is passed as an
 
148
 * argument.
 
149
 *
 
150
 * Removed init function, since all initialization is now done in
 
151
 * init_lpc10_encoder_state().
 
152
 *
 
153
 * Revision 1.1  1996/08/19  22:32:26  jaf
 
154
 * Initial revision
 
155
 * */
 
156
/* Revision 1.3  1996/03/29  22:05:55  jaf */
 
157
/* Commented out the common block variables that are not needed by the */
 
158
/* embedded version. */
 
159
 
 
160
/* Revision 1.2  1996/03/26  19:34:50  jaf */
 
161
/* Added comments indicating which constants are not needed in an */
 
162
/* application that uses the LPC-10 coder. */
 
163
 
 
164
/* Revision 1.1  1996/02/07  14:44:09  jaf */
 
165
/* Initial revision */
 
166
 
 
167
/*   LPC Processing control variables: */
 
168
 
 
169
/* *** Read-only: initialized in setup */
 
170
 
 
171
/*  Files for Speech, Parameter, and Bitstream Input & Output, */
 
172
/*    and message and debug outputs. */
 
173
 
 
174
/* Here are the only files which use these variables: */
 
175
 
 
176
/* lpcsim.f setup.f trans.f error.f vqsetup.f */
 
177
 
 
178
/* Many files which use fdebug are not listed, since it is only used in */
 
179
/* those other files conditionally, to print trace statements. */
 
180
/*      integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
 
181
/*  LPC order, Frame size, Quantization rate, Bits per frame, */
 
182
/*    Error correction */
 
183
/* Subroutine SETUP is the only place where order is assigned a value, */
 
184
/* and that value is 10.  It could increase efficiency 1% or so to */
 
185
/* declare order as a constant (i.e., a Fortran PARAMETER) instead of as 
 
186
*/
 
187
/* a variable in a COMMON block, since it is used in many places in the */
 
188
/* core of the coding and decoding routines.  Actually, I take that back. 
 
189
*/
 
190
/* At least when compiling with f2c, the upper bound of DO loops is */
 
191
/* stored in a local variable before the DO loop begins, and then that is 
 
192
*/
 
193
/* compared against on each iteration. */
 
194
/* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
 
195
/* Similarly for quant, which is given a value of 2400 in SETUP.  quant */
 
196
/* is used in only a few places, and never in the core coding and */
 
197
/* decoding routines, so it could be eliminated entirely. */
 
198
/* nbits is similar to quant, and is given a value of 54 in SETUP. */
 
199
/* corrp is given a value of .TRUE. in SETUP, and is only used in the */
 
200
/* subroutines ENCODE and DECODE.  It doesn't affect the speed of the */
 
201
/* coder significantly whether it is .TRUE. or .FALSE., or whether it is 
 
202
*/
 
203
/* a constant or a variable, since it is only examined once per frame. */
 
204
/* Leaving it as a variable that is set to .TRUE.  seems like a good */
 
205
/* idea, since it does enable some error-correction capability for */
 
206
/* unvoiced frames, with no change in the coding rate, and no noticeable 
 
207
*/
 
208
/* quality difference in the decoded speech. */
 
209
/*      integer quant, nbits */
 
210
/* *** Read/write: variables for debugging, not needed for LPC algorithm 
 
211
*/
 
212
 
 
213
/*  Current frame, Unstable frames, Output clip count, Max onset buffer, 
 
214
*/
 
215
/*    Debug listing detail level, Line count on listing page */
 
216
 
 
217
/* nframe is not needed for an embedded LPC10 at all. */
 
218
/* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
 
219
/* ERROR, which is only called from RCCHK.  When LPC10 is embedded into */
 
220
/* an application, I would recommend removing the call to ERROR in RCCHK, 
 
221
*/
 
222
/* and remove ERROR and nunsfm completely. */
 
223
/* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in 
 
224
*/
 
225
/* sread.f.  When LPC10 is embedded into an application, one might want */
 
226
/* to cause it to be incremented in a routine that takes the output of */
 
227
/* SYNTHS and sends it to an audio device.  It could be optionally */
 
228
/* displayed, for those that might want to know what it is. */
 
229
/* maxosp is never initialized to 0 in SETUP, although it probably should 
 
230
*/
 
231
/* be, and it is updated in subroutine ANALYS.  I doubt that its value */
 
232
/* would be of much interest to an application in which LPC10 is */
 
233
/* embedded. */
 
234
/* listl and lincnt are not needed for an embedded LPC10 at all. */
 
235
/*      integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
 
236
/*      common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
 
237
/*      common /contrl/ quant, nbits */
 
238
/*      common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
 
239
/*      Parameters/constants */
 
240
/*       Local variables that need not be saved */
 
241
/*       Note that PATH is only used for debugging purposes, and can be */
 
242
/*       removed. */
 
243
/*       Local state */
 
244
/*       It would be a bit more "general" to define S(LTAU), if Fortran */
 
245
/*       allows the argument of a function to be used as the dimension of 
 
246
*/
 
247
/*       a local array variable. */
 
248
/*       IPOINT is always in the range 0 to DEPTH-1. */
 
249
/*       WARNING! */
 
250
 
 
251
/*       In the original version of this subroutine, IPOINT, ALPHAX, */
 
252
/*       every element of S, and potentially any element of P with the */
 
253
/*       second index value .NE. IPTR were read without being given */
 
254
/*       initial values (all indices of P with second index equal to */
 
255
/*       IPTR are all written before being read in this subroutine). */
 
256
 
 
257
/*       From examining the code carefully, it appears that all of these 
 
258
*/
 
259
/*       should be saved from one invocation to the next. */
 
260
 
 
261
/*       I've run lpcsim with the "-l 6" option to see all of the */
 
262
/*       debugging information that is printed out by this subroutine */
 
263
/*       below, and it appears that S, P, IPOINT, and ALPHAX are all */
 
264
/*       initialized to 0 (these initial values would likely be different 
 
265
*/
 
266
/*       on different platforms, compilers, etc.).  Given that the output 
 
267
*/
 
268
/*       of the coder sounds reasonable, I'm going to initialize these */
 
269
/*       variables to 0 explicitly. */
 
270
 
 
271
    s = &(st->s[0]);
 
272
    p = &(st->p[0]);
 
273
    ipoint = &(st->ipoint);
 
274
    alphax = &(st->alphax);
 
275
 
 
276
 
 
277
    /* Parameter adjustments */
 
278
    if (amdf) {
 
279
        --amdf;
 
280
        }
 
281
 
 
282
    /* Function Body */
 
283
 
 
284
/*   Calculate the confidence factor ALPHA, used as a threshold slope in 
 
285
*/
 
286
/*   SEESAW.  If unvoiced, set high slope so that every point in P array 
 
287
*/
 
288
/*  is marked as a potential pitch frequency.  A scaled up version (ALPHAX
 
289
)*/
 
290
/*   is used to maintain arithmetic precision. */
 
291
    if (*voice == 1) {
 
292
        *alphax = *alphax * .75f + amdf[*minptr] / 2.f;
 
293
    } else {
 
294
        *alphax *= .984375f;
 
295
    }
 
296
    alpha = *alphax / 16;
 
297
    if (*voice == 0 && *alphax < 128.f) {
 
298
        alpha = 8.f;
 
299
    }
 
300
/* SEESAW: Construct a pitch pointer array and intermediate winner functio
 
301
n*/
 
302
/*   Left to right pass: */
 
303
    iptr = *ipoint + 1;
 
304
    p[iptr * 60 - 60] = 1;
 
305
    i__ = 1;
 
306
    pbar = 1;
 
307
    sbar = s[0];
 
308
    i__1 = *ltau;
 
309
    for (i__ = 1; i__ <= i__1; ++i__) {
 
310
        sbar += alpha;
 
311
        if (sbar < s[i__ - 1]) {
 
312
            s[i__ - 1] = sbar;
 
313
            p[i__ + iptr * 60 - 61] = pbar;
 
314
        } else {
 
315
            sbar = s[i__ - 1];
 
316
            p[i__ + iptr * 60 - 61] = i__;
 
317
            pbar = i__;
 
318
        }
 
319
    }
 
320
/*   Right to left pass: */
 
321
    i__ = pbar - 1;
 
322
    sbar = s[i__];
 
323
    while(i__ >= 1) {
 
324
        sbar += alpha;
 
325
        if (sbar < s[i__ - 1]) {
 
326
            s[i__ - 1] = sbar;
 
327
            p[i__ + iptr * 60 - 61] = pbar;
 
328
        } else {
 
329
            pbar = p[i__ + iptr * 60 - 61];
 
330
            i__ = pbar;
 
331
            sbar = s[i__ - 1];
 
332
        }
 
333
        --i__;
 
334
    }
 
335
/*   Update S using AMDF */
 
336
/*   Find maximum, minimum, and location of minimum */
 
337
    s[0] += amdf[1] / 2;
 
338
    minsc = s[0];
 
339
    maxsc = minsc;
 
340
    *midx = 1;
 
341
    i__1 = *ltau;
 
342
    for (i__ = 2; i__ <= i__1; ++i__) {
 
343
        s[i__ - 1] += amdf[i__] / 2;
 
344
        if (s[i__ - 1] > maxsc) {
 
345
            maxsc = s[i__ - 1];
 
346
        }
 
347
        if (s[i__ - 1] < minsc) {
 
348
            *midx = i__;
 
349
            minsc = s[i__ - 1];
 
350
        }
 
351
    }
 
352
/*   Subtract MINSC from S to prevent overflow */
 
353
    i__1 = *ltau;
 
354
    for (i__ = 1; i__ <= i__1; ++i__) {
 
355
        s[i__ - 1] -= minsc;
 
356
    }
 
357
    maxsc -= minsc;
 
358
/*   Use higher octave pitch if significant null there */
 
359
    j = 0;
 
360
    for (i__ = 20; i__ <= 40; i__ += 10) {
 
361
        if (*midx > i__) {
 
362
            if (s[*midx - i__ - 1] < maxsc / 4) {
 
363
                j = i__;
 
364
            }
 
365
        }
 
366
    }
 
367
    *midx -= j;
 
368
/*   TRACE: look back two frames to find minimum cost pitch estimate */
 
369
    j = *ipoint;
 
370
    *pitch = *midx;
 
371
    for (i__ = 1; i__ <= 2; ++i__) {
 
372
        j = j % 2 + 1;
 
373
        *pitch = p[*pitch + j * 60 - 61];
 
374
        path[i__ - 1] = *pitch;
 
375
    }
 
376
 
 
377
/*       The following statement subtracts one from IPOINT, mod DEPTH.  I 
 
378
*/
 
379
/*       think the author chose to add DEPTH-1, instead of subtracting 1, 
 
380
*/
 
381
/*       because then it will work even if MOD doesn't work as desired on 
 
382
*/
 
383
/*       negative arguments. */
 
384
 
 
385
    *ipoint = (*ipoint + 1) % 2;
 
386
    return 0;
 
387
} /* dyptrk_ */