• 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

85.42
/engine/src/chaserrunner.cpp
1
/*
2
  Q Light Controller Plus
3
  chaserrunner.cpp
4

5
  Copyright (c) Heikki Junnila
6
                Massimo Callegari
7
                Jano Svitok
8

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

13
      http://www.apache.org/licenses/LICENSE-2.0.txt
14

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

22
#include <QElapsedTimer>
23
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
24
#include <QRandomGenerator>
25
#endif
26
#include <QDebug>
27

28
#include "chaserrunner.h"
29
#include "mastertimer.h"
30
#include "chaserstep.h"
31
#include "qlcmacros.h"
32
#include "chaser.h"
33
#include "scene.h"
34
#include "doc.h"
35

36
ChaserRunner::ChaserRunner(const Doc *doc, const Chaser *chaser, quint32 startTime)
146 ✔
37
    : QObject(NULL)
38
    , m_doc(doc)
146 ✔
39
    , m_chaser(chaser)
146 ✔
40
    , m_updateOverrideSpeeds(false)
146 ✔
41
    , m_orderRefreshNeeded(false)
146 ✔
42
    , m_startOffset(0)
146 ✔
43
    , m_lastRunStepIdx(-1)
146 ✔
44
    , m_lastFunctionID(Function::invalidId())
146 ✔
45
    , m_roundTime(new QElapsedTimer())
146 ✔
46
    , m_order()
146 ✔
47
    , m_beatClockStarted(false)
146 ✔
48
    , m_firstStepStarted(false)
146 ✔
49
    , m_lastBeatPosition(0)
146 ✔
50
    , m_beatPosition(0)
146 ✔
51
    , m_msSinceBeat(0)
146 ✔
52
{
53
    Q_ASSERT(chaser != NULL);
146 ✔
54

55
    m_pendingAction.m_action = ChaserNoAction;
146 ✔
56
    m_pendingAction.m_masterIntensity = 1.0;
146 ✔
57
    m_pendingAction.m_stepIntensity = 1.0;
146 ✔
58
    m_pendingAction.m_fadeMode = Chaser::FromFunction;
146 ✔
59
    m_pendingAction.m_stepIndex = -1;
146 ✔
60

61
    if (startTime > 0)
146 ✔
62
    {
63
        qDebug() << "[ChaserRunner] startTime:" << startTime;
2 ✔
64
        int idx = 0;
2 ✔
65
        quint32 stepsTime = 0;
2 ✔
66
        foreach (ChaserStep step, chaser->steps())
4 ✔
67
        {
68
            uint duration = m_chaser->durationMode() == Chaser::Common ? m_chaser->duration() : step.duration;
2 ✔
69

70
            if (startTime < stepsTime + duration)
2 ✔
71
            {
72
                m_pendingAction.m_action = ChaserSetStepIndex;
2 ✔
73
                m_pendingAction.m_stepIndex = idx;
2 ✔
74
                m_startOffset = startTime - stepsTime;
2 ✔
75
                qDebug() << "[ChaserRunner] Starting from step:" << idx;
2 ✔
76
                break;
2 ✔
77
            }
78
            idx++;
×
79
            stepsTime += duration;
×
80
        }
4 ✔
81
    }
82

83
    m_direction = m_chaser->direction();
146 ✔
84
    connect(chaser, SIGNAL(changed(quint32)), this, SLOT(slotChaserChanged()));
146 ✔
85
    m_roundTime->restart();
146 ✔
86

87
    fillOrder();
146 ✔
88
}
146 ✔
89

90
ChaserRunner::~ChaserRunner()
159 ✔
91
{
92
    clearRunningList();
146 ✔
93
    delete m_roundTime;
146 ✔
94
}
159 ✔
95

96
/****************************************************************************
97
 * Speed
98
 ****************************************************************************/
99

100
void ChaserRunner::slotChaserChanged()
41 ✔
101
{
102
    // Handle (possible) speed change on the next write() pass
103
    m_updateOverrideSpeeds = true;
41 ✔
104

105
    // Steps added or removed: the random order must cover them all.
106
    // Rebuilt on the next write() pass, in the MasterTimer thread
107
    if (m_order.size() != m_chaser->stepsCount())
41 ✔
108
        m_orderRefreshNeeded = true;
22 ✔
109
    QList<ChaserRunnerStep*> delList;
41 ✔
110
    foreach (ChaserRunnerStep *step, m_runnerSteps)
50 ✔
111
    {
112
        if (!m_chaser->steps().contains(ChaserStep(step->m_function->id())))
9 ✔
113
        {
114
            // Disappearing function: remove step
115
            delList.append(step);
6 ✔
116
        }
117
        else
118
        {
119
            // Recalculate the speed of each running step
120
            step->m_fadeIn = stepFadeIn(step->m_index);
3 ✔
121
            step->m_fadeOut = stepFadeOut(step->m_index);
3 ✔
122
            step->m_duration = stepDuration(step->m_index);
3 ✔
123
        }
124
    }
41 ✔
125
    foreach (ChaserRunnerStep *step, delList)
47 ✔
126
    {
127
        step->m_function->stop(functionParent());
6 ✔
128
        m_runnerSteps.removeAll(step);
6 ✔
129
        delete step;
6 ✔
130
    }
41 ✔
131
}
41 ✔
132

133
uint ChaserRunner::stepFadeIn(int stepIdx) const
197 ✔
134
{
135
    uint speed = 0;
197 ✔
136
    if (m_chaser->overrideFadeInSpeed() != Function::defaultSpeed())
197 ✔
137
    {
138
        // Override speed is used when another function has started the chaser,
139
        // i.e. chaser inside a chaser that wants to impose its own fade in speed
140
        // to its members.
141
        speed = m_chaser->overrideFadeInSpeed();
10 ✔
142
    }
143
    else
144
    {
145
        switch (m_chaser->fadeInMode())
187 ✔
146
        {
147
            case Chaser::Common:
3 ✔
148
                // All steps' fade in speed is dictated by the chaser
149
                speed = m_chaser->fadeInSpeed();
3 ✔
150
            break;
3 ✔
151
            case Chaser::PerStep:
4 ✔
152
                // Each step specifies its own fade in speed
153
                if (stepIdx >= 0 && stepIdx < m_chaser->stepsCount())
4 ✔
154
                    speed = m_chaser->steps().at(stepIdx).fadeIn;
3 ✔
155
                else
156
                    speed = Function::defaultSpeed();
1 ✔
157
            break;
4 ✔
158
            default:
180 ✔
159
            case Chaser::Default:
160
                // Don't touch members' fade in speed at all
161
                speed = Function::defaultSpeed();
180 ✔
162
            break;
180 ✔
163
        }
164
    }
165

166
    return speed;
197 ✔
167
}
168

169
uint ChaserRunner::stepFadeOut(int stepIdx) const
245 ✔
170
{
171
    uint speed = 0;
245 ✔
172
    if (m_chaser->overrideFadeOutSpeed() != Function::defaultSpeed())
245 ✔
173
    {
174
        // Override speed is used when another function has started the chaser,
175
        // i.e. chaser inside a chaser that wants to impose its own fade out speed
176
        // to its members.
177
        speed = m_chaser->overrideFadeOutSpeed();
10 ✔
178
    }
179
    else
180
    {
181
        switch (m_chaser->fadeOutMode())
235 ✔
182
        {
183
            case Chaser::Common:
3 ✔
184
                // All steps' fade out speed is dictated by the chaser
185
                speed = m_chaser->fadeOutSpeed();
3 ✔
186
            break;
3 ✔
187
            case Chaser::PerStep:
4 ✔
188
                // Each step specifies its own fade out speed
189
                if (stepIdx >= 0 && stepIdx < m_chaser->stepsCount())
4 ✔
190
                    speed = m_chaser->steps().at(stepIdx).fadeOut;
3 ✔
191
                else
192
                    speed = Function::defaultSpeed();
1 ✔
193
            break;
4 ✔
194
            default:
228 ✔
195
            case Chaser::Default:
196
                // Don't touch members' fade out speed at all
197
                speed = Function::defaultSpeed();
228 ✔
198
            break;
228 ✔
199
        }
200
    }
201

202
    return speed;
245 ✔
203
}
204

205
uint ChaserRunner::stepDuration(int stepIdx) const
207 ✔
206
{
207
    uint speed = 0;
207 ✔
208
    if (m_chaser->overrideDuration() != Function::defaultSpeed())
207 ✔
209
    {
210
        // Override speed is used when another function has started the chaser,
211
        // i.e. chaser inside a chaser that wants to impose its own duration
212
        // to its members.
213
        speed = m_chaser->overrideDuration();
10 ✔
214
    }
215
    else
216
    {
217
        switch (m_chaser->durationMode())
197 ✔
218
        {
219
            default:
193 ✔
220
            case Chaser::Default:
221
            case Chaser::Common:
222
                // All steps' duration is dictated by the chaser
223
                speed = m_chaser->duration();
193 ✔
224
            break;
193 ✔
225
            case Chaser::PerStep:
4 ✔
226
                // Each step specifies its own duration
227
                if (stepIdx >= 0 && stepIdx < m_chaser->stepsCount())
4 ✔
228
                    speed = m_chaser->steps().at(stepIdx).duration;
3 ✔
229
                else
230
                    speed = m_chaser->duration();
1 ✔
231
            break;
4 ✔
232
        }
233
    }
234

235
    return speed;
207 ✔
236
}
237

238
/****************************************************************************
239
 * Step control
240
 ****************************************************************************/
241

242
void ChaserRunner::setAction(const ChaserAction &action)
41 ✔
243
{
244
    // apply the actions that can be applied immediately
245
    switch (action.m_action)
41 ✔
246
    {
247
        case ChaserNoAction:
7 ✔
248
            m_pendingAction.m_masterIntensity = action.m_masterIntensity;
7 ✔
249
            m_pendingAction.m_stepIntensity = action.m_stepIntensity;
7 ✔
250
        break;
7 ✔
251

252
        case ChaserStopStep:
×
253
        {
254
            bool stopped = false;
×
255

256
            foreach (ChaserRunnerStep *step, m_runnerSteps)
×
257
            {
258
                if (action.m_stepIndex == step->m_index)
×
259
                {
260
                    qDebug() << "[ChaserRunner] Stopping step idx:" << action.m_stepIndex << "(running:" << m_runnerSteps.count() << ")";
×
261
                    m_lastFunctionID = step->m_function->type() == Function::SceneType ? step->m_function->id() : Function::invalidId();
×
262
                    step->m_function->stop(functionParent());
×
263
                    m_runnerSteps.removeOne(step);
×
264
                    delete step;
×
265
                    stopped = true;
×
266
                }
267
            }
×
268

269
            if (stopped && m_runnerSteps.size() == 1)
×
270
            {
271
                ChaserRunnerStep *lastStep = m_runnerSteps.at(0);
×
272
                m_lastRunStepIdx = lastStep->m_index;
×
273
                emit currentStepChanged(m_lastRunStepIdx);
×
274
            }
275
        }
276
        break;
×
277

278
        // copy to pending action. Will be processed at the next write call
279
        default:
34 ✔
280
            m_pendingAction.m_stepIndex = action.m_stepIndex;
34 ✔
281
            m_pendingAction.m_masterIntensity = action.m_masterIntensity;
34 ✔
282
            m_pendingAction.m_stepIntensity = action.m_stepIntensity;
34 ✔
283
            m_pendingAction.m_fadeMode = action.m_fadeMode;
34 ✔
284
            m_pendingAction.m_action = action.m_action;
34 ✔
285
        break;
34 ✔
286
    }
287
}
41 ✔
288

289
void ChaserRunner::tap()
2 ✔
290
{
291
    if (uint(m_roundTime->elapsed()) >= (stepDuration(m_lastRunStepIdx) / 4))
2 ✔
292
        m_pendingAction.m_action = ChaserNextStep;
2 ✔
293
}
2 ✔
294

295
int ChaserRunner::currentStepIndex() const
65 ✔
296
{
297
    return m_lastRunStepIdx;
65 ✔
298
}
299

300
int ChaserRunner::runningStepsNumber() const
1 ✔
301
{
302
    return m_runnerSteps.count();
1 ✔
303
}
304

305
ChaserRunnerStep *ChaserRunner::currentRunningStep() const
×
306
{
307
    if (m_runnerSteps.count() > 0)
×
308
        return m_runnerSteps.at(0);
×
309
    return NULL;
×
310
}
311

312
int ChaserRunner::computeNextStep(int currentStep) const
122 ✔
313
{
314
    int nextStep = currentStep;
122 ✔
315

316
    if (m_chaser->runOrder() == Function::Random)
122 ✔
317
    {
318
        nextStep = m_order.indexOf(nextStep);
101 ✔
319
        if (nextStep == -1)
101 ✔
320
        {
321
            qDebug() << "[ChaserRunner] steps order not found";
×
322
            nextStep = currentStep;
×
323
        }
324
    }
325

326
    // Next step
327
    if (m_direction == Function::Forward)
122 ✔
328
    {
329
        nextStep++;
22 ✔
330
    }
331
    else
332
    {
333
        nextStep--;
100 ✔
334
    }
335

336
    if (nextStep < m_chaser->stepsCount() && nextStep >= 0)
122 ✔
337
    {
338
        if (m_chaser->runOrder() == Function::Random)
16 ✔
339
        {
340
            nextStep = randomStepIndex(nextStep);
×
341
        }
342
        return nextStep; // In the middle of steps. No need to go any further.
16 ✔
343
    }
344

345
    if (m_chaser->runOrder() == Function::SingleShot)
106 ✔
346
    {
347
        return -1; // Forward or Backward SingleShot has been completed.
×
348
    }
349
    else if (m_chaser->runOrder() == Function::Loop)
106 ✔
350
    {
351
        if (m_direction == Function::Forward)
5 ✔
352
        {
353
            if (nextStep >= m_chaser->stepsCount())
5 ✔
354
                nextStep = 0;
5 ✔
355
            else
356
                nextStep = m_chaser->stepsCount() - 1; // Used by CueList with manual prev
×
357
        }
358
        else // Backward
359
        {
360
            if (nextStep < 0)
×
361
                nextStep = m_chaser->stepsCount() - 1;
×
362
            else
363
                nextStep = 0;
×
364
        }
365
    }
366
    else if (m_chaser->runOrder() == Function::Random)
101 ✔
367
    {
368
        // The next round will be shuffled again, so its first step is
369
        // not known yet. Return a valid step meanwhile
370
        nextStep = randomStepIndex(m_direction == Function::Forward ? 0 : m_chaser->stepsCount() - 1);
101 ✔
371
    }
372
    else // Ping Pong
373
    {
374
        // Change direction, but don't run the first/last step twice.
375
        if (m_direction == Function::Forward)
×
376
        {
377
            nextStep = m_chaser->stepsCount() - 2;
×
378
        }
379
        else // Backwards
380
        {
381
            nextStep = 1;
×
382
        }
383

384
        // Make sure we don't go beyond limits.
385
        nextStep = CLAMP(nextStep, 0, m_chaser->stepsCount() - 1);
×
386
    }
387

388
    return nextStep;
106 ✔
389
}
390

391
void ChaserRunner::shuffle(QVector<int> & data)
248 ✔
392
{
393
   int n = data.size();
248 ✔
394
   for (int i = n - 1; i > 0; --i)
736 ✔
395
   {
396
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
397
      qSwap(data[i], data[qrand() % (i + 1)]);
398
#else
399
      qSwap(data[i], data[QRandomGenerator::global()->generate() % (i + 1)]);
488 ✔
400
#endif
401
   }
402
}
248 ✔
403

404
int ChaserRunner::randomStepIndex(int step) const
328 ✔
405
{
406
   if (m_chaser->runOrder() == Function::Random && step >= 0 && step < m_order.size())
328 ✔
407
       return m_order[step];
328 ✔
408

409
   return step;
×
410
}
411

412
void ChaserRunner::fillOrder()
248 ✔
413
{
414
    fillOrder(m_chaser->stepsCount());
248 ✔
415
}
248 ✔
416

417
void ChaserRunner::fillOrder(int size)
248 ✔
418
{
419
   m_order.resize(size);
248 ✔
420
   for (int i = 0; i < size; ++i)
980 ✔
421
       m_order[i] = i;
732 ✔
422

423
   shuffle(m_order);
248 ✔
424
}
248 ✔
425

426
/****************************************************************************
427
 * Intensity
428
 ****************************************************************************/
429

430
void ChaserRunner::adjustStepIntensity(qreal fraction, int requestedStepIndex, int fadeControl)
12 ✔
431
{
432
    fraction = CLAMP(fraction, qreal(0.0), qreal(1.0));
12 ✔
433

434
    //qDebug() << "Adjust intensity" << fraction << "step:" << requestedStepIndex << "fade:" << fadeControl;
435

436
    int stepIndex = requestedStepIndex;
12 ✔
437
    if (stepIndex == -1)
12 ✔
438
    {
439
        // store the intensity to be applied at the next step startup
440
        m_pendingAction.m_masterIntensity = fraction;
10 ✔
441

442
        foreach (ChaserRunnerStep *step, m_runnerSteps)
16 ✔
443
        {
444
            if (step == NULL || step->m_function == NULL)
6 ✔
445
                continue;
×
446

447
            step->m_masterIntensity = fraction;
6 ✔
448
            if (step->m_function->type() == Function::SceneType)
6 ✔
449
            {
450
                Scene *scene = qobject_cast<Scene *>(step->m_function);
6 ✔
451
                scene->adjustAttribute(fraction, step->m_pIntensityOverrideId);
6 ✔
452
            }
453
            else
454
            {
455
                step->m_function->adjustAttribute(fraction * step->m_stepIntensity, step->m_intensityOverrideId);
×
456
            }
457
        }
10 ✔
458

459
        return;
10 ✔
460
    }
461

462
    foreach (ChaserRunnerStep *step, m_runnerSteps)
3 ✔
463
    {
464
        if (stepIndex == step->m_index && step->m_function != NULL)
1 ✔
465
        {
466
            step->m_stepIntensity = fraction;
×
467
            if (step->m_function->type() == Function::SceneType)
×
468
                step->m_function->adjustAttribute(fraction, step->m_intensityOverrideId);
×
469
            else
470
                step->m_function->adjustAttribute(step->m_masterIntensity * fraction, step->m_intensityOverrideId);
×
471
            return;
×
472
        }
473
    }
2 ✔
474

475
    // No need to start a new step if it is not wanted
476
    if (requestedStepIndex == -1)
2 ✔
477
        return;
×
478

479
    // Don't start a step with an intensity of zero
480
    if (fraction == qreal(0.0))
2 ✔
481
        return;
×
482

483
    // not found ? It means we need to start a new step and crossfade kicks in !
484
    startNewStep(stepIndex, m_doc->masterTimer(), m_pendingAction.m_masterIntensity, fraction, fadeControl);
2 ✔
485
}
486

487
/****************************************************************************
488
 * Running
489
 ****************************************************************************/
490

491
void ChaserRunner::clearRunningList()
193 ✔
492
{
493
    // empty the running queue
494
    foreach (ChaserRunnerStep *step, m_runnerSteps)
241 ✔
495
    {
496
        if (step->m_function)
48 ✔
497
        {
498
            // restore the original Function fade out time
499
            step->m_function->setOverrideFadeOutSpeed(stepFadeOut(step->m_index));
48 ✔
500
            step->m_function->stop(functionParent(), m_chaser->type() == Function::SequenceType);
48 ✔
501
            m_lastFunctionID = step->m_function->type() == Function::SceneType ? step->m_function->id() : Function::invalidId();
48 ✔
502
        }
503
        delete step;
48 ✔
504
    }
193 ✔
505
    m_runnerSteps.clear();
193 ✔
506
}
193 ✔
507

508
void ChaserRunner::updateBeatPosition(MasterTimer *timer)
1,534 ✔
509
{
510
    int beatDuration = qMax(1, timer->beatTimeDuration());
1,534 ✔
511

512
    if (m_beatClockStarted == false)
1,534 ✔
513
    {
514
        // start in phase with the MasterTimer beat clock
515
        m_beatClockStarted = true;
33 ✔
516
        if (timer->isBeat())
33 ✔
517
            m_msSinceBeat = 0;
5 ✔
518
        else
519
            m_msSinceBeat = qBound(0, beatDuration - timer->timeToNextBeat(), beatDuration - 1);
28 ✔
520
    }
521
    else if (timer->isBeat())
1,501 ✔
522
    {
523
        m_lastBeatPosition += 1000;
19 ✔
524
        m_msSinceBeat = 0;
19 ✔
525
    }
526
    else if (m_msSinceBeat < beatDuration)
1,482 ✔
527
    {
528
        m_msSinceBeat += MasterTimer::tick();
1,375 ✔
529
    }
530

531
    // never reach the next whole beat before the beat actually happens
532
    qint64 phase = qMin(qint64(999), (qint64(m_msSinceBeat) * 1000) / beatDuration);
1,534 ✔
533
    m_beatPosition = m_lastBeatPosition + phase;
1,534 ✔
534
}
1,534 ✔
535

536
qint64 ChaserRunner::alignedBeatStart(uint duration) const
7 ✔
537
{
538
    qint64 phase = m_beatPosition - m_lastBeatPosition;
7 ✔
539

540
    // anchor the step to the last beat if it still has a reasonable time to run.
541
    // Whole beat steps always end on a beat, like the beat count used to do
542
    if (duration >= 1000 || phase < qint64(duration / 2))
7 ✔
543
        return m_lastBeatPosition;
6 ✔
544

545
    // chaser start: extend the first step until the next beat, so that the
546
    // following steps fall on the beat grid
547
    if (m_firstStepStarted == false)
1 ✔
548
        return m_lastBeatPosition + 1000;
1 ✔
549

550
    return m_beatPosition;
×
551
}
552

553
void ChaserRunner::startNewStep(int index, MasterTimer *timer, qreal mIntensity, qreal sIntensity,
182 ✔
554
                                int fadeControl, quint32 elapsed, qint64 beatStart)
555
{
556
    if (m_chaser == NULL || m_chaser->stepsCount() == 0)
182 ✔
557
        return;
×
558

559
    if (index < 0 || index >= m_chaser->stepsCount())
182 ✔
560
        index = 0; // fallback to the first step
×
561

562
    ChaserStep step(m_chaser->steps().at(index));
182 ✔
563
    Function *func = m_doc->function(step.fid);
182 ✔
564
    if (func == NULL)
182 ✔
565
        return;
×
566

567
    ChaserRunnerStep *newStep = new ChaserRunnerStep();
182 ✔
568
    newStep->m_index = index;
182 ✔
569
    newStep->m_function = func;
182 ✔
570
    newStep->m_masterIntensity = mIntensity;
182 ✔
571
    newStep->m_stepIntensity = sIntensity;
182 ✔
572
    newStep->m_intensityOverrideId = Function::invalidAttributeId();
182 ✔
573
    newStep->m_pIntensityOverrideId = Function::invalidAttributeId();
182 ✔
574

575
    // check if blending between Scenes is needed
576
    if (m_lastFunctionID != Function::invalidId() &&
327 ✔
577
        func->type() == Function::SceneType)
145 ✔
578
    {
579
        Scene *scene = qobject_cast<Scene *>(func);
145 ✔
580
        scene->setBlendFunctionID(m_lastFunctionID);
145 ✔
581
    }
582

583
    // this happens only during crossfades
584
    if (m_runnerSteps.count())
182 ✔
585
    {
586
        ChaserRunnerStep *lastStep = m_runnerSteps.last();
1 ✔
587
        if (lastStep->m_function &&
3 ✔
588
            lastStep->m_function->type() == Function::SceneType &&
2 ✔
589
            func->type() == Function::SceneType)
1 ✔
590
        {
591
            Scene *lastScene = qobject_cast<Scene *>(lastStep->m_function);
1 ✔
592
            lastScene->setBlendFunctionID(Function::invalidId());
1 ✔
593
            Scene *scene = qobject_cast<Scene *>(func);
1 ✔
594
            scene->setBlendFunctionID(lastStep->m_function->id());
1 ✔
595
        }
596
    }
597

598
    switch (fadeControl)
182 ✔
599
    {
600
        case Chaser::FromFunction:
172 ✔
601
            newStep->m_fadeIn = stepFadeIn(index);
172 ✔
602
            newStep->m_fadeOut = stepFadeOut(index);
172 ✔
603
        break;
172 ✔
604
        case Chaser::Blended:
2 ✔
605
            newStep->m_fadeIn = stepFadeIn(index);
2 ✔
606
            newStep->m_fadeOut = stepFadeOut(index);
2 ✔
607
        break;
2 ✔
608
        case Chaser::Crossfade:
×
609
            newStep->m_fadeIn = 0;
×
610
            newStep->m_fadeOut = 0;
×
611
        break;
×
612
        case Chaser::BlendedCrossfade:
2 ✔
613
            newStep->m_fadeIn = 0;
2 ✔
614
            newStep->m_fadeOut = 0;
2 ✔
615
        break;
2 ✔
616
    }
617

618
    newStep->m_duration = stepDuration(index);
182 ✔
619

620
    if (m_startOffset != 0)
182 ✔
621
        newStep->m_elapsed = m_startOffset + MasterTimer::tick();
2 ✔
622
    else
623
        newStep->m_elapsed = MasterTimer::tick() + elapsed;
180 ✔
624

625
    /* Beats tempo: a step chained to the previous one starts exactly where the
626
     * previous one should have ended, so that steps don't accumulate drift.
627
     * Otherwise (or if that would end the step right away) align it to the beat */
628
    if (m_chaser->tempoType() == Function::Beats)
182 ✔
629
    {
630
        if (beatStart >= 0 && beatStart + qint64(newStep->m_duration) > m_beatPosition)
57 ✔
631
            newStep->m_beatStart = beatStart;
50 ✔
632
        else
633
            newStep->m_beatStart = alignedBeatStart(newStep->m_duration);
7 ✔
634
    }
635
    else
636
    {
637
        newStep->m_beatStart = m_beatPosition;
125 ✔
638
    }
639
    m_firstStepStarted = true;
182 ✔
640

641
    m_startOffset = 0;
182 ✔
642

643
    if (m_chaser->type() == Function::SequenceType)
182 ✔
644
    {
645
        Scene *s = qobject_cast<Scene*>(func);
×
646
        // blind == true is a workaround to reuse the same scene
647
        // without messing up the previous values
648
        for (int i = 0; i < step.values.count(); i++)
×
649
            s->setValue(step.values.at(i), true);
×
650
    }
651

652
    qDebug() << "[ChaserRunner] Starting step" << index << "fade in" << newStep->m_fadeIn
364 ✔
653
             << "fade out" << newStep->m_fadeOut << "intensity" << mIntensity
182 ✔
654
             << "fadeMode" << fadeControl;
182 ✔
655

656
    // Set intensity before starting the function. Otherwise the intensity
657
    // might momentarily jump too high.
658
    if (func->type() == Function::SceneType)
182 ✔
659
    {
660
        Scene *scene = qobject_cast<Scene *>(func);
182 ✔
661
        newStep->m_intensityOverrideId = func->requestAttributeOverride(Function::Intensity, sIntensity);
182 ✔
662
        newStep->m_pIntensityOverrideId = scene->requestAttributeOverride(Scene::ParentIntensity, mIntensity);
182 ✔
663
        qDebug() << "[ChaserRunner] Set step intensity:" << sIntensity << ", master:" << mIntensity;
182 ✔
664
    }
665
    else
666
    {
667
        newStep->m_intensityOverrideId = func->requestAttributeOverride(Function::Intensity, mIntensity * sIntensity);
×
668
    }
669

670
    // Start the fire up!
671
    func->start(timer, functionParent(), 0, newStep->m_fadeIn, newStep->m_fadeOut,
182 ✔
672
                func->defaultSpeed(), m_chaser->tempoType());
182 ✔
673
    m_runnerSteps.append(newStep);
182 ✔
674
    m_roundTime->restart();
182 ✔
675
}
182 ✔
676

677
int ChaserRunner::getNextStepIndex()
277 ✔
678
{
679
    int currentStepIndex = m_lastRunStepIdx;
277 ✔
680

681
    if (m_chaser->runOrder() == Function::Random)
277 ✔
682
    {
683
        currentStepIndex = m_order.indexOf(currentStepIndex);
102 ✔
684
        if (currentStepIndex == -1)
102 ✔
685
        {
686
            qDebug() << "[ChaserRunner] steps order not found";
1 ✔
687
            currentStepIndex = m_lastRunStepIdx;
1 ✔
688
        }
689
    }
690

691
    if (currentStepIndex == -1 &&
306 ✔
692
        m_chaser->direction() == Function::Backward)
29 ✔
693
            currentStepIndex = m_chaser->stepsCount();
6 ✔
694

695
    // Handle reverse Ping Pong at boundaries
696
    if (m_chaser->runOrder() == Function::PingPong &&
305 ✔
697
        m_pendingAction.m_action == ChaserPreviousStep)
28 ✔
698
    {
699
        if (currentStepIndex == 0)
×
700
            m_direction = Function::Backward;
×
701
        else if (currentStepIndex == m_chaser->stepsCount() - 1)
×
702
            m_direction = Function::Forward;
×
703
    }
704

705
    // Next step
706
    if (m_direction == Function::Forward)
277 ✔
707
    {
708
        // "Previous" for a forward chaser is -1
709
        if (m_pendingAction.m_action == ChaserPreviousStep)
143 ✔
710
            currentStepIndex--;
10 ✔
711
        else
712
            currentStepIndex++;
133 ✔
713
    }
714
    else
715
    {
716
        // "Previous" for a backward scene is +1
717
        if (m_pendingAction.m_action == ChaserPreviousStep)
134 ✔
718
            currentStepIndex++;
×
719
        else
720
            currentStepIndex--;
134 ✔
721
    }
722

723
    if (currentStepIndex < m_chaser->stepsCount() && currentStepIndex >= 0)
277 ✔
724
    {
725
        if (m_chaser->runOrder() == Function::Random)
134 ✔
726
        {
727
            currentStepIndex = randomStepIndex(currentStepIndex);
1 ✔
728
        }
729
        return currentStepIndex; // In the middle of steps. No need to go any further.
134 ✔
730
    }
731

732
    if (m_chaser->runOrder() == Function::SingleShot)
143 ✔
733
    {
734
        return -1; // Forward or Backward SingleShot has been completed.
4 ✔
735
    }
736
    else if (m_chaser->runOrder() == Function::Loop)
139 ✔
737
    {
738
        if (m_direction == Function::Forward)
30 ✔
739
        {
740
            if (currentStepIndex >= m_chaser->stepsCount())
28 ✔
741
                currentStepIndex = 0;
24 ✔
742
            else
743
                currentStepIndex = m_chaser->stepsCount() - 1; // Used by CueList with manual prev
4 ✔
744
        }
745
        else // Backward
746
        {
747
            if (currentStepIndex < 0)
2 ✔
748
                currentStepIndex = m_chaser->stepsCount() - 1;
2 ✔
749
            else
750
                currentStepIndex = 0;
×
751
        }
752
    }
753
    else if (m_chaser->runOrder() == Function::Random)
109 ✔
754
    {
755
        fillOrder();
101 ✔
756
        if (m_direction == Function::Forward)
101 ✔
757
        {
758
            if (currentStepIndex >= m_chaser->stepsCount())
1 ✔
759
                currentStepIndex = 0;
1 ✔
760
            else
761
                currentStepIndex = m_chaser->stepsCount() - 1; // Used by CueList with manual prev
×
762
        }
763
        else // Backward
764
        {
765
            if (currentStepIndex < 0)
100 ✔
766
                currentStepIndex = m_chaser->stepsCount() - 1;
100 ✔
767
            else
768
                currentStepIndex = 0;
×
769
        }
770
        // Don't run the same function 2 times in a row, moving inwards from
771
        // the end of the new order the round starts from. With a single step
772
        // there is no other choice than repeating it
773
        int position = currentStepIndex;
101 ✔
774
        int increment = position == 0 ? 1 : -1;
101 ✔
775
        while (position >= 0 && position < m_chaser->stepsCount()
227 ✔
776
               && randomStepIndex(position) == m_lastRunStepIdx)
252 ✔
777
            position += increment;
25 ✔
778
        if (position < 0 || position >= m_chaser->stepsCount())
101 ✔
779
            position = currentStepIndex;
1 ✔
780
        currentStepIndex = randomStepIndex(position);
101 ✔
781
    }
782
    else // Ping Pong
783
    {
784
        // Change direction, but don't run the first/last step twice.
785
        if (m_direction == Function::Forward)
8 ✔
786
        {
787
            currentStepIndex = m_chaser->stepsCount() - 2;
4 ✔
788
            m_direction = Function::Backward;
4 ✔
789
        }
790
        else // Backwards
791
        {
792
            currentStepIndex = 1;
4 ✔
793
            m_direction = Function::Forward;
4 ✔
794
        }
795

796
        // Make sure we don't go beyond limits.
797
        currentStepIndex = CLAMP(currentStepIndex, 0, m_chaser->stepsCount() - 1);
8 ✔
798
    }
799

800
    return currentStepIndex;
139 ✔
801
}
802

803
void ChaserRunner::setPause(bool enable, QList<Universe *> universes)
2 ✔
804
{
805
    // Nothing to do
806
    if (m_chaser->stepsCount() == 0)
2 ✔
807
        return;
×
808

809
    qDebug() << "[ChaserRunner] processing pause request:" << enable;
2 ✔
810

811
    foreach (ChaserRunnerStep *step, m_runnerSteps)
4 ✔
812
        step->m_function->setPause(enable);
4 ✔
813

814
    // there might be a Scene fading out, so request pause
815
    // to faders bound to the Scene ID running on universes
816
    Function *f = m_doc->function(m_lastFunctionID);
2 ✔
817
    if (f != NULL && f->type() == Function::SceneType)
2 ✔
818
    {
819
        foreach (Universe *universe, universes)
10 ✔
820
            universe->setFaderPause(m_lastFunctionID, enable);
10 ✔
821
    }
822
}
823

824
FunctionParent ChaserRunner::functionParent() const
364 ✔
825
{
826
    return FunctionParent(FunctionParent::Function, m_chaser->id());
364 ✔
827
}
828

829
bool ChaserRunner::write(MasterTimer *timer, QList<Universe *> universes)
1,535 ✔
830
{
831
    // Nothing to do
832
    if (m_chaser->stepsCount() == 0)
1,535 ✔
833
        return false;
1 ✔
834

835
    if (m_orderRefreshNeeded)
1,534 ✔
836
    {
837
        m_orderRefreshNeeded = false;
1 ✔
838
        fillOrder();
1 ✔
839
    }
840

841
    updateBeatPosition(timer);
1,534 ✔
842

843
    // a step started by a pending action, before the running steps are
844
    // advanced below. It must not be advanced in the same write, as a step
845
    // started after them isn't, or it would run a tick ahead
846
    ChaserRunnerStep *startedStep = NULL;
1,534 ✔
847

848
    switch (m_pendingAction.m_action)
1,534 ✔
849
    {
850
        case ChaserNextStep:
19 ✔
851
        case ChaserPreviousStep:
852
            clearRunningList();
19 ✔
853
            // the actual action will be performed below, on startNewStep
854
        break;
19 ✔
855
        case ChaserSetStepIndex:
11 ✔
856
            if (m_pendingAction.m_stepIndex != -1)
11 ✔
857
            {
858
                clearRunningList();
8 ✔
859
                if (m_chaser->runOrder() == Function::Random)
8 ✔
860
                    m_lastRunStepIdx = randomStepIndex(m_pendingAction.m_stepIndex);
×
861
                else
862
                    m_lastRunStepIdx = m_pendingAction.m_stepIndex;
8 ✔
863

864
                qDebug() << "[ChaserRunner] Starting from step" << m_lastRunStepIdx << "@ offset" << m_startOffset;
8 ✔
865
                startNewStep(m_lastRunStepIdx, timer, m_pendingAction.m_masterIntensity,
8 ✔
866
                             m_pendingAction.m_stepIntensity, m_pendingAction.m_fadeMode);
867
                if (m_runnerSteps.isEmpty() == false)
8 ✔
868
                    startedStep = m_runnerSteps.last();
8 ✔
869
                emit currentStepChanged(m_lastRunStepIdx);
8 ✔
870
            }
871
        break;
11 ✔
872
        case ChaserPauseRequest:
2 ✔
873
            setPause(m_pendingAction.m_fadeMode ? true : false, universes);
2 ✔
874
        break;
2 ✔
875
        default:
1,502 ✔
876
        break;
1,502 ✔
877
    }
878

879
    quint32 prevStepRoundElapsed = 0;
1,534 ✔
880
    qint64 nextBeatStart = -1;
1,534 ✔
881

882
    foreach (ChaserRunnerStep *step, m_runnerSteps)
3,020 ✔
883
    {
884
        if (step == startedStep)
1,486 ✔
885
            continue;
8 ✔
886

887
        bool stepDone = false;
1,478 ✔
888

889
        if (step->m_duration != Function::infiniteSpeed())
1,478 ✔
890
        {
891
            if (m_chaser->tempoType() == Function::Time)
1,385 ✔
892
                stepDone = step->m_elapsed >= step->m_duration;
267 ✔
893
            else
894
                stepDone = m_beatPosition - step->m_beatStart >= qint64(step->m_duration);
1,118 ✔
895
        }
896

897
        if (stepDone)
1,478 ✔
898
        {
899
            if (m_chaser->tempoType() == Function::Beats)
128 ✔
900
                nextBeatStart = step->m_beatStart + step->m_duration;
50 ✔
901
            else if (step->m_duration != 0)
78 ✔
902
                prevStepRoundElapsed = step->m_elapsed % step->m_duration;
33 ✔
903

904
            m_lastFunctionID = step->m_function->type() == Function::SceneType ? step->m_function->id() : Function::invalidId();
128 ✔
905
            step->m_function->stop(functionParent(), m_chaser->type() == Function::SequenceType);
128 ✔
906
            m_runnerSteps.removeOne(step);
128 ✔
907
            delete step;
128 ✔
908
        }
909
        else
910
        {
911
            if (step->m_elapsed < UINT_MAX)
1,350 ✔
912
                step->m_elapsed += MasterTimer::tick();
1,350 ✔
913

914
            // When the speeds of the chaser change, they need to be updated to the lower
915
            // level (only current function) as well. Otherwise the new speeds would take
916
            // effect only on the next step change.
917
            if (m_updateOverrideSpeeds == true)
1,350 ✔
918
            {
919
                m_updateOverrideSpeeds = false;
×
920
                if (step->m_function != NULL)
×
921
                {
922
                    step->m_function->setOverrideFadeInSpeed(step->m_fadeIn);
×
923
                    step->m_function->setOverrideFadeOutSpeed(step->m_fadeOut);
×
924
                }
925
            }
926
        }
927
    }
1,534 ✔
928

929
    if (m_runnerSteps.isEmpty())
1,534 ✔
930
    {
931
        m_lastRunStepIdx = getNextStepIndex();
176 ✔
932
        if (m_lastRunStepIdx != -1)
176 ✔
933
        {
934
            int blend = m_pendingAction.m_action == ChaserNoAction ? Chaser::FromFunction : m_pendingAction.m_fadeMode;
172 ✔
935

936
            startNewStep(m_lastRunStepIdx, timer, m_pendingAction.m_masterIntensity,
172 ✔
937
                         m_pendingAction.m_stepIntensity, blend, prevStepRoundElapsed, nextBeatStart);
938
            emit currentStepChanged(m_lastRunStepIdx);
172 ✔
939
        }
940
        else
941
        {
942
            m_pendingAction.m_action = ChaserNoAction;
4 ✔
943
            return false;
4 ✔
944
        }
945
    }
946

947
    m_pendingAction.m_action = ChaserNoAction;
1,530 ✔
948
    return true;
1,530 ✔
949
}
950

951
void ChaserRunner::postRun(MasterTimer *timer, QList<Universe*> universes)
20 ✔
952
{
953
    Q_UNUSED(universes);
954
    Q_UNUSED(timer);
955

956
    qDebug() << Q_FUNC_INFO;
20 ✔
957
    clearRunningList();
20 ✔
958
}
20 ✔
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