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,
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);
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;
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));
283 rate, st_short, EST_NATIVE_BO, 1,
293 EST_read_status
stat = read_error;
298 else if ((
ts.open(filename)) == -1)
300 cerr <<
"Wave load: can't open file \"" << filename <<
"\"" <<
endl;
315 EST_WaveFileType
t = EST_WaveFile::map.token(type);
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;
345 if (
nchan == EST_ALL)
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);
376 EST_WaveFileType
t = EST_WaveFile::map.token(
save_type);
384 EST_WaveFile::Save_TokenStream *
s_fun = EST_WaveFile::map.info(
t).save;
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);
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);
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);
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;
494 if (p_values.rateconv(p_sample_rate,
new_freq) != 0)
495 cerr <<
"rateconv: failed to convert from " << p_sample_rate <<
528 if (
abs(a_no_check(i,
j)) > max)
529 max =
abs(a_no_check(i,
j));
540 ns = a_no_check(i,
j);
542 ns = -a_no_check(i,
j);
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;
571 EST_error(
"Factor contour track exceeds waveform length (%d samples)",
574 start_sample =
static_cast<unsigned int>(
fc.t( 0 )*p_sample_rate );
578 end_sample =
static_cast<unsigned int>(
fc.t( k )*p_sample_rate );
587 ns = a_no_check(i,
j);
589 ns = -a_no_check(i,
j);
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;
625 cerr <<
"Cannot concatenate waveforms with differing numbers of channels\n";
632 w2.resample(p_sample_rate);
664 for (
int i = 0; i <
sig.num_samples(); ++i)
665 p_values <<
sig(i) <<
"\n";
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
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