41#include "EST_WaveFile.h"
43#include "EST_cutils.h"
44#include "EST_Option.h"
45#include "EST_io_aux.h"
53 short **data,
int *nsamp,
int *nchan,
56 EST_sample_type_t *stype,
int *bo,
57 int offset,
int length);
60EST_write_status (*standard_save_fn_fp)(FILE *fp,
62 int offset,
int nsamp,
64 EST_sample_type_t stype,
int bo);
67EST_write_status (*standard_save_header_fn_fp)(FILE *fp,
70 EST_sample_type_t stype,
int bo);
73EST_read_status load_using(standard_load_fn_fp fn,
77 EST_sample_type_t stype,
int bo,
int nchan,
78 int offset,
int length)
85 EST_read_status status = (*fn)(ts,
86 &data, &nsamp, &nchan,
91 if (status == read_ok)
93 short *data2 =
new short[nsamp*nchan];
94 memcpy(data2, data, nsamp*nchan*
sizeof(
short));
96 wv.values().
set_memory(data2, 0, nsamp, nchan, TRUE);
104EST_write_status save_using(standard_save_fn_fp fn,
106 EST_sample_type_t stype,
int bo)
108EST_write_status status = (*fn)(fp,
118EST_write_status save_header_using(standard_save_header_fn_fp fn,
120 EST_sample_type_t stype,
int bo)
123EST_write_status status = (*fn)(fp,
133 EST_sample_type_t stype,
int bo,
int nchan,
134 int offset,
int length)
136 return load_using(load_wave_nist,
142EST_write_status EST_WaveFile::save_nist(FILE *fp,
144 EST_sample_type_t stype,
int bo)
146 return save_using(save_wave_nist, fp, wv, stype, bo);
149EST_write_status EST_WaveFile::save_nist_data(FILE *fp,
151 EST_sample_type_t stype,
int bo)
153 return save_using(save_wave_nist_data, fp, wv, stype, bo);
156EST_write_status EST_WaveFile::save_nist_header(FILE *fp,
158 EST_sample_type_t stype,
int bo)
160 return save_header_using(save_wave_nist_header, fp, wv, stype, bo);
166 EST_sample_type_t stype,
int bo,
int nchan,
167 int offset,
int length)
170 return load_using(load_wave_est,
177EST_write_status EST_WaveFile::save_est(FILE *fp,
179 EST_sample_type_t stype,
int bo)
181 return save_using(save_wave_est,
186EST_write_status EST_WaveFile::save_est_data(FILE *fp,
188 EST_sample_type_t stype,
int bo)
190 return save_using(save_wave_est_data,
195EST_write_status EST_WaveFile::save_est_header(FILE *fp,
197 EST_sample_type_t stype,
int bo)
199 return save_header_using(save_wave_est_header,
207 EST_sample_type_t stype,
int bo,
int nchan,
208 int offset,
int length)
210 return load_using(load_wave_aiff,
216EST_write_status EST_WaveFile::save_aiff(FILE *fp,
218 EST_sample_type_t stype,
int bo)
220 return save_using(save_wave_aiff, fp, wv, stype, bo);
223EST_write_status EST_WaveFile::save_aiff_data(FILE *fp,
225 EST_sample_type_t stype,
int bo)
227 return save_using(save_wave_aiff_data, fp, wv, stype, bo);
230EST_write_status EST_WaveFile::save_aiff_header(FILE *fp,
232 EST_sample_type_t stype,
int bo)
234 return save_header_using(save_wave_aiff_header, fp, wv, stype, bo);
240 EST_sample_type_t stype,
int bo,
int nchan,
241 int offset,
int length)
243 return load_using(load_wave_riff,
249EST_write_status EST_WaveFile::save_riff(FILE *fp,
251 EST_sample_type_t stype,
int bo)
253 return save_using(save_wave_riff, fp, wv, stype, bo);
256EST_write_status EST_WaveFile::save_riff_data(FILE *fp,
258 EST_sample_type_t stype,
int bo)
260 return save_using(save_wave_riff_data, fp, wv, stype, bo);
263EST_write_status EST_WaveFile::save_riff_header(FILE *fp,
265 EST_sample_type_t stype,
int bo)
267 return save_header_using(save_wave_riff_header, fp, wv, stype, bo);
273 EST_sample_type_t stype,
int bo,
int nchan,
274 int offset,
int length)
276 return load_using(load_wave_sd,
282EST_write_status EST_WaveFile::save_esps(FILE *fp,
284 EST_sample_type_t stype,
int bo)
286 return save_using(save_wave_sd,
291EST_write_status EST_WaveFile::save_esps_data(FILE *fp,
293 EST_sample_type_t stype,
int bo)
295 return save_using(save_wave_sd_data,
300EST_write_status EST_WaveFile::save_esps_header(FILE *fp,
302 EST_sample_type_t stype,
int bo)
304 return save_header_using(save_wave_sd_header,
312 EST_sample_type_t stype,
int bo,
int nchan,
313 int offset,
int length)
315 return load_using(load_wave_audlab,
321EST_write_status EST_WaveFile::save_audlab(FILE *fp,
323 EST_sample_type_t stype,
int bo)
325 return save_using(save_wave_audlab, fp, wv, stype, bo);
328EST_write_status EST_WaveFile::save_audlab_data(FILE *fp,
330 EST_sample_type_t stype,
int bo)
332 return save_using(save_wave_audlab_data, fp, wv, stype, bo);
335EST_write_status EST_WaveFile::save_audlab_header(FILE *fp,
337 EST_sample_type_t stype,
int bo)
339 return save_header_using(save_wave_audlab_header, fp, wv, stype, bo);
345 EST_sample_type_t stype,
int bo,
int nchan,
346 int offset,
int length)
348 return load_using(load_wave_snd,
354EST_write_status EST_WaveFile::save_snd(FILE *fp,
356 EST_sample_type_t stype,
int bo)
358 return save_using(save_wave_snd, fp, wv, stype, bo);
361EST_write_status EST_WaveFile::save_snd_data(FILE *fp,
363 EST_sample_type_t stype,
int bo)
365 return save_using(save_wave_snd_data, fp, wv, stype, bo);
368EST_write_status EST_WaveFile::save_snd_header(FILE *fp,
370 EST_sample_type_t stype,
int bo)
372 return save_header_using(save_wave_snd_header, fp, wv, stype, bo);
380 EST_sample_type_t stype,
int bo,
int nchan,
381 int offset,
int length)
388 EST_read_status status = load_wave_raw(ts,
389 &data, &nsamp, &nchan,
393 rate, stype, bo, nchan);
395 if (status == read_ok)
397 wv.values().
set_memory(data, 0, nsamp, nchan, TRUE);
404EST_write_status EST_WaveFile::save_raw(FILE *fp,
406 EST_sample_type_t stype,
int bo)
408EST_write_status status = save_wave_raw(fp,
409 (
short *)wv.values().
memory(),
416EST_write_status EST_WaveFile::save_raw_data(FILE *fp,
418 EST_sample_type_t stype,
int bo)
420return save_raw(fp, wv, stype, bo);
424EST_write_status EST_WaveFile::save_raw_header(FILE *fp,
426 EST_sample_type_t stype,
int bo)
428 return save_header_using(save_wave_raw_header, fp, wv, stype, bo);
434 EST_sample_type_t stype,
int bo,
int nchan,
435 int offset,
int length)
437 return load_using(load_wave_ulaw,
443EST_write_status EST_WaveFile::save_ulaw(FILE *fp,
445 EST_sample_type_t stype,
int bo)
449 return save_using(save_wave_ulaw, fp, localwv, stype, bo);
452EST_write_status EST_WaveFile::save_ulaw_data(FILE *fp,
454 EST_sample_type_t stype,
int bo)
458 return save_using(save_wave_ulaw_data, fp, localwv, stype, bo);
462EST_write_status EST_WaveFile::save_ulaw_header(FILE *fp,
464 EST_sample_type_t stype,
int bo)
468 return save_header_using(save_wave_ulaw_header, fp, localwv, stype, bo);
474 EST_sample_type_t stype,
int bo,
int nchan,
475 int offset,
int length)
477 return load_using(load_wave_alaw,
483EST_write_status EST_WaveFile::save_alaw_header(FILE *fp,
485 EST_sample_type_t stype,
int bo)
489 return save_header_using(save_wave_alaw_header, fp, localwv, stype, bo);
492EST_write_status EST_WaveFile::save_alaw_data(FILE *fp,
494 EST_sample_type_t stype,
int bo)
498 return save_using(save_wave_alaw_data, fp, localwv, stype, bo);
501EST_write_status EST_WaveFile::save_alaw(FILE *fp,
503 EST_sample_type_t stype,
int bo)
507 return save_using(save_wave_alaw, fp, localwv, stype, bo);
510static int parse_esps_r_option(
EST_String arg,
int &offset,
int &length)
526 cerr <<
"Argument to -r is illformed " << arg << endl;
532 cerr <<
"First argument to -r must be an integer " << arg << endl;
543 length = atoi(e) - offset;
547 cerr <<
"length is negative or zero " << arg << endl;
560 EST_read_status rval;
562 int offset=0, length=0;
566 fname = stdin_to_file();
571 num_channels = al.
ival(
"-n", 0);
589 sample_type = al.
val(
"-istype");
594 file_type = al.
val(
"-itype");
600 sample_rate = al.
ival(
"-f", 0);
601 else if ((sr = getenv(
"NA_PLAY_SAMPLE_RATE")) != NULL)
603 sample_rate = atoi(sr);
604 cerr <<
"Warning: no sample rate specified, " <<
605 " using NA_PLAY_SAMPLE_RATE environment variable\n";
609 sample_rate = EST_Wave::default_sample_rate;
610 if (file_type ==
"raw")
611 cerr <<
"Warning: no sample rate specified - using default " <<
615 if (file_type ==
"ulaw")
618 sample_type =
"mulaw";
623 if (parse_esps_r_option(al.
val(
"-r"), offset, length) != 0)
630 bo = str_to_bo(
"swap");
632 bo = str_to_bo(
"native");
634 bo = str_to_bo(al.
val(
"-ibo"));
636 if (file_type ==
"" ||file_type ==
"undef")
637 rval = sig.
load(fname, offset, length, sample_rate);
639 rval = sig.
load_file(fname,file_type, sample_rate,
640 sample_type, bo, num_channels, offset, length);
642 if ((rval == wrong_format) && (al.
present(
"-basic")))
647 "mulaw", bo, 1, offset, length);
649 if (rval != format_ok)
651 if (in_file ==
"-") unlink(fname);
652 cerr <<
"Cannot recognize file format or cannot access file: \"" << in_file <<
"\"\n";
655 if (file_type ==
"alaw")
665 if (in_file ==
"-") unlink(fname);
676 file_type = al.
val(
"-otype");
678 file_type = sig.file_type();
681 sample_type = al.
val(
"-ostype");
683 sample_type =
"undef";
686 bo = str_to_bo(
"swap");
688 bo = str_to_bo(
"native");
691 bo = str_to_bo(al.
val(
"-obo"));
693 if (sample_type ==
"undef" || sample_type ==
"")
694 sample_type =
"short";
696 if (sig.save_file(out_file, file_type,
697 sample_type, bo) != write_ok)
699 cerr <<
"Cannot write file: \"" << out_file <<
"\"\n";
710 for(
int n=0; n< EST_WaveFile::map.n() ; n++)
712 const char *nm = EST_WaveFile::map.name(EST_WaveFile::map.token(n));
723EST_String EST_WaveFile::options_supported(
void)
725 EST_String s(
"Available wave file formats:\n");
727 for(
int n=0; n< EST_WaveFile::map.n() ; n++)
729 const char *nm = EST_WaveFile::map.name(EST_WaveFile::map.token(n));
730 const char *d = EST_WaveFile::map.info(EST_WaveFile::map.token(n)).description;
737typedef struct TInfo {
739 const char *description;
748 { wff_none, { NULL },
749 { FALSE, NULL, NULL, NULL, NULL,
"unknown track file type"} },
750 { wff_nist, {
"nist",
"timit" },
751 { TRUE, EST_WaveFile::load_nist, EST_WaveFile::save_nist,
752 EST_WaveFile::save_nist_header, EST_WaveFile::save_nist_data,
755 { TRUE, EST_WaveFile::load_est, EST_WaveFile::save_est,
756 EST_WaveFile::save_est_header, EST_WaveFile::save_est_data,
758 { wff_esps, {
"esps",
"sd"},
759 { TRUE, EST_WaveFile::load_esps, EST_WaveFile::save_esps,
760 EST_WaveFile::save_esps_header, EST_WaveFile::save_esps_data,
761 "esps SD waveform" } },
762 { wff_audlab, {
"audlab",
"vox"},
763 { TRUE, EST_WaveFile::load_audlab, EST_WaveFile::save_audlab,
764 EST_WaveFile::save_audlab_header, EST_WaveFile::save_audlab_data,
765 "audlab waveform" } },
766 { wff_snd, {
"snd",
"au"},
767 { TRUE, EST_WaveFile::load_snd, EST_WaveFile::save_snd,
768 EST_WaveFile::save_snd_header, EST_WaveFile::save_snd_data,
770 { wff_aiff, {
"aiff" },
771 { TRUE, EST_WaveFile::load_aiff, EST_WaveFile::save_aiff,
772 EST_WaveFile::save_aiff_header, EST_WaveFile::save_aiff_data,
773 "Apple aiff file" } },
774 { wff_riff, {
"riff",
"wav" },
775 { TRUE, EST_WaveFile::load_riff, EST_WaveFile::save_riff,
776 EST_WaveFile::save_riff_header, EST_WaveFile::save_riff_data,
777 "Microsoft wav/riff file" } },
778 { wff_raw, {
"raw" },
779 { FALSE, EST_WaveFile::load_raw, EST_WaveFile::save_raw,
780 EST_WaveFile::save_raw_header, EST_WaveFile::save_raw_data,
781 "Headerless File" } },
782 { wff_ulaw, {
"ulaw",
"basic" },
783 { FALSE, EST_WaveFile::load_ulaw, EST_WaveFile::save_ulaw,
784 EST_WaveFile::save_ulaw_header, EST_WaveFile::save_ulaw_data,
785 "Headerless 8K ulaw File" } },
791#if defined(INSTANTIATE_TEMPLATES)
793#include "../base_class/EST_TNamedEnum.cc"
int ival(const EST_String &rkey, int m=1) const
EST_String before(int pos, int len=0) const
Part before position.
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)
int contains(const char *s, int pos=-1) const
Does it contain this substring?
EST_String after(int pos, int len=1) const
Part after pos+len.
int matches(const char *e, int pos=0) const
Exactly match this string?
int add_item(const K &rkey, const V &rval, int no_search=0)
add key-val pair to list
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
const int present(const K &rkey) const
Returns true if key is present.
void set_memory(T *buffer, int offset, int rows, int columns, int free_when_destroyed=0)
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)
int num_channels() const
return the number of channels in the waveform
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
EST_String sample_type() const
int num_samples() const
return the number of samples in the waveform
void set_sample_rate(const int n)
Set sampling rate to n