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

mcallegari / qlcplus / 40210868035

25 Oct 2026 04:16PM UTC coverage: 36.132% (+0.06%) from 36.076%
40210868035

push

github

mcallegari
qmlui: improve Chaser fractional beat support (fix #2189)

18843 of 52151 relevant lines covered (36.13%)

42079.81 hits per line

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

80.21
/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)
44 ✔
37
    : QObject(NULL)
38
    , m_doc(doc)
44 ✔
39
    , m_chaser(chaser)
44 ✔
40
    , m_updateOverrideSpeeds(false)
44 ✔
41
    , m_startOffset(0)
44 ✔
42
    , m_lastRunStepIdx(-1)
44 ✔
43
    , m_lastFunctionID(Function::invalidId())
44 ✔
44
    , m_roundTime(new QElapsedTimer())
44 ✔
45
    , m_order()
44 ✔
46
    , m_beatClockStarted(false)
44 ✔
47
    , m_firstStepStarted(false)
44 ✔
48
    , m_lastBeatPosition(0)
44 ✔
49
    , m_beatPosition(0)
44 ✔
50
    , m_msSinceBeat(0)
44 ✔
51
{
52
    Q_ASSERT(chaser != NULL);
44 ✔
53

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

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

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

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

86
    fillOrder();
44 ✔
87
}
44 ✔
88

89
ChaserRunner::~ChaserRunner()
57 ✔
90
{
91
    clearRunningList();
44 ✔
92
    delete m_roundTime;
44 ✔
93
}
57 ✔
94

95
/****************************************************************************
96
 * Speed
97
 ****************************************************************************/
98

99
void ChaserRunner::slotChaserChanged()
39 ✔
100
{
101
    // Handle (possible) speed change on the next write() pass
102
    m_updateOverrideSpeeds = true;
39 ✔
103
    QList<ChaserRunnerStep*> delList;
39 ✔
104
    foreach (ChaserRunnerStep *step, m_runnerSteps)
48 ✔
105
    {
106
        if (!m_chaser->steps().contains(ChaserStep(step->m_function->id())))
9 ✔
107
        {
108
            // Disappearing function: remove step
109
            delList.append(step);
6 ✔
110
        }
111
        else
112
        {
113
            // Recalculate the speed of each running step
114
            step->m_fadeIn = stepFadeIn(step->m_index);
3 ✔
115
            step->m_fadeOut = stepFadeOut(step->m_index);
3 ✔
116
            step->m_duration = stepDuration(step->m_index);
3 ✔
117
        }
118
    }
39 ✔
119
    foreach (ChaserRunnerStep *step, delList)
45 ✔
120
    {
121
        step->m_function->stop(functionParent());
6 ✔
122
        m_runnerSteps.removeAll(step);
6 ✔
123
        delete step;
6 ✔
124
    }
39 ✔
125
}
39 ✔
126

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

160
    return speed;
196 ✔
161
}
162

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

196
    return speed;
243 ✔
197
}
198

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

229
    return speed;
206 ✔
230
}
231

232
/****************************************************************************
233
 * Step control
234
 ****************************************************************************/
235

236
void ChaserRunner::setAction(const ChaserAction &action)
41 ✔
237
{
238
    // apply the actions that can be applied immediately
239
    switch (action.m_action)
41 ✔
240
    {
241
        case ChaserNoAction:
7 ✔
242
            m_pendingAction.m_masterIntensity = action.m_masterIntensity;
7 ✔
243
            m_pendingAction.m_stepIntensity = action.m_stepIntensity;
7 ✔
244
        break;
7 ✔
245

246
        case ChaserStopStep:
×
247
        {
248
            bool stopped = false;
×
249

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

263
            if (stopped && m_runnerSteps.size() == 1)
×
264
            {
265
                ChaserRunnerStep *lastStep = m_runnerSteps.at(0);
×
266
                m_lastRunStepIdx = lastStep->m_index;
×
267
                emit currentStepChanged(m_lastRunStepIdx);
×
268
            }
269
        }
270
        break;
×
271

272
        // copy to pending action. Will be processed at the next write call
273
        default:
34 ✔
274
            m_pendingAction.m_stepIndex = action.m_stepIndex;
34 ✔
275
            m_pendingAction.m_masterIntensity = action.m_masterIntensity;
34 ✔
276
            m_pendingAction.m_stepIntensity = action.m_stepIntensity;
34 ✔
277
            m_pendingAction.m_fadeMode = action.m_fadeMode;
34 ✔
278
            m_pendingAction.m_action = action.m_action;
34 ✔
279
        break;
34 ✔
280
    }
281
}
41 ✔
282

283
void ChaserRunner::tap()
2 ✔
284
{
285
    if (uint(m_roundTime->elapsed()) >= (stepDuration(m_lastRunStepIdx) / 4))
2 ✔
286
        m_pendingAction.m_action = ChaserNextStep;
2 ✔
287
}
2 ✔
288

289
int ChaserRunner::currentStepIndex() const
65 ✔
290
{
291
    return m_lastRunStepIdx;
65 ✔
292
}
293

294
int ChaserRunner::runningStepsNumber() const
1 ✔
295
{
296
    return m_runnerSteps.count();
1 ✔
297
}
298

299
ChaserRunnerStep *ChaserRunner::currentRunningStep() const
×
300
{
301
    if (m_runnerSteps.count() > 0)
×
302
        return m_runnerSteps.at(0);
×
303
    return NULL;
×
304
}
305

306
int ChaserRunner::computeNextStep(int currentStep) const
21 ✔
307
{
308
    int nextStep = currentStep;
21 ✔
309

310
    if (m_chaser->runOrder() == Function::Random)
21 ✔
311
    {
312
        nextStep = m_order.indexOf(nextStep);
×
313
        if (nextStep == -1)
×
314
        {
315
            qDebug() << "[ChaserRunner] steps order not found";
×
316
            nextStep = currentStep;
×
317
        }
318
    }
319

320
    // Next step
321
    if (m_direction == Function::Forward)
21 ✔
322
    {
323
        nextStep++;
21 ✔
324
    }
325
    else
326
    {
327
        nextStep--;
×
328
    }
329

330
    if (nextStep < m_chaser->stepsCount() && nextStep >= 0)
21 ✔
331
    {
332
        if (m_chaser->runOrder() == Function::Random)
16 ✔
333
        {
334
            nextStep = randomStepIndex(nextStep);
×
335
        }
336
        return nextStep; // In the middle of steps. No need to go any further.
16 ✔
337
    }
338

339
    if (m_chaser->runOrder() == Function::SingleShot)
5 ✔
340
    {
341
        return -1; // Forward or Backward SingleShot has been completed.
×
342
    }
343
    else if (m_chaser->runOrder() == Function::Loop)
5 ✔
344
    {
345
        if (m_direction == Function::Forward)
5 ✔
346
        {
347
            if (nextStep >= m_chaser->stepsCount())
5 ✔
348
                nextStep = 0;
5 ✔
349
            else
350
                nextStep = m_chaser->stepsCount() - 1; // Used by CueList with manual prev
×
351
        }
352
        else // Backward
353
        {
354
            if (nextStep < 0)
×
355
                nextStep = m_chaser->stepsCount() - 1;
×
356
            else
357
                nextStep = 0;
×
358
        }
359
    }
360
    else if (m_chaser->runOrder() == Function::Random)
×
361
    {
362
        nextStep = randomStepIndex(nextStep);
×
363
    }
364
    else // Ping Pong
365
    {
366
        // Change direction, but don't run the first/last step twice.
367
        if (m_direction == Function::Forward)
×
368
        {
369
            nextStep = m_chaser->stepsCount() - 2;
×
370
        }
371
        else // Backwards
372
        {
373
            nextStep = 1;
×
374
        }
375

376
        // Make sure we don't go beyond limits.
377
        nextStep = CLAMP(nextStep, 0, m_chaser->stepsCount() - 1);
×
378
    }
379

380
    return nextStep;
5 ✔
381
}
382

383
void ChaserRunner::shuffle(QVector<int> & data)
44 ✔
384
{
385
   int n = data.size();
44 ✔
386
   for (int i = n - 1; i > 0; --i)
127 ✔
387
   {
388
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
389
      qSwap(data[i], data[qrand() % (i + 1)]);
390
#else
391
      qSwap(data[i], data[QRandomGenerator::global()->generate() % (i + 1)]);
83 ✔
392
#endif
393
   }
394
}
44 ✔
395

396
int ChaserRunner::randomStepIndex(int step) const
×
397
{
398
   if (m_chaser->runOrder() == Function::Random && step >= 0 && step < m_order.size())
×
399
       return m_order[step];
×
400

401
   return step;
×
402
}
403

404
void ChaserRunner::fillOrder()
44 ✔
405
{
406
    fillOrder(m_chaser->stepsCount());
44 ✔
407
}
44 ✔
408

409
void ChaserRunner::fillOrder(int size)
44 ✔
410
{
411
   m_order.resize(size);
44 ✔
412
   for (int i = 0; i < size; ++i)
167 ✔
413
       m_order[i] = i;
123 ✔
414

415
   shuffle(m_order);
44 ✔
416
}
44 ✔
417

418
/****************************************************************************
419
 * Intensity
420
 ****************************************************************************/
421

422
void ChaserRunner::adjustStepIntensity(qreal fraction, int requestedStepIndex, int fadeControl)
12 ✔
423
{
424
    fraction = CLAMP(fraction, qreal(0.0), qreal(1.0));
12 ✔
425

426
    //qDebug() << "Adjust intensity" << fraction << "step:" << requestedStepIndex << "fade:" << fadeControl;
427

428
    int stepIndex = requestedStepIndex;
12 ✔
429
    if (stepIndex == -1)
12 ✔
430
    {
431
        // store the intensity to be applied at the next step startup
432
        m_pendingAction.m_masterIntensity = fraction;
10 ✔
433

434
        foreach (ChaserRunnerStep *step, m_runnerSteps)
16 ✔
435
        {
436
            if (step == NULL || step->m_function == NULL)
6 ✔
437
                continue;
×
438

439
            step->m_masterIntensity = fraction;
6 ✔
440
            if (step->m_function->type() == Function::SceneType)
6 ✔
441
            {
442
                Scene *scene = qobject_cast<Scene *>(step->m_function);
6 ✔
443
                scene->adjustAttribute(fraction, step->m_pIntensityOverrideId);
6 ✔
444
            }
445
            else
446
            {
447
                step->m_function->adjustAttribute(fraction * step->m_stepIntensity, step->m_intensityOverrideId);
×
448
            }
449
        }
10 ✔
450

451
        return;
10 ✔
452
    }
453

454
    foreach (ChaserRunnerStep *step, m_runnerSteps)
3 ✔
455
    {
456
        if (stepIndex == step->m_index && step->m_function != NULL)
1 ✔
457
        {
458
            step->m_stepIntensity = fraction;
×
459
            if (step->m_function->type() == Function::SceneType)
×
460
                step->m_function->adjustAttribute(fraction, step->m_intensityOverrideId);
×
461
            else
462
                step->m_function->adjustAttribute(step->m_masterIntensity * fraction, step->m_intensityOverrideId);
×
463
            return;
×
464
        }
465
    }
2 ✔
466

467
    // No need to start a new step if it is not wanted
468
    if (requestedStepIndex == -1)
2 ✔
469
        return;
×
470

471
    // Don't start a step with an intensity of zero
472
    if (fraction == qreal(0.0))
2 ✔
473
        return;
×
474

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

479
/****************************************************************************
480
 * Running
481
 ****************************************************************************/
482

483
void ChaserRunner::clearRunningList()
90 ✔
484
{
485
    // empty the running queue
486
    foreach (ChaserRunnerStep *step, m_runnerSteps)
137 ✔
487
    {
488
        if (step->m_function)
47 ✔
489
        {
490
            // restore the original Function fade out time
491
            step->m_function->setOverrideFadeOutSpeed(stepFadeOut(step->m_index));
47 ✔
492
            step->m_function->stop(functionParent(), m_chaser->type() == Function::SequenceType);
47 ✔
493
            m_lastFunctionID = step->m_function->type() == Function::SceneType ? step->m_function->id() : Function::invalidId();
47 ✔
494
        }
495
        delete step;
47 ✔
496
    }
90 ✔
497
    m_runnerSteps.clear();
90 ✔
498
}
90 ✔
499

500
void ChaserRunner::updateBeatPosition(MasterTimer *timer)
1,533 ✔
501
{
502
    int beatDuration = qMax(1, timer->beatTimeDuration());
1,533 ✔
503

504
    if (m_beatClockStarted == false)
1,533 ✔
505
    {
506
        // start in phase with the MasterTimer beat clock
507
        m_beatClockStarted = true;
32 ✔
508
        if (timer->isBeat())
32 ✔
509
            m_msSinceBeat = 0;
5 ✔
510
        else
511
            m_msSinceBeat = qBound(0, beatDuration - timer->timeToNextBeat(), beatDuration - 1);
27 ✔
512
    }
513
    else if (timer->isBeat())
1,501 ✔
514
    {
515
        m_lastBeatPosition += 1000;
19 ✔
516
        m_msSinceBeat = 0;
19 ✔
517
    }
518
    else if (m_msSinceBeat < beatDuration)
1,482 ✔
519
    {
520
        m_msSinceBeat += MasterTimer::tick();
1,375 ✔
521
    }
522

523
    // never reach the next whole beat before the beat actually happens
524
    qint64 phase = qMin(qint64(999), (qint64(m_msSinceBeat) * 1000) / beatDuration);
1,533 ✔
525
    m_beatPosition = m_lastBeatPosition + phase;
1,533 ✔
526
}
1,533 ✔
527

528
qint64 ChaserRunner::alignedBeatStart(uint duration) const
7 ✔
529
{
530
    qint64 phase = m_beatPosition - m_lastBeatPosition;
7 ✔
531

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

537
    // chaser start: extend the first step until the next beat, so that the
538
    // following steps fall on the beat grid
539
    if (m_firstStepStarted == false)
1 ✔
540
        return m_lastBeatPosition + 1000;
1 ✔
541

542
    return m_beatPosition;
×
543
}
544

545
void ChaserRunner::startNewStep(int index, MasterTimer *timer, qreal mIntensity, qreal sIntensity,
181 ✔
546
                                int fadeControl, quint32 elapsed, qint64 beatStart)
547
{
548
    if (m_chaser == NULL || m_chaser->stepsCount() == 0)
181 ✔
549
        return;
×
550

551
    if (index < 0 || index >= m_chaser->stepsCount())
181 ✔
552
        index = 0; // fallback to the first step
×
553

554
    ChaserStep step(m_chaser->steps().at(index));
181 ✔
555
    Function *func = m_doc->function(step.fid);
181 ✔
556
    if (func == NULL)
181 ✔
557
        return;
×
558

559
    ChaserRunnerStep *newStep = new ChaserRunnerStep();
181 ✔
560
    newStep->m_index = index;
181 ✔
561
    newStep->m_function = func;
181 ✔
562
    newStep->m_masterIntensity = mIntensity;
181 ✔
563
    newStep->m_stepIntensity = sIntensity;
181 ✔
564
    newStep->m_intensityOverrideId = Function::invalidAttributeId();
181 ✔
565
    newStep->m_pIntensityOverrideId = Function::invalidAttributeId();
181 ✔
566

567
    // check if blending between Scenes is needed
568
    if (m_lastFunctionID != Function::invalidId() &&
326 ✔
569
        func->type() == Function::SceneType)
145 ✔
570
    {
571
        Scene *scene = qobject_cast<Scene *>(func);
145 ✔
572
        scene->setBlendFunctionID(m_lastFunctionID);
145 ✔
573
    }
574

575
    // this happens only during crossfades
576
    if (m_runnerSteps.count())
181 ✔
577
    {
578
        ChaserRunnerStep *lastStep = m_runnerSteps.last();
1 ✔
579
        if (lastStep->m_function &&
3 ✔
580
            lastStep->m_function->type() == Function::SceneType &&
2 ✔
581
            func->type() == Function::SceneType)
1 ✔
582
        {
583
            Scene *lastScene = qobject_cast<Scene *>(lastStep->m_function);
1 ✔
584
            lastScene->setBlendFunctionID(Function::invalidId());
1 ✔
585
            Scene *scene = qobject_cast<Scene *>(func);
1 ✔
586
            scene->setBlendFunctionID(lastStep->m_function->id());
1 ✔
587
        }
588
    }
589

590
    switch (fadeControl)
181 ✔
591
    {
592
        case Chaser::FromFunction:
171 ✔
593
            newStep->m_fadeIn = stepFadeIn(index);
171 ✔
594
            newStep->m_fadeOut = stepFadeOut(index);
171 ✔
595
        break;
171 ✔
596
        case Chaser::Blended:
2 ✔
597
            newStep->m_fadeIn = stepFadeIn(index);
2 ✔
598
            newStep->m_fadeOut = stepFadeOut(index);
2 ✔
599
        break;
2 ✔
600
        case Chaser::Crossfade:
×
601
            newStep->m_fadeIn = 0;
×
602
            newStep->m_fadeOut = 0;
×
603
        break;
×
604
        case Chaser::BlendedCrossfade:
2 ✔
605
            newStep->m_fadeIn = 0;
2 ✔
606
            newStep->m_fadeOut = 0;
2 ✔
607
        break;
2 ✔
608
    }
609

610
    newStep->m_duration = stepDuration(index);
181 ✔
611

612
    if (m_startOffset != 0)
181 ✔
613
        newStep->m_elapsed = m_startOffset + MasterTimer::tick();
2 ✔
614
    else
615
        newStep->m_elapsed = MasterTimer::tick() + elapsed;
179 ✔
616

617
    /* Beats tempo: a step chained to the previous one starts exactly where the
618
     * previous one should have ended, so that steps don't accumulate drift.
619
     * Otherwise (or if that would end the step right away) align it to the beat */
620
    if (m_chaser->tempoType() == Function::Beats)
181 ✔
621
    {
622
        if (beatStart >= 0 && beatStart + qint64(newStep->m_duration) > m_beatPosition)
57 ✔
623
            newStep->m_beatStart = beatStart;
50 ✔
624
        else
625
            newStep->m_beatStart = alignedBeatStart(newStep->m_duration);
7 ✔
626
    }
627
    else
628
    {
629
        newStep->m_beatStart = m_beatPosition;
124 ✔
630
    }
631
    m_firstStepStarted = true;
181 ✔
632

633
    m_startOffset = 0;
181 ✔
634

635
    if (m_chaser->type() == Function::SequenceType)
181 ✔
636
    {
637
        Scene *s = qobject_cast<Scene*>(func);
×
638
        // blind == true is a workaround to reuse the same scene
639
        // without messing up the previous values
640
        for (int i = 0; i < step.values.count(); i++)
×
641
            s->setValue(step.values.at(i), true);
×
642
    }
643

644
    qDebug() << "[ChaserRunner] Starting step" << index << "fade in" << newStep->m_fadeIn
362 ✔
645
             << "fade out" << newStep->m_fadeOut << "intensity" << mIntensity
181 ✔
646
             << "fadeMode" << fadeControl;
181 ✔
647

648
    // Set intensity before starting the function. Otherwise the intensity
649
    // might momentarily jump too high.
650
    if (func->type() == Function::SceneType)
181 ✔
651
    {
652
        Scene *scene = qobject_cast<Scene *>(func);
181 ✔
653
        newStep->m_intensityOverrideId = func->requestAttributeOverride(Function::Intensity, sIntensity);
181 ✔
654
        newStep->m_pIntensityOverrideId = scene->requestAttributeOverride(Scene::ParentIntensity, mIntensity);
181 ✔
655
        qDebug() << "[ChaserRunner] Set step intensity:" << sIntensity << ", master:" << mIntensity;
181 ✔
656
    }
657
    else
658
    {
659
        newStep->m_intensityOverrideId = func->requestAttributeOverride(Function::Intensity, mIntensity * sIntensity);
×
660
    }
661

662
    // Start the fire up!
663
    func->start(timer, functionParent(), 0, newStep->m_fadeIn, newStep->m_fadeOut,
181 ✔
664
                func->defaultSpeed(), m_chaser->tempoType());
181 ✔
665
    m_runnerSteps.append(newStep);
181 ✔
666
    m_roundTime->restart();
181 ✔
667
}
181 ✔
668

669
int ChaserRunner::getNextStepIndex()
175 ✔
670
{
671
    int currentStepIndex = m_lastRunStepIdx;
175 ✔
672

673
    if (m_chaser->runOrder() == Function::Random)
175 ✔
674
    {
675
        currentStepIndex = m_order.indexOf(currentStepIndex);
×
676
        if (currentStepIndex == -1)
×
677
        {
678
            qDebug() << "[ChaserRunner] steps order not found";
×
679
            currentStepIndex = m_lastRunStepIdx;
×
680
        }
681
    }
682

683
    if (currentStepIndex == -1 &&
203 ✔
684
        m_chaser->direction() == Function::Backward)
28 ✔
685
            currentStepIndex = m_chaser->stepsCount();
6 ✔
686

687
    // Handle reverse Ping Pong at boundaries
688
    if (m_chaser->runOrder() == Function::PingPong &&
203 ✔
689
        m_pendingAction.m_action == ChaserPreviousStep)
28 ✔
690
    {
691
        if (currentStepIndex == 0)
×
692
            m_direction = Function::Backward;
×
693
        else if (currentStepIndex == m_chaser->stepsCount() - 1)
×
694
            m_direction = Function::Forward;
×
695
    }
696

697
    // Next step
698
    if (m_direction == Function::Forward)
175 ✔
699
    {
700
        // "Previous" for a forward chaser is -1
701
        if (m_pendingAction.m_action == ChaserPreviousStep)
141 ✔
702
            currentStepIndex--;
10 ✔
703
        else
704
            currentStepIndex++;
131 ✔
705
    }
706
    else
707
    {
708
        // "Previous" for a backward scene is +1
709
        if (m_pendingAction.m_action == ChaserPreviousStep)
34 ✔
710
            currentStepIndex++;
×
711
        else
712
            currentStepIndex--;
34 ✔
713
    }
714

715
    if (currentStepIndex < m_chaser->stepsCount() && currentStepIndex >= 0)
175 ✔
716
    {
717
        if (m_chaser->runOrder() == Function::Random)
133 ✔
718
        {
719
            currentStepIndex = randomStepIndex(currentStepIndex);
×
720
        }
721
        return currentStepIndex; // In the middle of steps. No need to go any further.
133 ✔
722
    }
723

724
    if (m_chaser->runOrder() == Function::SingleShot)
42 ✔
725
    {
726
        return -1; // Forward or Backward SingleShot has been completed.
4 ✔
727
    }
728
    else if (m_chaser->runOrder() == Function::Loop)
38 ✔
729
    {
730
        if (m_direction == Function::Forward)
30 ✔
731
        {
732
            if (currentStepIndex >= m_chaser->stepsCount())
28 ✔
733
                currentStepIndex = 0;
24 ✔
734
            else
735
                currentStepIndex = m_chaser->stepsCount() - 1; // Used by CueList with manual prev
4 ✔
736
        }
737
        else // Backward
738
        {
739
            if (currentStepIndex < 0)
2 ✔
740
                currentStepIndex = m_chaser->stepsCount() - 1;
2 ✔
741
            else
742
                currentStepIndex = 0;
×
743
        }
744
    }
745
    else if (m_chaser->runOrder() == Function::Random)
8 ✔
746
    {
747
        fillOrder();
×
748
        if (m_direction == Function::Forward)
×
749
        {
750
            if (currentStepIndex >= m_chaser->stepsCount())
×
751
                currentStepIndex = 0;
×
752
            else
753
                currentStepIndex = m_chaser->stepsCount() - 1; // Used by CueList with manual prev
×
754
        }
755
        else // Backward
756
        {
757
            if (currentStepIndex < 0)
×
758
                currentStepIndex = m_chaser->stepsCount() - 1;
×
759
            else
760
                currentStepIndex = 0;
×
761
        }
762
        // Don't run the same function 2 times in a row
763
        while (currentStepIndex < m_chaser->stepsCount()
×
764
                && randomStepIndex(currentStepIndex) == m_lastRunStepIdx)
×
765
            ++currentStepIndex;
×
766
        currentStepIndex = randomStepIndex(currentStepIndex);
×
767
    }
768
    else // Ping Pong
769
    {
770
        // Change direction, but don't run the first/last step twice.
771
        if (m_direction == Function::Forward)
8 ✔
772
        {
773
            currentStepIndex = m_chaser->stepsCount() - 2;
4 ✔
774
            m_direction = Function::Backward;
4 ✔
775
        }
776
        else // Backwards
777
        {
778
            currentStepIndex = 1;
4 ✔
779
            m_direction = Function::Forward;
4 ✔
780
        }
781

782
        // Make sure we don't go beyond limits.
783
        currentStepIndex = CLAMP(currentStepIndex, 0, m_chaser->stepsCount() - 1);
8 ✔
784
    }
785

786
    return currentStepIndex;
38 ✔
787
}
788

789
void ChaserRunner::setPause(bool enable, QList<Universe *> universes)
2 ✔
790
{
791
    // Nothing to do
792
    if (m_chaser->stepsCount() == 0)
2 ✔
793
        return;
×
794

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

797
    foreach (ChaserRunnerStep *step, m_runnerSteps)
4 ✔
798
        step->m_function->setPause(enable);
4 ✔
799

800
    // there might be a Scene fading out, so request pause
801
    // to faders bound to the Scene ID running on universes
802
    Function *f = m_doc->function(m_lastFunctionID);
2 ✔
803
    if (f != NULL && f->type() == Function::SceneType)
2 ✔
804
    {
805
        foreach (Universe *universe, universes)
10 ✔
806
            universe->setFaderPause(m_lastFunctionID, enable);
10 ✔
807
    }
808
}
809

810
FunctionParent ChaserRunner::functionParent() const
362 ✔
811
{
812
    return FunctionParent(FunctionParent::Function, m_chaser->id());
362 ✔
813
}
814

815
bool ChaserRunner::write(MasterTimer *timer, QList<Universe *> universes)
1,534 ✔
816
{
817
    // Nothing to do
818
    if (m_chaser->stepsCount() == 0)
1,534 ✔
819
        return false;
1 ✔
820

821
    updateBeatPosition(timer);
1,533 ✔
822

823
    // a step started by a pending action, before the running steps are
824
    // advanced below. It must not be advanced in the same write, as a step
825
    // started after them isn't, or it would run a tick ahead
826
    ChaserRunnerStep *startedStep = NULL;
1,533 ✔
827

828
    switch (m_pendingAction.m_action)
1,533 ✔
829
    {
830
        case ChaserNextStep:
19 ✔
831
        case ChaserPreviousStep:
832
            clearRunningList();
19 ✔
833
            // the actual action will be performed below, on startNewStep
834
        break;
19 ✔
835
        case ChaserSetStepIndex:
11 ✔
836
            if (m_pendingAction.m_stepIndex != -1)
11 ✔
837
            {
838
                clearRunningList();
8 ✔
839
                if (m_chaser->runOrder() == Function::Random)
8 ✔
840
                    m_lastRunStepIdx = randomStepIndex(m_pendingAction.m_stepIndex);
×
841
                else
842
                    m_lastRunStepIdx = m_pendingAction.m_stepIndex;
8 ✔
843

844
                qDebug() << "[ChaserRunner] Starting from step" << m_lastRunStepIdx << "@ offset" << m_startOffset;
8 ✔
845
                startNewStep(m_lastRunStepIdx, timer, m_pendingAction.m_masterIntensity,
8 ✔
846
                             m_pendingAction.m_stepIntensity, m_pendingAction.m_fadeMode);
847
                if (m_runnerSteps.isEmpty() == false)
8 ✔
848
                    startedStep = m_runnerSteps.last();
8 ✔
849
                emit currentStepChanged(m_lastRunStepIdx);
8 ✔
850
            }
851
        break;
11 ✔
852
        case ChaserPauseRequest:
2 ✔
853
            setPause(m_pendingAction.m_fadeMode ? true : false, universes);
2 ✔
854
        break;
2 ✔
855
        default:
1,501 ✔
856
        break;
1,501 ✔
857
    }
858

859
    quint32 prevStepRoundElapsed = 0;
1,533 ✔
860
    qint64 nextBeatStart = -1;
1,533 ✔
861

862
    foreach (ChaserRunnerStep *step, m_runnerSteps)
3,019 ✔
863
    {
864
        if (step == startedStep)
1,486 ✔
865
            continue;
8 ✔
866

867
        bool stepDone = false;
1,478 ✔
868

869
        if (step->m_duration != Function::infiniteSpeed())
1,478 ✔
870
        {
871
            if (m_chaser->tempoType() == Function::Time)
1,385 ✔
872
                stepDone = step->m_elapsed >= step->m_duration;
267 ✔
873
            else
874
                stepDone = m_beatPosition - step->m_beatStart >= qint64(step->m_duration);
1,118 ✔
875
        }
876

877
        if (stepDone)
1,478 ✔
878
        {
879
            if (m_chaser->tempoType() == Function::Beats)
128 ✔
880
                nextBeatStart = step->m_beatStart + step->m_duration;
50 ✔
881
            else if (step->m_duration != 0)
78 ✔
882
                prevStepRoundElapsed = step->m_elapsed % step->m_duration;
33 ✔
883

884
            m_lastFunctionID = step->m_function->type() == Function::SceneType ? step->m_function->id() : Function::invalidId();
128 ✔
885
            step->m_function->stop(functionParent(), m_chaser->type() == Function::SequenceType);
128 ✔
886
            m_runnerSteps.removeOne(step);
128 ✔
887
            delete step;
128 ✔
888
        }
889
        else
890
        {
891
            if (step->m_elapsed < UINT_MAX)
1,350 ✔
892
                step->m_elapsed += MasterTimer::tick();
1,350 ✔
893

894
            // When the speeds of the chaser change, they need to be updated to the lower
895
            // level (only current function) as well. Otherwise the new speeds would take
896
            // effect only on the next step change.
897
            if (m_updateOverrideSpeeds == true)
1,350 ✔
898
            {
899
                m_updateOverrideSpeeds = false;
×
900
                if (step->m_function != NULL)
×
901
                {
902
                    step->m_function->setOverrideFadeInSpeed(step->m_fadeIn);
×
903
                    step->m_function->setOverrideFadeOutSpeed(step->m_fadeOut);
×
904
                }
905
            }
906
        }
907
    }
1,533 ✔
908

909
    if (m_runnerSteps.isEmpty())
1,533 ✔
910
    {
911
        m_lastRunStepIdx = getNextStepIndex();
175 ✔
912
        if (m_lastRunStepIdx != -1)
175 ✔
913
        {
914
            int blend = m_pendingAction.m_action == ChaserNoAction ? Chaser::FromFunction : m_pendingAction.m_fadeMode;
171 ✔
915

916
            startNewStep(m_lastRunStepIdx, timer, m_pendingAction.m_masterIntensity,
171 ✔
917
                         m_pendingAction.m_stepIntensity, blend, prevStepRoundElapsed, nextBeatStart);
918
            emit currentStepChanged(m_lastRunStepIdx);
171 ✔
919
        }
920
        else
921
        {
922
            m_pendingAction.m_action = ChaserNoAction;
4 ✔
923
            return false;
4 ✔
924
        }
925
    }
926

927
    m_pendingAction.m_action = ChaserNoAction;
1,529 ✔
928
    return true;
1,529 ✔
929
}
930

931
void ChaserRunner::postRun(MasterTimer *timer, QList<Universe*> universes)
19 ✔
932
{
933
    Q_UNUSED(universes);
934
    Q_UNUSED(timer);
935

936
    qDebug() << Q_FUNC_INFO;
19 ✔
937
    clearRunningList();
19 ✔
938
}
19 ✔
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