Edinburgh Speech Tools  2.1-release
esps_utils.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 : Alan W Black (and Paul Taylor) */
34 /* Date : June 1996 */
35 /*-----------------------------------------------------------------------*/
36 /* These routines form a basis for reading and writing Entropic's ESPS */
37 /* headered files. The reason we wrote them was to avoid including */
38 /* Entropic's own (large and cumbersome) code into all our programs. */
39 /* No Entropic proprietary code is included in this code which means */
40 /* you do not needs an Entropic licence to use it. */
41 /* */
42 /* However this should not be seen as anti-Entropic in anyway it is for */
43 /* our and your convenience. We would like to specifically thank */
44 /* Rodney Johnson of Entropic for giving us help and confirming to us */
45 /* the header format is in fact more complex than one can imagine, */
46 /* mostly for very bad reasons, (backward compatibility cripples all */
47 /* software in the long run). Hence this code is almost definitely */
48 /* incomplete and is not guaranteed to read or create all ESPS files */
49 /* properly but it is adequate for many tasks. */
50 /* */
51 /* Also thanks go to Peter Kabal from McGill University whose AF code */
52 /* showed me this might be worth attempting, his code gave me something */
53 /* to look at to start with. */
54 /* */
55 /* I should add, this wasn't easy to write, though I'm much better at */
56 /* octal and hex dumps now. */
57 /*=======================================================================*/
58 #include <cstdio>
59 #include <cstdlib>
60 #include "EST_unix.h"
61 #include <cstring>
62 #include <ctime>
63 #include "EST_wave_utils.h"
64 #include "esps_utils.h"
65 #include "EST_File.h"
66 
67 using namespace std;
68 
69 /* First you must realise there is in fact a number of very similar but */
70 /* subtly different header formats that appear on ESPS files. */
71 /* ESPS_FEA and ESPS_SD (others for filters, spectrograms, etc) */
72 /* The basic format is */
73 /* preamble */
74 /* fixed header */
75 /* variable header */
76 /* field descriptions (type and dimensions) */
77 /* field names */
78 /* header FEAs (maybe with values) */
79 /* old and foreign headers (mostly ignored here) */
80 /* data records themselves */
81 
82 /* esps_fea contain a name and possibly a value. They appear in the */
83 /* variable part of the header */
84 esps_fea new_esps_fea()
85 {
86  esps_fea r = walloc(struct ESPS_FEA_struct,1);
87  r->type = 0;
88  r->clength = 0;
89  r->name = NULL;
90  r->dtype = 0;
91  r->count = 0;
92  r->v.ival = NULL;
93  return r;
94 }
95 
96 void delete_esps_fea(esps_fea r)
97 {
98  esps_fea t,u;
99 
100  for (t=r; t != NULL; t=u)
101  {
102  if (t->clength != 0)
103  wfree(t->name);
104  if (t->count != 0) /* this wont work if fields in v aren't aligned */
105  wfree(t->v.ival);
106  u = t->next;
107  wfree(t);
108  }
109 }
110 
111 void print_esps_fea(esps_fea r)
112 {
113  /* Print out the information in the FEA record */
114  int i;
115 
116  fprintf(stdout,"type: %d\n",r->type);
117  fprintf(stdout,"name: %s\n",r->name);
118  fprintf(stdout,"size: %d\n",r->count);
119  fprintf(stdout,"dtype: %d\n",r->dtype);
120  for (i=0; i<r->count; i++)
121  switch (r->dtype)
122  {
123  case ESPS_DOUBLE:
124  fprintf(stdout," %d: %g\n",i,r->v.dval[i]); break;
125  case ESPS_FLOAT:
126  fprintf(stdout," %d: %f\n",i,r->v.fval[i]); break;
127  case ESPS_INT:
128  fprintf(stdout," %d: %d\n",i,r->v.ival[i]); break;
129  case ESPS_SHORT:
130  fprintf(stdout," %d: %d\n",i,r->v.sval[i]); break;
131  case ESPS_CHAR:
132  fprintf(stdout," %d: %d\n",i,r->v.cval[i]); break;
133  default:
134  fprintf(stdout," %d: unknown\n",i);
135  }
136 }
137 
138 void add_field(esps_hdr hdr, const char *name, int type, int dimension)
139 {
140  /* Add a new field to the record */
141  char **names = hdr->field_name;
142  short *types = hdr->field_type;
143  int *dims = hdr->field_dimension;
144  int i;
145 
146  hdr->field_name = walloc(char *,hdr->num_fields+1);
147  hdr->field_type = walloc(short,hdr->num_fields+1);
148  hdr->field_dimension = walloc(int,hdr->num_fields+1);
149  for (i=0; i < hdr->num_fields; i++)
150  {
151  hdr->field_name[i] = names[i];
152  hdr->field_type[i] = types[i];
153  hdr->field_dimension[i] = dims[i];
154  }
155  wfree(names);
156  wfree(types);
157  wfree(dims);
158  hdr->field_name[hdr->num_fields] = wstrdup(name);
159  hdr->field_type[hdr->num_fields] = type;
160  hdr->field_dimension[hdr->num_fields] = dimension;
161  hdr->num_fields++;
162 
163  return;
164 
165 }
166 
167 void add_fea_d(esps_hdr hdr,const char *name, int pos, double d)
168 {
169  /* Add a double FEA field to the header */
170  esps_fea t = new_esps_fea();
171  int i;
172 
173  t->type = 13; /* must be lucky for some !! */
174  t->clength = strlen(name);
175  t->name = wstrdup(name);
176  if (t->count < pos+1)
177  {
178  double *dval = t->v.dval;
179  t->v.dval = walloc(double,pos+1);
180  for (i=0; i<t->count; i++)
181  t->v.dval[i] = dval[i];
182  for (; i < pos+1; i++)
183  t->v.dval[i] = 0.0;
184  wfree(dval);
185  t->count = pos+1;
186  }
187  t->dtype = ESPS_DOUBLE;
188  if (pos >= 0)
189  t->v.dval[pos] = d;
190 
191  t->next = hdr->fea;
192  hdr->fea = t;
193 
194  return;
195 }
196 
197 void add_fea_f(esps_hdr hdr,const char *name, int pos, float d)
198 {
199  /* Add a float FEA field to the header */
200  esps_fea t = new_esps_fea();
201  int i;
202 
203  t->type = 13;
204  t->clength = strlen(name);
205  t->name = wstrdup(name);
206  if (t->count < pos+1)
207  {
208  float *fval = t->v.fval;
209  t->v.fval = walloc(float,pos+1);
210  for (i=0; i<t->count; i++)
211  t->v.fval[i] = fval[i];
212  for (; i < pos+1; i++)
213  t->v.fval[i] = 0.0;
214  wfree(fval);
215  t->count = pos+1;
216  }
217  t->dtype = ESPS_FLOAT;
218  t->v.fval[pos] = d;
219 
220  t->next = hdr->fea;
221  hdr->fea = t;
222 
223  return;
224 }
225 
226 void add_fea_i(esps_hdr hdr,const char *name, int pos, int d)
227 {
228  /* Add an int FEA field to the header */
229  esps_fea t = new_esps_fea();
230  int i;
231 
232  t->type = 13;
233  t->clength = strlen(name);
234  t->name = wstrdup(name);
235  if (pos < 0) {
236  fprintf(stderr, "%s", "ESPS: add_fea_i: pos should not be < 0\n");
237  delete_esps_fea(t);
238  return;
239  }
240  if (pos == INT_MAX) {
241  fprintf(stderr, "%s", "ESPS: add_fea_i: pos too large\n");
242  pos -=1;
243  }
244  if (t->count < pos+1)
245  {
246  int *ival = t->v.ival;
247  t->v.ival = walloc(int,pos+1);
248  for (i=0; i<t->count; i++)
249  t->v.ival[i] = ival[i];
250  for (; i < pos+1; i++)
251  t->v.ival[i] = 0;
252  wfree(ival);
253  t->count = pos+1;
254  }
255  t->dtype = ESPS_INT;
256  t->v.ival[pos] = d;
257 
258  t->next = hdr->fea;
259  hdr->fea = t;
260 
261  return;
262 }
263 
264 void add_fea_s(esps_hdr hdr,const char *name, int pos, short d)
265 {
266  /* Add a short FEA field to the header */
267  esps_fea t = new_esps_fea();
268  int i;
269 
270  t->type = 13;
271  t->clength = strlen(name);
272  t->name = wstrdup(name);
273  if (t->count < pos+1)
274  {
275  short *sval = t->v.sval;
276  t->v.sval = walloc(short,pos+1);
277  for (i=0; i<t->count; i++)
278  t->v.sval[i] = sval[i];
279  for (; i < pos+1; i++)
280  t->v.sval[i] = (short)0;
281  wfree(sval);
282  t->count = pos+1;
283  }
284  t->dtype = ESPS_SHORT;
285  t->v.sval[pos] = d;
286 
287  t->next = hdr->fea;
288  hdr->fea = t;
289 
290  return;
291 }
292 
293 void add_fea_c(esps_hdr hdr,const char *name, int pos, char d)
294 {
295  /* Add a char FEA field to the header */
296  esps_fea t = new_esps_fea();
297  int i;
298 
299  t->type = 13;
300  t->clength = strlen(name);
301  t->name = wstrdup(name);
302  if (t->count < pos+1)
303  {
304  char *cval = t->v.cval;
305  t->v.cval = walloc(char,pos+1);
306  for (i=0; i<t->count; i++)
307  t->v.cval[i] = cval[i];
308  for (; i < pos+1; i++)
309  t->v.cval[i] = (char)0;
310  wfree(cval);
311  t->count = pos+1;
312  }
313  t->dtype = ESPS_CHAR;
314  t->v.cval[pos] = d;
315 
316  t->next = hdr->fea;
317  hdr->fea = t;
318 
319  return;
320 }
321 
322 void add_fea_special(esps_hdr hdr,int type,const char *name)
323 {
324  /* Add a special FEA field to the header */
325  esps_fea t = new_esps_fea();
326 
327  t->type = type;
328  t->clength = strlen(name);
329  t->name = wstrdup(name);
330  t->count = 0;
331 
332  t->next = hdr->fea;
333  hdr->fea = t;
334 
335  return;
336 }
337 
338 int fea_value_d(const char *name,int pos,esps_hdr hdr, double *d)
339 {
340  /* Get value of double FEA */
341  esps_fea t;
342 
343  for (t=hdr->fea; t != NULL; t=t->next)
344  if (streq(name,t->name))
345  {
346  if (t->dtype != ESPS_DOUBLE)
347  {
348  fprintf(stderr,"ESPS hdr: access non-double field \"%s\" as double\n",
349  name);
350  return -1;
351  }
352  *d = t->v.dval[pos];
353  return 0;
354  }
355 
356  return -1; /* failed to find it */
357 }
358 
359 int fea_value_f(const char *name,int pos,esps_hdr hdr, float *d)
360 {
361  /* Get value of float FEA */
362  esps_fea t;
363 
364  for (t=hdr->fea; t != NULL; t=t->next)
365  if (streq(name,t->name))
366  {
367  if (t->dtype != ESPS_FLOAT)
368  {
369  fprintf(stderr,"ESPS hdr: access non-float field \"%s\" as float\n",
370  name);
371  return -1;
372  }
373  *d = t->v.fval[pos];
374  return 0;
375  }
376 
377  return -1; /* failed to find it */
378 }
379 
380 int fea_value_s(const char *name,int pos,esps_hdr hdr, short *d)
381 {
382  /* Get value of short FEA */
383  esps_fea t;
384 
385  for (t=hdr->fea; t != NULL; t=t->next)
386  if (streq(name,t->name))
387  {
388  if (t->dtype != ESPS_SHORT)
389  {
390  fprintf(stderr,"ESPS hdr: access non-short field \"%s\" as short\n",
391  name);
392  return -1;
393  }
394  *d = t->v.sval[pos];
395  return 0;
396  }
397 
398  return -1; /* failed to find it */
399 }
400 
401 int fea_value_i(const char *name,int pos,esps_hdr hdr, int *d)
402 {
403  /* Get value of int FEA */
404  esps_fea t;
405 
406  for (t=hdr->fea; t != NULL; t=t->next)
407  if (streq(name,t->name))
408  {
409  if (t->dtype != ESPS_INT)
410  {
411  fprintf(stderr,"ESPS hdr: access non-int field \"%s\" as int\n",
412  name);
413  return -1;
414  }
415  *d = t->v.ival[pos];
416  return 0;
417  }
418 
419  return -1; /* failed to find it */
420 }
421 
422 int fea_value_c(const char *name,int pos,esps_hdr hdr, char *d)
423 {
424  /* Get value of int FEA */
425  esps_fea t;
426 
427  for (t=hdr->fea; t != NULL; t=t->next)
428  if (streq(name,t->name))
429  {
430  if (t->dtype != ESPS_CHAR)
431  {
432  fprintf(stderr,"ESPS hdr: access non-char field \"%s\" as char\n",
433  name);
434  return -1;
435  }
436  *d = t->v.cval[pos];
437  return 0;
438  }
439 
440  return -1; /* failed to find it */
441 }
442 
443 static int esps_alloc_fea(esps_fea r)
444 {
445  switch (r->dtype)
446  {
447  case 0: /* nothing */
448  break;
449  case ESPS_DOUBLE:
450  r->v.dval = walloc(double,r->count); break;
451  case ESPS_FLOAT:
452  r->v.fval = walloc(float,r->count); break;
453  case ESPS_INT:
454  r->v.ival = walloc(int,r->count); break;
455  case ESPS_SHORT:
456  r->v.sval = walloc(short,r->count); break;
457  case ESPS_CHAR:
458  r->v.cval = walloc(char,r->count); break;
459  default:
460  fprintf(stderr,"ESPS file: unsupported FEA dtype\n");
461  return -1;
462  }
463 
464  return 0;
465 }
466 
467 void write_esps_fea(FILE *fd, esps_fea t,esps_hdr hdr)
468 {
469  /* write out this fea */
470  (void)hdr;
471  short clength;
472  char *nspace;
473  int i;
474 
475  fwrite(&t->type,2,1,fd);
476  clength = (strlen(t->name)+3)/4;
477  fwrite(&clength,2,1,fd);
478  nspace = walloc(char, clength*4);
479  memset(nspace,0,clength*4);
480  memmove(nspace,t->name,strlen(t->name));
481  fwrite(nspace,1,clength*4,fd);
482  wfree(nspace);
483  if ((t->type == 11) ||
484  (t->type == 1) ||
485  (t->type == 15))
486  return;
487  fwrite(&t->count,4,1,fd);
488  fwrite(&t->dtype,2,1,fd);
489 
490  for (i=0; i<t->count; i++)
491  {
492  switch(t->dtype)
493  {
494  case ESPS_DOUBLE:
495  fwrite(&t->v.dval[i],8,1,fd); break;
496  case ESPS_FLOAT:
497  fwrite(&t->v.fval[i],4,1,fd); break;
498  case ESPS_INT:
499  fwrite(&t->v.ival[i],4,1,fd); break;
500  case ESPS_SHORT:
501  fwrite(&t->v.sval[i],2,1,fd); break;
502  case ESPS_CHAR:
503  fwrite(&t->v.cval[i],1,1,fd); break;
504  default:
505  fprintf(stderr,"ESPS write_hdr: unsupported FEA dtype %d\n",
506  t->dtype);
507  }
508  }
509  return;
510 }
511 
512 int write_esps_rec(esps_rec r, esps_hdr h, FILE *fd)
513 {
514  /* will have to worry about swap someday */
515  (void)h;
516  int i;
517 
518  for (i=0; i < r->num_fields; i++)
519  {
520  switch(r->field[i]->type)
521  {
522  case ESPS_DOUBLE:
523  fwrite(r->field[i]->v.dval,8,r->field[i]->dimension,fd);
524  break;
525  case ESPS_FLOAT:
526  fwrite(r->field[i]->v.fval,4,r->field[i]->dimension,fd);
527  break;
528  case ESPS_INT:
529  fwrite(r->field[i]->v.ival,4,r->field[i]->dimension,fd);
530  break;
531  case ESPS_SHORT:
532  fwrite(r->field[i]->v.sval,2,r->field[i]->dimension,fd);
533  break;
534  case ESPS_CHAR:
535  fwrite(r->field[i]->v.cval,1,r->field[i]->dimension,fd);
536  break;
537  case ESPS_CODED:
538  fwrite(r->field[i]->v.sval,2,r->field[i]->dimension,fd);
539  break;
540 
541  default:
542  fprintf(stderr,"ESPS file: unsupported field type %d\n",
543  r->field[i]->type);
544  }
545  }
546  return 0;
547 }
548 
549 esps_fea read_esps_fea(FILE *fd, esps_hdr hdr)
550 {
551  /* read next FEA record at point */
552  esps_fea r = new_esps_fea();
553  short sdata;
554  int i;
555  int idata;
556  float fdata;
557  double ddata;
558  char cdata;
559  int err=0;
560  if (fread(&sdata,sizeof(short),1,fd) != 1) err = 1;
561  if (hdr->swapped) sdata = SWAPSHORT(sdata);
562  r->type = sdata;
563  if (r->type == 0) /* a field name */
564  { /* next short is the size in bytes */
565  if (fread(&sdata,sizeof(short),1,fd) != 1) err = 1;
566  if (hdr->swapped) sdata = SWAPSHORT(sdata);
567  if (sdata < 0 || sdata > SHRT_MAX -1) {
568  fprintf(stderr, "ESPS: fea record wrong size\n");
569  wfree(r);
570  return NULL;
571  }
572  r->clength = sdata;
573  }
574  else if ((r->type == 13) || /* a feature and value */
575  (r->type == 11) || /* a single string (comment ?) */
576  (r->type == 1) || /* a filename */
577  (r->type == 4) || /* a filename */
578  (r->type == 15)) /* directory name */
579  {
580  if (fread(&sdata,sizeof(short),1,fd) != 1) err = 1;
581  if (hdr->swapped) sdata = SWAPSHORT(sdata);
582  if (sdata > SHRT_MAX/4) {
583  fprintf(stderr, "ESPS: fea record too large\n");
584  wfree(r);
585  return NULL;
586  }
587  if (sdata < 0) {
588  fprintf(stderr, "ESPS: fea record too small\n");
589  wfree(r);
590  return NULL;
591  }
592  r->clength = sdata * 4;
593  if (r->clength > SHRT_MAX -1) {
594  fprintf(stderr, "ESPS: fea record wrong size\n");
595  wfree(r);
596  return NULL;
597  }
598  }
599  else
600  {
601  fprintf(stderr,"ESPS: fea record unknown type\n");
602  wfree(r);
603  return NULL;
604  }
605  r->name = walloc(char,r->clength+1);
606  if (fread(r->name,sizeof(char),r->clength,fd) != (size_t) r->clength) err=1;
607  r->name[r->clength] = '\0';
608  if ((r->type == 11) || /* a single string */
609  (r->type == 1) || /* a filename */
610  (r->type == 15)) /* directory name */
611  return r;
612  if (fread(&idata,sizeof(int),1,fd) != 1) err = 1;
613  if (hdr->swapped) idata = SWAPINT(idata);
614  if (idata < 0 ) {
615  fprintf(stderr, "ESPS: negative r->count: Bad header\n");
616  wfree(r);
617  return NULL;
618  }
619  r->count = idata;
620  if (fread(&sdata,sizeof(short),1,fd) != 1) err = 1;
621  if (hdr->swapped) sdata = SWAPSHORT(sdata);
622  r->dtype = sdata;
623  if (esps_alloc_fea(r) == -1) {
624  wfree(r->name);
625  wfree(r);
626  return NULL;
627  }
628  for (i=0; i<r->count; i++)
629  {
630  switch (r->dtype)
631  {
632  case ESPS_DOUBLE:
633  if (fread(&ddata,sizeof(double),1,fd) != 1) err=1;
634  if (hdr->swapped) swapdouble(&ddata);
635  r->v.dval[i] = ddata;
636  break;
637  case ESPS_FLOAT:
638  if (fread(&fdata,sizeof(float),1,fd) != 1) err=1;
639  if (hdr->swapped) swapfloat(&fdata);
640  r->v.fval[i] = fdata;
641  break;
642  case ESPS_INT:
643  if (fread(&idata,sizeof(int),1,fd) != 1) err = 1;
644  if (hdr->swapped) idata = SWAPINT(idata);
645  r->v.ival[i] = idata;
646  break;
647  case ESPS_SHORT:
648  if (fread(&sdata,sizeof(short),1,fd) != 1) err=1;
649  if (hdr->swapped) sdata = SWAPSHORT(sdata);
650  r->v.sval[i] = sdata;
651  break;
652  case ESPS_CHAR:
653  if (fread(&cdata,sizeof(char),1,fd) != 1) err=1;
654  r->v.cval[i] = cdata;
655  break;
656  default:
657  fprintf(stderr,"ESPS read_hdr: unsupported FEA dtype %d\n",r->dtype);
658  wfree(r);
659  return NULL;
660  }
661  }
662  if (err==1) {
663  fprintf(stderr, "ESPS read_hdr: Wrong format\n");
664  wfree(r);
665  return NULL;
666  }
667 
668  return r;
669 }
670 
671 static char *esps_get_field_name(FILE *fd, esps_hdr hdr, int expect_source)
672 {
673  /* read the next field name */
674  short size=0; /* bet its really a short */
675  char *name;
676 
677  if (fread(&size,2,1,fd) != 1)
678  {
679  fputs("error reading field name size\n", stderr);
680  return wstrdup("ERROR");
681  }
682  if (hdr->swapped) size = SWAPSHORT(size);
683  if (size < 0 || size > SHRT_MAX -1) {
684  fputs("error reading field size\n", stderr);
685  return wstrdup("ERROR");
686  }
687  name = walloc(char, size+1);
688  if (fread(name,1,size,fd) != (unsigned)size)
689  {
690  fputs("error reading field name\n", stderr);
691  strncpy(name, "ERROR", size);
692  }
693  name[size] = '\0';
694  if (hdr->file_type == ESPS_SD || expect_source) {
695  if (fseek(fd,6,SEEK_CUR) != 0) { /* skip some zeroes */
696  fputs("error skipping some zeroes", stderr);
697  wfree(name);
698  return wstrdup("ERROR");
699  }
700  } else {
701  if (fseek(fd,2,SEEK_CUR) != 0){ /* skip some zeroes */
702  fputs("error skipping some zeroes", stderr);
703  wfree(name);
704  return wstrdup("ERROR");
705  }
706  }
707 
708  if (expect_source)
709  {
710  if (fread(&size,sizeof(short),1,fd) != 1) {
711  fprintf(stderr, "ESPS: wrong field format\n");
712  wfree(name);
713  return NULL;
714  }
715  if (hdr->swapped) size = SWAPSHORT(size);
716  if (EST_fseek(fd,size,SEEK_CUR) != 0) {
717  fprintf(stderr, "esps read error\n");
718  wfree(name);
719  return NULL;
720  }
721  }
722 
723  return name;
724 }
725 
726 static void esps_put_field_name(char *name,FILE *fd, esps_hdr hdr)
727 {
728  /* write the next field name */
729  short size = strlen(name);
730  short shortdata;
731 
732  shortdata = 0;
733  fwrite(&shortdata,2,1,fd);
734  fwrite(&size,2,1,fd);
735  fwrite(name,1,size,fd);
736  if (hdr->file_type == ESPS_SD)
737  {
738  shortdata = 0;
739  fwrite(&shortdata,2,1,fd);
740  fwrite(&shortdata,2,1,fd);
741  fwrite(&shortdata,2,1,fd);
742  }
743  return;
744 }
745 
746 esps_hdr new_esps_hdr(void)
747 {
748  esps_hdr h = walloc(struct ESPS_HDR_struct,1);
749  h->file_type = ESPS_FEA;
750  h->swapped = FALSE;
751  h->num_records = 0;
752  h->num_fields = 0;
753  h->field_name = NULL;
754  h->field_type = NULL;
755  h->field_dimension = NULL;
756  h->fea = NULL;
757  return h;
758 }
759 
760 void delete_esps_hdr(esps_hdr h)
761 {
762  int i;
763  if (h != NULL)
764  {
765  if (h->field_name != NULL)
766  {
767  for (i=0; i < h->num_fields; i++)
768  wfree(h->field_name[i]);
769  wfree(h->field_name);
770  }
771  delete_esps_fea(h->fea);
772  }
773 }
774 
775 esps_rec new_esps_rec(const esps_hdr hdr)
776 {
777  /* New esps record */
778  esps_rec r = walloc(struct ESPS_REC_struct,1);
779  int i,size;
780 
781  r->field = walloc(esps_field,hdr->num_fields);
782  for (size=0,i=0; i < hdr->num_fields; i++)
783  {
784  r->field[i]=walloc(struct ESPS_FIELD_struct,1);
785  r->field[i]->type = hdr->field_type[i];
786  r->field[i]->dimension = hdr->field_dimension[i];
787  switch(r->field[i]->type)
788  {
789  case ESPS_DOUBLE:
790  r->field[i]->v.dval = walloc(double,r->field[i]->dimension);
791  size += 8;
792  break;
793  case ESPS_FLOAT:
794  r->field[i]->v.fval = walloc(float,r->field[i]->dimension);
795  size += 4;
796  break;
797  case ESPS_INT:
798  r->field[i]->v.ival = walloc(int,r->field[i]->dimension);
799  size += 4;
800  break;
801  case ESPS_SHORT:
802  r->field[i]->v.sval = walloc(short,r->field[i]->dimension);
803  size += 2;
804  break;
805  case ESPS_CHAR:
806  r->field[i]->v.cval = walloc(char,r->field[i]->dimension);
807  size += 1;
808  break;
809  case ESPS_CODED:
810  r->field[i]->v.sval = walloc(short,r->field[i]->dimension);
811  size += 2;
812  break;
813  default:
814  fprintf(stderr,"ESPS file: unsupported field type %d\n",
815  r->field[i]->type);
816  }
817  }
818  r->num_fields = hdr->num_fields;
819  r->size = size;
820  return r;
821 
822 }
823 
824 void delete_esps_rec(esps_rec r)
825 {
826  int i;
827 
828  for (i=0; i<r->num_fields; i++)
829  {
830  wfree(r->field[i]->v.ival);
831  wfree(r->field[i]);
832  }
833  wfree(r->field);
834  /* wfree(r); (already freed on delete_esps_hdr) */
835  return;
836 }
837 
838 int read_esps_rec(esps_rec r, esps_hdr hdr, FILE *fd)
839 {
840  /* read the next record at point */
841  int i,j;
842  double doubledata;
843  float floatdata;
844  int intdata;
845  short shortdata;
846 
847  for (i=0; i< r->num_fields; i++)
848  {
849  switch (r->field[i]->type)
850  {
851  case ESPS_DOUBLE:
852  for(j=0; j < r->field[i]->dimension; j++)
853  {
854  if (fread(&doubledata,8,1,fd) != 1) return EOF;
855  if (hdr->swapped) swapdouble(&doubledata);
856  r->field[i]->v.dval[j] = doubledata;
857  }
858  break;
859  case ESPS_FLOAT:
860  for(j=0; j < r->field[i]->dimension; j++)
861  {
862  if (fread(&floatdata,4,1,fd) != 1) return EOF;
863  if (hdr->swapped) swapfloat(&floatdata);
864  r->field[i]->v.fval[j] = floatdata;
865  }
866  break;
867  case ESPS_INT:
868  for(j=0; j < r->field[i]->dimension; j++)
869  {
870  if (fread(&intdata,4,1,fd) != 1) return EOF;
871  if (hdr->swapped) intdata = SWAPINT(intdata);
872  r->field[i]->v.ival[j] = intdata;
873  }
874  break;
875  case ESPS_SHORT:
876  for(j=0; j < r->field[i]->dimension; j++)
877  {
878  if (fread(&shortdata,2,1,fd) != 1) return EOF;
879  if (hdr->swapped) shortdata = SWAPSHORT(shortdata);
880  r->field[i]->v.sval[j] = shortdata;
881  }
882  break;
883  case ESPS_CHAR:
884  if (fread(r->field[i]->v.cval,1,r->field[i]->dimension,fd) !=
885  (unsigned)r->field[i]->dimension) return EOF;
886  break;
887  case ESPS_CODED:
888  for(j=0; j < r->field[i]->dimension; j++)
889  {
890  if (fread(&shortdata,2,1,fd) != 1) return EOF;
891  if (hdr->swapped) shortdata = SWAPSHORT(shortdata);
892  r->field[i]->v.sval[j] = shortdata;
893  }
894  break;
895  default:
896  fprintf(stderr,"ESPS file: unsupported field type %d\n",
897  r->field[i]->type);
898  return EOF;
899  }
900 
901  }
902 
903  return 0;
904 
905 }
906 
907 double get_field_d(esps_rec r, int field, int pos)
908 {
909  return r->field[field]->v.dval[pos];
910 }
911 float get_field_f(esps_rec r, int field, int pos)
912 {
913  return r->field[field]->v.fval[pos];
914 }
915 int get_field_i(esps_rec r, int field, int pos)
916 {
917  return r->field[field]->v.ival[pos];
918 }
919 short get_field_s(esps_rec r, int field, int pos)
920 {
921  return r->field[field]->v.sval[pos];
922 }
923 char get_field_c(esps_rec r, int field, int pos)
924 {
925  return r->field[field]->v.cval[pos];
926 }
927 void set_field_d(esps_rec r, int field, int pos, double d)
928 {
929  r->field[field]->v.dval[pos] = d;
930 }
931 void set_field_f(esps_rec r, int field, int pos, float d)
932 {
933  r->field[field]->v.fval[pos] = d;
934 }
935 void set_field_i(esps_rec r, int field, int pos, int d)
936 {
937  r->field[field]->v.ival[pos] = d;
938 }
939 void set_field_s(esps_rec r, int field, int pos, short d)
940 {
941  r->field[field]->v.sval[pos] = d;
942 }
943 void set_field_c(esps_rec r, int field, int pos, char d)
944 {
945  r->field[field]->v.cval[pos] = d;
946 }
947 
948 int esps_record_size(esps_hdr hdr)
949 {
950  /* works out the number of bytes in a record */
951  esps_rec r = new_esps_rec(hdr);
952  int size = r->size;
953  delete_esps_rec(r);
954  wfree(r);
955  return size;
956 }
957 
958 static int esps_num_of_type(int type,esps_hdr hdr)
959 {
960  /* counts up the number of occurrences of fields of type in a record */
961  int i;
962  int sum;
963 
964  for (sum=i=0; i < hdr->num_fields; i++)
965  if (hdr->field_type[i] == type)
966  sum++;
967  return sum;
968 }
969 
970 
971 esps_hdr make_esps_sd_hdr(void)
972 {
973  /* returns a basic header for an ESPS_SD file */
974  esps_hdr hdr = new_esps_hdr();
975  hdr->file_type = ESPS_SD;
976  return hdr;
977 
978 }
979 
980 esps_hdr make_esps_hdr(void)
981 {
982  /* returns a basic header for an ESPS_SD file */
983  esps_hdr hdr = new_esps_hdr();
984  hdr->file_type = ESPS_FEA;
985  return hdr;
986 
987 }
988 
989 enum EST_read_status read_esps_hdr(esps_hdr *uhdr,FILE *fd)
990 {
991  /* reads an ESPS header from fd at point (should be position 0) */
992  /* leaves point at start of data (immediately after header) */
993  struct ESPS_PREAMBLE preamble;
994  struct ESPS_FIXED_HDR fhdr;
995  esps_hdr hdr;
996  EST_FilePos pos, end;
997  int intdata,i;
998  short shortdata;
999  double sd_sample_rate;
1000  int typematch;
1001  int swap;
1002  short name_flag;
1003  int err = 0;
1004  if (fread(&(preamble.machine_code), sizeof(int), 1, fd) != 1) err=1;
1005  if (fread(&(preamble.check_code), sizeof(int), 1, fd) != 1) err=1;
1006  if (fread(&(preamble.data_offset), sizeof(int), 1, fd) != 1) err=1;
1007  if (fread(&(preamble.record_size), sizeof(int), 1, fd) != 1) err=1;
1008  if (fread(&(preamble.check), sizeof(int), 1, fd) != 1) err=1;
1009  if (fread(&(preamble.edr), sizeof(int), 1, fd) != 1) err=1;
1010  if (fread(&(preamble.fil1), sizeof(int), 1, fd) != 1) err=1;
1011  if (fread(&(preamble.foreign_hd), sizeof(int), 1, fd) != 1) err=1;
1012  if (preamble.check == ESPS_MAGIC)
1013  swap = FALSE;
1014  else if (preamble.check == SWAPINT(ESPS_MAGIC))
1015  swap = TRUE;
1016  else
1017  return wrong_format;
1018 
1019  hdr = new_esps_hdr();
1020  hdr->swapped = swap;
1021  if (fread(&(fhdr.thirteen), sizeof(short), 1, fd) != 1) err=1;
1022  if (fread(&(fhdr.sdr_size), sizeof(short), 1, fd) != 1) err=1;
1023  if (fread(&(fhdr.magic), sizeof(int), 1, fd) != 1) err=1;
1024  if (fread(&(fhdr.date), sizeof(char), 26, fd) != 26) err=1;
1025  if (fread(&(fhdr.version), sizeof(char), 8, fd) != 8) err=1;
1026  if (fread(&(fhdr.prog), sizeof(char), 16, fd) != 16) err=1;
1027  if (fread(&(fhdr.vers), sizeof(char), 8, fd) != 8) err=1;
1028  if (fread(&(fhdr.progcompdate), sizeof(char), 26, fd) != 26) err=1;
1029  if (fread(&(fhdr.num_samples), sizeof(int), 1, fd) != 1) err=1;
1030  if (fread(&(fhdr.filler), sizeof(int), 1, fd) != 1) err=1;
1031  if (fread(&(fhdr.num_doubles), sizeof(int), 1, fd) != 1) err=1;
1032  if (fread(&(fhdr.num_floats), sizeof(int), 1, fd) != 1) err=1;
1033  if (fread(&(fhdr.num_ints), sizeof(int), 1, fd) != 1) err=1;
1034  if (fread(&(fhdr.num_shorts), sizeof(int), 1, fd) != 1) err=1;
1035  if (fread(&(fhdr.num_chars), sizeof(int), 1, fd) != 1) err=1;
1036  if (fread(&(fhdr.fsize), sizeof(int), 1, fd) != 1) err=1;
1037  if (fread(&(fhdr.hsize), sizeof(int), 1, fd) != 1) err=1;
1038  if (fread(&(fhdr.username), sizeof(char), 8, fd) != 8) err=1;
1039  if (fread(&(fhdr.fil1), sizeof(int), 5, fd) != 5) err=1;
1040  if (fread(&(fhdr.fea_type), sizeof(short), 1, fd) != 1) err=1;
1041  if (fread(&(fhdr.fil2), sizeof(short), 1, fd) != 1) err=1;
1042  if (fread(&(fhdr.num_fields), sizeof(short), 1, fd) != 1) err=1;
1043  if (fhdr.num_fields < 0 || fhdr.num_fields > SHRT_MAX -1 ) {
1044  cerr << "Wrong ESPS header: Wrong number of fields" << endl;
1045  fhdr.num_fields = 0;
1046  delete_esps_hdr(hdr);
1047  wfree(hdr);
1048  return wrong_format;
1049  }
1050  if (fread(&(fhdr.fil3), sizeof(short), 1, fd) != 1) err=1;
1051  if (fread(&(fhdr.fil4), sizeof(int), 9, fd) != 9) err=1;
1052  if (fread(&(fhdr.fil5), sizeof(int), 8, fd) != 8) err=1;
1053 
1054  if (err == 1)
1055  {
1056  cerr << "Could not read ESPS header." << endl;
1057  cerr << "Wrong format" << endl;
1058  delete_esps_hdr(hdr);
1059  wfree(hdr);
1060  return wrong_format;
1061  }
1062 
1063  if (hdr->swapped)
1064  {
1065  preamble.data_offset = SWAPINT(preamble.data_offset);
1066  preamble.record_size = SWAPINT(preamble.record_size);
1067  fhdr.num_samples = SWAPINT(fhdr.num_samples);
1068  fhdr.num_doubles = SWAPINT(fhdr.num_doubles);
1069  fhdr.num_floats = SWAPINT(fhdr.num_floats);
1070  fhdr.num_ints = SWAPINT(fhdr.num_ints);
1071  fhdr.num_shorts = SWAPINT(fhdr.num_shorts);
1072  fhdr.num_chars = SWAPINT(fhdr.num_chars);
1073  fhdr.fea_type = SWAPSHORT(fhdr.fea_type);
1074  fhdr.num_fields = SWAPSHORT(fhdr.num_fields);
1075  }
1076  if (fhdr.num_samples == 0) /* has to be derived from the file size */
1077  {
1078  pos = EST_ftell(fd);
1079  if (pos < 0) {
1080  delete_esps_hdr(hdr);
1081  wfree(hdr);
1082  return misc_read_error;
1083  }
1084  if (EST_fseek(fd,0,SEEK_END) != 0) {
1085  delete_esps_hdr(hdr);
1086  wfree(hdr);
1087  return misc_read_error;
1088  }
1089  end = EST_ftell(fd);
1090  if (end < 0) {
1091  delete_esps_hdr(hdr);
1092  wfree(hdr);
1093  return misc_read_error;
1094  }
1095  if (EST_fseek(fd,pos,SEEK_SET) != 0) {
1096  delete_esps_hdr(hdr);
1097  wfree(hdr);
1098  return misc_read_error;
1099  }
1100  if (preamble.data_offset < 0) { /* Nonsense */
1101  delete_esps_hdr(hdr);
1102  wfree(hdr);
1103  return misc_read_error;
1104  }
1105  fhdr.num_samples = (end - preamble.data_offset)/preamble.record_size;
1106  }
1107  hdr->num_records = fhdr.num_samples;
1108  hdr->num_fields = fhdr.num_fields;
1109  hdr->hdr_size = preamble.data_offset;
1110  if (fhdr.thirteen == 9)
1111  { /* esps identifies such files are as Sample Data Files */
1112  hdr->file_type = ESPS_SD;
1113  /* fake the rest to make it appear like other SD files */
1114  hdr->num_fields = 1;
1115  hdr->field_dimension = walloc(int,hdr->num_fields);
1116  hdr->field_dimension[0] = 1;
1117  hdr->field_type = walloc(short,hdr->num_fields);
1118  hdr->field_type[0] = ESPS_SHORT;
1119  hdr->field_name = walloc(char *,1);
1120  hdr->field_name[0] = wstrdup("samples");
1121  if (EST_fseek(fd,hdr->hdr_size,SEEK_SET) != 0) {
1122  delete_esps_hdr(hdr);
1123  wfree(hdr);
1124  return misc_read_error;
1125  }
1126  /* In this cases its just in the header as a float */
1127  float *tmpfloat = (float *)&fhdr.fil4[0];
1128  sd_sample_rate = *tmpfloat;
1129  add_fea_d(hdr,"record_freq",0,(double)sd_sample_rate);
1130  *uhdr = hdr;
1131  return format_ok;
1132  }
1133  else if ((fhdr.fea_type == 8) &&
1134  (hdr->num_fields == 1) &&
1135  ((fhdr.num_shorts*2) == preamble.record_size))
1136  hdr->file_type = ESPS_SD; /* this is a heuristic */
1137  else
1138  hdr->file_type = ESPS_FEA;
1139  /* Now we have the field descriptions */
1140 
1141  /* 0000 0001 dimensions */
1142  hdr->field_dimension = walloc(int,hdr->num_fields);
1143  for (i=0; i<hdr->num_fields; i++)
1144  {
1145  if (fread(&intdata,sizeof(int),1,fd) != 1) err = 1; /* dimensions */
1146  if (hdr->swapped) intdata = SWAPINT(intdata);
1147  hdr->field_dimension[i] = intdata;
1148  }
1149  /* 0 -> num_fields-1 -- probably ordering information */
1150  if (EST_fseek(fd,hdr->num_fields*4,SEEK_CUR) != 0) err = 1; /* ordering info */
1151  if (EST_fseek(fd,hdr->num_fields*2,SEEK_CUR) != 0) err = 1; /* zeros */
1152  hdr->field_type = walloc(short,hdr->num_fields);
1153  for (i=0; i<hdr->num_fields; i++)
1154  {
1155  if (fread(&shortdata,sizeof(short),1,fd) != 1) err = 1; /* field types */
1156  if (hdr->swapped) shortdata = SWAPSHORT(shortdata);
1157  hdr->field_type[i] = shortdata;
1158  }
1159  typematch = TRUE;
1160  if (fread(&intdata,sizeof(int),1,fd) != 1) err = 1; /* number of doubles */
1161  if (hdr->swapped) intdata = SWAPINT(intdata);
1162  if (fhdr.num_doubles != intdata) typematch = FALSE;
1163  if (fread(&intdata,sizeof(int),1,fd) != 1) err = 1; /* number of floats */
1164  if (hdr->swapped) intdata = SWAPINT(intdata);
1165  if (fhdr.num_floats != intdata) typematch = FALSE;
1166  if (fread(&intdata,sizeof(int),1,fd) != 1) err = 1; /* number of ints */
1167  if (hdr->swapped) intdata = SWAPINT(intdata);
1168  if (fhdr.num_ints != intdata) typematch = FALSE;
1169  if (fread(&intdata,sizeof(int), 1, fd) !=1) err = 1; /* number of shorts */
1170  if (hdr->swapped) intdata = SWAPINT(intdata);
1171  if (fhdr.num_shorts != intdata) typematch = FALSE;
1172  if (fread(&intdata,sizeof(int),1,fd) != 1) err = 1; /* number of chars */
1173  if (hdr->swapped) intdata = SWAPINT(intdata);
1174  if (fhdr.num_chars != intdata) typematch = FALSE;
1175  if ((hdr->file_type != ESPS_SD) && (typematch == FALSE))
1176  {
1177  fprintf(stderr,"ESPS hdr: got lost in the header (record description)\n");
1178  delete_esps_hdr(hdr);
1179  wfree(hdr);
1180  return misc_read_error;
1181  }
1182  /* other types ... */
1183  if (EST_fseek(fd,9*2,SEEK_CUR) != 0) { /* other types */
1184  fprintf(stderr, "ESPS hdr: fseek error\n");
1185  return misc_read_error;
1186  }
1187  if (EST_fseek(fd,hdr->num_fields*2,SEEK_CUR) != 0) { /* zeros */
1188  fprintf(stderr, "ESPS hdr: fseek error\n");
1189  return misc_read_error;
1190  }
1191  /* Now we can read the field names */
1192  hdr->field_name = walloc(char *,hdr->num_fields);
1193 
1194  if (fread(&name_flag, sizeof(short), 1, fd) != 1) err = 1;
1195  if (hdr->swapped) name_flag = SWAPSHORT(name_flag);
1196 
1197  for (i=0; i < hdr->num_fields; i++)
1198  hdr->field_name[i] = esps_get_field_name(fd,hdr,name_flag); /* field names */
1199  if (hdr->file_type == ESPS_SD)
1200  { /* Only one field 'samples' */
1201  if (!streq(hdr->field_name[0],"samples"))
1202  {
1203  fprintf(stderr,"ESPS hdr: guessed wrong about FEA_SD file (no 'samples' field)\n");
1204  delete_esps_hdr(hdr);
1205  wfree(hdr);
1206  return misc_read_error;
1207  }
1208  }
1209 
1210  /* Now fea, feature and value -- but how many are there ? */
1211  while (EST_ftell(fd) < preamble.data_offset-4)
1212  {
1213  esps_fea r = read_esps_fea(fd,hdr); /* feas */
1214  if (r == NULL) break;
1215 /* print_esps_fea(r); */
1216  r->next = hdr->fea;
1217  hdr->fea = r;
1218  if (r->type == 1)
1219  break; /* I think this (filename) is last FEA */
1220  }
1221  /* There's other gunk after this but I think I've done enough */
1222  /* The rest seems to be mostly previous headers */
1223 
1224  if (EST_fseek(fd,hdr->hdr_size,SEEK_SET) != 0) /* skip the rest of the header */
1225  {
1226  fprintf(stderr, "ESPS hdr: fseek error\n");
1227  delete_esps_hdr(hdr);
1228  wfree(hdr);
1229  return misc_read_error;
1230  }
1231  *uhdr = hdr;
1232 
1233  if (err == 1) {
1234  fprintf(stderr, "ESPS hdr: wrong format\n");
1235  return wrong_format;
1236  }
1237 
1238  return format_ok;
1239 }
1240 
1241 
1242 enum EST_write_status write_esps_hdr(esps_hdr hdr,FILE *fd)
1243 {
1244  /* well here's the scary part, try to write a valid file hdr to */
1245  /* the file */
1246  struct ESPS_PREAMBLE preamble;
1247  struct ESPS_FIXED_HDR fhdr;
1248  time_t tx = time(0);
1249  esps_fea t;
1250  int i,intdata;
1251  short shortdata;
1252 
1253  memset(&preamble,0,sizeof(preamble));
1254  memset(&fhdr,0,sizeof(fhdr));
1255  /* I can't really make the machine code work properly, so I'll */
1256  /* just fix it for the two major byte orders to Sun and Suni386 */
1257  if (EST_NATIVE_BO == bo_big)
1258  preamble.machine_code = 4; /* a sun */
1259  else
1260  preamble.machine_code = 6; /* a suni386 */
1261  preamble.check_code = 3000; /* ? */
1262  preamble.data_offset = 0; /* will come back and fix this later */
1263  preamble.record_size = esps_record_size(hdr);
1264  preamble.check = ESPS_MAGIC;
1265  preamble.edr = 0;
1266  preamble.fil1 = 0;
1267  preamble.foreign_hd = 0; /* docs say it should be -1, but its always 0 */
1268 
1269  fhdr.thirteen = 13; /* must be for luck */
1270  fhdr.sdr_size = 0;
1271  fhdr.magic = ESPS_MAGIC;
1272  strncpy(fhdr.date,ctime(&tx),26);
1273  fhdr.date[25] = '\0';
1274  sprintf(fhdr.version,"1.91"); /* that's what all the others have */
1275  sprintf(fhdr.prog,"EDST");
1276  sprintf(fhdr.vers,"0.1");
1277  strncpy(fhdr.progcompdate,ctime(&tx),26);
1278  fhdr.progcompdate[25] = '\0';
1279  fhdr.num_samples = hdr->num_records;
1280  fhdr.filler = 0;
1281  /* in each record */
1282  fhdr.num_doubles = esps_num_of_type(ESPS_DOUBLE,hdr);
1283  fhdr.num_floats = esps_num_of_type(ESPS_FLOAT,hdr);
1284  fhdr.num_ints = esps_num_of_type(ESPS_INT,hdr);
1285  fhdr.num_shorts = esps_num_of_type(ESPS_SHORT,hdr);
1286  fhdr.num_chars = esps_num_of_type(ESPS_CHAR,hdr);
1287  fhdr.fsize = 40;
1288  fhdr.hsize = 0; /* given value below on second shot */
1289  if (hdr->file_type == ESPS_SD)
1290  fhdr.fea_type = 8;
1291  else
1292  fhdr.fea_type = 0;
1293  fhdr.num_fields = hdr->num_fields;
1294 
1295 
1296  fwrite(&(preamble.machine_code),sizeof(int),1,fd);
1297  fwrite(&(preamble.check_code),sizeof(int),1,fd);
1298  fwrite(&(preamble.data_offset),sizeof(int),1,fd);
1299  fwrite(&(preamble.record_size),sizeof(int),1,fd);
1300  fwrite(&(preamble.check),sizeof(int),1,fd);
1301  fwrite(&(preamble.edr),sizeof(int),1,fd);
1302  fwrite(&(preamble.fil1),sizeof(int),1,fd);
1303  fwrite(&(preamble.foreign_hd),sizeof(int),1,fd);
1304 
1305 
1306  fwrite(&(fhdr.thirteen),sizeof(short),1,fd);
1307  fwrite(&(fhdr.sdr_size),sizeof(short),1,fd);
1308  fwrite(&(fhdr.magic),sizeof(int),1,fd);
1309  fwrite(&(fhdr.date),sizeof(char),26,fd);
1310  fwrite(&(fhdr.version),sizeof(char),8,fd);
1311  fwrite(&(fhdr.prog),sizeof(char),16,fd);
1312  fwrite(&(fhdr.vers),sizeof(char),8,fd);
1313  fwrite(&(fhdr.progcompdate),sizeof(char),26,fd);
1314  fwrite(&(fhdr.num_samples),sizeof(int),1,fd);
1315  fwrite(&(fhdr.filler),sizeof(int),1,fd);
1316  fwrite(&(fhdr.num_doubles),sizeof(int),1,fd);
1317  fwrite(&(fhdr.num_floats),sizeof(int),1,fd);
1318  fwrite(&(fhdr.num_ints),sizeof(int),1,fd);
1319  fwrite(&(fhdr.num_shorts),sizeof(int),1,fd);
1320  fwrite(&(fhdr.num_chars),sizeof(int),1,fd);
1321  fwrite(&(fhdr.fsize),sizeof(int),1,fd);
1322  fwrite(&(fhdr.hsize),sizeof(int),1,fd);
1323  fwrite(&(fhdr.username),sizeof(char),8,fd);
1324  fwrite(&(fhdr.fil1),sizeof(int),5,fd);
1325  fwrite(&(fhdr.fea_type),sizeof(short),1,fd);
1326  fwrite(&(fhdr.fil2),sizeof(short),1,fd);
1327  fwrite(&(fhdr.num_fields),sizeof(short),1,fd);
1328  fwrite(&(fhdr.fil3),sizeof(short),1,fd);
1329  fwrite(&(fhdr.fil4),sizeof(int),9,fd);
1330  fwrite(&(fhdr.fil5),sizeof(int),8,fd);
1331 
1332  /* The following cover dimensions, type and ordering info */
1333  for (i=0; i < hdr->num_fields; i++)
1334  { /* Dimensions (i.e. number of channels) */
1335  intdata = 1;
1336  fwrite(&intdata,4,1,fd); /* dimensions */
1337  }
1338  for (i=0; i < hdr->num_fields; i++) /* ordering info (?) */
1339  fwrite(&i,4,1,fd);
1340  if (hdr->file_type == ESPS_SD) /* zeros hmm should be zeroes only */
1341  shortdata = 1; /* is FEA case, 1 in ESPS_SD case */
1342  else /* fixed 24/7/98 */
1343  shortdata = 0;
1344  for (i=0; i < hdr->num_fields; i++)
1345  fwrite(&shortdata,2,1,fd);
1346  for (i=0; i < hdr->num_fields; i++)
1347  {
1348  shortdata = hdr->field_type[0]; /* field types */
1349  fwrite(&shortdata,2,1,fd);
1350  }
1351  intdata = fhdr.num_doubles; /* number of doubles */
1352  fwrite(&intdata,4,1,fd);
1353  intdata = fhdr.num_floats; /* number of floats */
1354  fwrite(&intdata,4,1,fd);
1355  intdata = fhdr.num_ints; /* number of ints */
1356  fwrite(&intdata,4,1,fd);
1357  intdata = fhdr.num_shorts; /* number of shorts */
1358  fwrite(&intdata,4,1,fd);
1359  intdata = fhdr.num_chars; /* number of chars */
1360  fwrite(&intdata,4,1,fd);
1361  shortdata = 0;
1362  for (i=0; i < 9; i++)
1363  fwrite(&shortdata,2,1,fd); /* other types */
1364  for (i=0; i < hdr->num_fields; i++)
1365  fwrite(&shortdata,2,1,fd); /* zeros */
1366  /* Now dump the filednames */
1367  for (i=0; i < hdr->num_fields; i++)
1368  esps_put_field_name(hdr->field_name[i],fd,hdr); /* field names */
1369  if (hdr->file_type != ESPS_SD)
1370  fwrite(&shortdata,2,1,fd); /* another 0 */
1371  /* Now the feas */
1372  for (t=hdr->fea; t != NULL; t=t->next)
1373  write_esps_fea(fd,t,hdr); /* feas */
1374  /* now have to go back and fix the header size */
1375  intdata = 0;
1376  fwrite(&intdata,4,1,fd);
1377  preamble.data_offset = ftell(fd);
1378  fhdr.hsize = (preamble.data_offset-249)/2;
1379  if (fseek(fd,0,SEEK_SET) == -1)
1380  {
1381  fprintf(stderr,"esps write header: can't fseek to start of file\n");
1382  return misc_write_error;
1383  }
1384  if (fwrite(&(preamble.machine_code),sizeof(int),1,fd) != 1) {
1385  fprintf(stderr, "esps write header: Write error\n");
1386  return misc_write_error;
1387  }
1388  if (fwrite(&(preamble.check_code),sizeof(int),1,fd) != 1) {
1389  fprintf(stderr, "esps write header: Write error\n");
1390  return misc_write_error;
1391  }
1392  if (fwrite(&(preamble.data_offset),sizeof(int),1,fd) != 1) {
1393  fprintf(stderr, "esps write header: Write error\n");
1394  return misc_write_error;
1395  }
1396  if (fwrite(&(preamble.record_size),sizeof(int),1,fd) != 1) {
1397  fprintf(stderr, "esps write header: Write error\n");
1398  return misc_write_error;
1399  }
1400  if (fwrite(&(preamble.check),sizeof(int),1,fd) != 1) {
1401  fprintf(stderr, "esps write header: Write error\n");
1402  return misc_write_error;
1403  }
1404  if (fwrite(&(preamble.edr),sizeof(int),1,fd) != 1) {
1405  fprintf(stderr, "esps write header: Write error\n");
1406  return misc_write_error;
1407  }
1408  if (fwrite(&(preamble.fil1),sizeof(int),1,fd) != 1) {
1409  fprintf(stderr, "esps write header: Write error\n");
1410  return misc_write_error;
1411  }
1412  if (fwrite(&(preamble.foreign_hd),sizeof(int),1,fd) != 1) {
1413  fprintf(stderr, "esps write header: Write error\n");
1414  return misc_write_error;
1415  }
1416 
1417 
1418  if (fwrite(&(fhdr.thirteen),sizeof(short),1,fd) != 1) {
1419  fprintf(stderr, "esps write header: Write error\n");
1420  return misc_write_error;
1421  }
1422  if (fwrite(&(fhdr.sdr_size),sizeof(short),1,fd) != 1) {
1423  fprintf(stderr, "esps write header: Write error\n");
1424  return misc_write_error;
1425  }
1426  if (fwrite(&(fhdr.magic),sizeof(int),1,fd) != 1) {
1427  fprintf(stderr, "esps write header: Write error\n");
1428  return misc_write_error;
1429  }
1430  if (fwrite(&(fhdr.date),sizeof(char),26,fd) != 26) {
1431  fprintf(stderr, "esps write header: Write error\n");
1432  return misc_write_error;
1433  }
1434  if (fwrite(&(fhdr.version),sizeof(char),8,fd) != 8) {
1435  fprintf(stderr, "esps write header: Write error\n");
1436  return misc_write_error;
1437  }
1438  if (fwrite(&(fhdr.prog),sizeof(char),16,fd) != 16) {
1439  fprintf(stderr, "esps write header: Write error\n");
1440  return misc_write_error;
1441  }
1442  if (fwrite(&(fhdr.vers),sizeof(char),8,fd) != 8) {
1443  fprintf(stderr, "esps write header: Write error\n");
1444  return misc_write_error;
1445  }
1446  if (fwrite(&(fhdr.progcompdate),sizeof(char),26,fd) != 26) {
1447  fprintf(stderr, "esps write header: Write error\n");
1448  return misc_write_error;
1449  }
1450  if (fwrite(&(fhdr.num_samples),sizeof(int),1,fd) != 1) {
1451  fprintf(stderr, "esps write header: Write error\n");
1452  return misc_write_error;
1453  }
1454  if (fwrite(&(fhdr.filler),sizeof(int),1,fd) != 1) {
1455  fprintf(stderr, "esps write header: Write error\n");
1456  return misc_write_error;
1457  }
1458  if (fwrite(&(fhdr.num_doubles),sizeof(int),1,fd) != 1) {
1459  fprintf(stderr, "esps write header: Write error\n");
1460  return misc_write_error;
1461  }
1462  if (fwrite(&(fhdr.num_floats),sizeof(int),1,fd) != 1) {
1463  fprintf(stderr, "esps write header: Write error\n");
1464  return misc_write_error;
1465  }
1466  if (fwrite(&(fhdr.num_ints),sizeof(int),1,fd) != 1) {
1467  fprintf(stderr, "esps write header: Write error\n");
1468  return misc_write_error;
1469  }
1470  if (fwrite(&(fhdr.num_shorts),sizeof(int),1,fd) != 1) {
1471  fprintf(stderr, "esps write header: Write error\n");
1472  return misc_write_error;
1473  }
1474  if (fwrite(&(fhdr.num_chars),sizeof(int),1,fd) != 1) {
1475  fprintf(stderr, "esps write header: Write error\n");
1476  return misc_write_error;
1477  }
1478  if (fwrite(&(fhdr.fsize),sizeof(int),1,fd) != 1) {
1479  fprintf(stderr, "esps write header: Write error\n");
1480  return misc_write_error;
1481  }
1482  if (fwrite(&(fhdr.hsize),sizeof(int),1,fd) != 1) {
1483  fprintf(stderr, "esps write header: Write error\n");
1484  return misc_write_error;
1485  }
1486  if (fwrite(&(fhdr.username),sizeof(char),8,fd) != 8) {
1487  fprintf(stderr, "esps write header: Write error\n");
1488  return misc_write_error;
1489  }
1490  if (fwrite(&(fhdr.fil1),sizeof(int),5,fd) != 5) {
1491  fprintf(stderr, "esps write header: Write error\n");
1492  return misc_write_error;
1493  }
1494  if (fwrite(&(fhdr.fea_type),sizeof(short),1,fd) != 1) {
1495  fprintf(stderr, "esps write header: Write error\n");
1496  return misc_write_error;
1497  }
1498  if (fwrite(&(fhdr.fil2),sizeof(short),1,fd) != 1) {
1499  fprintf(stderr, "esps write header: Write error\n");
1500  return misc_write_error;
1501  }
1502  if (fwrite(&(fhdr.num_fields),sizeof(short),1,fd) != 1) {
1503  fprintf(stderr, "esps write header: Write error\n");
1504  return misc_write_error;
1505  }
1506  if (fwrite(&(fhdr.fil3),sizeof(short),1,fd) != 1) {
1507  fprintf(stderr, "esps write header: Write error\n");
1508  return misc_write_error;
1509  }
1510  if (fwrite(&(fhdr.fil4),sizeof(int),9,fd) != 9) {
1511  fprintf(stderr, "esps write header: Write error\n");
1512  return misc_write_error;
1513  }
1514  if (fwrite(&(fhdr.fil5),sizeof(int),8,fd) != 8) {
1515  fprintf(stderr, "esps write header: Write error\n");
1516  return misc_write_error;
1517  }
1518  if (fseek(fd,preamble.data_offset,SEEK_SET) != 0) {
1519  fprintf(stderr, "esps write header: Write error\n");
1520  return misc_write_error;
1521  }
1522 
1523  return write_ok;
1524 }
#define ESPS_MAGIC
Definition: esps_utils.h:44
esps_rec new_esps_rec(const esps_hdr hdr)
Definition: esps_utils.cc:775
char * wstrdup(const char *s)
Definition: walloc.c:117
float end(const EST_Item &item)
Definition: EST_item_aux.cc:96
#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
#define SEEK_CUR
Definition: system.h:24
#define SWAPSHORT(x)
Definition: EST_cutils.h:79
EST_FilePos EST_ftell(FILE *fp)
Definition: EST_File.h:71
void print_esps_fea(esps_fea r)
Definition: esps_utils.cc:111
EST_write_status
#define SWAPINT(x)
Definition: EST_cutils.h:75
double get_field_d(esps_rec r, int field, int pos)
Definition: esps_utils.cc:907
int machine_code
Definition: esps_utils.h:46
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
char progcompdate[26]
Definition: esps_utils.h:63
int fea_value_f(const char *name, int pos, esps_hdr hdr, float *d)
Definition: esps_utils.cc:359
int fea_value_c(const char *name, int pos, esps_hdr hdr, char *d)
Definition: esps_utils.cc:422
#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
void add_fea_c(esps_hdr hdr, const char *name, int pos, char d)
Definition: esps_utils.cc:293
char vers[8]
Definition: esps_utils.h:62
char date[26]
Definition: esps_utils.h:59
#define ESPS_SHORT
Definition: esps_utils.h:144
void err(const char *message, LISP x) EST_NORETURN
Definition: slib.cc:608
void add_fea_f(esps_hdr hdr, const char *name, int pos, float d)
Definition: esps_utils.cc:197
short fea_type
Definition: esps_utils.h:75
#define SEEK_END
Definition: system.h:28
char version[8]
Definition: esps_utils.h:60
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 swapfloat(float *f)
Definition: EST_swapping.cc:53
short thirteen
Definition: esps_utils.h:56
#define wrong_format
int esps_record_size(esps_hdr hdr)
Definition: esps_utils.cc:948
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
esps_fea new_esps_fea()
Definition: esps_utils.cc:84
#define FALSE
Definition: EST_bool.h:119
esps_hdr new_esps_hdr(void)
Definition: esps_utils.cc:746
void swapdouble(double *d)
Definition: EST_swapping.cc:44
short sdr_size
Definition: esps_utils.h:57
char username[8]
Definition: esps_utils.h:73
#define misc_read_error
float time(const EST_Item &item)
Definition: EST_item_aux.cc:82
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
int EST_fseek(FILE *fp, EST_FilePos offset, int whence)
Definition: EST_File.h:75
#define ESPS_INT
Definition: esps_utils.h:143
char prog[16]
Definition: esps_utils.h:61
void set_field_s(esps_rec r, int field, int pos, short d)
Definition: esps_utils.cc:939
void set_field_f(esps_rec r, int field, int pos, float d)
Definition: esps_utils.cc:931
short num_fields
Definition: esps_utils.h:77
char * name
Definition: siodp.h:43
EST_read_status
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
esps_fea read_esps_fea(FILE *fd, esps_hdr hdr)
Definition: esps_utils.cc:549
#define format_ok
void write_esps_fea(FILE *fd, esps_fea t, esps_hdr hdr)
Definition: esps_utils.cc:467
#define EST_NATIVE_BO
Definition: EST_cutils.h:72
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
float sum(const EST_FMatrix &a)
sum of elements
Definition: vec_mat_aux.cc:147
#define TRUE
Definition: EST_bool.h:118
void set_field_i(esps_rec r, int field, int pos, int d)
Definition: esps_utils.cc:935
off_t EST_FilePos
Definition: EST_File.h:69
void delete_esps_hdr(esps_hdr h)
Definition: esps_utils.cc:760
int fea_value_i(const char *name, int pos, esps_hdr hdr, int *d)
Definition: esps_utils.cc:401
void add_field(esps_hdr hdr, const char *name, int type, int dimension)
Definition: esps_utils.cc:138
void set_field_c(esps_rec r, int field, int pos, char d)
Definition: esps_utils.cc:943
esps_hdr make_esps_sd_hdr(void)
Definition: esps_utils.cc:971
void add_fea_s(esps_hdr hdr, const char *name, int pos, short d)
Definition: esps_utils.cc:264
void delete_esps_fea(esps_fea r)
Definition: esps_utils.cc:96
#define SEEK_SET
Definition: system.h:20
int write_esps_rec(esps_rec r, esps_hdr h, FILE *fd)
Definition: esps_utils.cc:512