summaryrefslogtreecommitdiffstats
path: root/examples/multimedia/audiosource/audiosource.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/multimedia/audiosource/audiosource.cpp')
-rw-r--r--examples/multimedia/audiosource/audiosource.cpp154
1 files changed, 66 insertions, 88 deletions
diff --git a/examples/multimedia/audiosource/audiosource.cpp b/examples/multimedia/audiosource/audiosource.cpp
index ef0379e02..48e33fa2d 100644
--- a/examples/multimedia/audiosource/audiosource.cpp
+++ b/examples/multimedia/audiosource/audiosource.cpp
@@ -1,70 +1,26 @@
-/****************************************************************************
-**
-** Copyright (C) 2021 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the examples of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** BSD License Usage
-** Alternatively, you may use this file under the terms of the BSD license
-** as follows:
-**
-** "Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are
-** met:
-** * Redistributions of source code must retain the above copyright
-** notice, this list of conditions and the following disclaimer.
-** * Redistributions in binary form must reproduce the above copyright
-** notice, this list of conditions and the following disclaimer in
-** the documentation and/or other materials provided with the
-** distribution.
-** * Neither the name of The Qt Company Ltd nor the names of its
-** contributors may be used to endorse or promote products derived
-** from this software without specific prior written permission.
-**
-**
-** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "audiosource.h"
-#include <stdlib.h>
-#include <math.h>
-
+#include <QAudioDevice>
+#include <QAudioSource>
#include <QDateTime>
#include <QDebug>
+#include <QLabel>
#include <QPainter>
#include <QVBoxLayout>
-#include <QAudioDevice>
-#include <QAudioSource>
-#include <qendian.h>
+#include <QtEndian>
-AudioInfo::AudioInfo(const QAudioFormat &format)
- : m_format(format)
-{
-}
+#if QT_CONFIG(permissions)
+ #include <QCoreApplication>
+ #include <QPermission>
+#endif
+
+#include <math.h>
+#include <stdlib.h>
+
+AudioInfo::AudioInfo(const QAudioFormat &format) : m_format(format) { }
void AudioInfo::start()
{
@@ -110,8 +66,7 @@ qint64 AudioInfo::writeData(const char *data, qint64 len)
return len;
}
-RenderArea::RenderArea(QWidget *parent)
- : QWidget(parent)
+RenderArea::RenderArea(QWidget *parent) : QWidget(parent)
{
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
@@ -132,8 +87,7 @@ void RenderArea::paintEvent(QPaintEvent * /* event */)
return;
const int pos = qRound(qreal(frame.width() - 1) * m_level);
- painter.fillRect(frame.left() + 1, frame.top() + 1,
- pos, frame.height() - 1, Qt::red);
+ painter.fillRect(frame.left() + 1, frame.top() + 1, pos, frame.height() - 1, Qt::red);
}
void RenderArea::setLevel(qreal value)
@@ -142,12 +96,9 @@ void RenderArea::setLevel(qreal value)
update();
}
-
-InputTest::InputTest()
- : m_devices(new QMediaDevices(this))
+InputTest::InputTest() : m_devices(new QMediaDevices(this))
{
- initializeWindow();
- initializeAudio(QMediaDevices::defaultAudioInput());
+ init();
}
void InputTest::initializeWindow()
@@ -160,7 +111,7 @@ void InputTest::initializeWindow()
m_deviceBox = new QComboBox(this);
const QAudioDevice &defaultDeviceInfo = QMediaDevices::defaultAudioInput();
m_deviceBox->addItem(defaultDeviceInfo.description(), QVariant::fromValue(defaultDeviceInfo));
- for (auto &deviceInfo: m_devices->audioInputs()) {
+ for (auto &deviceInfo : m_devices->audioInputs()) {
if (deviceInfo != defaultDeviceInfo)
m_deviceBox->addItem(deviceInfo.description(), QVariant::fromValue(deviceInfo));
}
@@ -191,18 +142,45 @@ void InputTest::initializeAudio(const QAudioDevice &deviceInfo)
format.setSampleFormat(QAudioFormat::Int16);
m_audioInfo.reset(new AudioInfo(format));
- connect(m_audioInfo.data(), &AudioInfo::levelChanged,
- m_canvas, &RenderArea::setLevel);
+ connect(m_audioInfo.data(), &AudioInfo::levelChanged, m_canvas, &RenderArea::setLevel);
m_audioInput.reset(new QAudioSource(deviceInfo, format));
- qreal initialVolume = QAudio::convertVolume(m_audioInput->volume(),
- QAudio::LinearVolumeScale,
+ qreal initialVolume = QAudio::convertVolume(m_audioInput->volume(), QAudio::LinearVolumeScale,
QAudio::LogarithmicVolumeScale);
m_volumeSlider->setValue(qRound(initialVolume * 100));
m_audioInfo->start();
toggleMode();
}
+void InputTest::initializeErrorWindow()
+{
+ QVBoxLayout *layout = new QVBoxLayout(this);
+ QLabel *errorLabel = new QLabel(tr("Microphone permission is not granted!"));
+ errorLabel->setWordWrap(true);
+ errorLabel->setAlignment(Qt::AlignCenter);
+ layout->addWidget(errorLabel);
+}
+
+void InputTest::init()
+{
+#if QT_CONFIG(permissions)
+ QMicrophonePermission microphonePermission;
+ switch (qApp->checkPermission(microphonePermission)) {
+ case Qt::PermissionStatus::Undetermined:
+ qApp->requestPermission(microphonePermission, this, &InputTest::init);
+ return;
+ case Qt::PermissionStatus::Denied:
+ qWarning("Microphone permission is not granted!");
+ initializeErrorWindow();
+ return;
+ case Qt::PermissionStatus::Granted:
+ break;
+ }
+#endif
+ initializeWindow();
+ initializeAudio(QMediaDevices::defaultAudioInput());
+}
+
void InputTest::toggleMode()
{
m_audioInput->stop();
@@ -218,18 +196,17 @@ void InputTest::toggleMode()
if (!io)
return;
- connect(io, &QIODevice::readyRead,
- [this, io]() {
- static const qint64 BufferSize = 4096;
- const qint64 len = qMin(m_audioInput->bytesAvailable(), BufferSize);
-
- QByteArray buffer(len, 0);
- qint64 l = io->read(buffer.data(), len);
- if (l > 0) {
- const qreal level = m_audioInfo->calculateLevel(buffer.constData(), l);
- m_canvas->setLevel(level);
- }
- });
+ connect(io, &QIODevice::readyRead, [this, io]() {
+ static const qint64 BufferSize = 4096;
+ const qint64 len = qMin(m_audioInput->bytesAvailable(), BufferSize);
+
+ QByteArray buffer(len, 0);
+ qint64 l = io->read(buffer.data(), len);
+ if (l > 0) {
+ const qreal level = m_audioInfo->calculateLevel(buffer.constData(), l);
+ m_canvas->setLevel(level);
+ }
+ });
}
m_pullMode = !m_pullMode;
@@ -256,18 +233,19 @@ void InputTest::toggleSuspend()
void InputTest::deviceChanged(int index)
{
- m_audioInfo->stop();
m_audioInput->stop();
m_audioInput->disconnect(this);
+ m_audioInfo->stop();
initializeAudio(m_deviceBox->itemData(index).value<QAudioDevice>());
}
void InputTest::sliderChanged(int value)
{
- qreal linearVolume = QAudio::convertVolume(value / qreal(100),
- QAudio::LogarithmicVolumeScale,
+ qreal linearVolume = QAudio::convertVolume(value / qreal(100), QAudio::LogarithmicVolumeScale,
QAudio::LinearVolumeScale);
m_audioInput->setVolume(linearVolume);
}
+
+#include "moc_audiosource.cpp"