Metro.hpp
1 #pragma once
2 #include <Fx/Types.hpp>
3 
4 #include <halp/audio.hpp>
5 #include <halp/callback.hpp>
6 #include <halp/controls.hpp>
7 #include <halp/meta.hpp>
8 
9 namespace Nodes
10 {
11 namespace Metro
12 {
13 struct Node
14 {
15  halp_meta(name, "Free metronome")
16  halp_meta(c_name, "Metro")
17  halp_meta(category, "Control/Generators")
18  halp_meta(author, "ossia score")
19  halp_meta(manual_url, "https://ossia.io/score-docs/processes/control-utilities.html#free-metronome")
20  halp_meta(description, "Metronome which is not synced to the parent quantization settings")
21 
22  halp_meta(uuid, "50439197-521E-4ED0-A3B7-EDD8DEAEAC93")
23 
24  struct
25  {
26  musical_duration_selector<"Duration"> dur;
27  halp::log_hslider_f32<"Frequency", halp::range{0.01f, 100.f, 1.f}> frequency;
28  struct : halp::toggle<"Quantify">
29  {
30  struct range
31  {
32  std::array<std::string_view, 2> values{"Free", "Sync"};
33  int init = 0;
34  };
35  } quantify;
36  } inputs;
37  struct
38  {
39  halp::timed_callback<"out"> out;
40 
41  } outputs;
42 
43  static constexpr int64_t get_period(bool use_tempo, double quantif, double freq, double tempo, int sr)
44  {
45  if(use_tempo)
46  {
47  const auto whole_dur = 240. / tempo;
48  const auto whole_samples = whole_dur * sr;
49  return quantif * whole_samples;
50  }
51  else
52  {
53  return sr / freq;
54  }
55  }
56 
57  static constexpr int64_t next_date(int64_t cur_date, int64_t period)
58  {
59  return (int64_t)(period * (cur_date / period));
60  }
61 
62  halp::setup setup;
63  void prepare(halp::setup s) { setup = s; }
64 
65  //using control_policy = ossia::safe_nodes::last_tick;
66  using tick = halp::tick_musical;
67  void operator()(const halp::tick_musical& tk)
68  {
69  if(tk.start_position_in_quarters < tk.end_position_in_quarters)
70  {
71  // in samples:
72  const auto period = get_period(
73  inputs.quantify, inputs.dur.value, inputs.frequency, tk.tempo, setup.rate);
74 
75  const auto next = next_date(tk.position_in_frames, period);
76  if(tk.in_range(next))
77  {
78  outputs.out(next - tk.position_in_frames);
79  }
80  }
81  }
82 };
83 }
84 }
Definition: Metro.hpp:14
Definition: Types.hpp:48
Definition: LFO_v2.hpp:68