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

mcallegari / qlcplus / 11388022654

17 Oct 2024 03:21PM UTC coverage: 31.573% (-0.4%) from 31.983%
11388022654

Pull #1422

github

web-flow
Merge 4147c937e into 5f77fc96f
Pull Request #1422: RgbScript make stage colors available to scripts

56 of 908 new or added lines in 11 files covered. (6.17%)

12 existing lines in 8 files now uncovered.

14057 of 44522 relevant lines covered (31.57%)

26666.02 hits per line

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

8.18
/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)
7✔
29
    : RGBAlgorithm(doc)
30
    , m_audioInput(NULL)
31
    , m_bandsNumber(-1)
32
    , m_maxMagnitude(0)
7✔
33
{
34
}
7✔
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()
14✔
46
{
47
    QSharedPointer<AudioCapture> capture(doc()->audioInputCapture());
7✔
48
    if (capture.data() == m_audioInput && m_bandsNumber > 0)
7✔
49
    {
50
        m_audioInput->unregisterBandsNumber(m_bandsNumber);
×
51
    }
52
}
7✔
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

UNCOV
131
void RGBAudio::rgbMap(const QSize& size, uint rgb, int step, RGBMap &map)
×
132
{
133
    Q_UNUSED(step);
134

135
    QMutexLocker locker(&m_mutex);
×
136

137
    QSharedPointer<AudioCapture> capture = doc()->audioInputCapture();
×
138
    if (capture.data() != m_audioInput)
×
139
        setAudioCapture(capture.data());
×
140

141
    map.resize(size.height());
×
142
    for (int y = 0; y < size.height(); y++)
×
143
    {
144
        map[y].resize(size.width());
×
145
        map[y].fill(0);
×
146
    }
147

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

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

182
void RGBAudio::postRun()
×
183
{
184
    QMutexLocker locker(&m_mutex);
×
185

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

198
QString RGBAudio::name() const
6✔
199
{
200
    return QString("Audio Spectrum");
6✔
201
}
202

203
QString RGBAudio::author() const
×
204
{
205
    return QString("Massimo Callegari");
×
206
}
207

208
int RGBAudio::apiVersion() const
×
209
{
210
    return 1;
×
211
}
212

NEW
213
void RGBAudio::setColors(QVector<QColor> colors)
×
214
{
NEW
215
    RGBAlgorithm::setColors(colors);
×
216

217
    // invalidate bars colors so the next time a rendering is
218
    // required, it will be filled with the right values
219
    m_barColors.clear();
×
220
}
×
221

222
RGBAlgorithm::Type RGBAudio::type() const
×
223
{
224
    return RGBAlgorithm::Audio;
×
225
}
226

227
int RGBAudio::acceptColors() const
×
228
{
229
    return 2; // start and end colors accepted
×
230
}
231

232
bool RGBAudio::loadXML(QXmlStreamReader &root)
×
233
{
234
    if (root.name() != KXMLQLCRGBAlgorithm)
×
235
    {
236
        qWarning() << Q_FUNC_INFO << "RGB Algorithm node not found";
×
237
        return false;
×
238
    }
239

240
    if (root.attributes().value(KXMLQLCRGBAlgorithmType).toString() != KXMLQLCRGBAudio)
×
241
    {
242
        qWarning() << Q_FUNC_INFO << "RGB Algorithm is not Audio";
×
243
        return false;
×
244
    }
245

246
    root.skipCurrentElement();
×
247

248
    return true;
×
249
}
250

251
bool RGBAudio::saveXML(QXmlStreamWriter *doc) const
×
252
{
253
    Q_ASSERT(doc != NULL);
254

255
    doc->writeStartElement(KXMLQLCRGBAlgorithm);
×
256
    doc->writeAttribute(KXMLQLCRGBAlgorithmType, KXMLQLCRGBAudio);
×
257
    doc->writeEndElement();
×
258

259
    return true;
×
260
}
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

© 2025 Coveralls, Inc