IT++ Logo Newcom Logo

audiofile.h

Go to the documentation of this file.
00001 
00033 #ifndef AUDIOFILE_H
00034 #define AUDIOFILE_H
00035 
00036 #include <itpp/base/vec.h>
00037 #include <fstream>
00038 
00039 
00040 namespace itpp {
00041 
00042 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00043 
00044 #define SND_INFO_LEN       8
00045   //#define SAP_HEADER_SIZE  512
00046 
00047 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
00048 
00059   class Audio_File {
00060   public:
00062     Audio_File();
00064     virtual ~Audio_File() { }
00065 
00067     bool good() { return is_valid && file.good(); }
00068     
00069   protected:
00071     std::fstream file;
00073     bool is_valid;
00074   };
00075 
00082   class SND_Format {
00083   public:
00085     enum data_encoding { enc_unknown  =  0,
00086                          enc_mulaw8   =  1,
00087                          enc_alaw8    = 27,
00088                          enc_linear8  =  2,
00089                          enc_linear16 =  3,
00090                          enc_linear24 =  4,
00091                          enc_linear32 =  5,
00092                          enc_float    =  6,
00093                          enc_double   =  7 
00094     };
00095     
00097     int samples() const { return header.data_size / sample_size(); }
00099     data_encoding encoding() const { return (data_encoding)header.encoding; }
00101     int rate() const { return header.sample_rate; }
00103     void set_rate(int r) { header.sample_rate = r; }
00105     int channels() const { return header.channels; }
00106     
00107   protected:
00108     
00110     struct {
00112       unsigned magic;          
00114       unsigned hdr_size;       
00116       unsigned data_size;      
00118       unsigned encoding;     
00120       unsigned sample_rate;   
00122       unsigned channels;       
00124       char info[SND_INFO_LEN]; 
00125     } header;
00126     
00128     int sample_size() const;
00130     bool read_header(std::istream &f);
00132     bool write_header(std::ostream &f);
00133   };
00134 
00141   class SND_In_File : virtual public Audio_File, virtual public SND_Format {
00142   public:
00144     SND_In_File();
00146     SND_In_File(const char *fname);
00148     virtual ~SND_In_File() { close(); }
00149 
00151     virtual bool open(const char *fname);
00153     virtual void close();
00154 
00156     bool seek_read(int pos);
00158     int tell_read();
00159 
00161     virtual bool read(vec &v);
00163     virtual bool read(vec &v, int n);
00164   };
00165 
00172   class SND_Out_File : virtual public Audio_File, virtual public SND_Format {
00173   public:
00175     SND_Out_File();
00177     SND_Out_File(const char *fname, int rate=8000, data_encoding e=enc_linear16);
00179     virtual ~SND_Out_File() { close(); }
00180 
00182     bool open(const char *fname, int rate=8000, data_encoding e=enc_linear16);
00183 
00184     // Old definition. Removed since Sun CC gave a warning
00185     //virtual bool open(const char *fname, int rate=8000, data_encoding e=enc_linear16);
00186 
00188     virtual void close();
00189     
00191     bool seek_write(int pos);
00193     int tell_write();
00194     
00196     virtual bool write(const vec &v);
00197   };
00198 
00205   class SND_IO_File : public SND_In_File, public SND_Out_File {
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 
00352 } // namespace itpp
00353 
00354 #endif // #ifndef AUDIOFILE_H
SourceForge Logo

Generated on Thu Apr 19 14:19:55 2007 for IT++ by Doxygen 1.4.6