Loading...
Searching...
No Matches
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
9namespace Nodes
10{
11namespace Metro
12{
13struct Node
14{
15 halp_meta(name, "Free metronome (old)")
16 halp_meta(c_name, "Metro")
17 halp_meta(category, "Timing/Control")
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_flag(deprecated);
23 halp_meta(uuid, "50439197-521E-4ED0-A3B7-EDD8DEAEAC93")
24
25 struct
26 {
27 musical_duration_selector<"Duration"> dur;
28 halp::log_hslider_f32<"Frequency", halp::range{0.01f, 100.f, 1.f}> frequency;
29 struct : halp::toggle<"Quantify">
30 {
31 struct range
32 {
33 std::array<std::string_view, 2> values{"Free", "Sync"};
34 int init = 0;
35 };
36 } quantify;
37 } inputs;
38 struct
39 {
40 halp::timed_callback<"out"> out;
41
42 } outputs;
43
44 static constexpr int64_t get_period(bool use_tempo, double quantif, double freq, double tempo, int sr)
45 {
46 if(use_tempo)
47 {
48 const auto whole_dur = 240. / tempo;
49 const auto whole_samples = whole_dur * sr;
50 return quantif * whole_samples;
51 }
52 else
53 {
54 return sr / freq;
55 }
56 }
57
59 static constexpr int64_t next_date(int64_t cur_date, int64_t period)
60 {
61 return period * ((cur_date + period - 1) / period);
62 }
63
64 halp::setup setup;
65 void prepare(halp::setup s) { setup = s; }
66
67 //using control_policy = ossia::safe_nodes::last_tick;
68 using tick = halp::tick_musical;
69 void operator()(const halp::tick_musical& tk)
70 {
71 if(tk.start_position_in_quarters < tk.end_position_in_quarters)
72 {
73 // in samples:
74 const auto period = get_period(
75 inputs.quantify, inputs.dur.value, inputs.frequency, tk.tempo, setup.rate);
76 if(period <= 0)
77 return;
78
79 const auto next = next_date(tk.position_in_frames, period);
80 if(tk.in_range(next))
81 {
82 outputs.out(next - tk.position_in_frames);
83 }
84 }
85 }
86};
87}
88}
Definition Metro.hpp:14
static constexpr int64_t next_date(int64_t cur_date, int64_t period)
The first multiple of the period at or after cur_date.
Definition Metro.hpp:59
Definition Types.hpp:49
Definition MIDISync.hpp:127