Metro.hpp
1 #pragma once
2 #include <Engine/Node/SimpleApi.hpp>
3 
4 #include <numeric>
5 namespace Nodes
6 {
7 namespace Metro
8 {
9 struct Node
10 {
12  {
13  static const constexpr auto prettyName = "Free metronome";
14  static const constexpr auto objectKey = "Metro";
15  static const constexpr auto category = "Control/Generators";
16  static const constexpr auto author = "ossia score";
17  static const constexpr auto kind = Process::ProcessCategory::Generator;
18  static const constexpr auto description
19  = "Metronome which is not synced to the parent quantization settings";
20 
21  static const constexpr auto tags = std::array<const char*, 0>{};
22  static const uuid_constexpr auto uuid
23  = make_uuid("50439197-521E-4ED0-A3B7-EDD8DEAEAC93");
24 
25  static const constexpr value_out value_outs[]{"out"};
26 
27  static const constexpr auto controls = tuplet::make_tuple(
28  Control::Widgets::MusicalDurationChooser(), Control::Widgets::LFOFreqSlider(),
29  Control::ChooserToggle{"Quantify", {"Free", "Sync"}, false});
30  };
31 
32  static constexpr int64_t
33  get_period(bool use_tempo, double quantif, double freq, double tempo, int sr)
34  {
35  if(use_tempo)
36  {
37  const auto whole_dur = 240. / tempo;
38  const auto whole_samples = whole_dur * sr;
39  return quantif * whole_samples;
40  }
41  else
42  {
43  return sr / freq;
44  }
45  }
46 
47  static constexpr ossia::time_value
48  next_date(ossia::time_value cur_date, int64_t period)
49  {
50  return ossia::time_value{(int64_t)(period * (1 + cur_date.impl / period))};
51  }
52 
53  using control_policy = ossia::safe_nodes::last_tick;
54  static void
55  run(float quantif, float freq, bool val, ossia::value_port& res,
56  ossia::token_request tk, ossia::exec_state_facade st)
57  {
58  if(tk.date > tk.prev_date)
59  {
60  const auto period = get_period(val, quantif, freq, tk.tempo, st.sampleRate());
61  const auto next = next_date(tk.prev_date, period * st.samplesToModel());
62  if(tk.in_range(next))
63  {
64  res.write_value(
65  ossia::impulse{}, tk.to_physical_time_in_tick(next, st.modelToSamples()));
66  }
67  }
68  }
69 };
70 }
71 }
Definition: score-lib-process/Control/Widgets.hpp:374
Definition: SimpleApi.hpp:32
Definition: Metro.hpp:12
Definition: Metro.hpp:10