aboutsummaryrefslogtreecommitdiff
path: root/lib/decoder.h
blob: 97a3e1da6a4989424f48c8fb9445339a1dfd9128 (plain) (blame)
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
// Copyright (c) 2000-2001 Brad Hughes <bhughes@trolltech.com>
//
// Use, modification and distribution is allowed without limitation,
// warranty, or liability of any kind.
//

#ifndef   DECODER_H
#define   DECODER_H

#include <QThread>
#include <QList>
#include <QMutex>
#include <QWaitCondition>
#include <QObject>
#include <QStringList>

#include "filetag.h"

class QObject;
class QIODevice;

class Decoder;
class DecoderFactory;
class Buffer;
class Recycler;
class Output;
class Visualization;
class Effect;



class DecoderState
{
public:
    enum Type { Decoding, Stopped, Finished, Info, Error };

    DecoderState(const DecoderState &st)
            : m_error_msg(0), m_tag(0)
    {
        m_type = st.type();
        if (m_type == Info)
            m_tag = new FileTag(*st.tag());
        else if (m_type == Error)
            m_error_msg = new QString(*st.errorMessage());
    }


    DecoderState(Type t)
            : m_type(t), m_error_msg(0), m_tag(0)
{}

    DecoderState(const QString &e)
            : m_type(Error), m_tag(0)
    {
        m_error_msg = new QString(e);
    }

    DecoderState()
            : m_type(Stopped), m_error_msg(0), m_tag(0)
    {}

    DecoderState(const FileTag &tag)
            : m_type(Info), m_error_msg(0),  m_tag(0)
    {
        m_tag = new FileTag(tag);
    }

    ~DecoderState()
    {
        if (m_error_msg)
            delete m_error_msg;
        if (m_tag)
            delete m_tag;
    }

    const QString *errorMessage() const
    {
        return m_error_msg;
    }
    const Type &type() const
    {
        return m_type;
    }
    const FileTag *tag() const
    {
        return m_tag;
    }

private:
    Type m_type;
    const QString *m_error_msg;
    FileTag *m_tag;
};



class Decoder : public QThread
{
    Q_OBJECT
public:
    Decoder(QObject *parent, DecoderFactory *d,
            QIODevice *i, Output *o);
    virtual ~Decoder();

    // Standard Decoder API
    virtual bool initialize() = 0;
    virtual double lengthInSeconds() = 0;
    virtual void seek(double) = 0;
    virtual void stop() = 0;

    DecoderFactory *factory() const
    {
        return fctry;
    }

    QIODevice *input()
    {
        return in;
    }
    Output *output()
    {
        return m_output;
    }

    QMutex *mutex()
    {
        return &mtx;
    }
    QWaitCondition *cond()
    {
        return &cnd;
    }

    void setBlockSize(unsigned int sz)
    {
        blksize = sz;
    }

    unsigned int blockSize() const
    {
        return blksize;
    }
    ulong produceSound(char *data, ulong output_bytes, ulong bitrate, int nch);
    void setEQ(int bands[10], int preamp);
    void setEQEnabled(bool on)
    {
        m_useEQ = on;
    };

    // static methods
    static QStringList all();
    static bool supports(const QString &);
    //static void registerFactory(DecoderFactory *);
    static Decoder *create(QObject *, const QString &, QIODevice *, Output *);
    static DecoderFactory *findByPath(const QString&);
    static DecoderFactory *findByMime(const QString&);
    static DecoderFactory *findByContent(QIODevice *);
    static FileTag *createTag(const QString&);
    static QString filter();
    static QStringList nameFilters();
    static QList<DecoderFactory*> *decoderFactories();
    static QStringList decoderFiles();

signals:
    void stateChanged(const DecoderState&);

protected:
    void configure(long freq, int channels, int prec, int bitrate);
    void dispatch(DecoderState::Type);
    void dispatch(const DecoderState&);
    void dispatch(const FileTag&);
    void error(const QString&);

private:
    DecoderFactory *fctry;

    QList <Effect*> m_effects;
    QIODevice *in;
    Output *m_output;

    QMutex mtx;
    QWaitCondition cnd;

    uint blksize;
    bool m_eqInited;
    bool m_useEQ;

};

#endif // DECODER_H