IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
audiofile.h
Go to the documentation of this file.
1 
29 #ifndef AUDIOFILE_H
30 #define AUDIOFILE_H
31 
32 #include <itpp/base/vec.h>
33 #include <itpp/base/math/misc.h>
34 #include <fstream>
35 
36 
37 namespace itpp
38 {
39 
41 #define SND_INFO_LEN 8
42 
43 
55 {
56 public:
58  Audio_File();
60  virtual ~Audio_File() { }
61 
63  bool good() { return is_valid && file.good(); }
64 
65 protected:
67  std::fstream file;
69  bool is_valid;
70 };
71 
79 {
80 public:
82  enum data_encoding { enc_unknown = 0,
83  enc_mulaw8 = 1,
84  enc_alaw8 = 27,
85  enc_linear8 = 2,
86  enc_linear16 = 3,
87  enc_linear24 = 4,
88  enc_linear32 = 5,
89  enc_float = 6,
90  enc_double = 7
91  };
92 
94  int samples() const { return header.data_size / sample_size(); }
96  data_encoding encoding() const { return (data_encoding)header.encoding; }
98  int rate() const { return header.sample_rate; }
100  void set_rate(int r) { header.sample_rate = r; }
102  int channels() const { return header.channels; }
103 
104 protected:
105 
106  struct {
108  unsigned magic;
110  unsigned hdr_size;
112  unsigned data_size;
114  unsigned encoding;
116  unsigned sample_rate;
118  unsigned channels;
120  char info[SND_INFO_LEN];
121  } header;
122 
123 
125  int sample_size() const;
127  bool read_header(std::istream &f);
129  bool write_header(std::ostream &f);
130 };
131 
138 class SND_In_File : virtual public Audio_File, virtual public SND_Format
139 {
140 public:
142  SND_In_File();
144  SND_In_File(const char *fname);
146  virtual ~SND_In_File() { close(); }
147 
149  virtual bool open(const char *fname);
151  virtual void close();
152 
154  bool seek_read(int pos);
156  int tell_read();
157 
159  virtual bool read(vec &v);
161  virtual bool read(vec &v, int n);
162 };
163 
170 class SND_Out_File : virtual public Audio_File, virtual public SND_Format
171 {
172 public:
174  SND_Out_File();
176  SND_Out_File(const char *fname, int rate = 8000, data_encoding e = enc_linear16);
178  virtual ~SND_Out_File() { close(); }
179 
181  bool open(const char *fname, int rate = 8000, data_encoding e = enc_linear16);
182 
183  // Old definition. Removed since Sun CC gave a warning
184  //virtual bool open(const char *fname, int rate=8000, data_encoding e=enc_linear16);
185 
187  virtual void close();
188 
190  bool seek_write(int pos);
192  int tell_write();
193 
195  virtual bool write(const vec &v);
196 };
197 
204 class SND_IO_File : public SND_In_File, public SND_Out_File
205 {
206 public:
210  SND_IO_File(const char *fname) { open(fname); }
212  virtual ~SND_IO_File() { close(); }
213 
215  virtual bool open(const char *fname);
217  virtual void close();
218 };
219 
220 /*
221  \brief SAP audio file input class
222  \ingroup audio
223 
224  ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!!
225 */
226 /*
227  class SAP_In_File : virtual public Audio_File {
228  public:
229  // Constructor
230  SAP_In_File();
231  // Open the file {\em fname}.
232  SAP_In_File(const char *fname);
233  // Destructor
234  virtual ~SAP_In_File() { close(); }
235 
236  // Open the file {\em fname}.
237  virtual bool open(const char *fname);
238  // Close the file.
239  virtual void close();
240 
241  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
242  virtual bool seek_read(int pos);
243  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
244  virtual int tell_read();
245 
246  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
247  bool read(vec &v);
248  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
249  bool read(vec &v, int n);
250 
251  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
252  const char *get_header() { return header; }
253 
254  protected:
255  char header[SAP_HEADER_SIZE];
256  };
257 */
258 
259 /*
260  \brief SAP audio file output class
261  \ingroup audio
262 
263  ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!!
264 */
265 /*
266  class SAP_Out_File : virtual public Audio_File {
267  public:
268  // Constructor
269  SAP_Out_File();
270  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
271  SAP_Out_File(const char *fname, const char *hdr);
272  // Destructor
273  virtual ~SAP_Out_File() { close(); }
274 
275  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
276  bool open(const char *fname, const char *hdr);
277 
278  // Old def. Removed since Sun CC gave warning.
279  //virtual bool open(const char *fname, const char *hdr);
280 
281  // Close the file
282  virtual void close();
283 
284  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
285  bool seek_write(int pos);
286  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
287  int tell_write();
288 
289  // ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
290  virtual bool write(const vec &v);
291  };
292 */
293 
294 /*
295  \brief SAP audio file input and output class
296  \ingroup audio
297 
298  ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!!
299 */
300 /*
301  class SAP_IO_File : public SAP_In_File, public SAP_Out_File {
302  public:
303  // Constructor
304  SAP_IO_File() { }
305  // Open the file {\em fname}.
306  SAP_IO_File(const char *fname) { open(fname); }
307  // Destructor
308  virtual ~SAP_IO_File() { close(); }
309 
310  // Open the file {\em fname}.
311  virtual bool open(const char *fname);
312  // Close the file
313  virtual void close();
314  };
315 */
316 
318 
319 
321 bool raw16le_read(const char *fname, vec &v);
323 bool raw16le_read(const char *fname, vec &v, int beg, int len);
325 bool raw16le_write(const char *fname, const vec &v, bool append = false);
326 
328 bool raw16be_read(const char *fname, vec &v);
330 bool raw16be_read(const char *fname, vec &v, int beg, int len);
332 bool raw16be_write(const char *fname, const vec &v, bool append = false);
333 
335 bool snd_read(const char *fname, vec &v);
337 bool snd_read(const char *fname, vec &v, int beg, int len);
339 bool snd_write(const char *fname, const vec &v, int rate = 8000,
340  SND_Format::data_encoding e = SND_Format::enc_linear16);
341 /*
342 // Read SAP audio data
343 bool sap_read(const char *fname, vec &v);
344 // Read SAP audio data
345 bool sap_read(const char *fname, vec &v, int beg, int len);
346 // Write SAP audio data
347 bool sap_write(const char *fname, const vec &v, const char *hdr);
348 */
349 
351 template<typename T>
352 inline T read_endian(std::istream &s, bool switch_endian = false)
353 {
354  T data;
355  int bytes = sizeof(T);
356  char *c = reinterpret_cast<char *>(&data);
357  if (!switch_endian) {
358  s.read(c, bytes);
359  }
360  else {
361  for (int i = bytes - 1; i >= 0; i--)
362  s.get(c[i]);
363  }
364  return data;
365 }
366 
368 template<typename T>
369 inline void write_endian(std::ostream &s, T data, bool switch_endian = false)
370 {
371  int bytes = sizeof(T);
372  char *c = reinterpret_cast<char *>(&data);
373  if (!switch_endian) {
374  s.write(c, bytes);
375  }
376  else {
377  for (int i = bytes - 1; i >= 0; i--)
378  s.put(c[i]);
379  }
380 }
381 
383 
384 } // namespace itpp
385 
386 #endif // #ifndef AUDIOFILE_H
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:14 for IT++ by Doxygen 1.8.1.2