-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudioplayer.cpp
More file actions
65 lines (57 loc) · 1.82 KB
/
audioplayer.cpp
File metadata and controls
65 lines (57 loc) · 1.82 KB
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
#include "audioplayer.h"
AudioPlayer::AudioPlayer(QObject *parent) : QObject(parent)
{
_dataBuffer = new QBuffer(&_data,this);
_audioInputBuffer = new QBuffer(&_audioData,this);
_audioOutputBuffer = new QBuffer(&_audioData,this);
_audioOutput = nullptr;
_decoder = new QAudioDecoder(this);
_audioOutput = nullptr;
_audioInputBuffer->open(QIODevice::WriteOnly);
_audioOutputBuffer->open(QIODevice::ReadOnly);
_decoder->setSourceDevice(_dataBuffer);
QAudioFormat format;
// Set up the format, eg.
format.setSampleRate(48000);
format.setChannelCount(1);
format.setSampleSize(32);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::Float);
_decoder->setAudioFormat(format);
_timer = new QTimer(this);
_timer->setSingleShot(true);
_timer->setInterval(50);
_audioOutput = new QAudioOutput(format,this);
connect(_decoder,&QAudioDecoder::bufferReady,this,&AudioPlayer::bufferReady);
connect(_timer,&QTimer::timeout,this,&AudioPlayer::playInternal);
}
void AudioPlayer::play(const QByteArray& data)
{
_newData = data;
_timer->start();
}
void AudioPlayer::playInternal()
{
_decoder->stop();
_audioOutput->stop();
_dataBuffer->close();
_audioInputBuffer->close();
_audioOutputBuffer->close();
_data = _newData;
_audioData.clear();
_dataBuffer->open(QIODevice::ReadOnly);
_audioInputBuffer->open(QIODevice::WriteOnly);
_audioOutputBuffer->open(QIODevice::ReadOnly);
_audioOutput->start(_audioOutputBuffer);
_decoder->start();
}
void AudioPlayer::bufferReady()
{
if(_decoder->bufferAvailable() && _audioInputBuffer->isOpen())
{
QAudioBuffer buffer = _decoder->read();
_audioData.append(buffer.constData<char>(),buffer.byteCount());
// _audioInputBuffer->write(buffer.constData<char>(),buffer.byteCount());
}
}