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

mcallegari / qlcplus / 42371974533

07 Nov 2026 12:19PM UTC coverage: 36.826% (-0.1%) from 36.925%
42371974533

push

github

mcallegari
Back to 5.4.0 debug

19248 of 52268 relevant lines covered (36.83%)

41917.74 hits per line

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

71.75
/engine/src/showrunner.cpp
1
/*
2
  Q Light Controller
3
  showrunner.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 <QMutex>
21
#include <QDebug>
22

23
#include "showrunner.h"
24
#include "function.h"
25
#include "track.h"
26
#include "show.h"
27
#include "doc.h"
28
#include "inputoutputmap.h"
29

30
#define TIMER_INTERVAL 50
31

32
static bool compareShowFunctions(const ShowFunction *sf1, const ShowFunction *sf2)
8 ✔
33
{
34
    if (sf1->startTime() < sf2->startTime())
8 ✔
35
        return true;
×
36
    return false;
8 ✔
37
}
38

39
ShowRunner::ShowRunner(const Doc* doc, quint32 showID, quint32 startTime)
8 ✔
40
    : QObject(NULL)
41
    , m_doc(doc)
8 ✔
42
    , m_currentTimeFunctionIndex(0)
8 ✔
43
    , m_elapsedTime(startTime)
8 ✔
44
    , m_currentBeatFunctionIndex(0)
8 ✔
45
    , m_elapsedBeats(0)
8 ✔
46
    , beatSynced(false)
8 ✔
47
    , m_syncElapsedTime(0)
8 ✔
48
    , m_syncBeatsTime(0)
8 ✔
49
    , m_totalRunTime(0)
8 ✔
50
    , m_totalRunBeats(0)
8 ✔
51
{
52
    Q_ASSERT(m_doc != NULL);
8 ✔
53
    Q_ASSERT(showID != Show::invalidId());
8 ✔
54

55
    m_show = qobject_cast<Show*>(m_doc->function(showID));
8 ✔
56
    if (m_show == NULL)
8 ✔
57
        return;
×
58

59
    /* startTime (e.g. coming from the cursor position) is always real
60
       milliseconds. If playback doesn't start from 0, m_elapsedBeats needs
61
       the equivalent estimate in "beats as ms" too, otherwise every Play
62
       would always start counting beats from 0 regardless of where
63
       playback actually begins - making beat-based Functions that should
64
       already be running (or long finished) at startTime behave as if
65
       playback had just begun. This is only a starting estimate: once the
66
       first real beat lands (see beatSynced in write()), m_elapsedBeats
67
       keeps advancing in lockstep with the actual beat clock from there */
68
    if (startTime > 0)
8 ✔
69
    {
70
        int bpm = m_doc->inputOutputMap()->bpmNumber();
1 ✔
71
        if (bpm > 0)
1 ✔
72
            m_elapsedBeats = Function::timeToBeats(startTime, 60000 / bpm);
1 ✔
73
    }
74

75
    foreach (Track *track, m_show->tracks())
25 ✔
76
    {
77
        // some sanity checks
78
        if (track == NULL ||
18 ✔
79
            track->id() == Track::invalidId())
9 ✔
80
                continue;
×
81

82
        if (track->isMute())
9 ✔
83
            continue;
×
84

85
        // get all the functions of the track and append them to the runner queue
86
        foreach (ShowFunction *sfunc, track->showFunctions())
31 ✔
87
        {
88
            Function *f = m_doc->function(sfunc->functionID());
13 ✔
89
            if (f == NULL)
13 ✔
90
                continue;
×
91

92
            // beat-based items are placed in "beats as ms", so they must be
93
            // compared with the beat position, not with real milliseconds
94
            quint32 startPosition = f->tempoType() == Function::Time ? startTime : m_elapsedBeats;
13 ✔
95
            if (sfunc->startTime() + sfunc->duration(m_doc) <= startPosition)
13 ✔
96
                continue;
1 ✔
97

98
            if (f->tempoType() == Function::Time)
12 ✔
99
            {
100
                m_timeFunctions.append(sfunc);
11 ✔
101
                if (sfunc->startTime() + sfunc->duration(m_doc) > m_totalRunTime)
11 ✔
102
                    m_totalRunTime = sfunc->startTime() + sfunc->duration(m_doc);
11 ✔
103
            }
104
            else
105
            {
106
                m_beatFunctions.append(sfunc);
1 ✔
107
                if (sfunc->startTime() + sfunc->duration(m_doc) > m_totalRunBeats)
1 ✔
108
                    m_totalRunBeats = sfunc->startTime() + sfunc->duration(m_doc);
1 ✔
109
            }
110
        }
9 ✔
111

112
        // Initialize the intensity map
113
        m_intensityMap[track->id()] = 1.0;
9 ✔
114
    }
8 ✔
115

116
    std::sort(m_timeFunctions.begin(), m_timeFunctions.end(), compareShowFunctions);
8 ✔
117
    std::sort(m_beatFunctions.begin(), m_beatFunctions.end(), compareShowFunctions);
8 ✔
118

119
#if 1
120
    qDebug() << "Ordered list of ShowFunctions (time):";
8 ✔
121
    foreach (ShowFunction *sfunc, m_timeFunctions)
19 ✔
122
        qDebug() << "[Show] Function ID:" << sfunc->functionID() << "start time:" << sfunc->startTime() << "duration:" << sfunc->duration(m_doc);
19 ✔
123

124
    qDebug() << "Ordered list of ShowFunctions (beats):";
8 ✔
125
    foreach (ShowFunction *sfunc, m_beatFunctions)
9 ✔
126
        qDebug() << "[Show] Function ID:" << sfunc->functionID() << "start time:" << sfunc->startTime() << "duration:" << sfunc->duration(m_doc);
9 ✔
127
#endif
128
    m_runningQueue.clear();
8 ✔
129

130
    qDebug() << "ShowRunner created";
8 ✔
131
}
×
132

133
ShowRunner::~ShowRunner()
12 ✔
134
{
135
}
12 ✔
136

137
void ShowRunner::start()
4 ✔
138
{
139
    qDebug() << "ShowRunner started";
4 ✔
140
}
4 ✔
141

142
void ShowRunner::setPause(bool enable)
×
143
{
144
    for (int i = 0; i < m_runningQueue.count(); i++)
×
145
    {
146
        Function *f = m_runningQueue.at(i).first;
×
147
        f->setPause(enable);
×
148
    }
149
}
×
150

151
void ShowRunner::stop()
5 ✔
152
{
153
    m_elapsedTime = 0;
5 ✔
154
    m_elapsedBeats = 0;
5 ✔
155
    m_currentTimeFunctionIndex = 0;
5 ✔
156
    m_currentBeatFunctionIndex = 0;
5 ✔
157

158
    for (int i = 0; i < m_runningQueue.count(); i++)
10 ✔
159
    {
160
        Function *f = m_runningQueue.at(i).first;
5 ✔
161
        f->stop(functionParent());
5 ✔
162
    }
163

164
    m_runningQueue.clear();
5 ✔
165
    qDebug() << "ShowRunner stopped";
5 ✔
166
}
5 ✔
167

168
FunctionParent ShowRunner::functionParent() const
15 ✔
169
{
170
    return FunctionParent(FunctionParent::Function, m_show->id());
15 ✔
171
}
172

173
bool ShowRunner::isQueued(Function *function) const
12 ✔
174
{
175
    for (int i = 0; i < m_runningQueue.count(); i++)
12 ✔
176
    {
177
        if (m_runningQueue.at(i).first == function)
2 ✔
178
            return true;
2 ✔
179
    }
180

181
    return false;
10 ✔
182
}
183

184
void ShowRunner::startShowFunction(ShowFunction *sf, Function *f, quint32 startTime)
8 ✔
185
{
186
    // The same Function can be used by more than one item, e.g. on
187
    // overlapping items on different tracks. A Function can run only once,
188
    // so if it is already running for another item it just keeps running,
189
    // and it will be stopped when the last of its items ends
190
    if (isQueued(f) == false)
8 ✔
191
    {
192
        foreach (Track *track, m_show->tracks())
14 ✔
193
        {
194
            if (track->showFunctions().contains(sf))
7 ✔
195
            {
196
                int intOverrideId = f->requestAttributeOverride(Function::Intensity, m_intensityMap[track->id()]);
7 ✔
197
                //f->adjustAttribute(m_intensityMap[track->id()], Function::Intensity);
198
                sf->setIntensityOverrideId(intOverrideId);
7 ✔
199
                break;
7 ✔
200
            }
201
        }
7 ✔
202

203
        f->start(m_doc->masterTimer(), functionParent(), startTime);
7 ✔
204
    }
205

206
    m_runningQueue.append(QPair<Function *, quint32>(f, sf->startTime() + sf->duration(m_doc)));
8 ✔
207
}
8 ✔
208

209
void ShowRunner::write(MasterTimer *timer)
72 ✔
210
{
211
    //qDebug() << Q_FUNC_INFO << "elapsed:" << m_elapsedTime << ", total:" << m_totalRunTime;
212

213
    // A Show can freely mix time-based and beat-based Functions on its
214
    // tracks (e.g. a beat-synced Chaser next to a Time-based Audio track),
215
    // regardless of the Show's own timeline display type. So beat tracking
216
    // must not depend on, nor gate, anything based on the Show's own
217
    // division: it only needs to run when this Show actually has beat-based
218
    // Functions to drive, and it must never block m_timeFunctions/m_elapsedTime
219
    // from progressing while waiting for the first beat to land.
220
    if (m_beatFunctions.isEmpty() == false && timer->isBeat())
72 ✔
221
    {
222
        if (beatSynced == false)
×
223
        {
224
            beatSynced = true;
×
225
            m_syncElapsedTime = m_elapsedTime;
×
226
            int syncBpm = timer->bpmNumber();
×
227
            m_syncBeatsTime = syncBpm > 0 ? Function::beatsToTime(m_elapsedBeats, 60000 / syncBpm) : 0;
×
228
            qDebug() << "Beat synced";
×
229
        }
230
        else
231
        {
232
            m_elapsedBeats += 1000;
×
233
        }
234
    }
235

236
    // Phase 1. Check if we need to stop some running Functions.
237
    // This is done before starting new ones, so that when an item ends
238
    // exactly where another item of the same Function begins, the Function
239
    // is stopped and started again (MasterTimer restarts it cleanly),
240
    // instead of the start being ignored and the stop winning.
241
    // It is done in reverse order for two reasons:
242
    // 1- m_runningQueue is not ordered by stop time
243
    // 2- to avoid messing up with indices when an entry is removed
244
    for (int i = m_runningQueue.count() - 1; i >= 0; i--)
145 ✔
245
    {
246
        Function *func = m_runningQueue.at(i).first;
73 ✔
247
        quint32 stopTime = m_runningQueue.at(i).second;
73 ✔
248
        quint32 currTime = func->tempoType() == Function::Time ? m_elapsedTime : m_elapsedBeats;
73 ✔
249

250
        // if we passed the function stop time
251
        if (currTime >= stopTime)
73 ✔
252
        {
253
            // remove it from the running queue
254
            m_runningQueue.removeAt(i);
4 ✔
255
            // and stop it, unless another item still needs it running
256
            if (isQueued(func) == false)
4 ✔
257
                func->stop(functionParent());
3 ✔
258
        }
259
    }
260

261
    // Phase 2. Check all the Functions that need to be started
262
    // m_timeFunctions is ordered by startup time, so when we found an entry
263
    // with start time greater than m_elapsed, this phase is over
264
    bool startFunctionsDone = false;
72 ✔
265

266
    // check if there are time-based functions to start
267
    while (startFunctionsDone == false)
115 ✔
268
    {
269
        if (m_currentTimeFunctionIndex == m_timeFunctions.count())
80 ✔
270
            break;
37 ✔
271

272
        ShowFunction *sf = m_timeFunctions.at(m_currentTimeFunctionIndex);
43 ✔
273
        quint32 funcStartTime = sf->startTime();
43 ✔
274
        quint32 functionTimeOffset = 0;
43 ✔
275
        Function *f = m_doc->function(sf->functionID());
43 ✔
276
        if (f == nullptr)
43 ✔
277
        {
278
            m_currentTimeFunctionIndex++;
×
279
            continue;
×
280
        }
281

282
        // this should happen only when a Show is not started from 0
283
        if (m_elapsedTime > funcStartTime)
43 ✔
284
        {
285
            functionTimeOffset = m_elapsedTime - funcStartTime;
×
286
            funcStartTime = m_elapsedTime;
×
287
        }
288
        if (m_elapsedTime >= funcStartTime)
43 ✔
289
        {
290
            startShowFunction(sf, f, functionTimeOffset);
8 ✔
291
            m_currentTimeFunctionIndex++;
8 ✔
292
        }
293
        else
294
            startFunctionsDone = true;
35 ✔
295
    }
296

297
    startFunctionsDone = false;
72 ✔
298

299
    // check if there are beat-based functions to start
300
    // (wait for the first real beat to land before considering any of
301
    // them, so m_elapsedBeats == 0 is not mistaken for "beat zero happened")
302
    while (startFunctionsDone == false && beatSynced)
72 ✔
303
    {
304
        if (m_currentBeatFunctionIndex == m_beatFunctions.count())
×
305
            break;
×
306

307
        ShowFunction *sf = m_beatFunctions.at(m_currentBeatFunctionIndex);
×
308
        quint32 funcStartTime = sf->startTime();
×
309
        quint32 functionTimeOffset = 0;
×
310
        Function *f = m_doc->function(sf->functionID());
×
311
        if (f == nullptr)
×
312
        {
313
            m_currentBeatFunctionIndex++;
×
314
            continue;
×
315
        }
316

317
        // this should happen only when a Show is not started from 0
318
        if (m_elapsedBeats > funcStartTime)
×
319
        {
320
            functionTimeOffset = m_elapsedBeats - funcStartTime;
×
321
            funcStartTime = m_elapsedBeats;
×
322
        }
323
        if (m_elapsedBeats >= funcStartTime)
×
324
        {
325
            startShowFunction(sf, f, functionTimeOffset);
×
326
            m_currentBeatFunctionIndex++;
×
327
        }
328
        else
329
            startFunctionsDone = true;
×
330
    }
331

332
    // Phase 3. Check if this is the end of the Show. A Show can mix
333
    // time-based and beat-based tracks, so it is only really over once
334
    // both the time-based and the beat-based timelines have completed.
335
    // While there are beat-based Functions but no beat has been detected
336
    // yet, the beat timeline hasn't even started, so it can't be "done".
337
    bool timeDone = m_elapsedTime >= m_totalRunTime;
72 ✔
338
    bool beatsDone = m_beatFunctions.isEmpty() ||
72 ✔
339
                      (beatSynced && m_elapsedBeats >= m_totalRunBeats);
×
340

341
    if (timeDone && beatsDone)
72 ✔
342
    {
343
        if (m_show != NULL)
×
344
            m_show->stop(functionParent());
×
345
        emit showFinished();
×
346
        return;
×
347
    }
348

349
    m_elapsedTime += MasterTimer::tick();
72 ✔
350

351
    // Report plain elapsed milliseconds: it advances smoothly on every
352
    // tick, and the UI scales it to a beat/bar position against the
353
    // current BPM (see ShowManager and TimeUtils.timeToBeatPosition()) so
354
    // it reacts immediately to live BPM changes.
355
    //
356
    // However, when the Show's own timeline is BPM based, m_elapsedTime is
357
    // real wall-clock time since the Show started, which includes however
358
    // long it took to wait for the first beat to sync (beat 0 is defined
359
    // by that sync moment, not by when the Show started) - reporting it
360
    // directly would make the cursor jump to an arbitrary, not
361
    // beat-aligned position the instant sync happens. Instead, report the
362
    // beat-zeroed resume position (m_syncBeatsTime) plus how much real
363
    // time has passed since the sync moment: this still advances smoothly
364
    // every tick (unlike reporting m_elapsedBeats directly, which only
365
    // moves in whole-beat jumps), while staying exactly beat-aligned at
366
    // the sync instant itself.
367
    if (m_show->timeDivisionType() == Show::Time)
72 ✔
368
    {
369
        emit timeChanged(m_elapsedTime);
72 ✔
370
    }
371
    else if (beatSynced)
×
372
    {
373
        emit timeChanged(m_syncBeatsTime + (m_elapsedTime - m_syncElapsedTime));
×
374
    }
375
}
376

377
/************************************************************************
378
 * Intensity
379
 ************************************************************************/
380

381
void ShowRunner::adjustIntensity(qreal fraction, const Track *track)
6 ✔
382
{
383
    if (track == NULL)
6 ✔
384
        return;
×
385

386
    qDebug() << Q_FUNC_INFO << "Track ID: " << track->id() << ", val:" << fraction;
6 ✔
387
    m_intensityMap[track->id()] = fraction;
6 ✔
388

389
    foreach (ShowFunction *sf, track->showFunctions())
21 ✔
390
    {
391
        Function *f = m_doc->function(sf->functionID());
9 ✔
392
        if (f == NULL)
9 ✔
393
            continue;
×
394

395
        for (int i = 0; i < m_runningQueue.count(); i++)
9 ✔
396
        {
397
            Function *rf = m_runningQueue.at(i).first;
×
398
            if (f == rf)
×
399
                f->adjustAttribute(fraction, sf->intensityOverrideId());
×
400
        }
401
    }
6 ✔
402
}
403

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc