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

ossia / score / 40987115292

30 Oct 2026 01:27AM UTC coverage: 51.229% (+2.1%) from 49.178%
40987115292

push

github

jcelerier
avendish: main

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

179452 of 350293 relevant lines covered (51.23%)

175809.48 hits per line

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

59.33
/src/plugins/score-plugin-engine/Execution/ExecutionTick.cpp
1
#include <Process/ExecutionCommand.hpp>
2

3
#include <Scenario/Document/Interval/IntervalExecution.hpp>
4

5
#include <Audio/AudioApplicationPlugin.hpp>
6
#include <Audio/AudioTick.hpp>
7
#include <Execution/BaseScenarioComponent.hpp>
8
#include <Execution/DocumentPlugin.hpp>
9
#include <Execution/ExecutionController.hpp>
10

11
#include <ossia/audio/audio_protocol.hpp>
12
#include <ossia/dataflow/execution_state.hpp>
13
#include <ossia/dataflow/graph/graph_interface.hpp>
14
#include <ossia/dataflow/graph/tick_setup.hpp>
15
#include <ossia/editor/scenario/execution_log.hpp>
16
#include <ossia/editor/scenario/scenario.hpp>
17
#include <ossia/editor/scenario/time_interval.hpp>
18

19
#include <Transport/TransportInterface.hpp>
20

21
#include <QElapsedTimer>
22

23
namespace Execution
24
{
25

26
namespace
27
{
28
struct AudioTickHelper
29
{
30
#if defined(OSSIA_EXECUTION_LOG)
31
  std::shared_ptr<ossia::on_destruct> log_context
32
      = std::make_shared<ossia::on_destruct>(ossia::g_exec_log.init());
33
#endif
34
  AudioTickHelper(
1,053 ✔
35
      ossia::tick_setup_options opt, Execution::DocumentPlugin& plug,
36
      const std::shared_ptr<Execution::BaseScenarioElement>& scenar)
37
      : m_scenar{scenar}
351 ✔
38
      , m_context{plug.contextData()}
351 ✔
39
      , m_itv{*scenar->baseInterval().OSSIAInterval()}
351 ✔
40
      , m_proto{plug.audioProto()}
351 ✔
41
      , m_actions{plug.actions()}
351 ✔
42
      , m_engine{plug.context()
351 ✔
43
                     .doc.app.guiApplicationPlugin<Audio::ApplicationPlugin>()
351 ✔
44
                     .audio}
351 ✔
45
  {
46
    m_tick = ossia::make_tick(
351 ✔
47
        opt, *m_context->execState, *m_context->execGraph, m_itv, scenar->baseScenario(),
351 ✔
48
        plug.executionController().transport().transportUpdateFunction());
351 ✔
49

50
    for(Execution::ExecutionAction& act :
704 ✔
51
        plug.context().doc.app.interfaces<Execution::ExecutionActionList>())
351 ✔
52
    {
53
      m_actions.push_back(&act);
353 ✔
54
    }
55
  }
351 ✔
56

57
  ~AudioTickHelper()
351 ✔
58
  {
59
    OSSIA_ENSURE_CURRENT_THREAD_KIND(ossia::thread_type::Ui);
351 ✔
60
    m_scenar->cleanup();
351 ✔
61
    auto& q = m_context->m_execQueue;
351 ✔
62
    // Who gets to release the graph: the audio thread, by running the command
63
    // below, or this destructor, if the wait below gives up first.
64
    enum release_state : int
65
    {
66
      Pending,
67
      Running,
68
      Done,
69
      Abandoned
70
    };
71
    auto release = std::make_shared<std::atomic_int>(Pending);
351 ✔
72
    // Weak: this command sits in the ContextData's own queue; if never run, a
73
    // strong reference would keep the ContextData alive through that queue.
74
    q.enqueue([weak_ctx = std::weak_ptr{m_context}, graph = m_context->execGraph,
1,053 ✔
75
               release]() mutable {
351 ✔
76
      OSSIA_ENSURE_CURRENT_THREAD_KIND(ossia::thread_type::Audio);
351 ✔
77
      int expected = Pending;
351 ✔
78
      if(!release->compare_exchange_strong(expected, Running))
351 ✔
79
        return;
×
80
      auto ctx = weak_ctx.lock();
351 ✔
81
      if(ctx)
351 ✔
82
      {
83
        // FIXME this frees the graph nodes in the audio thread,
84
        // should do it in exec thread.
85
        // FIXME why not just move all the structures back to the main thread the moment we hit stop?
86
        graph->clear();
351 ✔
87
        ctx->m_gcQueue.enqueue(gc(std::move(graph), std::move(ctx)));
351 ✔
88
      }
351 ✔
89
      *release = Done;
351 ✔
90
    });
351 ✔
91
    m_context->execGraph.reset();
351 ✔
92

93
    auto ptr = std::make_shared<std::atomic_bool>();
351 ✔
94
    q.enqueue([ptr] { *ptr = true; });
702 ✔
95
    m_scenar.reset();
351 ✔
96

97
    // The teardown queued above reads the devices the ports are bound to, and
98
    // whoever stops the execution may destroy them as soon as this returns: a
99
    // closing document closes its devices right after. The audio thread runs
100
    // the queue once per buffer, so the wait is measured in buffers, not in
101
    // spins: a fast machine spins through any fixed count within one large
102
    // buffer. A stopped engine runs nothing, so there is nothing to wait for.
103
    const auto engine = m_engine.lock();
351 ✔
104
    if(engine && engine->running())
351 ✔
105
    {
106
      const auto& st = *m_context->execState;
351 ✔
107
      const double buffer_ms
351 ✔
108
          = st.sampleRate > 0 ? 1000. * st.bufferSize / st.sampleRate : 0.;
351 ✔
109
      const qint64 timeout_ms = 1000 + qint64(8. * buffer_ms);
351 ✔
110
      QElapsedTimer t;
351 ✔
111
      t.start();
351 ✔
112
      while(!*ptr && t.elapsed() < timeout_ms && engine->running())
7,849,151 ✔
113
      {
114
        std::this_thread::yield();
3,924,400 ✔
115
        m_context->processEditCommands();
3,924,400 ✔
116
      }
117
      if(!*ptr)
351 ✔
118
        qDebug() << "Execution: the audio thread did not run the teardown in time";
×
119
    }
351 ✔
120
    // The wait gave up: take the release command back from the audio thread,
121
    // or, if it is running now, let it finish so the drain below sees its gc.
122
    if(!*ptr)
351 ✔
123
    {
124
      int expected = Pending;
×
125
      if(!release->compare_exchange_strong(expected, Abandoned))
×
126
        while(release->load() != Done)
×
127
          std::this_thread::yield();
×
128
    }
×
129

130
    // Once more after seeing *ptr: the release may have left gc(graph, ctx) in
131
    // the GC queue, a strong reference that would keep the ContextData alive.
132
    m_context->processEditCommands();
351 ✔
133
    m_context.reset();
351 ✔
134
  }
351 ✔
135

136
  void clearBuffers(const ossia::audio_tick_state& t) const
35,193 ✔
137
  {
138
    // Clear buffers as some APIs are nastyyyy
139
    for(int chan = 0; chan < t.n_out; chan++)
35,193 ✔
140
    {
141
      float* c = t.outputs[chan];
×
142
      for(std::size_t i = 0; i < t.frames; i++)
×
143
      {
144
        c[i] = 0.f;
×
145
      }
×
146
    }
×
147
  }
35,193 ✔
148

149
  void dequeueCommands() const
35,193 ✔
150
  {
151
    OSSIA_ENSURE_CURRENT_THREAD_KIND(ossia::thread_type::Audio);
35,193 ✔
152
    // Run some commands if they have been submitted.
153
    Execution::ExecutionCommand c;
35,193 ✔
154
    while(m_context->m_execQueue.try_dequeue(c))
56,138 ✔
155
    {
156
      try
157
      {
158
        c();
20,945 ✔
159
        m_context->m_gcQueue.enqueue(gc(std::move(c)));
20,945 ✔
160
      }
20,945 ✔
161
      catch(...)
162
      {
163
      }
×
164
    }
165
  }
35,193 ✔
166

167
  void main_tick(const ossia::audio_tick_state& t) const
35,193 ✔
168
  {
169
    OSSIA_ENSURE_CURRENT_THREAD_KIND(ossia::thread_type::Audio);
35,193 ✔
170
    // TODO this means that transport isn't visible until we play again
171
    if(t.status && *t.status != ossia::transport_status::playing)
35,193 ✔
172
      return;
×
173

174
    // Transport if necessary
175
    if(t.position_in_frames)
35,193 ✔
176
    {
177
      auto cur = *t.position_in_frames;
×
178
      if(m_prev_frame)
×
179
      {
180
        auto prev = *m_prev_frame;
×
181
        if(prev + t.frames == cur)
×
182
        {
183
          // Normal playback
184
          m_tick(t.frames, t.seconds);
×
185
        }
×
186
        else if(prev == cur)
×
187
        {
188
          // Pause, do not advance the score execution
189
        }
×
190
        else
191
        {
192
          // transport
193
          // TODO here we must multiply by root_tempo / tempo  ... if we have  a fixed tempo !
194
          // If we have a tempo map we have to integrate over it to compute which logical duration
195
          // maps to which physical duration.
196
          const auto flicks = cur * m_context->execState->samplesToModelRatio;
×
197
          m_itv.transport(ossia::time_value{int64_t(flicks)});
×
198

199
          m_tick(t.frames, t.seconds);
×
200
        }
201
      }
×
202
      else
203
      {
204
        // first time this is called, m_prev_frame not initialized yet
205
        if(cur != 0)
×
206
        {
207
          // transport to pur ourselves aligned with the global clock
208
          const auto flicks = cur * m_context->execState->samplesToModelRatio;
×
209
          m_itv.transport(ossia::time_value{int64_t(flicks)});
×
210
        }
×
211

212
        m_tick(t.frames, t.seconds);
×
213
      }
214
      m_prev_frame = *t.position_in_frames;
×
215
    }
×
216
    else
217
    {
218
      // No transport information, normal playback
219
      m_tick(t.frames, t.seconds);
35,193 ✔
220
    }
221
  }
35,193 ✔
222

223
  void main(const ossia::audio_tick_state& t) const
35,193 ✔
224
  try
225
  {
226
    OSSIA_ENSURE_CURRENT_THREAD_KIND(ossia::thread_type::Audio);
35,193 ✔
227
    // Match the audio_protocol with the actual I/O
228
    m_proto->setup_buffers(t);
35,193 ✔
229

230
    // The actual tick
231
    for(auto act : m_actions)
105,591 ✔
232
      act->startTick(t);
70,398 ✔
233

234
    main_tick(t);
35,193 ✔
235

236
    for(auto act : m_actions)
105,591 ✔
237
      act->endTick(t);
70,398 ✔
238
  }
35,193 ✔
239
  catch(...)
240
  {
241
  }
35,193 ✔
242

243
  std::shared_ptr<Execution::BaseScenarioElement> m_scenar;
244
  std::shared_ptr<Execution::DocumentPlugin::ContextData> m_context;
245

246
  ossia::time_interval& m_itv;
247
  smallfun::function<void(unsigned long, double), 128> m_tick;
248
  std::shared_ptr<ossia::audio_protocol> m_proto;
249
  std::vector<ExecutionAction*> m_actions;
250
  //! The engine that runs this tick, to wait for it at teardown
251
  std::weak_ptr<ossia::audio_engine> m_engine;
252

253
  mutable std::optional<uint64_t> m_prev_frame;
254
};
255
}
256

257
Audio::tick_fun makeExecutionTick(
351 ✔
258
    ossia::tick_setup_options opt, Execution::DocumentPlugin& plug,
259
    const std::shared_ptr<Execution::BaseScenarioElement>& scenar)
260
{
261
  return [helper = std::make_shared<AudioTickHelper>(opt, plug, scenar)](
35,544 ✔
262
             const ossia::audio_tick_state& t) {
263
    Audio::execution_status.store(ossia::transport_status::playing);
35,193 ✔
264
    Audio::execution_samples.fetch_add(t.frames, std::memory_order_release);
35,193 ✔
265

266
    helper->clearBuffers(t);
35,193 ✔
267
    helper->dequeueCommands();
35,193 ✔
268
    helper->main(t);
35,193 ✔
269
  };
35,193 ✔
270
}
271

272
Audio::tick_fun makeBenchmarkTick(
×
273
    ossia::tick_setup_options opt, Execution::DocumentPlugin& plug,
274
    const std::shared_ptr<Execution::BaseScenarioElement>& scenar)
275
{
276
  int i = 0;
×
277
  QPointer<Execution::DocumentPlugin> plugPtr = &plug;
×
278
  return [helper = std::make_shared<AudioTickHelper>(opt, plug, scenar), plugPtr,
×
279
          i](const ossia::audio_tick_state& t) mutable {
×
280
    Audio::execution_status.store(ossia::transport_status::playing);
×
281
    Audio::execution_samples.fetch_add(t.frames, std::memory_order_release);
×
282

283
    helper->clearBuffers(t);
×
284
    helper->dequeueCommands();
×
285

286
    auto& bench = *helper->m_context->bench;
×
287
    if(i % 50 == 0)
×
288
    {
289
      bench.measure = true;
×
290
      auto t0 = std::chrono::steady_clock::now();
×
291

292
      helper->main(t);
×
293

294
      auto t1 = std::chrono::steady_clock::now();
×
295
      auto total = std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count();
×
296
#if !defined(_MSC_VER)
297
      //FIXME: MSVC unordered_map isn't move noexcept so it does not work here in Debug
298
      helper->m_context->m_editionQueue.enqueue([plugPtr, bench, total]() mutable {
×
299
        if(plugPtr)
×
300
          plugPtr->slot_bench(std::move(bench), total);
×
301
      });
×
302
#endif
303

304
      for(auto& p : bench)
×
305
      {
306
        p.second = {};
×
307
      }
308
    }
×
309
    else
310
    {
311
      bench.measure = false;
×
312

313
      helper->main(t);
×
314
    }
315

316
    i++;
×
317
  };
×
318
}
×
319
}
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