wav: prettify wav_read_fmt() function
[sox.git] / lpc10 / encode.c
blobb8d6cbae67f0c7e8220b28264ece9875e9473aeb
1 /*
3 * Revision 1.1 1996/08/19 22:32:21 jaf
4 * Initial revision
7 */
9 /* -- translated by f2c (version 19951025).
10 You must link the resulting object file with the libraries:
11 -lf2c -lm (in that order)
14 #include "f2c.h"
16 extern int encode_(integer *voice, integer *pitch, real *rms, real *rc, integer *ipitch, integer *irms, integer *irc);
18 /* Common Block Declarations */
20 extern struct {
21 integer order, lframe;
22 logical corrp;
23 } contrl_;
25 #define contrl_1 contrl_
27 /* Table of constant values */
29 static integer c__2 = 2;
31 /* ***************************************************************** */
33 /* ENCODE Version 54 */
36 * Revision 1.1 1996/08/19 22:32:21 jaf
37 * Initial revision
38 * */
39 /* Revision 1.5 1996/03/26 19:35:50 jaf */
40 /* Commented out trace statements. */
42 /* Revision 1.4 1996/03/21 00:26:29 jaf */
43 /* Added the comment that this subroutine has no local state. */
45 /* In the last check-in, I forgot to mention that I had added comments */
46 /* explaining which indices of array arguments are read or written. */
48 /* Revision 1.3 1996/03/21 00:22:39 jaf */
49 /* Added comments explaining that all local arrays are effectively */
50 /* constants. */
52 /* Revision 1.2 1996/03/13 18:48:33 jaf */
53 /* Comments added explaining that none of the local variables of this */
54 /* subroutine need to be saved from one invocation to the next. */
56 /* Revision 1.1 1996/02/07 14:45:29 jaf */
57 /* Initial revision */
60 /* ***************************************************************** */
62 /* Quantize LPC parameters for transmission */
64 /* INPUTS: */
65 /* VOICE - Half frame voicing decisions */
66 /* Indices 1 through 2 read. */
67 /* PITCH - Pitch */
68 /* RMS - Energy */
69 /* RC - Reflection coefficients */
70 /* Indices 1 through ORDER read. */
71 /* CORRP - Error Correction: TRUE = yes, FALSE = none */
72 /* (this is defined in file control.fh) */
73 /* OUTPUTS: */
74 /* IPITCH - Coded pitch and voicing */
75 /* IRMS - Quantized energy */
76 /* IRC - Quantized reflection coefficients */
77 /* Indices 1 through MAX(ORDER,2) written. */
78 /* If CORRP is .TRUE., then indices 1 through 10 written */
79 /* for unvoiced frames. */
81 /* This subroutine has no local state. */
83 /* Subroutine */ int encode_(integer *voice, integer *pitch, real *rms, real *
84 rc, integer *ipitch, integer *irms, integer *irc)
86 /* Initialized data */
88 static integer enctab[16] = { 0,7,11,12,13,10,6,1,14,9,5,2,3,4,8,15 };
89 static integer entau[60] = { 19,11,27,25,29,21,23,22,30,14,15,7,39,38,46,
90 42,43,41,45,37,53,49,51,50,54,52,60,56,58,26,90,88,92,84,86,82,83,
91 81,85,69,77,73,75,74,78,70,71,67,99,97,113,112,114,98,106,104,108,
92 100,101,76 };
93 static integer enadd[8] = { 1920,-768,2432,1280,3584,1536,2816,-1152 };
94 static real enscl[8] = { .0204f,.0167f,.0145f,.0147f,.0143f,.0135f,.0125f,
95 .0112f };
96 static integer enbits[8] = { 6,5,4,4,4,4,3,3 };
97 static integer entab6[64] = { 0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,
98 3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,9,9,
99 9,10,10,11,11,12,13,14,15 };
100 static integer rmst[64] = { 1024,936,856,784,718,656,600,550,502,460,420,
101 384,352,328,294,270,246,226,206,188,172,158,144,132,120,110,102,
102 92,84,78,70,64,60,54,50,46,42,38,34,32,30,26,24,22,20,18,17,16,15,
103 14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 };
105 /* System generated locals */
106 integer i__1, i__2;
108 /* Builtin functions */
109 integer pow_ii(integer *, integer *);
111 /* Local variables */
112 integer idel, nbit, i__, j, i2, i3, mrk;
115 /* LPC Configuration parameters: */
116 /* Frame size, Prediction order, Pitch period */
117 /* Arguments */
119 /* LPC Processing control variables: */
121 /* *** Read-only: initialized in setup */
123 /* Files for Speech, Parameter, and Bitstream Input & Output, */
124 /* and message and debug outputs. */
126 /* Here are the only files which use these variables: */
128 /* lpcsim.f setup.f trans.f error.f vqsetup.f */
130 /* Many files which use fdebug are not listed, since it is only used in */
131 /* those other files conditionally, to print trace statements. */
132 /* integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
133 /* LPC order, Frame size, Quantization rate, Bits per frame, */
134 /* Error correction */
135 /* Subroutine SETUP is the only place where order is assigned a value, */
136 /* and that value is 10. It could increase efficiency 1% or so to */
137 /* declare order as a constant (i.e., a Fortran PARAMETER) instead of as
139 /* a variable in a COMMON block, since it is used in many places in the */
140 /* core of the coding and decoding routines. Actually, I take that back.
142 /* At least when compiling with f2c, the upper bound of DO loops is */
143 /* stored in a local variable before the DO loop begins, and then that is
145 /* compared against on each iteration. */
146 /* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
147 /* Similarly for quant, which is given a value of 2400 in SETUP. quant */
148 /* is used in only a few places, and never in the core coding and */
149 /* decoding routines, so it could be eliminated entirely. */
150 /* nbits is similar to quant, and is given a value of 54 in SETUP. */
151 /* corrp is given a value of .TRUE. in SETUP, and is only used in the */
152 /* subroutines ENCODE and DECODE. It doesn't affect the speed of the */
153 /* coder significantly whether it is .TRUE. or .FALSE., or whether it is
155 /* a constant or a variable, since it is only examined once per frame. */
156 /* Leaving it as a variable that is set to .TRUE. seems like a good */
157 /* idea, since it does enable some error-correction capability for */
158 /* unvoiced frames, with no change in the coding rate, and no noticeable
160 /* quality difference in the decoded speech. */
161 /* integer quant, nbits */
162 /* *** Read/write: variables for debugging, not needed for LPC algorithm
165 /* Current frame, Unstable frames, Output clip count, Max onset buffer,
167 /* Debug listing detail level, Line count on listing page */
169 /* nframe is not needed for an embedded LPC10 at all. */
170 /* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
171 /* ERROR, which is only called from RCCHK. When LPC10 is embedded into */
172 /* an application, I would recommend removing the call to ERROR in RCCHK,
174 /* and remove ERROR and nunsfm completely. */
175 /* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in
177 /* sread.f. When LPC10 is embedded into an application, one might want */
178 /* to cause it to be incremented in a routine that takes the output of */
179 /* SYNTHS and sends it to an audio device. It could be optionally */
180 /* displayed, for those that might want to know what it is. */
181 /* maxosp is never initialized to 0 in SETUP, although it probably should
183 /* be, and it is updated in subroutine ANALYS. I doubt that its value */
184 /* would be of much interest to an application in which LPC10 is */
185 /* embedded. */
186 /* listl and lincnt are not needed for an embedded LPC10 at all. */
187 /* integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
188 /* common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
189 /* common /contrl/ quant, nbits */
190 /* common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
191 /* Parameters/constants */
192 /* These arrays are not Fortran PARAMETER's, but they are defined */
193 /* by DATA statements below, and their contents are never altered.
195 /* Local variables that need not be saved */
196 /* Parameter adjustments */
197 --irc;
198 --rc;
199 --voice;
201 /* Function Body */
202 /* Scale RMS and RC's to integers */
203 *irms = *rms;
204 i__1 = contrl_1.order;
205 for (i__ = 1; i__ <= i__1; ++i__) {
206 irc[i__] = rc[i__] * 32768.f;
208 /* IF(LISTL.GE.3)WRITE(FDEBUG,800)VOICE,PITCH,IRMS,(IRC(I),I=1,ORDER) */
209 /* 800 FORMAT(1X,/,' <<ENCODE IN>>',T32,2I3,I6,I5,T50,10I8) */
210 /* Encode pitch and voicing */
211 if (voice[1] != 0 && voice[2] != 0) {
212 *ipitch = entau[*pitch - 1];
213 } else {
214 if (contrl_1.corrp) {
215 *ipitch = 0;
216 if (voice[1] != voice[2]) {
217 *ipitch = 127;
219 } else {
220 *ipitch = (voice[1] << 1) + voice[2];
223 /* Encode RMS by binary table search */
224 j = 32;
225 idel = 16;
226 *irms = min(*irms,1023);
227 while(idel > 0) {
228 if (*irms > rmst[j - 1]) {
229 j -= idel;
231 if (*irms < rmst[j - 1]) {
232 j += idel;
234 idel /= 2;
236 if (*irms > rmst[j - 1]) {
237 --j;
239 *irms = 31 - j / 2;
240 /* Encode RC(1) and (2) as log-area-ratios */
241 for (i__ = 1; i__ <= 2; ++i__) {
242 i2 = irc[i__];
243 mrk = 0;
244 if (i2 < 0) {
245 i2 = -i2;
246 mrk = 1;
248 i2 /= 512;
249 i2 = min(i2,63);
250 i2 = entab6[i2];
251 if (mrk != 0) {
252 i2 = -i2;
254 irc[i__] = i2;
256 /* Encode RC(3) - (10) linearly, remove bias then scale */
257 i__1 = contrl_1.order;
258 for (i__ = 3; i__ <= i__1; ++i__) {
259 i2 = irc[i__] / 2;
260 i2 = (i2 + enadd[contrl_1.order + 1 - i__ - 1]) * enscl[
261 contrl_1.order + 1 - i__ - 1];
262 /* Computing MIN */
263 i__2 = max(i2,-127);
264 i2 = min(i__2,127);
265 nbit = enbits[contrl_1.order + 1 - i__ - 1];
266 i3 = 0;
267 if (i2 < 0) {
268 i3 = -1;
270 i2 /= pow_ii(&c__2, &nbit);
271 if (i3 == -1) {
272 --i2;
274 irc[i__] = i2;
276 /* Protect the most significant bits of the most */
277 /* important parameters during non-voiced frames. */
278 /* RC(1) - RC(4) are protected using 20 parity bits */
279 /* replacing RC(5) - RC(10). */
280 if (contrl_1.corrp) {
281 if (*ipitch == 0 || *ipitch == 127) {
282 irc[5] = enctab[(irc[1] & 30) / 2];
283 irc[6] = enctab[(irc[2] & 30) / 2];
284 irc[7] = enctab[(irc[3] & 30) / 2];
285 irc[8] = enctab[(*irms & 30) / 2];
286 irc[9] = enctab[(irc[4] & 30) / 2] / 2;
287 irc[10] = enctab[(irc[4] & 30) / 2] & 1;
290 /* IF(LISTL.GE.3)WRITE(FDEBUG,801)VOICE,IPITCH,IRMS,(IRC(J),J=1,ORDER) */
291 /* 801 FORMAT(1X,'<<ENCODE OUT>>',T32,2I3,I6,I5,T50,10I8) */
292 return 0;
293 } /* encode_ */