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

ossia / libossia / 43737844130

15 Nov 2026 04:51PM UTC coverage: 52.708% (+0.4%) from 52.341%
43737844130

push

github

jcelerier
dataflow: a node reads the transport position of the tick it runs

transport_frames() is documented as the position of the first sample of
the current tick, but process_time() set it after the node had run:
during run() a node read the position of the previous tick, a block
late, and after a loop or a seek the first tick still had the old one.
The VST 2, VST3 and LV2 hosts of score hand it to plug-ins. Every graph
now sets it in begin_request(), before run(), and process_time() only
counts the frames.

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

130 of 133 new or added lines in 4 files covered. (97.74%)

3 existing lines in 2 files now uncovered.

32724 of 62085 relevant lines covered (52.71%)

205057.03 hits per line

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

98.32
/tests/Dataflow/TransportPositionTest.cpp
1
// The transport position a node reads while it runs: that of the first sample
2
// of the tick being run, whichever graph runs it, through a play from 0, a
3
// tick that starts inside its buffer, a loop, a seek, other speeds and
4
// backwards play; and at any speed, the one the musical position and the
5
// tempo of the tick say.
6
#include <ossia/detail/config.hpp>
7

8
#include <ossia/dataflow/execution_state.hpp>
9
#include <ossia/dataflow/graph/graph_interface.hpp>
10
#include <ossia/dataflow/graph_node.hpp>
11
#include <ossia/dataflow/port.hpp>
12
#include <ossia/dataflow/node_process.hpp>
13
#include <ossia/dataflow/token_request.hpp>
14
#include <ossia/editor/scenario/scenario.hpp>
15
#include <ossia/editor/scenario/time_event.hpp>
16
#include <ossia/editor/scenario/time_interval.hpp>
17
#include <ossia/editor/scenario/time_sync.hpp>
18

19
#include "include_catch.hpp"
20

21
#include "../Editor/TestUtils.hpp"
22

23
#include <spdlog/sinks/null_sink.h>
24
#include <spdlog/spdlog.h>
25

26
#include <vector>
27

28
namespace
29
{
30
constexpr double flicks_ratio_48k = 48000. / ossia::flicks_per_second<double>;
31

32
//! What a plug-in host reads from the node while it runs
33
struct position_probe final : public ossia::graph_node
34
{
35
  std::vector<int64_t> positions;
36
  std::vector<ossia::token_request> requests;
37
  position_probe()
21 ✔
38
  {
21 ✔
39
    m_inlets.push_back(new ossia::audio_inlet);
21 ✔
40
    m_outlets.push_back(new ossia::audio_outlet);
21 ✔
41
  }
21 ✔
NEW
42
  std::string label() const noexcept override { return "position probe"; }
×
43
  void run(const ossia::token_request& tk, ossia::exec_state_facade) noexcept override
240 ✔
44
  {
45
    positions.push_back(transport_frames());
240 ✔
46
    requests.push_back(tk);
240 ✔
47
  }
240 ✔
48
};
49

50
struct tick_t
51
{
52
  int64_t prev, date; // in samples
53
  int64_t start_sample, length;
54
  double speed;
55
};
56

57
// A play from 0, a tick starting inside its buffer, a loop back to 0, a seek,
58
// twice and half the speed, then backwards
59
const tick_t ticks[]{
60
    {0, 512, 0, 512, 1.},          {512, 1024, 0, 512, 1.},
61
    {1024, 1388, 148, 364, 1.},    {1388, 1900, 0, 512, 1.},
62
    {0, 512, 0, 512, 1.},          {48000, 48512, 0, 512, 1.},
63
    {48512, 49536, 0, 512, 2.},    {49536, 49792, 0, 512, 0.5},
64
    {49792, 49280, 0, 512, -1.},   {49280, 48768, 0, 512, -1.}};
65

66
ossia::token_request request(const tick_t& t)
160 ✔
67
{
68
  ossia::token_request tk;
160 ✔
69
  tk.prev_date = ossia::time_value{int64_t(t.prev / flicks_ratio_48k)};
160 ✔
70
  tk.date = ossia::time_value{int64_t(t.date / flicks_ratio_48k)};
160 ✔
71
  tk.start_sample = t.start_sample;
160 ✔
72
  tk.length_sample = t.length;
160 ✔
73
  tk.speed = t.speed;
160 ✔
74
  tk.tempo = 120.;
160 ✔
75
  return tk;
160 ✔
76
}
77

78
//! The frames the timeline would have played up to the tick's first sample at
79
//! the tick's speed: the frames the tick's tempo, which carries the speed,
80
//! turns into its musical position
81
std::vector<int64_t> expected_positions()
16 ✔
82
{
83
  std::vector<int64_t> res;
16 ✔
84
  for(const auto& t : ticks)
176 ✔
85
    res.push_back(int64_t(t.prev / (t.speed < 0 ? -t.speed : t.speed)));
160 ✔
86
  return res;
16 ✔
NEW
87
}
×
88

89
void setup_state(ossia::execution_state& e)
21 ✔
90
{
91
  e.bufferSize = 512;
21 ✔
92
  e.sampleRate = 48000;
21 ✔
93
  e.modelToSamplesRatio = flicks_ratio_48k;
21 ✔
94
  e.samplesToModelRatio = 1. / flicks_ratio_48k;
21 ✔
95
}
21 ✔
96
}
97

98

99
TEST_CASE("a_node_reads_the_position_of_the_tick_it_runs", "[transport]")
1 ✔
100
{
101
  using opt = ossia::graph_setup_options;
102
  for(auto scheduling : {opt::StaticFixed, opt::StaticBFS, opt::StaticTC, opt::Dynamic})
5 ✔
103
    for(bool parallel : {false, true})
12 ✔
104
      for(bool logged : {false, true})
24 ✔
105
      {
106
        CAPTURE(int(scheduling), parallel, logged);
16 ✔
107
        opt options;
16 ✔
108
        options.scheduling = scheduling;
16 ✔
109
        options.parallel = parallel;
16 ✔
110
        options.parallel_threads = 2;
16 ✔
111
        if(logged)
16 ✔
112
          options.log = std::make_shared<spdlog::logger>(
16 ✔
113
              "transport", std::make_shared<spdlog::sinks::null_sink_mt>());
24 ✔
114
        auto g = ossia::make_graph(options);
16 ✔
115

116
        auto probe = std::make_shared<position_probe>();
16 ✔
117
        g->add_node(probe);
16 ✔
118

119
        ossia::execution_state e;
16 ✔
120
        setup_state(e);
16 ✔
121
        for(const auto& t : ticks)
176 ✔
122
        {
123
          probe->request(request(t));
160 ✔
124
          e.begin_tick();
160 ✔
125
          g->state(e);
160 ✔
126
          e.commit();
160 ✔
127
        }
128

129
        // Within a sample of the exact one: the dates are whole flicks
130
        REQUIRE(probe->positions.size() == std::size(ticks));
32 ✔
131
        const auto expected = expected_positions();
16 ✔
132
        for(std::size_t i = 0; i < expected.size(); i++)
176 ✔
133
        {
134
          CAPTURE(i, probe->positions[i], expected[i]);
160 ✔
135
          CHECK(std::abs(probe->positions[i] - expected[i]) <= 1);
160 ✔
136
        }
160 ✔
137
        g->clear();
16 ✔
138
      }
16 ✔
139
}
1 ✔
140

141
TEST_CASE("the_position_agrees_with_the_musical_position_at_any_speed", "[transport]")
1 ✔
142
{
143
  // An interval with a meter, played at a speed: the tick's tempo is the
144
  // interval's times the speed, and the position, the musical position and
145
  // that tempo must tell the same story, tick after tick, as a plug-in
146
  // turning one into the other expects
147
  for(double speed : {1., 2., 0.5, 1.5, -1.})
6 ✔
148
  {
149
    CAPTURE(speed);
5 ✔
150
    ossia::execution_state e;
5 ✔
151
    setup_state(e);
5 ✔
152
    root_scenario s;
5 ✔
153
    auto se = *s.scenario->get_start_time_sync()->get_time_events().begin();
10 ✔
154
    auto en = std::make_shared<ossia::time_sync>();
5 ✔
155
    en->set_expression(ossia::expressions::make_expression_true());
5 ✔
156
    auto ee = std::make_shared<ossia::time_event>(
157
        ossia::time_event::exec_callback{}, *en,
10 ✔
158
        ossia::expressions::make_expression_true());
15 ✔
159
    en->insert(en->get_time_events().end(), ee);
10 ✔
160
    s.scenario->add_time_sync(en);
5 ✔
161
    auto itv = ossia::time_interval::create(
10 ✔
162
        {}, *se, *ee, ossia::Infinite, ossia::Infinite, ossia::Infinite);
10 ✔
163
    ossia::time_signature_map m;
5 ✔
164
    m[ossia::time_value{0}] = {4, 4};
5 ✔
165
    itv->set_time_signature_map(m);
5 ✔
166
    // A quarter is half a second: 120 BPM at speed 1
167
    itv->set_quarter_duration(ossia::flicks_per_second<double> / 2.);
5 ✔
168
    s.scenario->add_time_interval(itv);
5 ✔
169
    auto probe = std::make_shared<position_probe>();
5 ✔
170
    itv->add_time_process(std::make_shared<ossia::node_process>(probe));
5 ✔
171
    itv->set_speed(speed);
5 ✔
172
    s.interval->start();
5 ✔
173
    s.interval->tick_current(ossia::time_value{}, {});
5 ✔
174

175
    ossia::token_request root;
5 ✔
176
    root.tempo = 120;
5 ✔
177
    root.speed = 1.;
5 ✔
178
    root.signature = {4, 4};
5 ✔
179
    root.start_sample = 0;
5 ✔
180
    root.length_sample = 512;
5 ✔
181
    const int64_t buffer_flicks = int64_t(512 / flicks_ratio_48k);
5 ✔
182

183
    // Backwards from 10 s in
184
    if(speed < 0)
5 ✔
185
    {
186
      itv->set_speed(1.);
1 ✔
187
      for(int i = 0; i < 1000; i++)
1,001 ✔
188
        s.interval->tick(ossia::time_value{buffer_flicks}, root);
1,000 ✔
189
      probe->requested_tokens.clear();
1 ✔
190
      itv->set_speed(speed);
1 ✔
191
    }
192

193
    auto g = ossia::make_graph({});
5 ✔
194
    g->add_node(probe);
5 ✔
195
    for(int i = 0; i < 16; i++)
85 ✔
196
    {
197
      probe->requested_tokens.clear();
80 ✔
198
      s.interval->tick(ossia::time_value{buffer_flicks}, root);
80 ✔
199
      e.begin_tick();
80 ✔
200
      g->state(e);
80 ✔
201
      e.commit();
80 ✔
202
    }
203

204
    REQUIRE(probe->requests.size() >= 16);
5 ✔
205
    for(std::size_t i = 0; i < probe->requests.size(); i++)
85 ✔
206
    {
207
      const auto& tk = probe->requests[i];
80 ✔
208
      // Played backwards, the tempo is negative: the beats still count from 0
209
      const double tempo = tk.tempo < 0 ? -tk.tempo : tk.tempo;
80 ✔
210
      const double beats_from_position
211
          = double(probe->positions[i]) / e.sampleRate * (tempo / 60.);
80 ✔
212
      CAPTURE(i, probe->positions[i], tk.tempo, tk.musical_start_position);
80 ✔
213
      // Within a sample's worth of beats
214
      CHECK(
80 ✔
215
          std::abs(beats_from_position - tk.musical_start_position)
216
          <= tempo / 60. / e.sampleRate + 1e-9);
217
    }
80 ✔
218
    g->clear();
5 ✔
219
  }
5 ✔
220
}
6 ✔
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