From 5aa705f448900cfe9e6a31dca1c0cd5d3e95df7e Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Mon, 9 Jun 2014 09:27:32 +0300 Subject: Fix audiolevels crash when no audio devices found MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ia5786ea399e2602cd5c36486c8a0d945bad4fb45 Reviewed-by: Tomi Korpipää --- .../datavisualization/audiolevels/audiolevels.cpp | 50 +++++++++++++++------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to 'examples') diff --git a/examples/datavisualization/audiolevels/audiolevels.cpp b/examples/datavisualization/audiolevels/audiolevels.cpp index 81059460..672e4984 100644 --- a/examples/datavisualization/audiolevels/audiolevels.cpp +++ b/examples/datavisualization/audiolevels/audiolevels.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -59,31 +60,48 @@ AudioLevels::AudioLevels(Q3DBars *graph, QObject *parent) //! [0] QAudioDeviceInfo inputDevice = QAudioDeviceInfo::defaultInputDevice(); - QAudioFormat formatAudio; - formatAudio.setSampleRate(inputDevice.supportedSampleRates().at(0)); - formatAudio.setChannelCount(inputDevice.supportedChannelCounts().at(0)); - formatAudio.setSampleSize(inputDevice.supportedSampleSizes().at(0)); - formatAudio.setCodec(inputDevice.supportedCodecs().at(0)); - formatAudio.setByteOrder(QAudioFormat::LittleEndian); - formatAudio.setSampleType(QAudioFormat::UnSignedInt); + if (inputDevice.supportedSampleRates().size() > 0 + && inputDevice.supportedChannelCounts().size() > 0 + && inputDevice.supportedSampleSizes().size() > 0 + && inputDevice.supportedCodecs().size() > 0) { + QAudioFormat formatAudio; + formatAudio.setSampleRate(inputDevice.supportedSampleRates().at(0)); + formatAudio.setChannelCount(inputDevice.supportedChannelCounts().at(0)); + formatAudio.setSampleSize(inputDevice.supportedSampleSizes().at(0)); + formatAudio.setCodec(inputDevice.supportedCodecs().at(0)); + formatAudio.setByteOrder(QAudioFormat::LittleEndian); + formatAudio.setSampleType(QAudioFormat::UnSignedInt); - m_audioInput = new QAudioInput(inputDevice, formatAudio, this); + m_audioInput = new QAudioInput(inputDevice, formatAudio, this); #ifdef Q_OS_MAC - // Mac seems to wait for entire buffer to fill before calling writeData, so use smaller buffer - m_audioInput->setBufferSize(256); + // Mac seems to wait for entire buffer to fill before calling writeData, so use smaller buffer + m_audioInput->setBufferSize(256); #else - m_audioInput->setBufferSize(1024); + m_audioInput->setBufferSize(1024); #endif - m_device = new AudioLevelsIODevice(m_graph->seriesList().at(0)->dataProxy(), this); - m_device->open(QIODevice::WriteOnly); + m_device = new AudioLevelsIODevice(m_graph->seriesList().at(0)->dataProxy(), this); + m_device->open(QIODevice::WriteOnly); - m_audioInput->start(m_device); + m_audioInput->start(m_device); + } else { + // No graph content can be shown, so add a custom warning label + QCustom3DLabel *titleLabel = new QCustom3DLabel("No valid audio input device found", + QFont(), + QVector3D(0.2f, 0.2f, 0.0f), + QVector3D(1.0f, 1.0f, 0.0f), + QQuaternion()); + titleLabel->setPositionAbsolute(true); + titleLabel->setFacingCamera(true); + m_graph->addCustomItem(titleLabel); + } //! [0] } AudioLevels::~AudioLevels() { - m_audioInput->stop(); - m_device->close(); + if (m_audioInput) + m_audioInput->stop(); + if (m_device) + m_device->close(); } -- cgit v1.2.3