• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mcallegari / qlcplus / 12547752943

30 Dec 2024 02:17PM UTC coverage: 31.592% (-0.4%) from 31.949%
12547752943

push

github

web-flow
Merge pull request #1653 from mcallegari/stagecolors

RGB Matrix: allow to get/set multiple colors programmatically

72 of 893 new or added lines in 11 files covered. (8.06%)

12 existing lines in 7 files now uncovered.

14085 of 44584 relevant lines covered (31.59%)

26912.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

8.04
/engine/src/rgbaudio.cpp
1
/*
2
  Q Light Controller Plus
3
  rgbaudio.cpp
4

5
  Copyright (c) Massimo Callegari
6

7
  Licensed under the Apache License, Version 2.0 (the "License");
8
  you may not use this file except in compliance with the License.
9
  You may obtain a copy of the License at
10

11
      http://www.apache.org/licenses/LICENSE-2.0.txt
12

13
  Unless required by applicable law or agreed to in writing, software
14
  distributed under the License is distributed on an "AS IS" BASIS,
15
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
  See the License for the specific language governing permissions and
17
  limitations under the License.
18
*/
19

20
#include <QXmlStreamReader>
21
#include <QXmlStreamWriter>
22
#include <QDebug>
23

24
#include "rgbaudio.h"
25
#include "audiocapture.h"
26
#include "doc.h"
27

28
RGBAudio::RGBAudio(Doc * doc)
8✔
29
    : RGBAlgorithm(doc)
30
    , m_audioInput(NULL)
31
    , m_bandsNumber(-1)
32
    , m_maxMagnitude(0)
8✔
33
{
34
}
8✔
35

36
RGBAudio::RGBAudio(const RGBAudio& a, QObject *parent)
×
37
    : QObject(parent)
38
    , RGBAlgorithm(a.doc())
39
    , m_audioInput(NULL)
40
    , m_bandsNumber(-1)
41
    , m_maxMagnitude(0)
×
42
{
43
}
×
44

45
RGBAudio::~RGBAudio()
16✔
46
{
47
    QSharedPointer<AudioCapture> capture(doc()->audioInputCapture());
8✔
48
    if (capture.data() == m_audioInput && m_bandsNumber > 0)
8✔
49
    {
50
        m_audioInput->unregisterBandsNumber(m_bandsNumber);
×
51
    }
52
}
8✔
53

54
RGBAlgorithm* RGBAudio::clone() const
×
55
{
56
    RGBAudio* audio = new RGBAudio(*this);
×
57
    return static_cast<RGBAlgorithm*> (audio);
×
58
}
59

60
void RGBAudio::setAudioCapture(AudioCapture* cap)
×
61
{
62
    qDebug() << Q_FUNC_INFO << "Audio capture set";
63

64
    m_audioInput = cap;
×
65
    connect(m_audioInput, SIGNAL(dataProcessed(double*,int,double,quint32)),
×
66
            this, SLOT(slotAudioBarsChanged(double*,int,double,quint32)));
67
    m_bandsNumber = -1;
×
68
}
×
69

70
void RGBAudio::slotAudioBarsChanged(double *spectrumBands, int size,
×
71
                                    double maxMagnitude, quint32 power)
72
{
73
    if (size != m_bandsNumber)
×
74
        return;
75

76
    QMutexLocker locker(&m_mutex);
×
77

78
    m_spectrumValues.clear();
×
79
    for (int i = 0; i < m_bandsNumber; i++)
×
80
        m_spectrumValues.append(spectrumBands[i]);
×
81
    m_maxMagnitude = maxMagnitude;
×
82
    m_volumePower = power;
×
83
}
84

85
void RGBAudio::calculateColors(int barsHeight)
×
86
{
87
    if (barsHeight > 0)
×
88
    {
NEW
89
        QColor startColor = getColor(0);
×
NEW
90
        QColor endColor = getColor(1);
×
91
        m_barColors.clear();
×
NEW
92
        if (endColor == QColor()
×
93
            || barsHeight == 1) // to avoid division by 0 below
×
94
        {
95
            for (int i = 0; i < barsHeight; i++)
×
NEW
96
                m_barColors.append(startColor.rgb());
×
97
        }
98
        else
99
        {
NEW
100
            int crDelta = (endColor.red() - startColor.red()) / (barsHeight - 1);
×
NEW
101
            int cgDelta = (endColor.green() - startColor.green()) / (barsHeight - 1);
×
NEW
102
            int cbDelta = (endColor.blue() - startColor.blue()) / (barsHeight - 1);
×
103
            QColor pixelColor = startColor;
104

105
            for (int i = 0; i < barsHeight; i++)
×
106
            {
107
                m_barColors.append(pixelColor.rgb());
×
108
                pixelColor = QColor(pixelColor.red() + crDelta,
×
109
                                    pixelColor.green() + cgDelta,
×
110
                                    pixelColor.blue() + cbDelta);
×
111
            }
112
        }
113
    }
114
}
×
115

116
/****************************************************************************
117
 * RGBAlgorithm
118
 ****************************************************************************/
119

120
int RGBAudio::rgbMapStepCount(const QSize& size)
×
121
{
122
    Q_UNUSED(size);
123
    return 1;
×
124
}
125

NEW
126
void RGBAudio::rgbMapSetColors(QVector<uint> &colors)
×
127
{
128
    Q_UNUSED(colors);
NEW
129
}
×
130

NEW
131
QVector<uint> RGBAudio::rgbMapGetColors()
×
132
{
NEW
133
    return QVector<uint>();
×
134
}
135

UNCOV
136
void RGBAudio::rgbMap(const QSize& size, uint rgb, int step, RGBMap &map)
×
137
{
138
    Q_UNUSED(step);
139

140
    QMutexLocker locker(&m_mutex);
×
141

142
    QSharedPointer<AudioCapture> capture = doc()->audioInputCapture();
×
143
    if (capture.data() != m_audioInput)
×
144
        setAudioCapture(capture.data());
×
145

146
    map.resize(size.height());
×
147
    for (int y = 0; y < size.height(); y++)
×
148
    {
149
        map[y].resize(size.width());
×
150
        map[y].fill(0);
×
151
    }
152

153
    // on the first round, just set the proper number of
154
    // spectrum bands to receive
155
    if (m_bandsNumber == -1)
×
156
    {
157
        m_bandsNumber = size.width();
×
158
        qDebug() << "[RGBAudio] set" << m_bandsNumber << "bars";
159
        m_audioInput->registerBandsNumber(m_bandsNumber);
×
160
        return;
161
    }
162
    if (m_barColors.count() == 0)
×
163
        calculateColors(size.height());
×
164

165
    double volHeight = (m_volumePower * size.height()) / 0x7FFF;
×
166
    for (int x = 0; x < m_spectrumValues.count(); x++)
×
167
    {
168
        int barHeight;
169
        if (m_maxMagnitude == 0)
×
170
            barHeight = 0;
171
        else
172
        {
173
            barHeight = (volHeight * m_spectrumValues[x]) / m_maxMagnitude;
×
174
            if (barHeight > size.height())
×
175
                barHeight = size.height();
176
        }
177
        for (int y = size.height() - barHeight; y < size.height(); y++)
×
178
        {
179
            if (m_barColors.count() == 0)
×
180
                map[y][x] = rgb;
×
181
            else
182
                map[y][x] = m_barColors.at(y);
×
183
        }
184
    }
185
}
186

187
void RGBAudio::postRun()
×
188
{
189
    QMutexLocker locker(&m_mutex);
×
190

191
    QSharedPointer<AudioCapture> capture = doc()->audioInputCapture();
×
192
    if (capture.data() == m_audioInput)
×
193
    {
194
        disconnect(m_audioInput, SIGNAL(dataProcessed(double*,int,double,quint32)),
×
195
                   this, SLOT(slotAudioBarsChanged(double*,int,double,quint32)));
196
        if (m_bandsNumber > 0)
×
197
            m_audioInput->unregisterBandsNumber(m_bandsNumber);
×
198
    }
199
    m_audioInput = NULL;
×
200
    m_bandsNumber = -1;
×
201
}
×
202

203
QString RGBAudio::name() const
7✔
204
{
205
    return QString("Audio Spectrum");
7✔
206
}
207

208
QString RGBAudio::author() const
×
209
{
210
    return QString("Massimo Callegari");
×
211
}
212

213
int RGBAudio::apiVersion() const
×
214
{
215
    return 1;
×
216
}
217

NEW
218
void RGBAudio::setColors(QVector<QColor> colors)
×
219
{
NEW
220
    RGBAlgorithm::setColors(colors);
×
221

222
    // invalidate bars colors so the next time a rendering is
223
    // required, it will be filled with the right values
224
    m_barColors.clear();
×
225
}
×
226

227
RGBAlgorithm::Type RGBAudio::type() const
×
228
{
229
    return RGBAlgorithm::Audio;
×
230
}
231

232
int RGBAudio::acceptColors() const
×
233
{
234
    return 2; // start and end colors accepted
×
235
}
236

237
bool RGBAudio::loadXML(QXmlStreamReader &root)
×
238
{
239
    if (root.name() != KXMLQLCRGBAlgorithm)
×
240
    {
241
        qWarning() << Q_FUNC_INFO << "RGB Algorithm node not found";
×
242
        return false;
×
243
    }
244

245
    if (root.attributes().value(KXMLQLCRGBAlgorithmType).toString() != KXMLQLCRGBAudio)
×
246
    {
247
        qWarning() << Q_FUNC_INFO << "RGB Algorithm is not Audio";
×
248
        return false;
×
249
    }
250

251
    root.skipCurrentElement();
×
252

253
    return true;
×
254
}
255

256
bool RGBAudio::saveXML(QXmlStreamWriter *doc) const
×
257
{
258
    Q_ASSERT(doc != NULL);
259

260
    doc->writeStartElement(KXMLQLCRGBAlgorithm);
×
261
    doc->writeAttribute(KXMLQLCRGBAlgorithmType, KXMLQLCRGBAudio);
×
262
    doc->writeEndElement();
×
263

264
    return true;
×
265
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc