MidiHiRes.hpp
1 #pragma once
2 #include <Engine/Node/SimpleApi.hpp>
3 
4 #include <ossia/network/value/value_conversion.hpp>
5 
6 namespace Nodes
7 {
8 namespace MidiHiRes
9 {
10 struct Input
11 {
13  {
14  static const constexpr auto prettyName = "Midi hi-res input";
15  static const constexpr auto objectKey = "MidiHiResIn";
16  static const constexpr auto category = "Midi";
17  static const constexpr auto author = "ossia score";
18  static const constexpr auto kind = Process::ProcessCategory::MidiEffect;
19  static const constexpr auto description = "Creates a float from MSB/LSB CCs";
20  static const constexpr auto tags = std::array<const char*, 0>{};
21  static const uuid_constexpr auto uuid
22  = make_uuid("28ca746e-c304-4ba6-bd5b-78934a1dec55");
23 
24  static const constexpr value_in value_ins[]{{"msb", false}, {"lsb", false}};
25  static const constexpr value_out value_outs[]{"int", "float"};
26  };
27 
28  using control_policy = ossia::safe_nodes::default_tick;
29 
30  static void
31  run(const ossia::value_port& msb, const ossia::value_port& lsb, ossia::value_port& out,
32  ossia::value_port& out_f, const ossia::token_request& tk,
33  ossia::exec_state_facade st)
34  {
35  auto& msbs = msb.get_data();
36  auto& lsbs = lsb.get_data();
37  if(msbs.empty() && lsbs.empty())
38  return;
39 
40  const auto m = msbs.empty() ? 0 : ossia::convert<int>(msbs.back().value);
41  const auto l = lsbs.empty() ? 0 : ossia::convert<int>(lsbs.back().value);
42 
43  auto [start, dur] = st.timings(tk);
44  out.write_value(m * 127 + l, start);
45  out_f.write_value(double(m * 127 + l) / (128. * 128.), start);
46  }
47 };
48 
49 struct Output
50 {
52  {
53  static const constexpr auto prettyName = "Midi hi-res output";
54  static const constexpr auto objectKey = "MidiHiResOut";
55  static const constexpr auto category = "Midi";
56  static const constexpr auto author = "ossia score";
57  static const constexpr auto kind = Process::ProcessCategory::MidiEffect;
58  static const constexpr auto description
59  = "Creates MIDI LSB/MSB from a 0-16384 or 0-1 value";
60  static const constexpr auto tags = std::array<const char*, 0>{};
61  static const uuid_constexpr auto uuid
62  = make_uuid("d6f5173b-b823-4571-b31f-660832b6132b");
63 
64  static const constexpr value_in value_ins[]{"int", "float"};
65  static const constexpr value_out value_outs[]{"msb", "lsb"};
66  };
67 
68  using control_policy = ossia::safe_nodes::default_tick;
69 
70  static void
71  run(const ossia::value_port& in, const ossia::value_port& in_f, ossia::value_port& msb,
72  ossia::value_port& lsb, const ossia::token_request& tk,
73  ossia::exec_state_facade st)
74  {
75  for(auto& [val, t] : in.get_data())
76  {
77  const int32_t v = ossia::convert<int>(val);
78  const int32_t m = ossia::clamp(int32_t(v / 127), 0, 127);
79  const int32_t l = ossia::clamp(int32_t(v - m * 127), 0, 127);
80 
81  msb.write_value(m, t);
82  lsb.write_value(l, t);
83  }
84 
85  for(auto& [val, t] : in_f.get_data())
86  {
87  const float v = ossia::convert<float>(val) * (128. * 128.);
88  const int32_t m = ossia::clamp(int32_t(v / 127.), 0, 127);
89  const int32_t l = ossia::clamp(int32_t(v - m * 127), 0, 127);
90 
91  msb.write_value(m, t);
92  lsb.write_value(l, t);
93  }
94  }
95 };
96 }
97 }
Definition: SimpleApi.hpp:32
Definition: MidiHiRes.hpp:13
Definition: MidiHiRes.hpp:11
Definition: MidiHiRes.hpp:52
Definition: MidiHiRes.hpp:50