File: audiofile.cpp

package info (click to toggle)
libitpp 4.3.1-13
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 9,952 kB
  • sloc: cpp: 73,628; makefile: 661; python: 548; sh: 261
file content (758 lines) | stat: -rw-r--r-- 24,161 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*!
 * \file
 * \brief Implementation of audio Audio classes and functions
 * \author Tobias Ringstrom, Adam Piatyszek and Andy Panov
 *
 * -------------------------------------------------------------------------
 *
 * Copyright (C) 1995-2013  (see AUTHORS file for a list of contributors)
 *
 * This file is part of IT++ - a C++ library of mathematical, signal
 * processing, speech processing, and communications classes and functions.
 *
 * IT++ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * IT++ is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with IT++.  If not, see <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/srccode/audiofile.h>
#include <itpp/base/converters.h>
#include <itpp/base/ittypes.h>
#include <itpp/base/itassert.h>
#include <iostream>

//! \cond
namespace itpp
{

//magic id of snd file header
static const uint32_t snd_magic = 0x2e736e64;
//maximum length of annotation to extract from snd file
static const std::size_t max_annotation_length = 1024;

//////////////////////////////////////////////////
//
// Audio_Samples_Reader - templated implementation of Audio_Samples_Reader_If
//
//////////////////////////////////////////////////
template<typename Binary_In_Stream, Audio_Encoding Encoding>
class Audio_Samples_Reader : public audiofile_details::Audio_Samples_Reader_If
{
public:
  Audio_Samples_Reader(Binary_In_Stream& str, std::streamoff start, int nc):
    _str(str), _start_pos(start), _num_channels(nc), _cur_pos(0){}
  bool read_sample(double& s, int ch);
  //Read n samples from audio channel ch
  vec read_channel(int n, int ch);
  mat read(int n);
  virtual std::streamoff tell() const;
  virtual bool seek(std::streamoff n);
  virtual std::streamoff num_samples();
private:
  static const std::size_t sample_size = Audio_Sample<Encoding>::enc_sample_size;
  typedef typename Audio_Sample<Encoding>::enc_sample_type sample_type;
  //Number of audio channels
  int _num_channels;
  //! First sample offset from the start of the file
  std::streamoff _start_pos;
  //! Current position in samples
  std::streamoff _cur_pos;
  //! Binary stream
  Binary_In_Stream& _str;
};

template<typename Binary_In_Stream, Audio_Encoding Encoding>
std::streamoff Audio_Samples_Reader<Binary_In_Stream,Encoding>::tell() const
{
  return _cur_pos;
}

template<typename Binary_In_Stream, Audio_Encoding Encoding>
bool Audio_Samples_Reader<Binary_In_Stream,Encoding>::seek(std::streamoff n)
{
  _str.seekg(_start_pos + (_cur_pos * _num_channels *sample_size), std::ios_base::beg);
  if(_str){
    _cur_pos = n;
    return true;
  }
  else{
    return false;
  }
}

template<typename Binary_In_Stream, Audio_Encoding Encoding>
std::streamoff Audio_Samples_Reader<Binary_In_Stream,Encoding>::num_samples()
{
  _str.seekg(0, std::ios_base::end);
  if(!_str) return -1;
  std::streamoff end_pos = _str.tellg();
  return (end_pos - _start_pos)/(_num_channels * sample_size);
}

//read single channel samples starting at current position
template<typename Binary_In_Stream, Audio_Encoding Encoding>
bool Audio_Samples_Reader<Binary_In_Stream,Encoding>::read_sample(double& s, int ch)
{
  if(ch >= _num_channels) return false;
  std::streamoff read_pos = _start_pos + (_cur_pos * _num_channels + ch )*sample_size;
  _str.seekg(read_pos, std::ios_base::beg);
  if(!_str) return false;
  sample_type raw_sample;
  _str >> raw_sample;
  if(_str){
    s = Audio_Sample<Encoding>::decode(raw_sample);
    _cur_pos++;
    return true;
  }
  return false;
}

//read n samples from channel ch starting at current position
template<typename Binary_In_Stream, Audio_Encoding Encoding>
vec Audio_Samples_Reader<Binary_In_Stream,Encoding>::read_channel(int n, int ch)
{
  //ignore threshold - ignore() is used instead of seekg()
  //if stride between samples is smaller then this threshold
  static const std::streamsize ignore_threshold = 64;

  if((n <= 0) || (ch >= _num_channels)) return vec();
  vec ret(n);

  //read first n-1 samples
  const std::streamsize stride = sample_size*(_num_channels - 1);
  _str.seekg(_start_pos + (_cur_pos *_num_channels + ch) * sample_size, std::ios_base::beg);
  for(int i = 0; (i < (n-1)) && _str; ++i) {
    sample_type raw_sample; _str >> raw_sample;
    ret(i) = Audio_Sample<Encoding>::decode(raw_sample);
    if(stride > ignore_threshold)
      _str.seekg(stride, std::ios_base::cur);
    else
      _str.ignore(stride);
  }

  //read last sample
  if(_str){
    sample_type raw_sample; _str >> raw_sample;
    ret(n-1) = Audio_Sample<Encoding>::decode(raw_sample);
  }

  if(_str){
    _cur_pos += n;
  }
  else{
    ret.set_size(0);
  }

  return ret;
}

//read n samples from all channels starting at current position
template<typename Binary_In_Stream, Audio_Encoding Encoding>
mat Audio_Samples_Reader<Binary_In_Stream,Encoding>::read(int n)
{
  if(n <= 0) return mat();
  mat ret(n,_num_channels);

  //read samples
  const std::streamsize stride = sample_size*(_num_channels - 1);
  _str.seekg(_start_pos + _cur_pos * sample_size *_num_channels, std::ios_base::beg);
  for(int i = 0; (i < n) && _str; ++i) {
    for(int j = 0; j < _num_channels && _str; ++j) {
      sample_type raw_sample; _str >> raw_sample;
      ret(i,j) = Audio_Sample<Encoding>::decode(raw_sample);
    }
  }

  if(_str){
    _cur_pos += n;
  }
  else{
    ret.set_size(0,0);
  }
  return ret;
}

//make audio samples reader to input data from stream
template<typename Binary_In_Stream>
audiofile_details::Audio_Samples_Reader_If* make_reader(Binary_In_Stream& str,
    std::streamoff start_pos, Audio_Stream_Description* d)
{
  Audio_Encoding encoding = d->get_encoding();
  int num_channels = d->get_num_channels();
  switch(encoding){
    case enc_mulaw8:
      return new Audio_Samples_Reader<Binary_In_Stream, enc_mulaw8>(str,start_pos,num_channels);
    case enc_alaw8:
      return new Audio_Samples_Reader<Binary_In_Stream, enc_alaw8>(str,start_pos,num_channels);
    case enc_linear8:
      return new Audio_Samples_Reader<Binary_In_Stream, enc_linear8>(str,start_pos,num_channels);
    case enc_linear16:
      return new Audio_Samples_Reader<Binary_In_Stream, enc_linear16>(str,start_pos,num_channels);
    case enc_linear24:
      return new Audio_Samples_Reader<Binary_In_Stream, enc_linear24>(str,start_pos,num_channels);
    case enc_linear32:
      return new Audio_Samples_Reader<Binary_In_Stream, enc_linear32>(str,start_pos,num_channels);
    case enc_float:
      return new Audio_Samples_Reader<Binary_In_Stream, enc_float>(str,start_pos,num_channels);
    case enc_double:
      return new Audio_Samples_Reader<Binary_In_Stream, enc_double>(str,start_pos,num_channels);
    case enc_unknown:
    default:
      return 0;
  }
}
//////////////////////////////////////////////////
//
// Audio_Samples_Writer - templated implementation of Audio_Samples_Writer_If
//
//////////////////////////////////////////////////
template<typename Binary_Out_Stream, Audio_Encoding Encoding>
class Audio_Samples_Writer : public audiofile_details::Audio_Samples_Writer_If
{
public:
  Audio_Samples_Writer(Binary_Out_Stream& str, std::streamoff start, int nc):
    _str(str), _start_pos(start), _num_channels(nc), _cur_pos(0),
    _zero(Audio_Sample<Encoding>::encode(0.0)){}
  virtual bool write_sample(const double& s, int ch);
  virtual bool write_channel(const vec& s, int ch);
  //Write n samples to audio channel ch
  virtual bool write(const mat& s);
  virtual std::streamoff tell() const;
  virtual bool seek(std::streamoff n);
  virtual std::streamoff num_samples();
private:
  static const std::size_t sample_size = Audio_Sample<Encoding>::enc_sample_size;
  typedef typename Audio_Sample<Encoding>::enc_sample_type sample_type;
  //Number of audio channels
  int _num_channels;
  //! First sample offset from the start of the file
  std::streamoff _start_pos;
  //! Current position in samples
  std::streamoff _cur_pos;
  //! Binary stream
  Binary_Out_Stream& _str;
  //Zero sample
  sample_type _zero;
};

template<typename Binary_Out_Stream, Audio_Encoding Encoding>
std::streamoff Audio_Samples_Writer<Binary_Out_Stream,Encoding>::tell() const
{
  return _cur_pos;
}

template<typename Binary_Out_Stream, Audio_Encoding Encoding>
bool Audio_Samples_Writer<Binary_Out_Stream,Encoding>::seek(std::streamoff n)
{
  _str.seekp(_start_pos + (n * _num_channels *sample_size), std::ios_base::beg);
  if(_str){
    _cur_pos = n;
    return true;
  }
  else{
    return false;
  }
}

template<typename Binary_Out_Stream, Audio_Encoding Encoding>
std::streamoff Audio_Samples_Writer<Binary_Out_Stream,Encoding>::num_samples()
{
  _str.seekp(0, std::ios_base::end);
  if(!_str) return -1;
  std::streamoff end_pos = _str.tellp();
  return (end_pos - _start_pos)/(_num_channels * sample_size);
}

//write single sample starting at current position
template<typename Binary_Out_Stream, Audio_Encoding Encoding>
bool Audio_Samples_Writer<Binary_Out_Stream,Encoding>::write_sample(const double& s, int ch)
{
  if(ch >= _num_channels) return false;
  std::streamoff write_pos = _start_pos + (_cur_pos * _num_channels + ch )*sample_size;
  _str.seekp(write_pos, std::ios_base::beg);
  if(!_str) return false;
  _str << Audio_Sample<Encoding>::encode(s);
  if(_str){
    _cur_pos++;
    return true;
  }
  return false;
}

//write single channel samples starting at current position
template<typename Binary_Out_Stream, Audio_Encoding Encoding>
bool Audio_Samples_Writer<Binary_Out_Stream,Encoding>::write_channel(const vec& s, int ch)
{
  if(ch >= _num_channels) return false;

  int len = s.length();
  //overall number of already written samples
  std::streamoff ns = num_samples();
  if(ns < 0) return false;
  //compute number of samples to overwrite and number of samples to add to the end of file
  int to_overwrite = (int)std::min(ns - _cur_pos, (std::streamoff)len);

  const std::streamoff stride = sample_size*(_num_channels - 1);

  int i = 0;
  //overwrite samples
  if(to_overwrite)
  {
    //process first to_overwrite-1 samples
    _str.seekp(_start_pos + (_cur_pos * _num_channels + ch )*sample_size, std::ios_base::beg);
    for(i = 0; (i < (to_overwrite-1)) && _str; ++i) {
      _str << Audio_Sample<Encoding>::encode(s(i));
      if(stride) _str.seekp(stride,std::ios_base::cur);
    }
    if(_str){
      _str << Audio_Sample<Encoding>::encode(s(i)); ++i;
    }
  }

  //add samples to the end of file
  if(i < len)
  {
    _str.seekp(_start_pos + ns * _num_channels * sample_size, std::ios_base::beg);
    for(; (i < len) && _str; ++i) {
      for(int j = 0; (j < _num_channels) && _str; ++j){
        if(j == ch)
          _str << Audio_Sample<Encoding>::encode(s(i));
        else
          _str << _zero;
      }
    }
  }

  if(_str){
    _cur_pos += len;
    return true;
  }
  return false;
}

//write samples starting at current position
template<typename Binary_Out_Stream, Audio_Encoding Encoding>
bool Audio_Samples_Writer<Binary_Out_Stream,Encoding>::write(const mat& s)
{
  if(s.cols() < _num_channels) return false;
  int len = s.rows();
  for(int i = 0; (i < len) && _str; ++i){
    for(int j = 0; (j < _num_channels) && _str; ++j){
      sample_type raw_sample = Audio_Sample<Encoding>::encode(s(i,j));
      _str << raw_sample;
    }
  }
  if(_str){
    _cur_pos += len;
    return true;
  }
  return false;
}

//make audio samples writer for stream output
template<typename Binary_Out_Stream>
audiofile_details::Audio_Samples_Writer_If* make_writer(Binary_Out_Stream& str,
    std::streamoff start_pos, Audio_Stream_Description* d)
{
  Audio_Encoding encoding = d->get_encoding();
  int num_channels = d->get_num_channels();
  switch(encoding){
    case enc_mulaw8:
      return new Audio_Samples_Writer<Binary_Out_Stream, enc_mulaw8>(str,start_pos,num_channels);
    case enc_alaw8:
      return new Audio_Samples_Writer<Binary_Out_Stream, enc_alaw8>(str,start_pos,num_channels);
    case enc_linear8:
      return new Audio_Samples_Writer<Binary_Out_Stream, enc_linear8>(str,start_pos,num_channels);
    case enc_linear16:
      return new Audio_Samples_Writer<Binary_Out_Stream, enc_linear16>(str,start_pos,num_channels);
    case enc_linear24:
      return new Audio_Samples_Writer<Binary_Out_Stream, enc_linear24>(str,start_pos,num_channels);
    case enc_linear32:
      return new Audio_Samples_Writer<Binary_Out_Stream, enc_linear32>(str,start_pos,num_channels);
    case enc_float:
      return new Audio_Samples_Writer<Binary_Out_Stream, enc_float>(str,start_pos,num_channels);
    case enc_double:
      return new Audio_Samples_Writer<Binary_Out_Stream, enc_double>(str,start_pos,num_channels);
    case enc_unknown:
    default:
      return 0;
  }
}

//////////////////////////////////////////////////
//
// SND Header helpers
//
//////////////////////////////////////////////////
//snd header consists of 6 uint32_t fixed fields possibly followed
//by the variable length annotation
static const std::size_t snd_fixed_header_size = 24;

//read_header() reads audio stream information from snd file header.
//returns true if successfull.
//d - pointer to stream description to collect info from snd header
//audio_offset - byte offset of first audio sample inside snd file
//number of audio samples stored in snd file, num_samples
template<typename Binary_In_Stream>
bool read_header(Binary_In_Stream& _str, Audio_Stream_Description* d,
  std::streamoff& audio_offset, std::streamoff& num_samples)
{
  //SND header fields
  uint32_t magic, hdr_size, data_size, encoding, sampling_rate, num_channels;
  //encoded sample size
  std::size_t sample_size;
  //annotation
  std::string annotation;

  //read fixed fields of snd file header
  _str.seekg(0, std::ios_base::beg);
  _str >> magic >> hdr_size>> data_size>> encoding >> sampling_rate>> num_channels;
  if(!_str) return false;

  //check magic
  if(magic != snd_magic) return false; //invalid magic

  //check header size (we do not verify divisibility of header size by 8 since we still able to
  //read unaligned data samples)
  if(hdr_size < snd_fixed_header_size) return false; //header is too short
  audio_offset = hdr_size;

  //check encoding
  sample_size = encoded_sample_size((Audio_Encoding)encoding);
  if(!sample_size) return false; //unknown or invalid encoding

  //read annotation
  if(hdr_size > snd_fixed_header_size)
  {//annotation is present
    //get annotation length
    std::streamsize ann_length = (std::streamsize)std::min(hdr_size - snd_fixed_header_size, max_annotation_length);
  for(int i = 0; i < ann_length; ++i){
    char s; _str>>s;
    if(_str && s)
      annotation += s;
    else
      break;
  }
  if(!_str) return false; //failed to read annotation
  }

  //compute number of audio samples based on the file length
  _str.seekg(0, std::ios_base::end);
  if(!_str) return false; //failed to seek to the end
  std::streamoff ns = ((std::streamoff)_str.tellg() - hdr_size)/(num_channels * sample_size);

  //update number of samples just read from header
  if(data_size = 0xffffffff){
    //data size was set to unknown in file header, use number of smaples obtained from file length
    num_samples = ns;
  }
  else{
    num_samples = std::min<long unsigned int>((long unsigned int)ns,(std::streamoff)data_size/(num_channels * sample_size));
  }

  //update start position of audio samples
  audio_offset = hdr_size;

  //update stream description
  d->set_encoding((Audio_Encoding) encoding);
  d->set_num_channels(num_channels);
  d->set_sampling_rate(sampling_rate);
  d->set_description(annotation);
  return true;
}

//write_header() writes audio stream information to snd file header.
//it is assumed that stream description pointed by d is initialized with correct values
template<typename Binary_Out_Stream>
bool write_header(Binary_Out_Stream& _str, const Audio_Stream_Description* const d, std::streamoff& audio_offset)
{
  uint32_t hdr_size, data_size, encoding, sampling_rate, num_channels;
  data_size = 0xffffffff;
  encoding = (uint32_t)d->get_encoding();
  sampling_rate = (uint32_t)d->get_sampling_rate();
  num_channels = (uint32_t)d->get_num_channels();

  //compute header size based on fixed fields length and length of the annotation
  uint32_t ann_length = (uint32_t)std::min(d->get_description().length(), max_annotation_length);
  uint32_t padding_length = (8 - ((ann_length+1) % 8)) % 8; //compute padding length
  hdr_size = (uint32_t)snd_fixed_header_size + ann_length + padding_length + 1;

  //position stream pointer at the beginning of the file
  _str.seekp(0,std::ios_base::beg);
  if(!_str) return false;

  //write fixed-sized part of the header
  _str << snd_magic << hdr_size << data_size << encoding << sampling_rate << num_channels;
  if(!_str) return false;

  //write annotationn and padding
  _str.write(d->get_description().c_str(), ann_length);
  for(uint32_t i = 0; (i < (padding_length + 1)) && _str; ++i) _str << '\0';

  if(!_str) return false;
  audio_offset = hdr_size;

  return true;
}

//update_num_samples_in_header() updates numder of samples in snd file header
template<typename Binary_Out_Stream>
bool update_num_samples_in_header(Binary_Out_Stream& _str, const Audio_Stream_Description* const d, std::streamoff num_samples)
{
  uint32_t data_size = (uint32_t) std::min<long unsigned int>((long unsigned int)(num_samples * 
    d->get_num_channels() * encoded_sample_size(d->get_encoding())), 
    (long unsigned int)0xffffffff);
  _str.seekp(2*sizeof(uint32_t),std::ios_base::beg);
  if(!_str) return false;
  _str << data_size;
  if(!_str) return false;
  return true;
}


//////////////////////////////////////////////////
//
// SND_In_File
//
//////////////////////////////////////////////////

SND_In_File::SND_In_File():_samples_reader(0),
  _description(new Audio_Stream_Description), _num_samples(0)
{
}

SND_In_File::SND_In_File(const char *fname):_samples_reader(0),
  _description(new Audio_Stream_Description), _num_samples(0)
{
  open(fname);
}

bool SND_In_File::open(const char *fname)
{
  //try to reopen the stream
  if (_str.is_open()) close();
  _str.clear();
  _str.open(fname, bfstream_base::b_endian);
  if (!_str) return false;

  //read header and update description
  std::streamoff audio_offset;
  if (!read_header(_str,_description, audio_offset,_num_samples)) {
    _str.close();
    return false;
  }

  //create samples reader
  it_assert(_samples_reader == 0, "SND_In_File::open: samples reader was not deallocated properly.");
  _samples_reader = make_reader(_str,audio_offset,_description);
  return true;
}

void SND_In_File::close()
{
  //close stream
  if(_str.is_open()) _str.close();
  //dispose reader
  if(_samples_reader){
    delete _samples_reader;
    _samples_reader = 0;
  }
  //reset description
  _num_samples = 0;
  *_description = Audio_Stream_Description();
}

SND_In_File::~SND_In_File()
{
  //close file and dispose description
  close();
  delete _description;
}


//////////////////////////////////////////////////
//
// SND_Out_File
//
//////////////////////////////////////////////////
SND_Out_File::SND_Out_File():_samples_writer(0),
  _description(new Audio_Stream_Description), _num_samples(0)
{
}

SND_Out_File::SND_Out_File(const char *fname, const Audio_Stream_Description& d):
  _samples_writer(0), _description(new Audio_Stream_Description), _num_samples(0)
{
  open(fname, d);
}

bool SND_Out_File::open(const char *fname, const Audio_Stream_Description& d)
{
  //check if we have a valid description
  if(!is_valid(d)) return false;
  //try to reopen the stream
  if (_str.is_open()) close();
  _str.clear();
  _str.open(fname, true, bfstream_base::b_endian);
  if (!_str) return false;
  //init description and write header
  *_description = d;
  std::streamoff audio_offset;
  if (!write_header(_str, &d, audio_offset)){
    _str.close();
    return false;
  }
  //create samples writer
  it_assert(_samples_writer == 0, "SND_Out_File::open: samples writer was not deallocated properly.");
  _samples_writer = make_writer(_str,audio_offset,_description);
  _num_samples = 0;
  return true;
}

void SND_Out_File::close()
{
  //update file header with written samples counter and close the stream
  if(_str.is_open()){
    update_num_samples_in_header(_str,_description,_num_samples);
    _str.close();
  }
  //dispose samples writer
  if(_samples_writer){
    delete _samples_writer;
    _samples_writer = 0;
  }
  //reset description
  _num_samples = 0;
  *_description = Audio_Stream_Description();
}

SND_Out_File::~SND_Out_File()
{
  //close file and dispose description
  close();
  delete _description;
}
//////////////////////////////////////////////////
//
// SND_IO_File
//
//////////////////////////////////////////////////

SND_IO_File::SND_IO_File():
  _samples_reader(0), _samples_writer(0),
  _description(new Audio_Stream_Description), _num_samples(0)
{

}

SND_IO_File::SND_IO_File(const char *fname):
  _samples_reader(0), _samples_writer(0),
  _description(new Audio_Stream_Description), _num_samples(0)
{
  open(fname);
}

SND_IO_File::SND_IO_File(const char *fname, const Audio_Stream_Description& d):
  _samples_reader(0), _samples_writer(0),
  _description(new Audio_Stream_Description), _num_samples(0)
{
  open(fname, d);
}

//open IO file. Pick up information about audio stream from file header
bool SND_IO_File::open(const char *fname)
{
  //try to reopen the stream
  if (_str.is_open()) close();
  _str.clear();
  _str.open(fname, false, bfstream_base::b_endian);
  if (!_str) return false;
  //reader header and update description
  std::streamoff audio_offset;
  if (!read_header(_str,_description, audio_offset,_num_samples)) {
    _str.close();
    return false;
  }
  //create reader and writer for audio samples
  it_assert(_samples_reader == 0, "SND_IO_File::open: samples reader was not deallocated properly.");
  _samples_reader = make_reader(_str,audio_offset,_description);
  it_assert(_samples_writer == 0, "SND_IO_File::open: samples writer was not deallocated properly.");
  _samples_writer = make_writer(_str,audio_offset,_description);
  return true;

}

//open file for IO with new descrition - truncates file contents
bool SND_IO_File::open(const char *fname, const Audio_Stream_Description& d)
{
  //check if provided decription is valid
  if(!is_valid(d)) return false;

  //try reopen the stream
  if (_str.is_open()) close();
  _str.clear();
  _str.open(fname, true, bfstream_base::b_endian);
  if (!_str) return false;

  //init description and write file header
  *_description = d;
  std::streamoff audio_offset;
  if (!write_header(_str, &d, audio_offset)){
    _str.close();
    return false;
  }
  //create reader and writer for audio samples
  it_assert(_samples_reader == 0, "SND_IO_File::open: samples reader was not deallocated properly.");
  _samples_reader = make_reader(_str,audio_offset,_description);
  it_assert(_samples_writer == 0, "SND_IO_File::open: samples writer was not deallocated properly.");
  _samples_writer = make_writer(_str,audio_offset,_description);

  _num_samples = 0;
  return true;

}


void SND_IO_File::close()
{
  //close the stream and update number of written samples
  if(_str.is_open()){
    update_num_samples_in_header(_str,_description,_num_samples);
    _str.close();
  }
  //dispose reader and writer of audio samples
  if(_samples_writer){
    delete _samples_writer;
    _samples_writer = 0;
  }

  if(_samples_reader){
    delete _samples_reader;
    _samples_reader = 0;
  }
  //reset description
  _num_samples = 0;
  *_description = Audio_Stream_Description();
}

SND_IO_File::~SND_IO_File()
{
  //close audio file and dispose description
  close();
  delete _description;
}

} // namespace itpp

//! \endcond