Edinburgh Speech Tools  2.1-release
esps_io.cc
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1994,1995,1996 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Paul Taylor and Alan W Black */
34 /* Date : June 1994 (June 1996) */
35 /*-----------------------------------------------------------------------*/
36 /* Access ESPS headered files */
37 /* This offers a licence free interface to reading and writing ESPS */
38 /* headered files. although not complete it works for most major ESPS */
39 /* files include signals, lpc and F0 files */
40 /* */
41 /*=======================================================================*/
42 #include <cstdio>
43 #include <cstdlib>
44 #include "EST_unix.h"
45 #include <cstring>
46 #include "EST_cutils.h"
47 #include "EST_wave_utils.h"
48 #include "esps_utils.h"
49 
50 /* The following are attempts to read and write ESPS header files without */
51 /* Using the Entropic libraries. These routines do not read all ESPS */
52 /* headers but are adequate for all of the uses we wish (and those you */
53 /* wish too probably) */
54 
55 enum EST_write_status put_esps(const char *filename,const char *style, float *t, float *a,
56  int *v, float fsize, float rate, int num_points)
57 {
58  (void)t;
59  esps_hdr hdr;
60  esps_rec rec;
61  FILE *fd;
62  int i;
63 
64  if ((fd=fopen(filename,"wb")) == NULL)
65  {
66  fprintf(stderr,"ESPS file: cannot open file \"%s\" for writing\n",
67  filename);
68  return misc_write_error;
69  }
70 
71  hdr = make_esps_hdr();
72 
73  if (streq(style,"F0"))
74  {
75  add_field(hdr,"F0",ESPS_DOUBLE,1);
76  add_field(hdr,"prob_voice",ESPS_DOUBLE,1);
77  add_field(hdr,"rms",ESPS_DOUBLE,1);
78  add_field(hdr,"ac_peak",ESPS_DOUBLE,1);
79  add_field(hdr,"k1",ESPS_DOUBLE,1);
80  add_fea_d(hdr,"record_freq",0,(double)rate);
81  add_fea_d(hdr,"frame_duration",0,(double)fsize);
82  add_fea_d(hdr,"start_time",0,(double)0);
84  "EDST F0 written as ESPS FEA_SD.\n");
85  write_esps_hdr(hdr,fd);
86  rec = new_esps_rec(hdr);
87  for (i = 0; i < num_points; i++)
88  {
89  set_field_d(rec,0,0,a[i]);
90  set_field_d(rec,1,0,(float)v[i]);
91  set_field_d(rec,2,0,0.5);
92  set_field_d(rec,3,0,0.5);
93  set_field_d(rec,4,0,0.5);
94  write_esps_rec(rec,hdr,fd);
95  }
96  delete_esps_rec(rec);
97  }
98  else
99  {
100  add_field(hdr,"Track",ESPS_DOUBLE,1);
101  add_fea_d(hdr,"window_duration",0,(double)0.049);
102  add_fea_d(hdr,"frame_duration",0,(double)fsize);
103  add_fea_d(hdr,"record_freq",0,(double)rate);
104  add_fea_d(hdr,"start_time",0,(double)0);
106  "EDST Track written as ESPS FEA_SD.\n");
107  write_esps_hdr(hdr,fd);
108  rec = new_esps_rec(hdr);
109  for (i = 0; i < num_points; i++)
110  {
111  set_field_d(rec,0,0,a[i]);
112  write_esps_rec(rec,hdr,fd);
113  }
114  delete_esps_rec(rec);
115  }
116 
117  delete_esps_hdr(hdr);
118  wfree(hdr);
119  wfree(rec);
120  fclose(fd);
121 
122  return write_ok;
123 }
124 
125 enum EST_write_status put_track_esps(const char *filename, char **f_names,
126  float **a, float fsize, float rate,
127  int order, int num_points, short fixed)
128 {
129  esps_hdr hdr;
130  esps_rec rec;
131  FILE *fd;
132  int i, j;
133 
134  if ((fd = fopen(filename, "wb")) == NULL)
135  {
136  fprintf(stderr,"ESPS file: cannot open file \"%s\" for writing\n",
137  filename);
138  return misc_write_error;
139  }
140 
141  hdr = make_esps_hdr();
142 
143  for (i = 0; i < order; ++i)
144  add_field(hdr,f_names[i],ESPS_DOUBLE,1);
145 
146  if (!streq(f_names[0],"F0"))
147  {
148  add_fea_s(hdr,"lpccep_order",0,(short)order);
149  add_fea_i(hdr,"step",0,(int)fsize);
150  add_fea_d(hdr,"window_duration",0,(double)0.049);
151  add_fea_i(hdr,"start",0,(int)1);
152  add_fea_f(hdr,"warping_param",0,(float)0.0);
153  add_fea_s(hdr,"window_type",0,(short)2);
154  }
155  add_fea_d(hdr,"record_freq",0,(double)rate);
156  add_fea_d(hdr,"frame_duration",0,(double)fsize);
157  add_fea_d(hdr,"start_time",0,(double)0.0);
158  if (!fixed)
159  add_fea_s(hdr,"est_variable_frame", 0, (short)1);
160 
161  write_esps_hdr(hdr,fd);
162 
163  rec = new_esps_rec(hdr);
164  for (i = 0; i < num_points; ++i)
165  {
166  for (j = 0; j < order; ++j)
167  set_field_d(rec, j, 0,(double)a[i][j]);
168  write_esps_rec(rec,hdr,fd);
169  }
170 
171  delete_esps_rec(rec);
172  delete_esps_hdr(hdr);
173  wfree(rec);
174  wfree(hdr);
175  fclose(fd);
176  return write_ok;
177 }
178 
179 enum EST_read_status get_esps(const char *filename, char *style,
180  float **t, float **a, int **v, float *fsize, int *num_points)
181 {
182  (void)t;
183  FILE *fd;
184  enum EST_read_status rv;
185  int ff0, fprob_voice, i;
186  esps_hdr hdr;
187  float *ta;
188  double d;
189  int *tv;
190  esps_rec rec;
191 
192  if ((fd = fopen(filename, "rb")) == NULL)
193  {
194  fprintf(stderr, "Can't open esps file %s for reading\n", filename);
195  return misc_read_error;
196  }
197 
198  if ((rv=read_esps_hdr(&hdr,fd)) != format_ok)
199  {
200  fclose(fd);
201  wfree(hdr);
202  return rv;
203  }
204  ta = walloc(float,hdr->num_records);
205  tv = walloc(int,hdr->num_records);
206  /* Find field number of FO and prob_voice */
207  for (ff0=fprob_voice=-1,i=0; i < hdr->num_fields; i++)
208  if (streq("F0",hdr->field_name[i]))
209  ff0=i;
210  else if (streq("prob_voice",hdr->field_name[i]))
211  fprob_voice = i;
212 
213  rec = new_esps_rec(hdr);
214  for (i=0; i < hdr->num_records; i++)
215  {
216  if (read_esps_rec(rec,hdr,fd) == EOF)
217  {
218  fprintf(stderr,"ESPS file: unexpected end of file when reading record %d\n", i);
219  delete_esps_rec(rec);
220  wfree(rec);
221  delete_esps_hdr(hdr);
222  wfree(hdr);
223  fclose(fd);
224  wfree(ta);
225  wfree(tv);
226  return misc_read_error;
227  }
228  if (ff0 == -1) /* F0 field isn't explicitly labelled */
229  { /* so take first field */
230  switch(rec->field[0]->type)
231  {
232  case ESPS_DOUBLE:
233  ta[i] = get_field_d(rec,0,0); break;
234  case ESPS_FLOAT:
235  ta[i] = get_field_f(rec,0,0); break;
236  default:
237  fprintf(stderr,"ESPS file: doesn't seem to be F0 file\n");
238  delete_esps_rec(rec);
239  delete_esps_hdr(hdr);
240  fclose(fd);
241  return misc_read_error;
242  }
243  }
244  else /* use named field -- assume its a double */
245  ta[i] = get_field_d(rec,ff0,0);
246  if (fprob_voice == -1)
247  tv[i] = 1; /* no prob_voice field in this */
248  else
249  tv[i] = ((get_field_d(rec,fprob_voice,0) < 0.5) ? 0 : 1);
250  }
251 
252  *num_points = hdr->num_records;
253  *a = ta;
254  *v = tv;
255  if (fea_value_d("record_freq",0,hdr,&d) != 0)
256  *fsize = 0;
257  else
258  *fsize = 1.0/d;
259  if (ff0 != -1)
260  strcpy(style, "F0");
261  else
262  strcpy(style, "track");
263  delete_esps_rec(rec);
264  wfree(rec);
265  delete_esps_hdr(hdr);
266  fclose(fd);
267  wfree(hdr);
268 
269  return format_ok;
270 }
271 
272 enum EST_read_status get_track_esps(const char *filename, char ***fields,
273  float ***a, float *fsize,
274  int *num_points, int *num_fields,
275  short *fixed)
276 {
277  esps_hdr hdr = NULL;
278  esps_rec rec;
279  FILE *fd;
280  int i, j, order, num_recs, jj;
281  enum EST_read_status rv;
282  double d;
283  char **tf;
284  float **ta;
285 
286  if ((fd = fopen(filename, "rb")) == NULL)
287  return misc_read_error;
288 
289  if ((rv=read_esps_hdr(&hdr,fd)) != format_ok)
290  {
291  fclose(fd);
292  if (hdr != NULL) {
293  delete_esps_hdr(hdr);
294  wfree(hdr);
295  }
296  return rv;
297  }
298  num_recs = hdr->num_records;
299  order = hdr->num_fields;
300 
301  ta = walloc(float *,num_recs);
302  tf = walloc(char *,order);
303  for (j = 0; j < num_recs; ++j)
304  ta[j] = walloc(float,order);
305 
306  /* Read data values */
307  rec = new_esps_rec(hdr);
308 
309  {
310  short v;
311  *fixed = fea_value_s("est_variable_frame", 0, hdr, &v) != 0;
312  }
313 
314  for (j = 0; j < hdr->num_records; j++)
315  {
316  if (read_esps_rec(rec,hdr,fd) == EOF)
317  {
318  fprintf(stderr,"ESPS file: unexpected end of file when reading record %d\n", j);
319  delete_esps_rec(rec);
320  delete_esps_hdr(hdr);
321  fclose(fd);
322  wfree(tf);
323  for (jj = 0; jj < num_recs; ++jj)
324  wfree(ta[jj]);
325  wfree(ta);
326  wfree(rec);
327  wfree(hdr);
328  return misc_read_error;
329  }
330  for (i = 0; i < order; ++i)
331  switch (rec->field[i]->type)
332  {
333  case ESPS_DOUBLE:
334  ta[j][i]=get_field_d(rec,i,0); break;
335  case ESPS_FLOAT:
336  ta[j][i]=get_field_f(rec,i,0); break;
337  case ESPS_INT:
338  ta[j][i]=(float)get_field_i(rec,i,0); break;
339  case ESPS_SHORT:
340  ta[j][i]=(float)get_field_s(rec,i,0); break;
341  case ESPS_CHAR:
342  ta[j][i]=(float)get_field_c(rec,i,0); break;
343  case ESPS_CODED:
344  ta[j][i]=(float)get_field_s(rec,i,0); break;
345  default:
346  fprintf(stderr,"ESPS file: unsupported type in record %d\n",
347  rec->field[i]->type);
348  delete_esps_rec(rec);
349  delete_esps_hdr(hdr);
350  fclose(fd);
351  return misc_read_error;
352  }
353  }
354  num_recs = j; /* just a safe guard */
355 
356  /* read field names */
357  for (j = 0; j < order; ++j)
358  tf[j] = wstrdup(hdr->field_name[j]);
359 
360  /* copy local variables into argument list */
361  *fields = tf;
362  *num_points = num_recs;
363  *num_fields = order;
364  *a = ta;
365 
366  if (fea_value_d("record_freq",0,hdr,&d) != 0)
367  *fsize = 0;
368  else
369  *fsize = 1.0/d;
370 
371  delete_esps_rec(rec);
372  delete_esps_hdr(hdr);
373 
374  fclose(fd);
375  wfree(rec);
376  wfree(hdr);
377  return format_ok;
378 }
379 
esps_rec new_esps_rec(const esps_hdr hdr)
Definition: esps_utils.cc:775
char * wstrdup(const char *s)
Definition: walloc.c:117
enum EST_write_status put_track_esps(const char *filename, char **f_names, float **a, float fsize, float rate, int order, int num_points, short fixed)
Definition: esps_io.cc:125
#define walloc(TYPE, SIZE)
Definition: EST_walloc.h:52
float get_field_f(esps_rec r, int field, int pos)
Definition: esps_utils.cc:911
EST_write_status
double get_field_d(esps_rec r, int field, int pos)
Definition: esps_utils.cc:907
void add_fea_d(esps_hdr hdr, const char *name, int pos, double d)
Definition: esps_utils.cc:167
#define ESPS_FLOAT
Definition: esps_utils.h:142
#define ESPS_DOUBLE
Definition: esps_utils.h:141
#define streq(X, Y)
Definition: EST_cutils.h:57
int read_esps_rec(esps_rec r, esps_hdr hdr, FILE *fd)
Definition: esps_utils.cc:838
void add_fea_i(esps_hdr hdr, const char *name, int pos, int d)
Definition: esps_utils.cc:226
esps_hdr make_esps_hdr(void)
Definition: esps_utils.cc:980
#define ESPS_SHORT
Definition: esps_utils.h:144
void add_fea_f(esps_hdr hdr, const char *name, int pos, float d)
Definition: esps_utils.cc:197
enum EST_read_status get_track_esps(const char *filename, char ***fields, float ***a, float *fsize, int *num_points, int *num_fields, short *fixed)
Definition: esps_io.cc:272
enum EST_read_status read_esps_hdr(esps_hdr *uhdr, FILE *fd)
Definition: esps_utils.cc:989
#define misc_write_error
int get_field_i(esps_rec r, int field, int pos)
Definition: esps_utils.cc:915
The file was written successfully.
void set_field_d(esps_rec r, int field, int pos, double d)
Definition: esps_utils.cc:927
void delete_esps_rec(esps_rec r)
Definition: esps_utils.cc:824
#define misc_read_error
NULL
Definition: EST_WFST.cc:55
#define ESPS_CHAR
Definition: esps_utils.h:145
enum EST_write_status write_esps_hdr(esps_hdr hdr, FILE *fd)
Definition: esps_utils.cc:1242
short get_field_s(esps_rec r, int field, int pos)
Definition: esps_utils.cc:919
#define ESPS_INT
Definition: esps_utils.h:143
enum EST_write_status put_esps(const char *filename, const char *style, float *t, float *a, int *v, float fsize, float rate, int num_points)
Definition: esps_io.cc:55
enum EST_read_status get_esps(const char *filename, char *style, float **t, float **a, int **v, float *fsize, int *num_points)
Definition: esps_io.cc:179
EST_read_status
#define ESPS_FEA_COMMAND
Definition: esps_utils.h:153
int fea_value_s(const char *name, int pos, esps_hdr hdr, short *d)
Definition: esps_utils.cc:380
#define ESPS_CODED
Definition: esps_utils.h:146
char get_field_c(esps_rec r, int field, int pos)
Definition: esps_utils.cc:923
#define format_ok
void add_fea_special(esps_hdr hdr, int type, const char *name)
Definition: esps_utils.cc:322
int fea_value_d(const char *name, int pos, esps_hdr hdr, double *d)
Definition: esps_utils.cc:338
void wfree(void *p)
Definition: walloc.c:131
void delete_esps_hdr(esps_hdr h)
Definition: esps_utils.cc:760
void add_field(esps_hdr hdr, const char *name, int type, int dimension)
Definition: esps_utils.cc:138
void add_fea_s(esps_hdr hdr, const char *name, int pos, short d)
Definition: esps_utils.cc:264
int write_esps_rec(esps_rec r, esps_hdr h, FILE *fd)
Definition: esps_utils.cc:512