Fix AudioOutput example when no audio devices are available.

Don't try to generate audio data with an invalid QAudioFormat, which
can happen when no audio devices are available.

Change-Id: I4de82dbf64def55fee21cf63ef99888a8084bd95
Reviewed-by: Christian Stromme <christian.stromme@digia.com>
This commit is contained in:
Yoann Lopes
2014-06-12 18:57:54 +02:00
parent a63da4b593
commit 382d4c873a

View File

@@ -66,7 +66,8 @@ Generator::Generator(const QAudioFormat &format,
: QIODevice(parent) : QIODevice(parent)
, m_pos(0) , m_pos(0)
{ {
generateData(format, durationUs, sampleRate); if (format.isValid())
generateData(format, durationUs, sampleRate);
} }
Generator::~Generator() Generator::~Generator()
@@ -133,11 +134,13 @@ void Generator::generateData(const QAudioFormat &format, qint64 durationUs, int
qint64 Generator::readData(char *data, qint64 len) qint64 Generator::readData(char *data, qint64 len)
{ {
qint64 total = 0; qint64 total = 0;
while (len - total > 0) { if (!m_buffer.isEmpty()) {
const qint64 chunk = qMin((m_buffer.size() - m_pos), len - total); while (len - total > 0) {
memcpy(data + total, m_buffer.constData() + m_pos, chunk); const qint64 chunk = qMin((m_buffer.size() - m_pos), len - total);
m_pos = (m_pos + chunk) % m_buffer.size(); memcpy(data + total, m_buffer.constData() + m_pos, chunk);
total += chunk; m_pos = (m_pos + chunk) % m_buffer.size();
total += chunk;
}
} }
return total; return total;
} }