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

mcallegari / qlcplus / 9226455509

24 May 2024 03:34PM UTC coverage: 31.637% (-0.4%) from 32.007%
9226455509

Pull #1422

github

web-flow
Merge 9870df2c8 into b9e7b67e6
Pull Request #1422: RgbScript make stage colors available to scripts

60 of 910 new or added lines in 11 files covered. (6.59%)

9 existing lines in 4 files now uncovered.

15421 of 48743 relevant lines covered (31.64%)

22674.46 hits per line

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

7.76
/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()
7✔
46
{
47
    QSharedPointer<AudioCapture> capture(doc()->audioInputCapture());
14✔
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);
×
NEW
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::rgbMap(const QSize& size, uint rgb, int step, RGBMap &map, uint (&rawColors)[RGBAlgorithmRawColorCount])
×
127
{
128
    Q_UNUSED(step);
129
    Q_UNUSED(rawColors);
130

131
    QMutexLocker locker(&m_mutex);
×
132

133
    QSharedPointer<AudioCapture> capture = doc()->audioInputCapture();
×
134
    if (capture.data() != m_audioInput)
×
135
        setAudioCapture(capture.data());
×
136

137
    map.resize(size.height());
×
138
    for (int y = 0; y < size.height(); y++)
×
139
    {
140
        map[y].resize(size.width());
×
141
        map[y].fill(0);
×
142
    }
143

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

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

178
void RGBAudio::postRun()
×
179
{
180
    QMutexLocker locker(&m_mutex);
×
181

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

194
QString RGBAudio::name() const
6✔
195
{
196
    return QString("Audio Spectrum");
6✔
197
}
198

199
QString RGBAudio::author() const
×
200
{
201
    return QString("Massimo Callegari");
×
202
}
203

204
int RGBAudio::apiVersion() const
×
205
{
206
    return 1;
×
207
}
208

NEW
209
void RGBAudio::setColors(QColor colors[RGBAlgorithmRawColorCount])
×
210
{
NEW
211
    RGBAlgorithm::setColors(colors);
×
212

213
    // invalidate bars colors so the next time a rendering is
214
    // required, it will be filled with the right values
215
    m_barColors.clear();
×
216
}
×
217

218
RGBAlgorithm::Type RGBAudio::type() const
×
219
{
220
    return RGBAlgorithm::Audio;
×
221
}
222

223
int RGBAudio::acceptColors() const
×
224
{
225
    return 2; // start and end colors accepted
×
226
}
227

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

236
    if (root.attributes().value(KXMLQLCRGBAlgorithmType).toString() != KXMLQLCRGBAudio)
×
237
    {
238
        qWarning() << Q_FUNC_INFO << "RGB Algorithm is not Audio";
×
239
        return false;
×
240
    }
241

242
    root.skipCurrentElement();
×
243

244
    return true;
×
245
}
246

247
bool RGBAudio::saveXML(QXmlStreamWriter *doc) const
×
248
{
249
    Q_ASSERT(doc != NULL);
×
250

251
    doc->writeStartElement(KXMLQLCRGBAlgorithm);
×
252
    doc->writeAttribute(KXMLQLCRGBAlgorithmType, KXMLQLCRGBAudio);
×
253
    doc->writeEndElement();
×
254

255
    return true;
×
256
}
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