1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Data Visualization module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include "audiolevelsiodevice.h" |
31 | #include "audiolevels.h" |
32 | |
33 | #include <QtDataVisualization/qbardataproxy.h> |
34 | #include <QtDataVisualization/qvalue3daxis.h> |
35 | #include <QtDataVisualization/q3dscene.h> |
36 | #include <QtDataVisualization/q3dcamera.h> |
37 | #include <QtDataVisualization/qbar3dseries.h> |
38 | #include <QtDataVisualization/q3dtheme.h> |
39 | #include <QtDataVisualization/qcustom3dlabel.h> |
40 | |
41 | #include <QtMultimedia/QAudioDeviceInfo> |
42 | #include <QtMultimedia/QAudioInput> |
43 | |
44 | using namespace QtDataVisualization; |
45 | |
46 | AudioLevels::AudioLevels(Q3DBars *graph, QObject *parent) |
47 | : QObject(parent), |
48 | m_graph(graph), |
49 | m_device(0), |
50 | m_audioInput(0) |
51 | { |
52 | // Set up the graph |
53 | m_graph->setBarThickness(0.5f); |
54 | m_graph->setBarSpacing(QSizeF(0.0, 1.0)); |
55 | m_graph->valueAxis()->setRange(min: -100.0f, max: 100.0f); |
56 | m_graph->valueAxis()->setSegmentCount(20); |
57 | m_graph->valueAxis()->setLabelFormat(QStringLiteral("%d%%" )); |
58 | m_graph->setShadowQuality(QAbstract3DGraph::ShadowQualityNone); |
59 | m_graph->setSelectionMode(QAbstract3DGraph::SelectionNone); |
60 | m_graph->scene()->activeCamera()->setCameraPosition(horizontal: -25.0f, vertical: 10.0f, zoom: 190.0f); |
61 | m_graph->activeTheme()->setType(Q3DTheme::ThemeIsabelle); |
62 | m_graph->activeTheme()->setGridEnabled(true); |
63 | m_graph->activeTheme()->setBackgroundEnabled(false); |
64 | QFont font = m_graph->activeTheme()->font(); |
65 | font.setPointSize(10); |
66 | m_graph->activeTheme()->setFont(font); |
67 | QBar3DSeries *series = new QBar3DSeries; |
68 | series->setMesh(QAbstract3DSeries::MeshBar); |
69 | m_graph->addSeries(series); |
70 | |
71 | //! [0] |
72 | QAudioDeviceInfo inputDevice = QAudioDeviceInfo::defaultInputDevice(); |
73 | |
74 | if (inputDevice.supportedSampleRates().size() > 0 |
75 | && inputDevice.supportedChannelCounts().size() > 0 |
76 | && inputDevice.supportedSampleSizes().size() > 0 |
77 | && inputDevice.supportedCodecs().size() > 0) { |
78 | QAudioFormat formatAudio; |
79 | formatAudio.setSampleRate(inputDevice.supportedSampleRates().at(i: 0)); |
80 | formatAudio.setChannelCount(inputDevice.supportedChannelCounts().at(i: 0)); |
81 | formatAudio.setSampleSize(inputDevice.supportedSampleSizes().at(i: 0)); |
82 | formatAudio.setCodec(inputDevice.supportedCodecs().at(i: 0)); |
83 | formatAudio.setByteOrder(QAudioFormat::LittleEndian); |
84 | formatAudio.setSampleType(QAudioFormat::UnSignedInt); |
85 | |
86 | m_audioInput = new QAudioInput(inputDevice, formatAudio, this); |
87 | #ifdef Q_OS_MAC |
88 | // OS X seems to wait for entire buffer to fill before calling writeData, so use smaller buffer |
89 | m_audioInput->setBufferSize(256); |
90 | #else |
91 | m_audioInput->setBufferSize(1024); |
92 | #endif |
93 | |
94 | m_device = new AudioLevelsIODevice(m_graph->seriesList().at(i: 0)->dataProxy(), this); |
95 | m_device->open(mode: QIODevice::WriteOnly); |
96 | |
97 | m_audioInput->start(device: m_device); |
98 | } else { |
99 | // No graph content can be shown, so add a custom warning label |
100 | QCustom3DLabel *titleLabel = new QCustom3DLabel("No valid audio input device found" , |
101 | QFont(), |
102 | QVector3D(0.2f, 0.2f, 0.0f), |
103 | QVector3D(1.0f, 1.0f, 0.0f), |
104 | QQuaternion()); |
105 | titleLabel->setPositionAbsolute(true); |
106 | titleLabel->setFacingCamera(true); |
107 | m_graph->addCustomItem(item: titleLabel); |
108 | } |
109 | //! [0] |
110 | } |
111 | |
112 | AudioLevels::~AudioLevels() |
113 | { |
114 | if (m_audioInput) |
115 | m_audioInput->stop(); |
116 | if (m_device) |
117 | m_device->close(); |
118 | } |
119 | |