IT++ Logo
audiofile.h
Go to the documentation of this file.
00001 
00029 #ifndef AUDIOFILE_H
00030 #define AUDIOFILE_H
00031 
00032 #include <itpp/base/vec.h>
00033 #include <itpp/base/math/misc.h>
00034 #include <fstream>
00035 
00036 
00037 namespace itpp
00038 {
00039 
00041 #define SND_INFO_LEN 8
00042 
00043 
00054 class Audio_File
00055 {
00056 public:
00058   Audio_File();
00060   virtual ~Audio_File() { }
00061 
00063   bool good() { return is_valid && file.good(); }
00064 
00065 protected:
00067   std::fstream file;
00069   bool is_valid;
00070 };
00071 
00078 class SND_Format
00079 {
00080 public:
00082   enum data_encoding { enc_unknown  =  0,
00083                        enc_mulaw8   =  1,
00084                        enc_alaw8    = 27,
00085                        enc_linear8  =  2,
00086                        enc_linear16 =  3,
00087                        enc_linear24 =  4,
00088                        enc_linear32 =  5,
00089                        enc_float    =  6,
00090                        enc_double   =  7
00091                      };
00092 
00094   int samples() const { return header.data_size / sample_size(); }
00096   data_encoding encoding() const { return (data_encoding)header.encoding; }
00098   int rate() const { return header.sample_rate; }
00100   void set_rate(int r) { header.sample_rate = r; }
00102   int channels() const { return header.channels; }
00103 
00104 protected:
00105 
00106   struct {
00108     unsigned magic;
00110     unsigned hdr_size;
00112     unsigned data_size;
00114     unsigned encoding;
00116     unsigned sample_rate;
00118     unsigned channels;
00120     char info[SND_INFO_LEN];
00121   } header; 
00122 
00123 
00125   int sample_size() const;
00127   bool read_header(std::istream &f);
00129   bool write_header(std::ostream &f);
00130 };
00131 
00138 class SND_In_File : virtual public Audio_File, virtual public SND_Format
00139 {
00140 public:
00142   SND_In_File();
00144   SND_In_File(const char *fname);
00146   virtual ~SND_In_File() { close(); }
00147 
00149   virtual bool open(const char *fname);
00151   virtual void close();
00152 
00154   bool seek_read(int pos);
00156   int tell_read();
00157 
00159   virtual bool read(vec &v);
00161   virtual bool read(vec &v, int n);
00162 };
00163 
00170 class SND_Out_File : virtual public Audio_File, virtual public SND_Format
00171 {
00172 public:
00174   SND_Out_File();
00176   SND_Out_File(const char *fname, int rate = 8000, data_encoding e = enc_linear16);
00178   virtual ~SND_Out_File() { close(); }
00179 
00181   bool open(const char *fname, int rate = 8000, data_encoding e = enc_linear16);
00182 
00183   // Old definition. Removed since Sun CC gave a warning
00184   //virtual bool open(const char *fname, int rate=8000, data_encoding e=enc_linear16);
00185 
00187   virtual void close();
00188 
00190   bool seek_write(int pos);
00192   int tell_write();
00193 
00195   virtual bool write(const vec &v);
00196 };
00197 
00204 class SND_IO_File : public SND_In_File, public SND_Out_File
00205 {
00206 public:
00208   SND_IO_File() { }
00210   SND_IO_File(const char *fname) { open(fname); }
00212   virtual ~SND_IO_File() { close(); }
00213 
00215   virtual bool open(const char *fname);
00217   virtual void close();
00218 };
00219 
00220 /*
00221    \brief SAP audio file input class
00222    \ingroup audio
00223 
00224    ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!!
00225 */
00226 /*
00227   class SAP_In_File : virtual public Audio_File {
00228   public:
00229   // Constructor
00230   SAP_In_File();
00231   // Open the file {\em fname}.
00232   SAP_In_File(const char *fname);
00233   // Destructor
00234   virtual ~SAP_In_File() { close(); }
00235 
00236   // Open the file {\em fname}.
00237   virtual bool open(const char *fname);
00238   // Close the file.
00239   virtual void close();
00240 
00241   // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
00242   virtual bool seek_read(int pos);
00243   // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
00244   virtual int tell_read();
00245 
00246   // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
00247   bool read(vec &v);
00248   // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
00249   bool read(vec &v, int n);
00250 
00251   // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
00252   const char *get_header() { return header; }
00253 
00254   protected:
00255   char header[SAP_HEADER_SIZE];
00256   };
00257 */
00258 
00259 /*
00260   \brief SAP audio file output class
00261   \ingroup audio
00262 
00263   ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!!
00264 */
00265 /*
00266   class SAP_Out_File : virtual public Audio_File {
00267   public:
00268   // Constructor
00269   SAP_Out_File();
00270   // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
00271   SAP_Out_File(const char *fname, const char *hdr);
00272   // Destructor
00273   virtual ~SAP_Out_File() { close(); }
00274 
00275   // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
00276   bool open(const char *fname, const char *hdr);
00277 
00278   // Old def. Removed since Sun CC gave warning.
00279   //virtual bool open(const char *fname, const char *hdr);
00280 
00281   // Close the file
00282   virtual void close();
00283 
00284   // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
00285   bool seek_write(int pos);
00286   // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
00287   int tell_write();
00288 
00289   // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
00290   virtual bool write(const vec &v);
00291   };
00292 */
00293 
00294 /*
00295   \brief SAP audio file input and output class
00296   \ingroup audio
00297 
00298   ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!!
00299 */
00300 /*
00301   class SAP_IO_File : public SAP_In_File, public SAP_Out_File {
00302   public:
00303   // Constructor
00304   SAP_IO_File() { }
00305   // Open the file {\em fname}.
00306   SAP_IO_File(const char *fname) { open(fname); }
00307   // Destructor
00308   virtual ~SAP_IO_File() { close(); }
00309 
00310   // Open the file {\em fname}.
00311   virtual bool open(const char *fname);
00312   // Close the file
00313   virtual void close();
00314   };
00315 */
00316 
00318 
00319 
00321 bool raw16le_read(const char *fname, vec &v);
00323 bool raw16le_read(const char *fname, vec &v, int beg, int len);
00325 bool raw16le_write(const char *fname, const vec &v, bool append = false);
00326 
00328 bool raw16be_read(const char *fname, vec &v);
00330 bool raw16be_read(const char *fname, vec &v, int beg, int len);
00332 bool raw16be_write(const char *fname, const vec &v, bool append = false);
00333 
00335 bool snd_read(const char *fname, vec &v);
00337 bool snd_read(const char *fname, vec &v, int beg, int len);
00339 bool snd_write(const char *fname, const vec &v, int rate = 8000,
00340                SND_Format::data_encoding e = SND_Format::enc_linear16);
00341 /*
00342 // Read SAP audio data
00343 bool sap_read(const char *fname, vec &v);
00344 // Read SAP audio data
00345 bool sap_read(const char *fname, vec &v, int beg, int len);
00346 // Write SAP audio data
00347 bool sap_write(const char *fname, const vec &v, const char *hdr);
00348 */
00349 
00351 template<typename T>
00352 inline T read_endian(std::istream &s, bool switch_endian = false)
00353 {
00354   T data;
00355   int bytes = sizeof(T);
00356   char *c = reinterpret_cast<char *>(&data);
00357   if (!switch_endian) {
00358     s.read(c, bytes);
00359   }
00360   else {
00361     for (int i = bytes - 1; i >= 0; i--)
00362       s.get(c[i]);
00363   }
00364   return data;
00365 }
00366 
00368 template<typename T>
00369 inline void write_endian(std::ostream &s, T data, bool switch_endian = false)
00370 {
00371   int bytes = sizeof(T);
00372   char *c = reinterpret_cast<char *>(&data);
00373   if (!switch_endian) {
00374     s.write(c, bytes);
00375   }
00376   else {
00377     for (int i = bytes - 1; i >= 0; i--)
00378       s.put(c[i]);
00379   }
00380 }
00381 
00383 
00384 } // namespace itpp
00385 
00386 #endif // #ifndef AUDIOFILE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SourceForge Logo

Generated on Wed Jul 27 2011 16:27:05 for IT++ by Doxygen 1.7.4