File: audiofile.cpp

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (529 lines) | stat: -rw-r--r-- 13,186 bytes parent folder | download
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
/*!
 * \file
 * \brief Implementation of audio Audio classes and functions
 * \author Tobias Ringstrom and Adam Piatyszek
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program is free software; you can redistribute it and/or modfy
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

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

//! \cond

#define SND_MAGIC    0x2e736e64

using std::istream;
using std::ostream;
using std::ifstream;
using std::ofstream;
using std::ios;

namespace itpp {

  inline static short double_to_short(double x)
  {
    if (x >= 32767.0)
      return 32767;
    else if (x <= -32768.0)
      return -32768;
    else
      return round_i(x);
  }

  inline static signed char double_to_char(double x)
  {
    if (x >= 127.0)
      return 127;
    else if (x <= -128.0)
      return -128;
    else
      return round_i(x);
  }

  bool raw16le_read(const char *fname, vec &v)
  {
    ifstream file(fname, ios::in | ios::binary);
    if (!file)
      return false;

    // Check size of a file
    file.seekg(0, ios::end);
    int size = file.tellg();
    file.seekg(0, ios::beg);

    bool switch_endian = check_big_endianness(); // if BIG_ENDIAN than switch
    int n = size / 2; // short vs. byte
    v.set_size(n, false);
    for (int i = 0; i < n; i++)
      v(i) = read_endian<short>(file, switch_endian) / 32768.0;

    return true;
  }

  bool raw16le_read(const char *fname, vec &v, int beg, int len)
  {
    it_assert_debug(len >= 0, "raw16le_read()");
    ifstream file(fname, ios::in | ios::binary);
    if (!file)
      return false;

    bool switch_endian = check_big_endianness(); // if BIG_ENDIAN than switch
    v.set_size(len, false);
    file.seekg(2 * beg);
    for (int i = 0; i < len; i++)
      v(i) = read_endian<short>(file, switch_endian) / 32768.0;

    return true;
  }

  bool raw16le_write(const char *fname, const vec &v, bool append)
  {
    ofstream file(fname, (append ? ios::app | ios::ate : ios::out | ios::trunc) | ios::binary);
    if (!file)
      return false;

    bool switch_endian = check_big_endianness(); // if BIG_ENDIAN than switch
    for (int i = 0; i < v.size(); i++)
      write_endian<short>(file, double_to_short(v(i) * 32768.0), switch_endian);

    return true;
  }

  bool raw16be_read(const char *fname, vec &v)
  {
    ifstream file(fname, ios::in | ios::binary);
    if (!file)
      return false;

    // Check size of a file
    file.seekg(0, ios::end);
    int size = file.tellg();
    file.seekg(0, ios::beg);

    bool switch_endian = !check_big_endianness(); // if LITTLE_ENDIAN than switch
    int n = size / 2; // short vs. byte
    v.set_size(n, false);
    for (int i = 0; i < n; i++)
      v(i) = read_endian<short>(file, switch_endian) / 32768.0;

    return true;
  }

  bool raw16be_read(const char *fname, vec &v, int beg, int len)
  {
    it_assert_debug(len >= 0, "raw16le_read()");
    ifstream file(fname, ios::in | ios::binary);
    if (!file)
      return false;

    bool switch_endian = !check_big_endianness(); // if LITTLE_ENDIAN than switch
    v.set_size(len, false);
    file.seekg(2 * beg);
    for (int i = 0; i < len; i++)
      v(i) = read_endian<short>(file, switch_endian) / 32768.0;

    return true;
  }

  bool raw16be_write(const char *fname, const vec &v, bool append)
  {
    ofstream file(fname, (append ? ios::app | ios::ate : ios::out | ios::trunc) | ios::binary);
    if (!file)
      return false;

    bool switch_endian = !check_big_endianness(); // if LITTLE_ENDIAN than switch
    for (int i = 0; i < v.size(); i++)
      write_endian<short>(file, double_to_short(v(i) * 32768.0), switch_endian);

    return true;
  }

  //////////////////////////////////////////////////
  //
  // Audio_File
  //
  //////////////////////////////////////////////////
  Audio_File::Audio_File()
  {
    is_valid = false;
  }

  //////////////////////////////////////////////////
  //
  // SND_Format
  //
  //////////////////////////////////////////////////
  int SND_Format::sample_size() const
  {
    switch (header.encoding) {
    case enc_mulaw8 :   return 1;
    case enc_alaw8 :    return 1;
    case enc_linear8 :  return 1;
    case enc_linear16 : return 2;
    case enc_linear24 : return 3;
    case enc_linear32 : return 4;
    case enc_float :    return 4;
    case enc_double :   return 8;
    }
    return 0;
  }

  bool SND_Format::read_header(std::istream &f)
  {
    bool switch_endian = !check_big_endianness(); // if LITTLE_ENDIAN than switch
    f.seekg(0);
    header.magic = read_endian<unsigned int>(f, switch_endian);
    header.hdr_size = read_endian<unsigned int>(f, switch_endian);
    header.data_size = read_endian<unsigned int>(f, switch_endian);
    header.encoding = read_endian<unsigned int>(f, switch_endian);
    header.sample_rate = read_endian<unsigned int>(f, switch_endian);
    header.channels = read_endian<unsigned int>(f, switch_endian);
    f.read(header.info, SND_INFO_LEN);
    if (!f || header.magic != SND_MAGIC) {
      std::cerr << header.magic << " != " << SND_MAGIC << std::endl;
      it_warning("SND_Format::read_header(): This is not a .snd file!");
      return false;
    }
    f.seekg(header.hdr_size);

    return f.good();
  }

  bool SND_Format::write_header(std::ostream &f)
  {
    f.seekp(0);
    header.magic = SND_MAGIC;
    header.hdr_size = sizeof(header);
    memset(header.info, 0, SND_INFO_LEN);

    bool switch_endian = !check_big_endianness(); // if LITTLE_ENDIAN than switch
    write_endian<unsigned int>(f, header.magic, switch_endian);
    write_endian<unsigned int>(f, header.hdr_size, switch_endian);
    write_endian<unsigned int>(f, header.data_size, switch_endian);
    write_endian<unsigned int>(f, header.encoding, switch_endian);
    write_endian<unsigned int>(f, header.sample_rate, switch_endian);
    write_endian<unsigned int>(f, header.channels, switch_endian);
    f.write(reinterpret_cast<char *>(&header.info), SND_INFO_LEN);

    return f.good();
  }

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

  SND_In_File::SND_In_File()
  {
  }

  SND_In_File::SND_In_File(const char *fname)
  {
    open(fname);
  }

  bool SND_In_File::open(const char *fname)
  {
    if (file.is_open())
      close();
    file.clear();
    is_valid = false;
    file.open(fname, ios::in | ios::binary);
    if (!file)
      return false;
    if (!read_header(file)) {
      file.close();
      return false;
    }

    is_valid = true;
    return true;
  }

  void SND_In_File::close()
  {
    file.close();
    is_valid = false;
  }

  bool SND_In_File::seek_read(int pos)
  {
    if (pos < 0)
      file.seekg(0, ios::end);
    else
      file.seekg(header.hdr_size + header.channels * sample_size() * pos);
    return true;
  }

  int SND_In_File::tell_read()
  {
    if (!good())
      return -1;

    return ((static_cast<int>(file.tellg()) - sizeof(header))
	    / (header.channels * sample_size()));
  }

  bool SND_In_File::read(vec &v)
  {
    if (!good())
      return false;

    int i, n;

    n = samples();
    v.set_size(n, false);
    seek_read(0);

    bool switch_endian = !check_big_endianness(); // if LITTLE_ENDIAN than switch
    switch (header.encoding) {
    case enc_linear8 :
      for (i=0; i<n; i++)
	v(i) = read_endian<char>(file, switch_endian) / 128.0;
      break;
    case enc_linear16 :
      for (i=0; i<n; i++)
	v(i) = read_endian<short>(file, switch_endian) / 32768.0;
      break;
    case enc_float :
      for (i=0; i<n; i++)
	v(i) = read_endian<float>(file, switch_endian);
      break;
    case enc_double :
      for (i=0; i<n; i++)
	v(i) = read_endian<double>(file, switch_endian);
      break;
    default :
      it_warning("SND_In_File::read(): Unsupported encoding!");
      return false;
    }
    return file.good();
  }

  bool SND_In_File::read(vec &v, int n)
  {
    if (!good())
      return false;

    int i;

    bool switch_endian = !check_big_endianness(); // if LITTLE_ENDIAN than switch
    v.set_size(n, false);
    switch (header.encoding) {
    case enc_linear8 :
      for (i=0; i<n; i++)
	v(i) = read_endian<char>(file, switch_endian) / 128.0;
      break;
    case enc_linear16 :
      for (i=0; i<n; i++)
	v(i) = read_endian<short>(file, switch_endian) / 32768.0;
      break;
    case enc_float :
      for (i=0; i<n; i++)
	v(i) = read_endian<float>(file, switch_endian);
      break;
    case enc_double :
      for (i=0; i<n; i++)
	v(i) = read_endian<double>(file, switch_endian);
      break;
    default :
      it_warning("SND_In_File::read(): Unsupported encoding!");
      return false;
    }
    return file.good();
  }

  //////////////////////////////////////////////////
  //
  // SND_Out_File
  //
  //////////////////////////////////////////////////
  SND_Out_File::SND_Out_File()
  {
  }

  SND_Out_File::SND_Out_File(const char *fname, int rate, data_encoding e)
  {
    open(fname, rate, e);
  }

  bool SND_Out_File::open(const char *fname, int rate, data_encoding e)
  {
    if (file.is_open())
      close();
    file.clear();
    is_valid = false;
    file.open(fname, ios::out | ios::trunc | ios::binary);
    if (!file)
      return false;

    header.data_size = 0;
    header.encoding = static_cast<unsigned int>(e);
    header.sample_rate = rate;
    header.channels = 1;

    if (!write_header(file))
      return false;

    is_valid = true;
    return true;
  }

  void SND_Out_File::close()
  {
    file.seekp(0, ios::end);
    header.data_size = static_cast<int>(file.tellp()) - sizeof(header);
    write_header(file);
    file.close();
    is_valid = false;
  }

  bool SND_Out_File::seek_write(int pos)
  {
    if (!good())
      return false;

    if (pos < 0)
      file.seekp(0, ios::end);
    else
      file.seekp(sizeof(header) + header.channels * sample_size() * pos);
    return true;
  }

  int SND_Out_File::tell_write()
  {
    if (!good())
      return -1;

    return ((static_cast<int>(file.tellp()) - sizeof(header))
	    / (header.channels * sample_size()));
  }

  bool SND_Out_File::write(const vec &v)
  {
    if (!good())
      return false;

    int i;

    bool switch_endian = !check_big_endianness(); // if LITTLE_ENDIAN than switch
    switch (header.encoding) {
    case enc_linear8 :
      for (i=0; i<v.size(); i++)
	write_endian<char>(file, double_to_char(v(i) * 128.0), switch_endian);
      break;
    case enc_linear16 :
      for (i=0; i<v.size(); i++)
	write_endian<short>(file, double_to_short(v(i) * 32768.0),
			    switch_endian);
      break;
    case enc_float :
      for (i=0; i<v.size(); i++)
	write_endian<float>(file, static_cast<float>(v(i)), switch_endian);
      break;
    case enc_double :
      for (i=0; i<v.size(); i++)
	write_endian<double>(file, static_cast<double>(v(i)), switch_endian);
      break;
    default :
      it_warning("SND_Out_File::write(): Unsupported encoding!");
      return false;
    }

    return file.good();
  }

  //////////////////////////////////////////////////
  //
  // SND_IO_File
  //
  //////////////////////////////////////////////////
  bool SND_IO_File::open(const char *fname)
  {
    if (file.is_open())
      close();
    file.clear();
    is_valid = false;
    file.open(fname, ios::in | ios::out | ios::binary);
    if (!file)
      return false;

    if (!read_header(file)) {
      file.close();
      return false;
    }

    if (!seek_read(0) || !seek_write(0)) {
      file.close();
      return false;
    }

    is_valid = true;
    return true;
  }

  void SND_IO_File::close()
  {
    write_header(file);
    file.close();
    is_valid = false;
  }

  bool snd_read(const char *fname, vec &v)
  {
    SND_In_File file;

    if (!file.open(fname))
      return false;

    return file.read(v);
  }

  bool snd_read(const char *fname, vec &v, int beg, int len)
  {
    SND_In_File file;

    if (!file.open(fname))
      return false;

    file.seek_read(beg);
    return file.read(v, len);
  }

  bool snd_write(const char *fname, const vec &v, int rate, SND_Format::data_encoding e)
  {
    SND_Out_File file;

    if (!file.open(fname, rate, e))
      return false;

    return file.write(v);
  }

} // namespace itpp

//! \endcond