audiolevelsiodevice.cpp Example File
audiolevels/audiolevelsiodevice.cpp
#include "audiolevelsiodevice.h"
#include <QtCore/QDebug>
using namespace QtDataVisualization;
static const int resolution = 8;
static const int rowSize = 800;
static const int rowCount = 7;
static const int middleRow = rowCount / 2;
AudioLevelsIODevice::AudioLevelsIODevice(QBarDataProxy *proxy, QObject *parent)
: QIODevice(parent),
m_proxy(proxy),
m_array(new QBarDataArray)
{
m_array->reserve(rowCount);
for (int i = 0; i < rowCount; i++)
m_array->append(new QBarDataRow(rowSize));
qDebug() << "Total of" << (rowSize * rowCount) << "items in the array.";
}
qint64 AudioLevelsIODevice::readData(char *data, qint64 maxSize)
{
Q_UNUSED(data)
Q_UNUSED(maxSize)
return -1;
}
qint64 AudioLevelsIODevice::writeData(const char *data, qint64 maxSize)
{
int newDataSize = maxSize / resolution;
int newDataStartIndex = qMax(0, (newDataSize - rowSize));
if (!newDataStartIndex) {
for (int i = 0; i <= middleRow; i++) {
QBarDataItem *srcPos = m_array->at(i)->data();
QBarDataItem *dstPos = srcPos + newDataSize;
memmove((void *)dstPos, (void *)srcPos, (rowSize - newDataSize) * sizeof(QBarDataItem));
}
}
int index = 0;
for (int i = newDataSize - 1; i >= newDataStartIndex; i--) {
float value = float(quint8(data[resolution * i]) - 128) / 1.28f + 0.01f;
(*m_array->at(middleRow))[index].setValue(value);
for (int j = 1; j <= middleRow; j++) {
float fractionalValue = value / float(j + 1);
(*m_array->at(middleRow - j))[index].setValue(fractionalValue);
}
index++;
}
index = 0;
for (int i = rowCount - 1; i > middleRow; i--) {
QBarDataItem *srcPos = m_array->at(index++)->data();
QBarDataItem *dstPos = m_array->at(i)->data();
memcpy((void *)dstPos, (void *)srcPos, rowSize * sizeof(QBarDataItem));
}
m_proxy->resetArray(m_array);
return maxSize;
}