MidiNote.hpp
1 #pragma once
2 #include <score/model/IdentifiedObject.hpp>
3 #include <score/selection/Selectable.hpp>
4 
5 #include <verdigris>
6 
7 namespace Midi
8 {
9 using midi_size_t = uint8_t;
10 struct NoteData
11 {
12  NoteData() = default;
13  NoteData(double s, double d, midi_size_t p, midi_size_t v)
14  : m_start{s}
15  , m_duration{d}
16  , m_pitch{p}
17  , m_velocity{v}
18  {
19  }
20 
21  double start() const { return m_start; }
22  double duration() const { return m_duration; }
23  double end() const { return m_start + m_duration; }
24 
25  midi_size_t pitch() const { return m_pitch; }
26  midi_size_t velocity() const { return m_velocity; }
27 
28  void setStart(double s) { m_start = s; }
29  void setDuration(double s) { m_duration = s; }
30  void setPitch(midi_size_t s) { m_pitch = s; }
31  void setVelocity(midi_size_t s) { m_velocity = s; }
32 
33  double m_start{};
34  double m_duration{};
35 
36  midi_size_t m_pitch{};
37  midi_size_t m_velocity{};
38 };
39 
41 {
42  bool operator()(const NoteData& lhs, const NoteData& rhs) const
43  {
44  return lhs.m_start < rhs.m_start;
45  }
46  bool operator()(const NoteData& lhs, double rhs) const { return lhs.m_start < rhs; }
47 };
48 
58 class Note final : public IdentifiedObject<Note>
59 {
60  W_OBJECT(Note)
61  SCORE_SERIALIZE_FRIENDS
62 
63 public:
64  Selectable selection{this};
65 
66  Note(const Id<Note>& id, QObject* parent);
67  Note(const Id<Note>& id, NoteData n, QObject* parent);
68 
69  template <IsDeserializer DeserializerVisitor>
70  Note(DeserializerVisitor&& vis, QObject* parent)
71  : IdentifiedObject<Note>{vis, parent}
72  {
73  vis.writeTo(*this);
74  }
75 
76  // Both are between 0 - 1, 1 being the process duration.
77 
78  double start() const noexcept { return m_start; }
79 
80  double duration() const noexcept { return m_duration; }
81 
82  double end() const noexcept { return m_start + m_duration; }
83 
84  midi_size_t pitch() const noexcept { return m_pitch; }
85 
86  midi_size_t velocity() const noexcept { return m_velocity; }
87 
88  void scale(double s) noexcept;
89 
90  void setStart(double s) noexcept;
91 
92  void setDuration(double s) noexcept;
93 
94  void setPitch(midi_size_t s) noexcept;
95 
96  void setVelocity(midi_size_t s) noexcept;
97 
98  NoteData noteData() const noexcept;
99 
100  void setData(NoteData d) noexcept;
101 
102 public:
103  void noteChanged() W_SIGNAL(noteChanged);
104 
105 private:
106  double m_start{};
107  double m_duration{};
108 
109  midi_size_t m_pitch{};
110  midi_size_t m_velocity{64};
111 };
112 }
The IdentifiedObject class.
Definition: IdentifiedObject.hpp:19
The Note class.
Definition: MidiNote.hpp:59
The Selectable class.
Definition: Selectable.hpp:14
The id_base_t class.
Definition: Identifier.hpp:57
Definition: MidiNote.hpp:41
Definition: MidiNote.hpp:11