-
Notifications
You must be signed in to change notification settings - Fork 4
/
QAudioDecoderStream.cpp
162 lines (130 loc) · 2.83 KB
/
QAudioDecoderStream.cpp
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
#include <QDebug>
#include "QAudioDecoderStream.h"
#include "QMixerStreamHandle.h"
QAudioDecoderStream::QAudioDecoderStream(const QString &fileName, const QAudioFormat &format)
: m_file(fileName)
, m_input(&m_data)
, m_output(&m_data)
, m_format(format)
, m_state(QtMixer::Stopped)
, m_loops(0)
, m_remainingLoops(0)
{
setOpenMode(QIODevice::ReadOnly);
const bool valid =
m_file.open(QIODevice::ReadOnly) &&
m_output.open(QIODevice::ReadOnly) &&
m_input.open(QIODevice::WriteOnly);
Q_ASSERT(valid);
m_decoder.setNotifyInterval(10);
m_decoder.setAudioFormat(format);
m_decoder.setSourceDevice(&m_file);
m_decoder.start();
connect(&m_decoder, &QAudioDecoder::bufferReady, this, &QAudioDecoderStream::bufferReady);
connect(&m_decoder, &QAudioDecoder::finished, this, &QAudioDecoderStream::finished);
}
qint64 QAudioDecoderStream::readData(char *data, qint64 maxlen)
{
memset(data, 0, maxlen);
if (m_state == QtMixer::Playing)
{
m_output.read(data, maxlen);
if (m_output.size() &&
m_output.atEnd())
{
if (m_loops != 0)
{
if (m_loops > 0)
{
if ((--m_remainingLoops) > 0)
{
rewind();
}
}
else
{
rewind();
}
}
}
}
return maxlen;
}
qint64 QAudioDecoderStream::writeData(const char *data, qint64 len)
{
Q_UNUSED(data);
Q_UNUSED(len);
return 0;
}
void QAudioDecoderStream::rewind()
{
m_output.seek(0);
}
void QAudioDecoderStream::bufferReady()
{
const QAudioBuffer &buffer = m_decoder.read();
const int length = buffer.byteCount();
const char *data = buffer.data<char>();
m_input.write(data, length);
}
void QAudioDecoderStream::finished()
{
emit decodingFinished(this);
}
bool QAudioDecoderStream::atEnd() const
{
return m_output.size()
&& m_output.atEnd();
}
void QAudioDecoderStream::play()
{
m_state = QtMixer::Playing;
emit stateChanged(this, m_state);
}
void QAudioDecoderStream::pause()
{
m_state = QtMixer::Paused;
emit stateChanged(this, m_state);
}
void QAudioDecoderStream::stop()
{
m_state = QtMixer::Stopped;
m_remainingLoops = m_loops;
rewind();
emit stateChanged(this, m_state);
}
QtMixer::State QAudioDecoderStream::state() const
{
return m_state;
}
int QAudioDecoderStream::loops() const
{
return m_loops;
}
void QAudioDecoderStream::setLoops(int loops)
{
m_loops = loops;
m_remainingLoops = loops;
}
int QAudioDecoderStream::position() const
{
return m_output.pos()
/ (m_format.sampleSize() / 8)
/ (m_format.sampleRate() / 1000)
/ (m_format.channelCount());
}
void QAudioDecoderStream::setPosition(int position)
{
const int target = position
* (m_format.sampleSize() / 8)
* (m_format.sampleRate() / 1000)
* (m_format.channelCount());
m_output.seek(target);
}
int QAudioDecoderStream::length()
{
return m_output.size()
/ (m_format.sampleSize() / 8)
/ (m_format.sampleRate() / 1000)
/ (m_format.channelCount());
}