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/midi_event.h"
7#include "dsp/parameter.h"
8#include "utils/icloneable.h"
9#include "utils/iobject_registry.h"
10
11#include <nlohmann/json_fwd.hpp>
12
13#define MIDI_MAPPINGS (PROJECT->midi_mappings_)
14
15namespace zrythm::engine::session
16{
20class MidiMapping : public QObject
21{
22 Q_OBJECT
23 QML_ELEMENT
24 QML_UNCREATABLE ("")
25
26public:
27 MidiMapping (utils::IObjectRegistry &registry, QObject * parent = nullptr);
28 Q_DISABLE_COPY_MOVE (MidiMapping)
29
30public:
31 friend void init_from (
32 MidiMapping &obj,
33 const MidiMapping &other,
34 utils::ObjectCloneType clone_type);
35
36 void set_enabled (bool enabled) { enabled_.store (enabled); }
37
38 void apply (std::array<midi_byte_t, 3> buf);
39
40private:
41 static constexpr auto kKeyKey = "key"sv;
42 static constexpr auto kDeviceIdKey = "deviceIdentifier"sv;
43 static constexpr auto kDestIdKey = "destId"sv;
44 static constexpr auto kEnabledKey = "enabled"sv;
45 friend void to_json (nlohmann::json &j, const MidiMapping &mapping);
46 friend void from_json (const nlohmann::json &j, MidiMapping &mapping);
47
48public:
49 utils::IObjectRegistry &registry_;
50
52 std::array<midi_byte_t, 3> key_ = {};
53
59 std::optional<utils::Utf8String> device_id_;
60
62 std::optional<dsp::ProcessorParameterUuidReference> dest_id_;
63
65 /* TODO: check if really should be atomic */
66 std::atomic<bool> enabled_ = false;
67};
68
72class MidiMappings final
73{
74public:
75 MidiMappings (utils::IObjectRegistry &registry);
76
85 void bind_at (
86 std::array<midi_byte_t, 3> buf,
87 std::optional<utils::Utf8String> device_id,
88 dsp::ProcessorParameterUuidReference dest_port,
89 int idx,
90 bool fire_events);
91
98 void unbind (int idx, bool fire_events);
99
100 void bind_device (
101 std::array<midi_byte_t, 3> buf,
102 std::optional<utils::Utf8String> device_id,
103 dsp::ProcessorParameterUuidReference dest_port,
104 bool fire_events)
105 {
106 bind_at (
107 buf, device_id, dest_port, static_cast<int> (mappings_.size ()),
108 fire_events);
109 }
110
111 void bind_track (
112 std::array<midi_byte_t, 3> buf,
113 dsp::ProcessorParameterUuidReference dest_port,
114 bool fire_events)
115 {
116 bind_at (
117 buf, std::nullopt, dest_port, static_cast<int> (mappings_.size ()),
118 fire_events);
119 }
120
121 int get_mapping_index (const MidiMapping &mapping) const;
122
130 void apply_from_cc_events (std::span<const dsp::RealtimeMidiEvent> events);
131
135 void apply (const midi_byte_t * buf);
136
145 const dsp::ProcessorParameter &dest_port,
146 std::vector<MidiMapping *> * arr) const;
147
148 friend void init_from (
149 MidiMappings &obj,
150 const MidiMappings &other,
151 utils::ObjectCloneType clone_type)
152 {
153 // TODO
154 // utils::clone_unique_ptr_container (obj.mappings_, other.mappings_);
155 }
156
157private:
158 static constexpr auto kMappingsKey = "mappings"sv;
159 friend void to_json (nlohmann::json &j, const MidiMappings &mappings);
160 friend void from_json (const nlohmann::json &j, MidiMappings &mappings);
161
162public:
163 std::vector<std::unique_ptr<MidiMapping>> mappings_;
164
165private:
166 utils::IObjectRegistry &registry_;
167};
168
169}
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(std::span< const dsp::RealtimeMidiEvent > 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