1 /*
2     lpc_import.c
3 
4     Copyright (C) 1995 John ffitch
5 
6     This file is part of Csound.
7 
8     The Csound Library is free software; you can redistribute it
9     and/or modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation; either
11     version 2.1 of the License, or (at your option) any later version.
12 
13     Csound is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Lesser General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public
19     License along with Csound; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21     02110-1301 USA
22 */
23 /* ***************************************************************** */
24 /* ******** Program to import lpanal files in tabular format. ****** */
25 /* ***************************************************************** */
26 
27 /* ***************************************************************** */
28 /* John ffitch 1998 Nov 15                                           */
29 /* ***************************************************************** */
30 
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include "std_util.h"
35 #include "lpc.h"
36 
lpc_import_usage(CSOUND * csound)37 void lpc_import_usage(CSOUND *csound)
38 {
39     csound->Message(csound, "%s", Str("Usage: lpc_import cstext_file lpc_file\n"));
40 }
41 
lpc_import(CSOUND * csound,int32_t argc,char ** argv)42 static int32_t lpc_import(CSOUND *csound, int32_t argc, char **argv)
43 {
44     FILE *inf;
45     FILE *outf;
46     LPHEADER hdr;
47     uint32_t i, j;
48     char *str;
49     MYFLT *coef;
50 
51     if (UNLIKELY(argc != 3)) {
52       lpc_import_usage(csound);
53       return 1;
54     }
55     inf = fopen(argv[1], "rb");
56     if (UNLIKELY(inf == NULL)) {
57       fprintf(stderr, Str("Cannot open input file %s\n"), argv[1]);
58       return 1;
59     }
60     outf = fopen(argv[2], "w");
61     if (UNLIKELY(outf == NULL)) {
62       csound->Message(csound, Str("Cannot open output file %s\n"), argv[2]);
63       fclose(inf);
64       return 1;
65     }
66     if (UNLIKELY(fread(&hdr, sizeof(LPHEADER)-4, 1, inf) != 1 ||
67                  (hdr.lpmagic != LP_MAGIC && hdr.lpmagic != LP_MAGIC2))) {
68       csound->Message(csound, "%s", Str("Failed to read LPC header\n"));
69       fclose(outf);
70       fclose(inf);
71       return 1;
72     }
73     fprintf(outf, "%d,%d,%d,%d,%f,%f,%f",
74             hdr.headersize, hdr.lpmagic, hdr.npoles, hdr.nvals,
75             hdr.framrate, hdr.srate, hdr.duration);
76     if (UNLIKELY(hdr.npoles<=0 ||
77                  hdr.headersize>0x40000000 ||
78                  hdr.headersize<sizeof(LPHEADER))) {
79       fclose(outf);
80       fclose(inf);
81       return 1;
82     }
83     str = (char *)csound->Malloc(csound,hdr.headersize-sizeof(LPHEADER)+8);
84     if (UNLIKELY(str==NULL)) {
85       fclose(outf);
86       fclose(inf);
87       return 1;
88       }
89     if (UNLIKELY(fread(str, sizeof(char),
90                        hdr.headersize-sizeof(LPHEADER)+4, inf)!=
91                  hdr.headersize-sizeof(LPHEADER)+4))
92       csound->Message(csound, "%s", Str("Read failure\n"));
93     for (i=0; i<hdr.headersize-sizeof(LPHEADER)+4; i++)
94       putc(str[i],outf);
95     putc('\n', outf);
96     coef = (MYFLT *)csound->Malloc(csound, (hdr.npoles+hdr.nvals)*sizeof(MYFLT));
97     for (i = 0; i<hdr.nvals; i++) {
98       if (UNLIKELY(fread(&coef[0], sizeof(MYFLT),
99                          hdr.npoles, inf)!=(size_t)hdr.npoles))
100         csound->Message(csound, "%s", Str("Read failure\n"));
101       for (j=0; j<hdr.npoles; j++)
102         fprintf(outf, "%f%c", coef[j], (j==hdr.npoles-1 ? '\n' : ','));
103     }
104     fclose(outf);
105     fclose(inf);
106     csound->Free(csound,coef); csound->Free(csound,str);
107     return 0;
108 }
109 
110 /* module interface */
111 
lpc_import_init_(CSOUND * csound)112 int32_t lpc_import_init_(CSOUND *csound)
113 {
114     int32_t retval = csound->AddUtility(csound, "lpc_import", lpc_import);
115     if (!retval) {
116       retval =
117         csound->SetUtilityDescription(csound, "lpc_import",
118                                       Str("translate text file to "
119                                           "linear predictive coding file"));
120     }
121     return retval;
122 }
123 
124