Zrythm v2.0.0-alpha.1
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
midi_mapping.h
1// SPDX-FileCopyrightText: © 2019-2022, 2024-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "dsp/parameter.h"
7#include "utils/icloneable.h"
8#include "utils/iobject_registry.h"
9
10#include <nlohmann/json_fwd.hpp>
11
12#define MIDI_MAPPINGS (PROJECT->midi_mappings_)
13
14namespace zrythm::engine::session
15{
19class MidiMapping : public QObject
20{
21 Q_OBJECT
22 QML_ELEMENT
23 QML_UNCREATABLE ("")
24
25public:
26 MidiMapping (utils::IObjectRegistry &registry, QObject * parent = nullptr);
27 Q_DISABLE_COPY_MOVE (MidiMapping)
28
29public:
30 friend void init_from (
31 MidiMapping &obj,
32 const MidiMapping &other,
33 utils::ObjectCloneType clone_type);
34
35 void set_enabled (bool enabled) { enabled_.store (enabled); }
36
37 void apply (std::array<midi_byte_t, 3> buf);
38
39private:
40 static constexpr auto kKeyKey = "key"sv;
41 static constexpr auto kDeviceIdKey = "deviceIdentifier"sv;
42 static constexpr auto kDestIdKey = "destId"sv;
43 static constexpr auto kEnabledKey = "enabled"sv;
44 friend void to_json (nlohmann::json &j, const MidiMapping &mapping);
45 friend void from_json (const nlohmann::json &j, MidiMapping &mapping);
46
47public:
48 utils::IObjectRegistry &registry_;
49
51 std::array<midi_byte_t, 3> key_ = {};
52
58 std::optional<utils::Utf8String> device_id_;
59
61 std::optional<dsp::ProcessorParameterUuidReference> dest_id_;
62
64 /* TODO: check if really should be atomic */
65 std::atomic<bool> enabled_ = false;
66};
67
71class MidiMappings final
72{
73public:
74 MidiMappings (utils::IObjectRegistry &registry);
75
84 void bind_at (
85 std::array<midi_byte_t, 3> buf,
86 std::optional<utils::Utf8String> device_id,
87 dsp::ProcessorParameterUuidReference dest_port,
88 int idx,
89 bool fire_events);
90
97 void unbind (int idx, bool fire_events);
98
99 void bind_device (
100 std::array<midi_byte_t, 3> buf,
101 std::optional<utils::Utf8String> device_id,
102 dsp::ProcessorParameterUuidReference dest_port,
103 bool fire_events)
104 {
105 bind_at (
106 buf, device_id, dest_port, static_cast<int> (mappings_.size ()),
107 fire_events);
108 }
109
110 void bind_track (
111 std::array<midi_byte_t, 3> buf,
112 dsp::ProcessorParameterUuidReference dest_port,
113 bool fire_events)
114 {
115 bind_at (
116 buf, std::nullopt, dest_port, static_cast<int> (mappings_.size ()),
117 fire_events);
118 }
119
120 int get_mapping_index (const MidiMapping &mapping) const;
121
130
134 void apply (const midi_byte_t * buf);
135
144 const dsp::ProcessorParameter &dest_port,
145 std::vector<MidiMapping *> * arr) const;
146
147 friend void init_from (
148 MidiMappings &obj,
149 const MidiMappings &other,
150 utils::ObjectCloneType clone_type)
151 {
152 // TODO
153 // utils::clone_unique_ptr_container (obj.mappings_, other.mappings_);
154 }
155
156private:
157 static constexpr auto kMappingsKey = "mappings"sv;
158 friend void to_json (nlohmann::json &j, const MidiMappings &mappings);
159 friend void from_json (const nlohmann::json &j, MidiMappings &mappings);
160
161public:
162 std::vector<std::unique_ptr<MidiMapping>> mappings_;
163
164private:
165 utils::IObjectRegistry &registry_;
166};
167
168}
A lock-free thread-safe vector of MidiEvents.
Definition midi_event.h:74
Processor parameter that accepts automation and modulation sources and integrates with QML and the DS...
Definition parameter.h:255
std::optional< dsp::ProcessorParameterUuidReference > dest_id_
Destination.
std::array< midi_byte_t, 3 > key_
Raw MIDI signal.
std::optional< utils::Utf8String > device_id_
The device that this connection will be mapped for.
std::atomic< bool > enabled_
Whether this binding is enabled.
All MIDI mappings in Zrythm.
int get_for_port(const dsp::ProcessorParameter &dest_port, std::vector< MidiMapping * > *arr) const
Get MIDI mappings for the given port.
void unbind(int idx, bool fire_events)
Unbinds the given binding.
void apply_from_cc_events(const dsp::MidiEventVector &events)
Applies the events to the appropriate mapping.
void apply(const midi_byte_t *buf)
Applies the given buffer to the matching ports.
void bind_at(std::array< midi_byte_t, 3 > buf, std::optional< utils::Utf8String > device_id, dsp::ProcessorParameterUuidReference dest_port, int idx, bool fire_events)
Binds the CC represented by the given raw buffer (must be size 3) to the given Port.
Abstract interface for a UUID-keyed object registry.
std::uint8_t midi_byte_t
MIDI byte.
Definition midi.h:43