Edinburgh Speech Tools  2.1-release
EST_TrackFile.cc
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 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 */
34 /* Date : August 1995 */
35 /*-----------------------------------------------------------------------*/
36 /* File I/O functions for EST_Track class */
37 /* */
38 /*=======================================================================*/
39 #include <fstream>
40 #include <iostream>
41 #include <sstream> /* for a workaround in printf with size_t */
42 #include <cstdlib>
43 #include <cmath>
44 #include <time.h>
45 #include "EST_unix.h"
46 #include "EST_types.h"
47 #include "EST_Track.h"
48 #include "EST_track_aux.h"
49 #include "EST_TrackMap.h"
50 #include "EST_cutils.h"
51 #include "EST_Token.h"
52 #include "EST_TList.h"
53 #include "EST_string_aux.h"
54 #include "EST_walloc.h"
55 #include "EST_TrackFile.h"
56 #include "EST_FileType.h"
57 #include "EST_WaveFile.h"
58 #include "EST_wave_utils.h"
59 
60 using namespace std;
61 
62 // size of locally allocated buffer. If more channels than this we have to
63 // call new
64 
65 #define NEARLY_ZERO 0.00001
66 
67 #define REASONABLE_FRAME_SIZE (20)
68 #define UNREASONABLE_FRAME_SIZE (80)
69 
70 #if 0
71 static const char *NIST_SIG = "NIST_1A\n 1024\n";
72 static const char *NIST_END_SIG = "end_head\n";
73 #define NIST_HDR_SIZE 1024
74 // default for tracks is the standard EMA sample rate
75 static int def_load_sample_rate = 500;
76 
77 #endif
78 
79 // some functions written for reading NIST headered waveform files,
80 // but useful here.
81 int nist_get_param_int(char *hdr, char *field, int def_val);
82 char *nist_get_param_str(char *hdr, char *field, char *def_val);
83 const char *sample_type_to_nist(enum EST_sample_type_t sample_type);
84 enum EST_sample_type_t nist_to_sample_type(char *type);
85 
87  bool &ascii, EST_EstFileType &t);
88 
89 EST_read_status EST_TrackFile::load_esps(const EST_String filename, EST_Track &tr, float ishift, float startt)
90 {
91  (void)ishift;
92  (void)startt;
93 
94  float **tt;
95  float fsize;
96  char **fields;
97  int num_points, num_fields, num_values;
98  ssize_t i,j;
99  EST_read_status r_val;
100  short fixed;
101  int first_channel=0;
102 
103  r_val = get_track_esps(filename, &fields, &tt, &fsize, &num_points,
104  &num_values, &fixed);
105  if (r_val == misc_read_error)
106  {
107  cerr << "Error reading ESPS file " << filename << endl;
108  return misc_read_error;
109  }
110  else if (r_val == wrong_format)
111  return wrong_format;
112 
113  num_fields = num_values;
114  if (!fixed)
115  {
116  --num_fields;
117  ++first_channel;
118  }
119 
120  tr.resize(num_points,num_fields);
121  tr.fill_time(fsize);
122 
123  for (i = 0; i < num_points; ++i)
124  {
125  for (j = 0; j < num_fields; ++j)
126  tr.a(i, j) = tt[i][j+first_channel];
127  tr.set_value(i);
128  if (!fixed)
129  tr.t(i) = tt[i][0];
130  }
131 
132  for (i = 0; i < num_fields; ++i)
133  tr.set_channel_name(fields[i+first_channel], i);
134 
135 
136  // REORG not sure what these should be -- awb
137  tr.set_single_break(false);
138  tr.set_equal_space(true);
139 
140  // get_track_esps allocs all the memory, we therefore need to release it
141  for (i = 0; i < num_values; ++i)
142  wfree(fields[i]);
143  wfree(fields);
144  for (i = 0; i < num_values; ++i)
145  wfree(tt[i]);
146  wfree(tt);
147 
149  tr.set_name(filename);
150 
151  if (tr.channel_name(0) == "F0")
152  espsf0_to_track(tr);
153 
154  return format_ok;
155 }
156 
157 EST_read_status EST_TrackFile::load_ascii(const EST_String filename, EST_Track &tr, float ishift, float startt)
158 {
159  (void)startt;
160 
161  EST_TokenStream ts, tt;
162  EST_StrList sl;
163 
164  ssize_t i, j, n_rows, n_cols=0;
165  EST_String t;
166  EST_Litem *p;
167 
168  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
169  {
170  cerr << "Can't open track file " << filename << endl;
171  return misc_read_error;
172  }
173  // set up the character constant values for this stream
174  ts.set_SingleCharSymbols(";");
175 
176  if (ishift < NEARLY_ZERO)
177  {
178  cerr<<
179  "Error: Frame spacing must be specified (or apparent frame shift nearly zero)\n";
180  return misc_read_error;
181  }
182 
183  // first read in as list
184 
185  for (n_rows = 0; !ts.eof(); ++n_rows)
186  sl.append(ts.get_upto_eoln().string());
187 
188  if (n_rows > 0)
189  {
190  tt.open_string(sl.first());
191  for (n_cols = 0; !tt.eof(); ++n_cols)
192  tt.get().string();
193  }
194 
195  // resize track and copy values in
196  tr.resize(n_rows, n_cols);
197 
198  for (p = sl.head(), i = 0; p != 0; ++i, p = p->next())
199  {
200  bool ok;
201  tt.open_string(sl(p));
202  for (j = 0; !tt.eof(); ++j)
203  tr.a(i, j) = tt.get().Float(ok);
204  if (j != n_cols)
205  {
206  cerr << "Wrong number of points in row " << i << endl;
207  cerr << "Expected " << n_cols << " got " << j << endl;
208  return misc_read_error;
209  }
210  }
211 
212  tr.fill_time(ishift);
214  tr.set_equal_space(TRUE);
216  tr.set_name(filename);
217 
218  return format_ok;
219 }
220 
221 EST_read_status EST_TrackFile::load_xgraph(const EST_String filename, EST_Track &tr, float ishift, float startt)
222 {
223  (void)ishift;
224  (void)startt;
225 
226  EST_TokenStream ts, tt;
227  EST_StrList sl;
228  // const float NEARLY_ZERO = 0.001;
229  ssize_t i, j, n_rows, n_cols;
230  EST_String t;
231  EST_Litem *p;
232 
233  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
234  {
235  cerr << "Can't open track file " << filename << endl;
236  return misc_read_error;
237  }
238  // set up the character constant values for this stream
239  ts.set_SingleCharSymbols(";");
240 
241  // first read in as list
242 
243  for (n_rows = 0; !ts.eof(); ++n_rows)
244  sl.append(ts.get_upto_eoln().string());
245 
246  tt.open_string(sl.first());
247  for (n_cols = 0; !tt.eof(); ++n_cols)
248  tt.get().string();
249 
250  --n_cols; // first column is time marks
251 
252  // resize track and copy values in
253  tr.resize(n_rows, n_cols);
254 
255  for (p = sl.head(), i = 0; p != 0; ++i, p = p->next())
256  {
257  bool ok;
258  tt.open_string(sl(p));
259  tr.t(i) = tt.get().Float(ok);
260  for (j = 0; !tt.eof(); ++j)
261  tr.a(i, j) = tt.get().Float(ok);
262  if (j != n_cols)
263  {
264  cerr << "Wrong number of points in row " << i << endl;
265  cerr << "Expected " << n_cols << " got " << j << endl;
266  return misc_read_error;
267  }
268  }
269 
271  tr.set_equal_space(TRUE);
273  tr.set_name(filename);
274 
275  return format_ok;
276 }
277 
278 EST_read_status EST_TrackFile::load_xmg(const EST_String filename, EST_Track &tr, float ishift, float startt)
279 {
280  (void)ishift;
281  (void)startt;
282 
283  EST_TokenStream ts;
284  EST_StrList sl;
285  ssize_t i, n;
286  /* int sr; (unused)*/
287 
288  EST_String t, k, v;
289  EST_Litem *p;
290 
291  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
292  {
293  cerr << "Can't open track file " << filename << endl;
294  return misc_read_error;
295  }
296  // set up the character constant values for this stream
297  ts.set_SingleCharSymbols(";");
298 
299  if (ts.peek().string() != "XAO1")
300  return wrong_format;
301 
302  ts.get().string();
303 
304  while ((!ts.eof()) && (ts.peek().string() != "\014"))
305  {
306  k = ts.get().string();
307  v = ts.get().string();
308  /*if (k == "Freq") {
309  sr = v.Int() * 1000;
310  } else */ if (k == "YMin") {
311  /* tr.amin = atof(v) */;
312  } else if (k == "YMax") {
313  /*tr.amax = atof(v) */;
314  }
315  }
316 
317  if (ts.eof())
318  {
319  cerr << "Unexpected end of file in reading xmg header\n";
320  return misc_read_error;
321  }
322  ts.get().string(); // read control L
323  ts.get_upto_eoln().string(); // read until end of header
324 
325  // read in lines to a list
326  for (n = 0; !ts.eof(); ++n)
327  sl.append(ts.get_upto_eoln().string());
328 
329  // note the track size is total number of points *and* breaks
330  tr.resize(n, 1 ); // REORG - fix this for multi channel work
331 
332  for (p = sl.head(), i = 0; p != 0; ++i, p = p->next())
333  {
334  bool ok;
335  ts.open_string(sl(p));
336  if (ts.peek().string() != "=")
337  {
338  tr.t(i) = ts.get().Float(ok) / 1000.0;
339  tr.a(i) = ts.get().Float(ok);
340  }
341  else
342  {
343  ts.get().string();
344  tr.set_break(i);
345  }
346  }
347 
351  tr.set_name(filename);
352 
353  return format_ok;
354 }
355 
357  EST_Track &tr, float ishift, float startt)
358 {
359  EST_TokenStream ts;
360  EST_read_status r;
361 
362  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
363  {
364  cerr << "Can't open track file " << filename << endl;
365  return misc_read_error;
366  }
367  // set up the character constant values for this stream
368  ts.set_SingleCharSymbols(";");
369  tr.set_name(filename);
370  r = load_est_ts(ts, tr, ishift, startt);
371 
372  if ((r == format_ok) && (!ts.eof()))
373  {
374  cerr << "Not end of file, but expected it\n";
375  return misc_read_error;
376  }
377  else
378  return r;
379 }
380 
381 static float get_float(EST_TokenStream &ts,int swap)
382 {
383  float f;
384  if (ts.fread(&f,4,1) != 1)
385  {
386  cerr << "Could not get_float" << endl;
387  return 0.0;
388  }
389  if (swap) swapfloat(&f);
390  return f;
391 }
392 
394  EST_Track &tr, float ishift, float startt)
395 {
396  (void)ishift;
397  (void)startt;
398  ssize_t i, j;
399  ssize_t num_frames, num_channels/*, num_aux_channels*/;
400  EST_Features hinfo;
401  EST_EstFileType t;
402  EST_String v;
403  bool ascii;
404  bool breaks;
405  bool eq_space;
406  EST_read_status r;
407  int swap;
408 
409  if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok)
410  return r;
411  if (t != est_file_track)
412  return misc_read_error;
413 
414  breaks = hinfo.present("BreaksPresent") ? true : false;
415  eq_space = false;
416  if ((hinfo.present("EqualSpace")) &&
417  ((hinfo.S("EqualSpace") == "true") ||
418  (hinfo.S("EqualSpace") == "1")))
419  eq_space = true;
420 
421  num_frames = hinfo.I("NumFrames");
422  num_channels = hinfo.I("NumChannels");
423  /*num_aux_channels = hinfo.I("NumAuxChannels", 0);*/
424  tr.resize(num_frames, num_channels);
425 
426  hinfo.remove("NumFrames");
427  hinfo.remove("EqualSpace");
428  hinfo.remove("NumChannels");
429  hinfo.remove("BreaksPresent");
430  hinfo.remove("DataType");
431  if (hinfo.present("NumAuxChannels"))
432  hinfo.remove("NumAuxChannels");
433 
434  EST_String strn, cname;
435 
437  EST_StrList ch_map;
438 
439  for (p.begin(hinfo); p;)
440  {
441  c = p++;
442 
443  if (c->k.contains("Aux_Channel_"))
444  {
445  ch_map.append(c->v.String());
446  hinfo.remove(c->k);
447  }
448  else if (c->k.contains("Channel_"))
449  {
450  tr.set_channel_name(c->v.String(),
451  c->k.after("Channel_").Int());
452  hinfo.remove(c->k);
453  }
454  }
455 
456  tr.resize_aux(ch_map);
457 
458 // tr.create_map();
459 
460 // if (((hinfo.S("ByteOrder", "") == "01") ? bo_little : bo_big)
461 
462  if (!hinfo.present("ByteOrder"))
463  swap = FALSE; // ascii or not there for some reason
464  else if (((hinfo.S("ByteOrder") == "01") ? bo_little : bo_big)
465  != EST_NATIVE_BO)
466  swap = TRUE;
467  else
468  swap = FALSE;
469 
470  const int BINARY_CHANNEL_BUFFER_SIZE=1024;
471  float *frame=0;
472  float frame_buffer[BINARY_CHANNEL_BUFFER_SIZE];
473  if( !ascii )
474  {
475  if( num_channels > BINARY_CHANNEL_BUFFER_SIZE )
476  frame = new float[num_channels];
477  else
478  frame = frame_buffer;
479  }
480 
481  // there are too many ifs here
482  for (i = 0; i < num_frames; ++i)
483  {
484  bool ok;
485 
486  // Read Times
487  if (ascii)
488  {
489  if (ts.eof())
490  {
491  cerr << "unexpected end of file when looking for " << num_frames-i << " more frame(s)" << endl;
492  return misc_read_error;
493  }
494  tr.t(i) = ts.get().Float(ok);
495  if (!ok)
496  return misc_read_error;
497  }
498  else
499  tr.t(i) = get_float(ts,swap);
500 
501  // Read Breaks
502  if (breaks)
503  {
504  if (ascii)
505  {
506  v = ts.get().string();
507  if (v == "0")
508  tr.set_break(i);
509  else
510  tr.set_value(i);
511  }
512  else
513  {
514  if (get_float(ts,swap) == 0.0)
515  tr.set_break(i);
516  else
517  tr.set_value(i);
518  }
519  }
520  else
521  tr.set_value(i);
522 
523  // Read Channels
524 // for (j = 0; j < num_channels; ++j)
525 // {
526 // if(ascii)
527 // {
528 // tr.a(i, j) = ts.get().Float(ok);
529 // if (!ok)
530 // return misc_read_error;
531 // }
532 // else
533 // tr.a(i,j) = get_float(ts,swap);
534 // }
535 
536  if( ascii ){
537  for (j = 0; j < num_channels; ++j){
538  tr.a(i, j) = ts.get().Float(ok);
539  if (!ok)
540  return misc_read_error;
541  }
542  }
543  else{
544  if (ts.fread( frame, sizeof(float), num_channels ) != num_channels)
545  {
546  cerr << "Could not read frame " << i << "/" << num_frames << endl;
547  return misc_read_error;
548  }
549  if( swap )
550  for( j=0; j<num_channels; ++j ){
551  swapfloat( &frame[j] );
552  tr.a(i,j) = frame[j];
553  }
554  else
555  for( j=0; j<num_channels; ++j )
556  tr.a(i,j) = frame[j];
557  }
558 
559 
560  // Read aux Channels
561  for (j = 0; j < tr.num_aux_channels(); ++j)
562  {
563  if (ascii)
564  {
565  tr.aux(i, j) = ts.get().string();
566  /*if (!ok)
567  return misc_read_error;*/
568  }
569  else
570  {
571  cerr << "Warning: Aux Channel reading not yet implemented";
572  cerr << "for binary tracks\n";
573  }
574  }
575  }
576 
577  if( !ascii )
578  if( frame != frame_buffer )
579  delete [] frame;
580 
581  // copy header info into track
582  tr.f_set(hinfo);
583 
585  tr.set_equal_space(eq_space);
586 
587  if(ascii)
589  else
591 
592  return format_ok;
593 }
594 
596  float ishift, float startt)
597 {
598  (void)startt;
599 
600  EST_TokenStream ts, str;
601  EST_StrList sl;
602  ssize_t i, j;
603  EST_String t, k, v;
604 
605  if (ishift < NEARLY_ZERO)
606  {
607  cerr<<
608  "Error: Frame spacing must be specified (or apparent frame shift nearly zero)\n";
609  return misc_read_error;
610  }
611 
612  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
613  {
614  cerr << "Can't open track file " << filename << endl;
615  return misc_read_error;
616  }
617 
618  if (ts.get().string() != "SNNS")
619  return wrong_format;
620  if (ts.get().string() != "result")
621  return wrong_format;
622 
623  ts.get_upto_eoln(); // SNNS bit
624  ts.get_upto_eoln(); // Time info
625 
626  ssize_t num_frames=0, num_channels=0;
627  int teaching = 0;
628 
629  while (1)
630  {
631  t = (EST_String)ts.get_upto_eoln();
632  // cout << "t=" << t << endl;
633  if (t.contains("teaching output included"))
634  teaching = 1;
635  if (!t.contains(":"))
636  break;
637  str.open_string(t);
638  k = (EST_String)str.get_upto(":");
639  v = (EST_String)str.get_upto_eoln();
640  if (k == "No. of output units")
641  num_channels = v.Int();
642  if (k == "No. of patterns")
643  num_frames = v.Int();
644 
645  // cout << "key " << k << endl;
646  // cout << "val " << v << endl;
647  }
648 
649  // cout << "num_frames = " << num_frames << endl;
650  // cout << "num_channels = " << num_channels << endl;
651  tr.resize(num_frames, num_channels);
652  // cout << "peek" << ts.peek().string() << endl;
653  // cout << "teaching " << teaching << endl;
654 
655  for (i = 0; (!ts.eof()) && (i < num_frames);)
656  // for (i = 0; i < 10; ++i)
657  {
658  if (ts.peek().string().contains("#")) // comment
659  {
660  ts.get_upto_eoln();
661  continue;
662  }
663  if (teaching) // get rid of teaching patterns
664  for (j = 0; j < num_channels; ++j)
665  ts.get().string();
666 
667  // cout << "i = " << i << " t = " << ts.peek().string() << endl;
668 
669  bool ok;
670 
671  for (j = 0; j < num_channels; ++j)
672  tr.a(i, j) = ts.get().Float(ok);
673 
674  ++i;
675  }
676 
677  tr.fill_time(ishift);
679  tr.set_equal_space(TRUE);
681  tr.set_name(filename);
682 
683  return format_ok;
684 }
685 
687 {
688  EST_write_status rc;
689  ssize_t i, j;
690  float shift;
691  bool include_time;
692  int extra_channels=0;
693 
694  EST_Track &track_tosave = tr;
695 
696  if (filename == "-")
697  {
698  cerr << "Output to stdout not available for ESPS file types:";
699  cerr << "no output written\n";
700  return write_fail;
701  }
702 
703  if ((include_time = (track_tosave.equal_space() != TRUE)))
704  {
706  extra_channels++;
707  }
708  else
709  shift = track_tosave.shift();
710 
711  track_tosave.change_type(0.0,FALSE);
712 
713  float **a = new float*[track_tosave.num_frames()];
714  // pity we need to copy it
715  for (i=0; i < track_tosave.num_frames(); i++)
716  {
717  a[i] = new float[track_tosave.num_channels() + extra_channels];
718 
719  if (include_time)
720  a[i][0] = track_tosave.t(i);
721 
722  for (j=0; j < track_tosave.num_channels(); j++)
723  a[i][j + extra_channels] = track_tosave.a(i,j);
724  }
725 
726  char **f_names = new char*[track_tosave.num_channels() + extra_channels];
727  for (i=0; i < track_tosave.num_channels(); i++)
728  {
729  // cout << "field " << i << "is '" << track_tosave.field_name(i) << "'\n";
730  f_names[i + extra_channels] = wstrdup(track_tosave.channel_name(i, esps_channel_names, 0));
731  }
732 
733  if (include_time)
734  f_names[0] = wstrdup("EST_TIME");
735 
736  rc = put_track_esps(filename, f_names,
737  a, shift, 1.0/shift,
738  track_tosave.num_channels() + extra_channels,
739  track_tosave.num_frames(),
740  !include_time);
741 
742  for (i=0; i < track_tosave.num_frames(); i ++)
743  delete [] a[i];
744  delete [] a;
745  for (i=0; i < track_tosave.num_channels()+extra_channels; i++)
746  delete [] f_names[i];
747  delete [] f_names;
748 
749  return rc;
750 }
751 
753 {
754  /* size_t does not have a ISO C++ printf format specifier (%zd is
755  * not standard for C++ 1998 nor C++ 2003). We use a workaround. */
756  std::stringstream tmpstring;
757  std::string tmpstring2;
758 
759  fprintf(fp, "EST_File Track\n"); // EST header identifier.
760  fprintf(fp, "DataType ascii\n");
761  tmpstring << tr.num_frames();
762  tmpstring2 = tmpstring.str();
763  fprintf(fp, "NumFrames %s\n", tmpstring2.c_str());
764  fprintf(fp, "NumChannels %d\n", tr.num_channels());
765  fprintf(fp, "NumAuxChannels %d\n", tr.num_aux_channels());
766  fprintf(fp, "EqualSpace %d\n",tr.equal_space());
767 
768  fprintf(fp, "BreaksPresent true\n");
769  for (int i = 0; i < tr.num_channels(); ++i)
770  fprintf(fp, "Channel_%d %s\n", i, (const char *)(tr.channel_name(i)));
771 
772  for (int i = 0; i < tr.num_aux_channels(); ++i)
773  fprintf(fp, "Aux_Channel_%d %s\n", i,
774  (const char *)(tr.aux_channel_name(i)));
775 
777 
778  for (p.begin(tr); p; ++p)
779  fprintf(fp, "%s %s\n", (const char *)p->k,
780  (const char *) p->v.String());
781 
782  fprintf(fp, "EST_Header_End\n");
783 
784  for (ssize_t i = 0; i < tr.num_frames(); ++i)
785  {
786  fprintf(fp, "%f\t", tr.t(i));
787  fprintf(fp, "%s\t", (char *)(tr.val(i) ? "1 " : "0 "));
788  for (int j = 0; j < tr.num_channels(); ++j)
789  fprintf(fp, "%f ", tr.a_no_check(i, j));
790  for (int j = 0; j < tr.num_aux_channels(); ++j)
791  fprintf(fp, "%s ", (const char *)tr.aux(i, j).string());
792  fprintf(fp, "\n");
793  }
794  return write_ok;
795 }
796 
798  EST_Track& tr)
799 {
800  FILE *fd;
802 
803  if (filename == "-")
804  fd = stdout;
805  else if ((fd = fopen(filename,"wb")) == NULL)
806  return write_fail;
807 
808  r = save_est_ts(fd,tr);
809 
810  if (fd != stdout)
811  fclose(fd);
812  return r;
813 }
814 
816 {
817  FILE *fd;
819 
820  if (filename == "-")
821  fd = stdout;
822  else if ((fd = fopen(filename,"wb")) == NULL)
823  return write_fail;
824 
825  r = save_est_binary_ts(fd,tr);
826 
827  if (fd != stdout)
828  fclose(fd);
829  return r;
830 
831 }
832 
834 {
835  // This should be made optional
836  bool breaks = TRUE;
837  /* size_t does not have a ISO C++ printf format specifier (%zd is
838  * not standard for C++ 1998 nor C++ 2003). We use a workaround. */
839  std::stringstream tmpstring;
840  std::string tmpstring2;
841 
842  fprintf(fp, "EST_File Track\n");
843  fprintf(fp, "DataType binary\n");
844  fprintf(fp, "ByteOrder %s\n", ((EST_NATIVE_BO == bo_big) ? "10" : "01"));
845  tmpstring.str("");
846  tmpstring << tr.num_frames();
847  tmpstring2 = tmpstring.str();
848  fprintf(fp, "NumFrames %s\n", tmpstring2.c_str());
849  fprintf(fp, "NumChannels %d\n",tr.num_channels());
850  fprintf(fp, "EqualSpace %d\n",tr.equal_space());
851  if(breaks)
852  fprintf(fp, "BreaksPresent true\n");
853  fprintf(fp, "CommentChar ;\n\n");
854  for (int i = 0; i < tr.num_channels(); ++i)
855  fprintf(fp, "Channel_%d %s\n",i,tr.channel_name(i).str());
856  fprintf(fp, "EST_Header_End\n");
857 
858  for (ssize_t i = 0; i < tr.num_frames(); ++i)
859  {
860  // time
861  if((int)fwrite(&tr.t(i),4,1,fp) != 1)
862  return misc_write_error;
863 
864  // break marker
865  if (breaks)
866  {
867  float bm = (tr.val(i) ? 1 : 0);
868  if((int)fwrite(&bm,4,1,fp) != 1)
869  return misc_write_error;
870  }
871  // data - restricted to floats at this time
872  for (int j = 0; j < tr.num_channels(); ++j)
873  if(fwrite(&tr.a_no_check(i, j),4,1,fp) != 1)
874  return misc_write_error;
875 
876  }
877  return write_ok;
878 }
879 
881 {
882 
883  if (tr.equal_space() == TRUE)
884  tr.change_type(0.0, FALSE);
885 
886  ostream *outf;
887  if (filename == "-")
888  outf = &cout;
889  else
890  outf = new ofstream(filename);
891 
892  if (!(*outf))
893  return write_fail;
894 
895  outf->precision(5);
896  outf->setf(ios::fixed, ios::floatfield);
897  outf->width(8);
898 
899  for (ssize_t i = 0; i < tr.num_frames(); ++i)
900  {
901  for (ssize_t j = 0; j < tr.num_channels(); ++j)
902  *outf << tr.a(i, j) << " ";
903  *outf << endl;
904  }
905 
906  if (outf != &cout)
907  delete outf;
908 
909  return write_ok;
910 }
911 
913 {
914 
915  ostream *outf;
916 
917  if (filename == "-")
918  outf = &cout;
919  else
920  outf = new ofstream(filename);
921 
922  if (!(*outf))
923  return write_fail;
924 
925  tr.change_type(0.0, TRUE);
926 
927  for (ssize_t j = 0; j < tr.num_channels(); ++j)
928  {
929  *outf << "\""<< tr.channel_name(j) << "\"\n";
930  for (ssize_t i = 0; i < tr.num_frames(); ++i)
931  if (tr.val(i))
932  *outf << tr.t(i) << "\t" << tr.a(i, j) << endl;
933  else
934  *outf << "move ";
935  }
936  if (outf != &cout)
937  delete outf;
938 
939  return write_ok;
940 }
941 
943  EST_TrackList &inpat, EST_TrackList &outpat)
944 {
945  ostream *outf;
946  int num_inputs, num_outputs, num_pats;
947  ssize_t i;
948  EST_Litem *pi, *po;
949 
950  if (filename == "-")
951  outf = &cout;
952  else
953  outf = new ofstream(filename);
954 
955  if (!(*outf))
956  return write_fail;
957 
958  num_pats = 0;
959  for (pi = inpat.head(); pi ; pi = pi->next())
960  num_pats += inpat(pi).num_frames();
961 
962  *outf << "SNNS pattern definition file V3.2\n";
963 
964  time_t thetime = time(0);
965  char *date = ctime(&thetime);
966 
967  *outf << date;
968  *outf << endl;
969 
970  num_inputs = inpat.first().num_channels();
971  num_outputs = outpat.first().num_channels();
972 
973  *outf << "No. of patterns : " << num_pats << endl;
974  *outf << "No. of input units : "<< num_inputs << endl;
975  *outf << "No. of output units : "<< num_outputs << endl;
976  *outf << endl << endl;
977 
978  for (pi = inpat.head(), po = outpat.head(); pi ;
979  pi = pi->next(), po = po->next())
980  {
981  if (inpat(pi).num_frames() != outpat(pi).num_frames())
982  {
983  cerr << "Error: Input pattern has " << inpat(pi).num_frames()
984  << " output pattern has " << outpat(pi).num_frames() << endl;
985  if (outf != &cout)
986  delete outf;
987  return misc_write_error;
988  }
989  for (i = 0; i < inpat(pi).num_frames(); ++i)
990  {
991  ssize_t j;
992  *outf << "#Input pattern " << (i + 1) << ":\n";
993  for (j = 0; j < inpat(pi).num_channels(); ++j)
994  *outf << inpat(pi).a(i, j) << " ";
995  *outf << endl;
996  *outf << "#Output pattern " << (i + 1) << ":\n";
997  for (j = 0; j < outpat(po).num_channels(); ++j)
998  *outf << outpat(po).a(i, j) << " ";
999  *outf << endl;
1000  }
1001  }
1002  if (outf != &cout)
1003  delete outf;
1004 
1005  return write_ok;
1006 }
1007 
1008 /*
1009  EST_write_status EST_TrackFile::save_snns_pat(const EST_String filename,
1010  EST_TrackList &trlist)
1011  {
1012  ostream *outf;
1013  int num_inputs, num_outputs, i;
1014  EST_Litem *p;
1015 
1016  if (filename == "-")
1017  outf = &cout;
1018  else
1019  outf = new ofstream(filename);
1020 
1021  if (!(*outf))
1022  return write_fail;
1023 
1024  *outf << "SNNS pattern definition file V3.2\n";
1025 
1026  char *date;
1027  date = ctime(clock());
1028 
1029  *cout << date << endl;
1030 
1031  *cout << endl << endl;
1032 
1033  num_inputs = tr.first.num_channels();
1034  num_outputs = tr.first.num_channels();
1035 
1036  *cout << "No. of patterns : " << tr.size() << endl;
1037  *cout << "No. of input units : "<< num_inputs << endl;
1038  *cout << "No. of output units : "<< num_outputs << endl;
1039 
1040  for (i = 0, p = trlist.head(); p ; p = p->next(), ++i)
1041  {
1042  *outf << "#Input pattern " << i << ":\n";
1043  for (int j = 0; j < num_inputs; ++j)
1044  *outf << << trlist(p)._name(j) << "\"\n";
1045  for (ssize_t i = 0; i < tr.num_frames(); ++i)
1046  if (tr.val(i))
1047  *outf << tr.t(i) << "\t" << tr.a(i, j) << endl;
1048  else
1049  *outf << "move ";
1050  }
1051  if (outf != &cout)
1052  delete outf;
1053 
1054  return write_ok;
1055  }
1056  */
1057 
1059 {
1060  ostream *outf;
1061  ssize_t i, j;
1062  // float min, max;
1063  int sr = 16000; // REORG - fixed sample rate until xmg is fixed
1064 
1065  // this takes care of rescaling
1066  tr.change_type(0.0, TRUE);
1067 
1068  if (filename == "-")
1069  outf = &cout;
1070  else
1071  outf = new ofstream(filename);
1072 
1073  if (!(*outf))
1074  return write_fail;
1075 
1076  outf->precision(5);
1077  outf->setf(ios::fixed, ios::floatfield);
1078  outf->width(8);
1079 
1080 /* min = max = tr.a(0);
1081  for (i = 0; i < tr.num_frames(); ++i)
1082  {
1083  if (tr.a(i) > max) max = tr.a(i);
1084  if (tr.a(i) < min) min = tr.a(i);
1085  }
1086 */
1087  *outf << "XAO1\n\n"; // xmg header identifier.
1088  *outf << "LineType segments \n";
1089  *outf << "LineStyle solid \n";
1090  *outf << "LineWidth 0 \n";
1091  *outf << "Freq " << sr / 1000 << endl; // a REAL pain!
1092  *outf << "Format Binary \n";
1093  // *outf << "YMin " << ((tr.amin != 0.0) ? tr.amin : min) << endl;
1094  // *outf << "YMax " << ((tr.amax != 0.0) ? tr.amax : max) << endl;
1095  /* if (tr.color != "")
1096  *outf << "LineColor " << tr.color << endl;
1097  */
1098  *outf << char(12) << "\n"; // control L character
1099 
1100  // rm_excess_breaks();
1101  // rm_trailing_breaks();
1102  for (i = 0; i < tr.num_frames(); ++i)
1103  if (tr.val(i))
1104  {
1105  *outf << tr.ms_t(i) << "\t";
1106  for (j = 0; j < tr.num_channels(); ++j)
1107  *outf <<tr.a(i, j) << " ";
1108  *outf << endl;
1109  }
1110  else
1111  *outf << "=\n";
1112  if (outf != &cout)
1113  delete outf;
1114 
1115  return write_ok;
1116 }
1117 
1118 static EST_write_status save_htk_as(const EST_String filename,
1119  EST_Track &orig,
1120  int use_type)
1121 {
1122  // file format is a 12 byte header
1123  // followed by data
1124 
1125  // the data is generally floats, except for DISCRETE
1126  // where it is 2 byte ints
1127 
1128  float s;
1129 
1130  EST_Track track;
1131  int type;
1132  int file_num_channels = orig.num_channels();
1133 
1134  if (orig.f_String("contour_type","none") == "ct_lpc")
1135  type = track_to_htk_lpc(orig, track);
1136  else
1137  {
1138  track = orig;
1139  type = use_type;
1140  }
1141 
1142  if (track.equal_space() != TRUE)
1143  {
1144  track.change_type(0.0, FALSE);
1145  s = rint((HTK_UNITS_PER_SECOND * EST_Track::default_frame_shift/1000.0)/10.0) * 10.0;
1146  type |= HTK_EST_PS;
1147  file_num_channels += 1;
1148  }
1149  else
1150  {
1151  track.change_type(0.0, FALSE);
1152  s = rint((HTK_UNITS_PER_SECOND * track.shift())/10.0) * 10.0;
1153  }
1154 
1155  // hkt files need to be big_endian irrespective of hardware. The
1156  // code here was obviously only ever ran on a Sun. I've tried to
1157  // fix this and it seems to work with floats, don't have data to
1158  // check with shorts though. (Rob, March 2004)
1159 
1160  struct htk_header header;
1161 
1162  header.num_samps = (EST_BIG_ENDIAN ? track.num_frames()
1163  : SWAPINT(track.num_frames()));
1164 
1165 
1166  header.samp_period = (EST_BIG_ENDIAN ? (long) s : SWAPINT((long) s));
1167  if(use_type == HTK_DISCRETE)
1168  header.samp_size = (EST_BIG_ENDIAN ? sizeof(short) :
1169  SWAPSHORT(sizeof(short)));
1170  else
1171  header.samp_size = (EST_BIG_ENDIAN ? (sizeof(float) * file_num_channels) :
1172  SWAPSHORT((sizeof(float) * file_num_channels)));
1173 
1174  header.samp_type = EST_BIG_ENDIAN ? type : SWAPSHORT(type);
1175 
1176  ssize_t i, j;
1177  FILE *outf;
1178  if (filename == "-")
1179  outf = stdout;
1180  else if ((outf = fopen(filename,"wb")) == NULL)
1181  {
1182  cerr << "save_htk: cannot open file \"" << filename <<
1183  "\" for writing." << endl;
1184  return misc_write_error;
1185  }
1186 
1187  // write the header
1188  fwrite((char*)&(header.num_samps), 1, sizeof(header.num_samps), outf);
1189  fwrite((char*)&(header.samp_period), 1, sizeof(header.samp_period), outf);
1190  fwrite((char*)&(header.samp_size), 1, sizeof(header.samp_size), outf);
1191  fwrite((char*)&(header.samp_type), 1, sizeof(header.samp_type), outf);
1192 
1193  // write the data
1194  if(use_type == HTK_DISCRETE)
1195  {
1196  if(track.num_channels() < 1)
1197  {
1198  cerr << "No data to write as HTK_DISCRETE !" << endl;
1199  }
1200  else
1201  {
1202  if(track.num_channels() > 1)
1203  {
1204  cerr << "Warning: multiple channel track being written" << endl;
1205  cerr << " as discrete will only save channel 0 !" << endl;
1206  }
1207  for (i = 0; i < track.num_frames(); ++i)
1208  {
1209  short tempshort = (EST_BIG_ENDIAN ? (short)(track.a(i, 0)) :
1210  SWAPSHORT((short)(track.a(i, 0)))) ;
1211  fwrite((unsigned char*) &tempshort, 1, sizeof(short), outf);
1212  }
1213  }
1214  }
1215  else // not HTK_DISCRETE
1216  for (i = 0; i < track.num_frames(); ++i)
1217  {
1218  if ((type & HTK_EST_PS) != 0)
1219  {
1220  if(!EST_BIG_ENDIAN)
1221  swapfloat(&(track.t(i)));
1222  fwrite((unsigned char*) &(track.t(i)), 1, sizeof(float), outf);
1223  }
1224  for (j = 0; j < track.num_channels(); ++j)
1225  {
1226  if(!EST_BIG_ENDIAN)
1227  swapfloat(&(track.a(i,j)));
1228  fwrite((unsigned char*) &(track.a(i, j)), 1, sizeof(float), outf);
1229  }
1230  }
1231 
1232  if (outf != stdout)
1233  fclose(outf);
1234 
1235  return write_ok;
1236 }
1237 
1238 static int htk_sane_header(htk_header *htk)
1239 {
1240  return htk->num_samps > 0 &&
1241  htk->samp_period > 0 &&
1242  htk->samp_size > 0 &&
1243  htk->samp_size < (short)(UNREASONABLE_FRAME_SIZE * sizeof(float));
1244 }
1245 
1246 static int htk_swapped_header(htk_header *header)
1247 {
1248  // Tries to guess if the header is swapped. If so it
1249  // swaps the contents and returns TRUE, other returns FALSE
1250  // HTK doesn't have a magic number so we need heuristics to
1251  // guess when its byte swapped
1252 
1253  if (htk_sane_header(header))
1254  return 0;
1255 
1256  header->num_samps = SWAPINT(header->num_samps);
1257  header->samp_period = SWAPINT(header->samp_period);
1258  header->samp_size = SWAPSHORT(header->samp_size);
1259  header->samp_type = SWAPSHORT(header->samp_type);
1260 
1261  if (htk_sane_header(header))
1262  return 1;
1263 
1264  return -1;
1265 
1266 }
1267 
1269 {
1270  return save_htk_as(filename, tmp, HTK_FBANK);
1271 }
1272 
1274 {
1275  return save_htk_as(filename, tmp, HTK_FBANK);
1276 }
1277 
1279 {
1280  return save_htk_as(filename, tmp, HTK_MFCC);
1281 }
1282 
1284 {
1285  return save_htk_as(filename, tmp, HTK_MFCC | HTK_ENERGY);
1286 }
1287 
1289 {
1290  return save_htk_as(filename, tmp, HTK_USER);
1291 }
1292 
1294 {
1295  return save_htk_as(filename, tmp, HTK_DISCRETE);
1296 }
1297 
1298 
1299 static EST_read_status load_ema_internal(const EST_String filename, EST_Track &tmp, float ishift, float startt, bool swap)
1300 {
1301  (void)ishift;
1302  (void)startt;
1303 
1304  ssize_t i, j, k, nframes, new_order;
1305  EST_TVector<short> file_data;
1306  int sample_width;
1307  ssize_t data_length;
1308  float shift;
1309  FILE *fp;
1310 
1311  if ((fp = fopen(filename, "rb")) == NULL)
1312  {
1313  cerr << "EST_Track load: couldn't open EST_Track input file" << endl;
1314  return misc_read_error;
1315  }
1316 
1317  EST_fseek(fp, 0, SEEK_END);
1318  sample_width = 2;
1319  data_length = EST_ftell(fp)/sample_width;
1320  new_order = 10;
1321  nframes = data_length /new_order;
1322  shift = 0.002;
1323 
1324  cout << "d length: " << data_length << " nfr " << nframes << endl;
1325 
1326  tmp.resize(nframes, new_order);
1327  tmp.fill_time(shift);
1328  tmp.set_equal_space(TRUE);
1329 
1330  file_data.resize(data_length);
1331 
1332  EST_fseek(fp, 0, SEEK_SET);
1333 
1334  if ((int)fread(file_data.memory(), sample_width, data_length, fp) != data_length)
1335  {
1336  fclose(fp);
1337  return misc_read_error;
1338  }
1339 
1340  if (swap)
1341  swap_bytes_short(file_data.memory(), data_length);
1342 
1343  for (i = k = 0; i < nframes; ++i)
1344  for (j = 0; j < new_order; ++j, ++k)
1345  tmp.a(i, j) = (float)file_data.a_no_check(k);
1346 
1347  // name the fields
1348  EST_String t;
1349  // the first 'order' fields are always called c1,c2...
1350  // AWB bug -- the following corrupts memory
1351  /* for (i = 0; i < order; i++)
1352  {
1353  EST_String t2;
1354  t2 = EST_String("c") + itoString(i+1);
1355  tmp.set_field_name(t2, i);
1356  }
1357  i=order;
1358  */
1359  cout << "here \n";
1360 
1361  tmp.set_name(filename);
1362  tmp.set_file_type(tff_ema);
1363 
1364  fclose(fp);
1365  return format_ok;
1366 }
1367 
1368 EST_read_status EST_TrackFile::load_ema(const EST_String filename, EST_Track &tmp, float ishift, float startt)
1369 {
1370  return load_ema_internal(filename, tmp, ishift, startt, FALSE);
1371 }
1372 
1373 
1374 EST_read_status EST_TrackFile::load_ema_swapped(const EST_String filename, EST_Track &tmp, float ishift, float startt)
1375 {
1376  return load_ema_internal(filename, tmp, ishift, startt, TRUE);
1377 }
1378 
1379 #if 0
1380 EST_read_status EST_TrackFile::load_NIST(const EST_String filename, EST_Track &tmp, float ishift, float startt)
1381 {
1382  (void)ishift; // what does this do ?
1383  (void)startt;
1384 
1385  char header[NIST_HDR_SIZE];
1386  int samps,sample_width,data_length,actual_bo;
1387  unsigned char *file_data;
1388  enum EST_sample_type_t actual_sample_type;
1389  char *byte_order, *sample_coding;
1390  int n,i,j,k;
1391  int current_pos;
1392  int offset=0;
1393 
1394  EST_TokenStream ts;
1395  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
1396  {
1397  cerr << "Can't open track file " << filename << endl;
1398  return misc_read_error;
1399  }
1400 
1401  current_pos = ts.tell();
1402  if (ts.fread(header,NIST_HDR_SIZE,1) != 1)
1403  return misc_read_error;
1404 
1405  if (strncmp(header,NIST_SIG,sizeof(NIST_SIG)) != 0)
1406  return wrong_format;
1407 
1408  samps = nist_get_param_int(header,"sample_count",-1);
1409  int num_channels = nist_get_param_int(header,"channel_count",1);
1410  sample_width = nist_get_param_int(header,"sample_n_bytes",2);
1411  int sample_rate =
1412  nist_get_param_int(header,"sample_rate",def_load_sample_rate);
1413  byte_order = nist_get_param_str(header,"sample_byte_format",
1414  (EST_BIG_ENDIAN ? "10" : "01"));
1415  sample_coding = nist_get_param_str(header,"sample_coding","pcm");
1416 
1417  data_length = (samps - offset)*num_channels;
1418  file_data = walloc(unsigned char,sample_width * data_length);
1419 
1420  ts.seek(current_pos+NIST_HDR_SIZE+(sample_width*offset*(num_channels)));
1421 
1422  n = ts.fread(file_data,sample_width,data_length);
1423 
1424  if ((n < 1) && (n != data_length))
1425  {
1426  wfree(file_data);
1427  wfree(sample_coding);
1428  wfree(byte_order);
1429  return misc_read_error;
1430  }
1431  else if ((n < data_length) && (data_length/num_channels == n))
1432  {
1433  fprintf(stderr,"TRACK read: nist header is (probably) non-standard\n");
1434  fprintf(stderr,"TRACK read: assuming different num_channel interpretation\n");
1435  data_length = n; /* wrongly headered file */
1436  }
1437  else if (n < data_length)
1438  {
1439  fprintf(stderr,"TRACK read: short file %s\n",
1440  (const char *)ts.filename());
1441  fprintf(stderr,"WAVE read: at %d got %d instead of %d samples\n",
1442  offset,n,data_length);
1443  data_length = n;
1444  }
1445 
1446  actual_sample_type = nist_to_sample_type(sample_coding);
1447  actual_bo = ((strcmp(byte_order,"10") == 0) ? bo_big : bo_little);
1448 
1449  short *data;
1450  data = convert_raw_data(file_data,data_length,
1451  actual_sample_type,actual_bo);
1452 
1453  // copy into the Track
1454  int num_samples = data_length/num_channels;
1455  tmp.resize(num_samples, num_channels);
1456  tmp.set_equal_space(TRUE);
1457  tmp.fill_time(1/(float)sample_rate);
1458 
1459  cerr << "shift " << 1/(float)sample_rate << endl;
1460 
1461  k=0;
1462  for (i=0; i<num_samples; i++)
1463  {
1464  for (j = 0; j < num_channels; ++j)
1465  tmp.a(i, j) = data[k++]; // channels are simply interleaved
1466  tmp.set_value(i);
1467  }
1468  for (j = 0; j < num_channels; ++j)
1469  tmp.set_channel_name("name", j);
1470 
1471 
1472 
1473  /*
1474  *sample_type = st_short;
1475  *bo = EST_NATIVE_BO;
1476  *word_size = 2;
1477  */
1478 
1479  //cerr << "NIST OK" << endl;
1480 
1481  return format_ok;
1482 }
1483 
1485 {
1486  FILE *fd;
1487  ssize_t i,j,k=0;
1488  if (filename == "-")
1489  fd = stdout;
1490  else if ((fd = fopen(filename,"wb")) == NULL)
1491  return write_fail;
1492 
1493  // create header
1494  char header[NIST_HDR_SIZE], p[1024];;
1495  const char *t;
1496 
1497  memset(header,0,1024);
1498  strcat(header, NIST_SIG);
1499  sprintf(p, "channel_count -i %d\n", tr.num_channels());
1500  strcat(header, p);
1501  sprintf(p, "sample_count -i %d\n", tr.num_frames());
1502  strcat(header, p);
1503  int sr = (int)(rint(1/(float)tr.shift()));
1504  sprintf(p, "sample_rate -i %d\n", sr);
1505  strcat(header, p);
1507  sprintf(p, "sample_coding -s%d %s\n", (signed)strlen(t), t);
1508  strcat(header, p);
1509 
1510  strcat(header, NIST_END_SIG);
1511  /*makes it nice to read */
1512  strcat(header, "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
1513 
1514  // write header
1515  if (fwrite(&header, 1024, 1, fd) != 1)
1516  return misc_write_error;
1517 
1518  // data
1519  short data[tr.num_frames() * tr.num_channels()];
1520 
1521 
1522  for (i = 0; i < tr.num_frames(); ++i)
1523  // restricted to shorts at this time
1524  for (j = 0; j < tr.num_channels(); ++j)
1525  data[k++] = (short)(tr.a_no_check(i, j));
1526 
1527  // byte swapping of output not supported - only write native bo
1528  int bo = str_to_bo("native");
1529  return save_raw_data(fd,data,0,tr.num_frames(),tr.num_channels(),
1530  st_short,bo);
1531 
1532  if (fd != stdout)
1533  fclose(fd);
1534  return write_ok;
1535 
1536 }
1537 #endif
1538 
1539 
1540 EST_read_status EST_TrackFile::load_htk(const EST_String filename, EST_Track &tmp, float ishift, float startt)
1541 {
1542  (void)ishift;
1543 
1544  // num_values is total number of fields in file
1545  // num_channels is number of fields in resultant track
1546  // order is order of LPC etc. analysis
1547  // e.g. if order is 12 and we have energy and delta then num_values = (12 + 1) * 2 = 26
1548 
1549  ssize_t i,j, order, new_frames, num_values, num_channels;
1550 
1551  EST_String pname;
1552  int swap;
1553  int time_included;
1554 
1555  FILE *fp;
1556  struct htk_header header;
1557  int header_sz = sizeof(header);
1558 
1559  // numbers A and B for decompression of generally compressed files
1560  float *compressA=NULL, compressA_Buffer[REASONABLE_FRAME_SIZE];
1561  float *compressB=NULL, compressB_Buffer[REASONABLE_FRAME_SIZE];
1562  bool fileIsCompressed=false;
1563 
1564  unsigned short samp_type, base_samp_type;
1565 
1566  if ((fp = fopen(filename, "rb")) == NULL){
1567  cerr << "EST_Track load: couldn't open EST_Track input file" << endl;
1568  return misc_read_error;
1569  }
1570 
1571  // try and read the header
1572  if (fread(&header, header_sz, 1, fp) != 1){
1573  fclose(fp);
1574  return wrong_format;
1575  }
1576 
1577  swap = htk_swapped_header(&header); // this is regrettable
1578 
1579  if( swap<0 ){
1580  fclose(fp);
1581  return read_format_error;
1582  }
1583 
1584  samp_type = header.samp_type;
1585  base_samp_type = samp_type & HTK_MASK;
1586 
1587  time_included = (samp_type & HTK_EST_PS) != 0;
1588 
1589  switch(base_samp_type){
1590  case HTK_WAVE:
1591  cerr << "Can't read HTK WAVEFORM format file into track" << endl;
1592  fclose(fp);
1593  return misc_read_error;
1594  break;
1595 
1596  case HTK_LPC:
1597  pname = "ct_lpc";
1598  break;
1599 
1600  case HTK_LPCREFC:
1601  case HTK_IREFC:
1602  EST_warning( "reading HTK_IREFC and HTK_LPREC parameter types is unsupported" );
1603  fclose( fp );
1604  return read_format_error;
1605  break;
1606 
1607  case HTK_LPCCEP:
1608  pname = "ct_cepstrum";
1609  break;
1610 
1611  case HTK_LPDELCEP:
1612  // equivalent to HTK_LPCCEP + DELTA
1613  base_samp_type = HTK_LPCCEP;
1614  samp_type = HTK_LPCCEP | HTK_DELTA; // set delta bit
1615  pname = "ct_cepstrum";
1616  break;
1617 
1618  case HTK_MFCC:
1619  pname = "ct_other";
1620  break;
1621 
1622  case HTK_FBANK:
1623  case HTK_USER:
1624  pname = "ct_other";
1625  break;
1626 
1627  case HTK_DISCRETE:
1628  cerr << "Can't read HTK DISCRETE format file into track" << endl;
1629  fclose(fp);
1630  return misc_read_error;
1631  break;
1632 
1633  case HTK_MELSPEC:
1634  pname = "ct_other";
1635  break;
1636 
1637  default:
1638  fclose(fp);
1639  return wrong_format;
1640  break;
1641  }
1642 
1643  // if we get this far we have decided this is a HTK format file
1644 
1645  // handle compressed/uncompressed files differently
1646  if( header.samp_type & HTK_COMP ){
1647 
1648  fileIsCompressed = true;
1649 
1650  num_channels = num_values = header.samp_size / sizeof(short int);
1651 
1652  // get compression numbers A and B
1653  if (num_channels > REASONABLE_FRAME_SIZE){
1654  compressA = new float[num_values];
1655  compressB = new float[num_values];
1656  }
1657  else{
1658  compressA = compressA_Buffer;
1659  compressB = compressB_Buffer;
1660  }
1661 
1662  if( (fread( compressA, sizeof(float), num_values, fp )) != static_cast<size_t>(num_values) ){
1663  if (compressA != compressA_Buffer) delete[] compressA;
1664  if (compressB != compressB_Buffer) delete[] compressB;
1665  fclose( fp );
1666  return read_format_error;
1667  }
1668 
1669  if( (fread( compressB, sizeof(float), num_values, fp )) != static_cast<size_t>(num_values) ){
1670  if (compressA != compressA_Buffer) delete[] compressA;
1671  if (compressB != compressB_Buffer) delete[] compressB;
1672  fclose( fp );
1673  return read_format_error;
1674  }
1675 
1676  if (swap){
1677  swap_bytes_float( compressA, num_values );
1678  swap_bytes_float( compressB, num_values );
1679  }
1680 
1681  // subtract extra frames to account for the two vectors of floats
1682  // used for decompression.
1683  new_frames = header.num_samps - (2*(sizeof(float)-sizeof(short int)));
1684  }
1685  else{
1686  num_channels = num_values = header.samp_size / sizeof(float);
1687  new_frames = header.num_samps;
1688  }
1689 
1690  if (num_values > UNREASONABLE_FRAME_SIZE){
1691  if (compressA != compressA_Buffer) delete[] compressA;
1692  if (compressB != compressB_Buffer) delete[] compressB;
1693  fclose(fp);
1694  return read_format_error;
1695  }
1696 
1697  if (time_included)
1698  num_channels -= 1;
1699 
1700  float shift = ((float)header.samp_period/ (float)HTK_UNITS_PER_SECOND);
1701 
1702  tmp.resize(new_frames, num_channels);
1703 
1704  if ((startt > 0) && (startt < NEARLY_ZERO ))
1705  EST_warning( "setting htk file start to %f", startt );
1706 
1707  tmp.fill_time(shift, startt);
1708 
1709  tmp.set_equal_space(!time_included);
1710 
1711  // check length of file is as expected from header info
1712  ssize_t dataBeginPosition = EST_ftell(fp);
1713  if( dataBeginPosition == -1 ){
1714  if (compressA != compressA_Buffer) delete[] compressA;
1715  if (compressB != compressB_Buffer) delete[] compressB;
1716  fclose(fp);
1717  return wrong_format;
1718  }
1719 
1720  if (EST_fseek(fp,0,SEEK_END)){
1721  if (compressA != compressA_Buffer) delete[] compressA;
1722  if (compressB != compressB_Buffer) delete[] compressB;
1723  fclose(fp);
1724  return wrong_format;
1725  }
1726 
1727  long file_length;
1728  if ((file_length = ftell(fp)) == -1){
1729  if (compressA != compressA_Buffer) delete[] compressA;
1730  if (compressB != compressB_Buffer) delete[] compressB;
1731  fclose(fp);
1732  return wrong_format;
1733  }
1734 
1735  long expected_vals;
1736  if( fileIsCompressed ){
1737  expected_vals = (file_length-dataBeginPosition) / sizeof(short int);
1738 
1739  if( header.samp_type & HTK_CRC )
1740  expected_vals -= 1; // just ignore the appended cyclic redundancy checksum
1741  }
1742  else
1743  expected_vals = (file_length-dataBeginPosition) / sizeof(float);
1744 
1745  /*
1746  printf( "%d %d %d %d %d %d\n",
1747  expected_vals, file_length, dataBeginPosition, sizeof(float), num_values, new_frames );
1748  */
1749 
1750  if( expected_vals != (num_values * new_frames) ){
1751  // it probably isn't HTK format after all
1752  if (compressA != compressA_Buffer) delete[] compressA;
1753  if (compressB != compressB_Buffer) delete[] compressB;
1754  fclose(fp);
1755  return wrong_format;
1756  }
1757 
1758  // work out the order of the analysis
1759  // Reorg -- surely you can't increase order
1760  order = num_channels;
1761  if( samp_type & HTK_NO_E )
1762  order++;
1763 
1764  if( samp_type & HTK_AC )
1765  order /= 3;
1766  else if( samp_type & HTK_DELTA )
1767  order /= 2;
1768 
1769  if( samp_type & HTK_ENERGY )
1770  order--;
1771 
1772  // go to start of data
1773  if( EST_fseek(fp, dataBeginPosition, SEEK_SET) == -1 ){
1774  cerr << "Couldn't position htk file at start of data" << endl;
1775  if (compressA != compressA_Buffer) delete[] compressA;
1776  if (compressB != compressB_Buffer) delete[] compressB;
1777  fclose(fp);
1778  return misc_read_error;
1779  }
1780 
1781  if( fileIsCompressed ){
1782  short int *frame, frame_buffer[REASONABLE_FRAME_SIZE];
1783  if( num_values > REASONABLE_FRAME_SIZE )
1784  frame = new short int[num_values];
1785  else
1786  frame = frame_buffer;
1787 
1788  int first_channel = time_included?1:0;
1789 
1790  for( i=0; i<new_frames; i++ ){
1791  if( fread( frame, sizeof(short int), num_values, fp ) != (size_t) num_values ){
1792  cerr << "Could not read data from htk track file" << endl;
1793  fclose(fp);
1794 
1795  if( frame != frame_buffer )
1796  delete [] frame;
1797  if( compressA != compressA_Buffer )
1798  delete [] compressA;
1799  if( compressB != compressB_Buffer )
1800  delete [] compressB;
1801 
1802  return misc_read_error;
1803  }
1804 
1805  if( swap )
1806  swap_bytes_short( frame, num_values );
1807 
1808  if( time_included )
1809  tmp.t(i) = ((float)frame[0]+compressB[0])/compressA[0];
1810 
1811  for( j=0; j<num_channels; ++j ){
1812  ssize_t index = j+first_channel;
1813  tmp.a(i,j) = ((float)frame[index]+compressB[index])/compressA[index];
1814  }
1815 
1816  tmp.set_value(i);
1817  }
1818 
1819  if( frame != frame_buffer )
1820  delete [] frame;
1821  if( compressA != compressA_Buffer )
1822  delete [] compressA;
1823  if( compressB != compressB_Buffer )
1824  delete [] compressB;
1825  }
1826  else{
1827  float *frame, frame_buffer[REASONABLE_FRAME_SIZE];
1828 
1829  if (num_values > REASONABLE_FRAME_SIZE)
1830  frame = new float[num_values];
1831  else
1832  frame = frame_buffer;
1833 
1834  int first_channel = time_included?1:0;
1835  for( i=0; i<new_frames; i++ ){
1836  if( fread( frame, sizeof(float), num_values, fp ) != (size_t) num_values ){
1837  cerr << "Could not read data from htk track file" << endl;
1838  fclose(fp);
1839  if (frame != frame_buffer)
1840  delete [] frame;
1841  return misc_read_error;
1842  }
1843  if( swap )
1844  swap_bytes_float( frame, num_values );
1845 
1846  if( time_included )
1847  tmp.t(i) = frame[0];
1848 
1849  for( j=0; j<num_channels; ++j )
1850  tmp.a(i, j) = frame[j+first_channel];
1851 
1852  tmp.set_value(i);
1853  }
1854 
1855  if( frame != frame_buffer )
1856  delete [] frame;
1857  }
1858 
1859  // name the fields
1860  EST_String t;
1861  // the first 'order' fields are always called c1,c2...
1862  // AWB bug -- the following corrupts memory
1863  for (i=0;i<order;i++)
1864  {
1865  EST_String t2;
1866  t2 = EST_String("c") + itoString(i+1);
1867  tmp.set_channel_name(t2, i);
1868  }
1869  i=order;
1870 
1871  // energy present and not suppressed
1872  if ( (samp_type & HTK_ENERGY) && !(samp_type & HTK_NO_E) )
1873  tmp.set_channel_name("E", i++);
1874 
1875  // delta coeffs ?
1876  if (samp_type & HTK_DELTA){
1877  for (j = 0; j < order; j++){
1878  t = EST_String("c") + itoString(j+1) + "_d";
1879  tmp.set_channel_name(t, i++);
1880  }
1881 
1882  // energy ?
1883  if (samp_type & HTK_ENERGY)
1884  tmp.set_channel_name("E_d", i++);
1885  }
1886 
1887  // 'acceleration' coeffs ?
1888  if (samp_type & HTK_AC){
1889  for(j=0;j<order;j++){
1890  t = EST_String("ac")+ itoString(j+1)+ "_d_d";
1891  tmp.set_channel_name(t, i++);
1892  }
1893  // energy ?
1894  if (samp_type & HTK_ENERGY)
1895  tmp.set_channel_name("E_d_d", i++);
1896  }
1897 
1898  // sanity check
1899  if (i != num_channels){
1900  cerr << "Something went horribly wrong - wanted " << num_values
1901  << " channels in track but got " << i << endl;
1902  fclose(fp);
1903  return wrong_format;
1904  }
1905  tmp.f_set("contour_type",pname);
1906  tmp.set_name(filename);
1907  tmp.set_file_type(tff_htk);
1908  fclose(fp);
1909  return format_ok;
1910 }
1911 
1912 /************************************************************************/
1913 /* */
1914 /* Convert single f0 channel tracks into arbitrarily chosen esps FEA */
1915 /* subtype, reputedly to make waves happy. This is evil beyond all */
1916 /* understanding. */
1917 /* */
1918 /************************************************************************/
1919 
1920 // format of the desired track.
1921 static struct EST_TrackMap::ChannelMappingElement espsf0_mapping[] =
1922 {
1923 { channel_f0, 0 },
1924 { channel_voiced, 1 },
1925 { channel_power, 2},
1926 { channel_peak, 3},
1927 { channel_unknown, 0}
1928 };
1929 static EST_TrackMap ESPSF0TrackMap(espsf0_mapping);
1930 
1931 // It seems that the vital thing is to call the track "F0", and so
1932 // we only need 1 channel instead of the normal 5. This saves *lots* of
1933 // space. For the time being we use 2 channels as the prob_voicnug filed is
1934 // used by our input routine.
1935 
1936 int track_to_espsf0(EST_Track &track, EST_Track &f0_track)
1937 {
1938  f0_track.resize(track.num_frames(), 2);
1939 
1940  f0_track.assign_map(ESPSF0TrackMap);
1941 
1942  // k1 is ratio of the first two cross-correlation values
1943  // f0_track.set_channel_name("k1", 4);
1944 
1945  // copy data. Remaining channels zeroed by resize. This is of course stupid
1946  // as if k1 iz zero mathematics is in deep problems.
1947  for (ssize_t i = 0; i < track.num_frames(); ++i)
1948  {
1949  f0_track.a(i, channel_voiced) = track.track_break(i) ? 0.1 : 1.2;
1950  f0_track.a(i, channel_f0) = track.track_break(i) ? 0.0: track.a(i,0);
1951  }
1952 
1953  f0_track.set_file_type(tff_esps);
1954  f0_track.fill_time(track.shift());
1955  track.set_name(track.name());
1956 
1957  /* f0_track.resize(track.num_frames(), 5);
1958 
1959  f0_track.assign_map(ESPSF0TrackMap);
1960 
1961  // k1 is ratio of the first two cross-correlation values
1962  f0_track.set_channel_name("k1", 4);
1963 
1964  // copy data. Remaining channels zeroed by resize. This is of course stupid
1965  // as if k1 iz zero mathematics is in deep problems.
1966  for (ssize_t i = 0; i < track.num_frames(); ++i)
1967  {
1968  f0_track.a(i, channel_voiced) = track.track_break(i) ? 0.1 : 1.2;
1969  f0_track.a(i, channel_f0) = track.a(i,0);
1970  }
1971 
1972  f0_track.set_file_type("esps");
1973  f0_track.fill_time(track.shift());
1974  track.set_name(track.name());
1975  */
1976 
1977  return 0;
1978 }
1979 
1981 {
1982  ssize_t f, p, i;
1983  f = p = -1;
1984 
1985  // check to see if prob of voicing channel exists
1986  for (i = 0; i < fz.num_channels(); ++i)
1987  {
1988  if (fz.channel_name(i) == "prob_voice")
1989  p = i;
1990  }
1991  for (i = 0; i < fz.num_channels(); ++i)
1992  {
1993  if (fz.channel_name(i) == "F0")
1994  f = i;
1995  }
1996 
1997  for (i = 0; i < fz.num_frames(); ++i)
1998  {
1999  if (p == -1) // if f0 val is < 1 make this a break
2000  {
2001  if (fz.a(i, f) < 1.0)
2002  fz.set_break(i);
2003  else
2004  fz.set_value(i);
2005  }
2006  else // use prob voicing
2007  {
2008  if (fz.a(i, p) < 0.5)
2009  {
2010  fz.a(i, f) = 0.0;
2011  fz.set_break(i);
2012  }
2013  else
2014  fz.set_value(i);
2015  }
2016  }
2017 
2018  return 0;
2019 }
2020 
2022 {
2023  int type = HTK_LPC;
2024  int ncoefs, nchannels;
2025 
2026  if (track.has_channel(channel_lpc_N))
2027  ncoefs = track.channel_position(channel_lpc_N) - track.channel_position(channel_lpc_0)+1;
2028  else
2029  ncoefs = track.num_channels()-track.channel_position(channel_lpc_0);
2030 
2031  nchannels = ncoefs;
2032 
2033  if (track.has_channel(channel_power))
2034  {
2035  nchannels++;
2036  type |= HTK_ENERGY;
2037  }
2038 
2039  lpc.resize(track.num_frames(), nchannels);
2040  lpc.set_equal_space(track.equal_space());
2041  lpc.set_single_break(track.single_break());
2042  lpc.set_single_break(track.single_break());
2043 
2044  for(ssize_t i = 0; i< track.num_frames(); i++)
2045  for (ssize_t c = 0; c < ncoefs; c++)
2046  {
2047  lpc.a(i, c) = track.a(i, channel_lpc_0, c);
2048  lpc.t(i) = track.t(i);
2049  }
2050 
2051 
2052  if (track.has_channel(channel_power))
2053  {
2054  for(ssize_t ii = 0; ii< track.num_frames(); ii++)
2055  lpc.a(ii, ncoefs) = track.a(ii, channel_power);
2056  }
2057 
2058  return type;
2059 
2060 }
2061 
2063 {
2064  for (EST_Litem *p = tlist.head(); p ; p = p->next())
2065  tlist(p).save(tlist(p).name(), otype);
2066 
2067  return write_ok;
2068 }
2069 
2071  EST_Option &al)
2072 {
2073  EST_Track s;
2074  EST_Litem *p, *plp;
2075 
2076  for (p = files.head(); p; p = p->next())
2077  {
2078  tlist.append(s);
2079  plp = tlist.tail();
2080  if (read_track(tlist(plp), files(p), al) != format_ok)
2081  exit (-1);
2082 
2083  tlist(plp).set_name(files(p));
2084  }
2085 
2086  return format_ok;
2087 }
2088 
2089 int read_track(EST_Track &tr, const EST_String &in_file, EST_Option &al)
2090 {
2091 
2092  float ishift = 0;
2093  float startt = 0.0;
2094 
2095  if( al.present("-startt") )
2096  startt = al.fval( "-startt" );
2097 
2098  if (al.present("ishift"))
2099  ishift = al.fval("ishift");
2100  else if (al.present("-s"))
2101  ishift = al.fval("-s");
2102  else if (al.present("time_channel"))
2103  ishift = 1.0; // doesn't matter, will be reset by track
2104 
2105  if (al.present("-itype"))
2106  {
2107  if (tr.load(in_file, al.val("-itype", 0), ishift, startt) != format_ok)
2108  return -1;
2109  }
2110  else
2111  {
2112  if (tr.load(in_file, ishift, startt ) != format_ok)
2113  return -1;
2114  }
2115 
2116 // tr.create_map();
2117 
2118  // cout << "f0 "<< tr.has_channel(channel_f0) << ".\n";
2119 // if (al.present("time_channel") && tr.has_channel(al.sval("time_channel")))
2120 // {
2121 // cout << " time from channel " << al.sval("time_channel") << "\n";
2122 // channel_to_time(tr, al.sval("time_channel"), al.fval("time_scale"));
2123 // }
2124 
2125 
2126  // cout << tr;
2127  return 0;
2128 }
2129 
2130 
2132 {
2133  EST_String s("");
2134 
2135  for(int n=0; n< EST_TrackFile::map.n() ; n++)
2136  {
2137  const char *nm = EST_TrackFile::map.name(EST_TrackFile::map.token(n));
2138 
2139  if (s != "")
2140  s += ", ";
2141 
2142  s += nm;
2143 
2144  }
2145  return s;
2146 }
2147 
2149 {
2150  EST_String s("AvailablE track file formats:\n");
2151 
2152  for(int n=0; n< EST_TrackFile::map.n() ; n++)
2153  {
2154  const char *nm = EST_TrackFile::map.name(EST_TrackFile::map.token(n));
2155  const char *d = EST_TrackFile::map.info(EST_TrackFile::map.token(n)).description;
2156 
2157  s += EST_String::cat(" ", nm, EST_String(" ")*(13-strlen(nm)), d, "\n");
2158  }
2159  return s;
2160 }
2161 
2162 // note the order here defines the order in which loads are tried.
2164 {
2165 { tff_none, { "none" },
2166 {FALSE, NULL, NULL,
2167  "unknown track file type"}},
2168 {tff_esps, { "esps" },
2170  "entropic sps file"}},
2171 {tff_est_ascii, { "est", "est_ascii" },
2173  "Edinburgh Speech Tools track file"}},
2174 {tff_est_binary, { "est_binary" },
2176  "Edinburgh Speech Tools track file"}}
2177 ,
2178 {tff_htk, { "htk" },
2180  "htk file"}},
2181 //{tff_NIST, { "NIST" },
2182 //{TRUE, EST_TrackFile::load_NIST, EST_TrackFile::save_NIST,
2183 // "NIST"}},
2184 {tff_htk_fbank, { "htk_fbank" },
2186  "htk file (as FBANK)"}},
2187 {tff_htk_mfcc, { "htk_mfcc" },
2189  "htk file (as MFCC)"}},
2190 {tff_htk_mfcc_e, { "htk_mfcc_e" },
2192  "htk file (as MFCC_E)"}},
2193 {tff_htk_user, { "htk_user" },
2195  "htk file (as USER)"}},
2196 {tff_htk_discrete, { "htk_discrete" },
2198  "htk file (as DISCRETE)"}},
2199 {tff_ssff, {"ssff"},
2201  "Macquarie University's Simple Signal File Format"}},
2202 {tff_xmg, { "xmg" },
2204  "xmg file viewer"}},
2205 {tff_xgraph, { "xgraph" },
2207  "xgraph display program format"}},
2208 {tff_ema, { "ema" },
2210  "ema"}},
2211 {tff_ema_swapped, { "ema_swapped" },
2213  "ema, swapped"}},
2214 {tff_ascii, { "ascii" },
2216  "ascii decimal numbers"}},
2217 { tff_none, {"none"}, {FALSE, NULL, NULL, "unknown track file type"} }
2218 };
2219 
2221 
2222 static EST_TValuedEnumDefinition<EST_TrackFileType, const char *,
2223 EST_TrackFile::TS_Info> track_ts_names[] =
2224 {
2225 { tff_none, { "none" },
2226 {FALSE, NULL, NULL,
2227  "unknown track file type"}},
2228 
2229 {tff_est_ascii, {"est"},
2231  "Edinburgh Speech Tools track file"}},
2232 
2233 {tff_est_binary, {"est_binary"},
2235  "Edinburgh Speech Tools track file"}},
2236 
2237 {tff_ssff, {"ssff"},
2239  "Macquarie University's Simple Signal File Format"}},
2240 
2241 { tff_none, { "none" },
2242 {FALSE, NULL, NULL,
2243  "unknown track file type"}}
2244 };
2245 
2247 EST_TrackFile::ts_map(track_ts_names);
2248 
2249 
2250 #if defined(INSTANTIATE_TEMPLATES)
2251 
2252 #include "../base_class/EST_TNamedEnum.cc"
2254 template class EST_TValuedEnumI<EST_TrackFileType,
2255 const char *, EST_TrackFile::Info>;
2257 template class EST_TValuedEnumI<EST_TrackFileType,
2258 const char *, EST_TrackFile::TS_Info>;
2259 
2260 #endif
2261 
bool single_break() const
Definition: EST_Track.h:673
static EST_read_status load_xmg(LoadTrackFileArgs)
#define HTK_IREFC
Definition: htk.h:62
EST_TokenStream & get(EST_Token &t)
get next token in stream
Definition: EST_Token.cc:499
char * wstrdup(const char *s)
Definition: walloc.c:117
#define HTK_ENERGY
Definition: htk.h:46
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
int num_aux_channels() const
return number of auxiliary channels in track
Definition: EST_Track.h:660
#define walloc(TYPE, SIZE)
Definition: EST_walloc.h:52
EST_read_status read_est_header(EST_TokenStream &ts, EST_Features &hinfo, bool &ascii, EST_EstFileType &t)
Definition: est_file.cc:84
const char * description
int contains(const char *s, ssize_t pos=-1) const
Does it contain this substring?
Definition: EST_String.h:365
#define HTK_LPC
Definition: htk.h:58
RMS power of section of signal.
static EST_write_status save_htk_user(SaveTrackFileArgs)
int track_to_htk_lpc(EST_Track &track, EST_Track &lpc)
int track_to_espsf0(EST_Track &track, EST_Track &f0_track)
#define SWAPSHORT(x)
Definition: EST_cutils.h:79
static EST_TNamedEnumI< EST_TrackFileType, TS_Info > ts_map
EST_FilePos EST_ftell(FILE *fp)
Definition: EST_File.h:71
static EST_write_status save_est_ascii(SaveTrackFileArgs)
enum EST_bo_t str_to_bo(const char *boname)
Definition: EST_cutils.c:95
EST_write_status
#define SWAPINT(x)
Definition: EST_cutils.h:75
short samp_type
Definition: htk.h:79
static EST_read_status load_NIST(LoadTrackFileArgs)
static EST_write_status save_est_binary_ts(SaveTrac_TokenStreamArgs)
enum EST_write_status save_raw_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, enum EST_sample_type_t sample_type, int bo)
int fread(void *buff, int size, int nitems) EST_WARN_UNUSED_RESULT
Reading binary data, (don&#39;t use peek() immediately beforehand)
Definition: EST_Token.cc:368
#define HTK_UNITS_PER_SECOND
Definition: relation_io.cc:54
#define HTK_WAVE
Definition: htk.h:57
const char * sample_type_to_nist(enum EST_sample_type_t sample_type)
Definition: EST_wave_io.cc:112
void set_value(ssize_t i)
set frame i to be a value
Definition: EST_Track.cc:133
int espsf0_to_track(EST_Track &fz)
void set_channel_name(const EST_String &name, int channel)
set the name of the channel.
Definition: EST_Track.cc:168
void set_break(ssize_t i)
set frame i to be a break
Definition: EST_Track.cc:124
short samp_size
Definition: htk.h:78
EST_String name() const
name of track - redundant use access to features
Definition: EST_Track.h:196
static EST_write_status save_est_ts(SaveTrac_TokenStreamArgs)
#define HTK_LPCCEP
Definition: htk.h:60
int num_channels() const
return number of channels in track
Definition: EST_Track.h:657
EST_EstFileType
Definition: EST_FileType.h:50
void set_SingleCharSymbols(const EST_String &sc)
set which characters are to be treated as single character symbols
Definition: EST_Token.h:344
void set_single_break(bool t)
Definition: EST_Track.h:676
static EST_read_status load_ascii(LoadTrackFileArgs)
static EST_write_status save_ssff_ts(SaveTrac_TokenStreamArgs)
Definition: ssff.cc:250
const EST_String filename() const
The originating filename (if there is one)
Definition: EST_Token.h:378
EST_ChannelNameMap esps_channel_names
Definition of the names ESPS programs use for channels.
INLINE const T & a_no_check(ssize_t n) const
read-only const access operator: without bounds checking
Definition: EST_TVector.h:254
#define NEARLY_ZERO
Voicing decision.
EST_FilePos tell(void) const
tell, synonym for filepos
Definition: EST_Token.h:369
EST_String itoString(int n)
Make a EST_String object from an integer.
Definition: util_io.cc:141
float fval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:104
#define HTK_AC
Definition: htk.h:49
static EST_read_status load_esps(LoadTrackFileArgs)
#define HTK_LPDELCEP
Definition: htk.h:61
short * convert_raw_data(unsigned char *file_data, int data_length, enum EST_sample_type_t sample_type, int bo)
int ssize_t
void resize(ssize_t num_frames, int num_channels, bool preserve=1)
Definition: EST_Track.cc:214
float & a_no_check(ssize_t i, int c=0)
Definition: EST_Track.h:420
static EST_String options_short(void)
#define HTK_USER
Definition: htk.h:66
static EST_write_status save_htk_fbank(SaveTrackFileArgs)
int index(EST_TList< T > &l, T &val, bool(*eq)(const EST_UItem *, const EST_UItem *)=NULL)
Definition: EST_TList.h:286
EST_UItem * next()
Definition: EST_UList.h:55
Peak amplitude.
static const float default_frame_shift
Definition: EST_Track.h:119
EST_TrackFileType
Definition: EST_Track.h:58
int open(const EST_String &filename)
open a EST_TokenStream for a file.
Definition: EST_Token.cc:213
static EST_String cat(const EST_String s1, const EST_String s2=Empty, const EST_String s3=Empty, const EST_String s4=Empty, const EST_String s5=Empty, const EST_String s6=Empty, const EST_String s7=Empty, const EST_String s8=Empty, const EST_String s9=Empty)
Definition: EST_String.cc:1084
int open_string(const EST_String &newbuffer)
open a EST_TokenStream for string rather than a file
Definition: EST_Token.cc:264
const EST_String S(const EST_String &path) const
Definition: EST_Features.h:158
int samp_period
Definition: htk.h:76
EST_read_status load_snns_res(const EST_String filename, EST_Track &tr, float ishift, float startt)
void swap_bytes_float(float *data, int length)
Definition: EST_swapping.cc:59
static EST_read_status load_ssff_ts(LoadTrack_TokenStreamArgs)
Definition: ssff.cc:75
int track_break(ssize_t i) const
return true if frame i is a break
Definition: EST_Track.h:634
int channel_position(const char *name, int offset=0) const
Definition: EST_Track.cc:395
void assign_map(EST_TrackMap::P map)
Definition: EST_Track.cc:1426
#define HTK_DISCRETE
Definition: htk.h:67
#define HTK_MASK
Definition: htk.h:55
static EST_write_status save_esps(SaveTrackFileArgs)
EST_write_status save_snns_pat(const EST_String filename, EST_TrackList &inpat, EST_TrackList &outpat)
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
int eof()
end of file
Definition: EST_Token.h:362
EST_read_status load(const EST_String name, float ishift=0.0, float startt=0.0)
Definition: EST_Track.cc:1312
#define HTK_COMP
Definition: htk.h:50
#define SEEK_END
Definition: system.h:28
bool equal_space() const
return true if track has equal (i.e. fixed) frame spacing */
Definition: EST_Track.h:670
void resize(ssize_t n, int set=1)
Definition: EST_TVector.cc:196
#define HTK_DELTA
Definition: htk.h:48
float & t(ssize_t i=0)
return time position of frame i
Definition: EST_Track.h:478
float & a(ssize_t i, int c=0)
Definition: EST_Track.cc:1025
EST_write_status save_ind_TrackList(EST_TrackList &tlist, EST_String &otype)
const char * name(ENUM tok, int n=0) const
#define misc_write_error
#define HTK_LPCREFC
Definition: htk.h:59
void set_file_type(EST_TrackFileType t)
Definition: EST_Track.h:736
int n(void) const
#define HTK_CRC
Definition: htk.h:52
The file was written successfully.
void set_name(const EST_String &n)
set name of track - redundant use access to features
Definition: EST_Track.h:199
void swapfloat(float *f)
Definition: EST_swapping.cc:53
#define wrong_format
void change_type(float nshift, bool single_break)
REDO.
Definition: EST_Track.cc:656
static EST_read_status load_ema_swapped(LoadTrackFileArgs)
#define FALSE
Definition: EST_bool.h:119
void remove(const EST_String &name)
Definition: EST_Features.h:247
structure for the table.
Definition: EST_TrackMap.h:73
static EST_read_status load_htk(LoadTrackFileArgs)
void resize_aux(EST_StrList &map, bool preserve=1)
Definition: EST_Track.cc:314
void swap_bytes_short(short *data, int length)
Definition: EST_swapping.cc:97
#define misc_read_error
float time(const EST_Item &item)
Definition: EST_item_aux.cc:82
The file exists but is not in the format specified.
NULL
Definition: EST_WFST.cc:55
int present(const EST_String &name) const
const T & first() const
return const reference to first item in list
Definition: EST_TList.h:152
f
Definition: EST_item_aux.cc:48
static EST_TNamedEnumI< EST_TrackFileType, Info > map
int EST_fseek(FILE *fp, EST_FilePos offset, int whence)
Definition: EST_File.h:75
ssize_t num_frames() const
return number of frames in track
Definition: EST_Track.h:651
EST_Token & peek(void)
peek at next token
Definition: EST_Token.h:332
const EST_String & string(void) const
Definition: EST_Val.h:161
static EST_write_status save_htk_mfcc_e(SaveTrackFileArgs)
getString int
Definition: EST_item_aux.cc:50
#define HTK_MELSPEC
Definition: htk.h:65
The file was not written successfully.
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
EST_UItem * tail() const
Definition: EST_UList.h:99
static EST_write_status save_NIST(SaveTrackFileArgs)
void append(const T &item)
add item onto end of list
Definition: EST_TList.h:196
#define REASONABLE_FRAME_SIZE
int read_track(EST_Track &tr, const EST_String &in_file, EST_Option &al)
const EST_String channel_name(int channel, const EST_ChannelNameMap &map, int strings_override=1) const
Definition: EST_Track.cc:138
EST_read_status
const T * memory() const
Definition: EST_TVector.h:238
INFO & info(ENUM token) const
EST_Token get_upto(const EST_String &s)
get up to s in stream as a single token.
Definition: EST_Token.cc:505
#define HTK_MFCC
Definition: htk.h:63
Definition: htk.h:73
EST_read_status read_TrackList(EST_TrackList &tlist, EST_StrList &files, EST_Option &al)
static EST_read_status load_est(LoadTrackFileArgs)
char * nist_get_param_str(char *hdr, char *field, char *def_val)
static EST_write_status save_htk_discrete(SaveTrackFileArgs)
const EST_String & string() const
Definition: EST_Token.h:120
void begin(const Container &over)
Set the iterator ready to run over this container.
#define EST_warning
Definition: EST_error.h:106
Template vector.
Definition: EST_TVector.h:145
const char * str(void) const
Get a const-pointer to the actual memory.
Definition: EST_String.h:235
#define format_ok
enum EST_sample_type_t nist_to_sample_type(char *type)
Definition: EST_wave_io.cc:139
EST_Val & aux(ssize_t i, int c)
Definition: EST_Track.cc:428
static EST_write_status save_htk(SaveTrackFileArgs)
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
#define HTK_NO_E
Definition: htk.h:47
static EST_read_status load_xgraph(LoadTrackFileArgs)
EST_sample_type_t
Definition: EST_wave_aux.h:105
#define EST_BIG_ENDIAN
Definition: EST_cutils.h:69
#define EST_NATIVE_BO
Definition: EST_cutils.h:72
static EST_write_status save_xmg(SaveTrackFileArgs)
EST_Token get_upto_eoln(void)
get up to s in end of line as a single token.
Definition: EST_Token.cc:529
EST_UItem * head() const
Definition: EST_UList.h:97
int val(ssize_t i) const
return true if frame i is a value
Definition: EST_Track.cc:542
float shift() const
Definition: EST_Track.cc:602
#define HTK_FBANK
Definition: htk.h:64
int nist_get_param_int(char *hdr, char *field, int def_val)
static EST_write_status save_ssff(SaveTrackFileArgs)
Definition: ssff.cc:232
int I(const EST_String &path) const
Definition: EST_Features.h:147
static EST_write_status save_est_binary(SaveTrackFileArgs)
LISP fp
Definition: kkcompile.cc:63
EST_String
#define UNREASONABLE_FRAME_SIZE
static EST_write_status save_xgraph(SaveTrackFileArgs)
void wfree(void *p)
Definition: walloc.c:131
bool has_channel(const char *name) const
Definition: EST_Track.h:385
static EST_write_status save_ascii(SaveTrackFileArgs)
#define TRUE
Definition: EST_bool.h:118
static EST_read_status load_ssff(LoadTrackFileArgs)
Definition: ssff.cc:61
int seek(int position)
seek, reposition file pointer
Definition: EST_Token.cc:318
#define NIST_HDR_SIZE
Definition: EST_wave_io.cc:73
#define HTK_EST_PS
Definition: htk.h:54
void set_equal_space(bool t)
Definition: EST_Track.h:675
float ms_t(ssize_t i) const
return time of frame i in milli-seconds.
Definition: EST_Track.h:482
F0 in Hz.
void fill_time(float t, int start=1)
Definition: EST_Track.cc:789
int num_samps
Definition: htk.h:75
static EST_read_status load_est_ts(LoadTrack_TokenStreamArgs)
static EST_write_status save_htk_mfcc(SaveTrackFileArgs)
static EST_String options_supported(void)
static EST_read_status load_ema(LoadTrackFileArgs)
const EST_String aux_channel_name(int channel) const
Definition: EST_Track.h:728
Value to return for errors, never occurs in TrackMaps.
Utility EST_String Functions header file.
#define SEEK_SET
Definition: system.h:20