Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
midi_note.h
1// SPDX-FileCopyrightText: © 2018-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "zrythm-config.h"
7
8#include <cstdint>
9#include <memory>
10
11#include "dsp/position.h"
12#include "gui/dsp/bounded_object.h"
13#include "gui/dsp/muteable_object.h"
14#include "gui/dsp/velocity.h"
15
21
25class MidiNote final
26 : public QObject,
27 public MuteableObject,
28 public RegionOwnedObject,
29 public BoundedObject,
30 public ICloneable<MidiNote>
31{
32 Q_OBJECT
33 QML_ELEMENT
34 DEFINE_ARRANGER_OBJECT_QML_PROPERTIES (MidiNote)
35 DEFINE_BOUNDED_OBJECT_QML_PROPERTIES (MidiNote)
36 Q_PROPERTY (int pitch READ getPitch WRITE setPitch NOTIFY pitchChanged)
37
38public:
39 using RegionT = MidiRegion;
40
41 DECLARE_FINAL_ARRANGER_OBJECT_CONSTRUCTORS (MidiNote)
42
43 Q_DISABLE_COPY_MOVE (MidiNote)
44 ~MidiNote () override = default;
45
46 enum class Notation
47 {
48 Musical,
49 Pitch,
50 };
51
52 // ========================================================================
53 // QML Interface
54 // ========================================================================
55
56 int getPitch () const { return static_cast<int> (pitch_); }
57
58 void setPitch (int ipitch)
59 {
60 const auto pitch = static_cast<midi_byte_t> (ipitch);
61 if (pitch_ != pitch)
62 {
63 pitch_ = pitch;
64 Q_EMIT pitchChanged ();
65 }
66 }
67 Q_SIGNAL void pitchChanged ();
68
69 // ========================================================================
70
71 void set_cache_val (const uint8_t val) { cache_pitch_ = val; }
72
79 get_val_as_string (Notation notation, bool use_markup) const;
80
87 void listen (bool listen);
88
94 void shift_pitch (int delta);
95
96 void set_velocity (int vel) { vel_->setValue (vel); }
97
103 void set_pitch (uint8_t val);
104
105 ArrangerObjectPtrVariant
106 add_clone_to_project (bool fire_events) const override;
107
108 ArrangerObjectPtrVariant insert_clone_to_project () const override;
109
110 // friend bool operator== (const MidiNote &lhs, const MidiNote &rhs);
111
113
114 bool
115 validate (bool is_project, dsp::FramesPerTick frames_per_tick) const override;
116
117 void init_after_cloning (const MidiNote &other, ObjectCloneType clone_type)
118 override;
119
120private:
121 static constexpr std::string_view kVelocityKey = "velocity";
122 static constexpr std::string_view kPitchKey = "pitch";
123 friend void to_json (nlohmann::json &j, const MidiNote &note)
124 {
125 to_json (j, static_cast<const ArrangerObject &> (note));
126 to_json (j, static_cast<const BoundedObject &> (note));
127 to_json (j, static_cast<const MuteableObject &> (note));
128 to_json (j, static_cast<const RegionOwnedObject &> (note));
129 j[kVelocityKey] = note.vel_;
130 j[kPitchKey] = note.pitch_;
131 }
132 friend void from_json (const nlohmann::json &j, MidiNote &note)
133 {
134 from_json (j, static_cast<ArrangerObject &> (note));
135 from_json (j, static_cast<BoundedObject &> (note));
136 from_json (j, static_cast<MuteableObject &> (note));
137 from_json (j, static_cast<RegionOwnedObject &> (note));
138 j.at (kVelocityKey).get_to (*note.vel_);
139 j.at (kPitchKey).get_to (note.pitch_);
140 }
141
142public:
145
147 uint8_t pitch_{};
148
150 uint8_t cache_pitch_{};
151
154
158};
159
160inline bool
161operator== (const MidiNote &lhs, const MidiNote &rhs)
162{
163 return lhs.pitch_ == rhs.pitch_ && *lhs.vel_ == *rhs.vel_
164 && static_cast<const MuteableObject &> (lhs)
165 == static_cast<const MuteableObject &> (rhs)
166 && static_cast<const RegionOwnedObject &> (lhs)
167 == static_cast<const RegionOwnedObject &> (rhs)
168 && static_cast<const BoundedObject &> (lhs)
169 == static_cast<const BoundedObject &> (rhs)
170 && static_cast<const ArrangerObject &> (lhs)
171 == static_cast<const ArrangerObject &> (rhs);
172}
173
175 return fmt::format (
176 "MidiNote [{} ~ {}]: note {}, vel {}", mn.get_position (),
177 mn.get_end_position (), mn.pitch_, mn.vel_->vel_);
178});
179
Base class for all objects in the arranger.
auto get_position() const
Getter.
Base class for all objects in the arranger that have a length.
auto get_end_position() const
Getter.
A MIDI note inside a Region shown in the piano roll.
Definition midi_note.h:31
utils::Utf8String get_val_as_string(Notation notation, bool use_markup) const
Gets the MIDI note's value as a string (eg "C#4").
bool currently_listened_
Whether or not this note is currently listened to.
Definition midi_note.h:153
bool validate(bool is_project, dsp::FramesPerTick frames_per_tick) const override
Validates the arranger object.
uint8_t last_listened_pitch_
The note/pitch that is currently playing, if currently_listened_ is true.
Definition midi_note.h:157
void init_after_cloning(const MidiNote &other, ObjectCloneType clone_type) override
Initializes the cloned object.
ArrangerObjectPtrVariant insert_clone_to_project() const override
Inserts the object where it belongs in the project (eg, a Track).
Velocity * vel_
Velocity.
Definition midi_note.h:144
uint8_t pitch_
The note/pitch, (0-127).
Definition midi_note.h:147
void set_pitch(uint8_t val)
Sends a note off if currently playing and sets the pitch of the MidiNote.
void shift_pitch(int delta)
Shifts MidiNote's position and/or value.
void listen(bool listen)
Listen to the given MidiNote.
ArrangerObjectPtrVariant add_clone_to_project(bool fire_events) const override
Appends the ArrangerObject to where it belongs in the project (eg, a Track), without taking into acco...
utils::Utf8String gen_human_friendly_name() const override
Generates a human readable name for the object.
uint8_t cache_pitch_
Cached note, for live operations.
Definition midi_note.h:150
The MidiNote velocity.
Definition velocity.h:29
VelocityValueT vel_
Pointer back to the MIDI note (this is also the QObject parent).
Definition velocity.h:93
Lightweight UTF-8 string wrapper with safe conversions.
Definition string.h:39
#define DEFINE_OBJECT_FORMATTER(obj_type, function_prefix, formatter_func)
Defines a formatter for the given object type.
Definition format.h:80
ObjectCloneType
Definition icloneable.h:25
uint8_t midi_byte_t
MIDI byte.
Definition types.h:59