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 { 00040 00042 #define SND_INFO_LEN 8 00043 00044 00055 class Audio_File 00056 { 00057 public: 00059 Audio_File(); 00061 virtual ~Audio_File() { } 00062 00064 bool good() { return is_valid && file.good(); } 00065 00066 protected: 00068 std::fstream file; 00070 bool is_valid; 00071 }; 00072 00079 class SND_Format 00080 { 00081 public: 00083 enum data_encoding { enc_unknown = 0, 00084 enc_mulaw8 = 1, 00085 enc_alaw8 = 27, 00086 enc_linear8 = 2, 00087 enc_linear16 = 3, 00088 enc_linear24 = 4, 00089 enc_linear32 = 5, 00090 enc_float = 6, 00091 enc_double = 7 00092 }; 00093 00095 int samples() const { return header.data_size / sample_size(); } 00097 data_encoding encoding() const { return (data_encoding)header.encoding; } 00099 int rate() const { return header.sample_rate; } 00101 void set_rate(int r) { header.sample_rate = r; } 00103 int channels() const { return header.channels; } 00104 00105 protected: 00106 00107 struct { 00109 unsigned magic; 00111 unsigned hdr_size; 00113 unsigned data_size; 00115 unsigned encoding; 00117 unsigned sample_rate; 00119 unsigned channels; 00121 char info[SND_INFO_LEN]; 00122 } header; 00123 00124 00126 int sample_size() const; 00128 bool read_header(std::istream &f); 00130 bool write_header(std::ostream &f); 00131 }; 00132 00139 class SND_In_File : virtual public Audio_File, virtual public SND_Format 00140 { 00141 public: 00143 SND_In_File(); 00145 SND_In_File(const char *fname); 00147 virtual ~SND_In_File() { close(); } 00148 00150 virtual bool open(const char *fname); 00152 virtual void close(); 00153 00155 bool seek_read(int pos); 00157 int tell_read(); 00158 00160 virtual bool read(vec &v); 00162 virtual bool read(vec &v, int n); 00163 }; 00164 00171 class SND_Out_File : virtual public Audio_File, virtual public SND_Format 00172 { 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 { 00207 public: 00209 SND_IO_File() { } 00211 SND_IO_File(const char *fname) { open(fname); } 00213 virtual ~SND_IO_File() { close(); } 00214 00216 virtual bool open(const char *fname); 00218 virtual void close(); 00219 }; 00220 00221 /* 00222 \brief SAP audio file input class 00223 \ingroup audio 00224 00225 ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!! 00226 */ 00227 /* 00228 class SAP_In_File : virtual public Audio_File { 00229 public: 00230 // Constructor 00231 SAP_In_File(); 00232 // Open the file {\em fname}. 00233 SAP_In_File(const char *fname); 00234 // Destructor 00235 virtual ~SAP_In_File() { close(); } 00236 00237 // Open the file {\em fname}. 00238 virtual bool open(const char *fname); 00239 // Close the file. 00240 virtual void close(); 00241 00242 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00243 virtual bool seek_read(int pos); 00244 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00245 virtual int tell_read(); 00246 00247 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00248 bool read(vec &v); 00249 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00250 bool read(vec &v, int n); 00251 00252 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00253 const char *get_header() { return header; } 00254 00255 protected: 00256 char header[SAP_HEADER_SIZE]; 00257 }; 00258 */ 00259 00260 /* 00261 \brief SAP audio file output class 00262 \ingroup audio 00263 00264 ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!! 00265 */ 00266 /* 00267 class SAP_Out_File : virtual public Audio_File { 00268 public: 00269 // Constructor 00270 SAP_Out_File(); 00271 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00272 SAP_Out_File(const char *fname, const char *hdr); 00273 // Destructor 00274 virtual ~SAP_Out_File() { close(); } 00275 00276 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00277 bool open(const char *fname, const char *hdr); 00278 00279 // Old def. Removed since Sun CC gave warning. 00280 //virtual bool open(const char *fname, const char *hdr); 00281 00282 // Close the file 00283 virtual void close(); 00284 00285 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00286 bool seek_write(int pos); 00287 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00288 int tell_write(); 00289 00290 // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!! 00291 virtual bool write(const vec &v); 00292 }; 00293 */ 00294 00295 /* 00296 \brief SAP audio file input and output class 00297 \ingroup audio 00298 00299 ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!! 00300 */ 00301 /* 00302 class SAP_IO_File : public SAP_In_File, public SAP_Out_File { 00303 public: 00304 // Constructor 00305 SAP_IO_File() { } 00306 // Open the file {\em fname}. 00307 SAP_IO_File(const char *fname) { open(fname); } 00308 // Destructor 00309 virtual ~SAP_IO_File() { close(); } 00310 00311 // Open the file {\em fname}. 00312 virtual bool open(const char *fname); 00313 // Close the file 00314 virtual void close(); 00315 }; 00316 */ 00317 00319 00320 00322 bool raw16le_read(const char *fname, vec &v); 00324 bool raw16le_read(const char *fname, vec &v, int beg, int len); 00326 bool raw16le_write(const char *fname, const vec &v, bool append = false); 00327 00329 bool raw16be_read(const char *fname, vec &v); 00331 bool raw16be_read(const char *fname, vec &v, int beg, int len); 00333 bool raw16be_write(const char *fname, const vec &v, bool append = false); 00334 00336 bool snd_read(const char *fname, vec &v); 00338 bool snd_read(const char *fname, vec &v, int beg, int len); 00340 bool snd_write(const char *fname, const vec &v, int rate = 8000, 00341 SND_Format::data_encoding e = SND_Format::enc_linear16); 00342 /* 00343 // Read SAP audio data 00344 bool sap_read(const char *fname, vec &v); 00345 // Read SAP audio data 00346 bool sap_read(const char *fname, vec &v, int beg, int len); 00347 // Write SAP audio data 00348 bool sap_write(const char *fname, const vec &v, const char *hdr); 00349 */ 00350 00352 template<typename T> 00353 inline T read_endian(std::istream &s, bool switch_endian = false) 00354 { 00355 T data; 00356 int bytes = sizeof(T); 00357 char *c = reinterpret_cast<char *>(&data); 00358 if (!switch_endian) { 00359 s.read(c, bytes); 00360 } 00361 else { 00362 for (int i = bytes - 1; i >= 0; i--) 00363 s.get(c[i]); 00364 } 00365 return data; 00366 } 00367 00369 template<typename T> 00370 inline void write_endian(std::ostream &s, T data, bool switch_endian = false) 00371 { 00372 int bytes = sizeof(T); 00373 char *c = reinterpret_cast<char *>(&data); 00374 if (!switch_endian) { 00375 s.write(c, bytes); 00376 } 00377 else { 00378 for (int i = bytes - 1; i >= 0; i--) 00379 s.put(c[i]); 00380 } 00381 } 00382 00384 00385 } // namespace itpp 00386 00387 #endif // #ifndef AUDIOFILE_H
Generated on Wed Feb 9 2011 13:47:17 for IT++ by Doxygen 1.7.3