1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef WAVEDECODER_H
5#define WAVEDECODER_H
6
7#include <QtCore/qiodevice.h>
8#include <QtMultimedia/qaudioformat.h>
9
10
11QT_BEGIN_NAMESPACE
12
13
14
15class Q_MULTIMEDIA_EXPORT QWaveDecoder : public QIODevice
16{
17 Q_OBJECT
18
19public:
20 explicit QWaveDecoder(QIODevice *device, QObject *parent = nullptr);
21 explicit QWaveDecoder(QIODevice *device, const QAudioFormat &format,
22 QObject *parent = nullptr);
23 ~QWaveDecoder();
24
25 QAudioFormat audioFormat() const;
26 QIODevice* getDevice();
27 int duration() const;
28 static qint64 headerLength();
29
30 bool open(QIODevice::OpenMode mode) override;
31 void close() override;
32 bool seek(qint64 pos) override;
33 qint64 pos() const override;
34 void setIODevice(QIODevice *device);
35 qint64 size() const override;
36 bool isSequential() const override;
37 qint64 bytesAvailable() const override;
38
39Q_SIGNALS:
40 void formatKnown();
41 void parsingError();
42
43private Q_SLOTS:
44 void handleData();
45
46private:
47 qint64 readData(char *data, qint64 maxlen) override;
48 qint64 writeData(const char *data, qint64 len) override;
49
50 bool writeHeader();
51 bool writeDataLength();
52 bool enoughDataAvailable();
53 bool findChunk(const char *chunkId);
54 void discardBytes(qint64 numBytes);
55 void parsingFailed();
56
57 enum State {
58 InitialState,
59 WaitingForFormatState,
60 WaitingForDataState
61 };
62
63 struct chunk
64 {
65 char id[4];
66 quint32 size;
67 };
68 bool peekChunk(chunk* pChunk, bool handleEndianness = true);
69
70 struct RIFFHeader
71 {
72 chunk descriptor;
73 char type[4];
74 };
75 struct WAVEHeader
76 {
77 chunk descriptor;
78 quint16 audioFormat;
79 quint16 numChannels;
80 quint32 sampleRate;
81 quint32 byteRate;
82 quint16 blockAlign;
83 quint16 bitsPerSample;
84 };
85
86 struct DATAHeader
87 {
88 chunk descriptor;
89 };
90
91 struct CombinedHeader
92 {
93 RIFFHeader riff;
94 WAVEHeader wave;
95 DATAHeader data;
96 };
97 static const int HeaderLength = sizeof(CombinedHeader);
98
99 bool haveFormat = false;
100 bool haveHeader = false;
101 qint64 dataSize = 0;
102 QIODevice *device = nullptr;
103 QAudioFormat format;
104 State state = InitialState;
105 quint32 junkToSkip = 0;
106 bool bigEndian = false;
107 bool byteSwap = false;
108 int bps = 0;
109};
110
111QT_END_NAMESPACE
112
113#endif // WAVEDECODER_H
114

source code of qtmultimedia/src/multimedia/audio/qwavedecoder.h