OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
midi_protocol.hpp
1#pragma once
2#include <ossia/detail/lockfree_queue.hpp>
3#include <ossia/network/base/device.hpp>
4#include <ossia/network/base/parameter.hpp>
5#include <ossia/network/base/protocol.hpp>
6#include <ossia/network/common/parameter_properties.hpp>
7#include <ossia/network/context_functions.hpp>
8#include <ossia/network/domain/domain.hpp>
9#include <ossia/network/value/value.hpp>
10#include <ossia/protocols/midi/detail/channel.hpp>
11
12#include <libremidi/api.hpp>
13#include <libremidi/configurations.hpp>
14#include <libremidi/detail/conversion.hpp>
15#include <libremidi/message.hpp>
16#include <libremidi/observer_configuration.hpp>
17#include <libremidi/ump.hpp>
18
19#include <array>
20#include <atomic>
21#include <cassert>
22namespace libremidi
23{
24class midi_in;
25class midi_out;
26struct input_configuration;
27struct output_configuration;
28struct message;
29}
30namespace ossia::net::midi
31{
32class midi_device;
33struct OSSIA_EXPORT midi_info
34{
35 enum class Type
36 {
37 Input,
38 Output
39 };
40
41 midi_info() = default;
42 midi_info(const libremidi::input_port& p, bool v)
43 : type{Type::Input}
44 , handle{p}
45 , is_virtual{v}
46 {
47 }
48 midi_info(const libremidi::output_port& p, bool v)
49 : type{Type::Output}
50 , handle{p}
51 , is_virtual{v}
52 {
53 }
54
55 midi_info(Type t, const libremidi::port_information& p, bool v)
56 : type{t}
57 , handle{p}
58 , is_virtual{v}
59 {
60 }
61
62 Type type{};
63 libremidi::port_information handle{};
64 bool is_virtual{};
65};
66
67class OSSIA_EXPORT midi_protocol final
69 , public Nano::Observer
70{
71public:
72 explicit midi_protocol(
73 ossia::net::network_context_ptr, std::string device_name,
74 libremidi::input_configuration&, libremidi::input_api_configuration midi_api);
75 explicit midi_protocol(
76 ossia::net::network_context_ptr, std::string device_name,
77 libremidi::output_configuration&, libremidi::output_api_configuration midi_api);
78 explicit midi_protocol(
79 ossia::net::network_context_ptr, midi_info,
80 libremidi::API api = libremidi::API::UNSPECIFIED);
81 ~midi_protocol();
82
83 bool set_info(midi_info);
84 midi_info get_info() const;
85
86 int64_t get_timestamp() const noexcept;
87
88 static std::string
89 get_midi_port_name(ossia::net::device_base* dev, const midi_info& info);
90
91 static std::vector<midi_info> scan(libremidi::API = libremidi::API::UNSPECIFIED);
92
93 void push_value(const libremidi::message&);
94 void push_value(const libremidi::ump&);
95
96 void enable_registration();
97
98 bool learning() const;
99 void set_learning(bool);
100
101 libremidi::midi_in* midi_in() const noexcept { return m_input.get(); }
102
103 ossia::spsc_queue<libremidi::ump> messages;
104
105private:
106 ossia::net::network_context_ptr m_context;
107 std::unique_ptr<libremidi::midi_in> m_input;
108 std::unique_ptr<libremidi::midi_out> m_output;
109
110 std::array<midi_channel, 16> m_channels;
111
112 midi_info m_info{};
113 midi_device* m_dev{};
114 bool m_registers{};
115 std::atomic_bool m_learning{};
116
117 friend class midi_device;
118 friend class midi_parameter;
119 bool pull(ossia::net::parameter_base&) override;
120 bool push(const ossia::net::parameter_base&, const ossia::value& v) override;
121 bool push_raw(const ossia::net::full_parameter_data& parameter_base) override;
122 bool observe(ossia::net::parameter_base&, bool) override;
123 bool update(ossia::net::node_base& node_base) override;
124 void set_device(ossia::net::device_base& dev) override;
125
126 void register_parameter(midi_parameter& adrs);
127 void unregister_parameter(midi_parameter& adrs);
128 void register_parameter_base(const ossia::net::parameter_base& adrs);
129 void unregister_parameter_base(const ossia::net::parameter_base& adrs);
130 void register_node_base(const ossia::net::node_base& adrs);
131 void unregister_node_base(const ossia::net::node_base& adrs);
132 void value_callback(
133 bool observed, ossia::net::parameter_base& param, const ossia::value& val);
134
135 void midi_callback(const libremidi::message&);
136 void on_learn(const libremidi::message& m);
137
138 libremidi::midi1_to_midi2 to_midi2;
139};
140}
Root of a device tree.
Definition ossia/network/base/device.hpp:58
The node_base class.
Definition node.hpp:48
The parameter_base class.
Definition ossia/network/base/parameter.hpp:48
The protocol_base class.
Definition protocol.hpp:40
The value class.
Definition value.hpp:173
void(* value_callback)(void *, const opp::value &)
container for a value callback
Definition ossia-cpp98.hpp:474
Definition git_info.h:7
Full information about a parameter.
Definition parameter_data.hpp:61