1 /*-------------- Telecommunications & Signal Processing Lab ---------------
2                              McGill University
3 
4 Routine:
5   LPsyn [options] -p PFile -c LPFile AFileI [AFileO]
6 
7 Purpose:
8   LPC synthesis from a residual file
9 
10 Description:
11   This program does linear prediction synthesis given a file of residual
12   samples and a file of linear prediction coefficients.  The output is an
13   audio file containing the reconstructed signal.
14 
15   The steps involved in forming the linear prediction coefficients are as
16   follows.
17    1: Use the all-pole LPC filter to filter the residual signal
18    2: Deemphasize the reconstructed signal
19 
20 Options:
21   Input file name: AFileI:
22       The environment variable AUDIOPATH specifies a list of directories to be
23       searched for the input residual file.
24   Output file name: AFileO:
25       This name specifies the optional output file containing the reconstructed
26       signal.  Specifying "-" as the output file indicates that output is to be
27       written to standard output.
28   -p PFile, --parameter_file=PFile
29       Parameter file.
30   -c LPFile, --lpc_file=LPFile
31       LPC predictor coefficient file.
32   -s, --statistics
33       Print frame-by-frame statistics.
34   -I INFO, --info=INFO
35       Audio file information string for the output file.
36   -h, --help
37       Print a list of options and exit.
38   -v, --version
39       Print the version number and exit.
40 
41   The analysis parameters are read from the parameter file.
42     preemphasis_factor = float  ! preemphasis factor (0 to 1, default 0)
43     window_length = int         ! analysis window length
44     window_offset = int         ! initial offset of the center of the analysis
45                                 ! window from the center of the frame (negative
46                                 ! values mean that the analysis window precedes
47                                 ! frame, default  0)
48     window_type = char          ! window type (Hamming or rectangular, default
49                                 ! Hamming)
50     frame_length = int          ! frame size
51     LPC_number = int            ! number of LPC coefficients
52     LPC_BW_expansion = float    ! bandwidth expansion factor (1 gives no
53                                 ! bandwidth expansion, default 1)
54 
55   By default, the output file contains a standard audio file information
56   string.
57     Standard Audio File Information:
58        date: 1994-01-25 19:19:39 UTC    date
59        program: LPsyn                   program name
60        parameters: <parameters>         list of parameters
61   This information can be changed with the header information string which is
62   specified as one of the command line options.  Structured information records
63   should adhere to the above format with a named field terminated by a colon,
64   followed by numeric data or text.  Comments can follow as unstructured
65   information.
66     Record delimiter: Newline character or the two character escape
67         sequence "\" + "n".
68     Line delimiter: Within records, lines are delimiteded by a carriage
69         control character, the two character escape sequence "\" + "r",
70         or the two character sequence "\" + newline.
71   If the information string starts with a record delimiter, the header
72   information string is appended to the standard header information.  If not,
73   the user supplied header information string appears alone.
74 
75 Environment variables:
76   AUDIOPATH:
77   This environment variable specifies a list of directories to be searched when
78   opening the input audio files.  Directories in the list are separated by
79   colons (semicolons for Windows).
80 
81 Author / version:
82   P. Kabal / v3r0a  2003-11-03  Copyright (C) 2006
83 
84 -------------------------------------------------------------------------*/
85 
86 #include <stdlib.h>
87 #include <string.h>
88 
89 #include <libtsp.h>
90 #include <libtsp/AFpar.h>
91 #include "LPsyn.h"
92 
93 #define MAXHEADER	1024
94 
95 
96 int
main(int argc,const char * argv[])97 main (int argc, const char *argv[])
98 
99 {
100   AFILE *AFpI, *AFpL, *AFpO;
101   FILE *fpout;
102   char Fname[4][FILENAME_MAX];
103   long int Nsamp, Nchan;
104   int Lwin, Woffs, Lframe, Np;
105   const float *Win;
106   double Sfreq, pre, bwexp, Frate;
107   long int Npx, Ntcoef;
108 
109 /* Get the input parameters */
110   LSoptions (argc, argv, Fname);
111 
112 /* If output is to stdout, use stderr for informational messages */
113   if (strcmp (Fname[1], "-") == 0)
114     fpout = stderr;
115   else
116     fpout = stdout;
117 
118 /* Read the analysis parameters */
119   LSlpcPar (Fname[0], &pre, &Win, &Lwin, &Woffs, &Lframe, &Np, &bwexp);
120 
121 /* Open the input residual file */
122   FLpathList (Fname[1], "$AUDIOPATH", Fname[1]);
123   AFpI = AFopnRead (Fname[1], &Nsamp, &Nchan, &Sfreq, fpout);
124   if (Nchan != 1)
125     UThalt ("%s: Multiple input channels not supported", PROGRAM);
126 
127 /* Open the LPC coefficient file */
128   FLpathList (Fname[2], "$AUDIOPATH", Fname[2]);
129   AFpL = AFopnRead (Fname[2], &Ntcoef, &Npx, &Frate, fpout);
130   if (Np != Npx)
131     UThalt ("%s: No. LPC coefficients does not match parameter file", PROGRAM);
132   if (Frate * Lframe != Sfreq)
133     UThalt ("%s: Frame rate, frame length and sample rate are incompatible",
134 	    PROGRAM);
135   if (Ntcoef * Lframe != Np * Nsamp)
136     UThalt ("%s: No. residual samples incompatible with no. frames", PROGRAM);
137   fprintf (fpout, "\n");
138 
139 /* Open the output audio file */
140   if (Fname[3][0] != '\0') {
141     if (strcmp (Fname[3], "-") != 0)
142       FLbackup (Fname[3]);
143     AFpO = AFopnWrite (Fname[3], FTW_AU, FD_INT16, 1L, Sfreq, fpout);
144   }
145   else
146     AFpO = NULL;
147 
148 /* Process the residual file */
149   LSlpcSyn (AFpI, AFpL, AFpO, pre, Lframe, Np);
150 
151 /* Close the files */
152   AFclose (AFpI);
153   AFclose (AFpL);
154   if (AFpO != NULL)
155     AFclose (AFpO);
156 
157   return EXIT_SUCCESS;
158 }
159