OSSIA
Open Scenario System for Interactive Application
midi_port.hpp
1 #pragma once
2 #include <ossia/dataflow/value_vector.hpp>
3 
4 #include <libremidi/message.hpp>
5 
6 namespace ossia
7 {
8 
9 struct midi_port
10 {
11  static const constexpr int which = 1;
12 
13  value_vector<libremidi::message> messages;
14 
15  using message = libremidi::message;
16  using message_type = libremidi::message_type;
17  using midi_bytes = libremidi::midi_bytes;
18 
19  message& note_on(uint8_t channel, uint8_t note, uint8_t velocity) noexcept
20  {
21  return create(make_command(message_type::NOTE_ON, channel), note, velocity);
22  }
23 
24  message& note_off(uint8_t channel, uint8_t note, uint8_t velocity) noexcept
25  {
26  return create(make_command(message_type::NOTE_OFF, channel), note, velocity);
27  }
28 
29  message& control_change(uint8_t channel, uint8_t control, uint8_t value) noexcept
30  {
31  return create(make_command(message_type::CONTROL_CHANGE, channel), control, value);
32  }
33 
34  message& program_change(uint8_t channel, uint8_t value) noexcept
35  {
36  return create(make_command(message_type::PROGRAM_CHANGE, channel), value);
37  }
38 
39  message& pitch_bend(uint8_t channel, int value) noexcept
40  {
41  return create(
42  make_command(message_type::PITCH_BEND, channel), (unsigned char)(value & 0x7F),
43  (uint8_t)((value >> 7) & 0x7F));
44  }
45 
46  message& pitch_bend(uint8_t channel, uint8_t lsb, uint8_t msb) noexcept
47  {
48  return create(make_command(message_type::PITCH_BEND, channel), lsb, msb);
49  }
50 
51  message& poly_pressure(uint8_t channel, uint8_t note, uint8_t value) noexcept
52  {
53  return create(make_command(message_type::POLY_PRESSURE, channel), note, value);
54  }
55 
56  message& aftertouch(uint8_t channel, uint8_t value) noexcept
57  {
58  return create(make_command(message_type::AFTERTOUCH, channel), value);
59  }
60 
61 private:
62  static uint8_t make_command(const message_type type, const int channel) noexcept
63  {
64  return (uint8_t)((uint8_t)type | std::clamp(channel, 0, channel - 1));
65  }
66 
67  message& create(uint8_t b0, uint8_t b1) noexcept
68  {
69  auto& m = messages.emplace_back();
70  m.bytes = {b0, b1};
71  return m;
72  }
73  message& create(uint8_t b0, uint8_t b1, uint8_t b2) noexcept
74  {
75  auto& m = messages.emplace_back();
76  m.bytes = {b0, b1, b2};
77  return m;
78  }
79 };
80 
81 struct midi_delay_line
82 {
83  std::vector<value_vector<libremidi::message>> messages;
84 };
85 
86 }
Definition: git_info.h:7
constexpr OSSIA_INLINE T clamp(T d, const T min, const T max) noexcept
clamp Returns the value bounded by a min and a max
Definition: math.hpp:154