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
00043
00044
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
00184
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
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
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
00343
00344
00345
00346
00347
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 }
00385
00386 #endif // #ifndef AUDIOFILE_H