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,6 +66,7 @@ Generator::Generator(const QAudioFormat &format,
: QIODevice(parent) : QIODevice(parent)
, m_pos(0) , m_pos(0)
{ {
if (format.isValid())
generateData(format, durationUs, sampleRate); generateData(format, durationUs, sampleRate);
} }
@@ -133,12 +134,14 @@ 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;
if (!m_buffer.isEmpty()) {
while (len - total > 0) { while (len - total > 0) {
const qint64 chunk = qMin((m_buffer.size() - m_pos), len - total); const qint64 chunk = qMin((m_buffer.size() - m_pos), len - total);
memcpy(data + total, m_buffer.constData() + m_pos, chunk); memcpy(data + total, m_buffer.constData() + m_pos, chunk);
m_pos = (m_pos + chunk) % m_buffer.size(); m_pos = (m_pos + chunk) % m_buffer.size();
total += chunk; total += chunk;
} }
}
return total; return total;
} }