RateLimiter.hpp
1 #pragma once
2 #include <Engine/Node/SimpleApi.hpp>
3 
4 #include <ossia/detail/logger.hpp>
5 #include <ossia/network/value/format_value.hpp>
6 
7 namespace Nodes::RateLimiter
8 {
9 
10 struct Node
11 {
12  struct RateLimiter
13  {
14  };
15 
17  {
18  static const constexpr auto prettyName = "Rate Limiter";
19  static const constexpr auto objectKey = "RateLimiter";
20  static const constexpr auto category = "Control/Mappings";
21  static const constexpr auto author = "ossia score";
22  static const constexpr auto tags = std::array<const char*, 0>{};
23  static const constexpr auto kind = Process::ProcessCategory::Mapping;
24  static const constexpr auto description
25  = "Limit and quantize the rate of a value stream";
26  static const uuid_constexpr auto uuid
27  = make_uuid("76cfd504-7c10-4bdb-a1b4-fbe449cc06f0");
28 
29  static const constexpr auto controls = tuplet::make_tuple(
30  Control::Widgets::QuantificationChooser(),
31  Control::IntSlider{"ms.", 0, 1000, 10});
32 
33  static const constexpr value_in value_ins[]{"in"};
34  static const constexpr value_out value_outs[]{"out"};
35  };
36 
37  class State
38  {
39  public:
40  bool should_output(
41  float quantif, const int64_t& ms, const ossia::token_request& t,
42  const ossia::exec_state_facade& st)
43  {
44  if(quantif != 0.)
45  return true;
46  else if(t.date.impl >= (last_time + ms * flicks_per_ms))
47  {
48  last_time = t.date.impl;
49  return true;
50  }
51  return false;
52  }
53 
54  private:
55  static const constexpr int64_t flicks_per_ms = 705'600;
56  int64_t last_time{};
57  };
58 
59  using control_policy = ossia::safe_nodes::last_tick;
60 
61  static void
62  run(const ossia::value_port& in, float quantif, uint64_t ms, ossia::value_port& out,
63  ossia::token_request t, ossia::exec_state_facade st, State& self)
64  {
65  for(const ossia::timed_value& v : in.get_data())
66  {
67 
68  if(quantif <= 0.)
69  {
70  if(self.should_output(quantif, ms, t, st))
71  out.write_value(v.value, v.timestamp); // TODO fix accuracy of timestamp
72  }
73  else
74  {
75  if(auto time
76  = t.get_physical_quantification_date(1. / quantif, st.modelToSamples()))
77  out.write_value(v.value, *time);
78  }
79  }
80  }
81 
82  static void item(
83  Process::ComboBox& quantif, Process::IntSlider& ms,
84  const Process::ProcessModel& process, QGraphicsItem& parent, QObject& context,
85  const Process::Context& doc)
86  {
87  using namespace Process;
88  const Process::PortFactoryList& portFactory
90  const auto cMarg = 15;
91  const auto w = 165;
92 
93  auto c1_bg = new score::BackgroundItem{&parent};
94  c1_bg->setRect({0., 0, w, 45.});
95 
96  auto quant_item = makeControl(
97  tuplet::get<0>(Metadata::controls), quantif, parent, context, doc, portFactory);
98  quant_item.root.setPos(5, 0);
99  quant_item.control.setPos(cMarg, cMarg);
100 
101  auto ms_item = makeControl(
102  tuplet::get<1>(Metadata::controls), ms, parent, context, doc, portFactory);
103  ms_item.root.setPos(100, 0);
104  ms_item.control.setPos(0, cMarg);
105  }
106 };
107 }
Definition: PortFactory.hpp:65
The Process class.
Definition: score-lib-process/Process/Process.hpp:61
Definition: RectItem.hpp:96
Base classes and tools to implement processes and layers.
Definition: JSONVisitor.hpp:1324
Utilities for OSSIA data structures.
Definition: DeviceInterface.hpp:33
Definition: score-lib-process/Control/Widgets.hpp:178
Definition: SimpleApi.hpp:32
Definition: RateLimiter.hpp:17
Definition: RateLimiter.hpp:13
Definition: RateLimiter.hpp:11
Definition: ProcessContext.hpp:12
const T & interfaces() const
Access to a specific interface list.
Definition: ApplicationContext.hpp:67