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 Interval : public Object
162{
163public:
165 Interval(const std::shared_ptr<json>& root, json_pointer pointer)
167 {
168 }
169
175 Interval(Base& parent, const std::string_view& key, int staffDistance, int halfSteps)
176 : Object(parent, key)
177 {
178 set_halfSteps(halfSteps);
179 set_staffDistance(staffDistance);
180 }
181
182 MNX_REQUIRED_PROPERTY(int, halfSteps);
183 MNX_REQUIRED_PROPERTY(int, staffDistance);
184};
185
190class KeySignature : public Object
191{
192public:
194 KeySignature(const std::shared_ptr<json>& root, json_pointer pointer)
196 {
197 }
198
203 KeySignature(Base& parent, const std::string_view& key, int fifths)
204 : Object(parent, key)
205 {
206 set_fifths(fifths);
207 }
208
209 MNX_OPTIONAL_NAMED_PROPERTY(std::string, styleClass, "class");
210 MNX_OPTIONAL_PROPERTY(std::string, color);
212};
213
218class NoteValue : public Object
219{
220public:
223 {
224 public:
226 unsigned dots;
227
229 Initializer(NoteValueBase inBase, unsigned inDots = 0) : base(inBase), dots(inDots) {}
230 };
231
233 NoteValue(const std::shared_ptr<json>& root, json_pointer pointer)
235 {
236 }
237
242 NoteValue(Base& parent, const std::string_view& key, const Initializer& noteValue)
243 : Object(parent, key)
244 {
245 set_base(noteValue.base);
246 if (noteValue.dots) {
247 set_dots(noteValue.dots);
248 }
249 }
250
253
255 unsigned calcNumberOfFlags() const;
256};
257
263{
264public:
266 NoteValueQuantity(const std::shared_ptr<json>& root, json_pointer pointer)
268 {
269 }
270
276 NoteValueQuantity(Base& parent, const std::string_view& key, unsigned count, const NoteValue::Initializer& noteValue)
277 : Object(parent, key)
278 {
279 set_multiple(count);
280 create_duration(noteValue);
281 }
282
284 MNX_REQUIRED_PROPERTY(unsigned, multiple);
285};
286
291class TimeSignature : public Object
292{
293public:
295 TimeSignature(const std::shared_ptr<json>& root, json_pointer pointer)
297 {
298 }
299
305 TimeSignature(Base& parent, const std::string_view& key, int count, TimeSignatureUnit unit)
306 : Object(parent, key)
307 {
308 set_count(count);
309 set_unit(unit);
310 }
311
314};
315
316} // namespace mnx
Represents an MNX array, encapsulating property access.
Definition BaseTypes.h:487
size_t size() const
Get the size of the array.
Definition BaseTypes.h:530
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:563
Base class wrapper for all MNX JSON nodes.
Definition BaseTypes.h:211
json_pointer pointer() const
Returns the json_pointer for this node.
Definition BaseTypes.h:272
T parent() const
Returns the parent object for this node.
Definition BaseTypes.h:259
const std::shared_ptr< json > & root() const
Returns the root.
Definition BaseTypes.h:289
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 musical chromatic interval.
Definition CommonClasses.h:162
MNX_REQUIRED_PROPERTY(int, halfSteps)
the number of 12-EDO chromatic halfsteps in the interval (negative is down)
Interval(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor for existing NoteValue instances.
Definition CommonClasses.h:165
MNX_REQUIRED_PROPERTY(int, staffDistance)
the number of diatonic steps in the interval (negative is down)
Interval(Base &parent, const std::string_view &key, int staffDistance, int halfSteps)
Creates a new Barline class as a child of a JSON element.
Definition CommonClasses.h:175
Represents a key signature.
Definition CommonClasses.h:191
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:203
KeySignature(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor for existing KeySignature objects.
Definition CommonClasses.h:194
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:263
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:276
NoteValueQuantity(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor for existing NoteValue instances.
Definition CommonClasses.h:266
initializer class for NoteValue
Definition CommonClasses.h:223
Initializer(NoteValueBase inBase, unsigned inDots=0)
constructor
Definition CommonClasses.h:229
unsigned dots
the number of dots to initialize
Definition CommonClasses.h:226
NoteValueBase base
the note value base to initialize
Definition CommonClasses.h:225
Represents a symbolic note value (not necessarily a duration)
Definition CommonClasses.h:219
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:143
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:242
NoteValue(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor for existing NoteValue instances.
Definition CommonClasses.h:233
MNX_REQUIRED_PROPERTY(NoteValueBase, base)
the type ("base") of note
Represents an MNX object, encapsulating property access.
Definition BaseTypes.h:421
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:292
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:295
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:305
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:198
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