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

ossia / score / 43892858954

16 Nov 2026 03:06PM UTC coverage: 61.472% (+0.3%) from 61.219%
43892858954

push

github

jcelerier
score: the open right-click box is exported in every build, so add-ons using score's knobs, sliders and spinboxes link on Windows

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KdPgufUMRpkKzaYvUs9iHW

261684 of 791694 branches covered (33.05%)

267913 of 435828 relevant lines covered (61.47%)

724440.84 hits per line

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

78.19
/src/plugins/score-plugin-engine/Execution/ExecutionController.cpp
1
#include "ExecutionController.hpp"
2

3
#include <LocalTree/LocalTreeDocumentPlugin.hpp>
4

5
#include <Explorer/DocumentPlugin/DeviceDocumentPlugin.hpp>
6
#include <Explorer/Explorer/DeviceExplorerModel.hpp>
7
#include <Explorer/Explorer/DeviceExplorerWidget.hpp>
8

9
#include <Scenario/Application/ScenarioActions.hpp>
10
#include <Scenario/Application/ScenarioApplicationPlugin.hpp>
11
#include <Scenario/Document/BaseScenario/BaseScenario.hpp>
12
#include <Scenario/Document/Interval/IntervalExecution.hpp>
13
#include <Scenario/Document/Interval/IntervalModel.hpp>
14
#include <Scenario/Document/ScenarioDocument/ScenarioDocumentModel.hpp>
15
#include <Scenario/Document/ScenarioDocument/ScenarioDocumentPresenter.hpp>
16
#include <Scenario/Execution/score2OSSIA.hpp>
17
#include <Scenario/Process/ScenarioExecution.hpp>
18
#include <Scenario/Process/ScenarioModel.hpp>
19

20
#include <Audio/AudioApplicationPlugin.hpp>
21
#include <Audio/Settings/Model.hpp>
22
#include <Execution/Clock/ClockFactory.hpp>
23
#include <Execution/DocumentPlugin.hpp>
24
#include <Execution/Settings/ExecutorModel.hpp>
25

26
#include <score/actions/ActionManager.hpp>
27
#include <score/model/ComponentUtils.hpp>
28
#include <score/tools/Bind.hpp>
29
#include <score/widgets/MessageBox.hpp>
30

31
#include <core/application/ApplicationSettings.hpp>
32
#include <core/document/Document.hpp>
33
#include <core/presenter/DocumentManager.hpp>
34

35
#include <ossia/dataflow/execution_state.hpp>
36
#include <ossia/editor/scenario/time_interval.hpp>
37
#include <ossia/editor/state/state.hpp>
38

39
#include <QGuiApplication>
40
#include <QMainWindow>
41

42
#include <Transport/DocumentPlugin.hpp>
43
#include <Transport/TransportInterface.hpp>
44

45
#include <wobjectimpl.h>
46
W_OBJECT_IMPL(Execution::ExecutionController)
13,567 !
47

48
/**
49
 * Execution state-machine explanation:
50
 *
51
 * * There are multiple sources for what controls the transport:
52
 * - From the GUI
53
 * - From the local tree or remote control plug-in
54
 * - From an external software (e.g. JACK transport)
55
 * - Automatically (e.g. --autoplay argument on command-line)
56
 *
57
 * * No matter the transport source, if there's a GUI its state must be consistent
58
 *   with what happens.
59
 *
60
 * Thus:
61
 * * The do_*** functions are requests
62
 * * The trigger_*** functions will perform the action and set the GUI state correctly
63
 * * The on_*** contain the actual implementation of the transport operation
64
 *
65
 * Notes on QAction:
66
 * * QAction::toggle(); -> will send toggled(true/false);
67
 * * QAction::trigger(); -> will send toggled(true/false); triggered(true/false);
68
 * * QAction::check(b); -> will send toggled(b);
69
 * * GUI: like QAction::trigger
70
 *
71
 * Thus, triggered can be used to differentiate between
72
 * * called from the GUI
73
 * * called from the software
74
 *
75
 * Example of call sequence:
76
 *
77
 * * Pressing the "global play button" (stop -> play):
78
 *   -> QAction::toggled(true)
79
 *     -> GUI changes (see TransportActions ctor)
80
 *   -> QAction::triggered(true)
81
 *     -> ApplicationPlugin::request_play_global(true)
82
 *     -> TransportInterface::requestPlay()
83
 *   -> the transport implementation eventually sends the "play()" signal
84
 *     -> &ApplicationPlugin::trigger_play_global
85
 *
86
 * * Pressing the "global play button" (play -> pause):
87
 *   -> QAction::toggled(false)
88
 *     -> GUI changes (see TransportActions ctor)
89
 *   -> QAction::triggered(false)
90
 *     -> ApplicationPlugin::request_play_global(false)
91
 *     -> TransportInterface::requestPause()
92
 *   -> the transport implementation eventually sends the "pause()" signal
93
 *     -> &ApplicationPlugin::trigger_pause()
94
 *
95
 * * Playing with "play from here":
96
 *   -> ApplicationPlugin::request_play(true, t)
97
 *   -> TransportInterface::requestTransport(t)
98
 *   -> TransportInterface::requestPlay
99
 *   -> The transport implementation eventually sends the "transport()" signal
100
 *     -> ??
101
 *   -> The transport implementation eventually sends the "play()" signal
102
 *     -> &ApplicationPlugin::trigger_play_global
103
 *
104
 * * Starting the transport from QJackCtl with a GUI
105
 *   -> the transport implementation eventually sends the "play()" signal
106
 *     -> &ApplicationPlugin::trigger_play_global
107
 *
108
 * * Starting the transport from QJackCtl without a GUI
109
 *   -> the transport implementation eventually sends the "play()" signal
110
 *     -> &ApplicationPlugin::trigger_play_global
111
 *
112
 * * Receiving a "/play" message to the local tree with a GUI
113
 *   -> ApplicationPlugin::request_play
114
 *   -> TransportInterface::requestPlay
115
 *   -> The transport implementation eventually sends the "play()" signal
116
 *     -> &ApplicationPlugin::trigger_play_global
117
 *
118
 * * Receiving a "/play" message to the local tree without a GUI
119
 */
120

121
namespace Execution
122
{
123
ExecutionController::ExecutionController(const score::GUIApplicationContext& ctx)
19,592 ✔
124
    : context{ctx}
4,898 ✔
125
    , m_scenario{ctx.guiApplicationPlugin<Scenario::ScenarioApplicationPlugin>()}
4,898 !
126
    , m_actions{m_scenario.transportActions()}
4,898 !
127
{
4,898 ✔
128
  if(ctx.applicationSettings.gui)
4,898 ✔
129
  {
130
    auto& acts = ctx.actions;
4,775 ✔
131
    using namespace Actions;
132
    connect(
4,775 !
133
        acts.action<Play>().action(), &QAction::triggered, this,
4,775 !
134
        &ExecutionController::request_play_local, Qt::QueuedConnection);
4,775 ✔
135

136
    connect(
4,775 !
137
        acts.action<PlayGlobal>().action(), &QAction::triggered, this,
4,775 !
138
        &ExecutionController::request_play_global, Qt::QueuedConnection);
4,775 ✔
139

140
    connect(
4,775 !
141
        acts.action<Stop>().action(), &QAction::triggered, this,
4,775 !
142
        &ExecutionController::request_stop, Qt::QueuedConnection);
4,775 ✔
143

144
    connect(
4,775 !
145
        acts.action<Reinitialize>().action(), &QAction::triggered, this,
4,775 !
146
        &ExecutionController::trigger_reinitialize, Qt::QueuedConnection);
4,775 ✔
147

148
    connect(
4,775 !
149
        &m_scenario.execution(), &Scenario::ScenarioExecution::playAtDate, this,
4,775 !
150
        qOverload<TimeVal>(&ExecutionController::request_play_from_here));
4,775 ✔
151
    connect(
4,775 !
152
        &m_scenario.execution(), &Scenario::ScenarioExecution::playIntervalAtDate,
4,775 !
153
        this, [this](Scenario::IntervalModel* itv, const TimeVal& t) {
4,775 ✔
154
      if(itv)
×
155
        request_play_from_here(*itv, t);
×
156
    });
×
157
    connect(
4,775 !
158
        &m_scenario.execution(), &Scenario::ScenarioExecution::beginScrub, this,
4,775 !
159
        &ExecutionController::request_begin_scrub);
4,775 ✔
160
    connect(
4,775 !
161
        &m_scenario.execution(), &Scenario::ScenarioExecution::scrub, this,
4,775 !
162
        &ExecutionController::request_scrub);
4,775 ✔
163
    connect(
4,775 !
164
        &m_scenario.execution(), &Scenario::ScenarioExecution::endScrub, this,
4,775 !
165
        &ExecutionController::request_end_scrub);
4,775 ✔
166
  }
4,775 ✔
167
}
4,898 ✔
168

169
ExecutionController::~ExecutionController()
4,897 ✔
170
{
4,897 ✔
171
  if(m_transport)
4,897 !
172
    m_transport->teardown();
4,897 !
173
}
4,897 ✔
174

175
TransportInterface& ExecutionController::transport() const noexcept
654 ✔
176
{
177
  return *m_transport;
654 ✔
178
}
179

180
void ExecutionController::request_play_global(bool b)
547 ✔
181
{
182
  if(b)
547 ✔
183
  {
184
    this->m_requestLocalPlay = false;
539 ✔
185
    m_transport->requestPlay();
539 ✔
186
  }
539 ✔
187
  else
188
  {
189
    m_transport->requestPause();
8 ✔
190
  }
191
}
547 ✔
192

193
void ExecutionController::request_play_local(bool b)
24 ✔
194
{
195
  if(b)
24 !
196
  {
197
    this->m_requestLocalPlay = true;
24 ✔
198
    m_transport->requestPlay();
24 ✔
199
  }
24 ✔
200
  else
201
  {
202
    m_transport->requestPause();
×
203
  }
204
}
24 ✔
205

206
void ExecutionController::request_play_interval(
3 ✔
207
    Scenario::IntervalModel& itv, exec_setup_fun setup, TimeVal t)
208
{
209
  m_intervalsToPlay.push_back({itv, std::move(setup), t});
3 !
210
  m_transport->requestPlay();
3 ✔
211
}
3 ✔
212

213
void ExecutionController::request_stop_interval(Scenario::IntervalModel& itv)
1 ✔
214
{
215
  stop_interval(itv);
1 ✔
216
}
1 ✔
217

218
void ExecutionController::request_stop()
6,938 ✔
219
{
220
  m_transport->requestStop();
6,938 ✔
221
}
6,938 ✔
222

223
void ExecutionController::trigger_play()
575 ✔
224
{
225
  if(!m_intervalsToPlay.empty())
575 ✔
226
  {
227
    m_actions.onPlayLocal();
3 ✔
228
    for(auto& to_play : m_intervalsToPlay)
6 ✔
229
      play_interval(to_play.interval, std::move(to_play.setup), to_play.t);
3 !
230
    m_intervalsToPlay.clear();
3 ✔
231
  }
3 ✔
232
  else if(this->m_requestLocalPlay)
572 ✔
233
  {
234
    m_actions.onPlayLocal();
24 ✔
235
    on_play_local(true);
24 ✔
236
  }
24 ✔
237
  else
238
  {
239
    m_actions.onPlayGlobal();
548 ✔
240
    on_play_global(true);
548 ✔
241
  }
242

243
  // Note: we set this here so that external "play" request are global
244
  // as it makes more sense:
245
  // local play is just for checking a sub-part of the score, but the external tools's times will
246
  // always be relative to the whole general score
247
  this->m_requestLocalPlay = false;
575 ✔
248
}
575 ✔
249

250
void ExecutionController::trigger_pause()
8 ✔
251
{
252
  m_actions.onPause();
8 ✔
253
  on_pause();
8 ✔
254
}
8 ✔
255

256
void ExecutionController::trigger_stop()
7,536 ✔
257
{
258
  m_actions.onStop();
7,536 ✔
259
  on_stop();
7,536 ✔
260

261
  // See note above
262
  this->m_requestLocalPlay = false;
7,536 ✔
263
}
7,536 ✔
264

265
void ExecutionController::trigger_reinitialize()
1 ✔
266
{
267
  m_actions.onStop();
1 ✔
268
  on_reinitialize();
1 ✔
269

270
  // See note above
271
  this->m_requestLocalPlay = false;
1 ✔
272
}
1 ✔
273

274
void ExecutionController::on_play_global(bool b)
548 ✔
275
{
276
  if(auto scenar = currentScenarioModel())
548 !
277
  {
278
    if(b)
548 !
279
    {
280
      play_interval(scenar->baseInterval(), {}, TimeVal::zero());
548 !
281
    }
548 ✔
282
    else
283
    {
284
      on_pause();
×
285
    }
286
  }
548 ✔
287
}
548 ✔
288

289
void ExecutionController::on_play_local(bool b, ::TimeVal t)
26 ✔
290
{
291
  if(auto scenar = currentScenarioPresenter();
26 !
292
     scenar && scenar->displayedElements.initialized())
26 !
293
  {
294
    if(b)
×
295
    {
296
      std::optional<TimeVal> startTime;
×
297
      auto& itv = scenar->displayedInterval();
×
298
      {
299
        if(itv.startMarker() != TimeVal::zero())
×
300
          startTime = itv.startMarker();
×
301
      }
302
      if(!startTime)
×
303
        if(t != TimeVal::zero())
×
304
          startTime = t;
×
305

306
      if(startTime && m_playing && m_clock)
×
307
      {
308
        if(m_clock->paused())
×
309
        {
310
          on_transport(*startTime);
×
311
        }
×
312
      }
×
313
      play_interval(scenar->displayedInterval(), {}, startTime ? *startTime : t);
×
314
    }
×
315
    else
316
    {
317
      on_pause();
×
318
    }
319
  }
×
320
  else if(auto scenar = currentScenarioModel(); scenar && b)
26 !
321
  {
322
    play_interval(scenar->baseInterval(), {}, t);
26 !
323
  }
26 ✔
324
  else
325
  {
326
    on_play_global(b);
×
327
  }
328
}
26 ✔
329

330
void ExecutionController::on_play_local(bool b)
24 ✔
331
{
332
  on_play_local(b, ::TimeVal::zero());
24 ✔
333
}
24 ✔
334

335
void ExecutionController::on_pause()
8 ✔
336
{
337
  ensure_audio_engine();
8 ✔
338

339
  if(m_clock)
8 ✔
340
  {
341
    m_clock->pause();
7 ✔
342
    m_paused = true;
7 ✔
343

344
    if(auto doc = currentDocument())
7 !
345
    {
346
      doc->context().execTimer.stop();
7 ✔
347

348
      if(auto transport_plug = doc->context().findPlugin<Transport::DocumentPlugin>())
7 !
349
        transport_plug->pause();
7 ✔
350
    }
7 ✔
351
  }
7 ✔
352
}
8 ✔
353

354
void ExecutionController::on_transport(TimeVal t)
3 ✔
355
{
356
  if(!m_clock)
3 ✔
357
    return;
2 ✔
358

359
  SCORE_ASSERT(m_clock->scenario);
1 !
360
  auto itv = m_clock->scenario->baseInterval().OSSIAInterval();
1 ✔
361
  if(!itv)
1 !
362
    return;
×
363

364
  auto& settings = context.settings<Execution::Settings::Model>();
1 !
365
  auto& ctx = m_clock->context;
1 ✔
366
  if(settings.getTransportValueCompilation())
1 !
367
  {
368
    auto execState = m_clock->context.execState;
×
369
    ctx.executionQueue.enqueue([execState, itv, time = m_clock->context.time(t)] {
×
370
      itv->offset(time);
×
371
      execState->commit();
×
372
    });
×
373
  }
×
374
  else
375
  {
376
    ctx.executionQueue.enqueue(
1 !
377
        [itv, time = m_clock->context.time(t)] { itv->transport(time); });
2 !
378
  }
379

380
  if(auto doc = currentDocument())
1 !
381
    if(auto transport_plug = doc->context().findPlugin<Transport::DocumentPlugin>())
2 !
382
      transport_plug->transport(t);
1 !
383
}
3 ✔
384

385
void ExecutionController::on_begin_scrub(TimeVal t)
×
386
{
387
  on_transport(t);
×
388
}
×
389

390
void ExecutionController::on_scrub(TimeVal t)
×
391
{
392
  on_transport(t);
×
393
}
×
394

395
void ExecutionController::on_end_scrub(TimeVal t)
×
396
{
397
  on_transport(t);
×
398
}
×
399

400
void ExecutionController::request_play_from_localtree(bool val)
×
401
{
402
  if(!m_playing && val)
×
403
  {
404
    // not playing, play requested
405
    request_play_local(val);
×
406
  }
×
407
  else if(m_playing)
×
408
  {
409
    if(m_paused == val)
×
410
    {
411
      // paused, play requested
412
      // or playing, pause requested
413
      request_play_local(val);
×
414
    }
×
415
  }
×
416
}
×
417

418
void ExecutionController::request_play_global_from_localtree(bool val)
×
419
{
420
  if(!m_playing && val)
×
421
  {
422
    // not playing, play requested
423
    request_play_global(val);
×
424
  }
×
425
  else if(m_playing)
×
426
  {
427
    if(m_paused == val)
×
428
    {
429
      // paused, play requested
430
      // or playing, pause requested
431
      request_play_global(val);
×
432
    }
×
433
  }
×
434
}
×
435

436
void ExecutionController::request_transport_from_localtree(TimeVal t)
2 ✔
437
{
438
  m_transport->requestTransport(t);
2 ✔
439
}
2 ✔
440

441
void ExecutionController::request_stop_from_localtree()
24 ✔
442
{
443
  trigger_stop();
24 ✔
444
}
24 ✔
445

446
void ExecutionController::request_reinitialize_from_localtree()
1 ✔
447
{
448
  trigger_reinitialize();
1 ✔
449
}
1 ✔
450

451
void ExecutionController::request_play_from_here(TimeVal t)
3 ✔
452
{
453
  if(m_clock)
3 ✔
454
  {
455
    m_transport->requestTransport(t);
1 ✔
456
  }
1 ✔
457
  else
458
  {
459
    on_play_local(true, t);
2 ✔
460

461
    // Syncs the transport buttons; actions only exist with a GUI.
462
    // FIXME this ends up calling play_interval again...
463
    if(this->context.applicationSettings.gui)
2 !
464
    {
465
      auto act = this->context.actions.action<Actions::Play>().action();
×
466
      act->trigger();
×
467
    }
×
468
  }
469
}
3 ✔
470

471
void ExecutionController::request_play_from_here(
4 ✔
472
    Scenario::IntervalModel& itv, TimeVal t)
473
{
474
  auto model = currentScenarioModel();
4 ✔
475
  const Scenario::IntervalModel* root = model ? &model->baseInterval() : nullptr;
4 !
476
  if(&itv == root)
4 !
477
    return request_play_from_here(t);
×
478

479
  if(!m_clock)
4 ✔
480
  {
481
    request_play_interval(itv, {}, t);
2 !
482
    return;
2 ✔
483
  }
484

485
  // The interval the clock plays; transport dates are relative to it.
486
  const Scenario::IntervalModel* played
2 ✔
487
      = m_clock->scenario ? &m_clock->scenario->baseInterval().scoreInterval() : root;
2 !
488
  if(&itv == played)
2 !
489
  {
490
    m_transport->requestTransport(t);
×
491
    return;
×
492
  }
493

494
  // The parent interval is running: restart only this interval at t.
495
  if(auto scenar = qobject_cast<Scenario::ProcessModel*>(itv.parent()))
2 !
496
  {
497
    auto parent = qobject_cast<Scenario::IntervalModel*>(scenar->parent());
2 ✔
498
    auto comp = score::findComponent<Execution::ScenarioComponentBase>(scenar->components());
2 ✔
499
    if(parent && parent->executing() && comp)
2 !
500
    {
501
      comp->playIntervalFrom(itv, t);
2 ✔
502
      return;
2 ✔
503
    }
504
  }
×
505

506
  // Otherwise transport the played interval to t, expressed in its time base.
507
  TimeVal date = t;
×
508
  const Scenario::IntervalModel* cur = &itv;
×
509
  while(cur && cur != played)
×
510
  {
511
    date += cur->date();
×
512
    cur = Scenario::closestParentInterval(cur->parent());
×
513
  }
514
  if(cur)
×
515
    m_transport->requestTransport(date);
×
516
}
4 ✔
517

518
void ExecutionController::request_begin_scrub(TimeVal t)
×
519
{
520
  if(m_clock)
×
521
  {
522
    m_transport->requestBeginScrub(t);
×
523
  }
×
524
  else
525
  {
526
    on_play_local(true, t);
×
527

528
    // FIXME this ends up calling play_interval again...
529
    if(this->context.applicationSettings.gui)
×
530
    {
531
      auto act = this->context.actions.action<Actions::Play>().action();
×
532
      act->trigger();
×
533
    }
×
534
  }
535
}
×
536

537
void ExecutionController::request_scrub(TimeVal t)
×
538
{
539
  if(m_clock)
×
540
  {
541
    m_transport->requestScrub(t);
×
542
  }
×
543
}
×
544

545
void ExecutionController::request_end_scrub(TimeVal t)
×
546
{
547
  if(m_clock)
×
548
  {
549
    m_transport->requestEndScrub(t);
×
550
  }
×
551
}
×
552

553
void ExecutionController::ensure_audio_engine()
586 ✔
554
{
555
  auto& audio_engine = this->context.guiApplicationPlugin<Audio::ApplicationPlugin>();
586 ✔
556
  if(!audio_engine.audio)
586 !
557
  {
558
    if(this->context.mainWindow)
×
559
    {
560
      score::warning(
×
561
          this->context.mainWindow, tr("Cannot play"),
×
562
          tr("Cannot start playback. It looks like the audio engine is not "
×
563
             "running.\n"
564
             "Check the audio settings in the software settings to ensure "
565
             "that a sound card "
566
             "is correctly configured.\n\n"
567
             "Check Settings > Audio > Device in particular. "
568
             "The power-on icon at the bottom of the transport toolbar will "
569
             "light up when the engine is running."));
570
      return;
×
571
    }
572
    else
573
    {
574
      qFatal("Cannot playback without an audio engine set up");
×
575
    }
576
  }
577
}
586 ✔
578

579
void ExecutionController::play_interval(
577 ✔
580
    Scenario::IntervalModel& cst, exec_setup_fun setup_fun, TimeVal t)
581
{
582
  auto doc = currentDocument();
577 ✔
583
  if(!doc)
577 !
584
    return;
×
585

586
  auto& ctx = doc->context();
577 ✔
587

588
  auto exec_plug = ctx.findPlugin<Execution::DocumentPlugin>();
577 ✔
589
  if(!exec_plug)
577 !
590
    return;
×
591

592
  ensure_audio_engine();
577 ✔
593

594
  if(m_playing)
577 ✔
595
  {
596
    SCORE_ASSERT(bool(m_clock));
32 !
597
    if(m_clock->paused())
32 ✔
598
    {
599
      m_clock->resume();
7 ✔
600
      m_paused = false;
7 ✔
601
    }
7 ✔
602
    else
603
    {
604
      // We are playing an interval while we are already executing.
605
      if(auto scenar = qobject_cast<Scenario::ProcessModel*>(cst.parent()))
25 ✔
606
      {
607
        auto exec_comp = score::findComponent<Execution::ScenarioComponentBase>(
1 ✔
608
            scenar->components());
1 ✔
609
        if(exec_comp)
1 !
610
        {
611
          exec_comp->playInterval(cst);
1 ✔
612
        }
1 ✔
613
        return;
1 ✔
614
      }
615
    }
616
  }
31 ✔
617
  else
618
  {
619
    // Here we stop the listening when we start playing the scenario.
620
    // Get all the selected nodes
621
    if(auto explorer = Explorer::try_deviceExplorerFromObject(*doc))
545 !
622
    {
623
      // Disable listening for everything
624
      if(explorer && !exec_plug->settings.getExecutionListening())
545 !
625
      {
626
        explorer->deviceModel().listening().stop();
×
627
      }
×
628

629
      if(this->context.applicationSettings.gui)
545 ✔
630
      {
631
        if(auto w = Explorer::findDeviceExplorerWidgetInstance(this->context))
468 ✔
632
        {
633
          w->setEditable(false);
294 ✔
634
        }
294 ✔
635
      }
468 ✔
636
    }
545 ✔
637

638
    // Ignores the top-level condition / triggers if any
639
    bool forcePlay{};
545 ✔
640
    if(this->context.applicationSettings.gui)
545 ✔
641
    {
642
      forcePlay = qApp->keyboardModifiers() & Qt::ControlModifier;
468 ✔
643
    }
468 ✔
644
    exec_plug->reload(forcePlay, cst);
545 ✔
645

646
    auto& c = exec_plug->context();
545 ✔
647
    m_clock = makeClock(c);
545 ✔
648

649
    if(setup_fun)
545 !
650
    {
651
      exec_plug->runAllCommands();
×
652
      SCORE_ASSERT(exec_plug->baseScenario());
×
653
      setup_fun(c, *exec_plug->baseScenario());
×
654
      exec_plug->runAllCommands();
×
655
    }
×
656

657
    m_clock->play(t);
545 ✔
658
    m_paused = false;
545 ✔
659
  }
660

661
  m_playing = true;
576 ✔
662
  m_playingDocument = &ctx.document;
576 ✔
663
  context.guiApplicationPlugin<Audio::ApplicationPlugin>().setPlayingDocument(&ctx.document);
576 ✔
664
  ctx.execTimer.start();
576 ✔
665

666
  if(auto transport_plug = ctx.findPlugin<Transport::DocumentPlugin>())
576 !
667
    transport_plug->play();
576 ✔
668

669
  playStarted();
576 ✔
670
}
577 ✔
671

672
void ExecutionController::stop_interval(Scenario::IntervalModel& cst)
1 ✔
673
{
674
  auto doc = currentDocument();
1 ✔
675
  if(!doc)
1 !
676
    return;
×
677

678
  auto& ctx = doc->context();
1 ✔
679

680
  auto exec_plug = ctx.findPlugin<Execution::DocumentPlugin>();
1 ✔
681
  if(!exec_plug)
1 !
682
    return;
×
683

684
  ensure_audio_engine();
1 ✔
685

686
  if(m_playing)
1 !
687
  {
688
    // We are stopping an interval while we are already executing.
689
    if(auto scenar = qobject_cast<Scenario::ProcessModel*>(cst.parent()))
1 !
690
    {
691
      auto exec_comp
1 ✔
692
          = score::findComponent<Execution::ScenarioComponentBase>(scenar->components());
1 ✔
693
      if(exec_comp)
1 !
694
      {
695
        exec_comp->stopInterval(cst);
1 ✔
696
      }
1 ✔
697
    }
1 ✔
698
  }
1 ✔
699
}
1 ✔
700

701
TimeVal ExecutionController::execution_time() const
18,203 ✔
702
{
703
  if(m_clock)
18,203 ✔
704
  {
705
    SCORE_ASSERT(m_clock->scenario);
4,670 !
706
    auto& itv = m_clock->scenario->baseInterval().scoreInterval().duration;
4,670 ✔
707
    return TimeVal(itv.defaultDuration() * itv.playPercentage());
4,670 ✔
708
  }
709
  return TimeVal::zero();
13,533 ✔
710
}
18,203 ✔
711

712
void ExecutionController::on_record(::TimeVal t)
1 ✔
713
{
714
  // Playback can start while a recording waits for its first message.
715
  if(m_playing)
1 !
716
    return;
×
717

718
  // TODO have a on_exit handler to properly stop the scenario.
719
  if(auto scenar = currentScenarioModel())
1 !
720
  {
721
    if(auto exec_plug = scenar->context().findPlugin<Execution::DocumentPlugin>())
1 !
722
    {
723
      // Listening isn't stopped here.
724
      exec_plug->reload(false, scenar->baseInterval());
1 ✔
725
      m_clock = makeClock(exec_plug->context());
1 ✔
726
      m_clock->play(t);
1 ✔
727

728
      scenar->context().execTimer.start();
1 ✔
729
      m_playing = true;
1 ✔
730
      m_playingDocument = &scenar->context().document;
1 ✔
731
      context.guiApplicationPlugin<Audio::ApplicationPlugin>().setPlayingDocument(
2 ✔
732
          &scenar->context().document);
1 ✔
733
      m_paused = false;
1 ✔
734

735
      if(auto transport_plug = scenar->context().findPlugin<Transport::DocumentPlugin>())
1 !
736
        transport_plug->play();
1 ✔
737
      playStarted();
1 ✔
738
    }
1 ✔
739
  }
1 ✔
740
}
1 ✔
741

742
void ExecutionController::on_stop()
7,537 ✔
743
{
744
  LocalTree::DocumentPlugin* tree{};
7,537 ✔
745
  if(auto doc = currentDocument())
7,537 ✔
746
    tree = doc->context().findPlugin<LocalTree::DocumentPlugin>();
5,375 ✔
747
  if(tree)
7,537 ✔
748
    tree->beginRecall(LocalTree::Recall::Stop);
5,375 ✔
749

750
  bool wasplaying = m_playing;
7,537 ✔
751
  m_playing = false;
7,537 ✔
752
  m_paused = false;
7,537 ✔
753

754
  stop_clock();
7,537 ✔
755

756
  if(wasplaying)
7,537 ✔
757
    send_end_state();
546 ✔
758

759
  reset_after_stop();
7,537 ✔
760

761
  if(tree)
7,537 ✔
762
    tree->endRecall(LocalTree::Recall::Stop);
5,375 ✔
763

764
  m_playingDocument = nullptr;
7,537 ✔
765
  context.guiApplicationPlugin<Audio::ApplicationPlugin>().setPlayingDocument(nullptr);
7,537 ✔
766

767
  if(wasplaying)
7,537 ✔
768
    playStopped();
546 ✔
769
}
7,537 ✔
770

771
void ExecutionController::stop_clock()
7,537 ✔
772
{
773
  if(m_clock)
7,537 ✔
774
  {
775
    auto clock = std::move(m_clock);
546 ✔
776
    m_clock.reset();
546 ✔
777
    if(auto scenar = currentScenarioModel())
546 !
778
      if(auto exec_plug = scenar->context().findPlugin<Execution::DocumentPlugin>();
546 !
779
         bool(exec_plug))
1,092 ✔
780
      {
781
        try
782
        {
783
          clock->stop();
546 !
784
        }
546 ✔
785
        catch(...)
786
        {
787
          qDebug() << "Error while stopping the clock. There is likely an audio "
×
788
                      "hardware issue.";
789
        }
×
790
      }
546 ✔
791
  }
546 ✔
792
}
7,537 ✔
793

794
void ExecutionController::send_end_state()
546 ✔
795
{
796
  if(auto doc = currentDocument())
546 !
797
  {
798
    auto& ctx = doc->context();
546 ✔
799
    if(auto exec_plug = ctx.findPlugin<Execution::DocumentPlugin>())
546 !
800
    {
801
      exec_plug->playStopState();
546 ✔
802
    }
546 ✔
803
  }
546 ✔
804
}
546 ✔
805
void ExecutionController::reset_after_stop()
7,537 ✔
806
{
807
  if(auto doc = currentDocument())
7,537 ✔
808
  {
809
    auto& ctx = doc->context();
5,375 ✔
810
    ctx.execTimer.stop();
5,375 ✔
811

812
    if(auto transport_plug = ctx.findPlugin<Transport::DocumentPlugin>())
5,375 !
813
      transport_plug->stop();
5,375 ✔
814

815
    if(auto exec_plug = ctx.findPlugin<Execution::DocumentPlugin>())
5,375 !
816
    {
817
      exec_plug->clear();
5,375 ✔
818
    }
5,375 ✔
819
    else
820
    {
821
      return;
×
822
    }
823

824
    // If we can we resume listening
825
    if(!context.docManager.preparingNewDocument())
5,375 ✔
826
    {
827
      auto explorer = Explorer::try_deviceExplorerFromObject(*doc);
4,152 ✔
828
      if(explorer)
4,152 !
829
        explorer->deviceModel().listening().restore();
4,152 ✔
830
    }
4,152 ✔
831
  }
5,375 ✔
832

833
  auto scenar = currentScenarioModel();
7,537 ✔
834
  if(!scenar || scenar->closing())
7,537 ✔
835
    return;
2,790 ✔
836
  QTimer::singleShot(
4,747 !
837
      50, this,
838
      [this, itv = QPointer<Scenario::IntervalModel>{&scenar->baseInterval()}] {
8,982 ✔
839
    if(itv)
4,235 ✔
840
      reset_edition(*itv);
1,706 ✔
841
      });
4,235 ✔
842
}
7,537 ✔
843

844
void ExecutionController::reset_edition(Scenario::IntervalModel& base)
1,706 ✔
845
{
846
  // Reset edition for the device explorer
847
  if(context.applicationSettings.gui)
1,706 ✔
848
  {
849
    if(auto w = Explorer::findDeviceExplorerWidgetInstance(context))
1,682 ✔
850
    {
851
      w->setEditable(true);
1,174 ✔
852
    }
1,174 ✔
853
  }
1,682 ✔
854

855
  // FIXME uuuugh have an event in scenario instead (or better, move this
856
  // in process)
857
  base.reset();
1,706 ✔
858
  base.executionEvent(Scenario::IntervalExecutionEvent::Finished);
1,706 ✔
859
}
1,706 ✔
860

861
void ExecutionController::on_reinitialize()
1 ✔
862
{
863
  // TODO to be more precise, we should stop the execution, but keep
864
  // the execution_state alive until the last message is sent
865
  if(auto scenar = currentScenarioModel())
1 !
866
  {
867
    auto& ctx = scenar->context();
1 ✔
868
    auto& doc = ctx.document;
1 ✔
869
    auto exec_plug = ctx.findPlugin<Execution::DocumentPlugin>();
1 ✔
870
    if(!exec_plug)
1 !
871
      return;
×
872

873
    auto explorer = Explorer::try_deviceExplorerFromObject(doc);
1 ✔
874

875
    // Disable listening for everything
876
    if(explorer)
1 !
877
      if(!ctx.app.settings<Execution::Settings::Model>().getExecutionListening())
1 !
878
        explorer->deviceModel().listening().stop();
×
879

880
    // If we can we resume listening
881
    if(explorer)
1 !
882
      if(!context.docManager.preparingNewDocument())
2 !
883
        explorer->deviceModel().listening().restore();
1 ✔
884

885
    on_stop();
1 ✔
886

887
    LocalTree::StateRecall recall{ctx, *exec_plug};
1 ✔
888
    exec_plug->playStartState();
1 !
889
  }
1 ✔
890
}
1 ✔
891

892
void ExecutionController::init_transport()
4,898 ✔
893
{
894
  if(m_transport)
4,898 !
895
    m_transport->teardown();
×
896

897
  auto& s = context.settings<Execution::Settings::Model>();
4,898 ✔
898
  m_transport = s.getTransport();
4,898 ✔
899
  SCORE_ASSERT(m_transport);
4,898 !
900
  m_transport->setup();
4,898 ✔
901
  connect(
4,898 ✔
902
      m_transport, &Execution::TransportInterface::play, this,
4,898 ✔
903
      &ExecutionController::trigger_play);
4,898 ✔
904
  connect(
4,898 ✔
905
      m_transport, &Execution::TransportInterface::pause, this,
4,898 ✔
906
      &ExecutionController::trigger_pause);
4,898 ✔
907
  connect(
4,898 ✔
908
      m_transport, &Execution::TransportInterface::stop, this,
4,898 ✔
909
      &ExecutionController::trigger_stop);
4,898 ✔
910
  connect(
4,898 ✔
911
      m_transport, &Execution::TransportInterface::transport, this,
4,898 ✔
912
      &ExecutionController::on_transport);
4,898 ✔
913
  connect(
4,898 ✔
914
      m_transport, &Execution::TransportInterface::beginScrub, this,
4,898 ✔
915
      &ExecutionController::on_begin_scrub);
4,898 ✔
916
  connect(
4,898 ✔
917
      m_transport, &Execution::TransportInterface::scrub, this,
4,898 ✔
918
      &ExecutionController::on_scrub);
4,898 ✔
919
  connect(
4,898 ✔
920
      m_transport, &Execution::TransportInterface::endScrub, this,
4,898 ✔
921
      &ExecutionController::on_end_scrub);
4,898 ✔
922

923
  auto& audio_settings = this->context.settings<Audio::Settings::Model>();
4,898 ✔
924
  con(audio_settings, &Audio::Settings::Model::JackTransportChanged, this,
9,796 ✔
925
      &ExecutionController::init_transport, Qt::UniqueConnection);
4,898 ✔
926

927
  con(audio_settings, &Audio::Settings::Model::changed, this,
9,796 ✔
928
      &ExecutionController::on_audioSettingsChanged,
4,898 ✔
929
      Qt::ConnectionType(Qt::DirectConnection | Qt::UniqueConnection));
4,898 ✔
930

931
  // Restarting the engine from its action, rebinding it to another
932
  // document's audio device, the device failing: synchronous, so that the
933
  // execution is stopped before the engine changes.
934
  con(context.guiApplicationPlugin<Audio::ApplicationPlugin>(),
9,796 ✔
935
      &Audio::ApplicationPlugin::engineAboutToChange, this,
4,898 ✔
936
      &ExecutionController::on_audioSettingsChanged,
4,898 ✔
937
      Qt::ConnectionType(Qt::DirectConnection | Qt::UniqueConnection));
4,898 ✔
938
}
4,898 ✔
939

940
void ExecutionController::on_audioSettingsChanged()
4,408 ✔
941
{
942
  // The engine is about to be restarted, which tears the audio device tree
943
  // down under the graph. Stopping here is synchronous, hence done before the
944
  // audio plug-in's queued restart.
945
  if(m_playing)
4,408 ✔
946
    trigger_stop();
16 ✔
947
}
4,408 ✔
948

949
Scenario::ScenarioDocumentModel* ExecutionController::currentScenarioModel()
8,663 ✔
950
{
951
  if(auto doc = currentDocument())
8,663 ✔
952
  {
953
    return score::IDocument::try_modelDelegate<Scenario::ScenarioDocumentModel>(*doc);
6,501 ✔
954
  }
955
  return nullptr;
2,162 ✔
956
}
8,663 ✔
957

958
Scenario::ScenarioDocumentPresenter* ExecutionController::currentScenarioPresenter()
26 ✔
959
{
960
  if(auto doc = currentDocument())
26 !
961
  {
962
    return score::IDocument::try_presenterDelegate<Scenario::ScenarioDocumentPresenter>(
26 ✔
963
        *doc);
26 ✔
964
  }
965
  return nullptr;
×
966
}
26 ✔
967

968
score::Document* ExecutionController::currentDocument() const
24,895 ✔
969
{
970
  if(m_playingDocument)
24,895 ✔
971
    return m_playingDocument;
2,805 ✔
972
  return context.documents.currentDocument();
22,090 ✔
973
}
24,895 ✔
974

975
std::unique_ptr<Execution::Clock>
976
ExecutionController::makeClock(const Execution::Context& ctx)
546 ✔
977
{
978
  auto& s = context.settings<Execution::Settings::Model>();
546 ✔
979
  auto clk = s.makeClock(ctx);
546 ✔
980
  SCORE_ASSERT(clk->scenario);
546 !
981
  auto& itv = clk->scenario->baseInterval().interval();
546 !
982
  con(itv, &IdentifiedObjectAbstract::identified_object_destroying, this,
1,092 !
983
      [this] { trigger_stop(); });
1,092 ✔
984
  return clk;
546 ✔
985
}
546 ✔
986

987
}
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