00001 00030 #ifndef AUDIOFILE_H 00031 #define AUDIOFILE_H 00032 00033 #include <itpp/base/vec.h> 00034 #include <itpp/base/math/misc.h> 00035 #include <fstream> 00036 00037 00038 namespace itpp { 00039 00041 #define SND_INFO_LEN 8 00043 00044 00054 class Audio_File { 00055 public: 00057 Audio_File(); 00059 virtual ~Audio_File() { } 00060 00062 bool good() { return is_valid && file.good(); } 00063 00064 protected: 00066 std::fstream file; 00068 bool is_valid; 00069 }; 00070 00077 class SND_Format { 00078 public: 00080 enum data_encoding { enc_unknown = 0, 00081 enc_mulaw8 = 1, 00082 enc_alaw8 = 27, 00083 enc_linear8 = 2, 00084 enc_linear16 = 3, 00085 enc_linear24 = 4, 00086 enc_linear32 = 5, 00087 enc_float = 6, 00088 enc_double = 7 00089 }; 00090 00092 int samples() const { return header.data_size / sample_size(); } 00094 data_encoding encoding() const { return (data_encoding)header.encoding; } 00096 int rate() const { return header.sample_rate; } 00098 void set_rate(int r) { header.sample_rate = r; } 00100 int channels() const { return header.channels; } 00101 00102 protected: 00103 00104 struct { 00106 unsigned magic; 00108 unsigned hdr_size; 00110 unsigned data_size; 00112 unsigned encoding; 00114 unsigned sample_rate; 00116 unsigned channels; 00118 char info[SND_INFO_LEN]; 00119 } header; 00120 00121 00123 int sample_size() const; 00125 bool read_header(std::istream &f); 00127 bool write_header(std::ostream &f); 00128 }; 00129 00136 class SND_In_File : virtual public Audio_File, virtual public SND_Format { 00137 public: 00139 SND_In_File(); 00141 SND_In_File(const char *fname); 00143 virtual ~SND_In_File() { close(); } 00144 00146 virtual bool open(const char *fname); 00148 virtual void close(); 00149 00151 bool seek_read(int pos); 00153 int tell_read(); 00154 00156 virtual bool read(vec &v); 00158 virtual bool read(vec &v, int n); 00159 }; 00160 00167 class SND_Out_File : virtual public Audio_File, virtual public SND_Format { 00168 public: 00170 SND_Out_File(); 00172 SND_Out_File(const char *fname, int rate=8000, data_encoding e=enc_linear16); 00174 virtual ~SND_Out_File() { close(); } 00175 00177 bool open(const char *fname, int rate=8000, data_encoding e=enc_linear16); 00178 00179 // Old definition. Removed since Sun CC gave a warning 00180 //virtual bool open(const char *fname, int rate=8000, data_encoding e=enc_linear16); 00181 00183 virtual void close(); 00184 00186 bool seek_write(int pos); 00188 int tell_write(); 00189 00191 virtual bool write(const vec &v); 00192 }; 00193 00200 class SND_IO_File : public SND_In_File, public SND_Out_File { 00201 public: 00203 SND_IO_File() { } 00205 SND_IO_File(const char *fname) { open(fname); } 00207 virtual ~SND_IO_File() { close(); } 00208 00210 virtual bool open(const char *fname); 00212 virtual void close(); 00213 }; 00214 00215 /* 00216 \brief SAP audio file input class 00217 \ingroup audio 00218 00219 ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!! 00220 */ 00221 /* 00222 class SAP_In_File : virtual public Audio_File { 00223 public: 00224 // Constructor 00225 SAP_In_File(); 00226 // Open the file {\em fname}. 00227 SAP_In_File(const char *fname); 00228 // Destructor 00229 virtual ~SAP_In_File() { close(); } 00230 00231 // Open the file {\em fname}. 00232 virtual bool open(const char *fname); 00233 // Close the file. 00234 virtual void close(); 00235 00236 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00237 virtual bool seek_read(int pos); 00238 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00239 virtual int tell_read(); 00240 00241 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00242 bool read(vec &v); 00243 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00244 bool read(vec &v, int n); 00245 00246 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00247 const char *get_header() { return header; } 00248 00249 protected: 00250 char header[SAP_HEADER_SIZE]; 00251 }; 00252 */ 00253 00254 /* 00255 \brief SAP audio file output class 00256 \ingroup audio 00257 00258 ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!! 00259 */ 00260 /* 00261 class SAP_Out_File : virtual public Audio_File { 00262 public: 00263 // Constructor 00264 SAP_Out_File(); 00265 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00266 SAP_Out_File(const char *fname, const char *hdr); 00267 // Destructor 00268 virtual ~SAP_Out_File() { close(); } 00269 00270 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00271 bool open(const char *fname, const char *hdr); 00272 00273 // Old def. Removed since Sun CC gave warning. 00274 //virtual bool open(const char *fname, const char *hdr); 00275 00276 // Close the file 00277 virtual void close(); 00278 00279 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00280 bool seek_write(int pos); 00281 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00282 int tell_write(); 00283 00284 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00285 virtual bool write(const vec &v); 00286 }; 00287 */ 00288 00289 /* 00290 \brief SAP audio file input and output class 00291 \ingroup audio 00292 00293 ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!! 00294 */ 00295 /* 00296 class SAP_IO_File : public SAP_In_File, public SAP_Out_File { 00297 public: 00298 // Constructor 00299 SAP_IO_File() { } 00300 // Open the file {\em fname}. 00301 SAP_IO_File(const char *fname) { open(fname); } 00302 // Destructor 00303 virtual ~SAP_IO_File() { close(); } 00304 00305 // Open the file {\em fname}. 00306 virtual bool open(const char *fname); 00307 // Close the file 00308 virtual void close(); 00309 }; 00310 */ 00311 00313 00314 00316 bool raw16le_read(const char *fname, vec &v); 00318 bool raw16le_read(const char *fname, vec &v, int beg, int len); 00320 bool raw16le_write(const char *fname, const vec &v, bool append=false); 00321 00323 bool raw16be_read(const char *fname, vec &v); 00325 bool raw16be_read(const char *fname, vec &v, int beg, int len); 00327 bool raw16be_write(const char *fname, const vec &v, bool append=false); 00328 00330 bool snd_read(const char *fname, vec &v); 00332 bool snd_read(const char *fname, vec &v, int beg, int len); 00334 bool snd_write(const char *fname, const vec &v, int rate=8000, 00335 SND_Format::data_encoding e=SND_Format::enc_linear16); 00336 /* 00337 // Read SAP audio data 00338 bool sap_read(const char *fname, vec &v); 00339 // Read SAP audio data 00340 bool sap_read(const char *fname, vec &v, int beg, int len); 00341 // Write SAP audio data 00342 bool sap_write(const char *fname, const vec &v, const char *hdr); 00343 */ 00344 00346 template<typename T> 00347 inline T read_endian(std::istream &s, bool switch_endian = false) 00348 { 00349 T data; 00350 int bytes = sizeof(T); 00351 char *c = reinterpret_cast<char *>(&data); 00352 if (!switch_endian) { 00353 s.read(c, bytes); 00354 } 00355 else { 00356 for (int i = bytes-1; i >= 0; i--) 00357 s.get(c[i]); 00358 } 00359 return data; 00360 } 00361 00363 template<typename T> 00364 inline void write_endian(std::ostream &s, T data, bool switch_endian = false) 00365 { 00366 int bytes = sizeof(T); 00367 char *c = reinterpret_cast<char *>(&data); 00368 if (!switch_endian) { 00369 s.write(c, bytes); 00370 } 00371 else { 00372 for (int i = bytes-1; i >= 0; i--) 00373 s.put(c[i]); 00374 } 00375 } 00376 00378 00379 } // namespace itpp 00380 00381 #endif // #ifndef AUDIOFILE_H
Generated on Sat Apr 19 10:59:23 2008 for IT++ by Doxygen 1.5.5