Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
piano_roll_track.h
1// SPDX-FileCopyrightText: © 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "dsp/midi_event.h"
7
8#include <QtQmlIntegration/qqmlintegration.h>
9
10namespace zrythm::structure::tracks
11{
15class PianoRollTrackMixin : public QObject
16{
17 Q_OBJECT
18 Q_PROPERTY (
19 bool drumMode READ drumMode WRITE setDrumMode NOTIFY drumModeChanged)
20 Q_PROPERTY (
21 bool passthroughMidiInput READ passthroughMidiInput WRITE
22 setPassthroughMidiInput NOTIFY passthroughMidiInputChanged)
23 Q_PROPERTY (
24 std::uint8_t midiChannel READ midiChannel WRITE setMidiChannel NOTIFY
25 midiChannelChanged)
26 QML_ELEMENT
27 QML_UNCREATABLE ("")
28public:
29 PianoRollTrackMixin (QObject * parent = nullptr);
30 Q_DISABLE_COPY_MOVE (PianoRollTrackMixin)
31 ~PianoRollTrackMixin () override = default;
32
33 // ========================================================================
34 // QML Interface
35 // ========================================================================
36
37 std::uint8_t midiChannel () const { return midi_ch_; }
38 void setMidiChannel (std::uint8_t midi_ch)
39 {
40 midi_ch = std::clamp (
41 midi_ch, static_cast<std::uint8_t> (1), static_cast<std::uint8_t> (16));
42 if (midi_ch_ == midi_ch)
43 return;
44
45 midi_ch_ = midi_ch;
46 Q_EMIT midiChannelChanged (midi_ch);
47 }
48 Q_SIGNAL void midiChannelChanged (std::uint8_t midi_ch);
49
50 bool passthroughMidiInput () const { return passthrough_midi_input_; }
51 void setPassthroughMidiInput (bool passthrough)
52 {
53 if (passthrough_midi_input_ == passthrough)
54 return;
55
56 passthrough_midi_input_ = passthrough;
57 Q_EMIT passthroughMidiInputChanged (passthrough);
58 }
59 Q_SIGNAL void passthroughMidiInputChanged (bool passthrough);
60
61 bool drumMode () const { return drum_mode_; }
62 void setDrumMode (bool drum_mode)
63 {
64 if (drum_mode_ == drum_mode)
65 return;
66
67 drum_mode_ = drum_mode;
68 Q_EMIT drumModeChanged (drum_mode);
69 }
70 Q_SIGNAL void drumModeChanged (bool drum_mode);
71
72 // ========================================================================
73
74 bool allow_only_specific_midi_channels () const
75 {
76 return midi_channels_.has_value ();
77 }
78 auto &midi_channels_to_allow () const { return midi_channels_.value (); }
79
84 {
85 // change the MIDI channel on the midi input to the channel set on the track
86 if (!passthrough_midi_input_)
87 {
88 events.set_channel (midi_ch_);
89 }
90 }
91
92private:
93 static constexpr auto kDrumModeKey = "drumMode"sv;
94 static constexpr auto kMidiChKey = "midiChannel"sv;
95 static constexpr auto kPassthroughMidiInputKey = "passthroughMidiInput"sv;
96 static constexpr auto kMidiChannelsKey = "midiChannels"sv;
97 friend void to_json (nlohmann::json &j, const PianoRollTrackMixin &track);
98 friend void from_json (const nlohmann::json &j, PianoRollTrackMixin &track);
99
100 friend void init_from (
101 PianoRollTrackMixin &obj,
102 const PianoRollTrackMixin &other,
103 utils::ObjectCloneType clone_type);
104
105private:
109 bool drum_mode_ = false;
110
112 midi_byte_t midi_ch_ = 1;
113
121 std::atomic_bool passthrough_midi_input_ = false;
122
128 std::optional<std::array<bool, 16>> midi_channels_;
129};
130}
A lock-free thread-safe vector of MidiEvents.
Definition midi_event.h:74
void set_channel(midi_byte_t channel)
Sets the given MIDI channel on all applicable MIDI events.
void transform_midi_inputs_func(dsp::MidiEventVector &events) const
Callback to be used as TrackProcessor's transform function.
std::uint8_t midi_byte_t
MIDI byte.
Definition midi.h:43