45 #include "EST_cutils.h"
47 #include "EST_wave_utils.h"
48 #include "EST_wave_aux.h"
49 #include "EST_TNamedEnum.h"
50 #include "EST_WaveFile.h"
52 #include "EST_Track.h"
59 const int EST_Wave::default_sample_rate=16000;
79 short *memory,
int offset,
int sample_rate,
80 int free_when_destroyed)
83 p_values.
set_memory(memory, offset, samps, chans, free_when_destroyed);
87 void EST_Wave::default_vals(
int n,
int c)
91 p_sample_rate = default_sample_rate;
96 void EST_Wave::free_wave()
98 if (!p_values.p_sub_matrix)
103 EST_Wave::~EST_Wave()
108 void EST_Wave::copy_setup(
const EST_Wave &w)
110 p_sample_rate = w.p_sample_rate;
114 void EST_Wave::copy_data(
const EST_Wave &w)
116 p_values.
copy(w.p_values);
119 void EST_Wave::copy(
const EST_Wave &w)
130 cerr <<
"Attempt to access sample " << i <<
" of a " <<
num_samples() <<
" sample wave.\n";
137 cerr <<
"Attempt to access channel " << channel <<
" of a " <<
num_channels() <<
" channel wave.\n";
147 return ((
EST_Wave *)
this)->a(i,channel);
152 static short out_of_bound_value = 0;
157 out_of_bound_value = 0;
158 return out_of_bound_value;
161 return a_no_check(i,channel);
164 void EST_Wave::fill(
short v,
int channel)
166 if (channel == EST_ALL)
179 int offset,
int length,
182 EST_read_status stat = read_error;
185 if ((ts.
open(filename)) == -1)
187 cerr <<
"Wave load: can't open file \"" << filename <<
"\"" << endl;
191 stat =
load(ts,offset,length,rate);
197 int offset,
int length,
200 EST_read_status stat = read_error;
203 for(
int n=0; n< EST_WaveFile::map.n() ; n++)
205 EST_WaveFileType
t = EST_WaveFile::map.token(n);
210 EST_WaveFile::Info *info = &(EST_WaveFile::map.info(t));
212 if (! info->recognise)
215 EST_WaveFile::Load_TokenStream * l_fun =info->load;
221 stat = (*l_fun)(ts, *
this,
222 rate, st_short, EST_NATIVE_BO, 1,
227 set_file_type(EST_WaveFile::map.value(t));
230 else if (stat == read_error)
239 int offset,
int length,
242 EST_read_status stat = read_error;
246 ts.
open(stdin,FALSE);
247 else if ((ts.
open(filename)) == -1)
249 cerr <<
"Wave load: can't open file \"" << filename <<
"\"" << endl;
253 stat =
load(ts,type,offset,length,rate);
260 int offset,
int length,
263 EST_WaveFileType t = EST_WaveFile::map.token(type);
267 cerr <<
"Unknown Wave file type " << type << endl;
271 EST_WaveFile::Load_TokenStream * l_fun = EST_WaveFile::map.info(t).load;
275 cerr <<
"Can't load waves to files type " << type << endl;
279 set_file_type(EST_WaveFile::map.value(t));
280 return (*l_fun)(ts, *
this,
281 rate, st_short, EST_NATIVE_BO, 1,
288 const EST_String stype,
int bov,
int nc,
int offset,
291 EST_read_status stat = read_error;
295 ts.
open(stdin,FALSE);
296 else if ((ts.
open(filename)) == -1)
298 cerr <<
"Wave load: can't open file \"" << filename <<
"\"" << endl;
302 stat =
load_file(ts,type,sample_rate,stype,bov,nc,offset,length);
309 const EST_String stype,
int bov,
int nc,
int offset,
313 EST_WaveFileType t = EST_WaveFile::map.token(type);
315 EST_sample_type_t values_type = EST_sample_type_map.token(stype);
319 cerr <<
"Unknown Wave file type " << type << endl;
323 EST_WaveFile::Load_TokenStream * l_fun = EST_WaveFile::map.info(t).load;
327 cerr <<
"Can't load waves to files type " << type << endl;
331 return (*l_fun)(ts, *
this,
337 void EST_Wave::sub_wave(
EST_Wave &sw,
339 int start_c,
int nchan)
343 if (nchan == EST_ALL)
346 p_values.
sub_matrix(sw.p_values, offset, num, start_c, nchan);
350 EST_write_status EST_Wave::save(
const EST_String filename,
357 else if ((fp = fopen(filename,
"wb")) == NULL)
359 cerr <<
"Wave save: can't open output file \"" <<
360 filename <<
"\"" << endl;
364 EST_write_status r = save(fp,type);
370 EST_write_status EST_Wave::save(FILE *fp,
const EST_String type)
372 EST_String save_type = (type ==
"") ? DEF_FILE_TYPE : type;
374 EST_WaveFileType t = EST_WaveFile::map.token(save_type);
378 cerr <<
"Wave: unknown filetype in saving " << save_type << endl;
382 EST_WaveFile::Save_TokenStream * s_fun = EST_WaveFile::map.info(t).save;
386 cerr <<
"Can't save waves to files type " << save_type << endl;
390 return (*s_fun)(fp, *
this, st_short, EST_NATIVE_BO);
393 EST_write_status EST_Wave::save_file(
const EST_String filename,
401 else if ((fp = fopen(filename,
"wb")) == NULL)
403 cerr <<
"Wave save: can't open output file \"" <<
404 filename <<
"\"" << endl;
408 EST_write_status r = save_file(fp,ftype,stype,obo);
414 EST_write_status EST_Wave::save_file(FILE *fp,
418 EST_WaveFileType t = EST_WaveFile::map.token(ftype);
419 EST_sample_type_t
sample_type = EST_sample_type_map.token(stype);
423 cerr <<
"Unknown Wave file type " << ftype << endl;
427 EST_WaveFile::Save_TokenStream * s_fun = EST_WaveFile::map.info(t).save;
431 cerr <<
"Can't save waves to files type " << ftype << endl;
442 if (new_freq != p_sample_rate)
444 if (p_values.rateconv(p_sample_rate, new_freq) != 0)
445 cerr <<
"rateconv: failed to convert from " << p_sample_rate <<
446 " to " << new_freq <<
"\n";
464 if (abs(a_no_check(i,j)) > max)
465 max = abs(a_no_check(i,j));
466 if (fabs(max/32766.0-gain) < 0.001)
469 factor *= 32766.0/(float)max;
476 ns = a_no_check(i,j);
477 else if (factor == -1.0)
478 ns = -a_no_check(i,j);
481 nsf = (float)a_no_check(i,j) * factor;
483 ns = (int)(nsf - 0.5);
485 ns = (int)(nsf + 0.5);
488 a_no_check(i,j)= -32766;
490 a_no_check(i,j)= 32766;
498 int ns, start_sample, end_sample;
499 float target1, target2, increment, factor, nsf;
501 int fc_length = fc.
length();
504 cerr << ((int)(fc.
t(fc_length-1) * p_sample_rate)) << endl;
506 if( ((
int)(fc.
t(fc_length-1) * p_sample_rate)) >
num_samples() )
507 EST_error(
"Factor contour track exceeds waveform length (%d samples)",
510 start_sample =
static_cast<unsigned int>( fc.
t( 0 )*p_sample_rate );
513 for (
int k = 1; k<fc_length; ++k ){
514 end_sample =
static_cast<unsigned int>( fc.
t( k )*p_sample_rate );
517 increment = (target2-target1)/(end_sample-start_sample+1);
520 for(
int i=start_sample; i<end_sample; ++i, factor+=increment )
521 for(
int j=0; j<_num_channels; ++j ){
523 ns = a_no_check(i,j);
524 else if (factor == -1.0)
525 ns = -a_no_check(i,j);
528 nsf = (float)a_no_check(i,j) * factor;
530 ns = (int)(nsf - 0.5);
532 ns = (int)(nsf + 0.5);
535 a_no_check(i,j)= -32766;
537 a_no_check(i,j)= 32766;
541 start_sample = end_sample;
561 cerr <<
"Cannot concatenate waveforms with differing numbers of channels\n";
589 resize(r_samples, r_channels);
593 a(i,k+o_channels) += w.
a(i, k);
598 ostream& operator << (ostream& p_values,
const EST_Wave &sig)
601 p_values << sig(i) <<
"\n";
607 { (void)a; (void)b;
return 1; }
609 { (void)a; (void)b;
return 0; }