45 #include "EST_Track.h"
46 #include "EST_string_aux.h"
47 #include "EST_TrackFile.h"
48 #include "EST_error.h"
50 const int EST_Track::default_sample_rate=16000;
51 const float EST_Track::default_frame_shift=0.005;
69 p_values.
resize(n_frames, n_channels);
72 p_channel_names.
resize(n_channels);
86 p_values.
resize(n_frames, n_channels);
89 p_channel_names.
resize(n_channels);
100 void EST_Track::default_channel_names()
106 void EST_Track::default_vals(
void)
108 p_equal_space = FALSE;
109 p_single_break = FALSE;
115 p_channel_names.
resize(0);
125 cerr <<
"Requested setting of break value of the end of the array\n";
139 (void)strings_override;
140 return p_channel_names(i);
168 p_channel_names[i] = fn;
176 ostream& operator << (ostream& s,
const EST_Track &tr)
183 s <<
"\t" << tr(i, j);
185 s <<
"\t" << tr.aux(i, j);
194 p_values = a.p_values;
196 p_is_val = a.p_is_val;
197 p_t_offset = a.p_t_offset;
199 p_aux_names = a.p_aux_names;
204 p_equal_space = a.p_equal_space;
205 p_single_break = a.p_single_break;
206 p_channel_names = a.p_channel_names;
215 if (new_num_frames<0)
218 if (new_num_channels<0)
221 p_channel_names.
resize(new_num_channels);
228 p_values.
resize(new_num_frames, new_num_channels,
set);
229 p_times.
resize(new_num_frames,
set);
230 p_is_val.
resize(new_num_frames,
set);
235 for (
int i = old_num_frames; i <
num_frames(); ++i)
240 static void map_to_channels(
EST_StrList &channel_map,
247 for (p = channel_map.head(); p; p = p->next())
256 cerr<<
"Ill formed coefficient range in map: " << b <<
"\n";
260 first = b.
after(
"-");
264 cerr<<
"Ill formed coefficient range in map: "<<first<<
"\n";
268 last = first.
after(
"+");
269 first = first.
before(
"+");
271 n_f = Stringtoi(first);
272 n_l = Stringtoi(last);
274 for (
int i = n_f; i < n_l; ++i)
275 channel_names.
append(type +
"_" + itoString(i));
276 channel_names.
append(type +
"_N");
286 map_to_channels(new_channels, x);
290 int new_num_channels;
291 new_num_channels = x.length();
293 if (new_num_frames<0)
297 p_channel_names.
resize(new_num_channels);
300 for (i = 0, p = x.head(); p ; p = p->next(), ++i)
303 p_values.
resize(new_num_frames, new_num_channels,
set);
304 p_times.
resize(new_num_frames,
set);
305 p_is_val.
resize(new_num_frames,
set);
316 int new_num_channels;
317 new_num_channels = new_aux_channels.length();
319 p_aux_names.
resize(new_num_channels);
322 for (i = 0, p = new_aux_channels.head(); p ; p = p->next(), ++i)
341 " channel EST_Track to "<<
num_channels() <<
" channel EST_Track\n";
346 float old_end =
end();
348 for (i = 0, j = old_num; i < a.
num_frames(); ++i, ++j)
352 p_times[j] = old_end + a.
t(i);
353 p_is_val[j] = a.p_is_val(i);
371 cerr <<
"Error: Tried to add " << a.
num_frames() <<
372 " channel EST_Track to "<<
num_frames()<<
" channel EST_Track\n";
378 for (i = 0, j = old_num; i < a.
num_channels(); ++i, ++j)
397 if (channel_name(c) ==
name)
408 if (channel_name(c) ==
name)
411 cerr <<
"no channel '" << name <<
"'\n";
415 EST_Val &EST_Track::aux(
int i,
const char *name)
418 if (aux_channel_name(c) ==
name)
421 cerr <<
"no auxiliary channel '" << name <<
"' found\n";
425 EST_Val &EST_Track::aux(
int i,
int c)
430 EST_Val &EST_Track::aux(
int i,
int c)
const
435 #define EPSILON (0.0001)
439 static float ia = 0.0;
441 if (interp == it_nearest)
443 else if (interp == it_linear)
449 float n =
a(i,c), n1 =
a(i+1,c);
450 float tn = p_times(i), tn1 = p_times(i+1);
451 ia = n + (n1-n)*(t-tn)/(tn1-tn);
454 else if (interp == it_linear_nz)
460 float n =
a(i,c), n1 =
a(i+1,c);
461 if (fabs(n) < EPSILON || fabs(n1) < EPSILON)
463 float tn = p_times(i), tn1 = p_times(i+1);
464 ia = n + (n1-n)*(t-tn)/(tn1-tn);
475 int f = (int)( (x-
t(0))/s+0.5 );
495 bmid = bst + (bend-bst)/2;
605 EST_error(
"Tried to take shift from non-fixed contour\n");
613 while ((j2 != 0) && (j2 != (j1 +1)));
618 return p_times(1) - p_times(0);
620 EST_error(
"Couldn't determine shift size\n");
623 return (p_times(j2) - p_times(j1));
647 for (--i; i >= 0 ; --i)
657 if (!p_equal_space || nshift !=
shift())
659 p_equal_space = TRUE;
662 if (single_break != p_single_break)
678 n = (int) rint(((
end())/ f_interval));
685 for (i = 0; i < n; ++i)
686 new_times[i] = (
float) ((i + 1) * f_interval);
688 for (i = 0; i < n; ++i)
690 new_is_break[i] = !interp_value(new_times(i), f_interval);
692 new_values(i, j) = !new_is_break(i) ? interp_amp(new_times(i), j, f_interval): 0.0;
696 p_values = new_values;
697 p_is_val = new_is_break;
698 p_single_break = FALSE;
699 p_equal_space = TRUE;
702 float EST_Track::interp_amp(
float x,
int c,
float fl)
705 float x1, x2, y1, y2, m;
708 if ((p_times(i) + (fl / 2.0))> x)
730 m = (y2 - y1) / (x2 -x1);
731 return ((x - x1) * m) + y1;
734 int EST_Track::interp_value(
float x,
float fl)
743 cf = estimate_shift(x);
746 if ((p_times(i) + (fl / 2.0))> x)
762 if ((x < p_times(p) + (cf / 2.0)) || (x > p_times(n) - (cf / 2.0)))
768 float EST_Track::estimate_shift(
float x)
775 for (i = j; i > 0; --i)
777 return p_times(i) - p_times(i - 1);
781 return p_times(i + 1) - p_times(i);
790 for(
unsigned int i=0; i<nframes; ++i )
791 p_times.
a_no_check(i) = t * (float) (i + start);
798 for(
unsigned int i=0; i<nframes; ++i )
799 p_times.
a_no_check(i) = startt + (t * (float)i);
806 for(
unsigned int i=0; i<nframes; ++i )
827 new_times[j] = p_times(i);
828 new_is_break[j] = p_is_val(i);
829 while ((!new_is_break(j)) && (!
val(i + 1)))
833 p_values = new_values;
834 p_is_val = new_is_break;
841 p_single_break = TRUE;
844 void EST_Track::rm_trailing_breaks()
861 for (
int i=start, j = 0; i <
end; ++i, ++j)
863 p_times[j] = p_times(i);
866 p_is_val[j] = p_is_val(i);
869 p_values.
resize(end-start, EST_CURRENT, 1);
875 void EST_Track::add_trailing_breaks()
890 new_times.
resize(new_num);
899 new_times[j] = p_times(i);
908 p_values = new_values;
913 void EST_Track::pad_breaks()
919 EST_error(
"pad_breaks: Can only operate on fixed data\n");
926 n = (int)(((
end())/
shift()) + 1.0);
929 for (i = 0; i < n; ++i)
931 new_times[i] = (float) (i *
shift());
933 new_values(k, i) = 0.0;
937 for (i = 0, j = s; j < n; ++i, ++j)
941 for (; new_times(j) < p_times(i + 1); ++j);
956 p_values = new_values;
957 p_is_val = new_is_break;
963 p_single_break = FALSE;
966 static bool bounds_check(
const EST_Track &t,
int f,
int c,
int set)
968 const char *what =
set?
"set" :
"access";
972 cerr <<
"Attempt to " << what <<
" frame " << f <<
" of " << t.
num_frames() <<
" frame track\n";
977 cerr <<
"Attempt to " << what <<
" channel " << c <<
" of " << t.
num_channels() <<
" channel track\n";
984 static bool bounds_check(
const EST_Track &t,
989 const char *what =
set?
"set" :
"access";
995 cerr <<
"Attempt to " << what <<
" frame " << f <<
" of " << t.
num_frames() <<
" frame track\n";
1000 cerr <<
"Attempt to " << what <<
" frame " << f+nf-1 <<
" of " << t.
num_frames() <<
" frame track\n";
1009 cerr <<
"Attempt to " << what <<
" channel " << c <<
" of " << t.
num_channels() <<
" channel track\n";
1014 cerr <<
"Attempt to " << what <<
" channel " << c+nc-1 <<
" of " << t.
num_channels() <<
" channel track\n";
1024 if (!bounds_check(*
this, i,c,0))
1051 cerr <<
"No such channel " << name << endl;
1058 int start_frame,
int nframes,
1059 const EST_String &start_chan_name,
int nchans)
1062 if (start_chan_name ==
"")
1066 EST_error(
"sub_track: No such channel %s\n",
1067 (
const char *)start_chan_name);
1069 sub_track(st, start_frame, nframes, start_chan, nchans);
1073 int start_frame,
int nframes,
1077 int start_chan, end_chan, nchans=0;
1080 EST_error(
"sub_track: No such channel %s\n",
1081 (
const char *)start_chan_name);
1083 if (end_chan_name ==
"")
1088 EST_error(
"sub_track: No such channel %s\n",
1089 (
const char*)end_chan_name);
1091 nchans = end_chan - start_chan + 1;
1094 sub_track(st, start_frame, nframes, start_chan, nchans);
1098 int start_frame,
int nframes,
1099 int start_chan,
int nchans)
1106 if (!bounds_check(*
this, start_frame, nframes, start_chan, nchans, 0))
1109 p_values.
sub_matrix(st.p_values, start_frame, nframes, start_chan, nchans);
1111 p_times.
sub_vector(st.p_times, start_frame, nframes);
1113 p_is_val.
sub_vector(st.p_is_val, start_frame, nframes);
1115 p_channel_names.
sub_vector(st.p_channel_names, start_chan, nchans);
1120 p_aux.
sub_matrix(st.p_aux, start_frame, nframes, 0, EST_ALL);
1121 p_aux_names.
sub_vector(st.p_aux_names, 0, EST_ALL);
1123 st.p_t_offset = p_t_offset;
1125 st.p_equal_space = p_equal_space;
1126 st.p_single_break = p_single_break;
1127 st.copy_features(*
this);
1130 st.p_map =
new EST_TrackMap(p_map, start_chan, EST_TM_REFCOUNTED);
1137 int start_frame,
int nframes,
1138 int start_chan,
int nchans)
const
1145 if (!bounds_check(*
this, start_frame, nframes, start_chan, nchans, 0))
1148 st.
resize(nframes, nchans);
1150 for (
int ff=0; ff<nframes; ff++)
1152 st.p_times.a(ff) = p_times.a(ff+start_frame);
1153 st.p_is_val.a(ff) = p_is_val.a(ff+start_frame);
1154 for (
int c=0; c<nchans; c++)
1155 st.p_values.a(ff,c) = p_values.a(ff+start_frame,c+start_chan);
1158 for (
int c=0; c<nchans; c++)
1159 st.p_channel_names.a(c) = p_channel_names.a(c+start_chan);
1162 st.p_aux_names = p_aux_names;
1164 st.p_equal_space = p_equal_space;
1165 st.p_single_break = p_single_break;
1167 st.copy_features(*
this);
1170 st.p_map =
new EST_TrackMap(p_map, start_chan, EST_TM_REFCOUNTED);
1177 int f_len = frame_times.
length();
1179 st.
resize( f_len, nchans );
1181 for(
int i=0; i<f_len; ++i ){
1183 int source_index =
index(frame_times(i));
1185 st.p_times.a(i) = p_times.a( source_index );
1186 st.p_is_val.a(i) = p_is_val.a( source_index );
1188 for(
int c=0; c<nchans; c++ )
1189 st.p_values.a(i,c) = p_values.a(source_index,c);
1193 st.set_equal_space(
false );
1201 int f_len = frame_indices.
length();
1203 st.
resize( f_len, nchans );
1207 for(
int i=0; i<f_len; ++i ){
1209 int source_index = frame_indices(i);
1211 if( source_index <= last_index ){
1213 st.p_times.a(i) = p_times.a( source_index );
1214 st.p_is_val.a(i) = p_is_val.a( source_index );
1216 for(
int c=0; c<nchans; c++ )
1217 st.p_values.a(i,c) = p_values.a(source_index,c);
1222 st.set_equal_space(
false );
1233 EST_String save_type = (type ==
"") ? DEF_FILE_TYPE : type;
1235 EST_TrackFileType t = EST_TrackFile::map.token(save_type);
1239 cerr <<
"Unknown Track file type " << save_type << endl;
1243 EST_TrackFile::Save_File * s_fun = EST_TrackFile::map.info(t).save;
1247 cerr <<
"Can't save tracks to files type " << save_type << endl;
1251 return (*s_fun)(filename, *
this);
1256 EST_TrackFileType t = EST_TrackFile::ts_map.token(type);
1260 cerr <<
"Unknown Track file type " << type << endl;
1264 EST_TrackFile::Save_TokenStream * s_fun =
1265 EST_TrackFile::ts_map.info(t).save;
1269 cerr <<
"Can't save tracks to files type " << type << endl;
1272 return (*s_fun)(fp, *
this);
1277 EST_read_status stat = read_error;
1279 for (
int n = 0; n < EST_TrackFile::ts_map.n(); n++)
1281 EST_TrackFileType t = EST_TrackFile::ts_map.token(n);
1286 EST_TrackFile::TS_Info *info = &(EST_TrackFile::ts_map.info(t));
1288 if (! info->recognise)
1291 EST_TrackFile::Load_TokenStream * l_fun =info->load;
1296 stat = (*l_fun)(ts, *
this, ishift, startt);
1298 if (stat != read_format_error)
1300 if (stat == read_ok)
1311 EST_read_status stat = read_error;
1313 for(
int n=0; n< EST_TrackFile::map.n() ; n++)
1315 EST_TrackFileType t = EST_TrackFile::map.token(n);
1321 EST_TrackFile::Info *info = &(EST_TrackFile::map.info(t));
1323 if (! info->recognise)
1326 EST_TrackFile::Load_File * l_fun =info->load;
1331 stat = (*l_fun)(filename, *
this, ishift, startt);
1333 if (stat == read_ok)
1338 else if (stat == read_error)
1347 EST_TrackFileType t = EST_TrackFile::map.token(type);
1351 cerr <<
"Unknown Track file type " << type << endl;
1355 EST_TrackFile::Load_File * l_fun = EST_TrackFile::map.info(t).load;
1359 cerr <<
"Can't load tracks from file type" << type << endl;
1364 return (*l_fun)(filename, *
this, ishift, startt);
1367 EST_write_status EST_Track::save_channel_names(
const EST_String filename)
1371 if ((file=fopen(filename,
"wb"))==NULL)
1375 fprintf(file,
"%s\n", (
const char *)channel_name(c));
1382 EST_read_status EST_Track::load_channel_names(
const EST_String filename)
1385 static const int buffer_length = 100;
1386 char buffer[buffer_length];
1388 if ((file=fopen(filename,
"rb"))==NULL)
1389 return misc_read_error;
1393 if (!fgets(buffer, buffer_length, file))
1396 buffer[strlen(buffer)-1] =
'\0';
1409 float &
EST_Track::a(
float t, EST_ChannelType type, EST_InterpType interp)
1411 short c = NO_SUCH_CHANNEL;
1413 if (p_map!=0 && (c = p_map->
get(type)) != NO_SUCH_CHANNEL)
1414 return a(t, c, interp);
1417 cerr <<
"no channel '" << EST_default_channel_names.name(type) <<
"' = " << (int)type <<
"\n";
1434 EST_ChannelType type = names.token(p_channel_names(i));
1436 if (type != channel_unknown)
1456 int p = (*p_map)(type);
1457 return (p!= NO_SUCH_CHANNEL)?(p+offset): NO_SUCH_CHANNEL;
1462 float &
EST_Track::a(
int i, EST_ChannelType type,
int offset)
1464 short c = NO_SUCH_CHANNEL;
1466 if (p_map!=0 && ((c = p_map->
get(type)) != NO_SUCH_CHANNEL))
1470 cerr <<
"no channel '" << EST_default_channel_names.name(type) <<
"' = " << (int)type <<
"\n";
1476 EST_Track::IPointer_f::IPointer_f()
1481 EST_Track::IPointer_f::IPointer_f(
const IPointer_f &p)
1486 EST_Track::IPointer_f::~IPointer_f()
1496 #if defined(INSTANTIATE_TEMPLATES)