MUSX Document Model
Loading...
Searching...
No Matches
Instrument.h
1/*
2 * Copyright (C) 2026, 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 <cstddef>
25#include <map>
26#include <string_view>
27#include <unordered_map>
28#include <vector>
29
30#include "CommonClasses.h"
31
32namespace musx {
33namespace dom {
34
35namespace others {
36class StaffComposite;
37}
38
40using InstrumentUuid = std::string_view;
41
44{
46 Brass,
47 Keyboard,
48 Organ,
50 PitchedPercussion,
51 PluckedStrings,
52 Strings,
53 Voice,
54 Woodwinds,
55 Synth,
56 Other
57};
58
61
69class InstrumentMap;
70
72{
73public:
76 std::string instUuid;
77
79 bool operator==(const InstrumentIdentity& other) const
80 { return instUuid == other.instUuid; }
81
83 bool operator<(const InstrumentIdentity& other) const
84 { return instUuid < other.instUuid; }
85 };
86
92
94 using InstrumentChangeEvents = std::map<MusicPoint, InstrumentChange>;
95
99 explicit InstrumentInfo(const DocumentWeakPtr& document, Cmper partId = SCORE_PARTID)
100 : DocumentElement(document, partId) {}
101
102 std::unordered_map<StaffCmper, size_t> staves;
105
107 std::vector<StaffCmper> getSequentialStaves() const;
108
117
119 std::vector<InstrumentIdentity> getInstrumentIdentities() const;
120
123 InstrumentIdentity getInstrumentIdentityAt(MusicPoint point) const;
124};
125
130class InstrumentMap : public DocumentElement, private std::unordered_map<StaffCmper, InstrumentInfo>
131{
132 // Private inheritance keeps the map implementation detail out of the public API and avoids any
133 // accidental polymorphic use of a standard container with a non-virtual destructor.
134public:
135 using typename std::unordered_map<StaffCmper, InstrumentInfo>::key_type;
136 using typename std::unordered_map<StaffCmper, InstrumentInfo>::mapped_type;
137 using typename std::unordered_map<StaffCmper, InstrumentInfo>::value_type;
138 using typename std::unordered_map<StaffCmper, InstrumentInfo>::hasher;
139 using typename std::unordered_map<StaffCmper, InstrumentInfo>::key_equal;
140 using typename std::unordered_map<StaffCmper, InstrumentInfo>::allocator_type;
141 using typename std::unordered_map<StaffCmper, InstrumentInfo>::size_type;
142 using typename std::unordered_map<StaffCmper, InstrumentInfo>::difference_type;
143 using typename std::unordered_map<StaffCmper, InstrumentInfo>::reference;
144 using typename std::unordered_map<StaffCmper, InstrumentInfo>::const_reference;
145 using typename std::unordered_map<StaffCmper, InstrumentInfo>::pointer;
146 using typename std::unordered_map<StaffCmper, InstrumentInfo>::const_pointer;
147 using typename std::unordered_map<StaffCmper, InstrumentInfo>::iterator;
148 using typename std::unordered_map<StaffCmper, InstrumentInfo>::const_iterator;
149 using typename std::unordered_map<StaffCmper, InstrumentInfo>::local_iterator;
150 using typename std::unordered_map<StaffCmper, InstrumentInfo>::const_local_iterator;
151
155 explicit InstrumentMap(const DocumentWeakPtr& document, Cmper partId = SCORE_PARTID)
156 : DocumentElement(document, partId) {}
157
158 using std::unordered_map<StaffCmper, InstrumentInfo>::begin;
159 using std::unordered_map<StaffCmper, InstrumentInfo>::end;
160 using std::unordered_map<StaffCmper, InstrumentInfo>::empty;
161 using std::unordered_map<StaffCmper, InstrumentInfo>::size;
162 using std::unordered_map<StaffCmper, InstrumentInfo>::clear;
163 using std::unordered_map<StaffCmper, InstrumentInfo>::find;
164 using std::unordered_map<StaffCmper, InstrumentInfo>::count;
165 using std::unordered_map<StaffCmper, InstrumentInfo>::emplace;
166 using std::unordered_map<StaffCmper, InstrumentInfo>::insert;
167 using std::unordered_map<StaffCmper, InstrumentInfo>::erase;
168 using std::unordered_map<StaffCmper, InstrumentInfo>::operator[];
169 using std::unordered_map<StaffCmper, InstrumentInfo>::at;
170
174 const InstrumentInfo* getInstrumentForStaff(StaffCmper staffId) const;
175};
176
177} // namespace dom
178} // namespace musx
Base for DOM classes that belong to a Document.
Definition DocumentElement.h:46
Derived description of a logical instrument in a document.
Definition Instrument.h:72
std::vector< StaffCmper > getSequentialStaves() const
Returns the staffIds in sequence as they appear in Scroll View in the score.
Definition Instrument.cpp:878
Cmper multistaffGroupId
The others::MultiStaffInstrumentGroup that defines the instrument. (May be zero.)
Definition Instrument.h:104
std::vector< InstrumentIdentity > getInstrumentIdentities() const
Returns the unique instrument identities in order of first appearance.
Definition Instrument.cpp:955
std::map< MusicPoint, InstrumentChange > InstrumentChangeEvents
Effective top-staff states keyed by the musical location where each state begins.
Definition Instrument.h:94
InstrumentInfo(const DocumentWeakPtr &document, Cmper partId=SCORE_PARTID)
Constructs an empty instrument info for a score or linked part.
Definition Instrument.h:99
InstrumentChangeEvents getChanges() const
Returns effective instrument states for this logical instrument.
Definition Instrument.cpp:892
Cmper staffGroupId
The details::StaffGroup that visually represents the instrument. (May be zero.)
Definition Instrument.h:103
std::unordered_map< StaffCmper, size_t > staves
List of each staffId with its sequence index from top to bottom.
Definition Instrument.h:102
InstrumentIdentity getInstrumentIdentityAt(MusicPoint point) const
Returns the instrument identity in effect at the specified music point.
Definition Instrument.cpp:967
A list of instruments, which may be single- or multi-staff.
Definition Instrument.h:131
const InstrumentInfo * getInstrumentForStaff(StaffCmper staffId) const
Get the instrument info for the given staffId in the given map.
Definition Instrument.cpp:977
InstrumentMap(const DocumentWeakPtr &document, Cmper partId=SCORE_PARTID)
Constructs an empty instrument map for a score or linked part.
Definition Instrument.h:155
Utility class that represents a single location in musical time.
Definition CommonClasses.h:435
@ Other
Other/unknown text encoding.
@ Unspecified
contour is automatic, indeterminate, or does not apply
std::shared_ptr< const T > MusxInstance
Defines the type of a musx instance stored in a pool.
Definition MusxInstance.h:40
constexpr Cmper SCORE_PARTID
The part id of the score.
Definition Fundamentals.h:81
InstrumentFamily
Broad source-domain family for a Finale instrument UUID.
Definition Instrument.h:44
std::string_view InstrumentUuid
Lightweight view of a Finale instrument UUID value.
Definition Instrument.h:40
uint16_t Cmper
Enigma "comperator" key type.
Definition Fundamentals.h:57
@ Percussion
Percussion clef, open rectangle (no pitch).
std::weak_ptr< Document > DocumentWeakPtr
Shared weak Document pointer.
Definition DocumentElement.h:37
InstrumentFamily instrumentFamilyFromUuid(InstrumentUuid uuid)
Returns the broad source-domain family for a Finale instrument UUID.
Definition Instrument.cpp:871
int16_t StaffCmper
Enigma staff (staffId) Cmper (may be negative when not applicable)
Definition Fundamentals.h:67
object model for musx file (enigmaxml)
Definition BaseClasses.h:38
Effective instrument state beginning at a musical location.
Definition Instrument.h:88
InstrumentIdentity identity
Stable identity for the effective instrument.
Definition Instrument.h:89
MusxInstance< others::StaffComposite > topStaffComposite
Effective state of the top staff.
Definition Instrument.h:90
Stable identity for a distinct instrument used by this logical instrument.
Definition Instrument.h:75
std::string instUuid
The effective instrument UUID.
Definition Instrument.h:76
bool operator==(const InstrumentIdentity &other) const
Equality comparison operator.
Definition Instrument.h:79
bool operator<(const InstrumentIdentity &other) const
Ordering operator for use in ordered containers.
Definition Instrument.h:83