MidiHiRes.hpp
1 #pragma once
2 #include <ossia/network/value/value_conversion.hpp>
3 
4 #include <halp/callback.hpp>
5 #include <halp/controls.hpp>
6 #include <halp/meta.hpp>
7 #include <halp/sample_accurate_controls.hpp>
8 
9 namespace Nodes
10 {
11 namespace MidiHiRes
12 {
13 struct Input
14 {
15  halp_meta(name, "Midi hi-res input")
16  halp_meta(c_name, "MidiHiResIn")
17  halp_meta(category, "Midi")
18  halp_meta(author, "ossia score")
19  halp_meta(
20  manual_url, "https://ossia.io/score-docs/processes/midi-utilities.html#midi-hires")
21  halp_meta(description, "Creates a float from MSB/LSB CCs")
22  halp_meta(uuid, "28ca746e-c304-4ba6-bd5b-78934a1dec55")
23 
24  struct
25  {
26  // FIXME is_event false
27  halp::accurate<halp::val_port<"msb", int>> msb;
28  halp::accurate<halp::val_port<"lsb", int>> lsb;
29  } inputs;
30  struct
31  {
32  halp::callback<"int", int> i;
33  halp::callback<"float", float> f;
34  } outputs;
35 
36  void operator()()
37  {
38  auto& msbs = inputs.msb.values;
39  auto& lsbs = inputs.lsb.values;
40  if(msbs.empty() && lsbs.empty())
41  return;
42 
43  const auto m = msbs.empty() ? 0 : ossia::convert<int>(msbs.rbegin()->second);
44  const auto l = lsbs.empty() ? 0 : ossia::convert<int>(lsbs.rbegin()->second);
45 
46  outputs.i(m * 127 + l);
47  outputs.f(double(m * 127 + l) / (128. * 128.));
48  }
49 };
50 
51 struct Output
52 {
53  halp_meta(name, "Midi hi-res output")
54  halp_meta(c_name, "MidiHiResOut")
55  halp_meta(category, "Midi")
56  halp_meta(manual_url, "https://ossia.io/score-docs/processes/javascript.html#midi")
57  halp_meta(author, "ossia score")
58  halp_meta(description, "Creates MIDI LSB/MSB from a 0-16384 or 0-1 value")
59  halp_meta(uuid, "d6f5173b-b823-4571-b31f-660832b6132b")
60 
61  struct
62  {
63  halp::accurate<halp::val_port<"int", int>> i;
64  halp::accurate<halp::val_port<"float", float>> f;
65  } inputs;
66  struct
67  {
68  halp::accurate<halp::val_port<"msb", int>> msb;
69  halp::accurate<halp::val_port<"lsb", int>> lsb;
70  } outputs;
71 
72  void operator()()
73  {
74  for(auto& [val, t] : inputs.i.values)
75  {
76  const int32_t v = ossia::convert<int>(val);
77  const int32_t m = ossia::clamp(int32_t(v / 127), 0, 127);
78  const int32_t l = ossia::clamp(int32_t(v - m * 127), 0, 127);
79 
80  outputs.msb.values.emplace(t, m);
81  outputs.lsb.values.emplace(t, l);
82  }
83 
84  for(auto& [val, t] : inputs.f.values)
85  {
86  const float v = ossia::convert<float>(val) * (128. * 128.);
87  const int32_t m = ossia::clamp(int32_t(v / 127.), 0, 127);
88  const int32_t l = ossia::clamp(int32_t(v - m * 127), 0, 127);
89 
90  outputs.msb.values.emplace(t, m);
91  outputs.lsb.values.emplace(t, l);
92  }
93  }
94 };
95 }
96 }
Definition: MidiHiRes.hpp:14
Definition: MidiHiRes.hpp:52