-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathinstrchange.cpp
More file actions
216 lines (189 loc) · 7.81 KB
/
instrchange.cpp
File metadata and controls
216 lines (189 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-Studio-CLA-applies
*
* MuseScore Studio
* Music Composition & Notation
*
* Copyright (C) 2021 MuseScore Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "instrchange.h"
#include "translation.h"
#include "../editing/editinstrumentchange.h"
#include "../editing/transpose.h"
#include "keysig.h"
#include "measure.h"
#include "mscore.h"
#include "part.h"
#include "score.h"
#include "segment.h"
#include "staff.h"
#include "log.h"
using namespace mu;
namespace mu::engraving {
//---------------------------------------------------------
// instrumentChangeStyle
//---------------------------------------------------------
static const ElementStyle instrumentChangeStyle {
{ Sid::instrumentChangePlacement, Pid::PLACEMENT },
{ Sid::instrumentChangeMinDistance, Pid::MIN_DISTANCE },
};
//---------------------------------------------------------
// InstrumentChange
//---------------------------------------------------------
InstrumentChange::InstrumentChange(EngravingItem* parent)
: TextBase(ElementType::INSTRUMENT_CHANGE, parent, TextStyleType::INSTRUMENT_CHANGE, ElementFlag::MOVABLE | ElementFlag::ON_STAFF)
{
initElementStyle(&instrumentChangeStyle);
m_instrument = new Instrument();
}
InstrumentChange::InstrumentChange(const Instrument& i, EngravingItem* parent)
: TextBase(ElementType::INSTRUMENT_CHANGE, parent, TextStyleType::INSTRUMENT_CHANGE, ElementFlag::MOVABLE | ElementFlag::ON_STAFF)
{
initElementStyle(&instrumentChangeStyle);
m_instrument = new Instrument(i);
}
InstrumentChange::InstrumentChange(const InstrumentChange& is)
: TextBase(is)
{
m_instrument = new Instrument(*is.m_instrument);
m_init = is.m_init;
}
InstrumentChange::~InstrumentChange()
{
delete m_instrument;
}
void InstrumentChange::setInstrument(const Instrument& i)
{
*m_instrument = i;
//delete _instrument;
//_instrument = new Instrument(i);
}
void InstrumentChange::setupInstrument(const Instrument* instrument)
{
if (!m_init) {
return;
}
Fraction tickStart = segment()->tick();
Part* part = staff()->part();
Interval oldV = part->instrument(tickStart)->transpose();
Interval oldKv = staff()->transpose(tickStart);
Interval v = instrument->transpose();
bool concPitch = style().styleB(Sid::concertPitch);
// change the clef for each staff
for (size_t i = 0; i < part->nstaves(); i++) {
ClefType oldClefType = concPitch ? part->instrument(tickStart)->clefType(i).concertClef
: part->instrument(tickStart)->clefType(i).transposingClef;
ClefType newClefType = concPitch ? instrument->clefType(i).concertClef
: instrument->clefType(i).transposingClef;
// Introduce cleff change only if the new clef *symbol* is different from the old one
if (ClefInfo::symId(oldClefType) != ClefInfo::symId(newClefType)) {
// If instrument change is at the start of a measure, use the measure as the element, as this will place the instrument change before the barline.
EngravingItem* element = rtick().isZero() ? toEngravingItem(findMeasure()) : toEngravingItem(this);
score()->undoChangeClef(part->staff(i), element, newClefType, true);
}
}
// Change key signature if necessary. CAUTION: not necessary in case of octave-transposing!
if ((v.chromatic - oldV.chromatic) % 12) {
for (size_t i = 0; i < part->nstaves(); i++) {
if (!part->staff(i)->keySigEvent(tickStart).isAtonal()) {
KeySigEvent ks;
// Check, if some key signature is already there, if no, mark new one "for instrument change"
Segment* seg = segment()->prev1(SegmentType::KeySig);
voice_idx_t voice = part->staff(i)->idx() * VOICES;
KeySig* ksig = seg ? toKeySig(seg->element(voice)) : nullptr;
bool forInstChange = !(ksig && ksig->tick() == tickStart && !ksig->generated());
ks.setForInstrumentChange(forInstChange);
Key cKey = part->staff(i)->concertKey(tickStart);
ks.setConcertKey(cKey);
score()->undoChangeKeySig(part->staff(i), tickStart, ks);
}
}
}
// change instrument in all linked scores
for (EngravingObject* se : linkList()) {
InstrumentChange* lic = static_cast<InstrumentChange*>(se);
Instrument* newInstrument = new Instrument(*instrument);
lic->score()->undo(new ChangeInstrument(lic, newInstrument));
}
// transpose for current score only
// this automatically propagates to linked scores
if (part->instrument(tickStart)->transpose() != oldV) {
auto i = part->instruments().upper_bound(tickStart.ticks()); // find(), ++i
Fraction tickEnd;
if (i == part->instruments().end()) {
tickEnd = Fraction(-1, 1);
} else {
tickEnd = Fraction::fromTicks(i->first);
}
Transpose::transpositionChanged(score(), part, oldKv, tickStart, tickEnd);
}
//: The text of an "instrument change" marking. It is an instruction to the player to switch to another instrument.
const String newInstrChangeText = muse::mtrc("engraving", "To %1").arg(instrument->trackNameWithTrait());
undoChangeProperty(Pid::TEXT, TextBase::plainToXmlText(newInstrChangeText));
}
//---------------------------------------------------------
// keySigs
//---------------------------------------------------------
std::vector<KeySig*> InstrumentChange::keySigs(bool all) const
{
std::vector<KeySig*> keysigs;
Segment* seg = segment()->prev1(SegmentType::KeySig);
if (seg) {
voice_idx_t startVoice = part()->staff(0)->idx() * VOICES;
voice_idx_t endVoice = part()->staff(part()->nstaves() - 1)->idx() * VOICES;
Fraction t = tick();
for (voice_idx_t i = startVoice; i <= endVoice; i += VOICES) {
KeySig* ks = toKeySig(seg->element(i));
if (ks && (all || ks->forInstrumentChange()) && ks->tick() == t) {
keysigs.push_back(ks);
}
}
}
return keysigs;
}
//---------------------------------------------------------
// clefs
//---------------------------------------------------------
std::vector<Clef*> InstrumentChange::clefs() const
{
std::vector<Clef*> clefs;
Segment* seg = segment()->prev1(SegmentType::Clef);
if (seg) {
voice_idx_t startVoice = part()->staff(0)->idx() * VOICES;
voice_idx_t endVoice = part()->staff(part()->nstaves() - 1)->idx() * VOICES;
Fraction t = tick();
for (voice_idx_t i = startVoice; i <= endVoice; i += VOICES) {
Clef* clef = toClef(seg->element(i));
if (clef && clef->forInstrumentChange() && clef->tick() == t) {
clefs.push_back(clef);
}
}
}
return clefs;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
engraving::PropertyValue InstrumentChange::propertyDefault(Pid propertyId) const
{
switch (propertyId) {
case Pid::TEXT_STYLE:
return TextStyleType::INSTRUMENT_CHANGE;
default:
return TextBase::propertyDefault(propertyId);
}
}
}