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"
56#define sgn(x) (x>0?1:x?-1:0)
61const int EST_Wave::default_sample_rate=16000;
81 short *memory,
int offset,
int sample_rate,
82 int free_when_destroyed)
85 p_values.
set_memory(memory, offset, samps, chans, free_when_destroyed);
89void EST_Wave::default_vals(
int n,
int c)
93 p_sample_rate = default_sample_rate;
98void EST_Wave::free_wave()
100 if (!p_values.p_sub_matrix)
110void EST_Wave::copy_setup(
const EST_Wave &w)
112 p_sample_rate = w.p_sample_rate;
116void EST_Wave::copy_data(
const EST_Wave &w)
118 p_values.
copy(w.p_values);
121void EST_Wave::copy(
const EST_Wave &w)
132 cerr <<
"Attempt to access sample " << i <<
" of a " <<
num_samples() <<
" sample wave.\n";
139 cerr <<
"Attempt to access channel " << channel <<
" of a " <<
num_channels() <<
" channel wave.\n";
149 return ((
EST_Wave *)
this)->a(i,channel);
154 static short out_of_bound_value = 0;
159 out_of_bound_value = 0;
160 return out_of_bound_value;
163 return a_no_check(i,channel);
166void EST_Wave::fill(
short v,
int channel)
168 if (channel == EST_ALL)
181 int offset,
int length,
184 EST_read_status stat = read_error;
187 if ((ts.
open(filename)) == -1)
189 cerr <<
"Wave load: can't open file \"" << filename <<
"\"" << endl;
199 int offset,
int length,
202 EST_read_status stat = read_error;
205 for(
int n=0; n< EST_WaveFile::map.n() ; n++)
207 EST_WaveFileType
t = EST_WaveFile::map.token(n);
212 EST_WaveFile::Info *info = &(EST_WaveFile::map.info(
t));
214 if (! info->recognise)
217 EST_WaveFile::Load_TokenStream * l_fun =info->load;
223 stat = (*l_fun)(ts, *
this,
224 rate, st_short, EST_NATIVE_BO, 1,
229 set_file_type(EST_WaveFile::map.value(
t));
232 else if (stat == read_error)
241 int offset,
int length,
244 EST_read_status stat = read_error;
248 ts.
open(stdin,FALSE);
249 else if ((ts.
open(filename)) == -1)
251 cerr <<
"Wave load: can't open file \"" << filename <<
"\"" << endl;
262 int offset,
int length,
265 EST_WaveFileType
t = EST_WaveFile::map.token(type);
269 cerr <<
"Unknown Wave file type " << type << endl;
273 EST_WaveFile::Load_TokenStream * l_fun = EST_WaveFile::map.info(
t).load;
277 cerr <<
"Can't load waves to files type " << type << endl;
281 set_file_type(EST_WaveFile::map.value(
t));
282 return (*l_fun)(ts, *
this,
283 rate, st_short, EST_NATIVE_BO, 1,
290 const EST_String stype,
int bov,
int nc,
int offset,
293 EST_read_status stat = read_error;
297 ts.
open(stdin,FALSE);
298 else if ((ts.
open(filename)) == -1)
300 cerr <<
"Wave load: can't open file \"" << filename <<
"\"" << endl;
311 const EST_String stype,
int bov,
int nc,
int offset,
315 EST_WaveFileType
t = EST_WaveFile::map.token(type);
317 EST_sample_type_t values_type = EST_sample_type_map.token(stype);
321 cerr <<
"Unknown Wave file type " << type << endl;
325 EST_WaveFile::Load_TokenStream * l_fun = EST_WaveFile::map.info(
t).load;
329 cerr <<
"Can't load waves to files type " << type << endl;
333 return (*l_fun)(ts, *
this,
339void EST_Wave::sub_wave(
EST_Wave &sw,
341 int start_c,
int nchan)
345 if (nchan == EST_ALL)
348 p_values.
sub_matrix(sw.p_values, offset, num, start_c, nchan);
352EST_write_status EST_Wave::save(
const EST_String filename,
359 else if ((fp = fopen(filename,
"wb")) == NULL)
361 cerr <<
"Wave save: can't open output file \"" <<
362 filename <<
"\"" << endl;
366 EST_write_status r = save(fp,type);
372EST_write_status EST_Wave::save(FILE *fp,
const EST_String type)
374 EST_String save_type = (type ==
"") ? DEF_FILE_TYPE : type;
376 EST_WaveFileType
t = EST_WaveFile::map.token(save_type);
380 cerr <<
"Wave: unknown filetype in saving " << save_type << endl;
384 EST_WaveFile::Save_TokenStream * s_fun = EST_WaveFile::map.info(
t).save;
388 cerr <<
"Can't save waves to files type " << save_type << endl;
392 return (*s_fun)(fp, *
this, st_short, EST_NATIVE_BO);
395EST_write_status EST_Wave::save_file(
const EST_String filename,
403 else if ((fp = fopen(filename, mode)) == NULL)
405 cerr <<
"Wave save: can't open output file \"" <<
406 filename <<
"\"" << endl;
410 EST_write_status r = save_file(fp,ftype,stype,obo);
416EST_write_status EST_Wave::save_file(FILE *fp,
420 EST_WaveFileType
t = EST_WaveFile::map.token(ftype);
421 EST_sample_type_t
sample_type = EST_sample_type_map.token(stype);
425 cerr <<
"Unknown Wave file type " << ftype << endl;
429 EST_WaveFile::Save_TokenStream * s_fun = EST_WaveFile::map.info(
t).save;
433 cerr <<
"Can't save waves to files type " << ftype << endl;
440EST_write_status EST_Wave::save_file_data(FILE *fp,
444 EST_WaveFileType
t = EST_WaveFile::map.token(ftype);
445 EST_sample_type_t
sample_type = EST_sample_type_map.token(stype);
449 cerr <<
"Unknown Wave file type " << ftype << endl;
453 EST_WaveFile::Save_TokenStream * s_fun = EST_WaveFile::map.info(
t).save_data;
457 cerr <<
"Can't save wave data to files type " << ftype << endl;
465EST_write_status EST_Wave::save_file_header(FILE *fp,
469 EST_WaveFileType
t = EST_WaveFile::map.token(ftype);
470 EST_sample_type_t
sample_type = EST_sample_type_map.token(stype);
474 cerr <<
"Unknown Wave file type " << ftype << endl;
478 EST_WaveFile::Save_TokenStream * s_fun = EST_WaveFile::map.info(
t).save_header;
482 cerr <<
"Can't save wave header to files type " << ftype << endl;
492 if (new_freq != p_sample_rate)
494 if (p_values.rateconv(p_sample_rate, new_freq) != 0)
495 cerr <<
"rateconv: failed to convert from " << p_sample_rate <<
496 " to " << new_freq <<
"\n";
512 a_no_check(i,j) = lim * (sgn(x)*(log(1+(mu/lim)*abs(x))/log(1+mu)));
528 if (abs(a_no_check(i,j)) > max)
529 max = abs(a_no_check(i,j));
530 if (fabs(max/32766.0-gain) < 0.001)
533 factor *= 32766.0/(float)max;
540 ns = a_no_check(i,j);
541 else if (factor == -1.0)
542 ns = -a_no_check(i,j);
545 nsf = (float)a_no_check(i,j) * factor;
547 ns = (int)(nsf - 0.5);
549 ns = (int)(nsf + 0.5);
552 a_no_check(i,j)= -32766;
554 a_no_check(i,j)= 32766;
562 int ns, start_sample, end_sample;
563 float target1, target2, increment, factor, nsf;
565 int fc_length = fc.
length();
568 cerr << ((int)(fc.
t(fc_length-1) * p_sample_rate)) << endl;
570 if( ((
int)(fc.
t(fc_length-1) * p_sample_rate)) >
num_samples() )
571 EST_error(
"Factor contour track exceeds waveform length (%d samples)",
574 start_sample =
static_cast<unsigned int>( fc.
t( 0 )*p_sample_rate );
577 for (
int k = 1; k<fc_length; ++k ){
578 end_sample =
static_cast<unsigned int>( fc.
t( k )*p_sample_rate );
581 increment = (target2-target1)/(end_sample-start_sample+1);
584 for(
int i=start_sample; i<end_sample; ++i, factor+=increment )
585 for(
int j=0; j<_num_channels; ++j ){
587 ns = a_no_check(i,j);
588 else if (factor == -1.0)
589 ns = -a_no_check(i,j);
592 nsf = (float)a_no_check(i,j) * factor;
594 ns = (int)(nsf - 0.5);
596 ns = (int)(nsf + 0.5);
599 a_no_check(i,j)= -32766;
601 a_no_check(i,j)= 32766;
605 start_sample = end_sample;
625 cerr <<
"Cannot concatenate waveforms with differing numbers of channels\n";
653 resize(r_samples, r_channels);
657 a(i,k+o_channels) += w.
a(i, k);
662ostream& operator << (ostream& p_values,
const EST_Wave &sig)
665 p_values << sig(i) <<
"\n";
671 { (void)a; (void)b;
return 1; }
673 { (void)a; (void)b;
return 0; }
void fill(const T &v)
fill matrix with value v
INLINE const T & a_no_check(int row, int col) const
const access with no bounds check, care recommend
void sub_matrix(EST_TMatrix< T > &sm, int r=0, int numr=EST_ALL, int c=0, int numc=EST_ALL)
Make the matrix {\tt sm} a window into this matrix.
void set_memory(T *buffer, int offset, int rows, int columns, int free_when_destroyed=0)
EST_TMatrix & add_rows(const EST_TMatrix &s)
The two versions of what might have been operator +=.
void resize(int rows, int cols, int set=1)
resize matrix
void copy(const EST_TSimpleMatrix< T > &a)
copy one matrix into another
void close(void)
Close stream.
int tell(void) const
tell, synonym for filepos
int open(const EST_String &filename)
open a \Ref{EST_TokenStream} for a file.
int seek(int position)
seek, reposition file pointer
float & t(int i=0)
return time position of frame i
float & a(int i, int c=0)
int length() const
return number of frames in track
EST_read_status load_file(const EST_String filename, const EST_String filetype, int sample_rate, const EST_String sample_type, int bo, int nc, int offset=0, int length=0)
short & a(int i, int channel=0)
int num_channels() const
return the number of channels in the waveform
int length() const
return the size of the waveform, i.e. the number of samples.
short & a_safe(int i, int channel=0)
EST_Wave & operator=(const EST_Wave &w)
Assignment operator.
void compress(float mu, float limit)
Dynamic Range Compression - SaiKrishna May 2017.
int sample_rate() const
return the sampling rate (frequency)
EST_read_status load(const EST_String filename, int offset=0, int length=0, int rate=default_sample_rate)
void resample(int rate)
Resample waveform to rate
void resize(int num_samples, int num_channels=EST_ALL, int set=1)
resize the waveform
EST_Wave & operator|=(const EST_Wave &a)
float t(int i) const
return the time position in seconds of the ith sample
void rescale(float gain, int normalize=0)
EST_Wave()
default constructor
EST_String sample_type() const
EST_Wave & operator+=(const EST_Wave &a)
int num_samples() const
return the number of samples in the waveform
void set_sample_rate(const int n)
Set sampling rate to n