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 <QtCore/QDebug>
32
33using namespace QtDataVisualization;
34
35//! [1]
36static const int resolution = 8;
37static const int rowSize = 800;
38static const int rowCount = 7; // Must be odd number
39static const int middleRow = rowCount / 2;
40//! [1]
41
42AudioLevelsIODevice::AudioLevelsIODevice(QBarDataProxy *proxy, QObject *parent)
43 : QIODevice(parent),
44 m_proxy(proxy),
45 m_array(new QBarDataArray)
46{
47 // We reuse the existing array for maximum performance, so construct the array here
48 //! [0]
49 m_array->reserve(rowCount);
50 for (int i = 0; i < rowCount; i++)
51 m_array->append(new QBarDataRow(rowSize));
52 //! [0]
53
54 qDebug() << "Total of" << (rowSize * rowCount) << "items in the array.";
55}
56
57// Implementation required for this pure virtual function
58qint64 AudioLevelsIODevice::readData(char *data, qint64 maxSize)
59{
60 Q_UNUSED(data)
61 Q_UNUSED(maxSize)
62 return -1;
63}
64
65//! [2]
66qint64 AudioLevelsIODevice::writeData(const char *data, qint64 maxSize)
67{
68 // The amount of new data available.
69 int newDataSize = maxSize / resolution;
70
71 // If we get more data than array size, we need to adjust the start index for new data.
72 int newDataStartIndex = qMax(0, (newDataSize - rowSize));
73
74 // Move the old data ahead in the rows (only do first half of rows + middle one now).
75 // If the amount of new data was larger than row size, skip copying.
76 if (!newDataStartIndex) {
77 for (int i = 0; i <= middleRow; i++) {
78 QBarDataItem *srcPos = m_array->at(i)->data();
79 QBarDataItem *dstPos = srcPos + newDataSize;
80 memmove((void *)dstPos, (void *)srcPos, (rowSize - newDataSize) * sizeof(QBarDataItem));
81 }
82 }
83
84 // Insert data in reverse order, so that newest data is always at the front of the row.
85 int index = 0;
86 for (int i = newDataSize - 1; i >= newDataStartIndex; i--) {
87 // Add 0.01 to the value to avoid gaps in the graph (i.e. zero height bars).
88 // Also, scale to 0...100
89 float value = float(quint8(data[resolution * i]) - 128) / 1.28f + 0.01f;
90 (*m_array->at(middleRow))[index].setValue(value);
91 // Insert a fractional value into front half of the rows.
92 for (int j = 1; j <= middleRow; j++) {
93 float fractionalValue = value / float(j + 1);
94 (*m_array->at(middleRow - j))[index].setValue(fractionalValue);
95 }
96 index++;
97 }
98
99 // Copy the front half of rows to the back half for symmetry.
100 index = 0;
101 for (int i = rowCount - 1; i > middleRow; i--) {
102 QBarDataItem *srcPos = m_array->at(index++)->data();
103 QBarDataItem *dstPos = m_array->at(i)->data();
104 memcpy((void *)dstPos, (void *)srcPos, rowSize * sizeof(QBarDataItem));
105 }
106
107 // Reset the proxy array now that data has been updated to trigger a redraw.
108 m_proxy->resetArray(m_array);
109
110 return maxSize;
111}
112//! [2]
113
114
115