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
41namespace itpp
42{
43
44
67{
68public:
70 Audio_Stream_Description():_encoding(enc_unknown), _sampling_rate(0), _num_channels(0){}
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;}
90private:
92 Audio_Encoding _encoding;
94 int _sampling_rate;
96 int _num_channels;
98 std::string _description;
99};
100
103{
104 if(!encoded_sample_size(d.get_encoding())) return false;
105 if(!d.get_num_channels()) return false;
106 return true;
107}
108
110namespace 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
176class ITPP_EXPORT SND_In_File
177{
178public:
182 SND_In_File(const char* fname);
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 }
236private:
238 bifstream _str;
240 std::streamoff _num_samples;
242 audiofile_details::Audio_Samples_Reader_If* _samples_reader;
244 Audio_Stream_Description* _description;
245};
246
282class ITPP_EXPORT SND_Out_File
283{
284public:
288 SND_Out_File(const char *fname, const Audio_Stream_Description& d);
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 }
357private:
359 bofstream _str;
361 std::streamoff _num_samples;
363 audiofile_details::Audio_Samples_Writer_If* _samples_writer;
365 Audio_Stream_Description* _description;
366};
367
374class ITPP_EXPORT SND_IO_File
375{
376public:
380 SND_IO_File(const char *fname);
382 SND_IO_File(const char *fname, const Audio_Stream_Description& d);
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 }
497private:
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
612
613
615inline 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}
622inline 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
630inline 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}
637inline 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
646inline 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
653inline 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
664bool sap_read(const char *fname, vec &v);
665// Read SAP audio data
666bool sap_read(const char *fname, vec &v, int beg, int len);
667// Write SAP audio data
668bool sap_write(const char *fname, const vec &v, const char *hdr);
669*/
671
672} // namespace itpp
673
674#endif // #ifndef AUDIOFILE_H
Encoding and decoding of audio samples.
Binary file formats definitions.
Description of audio stream.
Definition: audiofile.h:67
Audio_Stream_Description & set_num_channels(int nc)
Set number of audio channels.
Definition: audiofile.h:79
Audio_Encoding get_encoding() const
Get encoding of audio samples.
Definition: audiofile.h:83
Audio_Stream_Description()
Default ctor - creates uninitialized description.
Definition: audiofile.h:70
Audio_Stream_Description & set_sampling_rate(int sr)
Set sampling rate (samples per second)
Definition: audiofile.h:77
const std::string & get_description() const
Get stream annotation.
Definition: audiofile.h:89
Audio_Stream_Description & set_description(const std::string &d)
Set stream annotation.
Definition: audiofile.h:81
Audio_Stream_Description(Audio_Encoding e, int sr, int nc=1)
Construct with stream parameters: encoding e, sampling rate sr and number of audio channels nc.
Definition: audiofile.h:72
int get_num_channels() const
Get number of audio channels.
Definition: audiofile.h:87
Audio_Stream_Description & set_encoding(Audio_Encoding e)
Set encoding of audio samples.
Definition: audiofile.h:75
int get_sampling_rate() const
Get sampling rate (samples per second)
Definition: audiofile.h:85
A class for doing both input and output of audio samples.
Definition: audiofile.h:375
bool write_sample(const double &s, int ch=0)
Write single sample s at current position to channel ch.
Definition: audiofile.h:459
mat read(int n)
Read n samples from all channels starting at current position.
Definition: audiofile.h:450
bool seek_read(std::streamoff pos)
Set current position to read from pos (samples).
Definition: audiofile.h:394
bool open(const char *fname)
Open the file fname, check file header.
SND_IO_File(const char *fname)
Open the file fname, check file header.
Audio_Stream_Description get_description() const
Get stream description.
Definition: audiofile.h:392
bool open(const char *fname, const Audio_Stream_Description &d)
Open the file fname, truncate and overwrite header with description d.
void close()
Close the file.
std::streamoff tell_read()
Get current position to read from in samples.
Definition: audiofile.h:405
vec read_channel(int n, int ch=0)
Read n samples from channel ch starting at current position.
Definition: audiofile.h:442
SND_IO_File(const char *fname, const Audio_Stream_Description &d)
Open the file fname, truncate and overwrite header with description d.
std::streamoff num_samples() const
Get number of samples in stream.
Definition: audiofile.h:432
std::streamoff tell_write()
Get current position to write in samples.
Definition: audiofile.h:424
~SND_IO_File()
Stream destructor.
bool write(const mat &m)
Write audio channels from columns of the matrix m starting at current position.
Definition: audiofile.h:485
SND_IO_File()
Constructor - creates uninitialized stream.
bool seek_write(std::streamoff pos)
Set current position to write to pos (samples).
Definition: audiofile.h:413
bool write_channel(const vec &v, int ch=0)
Write the vector v to channel ch starting at current position.
Definition: audiofile.h:472
bool read_sample(double &s, int ch=0)
Read single sample s at current position to channel ch.
Definition: audiofile.h:434
Class to read audio data from au file.
Definition: audiofile.h:177
SND_In_File()
Default constructor - creates uninitialized stream.
std::streamoff tell_read()
Get current position in samples.
Definition: audiofile.h:203
Audio_Stream_Description get_description() const
Get stream description.
Definition: audiofile.h:190
vec read_channel(int n, int ch=0)
Read n samples from channel ch starting at current position.
Definition: audiofile.h:221
SND_In_File(const char *fname)
Constructor from file name fname.
bool read_sample(double &s, int ch=0)
Read single sample s at current position to channel ch.
Definition: audiofile.h:213
mat read(int n)
Read n samples from all channels starting at current position into matrix.
Definition: audiofile.h:229
void close()
Close the file.
bool open(const char *fname)
Open the file fname.
std::streamoff num_samples() const
Get number of samples in stream.
Definition: audiofile.h:211
bool seek_read(std::streamoff pos)
Go to sample number pos.
Definition: audiofile.h:192
~SND_In_File()
Stream destructor.
A class to write SND-files (the .au format)
Definition: audiofile.h:283
SND_Out_File()
Default constructor - creates uninitialized stream.
~SND_Out_File()
Stream destructor.
Audio_Stream_Description get_description() const
Get stream description.
Definition: audiofile.h:296
bool open(const char *fname, const Audio_Stream_Description &d)
Open the file fname with stream description d.
std::streamoff num_samples() const
Get number of samples in stream.
Definition: audiofile.h:317
SND_Out_File(const char *fname, const Audio_Stream_Description &d)
Constructor from file name fname and stream description d.
std::streamoff tell_write()
Get current position in samples.
Definition: audiofile.h:309
bool write_channel(const vec &v, int ch=0)
Write the vector v to channel ch starting at current position.
Definition: audiofile.h:332
void close()
Close the file.
bool write_sample(const double &s, int ch=0)
Write single sample s at current position to channel ch.
Definition: audiofile.h:319
bool seek_write(std::streamoff pos)
Set current position to write to pos (samples).
Definition: audiofile.h:298
bool write(const mat &m)
Write audio channels from columns of the matrix m starting at current position.
Definition: audiofile.h:345
Binary in/out-file Class.
Definition: binfile.h:604
Binary Infile Class.
Definition: binfile.h:539
Binary Outfile Class.
Definition: binfile.h:475
bool snd_write(const char *fname, const Audio_Stream_Description &descr, const mat &s)
Write audio data.
Definition: audiofile.h:653
vec snd_read_channel(const char *fname, int ch=0)
Read audio channel.
Definition: audiofile.h:615
bool snd_write_channel(const char *fname, const Audio_Stream_Description &descr, const vec &s, int ch=0)
Write audio channel from vector s using stream description descr.
Definition: audiofile.h:646
Audio_Encoding
Supported encoding types for audio samples.
Definition: audiosample.h:58
mat snd_read(const char *fname)
Read audio data.
Definition: audiofile.h:630
T min(const Vec< T > &in)
Minimum value of vector.
Definition: min_max.h:125
T max(const Vec< T > &v)
Maximum value of vector.
Definition: min_max.h:45
Matrix Class Definitions.
itpp namespace
Definition: itmex.h:37
bool is_valid(const Audio_Stream_Description &d)
validity check for stream description d
Definition: audiofile.h:102
std::size_t encoded_sample_size(Audio_Encoding e)
Size of encoded sample based on the encoding type e.
Definition: audiosample.h:302
Templated Vector Class Definitions.

Generated on Tue Aug 17 2021 10:59:15 for IT++ by Doxygen 1.9.4