-
Notifications
You must be signed in to change notification settings - Fork 0
/
audioplayback.cpp
106 lines (90 loc) · 2.92 KB
/
audioplayback.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
#include "audioplayback.h"
#include <QAudio>
#include <QDebug>
AudioPlayback::AudioPlayback(QObject *parent) :
QObject(parent)
,m_audioOutput()
,m_format()
{
m_format.setFrequency(8000);
m_format.setChannels(1);
m_format.setSampleSize(16);
m_format.setSampleType(QAudioFormat::SignedInt);
m_format.setByteOrder(QAudioFormat::LittleEndian);
m_format.setCodec("audio/pcm");
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(m_format))
{
qFatal("raw audio format not supported by backend, cannot play audio.");
}
}
void AudioPlayback::stopPlaying()
{
qDebug() << "AudioPlayback::stopPlaying";
m_audioOutput->stop();
}
void AudioPlayback::startPlaying(QIODevice * stream)
{
qDebug() << "AudioPlayback::startPlaying";
m_audioOutput = new QAudioOutput(m_format);
connect(m_audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(printState(QAudio::State)));
m_stream = stream;
connect(m_stream, SIGNAL(aboutToClose()), this, SLOT(stopPlaying()));
// connect(stream, SIGNAL(readyRead()), this, SLOT(processIncomingData()), Qt::DirectConnection);
// Open IODevice if not open already
if(!stream->isOpen())
{
stream->open(QIODevice::ReadOnly);
}
m_audioOutput->start(m_stream);
}
void AudioPlayback::printState(QAudio::State state)
{
QString output("QAudioOutput state: ");
switch(state)
{
case QAudio::ActiveState:
output += "QAudio::ActiveState";
break;
case QAudio::SuspendedState:
output += "QAudio::SuspendedState";
break;
case QAudio::StoppedState:
output += "QAudio::StoppedState";
break;
case QAudio::IdleState:
output += "QAudio::IdleState";
// m_audioOutput->stop();
// connect(m_stream, SIGNAL(readyRead()), this, SLOT(processIncomingData()), Qt::DirectConnection);
break;
}
if(m_audioOutput->error() != QAudio::NoError)
{
output += ", error: ";
switch(m_audioOutput->error())
{
case QAudio::NoError: // keeps QtCreator happy
case QAudio::OpenError:
output += "QAudio::OpenError";
break;
case QAudio::IOError:
output += "QAudio::IOError";
break;
case QAudio::UnderrunError:
output += "QAudio::UnderrunError";
break;
case QAudio::FatalError:
output += "QAudio::FatalError";
break;
}
}
qDebug() << output;
}
void AudioPlayback::processIncomingData()
{
// qDebug() << "AudioPlayback::processIncomingData";
// disconnect(m_stream, SIGNAL(readyRead()), this, SLOT(processIncomingData()));
// m_audioOutput->start(m_stream);
//m_audioOutput->start();
qDebug() << "Data.... ->" << m_stream->readAll();
}