summaryrefslogtreecommitdiffstats
path: root/kradio3/src/include/soundformat.h
blob: 0fcca2487b823ef13d747bb15a51d4b7ff493886 (plain)
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
/***************************************************************************
                      soundformat.h  -  description
                             -------------------
    begin                : Sun Aug 1 2004
    copyright            : (C) 2004 by Martin Witte
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef KRADIO_SOUNDFORMAT_H
#define KRADIO_SOUNDFORMAT_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <endian.h>
#include <tqstring.h>
#include <kconfig.h>

struct SoundFormat {
    unsigned     m_SampleRate;
    unsigned     m_Channels;
    unsigned     m_SampleBits;
    bool         m_IsSigned;
    unsigned     m_Endianess;
    TQString      m_Encoding;     // "raw", "mp3", ...  (no "wav", because it's only header + raw data)

    SoundFormat(unsigned sample_rate, unsigned channels, unsigned sample_bits, bool is_signed, unsigned endianess, const TQString &enc)
        : m_SampleRate(sample_rate), m_Channels(channels), m_SampleBits(sample_bits), m_IsSigned(is_signed), m_Endianess(endianess), m_Encoding(enc) {}
    SoundFormat(unsigned sample_rate, unsigned channels, unsigned sample_bits, bool is_signed, unsigned endianess)
        : m_SampleRate(sample_rate), m_Channels(channels), m_SampleBits(sample_bits), m_IsSigned(is_signed), m_Endianess(endianess), m_Encoding("raw") {}
    SoundFormat(unsigned sample_rate, unsigned channels, unsigned sample_bits, bool is_signed)
        : m_SampleRate(sample_rate), m_Channels(channels), m_SampleBits(sample_bits), m_IsSigned(is_signed), m_Endianess(BYTE_ORDER), m_Encoding("raw") {}
    SoundFormat(bool stereo)
        : m_SampleRate(44100), m_Channels(stereo ? 2 : 1), m_SampleBits(16), m_IsSigned(true), m_Endianess(BYTE_ORDER), m_Encoding("raw") {}
    SoundFormat()
        : m_SampleRate(44100), m_Channels(2), m_SampleBits(16), m_IsSigned(true), m_Endianess(BYTE_ORDER), m_Encoding("raw") {}

    bool operator == (const SoundFormat &o) const { return m_SampleRate == o.m_SampleRate &&
                                                           m_Channels   == o.m_Channels &&
                                                           m_SampleBits == o.m_SampleBits &&
                                                           m_IsSigned   == o.m_IsSigned &&
                                                           m_Endianess  == o.m_Endianess &&
                                                           m_Encoding   == o.m_Encoding
                                                    ;
                                                   }
    bool operator != (const SoundFormat &o) const  { return !operator == (o); }

    int      sampleSize() const;      // size of a single sample
    int      frameSize() const;       // sampleSize * channels
    int      minValue() const;
    int      maxValue() const;

    void     restoreConfig(const TQString &prefix, TDEConfig *c);
    void     saveConfig(const TQString &prefix, TDEConfig *c) const;

    int      convertSampleToInt(const char *sample, bool do_scale) const;
    void     convertIntToSample(int src, char *dst, bool is_scaled) const;
    void     convertSamplesToInts(const char *src, int  *dst, size_t n, bool do_scale) const;
    void     convertIntsToSamples(const int  *src, char *dst, size_t n, bool is_scaled) const;
    void     convertSamplesToFloat (const char   *src, float **dst, size_t n) const;
    void     convertFloatsToSamples(const float **src,   char *dst, size_t n) const;
};


#endif