Loading...
Searching...
No Matches
RateLimiter.hpp
1#pragma once
2#include <Fx/Types.hpp>
3
4#include <ossia/dataflow/value_port.hpp>
5#include <ossia/detail/logger.hpp>
6#include <ossia/network/value/format_value.hpp>
7
8#include <halp/callback.hpp>
9#include <halp/controls.hpp>
10#include <halp/layout.hpp>
11#include <halp/meta.hpp>
12#include <halp/midi.hpp>
13
14namespace Nodes::RateLimiter
15{
16struct Node
17{
18 halp_meta(name, "Rate Limiter")
19 halp_meta(c_name, "RateLimiter")
20 halp_meta(category, "Control/Mappings")
21 halp_meta(
22 manual_url, "https://ossia.io/score-docs/processes/rate-limiter.html#rate-limiter")
23 halp_meta(author, "ossia score")
24 halp_meta(description, "Limit and quantize the rate of a value stream")
25 halp_meta(uuid, "76cfd504-7c10-4bdb-a1b4-fbe449cc06f0")
26
27 struct ins
28 {
29 // FIXME all incorrect when token_request smaller than tick
30 ossia_port<"in", ossia::value_port> port{};
31 quant_selector<"Quantization"> quantification; // FIXME use proper time widget
32 halp::hslider_i32<"ms.", halp::irange{0, 1000, 10}> ms;
33 } inputs;
34 struct
35 {
36 halp::timed_callback<"out", ossia::value> out;
37 } outputs;
38
39 using tick = halp::tick_flicks;
40 bool should_output(float quantif, int64_t ms, tick t)
41 {
42 if(quantif != 0.)
43 return true;
44 else if(t.end_in_flicks >= (last_time + ms * flicks_per_ms))
45 {
46 last_time = t.end_in_flicks;
47 return true;
48 }
49 return false;
50 }
51
52 static const constexpr int64_t flicks_per_ms = 705'600;
53 int64_t last_time{};
54
55 void operator()(const tick& t)
56 {
57 auto& in = *inputs.port.value;
58 auto quantif = inputs.quantification.value;
59 auto ms = inputs.ms.value;
60 for(const ossia::timed_value& v : in.get_data())
61 {
62 if(quantif <= 0.)
63 {
64 if(should_output(quantif, ms, t))
65 outputs.out(v.timestamp, v.value); // FIXME fix accuracy of timestamp
66 }
67 else
68 {
69 for(auto [t, q] : t.get_quantification_date(1. / quantif))
70 {
71 outputs.out(t, v.value);
72 break;
73 }
74 }
75 }
76 }
77
78 struct ui
79 {
80 halp_meta(layout, halp::layouts::hbox)
81 halp_meta(background, halp::colors::mid)
82 halp::control<&ins::quantification> q;
83 halp::control<&ins::ms> t;
84 };
85};
86}
Definition RateLimiter.hpp:28
Definition RateLimiter.hpp:79
Definition RateLimiter.hpp:17
Definition Types.hpp:48
Definition Types.hpp:39