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