kradio4  r778
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
soundformat.h
Go to the documentation of this file.
1 /***************************************************************************
2  soundformat.h - description
3  -------------------
4  begin : Sun Aug 1 2004
5  copyright : (C) 2004 by Martin Witte
6  email : emw-kradio@nocabal.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef KRADIO_SOUNDFORMAT_H
19 #define KRADIO_SOUNDFORMAT_H
20 
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #include <endian.h>
26 #include <QtCore/QString>
27 #include <kconfig.h>
28 
29 class KConfigGroup;
30 
31 struct KDE_EXPORT SoundFormat {
32  unsigned m_SampleRate;
33  unsigned m_Channels;
34  unsigned m_SampleBits;
35  bool m_IsSigned;
36  unsigned m_Endianess;
37  QString m_Encoding; // "raw", "mp3", ... (no "wav", because it's only header + raw data)
38 
39  SoundFormat(unsigned sample_rate, unsigned channels, unsigned sample_bits, bool is_signed, unsigned endianess, const QString &enc)
40  : m_SampleRate(sample_rate), m_Channels(channels), m_SampleBits(sample_bits), m_IsSigned(is_signed), m_Endianess(endianess), m_Encoding(enc) {}
41  SoundFormat(unsigned sample_rate, unsigned channels, unsigned sample_bits, bool is_signed, unsigned endianess)
42  : m_SampleRate(sample_rate), m_Channels(channels), m_SampleBits(sample_bits), m_IsSigned(is_signed), m_Endianess(endianess), m_Encoding("raw") {}
43  SoundFormat(unsigned sample_rate, unsigned channels, unsigned sample_bits, bool is_signed)
44  : m_SampleRate(sample_rate), m_Channels(channels), m_SampleBits(sample_bits), m_IsSigned(is_signed), m_Endianess(BYTE_ORDER), m_Encoding("raw") {}
45  SoundFormat(bool stereo)
46  : m_SampleRate(44100), m_Channels(stereo ? 2 : 1), m_SampleBits(16), m_IsSigned(true), m_Endianess(BYTE_ORDER), m_Encoding("raw") {}
48  : m_SampleRate(44100), m_Channels(2), m_SampleBits(16), m_IsSigned(true), m_Endianess(BYTE_ORDER), m_Encoding("raw") {}
49 
50  bool operator == (const SoundFormat &o) const { return m_SampleRate == o.m_SampleRate &&
51  m_Channels == o.m_Channels &&
52  m_SampleBits == o.m_SampleBits &&
53  m_IsSigned == o.m_IsSigned &&
54  m_Endianess == o.m_Endianess &&
55  m_Encoding == o.m_Encoding
56  ;
57  }
58  bool operator != (const SoundFormat &o) const { return !operator == (o); }
59 
60  int sampleSize() const; // size of a single sample
61  int frameSize() const; // sampleSize * channels
62  int minValue() const;
63  int maxValue() const;
64 
65  void restoreConfig(const QString &prefix, const KConfigGroup &c);
66  void saveConfig (const QString &prefix, KConfigGroup &c) const;
67 
68  void convertSamplesToFloatInterleaved (const char *src, float *dst, size_t n_frames) const;
69  void convertFloatInterleavedToSamples (const float *src, char *dst, size_t n_frames) const;
70  void convertSamplesToFloatNonInterleaved(const char *src, float **dst, size_t n_frames) const;
71  void convertFloatNonInterleavedToSamples(const float **src, char *dst, size_t n_frames) const;
72 
73  void scaleSamples (char *_src, float scale, size_t n_frames) const;
74  void minMaxAvgMagnitudePerChannel(const char *src, size_t n_frames, double *vmin, double *vmax, double *vavg) const;
75 
76  bool isValid() const { return m_SampleRate > 1000 && m_Channels > 0 && m_SampleBits >= 8; }
77 
78 };
79 
80 
81 #endif