IT++ Logo
audiofile.h
Go to the documentation of this file.
1 
29 #ifndef AUDIOFILE_H
30 #define AUDIOFILE_H
31 
32 #include <string>
33 #include <algorithm>
34 #include <itpp/base/vec.h>
35 #include <itpp/base/mat.h>
36 #include <itpp/base/binfile.h>
38 #include <itpp/itexports.h>
39 
40 
41 namespace itpp
42 {
43 
44 
67 {
68 public:
70  Audio_Stream_Description():_encoding(enc_unknown), _sampling_rate(0), _num_channels(0){}
72  Audio_Stream_Description(Audio_Encoding e, int sr, int nc = 1):
73  _encoding(e), _sampling_rate(sr), _num_channels(nc){}
75  Audio_Stream_Description& set_encoding(Audio_Encoding e) {_encoding = e; return *this;}
77  Audio_Stream_Description& set_sampling_rate(int sr) {_sampling_rate = sr; return *this;}
79  Audio_Stream_Description& set_num_channels(int nc) {_num_channels = nc; return *this;}
81  Audio_Stream_Description& set_description(const std::string& d) {_description = d; return *this;}
83  Audio_Encoding get_encoding() const {return _encoding;}
85  int get_sampling_rate() const {return _sampling_rate;}
87  int get_num_channels() const {return _num_channels;}
89  const std::string& get_description() const {return _description;}
90 private:
92  Audio_Encoding _encoding;
94  int _sampling_rate;
96  int _num_channels;
98  std::string _description;
99 };
100 
102 inline bool is_valid(const Audio_Stream_Description& d)
103 {
104  if(!encoded_sample_size(d.get_encoding())) return false;
105  if(!d.get_num_channels()) return false;
106  return true;
107 }
108 
110 namespace audiofile_details{
111 
112  //abstract interfaces to read and write audio samples to streams
113 
114  class Audio_Samples_Reader_If
115  {
116  public:
117  virtual bool read_sample(double& s, int ch) = 0;
118  virtual vec read_channel(int n, int ch) = 0;
119  virtual mat read(int n) = 0;
120  virtual std::streamoff tell() const = 0;
121  virtual bool seek(std::streamoff n) = 0;
122  virtual std::streamoff num_samples() = 0;
123  virtual ~Audio_Samples_Reader_If() {}
124  };
125 
126 
127  class Audio_Samples_Writer_If
128  {
129  public:
130  virtual bool write_sample(const double& s, int ch) = 0;
131  virtual bool write_channel(const vec& s, int ch) = 0;
132  //Write n samples to audio channel ch
133  virtual bool write(const mat& s) = 0;
134  virtual std::streamoff tell() const = 0;
135  virtual bool seek(std::streamoff n) = 0;
136  virtual std::streamoff num_samples() = 0;
137  virtual ~Audio_Samples_Writer_If() {}
138  };
139 }
141 
176 class ITPP_EXPORT SND_In_File
177 {
178 public:
180  SND_In_File();
182  SND_In_File(const char* fname);
184  ~SND_In_File();
186  bool open(const char* fname);
188  void close();
190  Audio_Stream_Description get_description() const {return *_description;}
192  bool seek_read(std::streamoff pos)
193  {
194  if((pos > _num_samples) || (pos < 0))
195  return false;
196 
197  if(_samples_reader)
198  return _samples_reader->seek(pos);
199  else
200  return false;
201  }
203  std::streamoff tell_read()
204  {
205  if(_samples_reader)
206  return _samples_reader->tell();
207  else
208  return -1;
209  }
211  std::streamoff num_samples() const {return _num_samples;}
213  bool read_sample(double& s, int ch = 0)
214  {
215  if(_samples_reader)
216  return _samples_reader->read_sample(s,ch);
217  else
218  return false;
219  }
221  vec read_channel(int n, int ch = 0)
222  {
223  if(_samples_reader)
224  return _samples_reader->read_channel(n,ch);
225  else
226  return vec();
227  }
229  mat read(int n)
230  {
231  if(_samples_reader)
232  return _samples_reader->read(n);
233  else
234  return mat();
235  }
236 private:
238  bifstream _str;
240  std::streamoff _num_samples;
242  audiofile_details::Audio_Samples_Reader_If* _samples_reader;
244  Audio_Stream_Description* _description;
245 };
246 
282 class ITPP_EXPORT SND_Out_File
283 {
284 public:
286  SND_Out_File();
288  SND_Out_File(const char *fname, const Audio_Stream_Description& d);
290  ~SND_Out_File();
292  bool open(const char *fname, const Audio_Stream_Description& d);
294  void close();
296  Audio_Stream_Description get_description() const {return *_description;}
298  bool seek_write(std::streamoff pos)
299  {
300  if((pos > _num_samples) || (pos < 0))
301  return false;
302 
303  if(_samples_writer)
304  return _samples_writer->seek(pos);
305  else
306  return false;
307  }
309  std::streamoff tell_write()
310  {
311  if(_samples_writer)
312  return _samples_writer->tell();
313  else
314  return -1;
315  }
317  std::streamoff num_samples() const {return _num_samples;}
319  bool write_sample(const double &s, int ch = 0)
320  {
321  if(_samples_writer){
322  bool ret = _samples_writer->write_sample(s,ch);
323  if(ret){
324  _num_samples = std::max(_num_samples, _samples_writer->tell());
325  }
326  return ret;
327  }
328  else
329  return false;
330  }
332  bool write_channel(const vec &v, int ch = 0)
333  {
334  if(_samples_writer){
335  bool ret = _samples_writer->write_channel(v,ch);
336  if(ret){
337  _num_samples = std::max(_num_samples, _samples_writer->tell());
338  }
339  return ret;
340  }
341  else
342  return false;
343  }
345  bool write(const mat &m)
346  {
347  if(_samples_writer){
348  bool ret = _samples_writer->write(m);
349  if(ret){
350  _num_samples = std::max(_num_samples, _samples_writer->tell());
351  }
352  return ret;
353  }
354  else
355  return false;
356  }
357 private:
359  bofstream _str;
361  std::streamoff _num_samples;
363  audiofile_details::Audio_Samples_Writer_If* _samples_writer;
365  Audio_Stream_Description* _description;
366 };
367 
374 class ITPP_EXPORT SND_IO_File
375 {
376 public:
378  SND_IO_File();
380  SND_IO_File(const char *fname);
382  SND_IO_File(const char *fname, const Audio_Stream_Description& d);
384  ~SND_IO_File();
386  bool open(const char *fname);
388  bool open(const char *fname, const Audio_Stream_Description& d);
390  void close();
392  Audio_Stream_Description get_description() const {return *_description;}
394  bool seek_read(std::streamoff pos)
395  {
396  if((pos > _num_samples) || (pos < 0))
397  return false;
398 
399  if(_samples_reader)
400  return _samples_reader->seek(pos);
401  else
402  return false;
403  }
405  std::streamoff tell_read()
406  {
407  if(_samples_reader)
408  return _samples_reader->tell();
409  else
410  return -1;
411  }
413  bool seek_write(std::streamoff pos)
414  {
415  if((pos > _num_samples) || (pos < 0))
416  return false;
417 
418  if(_samples_writer)
419  return _samples_writer->seek(pos);
420  else
421  return false;
422  }
424  std::streamoff tell_write()
425  {
426  if(_samples_writer)
427  return _samples_writer->tell();
428  else
429  return -1;
430  }
432  std::streamoff num_samples() const {return _num_samples;}
434  bool read_sample(double& s, int ch = 0)
435  {
436  if(_samples_reader)
437  return _samples_reader->read_sample(s,ch);
438  else
439  return false;
440  }
442  vec read_channel(int n, int ch = 0)
443  {
444  if(_samples_reader)
445  return _samples_reader->read_channel(n,ch);
446  else
447  return vec();
448  }
450  mat read(int n)
451  {
452  if(_samples_reader)
453  return _samples_reader->read(n);
454  else
455  return mat();
456  }
457 
459  bool write_sample(const double &s, int ch = 0)
460  {
461  if(_samples_writer){
462  bool ret = _samples_writer->write_sample(s,ch);
463  if(ret){
464  _num_samples = std::max(_num_samples, _samples_writer->tell());
465  }
466  return ret;
467  }
468  else
469  return false;
470  }
472  bool write_channel(const vec &v, int ch = 0)
473  {
474  if(_samples_writer){
475  bool ret = _samples_writer->write_channel(v,ch);
476  if(ret){
477  _num_samples = std::max(_num_samples, _samples_writer->tell());
478  }
479  return ret;
480  }
481  else
482  return false;
483  }
485  bool write(const mat &m)
486  {
487  if(_samples_writer){
488  bool ret = _samples_writer->write(m);
489  if(ret){
490  _num_samples = std::max(_num_samples, _samples_writer->tell());
491  }
492  return ret;
493  }
494  else
495  return false;
496  }
497 private:
499  bfstream _str;
501  std::streamoff _num_samples;
503  audiofile_details::Audio_Samples_Reader_If* _samples_reader;
505  audiofile_details::Audio_Samples_Writer_If* _samples_writer;
507  Audio_Stream_Description* _description;
508 };
509 
511 /*
512  \brief SAP audio file input class
513  \ingroup audio
514 
515  ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!!
516 */
517 /*
518  class SAP_In_File : virtual public Audio_File {
519  public:
520  // Constructor
521  SAP_In_File();
522  // Open the file {\em fname}.
523  SAP_In_File(const char *fname);
524  // Destructor
525  virtual ~SAP_In_File() { close(); }
526 
527  // Open the file {\em fname}.
528  virtual bool open(const char *fname);
529  // Close the file.
530  virtual void close();
531 
532  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
533  virtual bool seek_read(int pos);
534  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
535  virtual int tell_read();
536 
537  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
538  bool read(vec &v);
539  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
540  bool read(vec &v, int n);
541 
542  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
543  const char *get_header() { return header; }
544 
545  protected:
546  char header[SAP_HEADER_SIZE];
547  };
548 */
549 
550 /*
551  \brief SAP audio file output class
552  \ingroup audio
553 
554  ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!!
555 */
556 /*
557  class SAP_Out_File : virtual public Audio_File {
558  public:
559  // Constructor
560  SAP_Out_File();
561  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
562  SAP_Out_File(const char *fname, const char *hdr);
563  // Destructor
564  virtual ~SAP_Out_File() { close(); }
565 
566  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
567  bool open(const char *fname, const char *hdr);
568 
569  // Old def. Removed since Sun CC gave warning.
570  //virtual bool open(const char *fname, const char *hdr);
571 
572  // Close the file
573  virtual void close();
574 
575  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
576  bool seek_write(int pos);
577  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
578  int tell_write();
579 
580  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
581  virtual bool write(const vec &v);
582  };
583 */
584 
585 /*
586  \brief SAP audio file input and output class
587  \ingroup audio
588 
589  ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!!
590 */
591 /*
592  class SAP_IO_File : public SAP_In_File, public SAP_Out_File {
593  public:
594  // Constructor
595  SAP_IO_File() { }
596  // Open the file {\em fname}.
597  SAP_IO_File(const char *fname) { open(fname); }
598  // Destructor
599  virtual ~SAP_IO_File() { close(); }
600 
601  // Open the file {\em fname}.
602  virtual bool open(const char *fname);
603  // Close the file
604  virtual void close();
605  };
606 */
608 
609 
611 
612 
613 
615 inline vec snd_read_channel(const char *fname, int ch = 0)
616 {
617  SND_In_File f(fname);
618  int ns = (int)std::min(f.num_samples(), (std::streamoff)std::numeric_limits<int>::max());
619  return f.read_channel(ns,ch);
620 }
622 inline vec snd_read_channel(const char *fname, int ch, int len, std::streamoff beg = 0)
623 {
624  vec ret; SND_In_File f(fname);
625  if(f.seek_read(beg)) ret = f.read_channel(len,ch);
626  return ret;
627 }
628 
630 inline mat snd_read(const char *fname)
631 {
632  SND_In_File f(fname);
633  int ns = (int)std::min(f.num_samples(), (std::streamoff)std::numeric_limits<int>::max());
634  return f.read(ns);
635 }
637 inline mat snd_read(const char *fname, int len, std::streamoff beg = 0)
638 {
639  mat ret; SND_In_File f(fname);
640  if(f.seek_read(beg)) ret = f.read(len);
641  return ret;
642 }
643 
644 
646 inline bool snd_write_channel(const char *fname, const Audio_Stream_Description& descr, const vec& s, int ch = 0)
647 {
648  SND_Out_File f(fname,descr);
649  return f.write_channel(s,ch);
650 }
651 
653 inline bool snd_write(const char *fname, const Audio_Stream_Description& descr, const mat& s)
654 {
655  SND_Out_File f(fname,descr);
656  return f.write(s);
657 }
658 
660 
662 /*
663 // Read SAP audio data
664 bool sap_read(const char *fname, vec &v);
665 // Read SAP audio data
666 bool sap_read(const char *fname, vec &v, int beg, int len);
667 // Write SAP audio data
668 bool sap_write(const char *fname, const vec &v, const char *hdr);
669 */
671 
672 } // namespace itpp
673 
674 #endif // #ifndef AUDIOFILE_H
SourceForge Logo

Generated on Sat May 25 2013 16:32:23 for IT++ by Doxygen 1.8.2