MNX Document Model
Loading...
Searching...
No Matches
CommonClasses.h
1/*
2 * Copyright (C) 2025, Robert Patterson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22#pragma once
23
24#include <utility>
25
26#include "BaseTypes.h"
27#include "Enumerations.h"
28
29namespace mnx {
30
33class Fraction : private Array<unsigned>
34{
35private:
36 using NumType = unsigned;
38
39 static constexpr size_t NUMERATOR_INDEX = 0;
40 static constexpr size_t DENOMINATOR_INDEX = 1;
41
42public:
43 using Initializer = std::pair<NumType, NumType>;
44
46 Fraction(const std::shared_ptr<json>& root, json_pointer pointer)
48 {
49 if (size() != 2) {
50 throw std::invalid_argument("mnx::Fraction must have exactly 2 elements.");
51 }
52 }
53
58 Fraction(Base& parent, const std::string_view& key, const Initializer& value)
59 : ArrayType(parent, key)
60 {
61 push_back(value.first);
62 push_back(value.second);
63 }
64
65 MNX_ARRAY_ELEMENT_PROPERTY(NumType, numerator, NUMERATOR_INDEX);
66 MNX_ARRAY_ELEMENT_PROPERTY(NumType, denominator, DENOMINATOR_INDEX);
67
68 friend class Base;
69};
70
71#ifndef DOXYGEN_SHOULD_IGNORE_THIS
72
73inline bool operator==(const Fraction& a, const Fraction& b)
74{
75 return a.numerator() * b.denominator() == b.numerator() * a.denominator();
76}
77
78inline bool operator!=(const Fraction& a, const Fraction& b) {
79 return !(a == b);
80}
81
82inline bool operator<(const Fraction& a, const Fraction& b) {
83 return a.numerator() * b.denominator() < b.numerator() * a.denominator();
84}
85
86inline bool operator<=(const Fraction& a, const Fraction& b) {
87 return !(b < a);
88}
89
90inline bool operator>(const Fraction& a, const Fraction& b) {
91 return b < a;
92}
93
94inline bool operator>=(const Fraction& a, const Fraction& b) {
95 return !(a < b);
96}
97
98#endif // DOXYGEN_SHOULD_IGNORE_THIS
99
105{
106public:
108 RhythmicPosition(const std::shared_ptr<json>& root, json_pointer pointer)
110 {
111 }
112
117 RhythmicPosition(Base& parent, const std::string_view& key, const Fraction::Initializer& position)
118 : Object(parent, key)
119 {
120 create_fraction(position);
121 }
122
124 MNX_OPTIONAL_PROPERTY(unsigned, graceIndex);
126};
127
133{
134public:
136 MeasureRhythmicPosition(const std::shared_ptr<json>& root, json_pointer pointer)
138 {
139 }
140
146 MeasureRhythmicPosition(Base& parent, const std::string_view& key, int measureId, const Fraction::Initializer& position)
147 : Object(parent, key)
148 {
149 set_measure(measureId);
150 create_position(position);
151 }
152
153 MNX_REQUIRED_PROPERTY(int, measure);
155};
156
161class KeySignature : public Object
162{
163public:
165 KeySignature(const std::shared_ptr<json>& root, json_pointer pointer)
167 {
168 }
169
174 KeySignature(Base& parent, const std::string_view& key, int fifths)
175 : Object(parent, key)
176 {
177 set_fifths(fifths);
178 }
179
180 MNX_OPTIONAL_NAMED_PROPERTY(std::string, styleClass, "class");
181 MNX_OPTIONAL_PROPERTY(std::string, color);
183};
184
189class NoteValue : public Object
190{
191public:
193 {
194 public:
195 NoteValueBase base;
196 unsigned dots;
197
198 Initializer(NoteValueBase inBase, unsigned inDots = 0) : base(inBase), dots(inDots) {}
199 };
200
202 NoteValue(const std::shared_ptr<json>& root, json_pointer pointer)
204 {
205 }
206
211 NoteValue(Base& parent, const std::string_view& key, const Initializer& noteValue)
212 : Object(parent, key)
213 {
214 set_base(noteValue.base);
215 if (noteValue.dots) {
216 set_dots(noteValue.dots);
217 }
218 }
219
222
224 unsigned calcNumberOfFlags() const;
225};
226
232{
233public:
235 NoteValueQuantity(const std::shared_ptr<json>& root, json_pointer pointer)
237 {
238 }
239
245 NoteValueQuantity(Base& parent, const std::string_view& key, unsigned count, const NoteValue::Initializer& noteValue)
246 : Object(parent, key)
247 {
248 set_multiple(count);
249 create_duration(noteValue);
250 }
251
253 MNX_REQUIRED_PROPERTY(unsigned, multiple);
254};
255
260class TimeSignature : public Object
261{
262public:
264 TimeSignature(const std::shared_ptr<json>& root, json_pointer pointer)
266 {
267 }
268
274 TimeSignature(Base& parent, const std::string_view& key, int count, TimeSignatureUnit unit)
275 : Object(parent, key)
276 {
277 set_count(count);
278 set_unit(unit);
279 }
280
283};
284
285} // namespace mnx
Represents an MNX array, encapsulating property access.
Definition BaseTypes.h:483
size_t size() const
Get the size of the array.
Definition BaseTypes.h:526
std::enable_if_t<!std::is_base_of_v< Base, U >, void > push_back(const U &value)
Append a new value to the array. (Available only for primitive types)
Definition BaseTypes.h:559
Base class wrapper for all MNX JSON nodes.
Definition BaseTypes.h:210
json_pointer pointer() const
Returns the json_pointer for this node.
Definition BaseTypes.h:271
T parent() const
Returns the parent object for this node.
Definition BaseTypes.h:258
const std::shared_ptr< json > & root() const
Returns the root.
Definition BaseTypes.h:288
Represents a fraction of a whole note, for measuring musical time.
Definition CommonClasses.h:34
std::pair< NumType, NumType > Initializer
initializer for Fraction class (numerator, denominator)
Definition CommonClasses.h:43
MNX_ARRAY_ELEMENT_PROPERTY(NumType, denominator, DENOMINATOR_INDEX)
the denominator of the fraction
Fraction(Base &parent, const std::string_view &key, const Initializer &value)
Creates a new Array class as a child of a JSON element.
Definition CommonClasses.h:58
MNX_ARRAY_ELEMENT_PROPERTY(NumType, numerator, NUMERATOR_INDEX)
the numerator of the fraction
Fraction(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor to wrap a Fraction instance around existing JSON.
Definition CommonClasses.h:46
Represents a key signature.
Definition CommonClasses.h:162
MNX_OPTIONAL_PROPERTY(std::string, color)
color to use when rendering the key signature
KeySignature(Base &parent, const std::string_view &key, int fifths)
Creates a new KeySignature class as a child of a JSON element.
Definition CommonClasses.h:174
KeySignature(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor for existing KeySignature objects.
Definition CommonClasses.h:165
MNX_OPTIONAL_NAMED_PROPERTY(std::string, styleClass, "class")
style class
MNX_REQUIRED_PROPERTY(int, fifths)
offset from signature with no accidentals
Represents a system on a page in a score.
Definition CommonClasses.h:133
MNX_REQUIRED_PROPERTY(int, measure)
The measure id of the measure of this MeasureRhythmicPosition.
MeasureRhythmicPosition(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor for existing rhythmic position instances.
Definition CommonClasses.h:136
MNX_REQUIRED_CHILD(RhythmicPosition, position)
The metric position, where 1/4 is a quarter note.
MeasureRhythmicPosition(Base &parent, const std::string_view &key, int measureId, const Fraction::Initializer &position)
Creates a new MeasureRhythmicPosition class as a child of a JSON element.
Definition CommonClasses.h:146
Represents a quantity of symbolic note values=.
Definition CommonClasses.h:232
MNX_REQUIRED_CHILD(NoteValue, duration)
duration unit
MNX_REQUIRED_PROPERTY(unsigned, multiple)
quantity of duration units
NoteValueQuantity(Base &parent, const std::string_view &key, unsigned count, const NoteValue::Initializer &noteValue)
Creates a new Barline class as a child of a JSON element.
Definition CommonClasses.h:245
NoteValueQuantity(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor for existing NoteValue instances.
Definition CommonClasses.h:235
Definition CommonClasses.h:193
Represents a symbolic note value (not necessarily a duration)
Definition CommonClasses.h:190
MNX_OPTIONAL_PROPERTY_WITH_DEFAULT(unsigned, dots, 0)
the number of dots
unsigned calcNumberOfFlags() const
Calculates the number of flags or beams required by this note value.
Definition Implementations.cpp:134
NoteValue(Base &parent, const std::string_view &key, const Initializer &noteValue)
Creates a new Barline class as a child of a JSON element.
Definition CommonClasses.h:211
NoteValue(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor for existing NoteValue instances.
Definition CommonClasses.h:202
MNX_REQUIRED_PROPERTY(NoteValueBase, base)
the type ("base") of note
Represents an MNX object, encapsulating property access.
Definition BaseTypes.h:420
Represents a system on a page in a score.
Definition CommonClasses.h:105
RhythmicPosition(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor for existing rhythmic position instances.
Definition CommonClasses.h:108
RhythmicPosition(Base &parent, const std::string_view &key, const Fraction::Initializer &position)
Creates a new RhythmicPosition class as a child of a JSON element.
Definition CommonClasses.h:117
MNX_OPTIONAL_PROPERTY(unsigned, graceIndex)
(0 is the primary, and then count to the left.)
MNX_REQUIRED_CHILD(Fraction, fraction)
The metric position, where 1/4 is a quarter note.
Represents the tempo for a global measure.
Definition CommonClasses.h:261
MNX_REQUIRED_PROPERTY(TimeSignatureUnit, unit)
the unit value (bottom number)
TimeSignature(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor for existing NoteValue instances.
Definition CommonClasses.h:264
TimeSignature(Base &parent, const std::string_view &key, int count, TimeSignatureUnit unit)
Creates a new Barline class as a child of a JSON element.
Definition CommonClasses.h:274
MNX_REQUIRED_PROPERTY(int, count)
the number of beats (top number)
object model for MNX format
json::json_pointer json_pointer
JSON pointer class for MNX.
Definition BaseTypes.h:197
TimeSignatureUnit
Valid units for the lower numbers of time signatures.
Definition Enumerations.h:276
NoteValueBase
The note values allowed in MNX.
Definition Enumerations.h:187