Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "playbackcontext.h"
24 :
25 : #include <cmath>
26 :
27 : #include "dom/lyrics.h"
28 : #include "dom/measure.h"
29 : #include "dom/measurerepeat.h"
30 : #include "dom/part.h"
31 : #include "dom/playtechannotation.h"
32 : #include "dom/repeatlist.h"
33 : #include "dom/score.h"
34 : #include "dom/segment.h"
35 : #include "dom/soundflag.h"
36 : #include "dom/staff.h"
37 : #include "dom/stafftext.h"
38 :
39 : #include "engraving/automation/automationdata.h"
40 :
41 : #include "utils/arrangementutils.h"
42 : #include "utils/expressionutils.h"
43 : #include "types/constants.h"
44 :
45 : #include "global/containers.h"
46 : #include "global/realfn.h"
47 :
48 : using namespace mu::engraving;
49 : using namespace muse;
50 : using namespace muse::mpe;
51 :
52 9 : static bool soundFlagPlayable(const SoundFlag* flag)
53 : {
54 9 : if (flag && flag->play()) {
55 9 : return !flag->soundPresets().empty() || !flag->playingTechnique().empty();
56 : }
57 :
58 0 : return false;
59 : }
60 :
61 26 : static dynamic_level_t toDynamicLevel(double normalizedValue)
62 : {
63 26 : return static_cast<dynamic_level_t>(std::lround(normalizedValue * MAX_DYNAMIC_LEVEL));
64 : }
65 :
66 : template<typename ByTrackMap, typename ResultMap, typename MergeFn>
67 233 : static void collectByTrackRange(const ByTrackMap& byTrackMap, const track_idx_t trackFrom, const track_idx_t trackTo,
68 : const Score* score, const std::set<track_idx_t>& usedTracks, ResultMap& result, MergeFn merge)
69 : {
70 257 : for (auto it = byTrackMap.lower_bound(trackFrom); it != byTrackMap.end() && it->first < trackTo; ++it) {
71 24 : if (!muse::contains(usedTracks, it->first)) {
72 18 : continue;
73 : }
74 :
75 35 : for (const auto& pair : it->second) {
76 29 : const timestamp_t timestamp = timestampFromTicks(score, pair.first);
77 29 : merge(result[timestamp], pair.second);
78 : }
79 : }
80 233 : }
81 :
82 79 : PlaybackContext::PlaybackContext(const Score* score)
83 79 : : m_score(score)
84 : {
85 79 : }
86 :
87 1817 : dynamic_level_t PlaybackContext::appliableDynamicLevel(const track_idx_t trackIdx, const int nominalPositionTick) const
88 : {
89 1818 : TRACEFUNC;
90 :
91 1817 : const AutomationCurve* curve = dynamicsCurve(trackIdx);
92 1817 : if (!curve) {
93 0 : return NATURAL_DYNAMIC_LEVEL;
94 : }
95 :
96 1817 : const std::optional<real_t> value = mu::engraving::evaluateCurveAt(*curve, nominalPositionTick);
97 1817 : return value ? toDynamicLevel(*value) : NATURAL_DYNAMIC_LEVEL;
98 1817 : }
99 :
100 1840 : std::pair<mpe::timestamp_t, PlayingTechniqueType> PlaybackContext::playingTechnique(const track_idx_t trackIdx,
101 : const int nominalPositionTick) const
102 : {
103 1840 : auto mapIt = m_playTechniquesByTrack.find(trackIdx);
104 1840 : if (mapIt == m_playTechniquesByTrack.cend()) {
105 1669 : return std::make_pair(0, PlayingTechniqueType::Natural);
106 : }
107 :
108 171 : const PlayTechniquesMap& playTechniquesMap = mapIt->second;
109 :
110 171 : auto it = findLessOrEqual(playTechniquesMap, nominalPositionTick);
111 171 : if (it == playTechniquesMap.cend()) {
112 6 : return std::make_pair(0, PlayingTechniqueType::Natural);
113 : }
114 :
115 165 : return std::make_pair(timestampFromTicks(m_score, it->first), it->second);
116 : }
117 :
118 13 : muse::mpe::timestamp_t PlaybackContext::findPlayingTechniqueTimestamp(const track_idx_t trackIdx, PlayingTechniqueType type,
119 : const int startFromTick) const
120 : {
121 13 : auto mapIt = m_playTechniquesByTrack.find(trackIdx);
122 13 : if (mapIt == m_playTechniquesByTrack.cend()) {
123 0 : return -1;
124 : }
125 :
126 13 : const PlayTechniquesMap& playTechniquesMap = mapIt->second;
127 :
128 13 : auto it = playTechniquesMap.upper_bound(startFromTick);
129 :
130 13 : for (; it != playTechniquesMap.end(); ++it) {
131 5 : if (it->second == type) {
132 5 : return timestampFromTicks(m_score, it->first);
133 : }
134 : }
135 :
136 8 : return -1;
137 : }
138 :
139 76 : std::map<timestamp_t, SoundPresetChangeEventList> PlaybackContext::soundPresets(const track_idx_t trackFrom,
140 : const track_idx_t trackTo) const
141 : {
142 76 : std::map<timestamp_t, SoundPresetChangeEventList> result;
143 :
144 76 : collectByTrackRange(m_soundPresetsByTrack, trackFrom, trackTo, m_score, m_usedTracks, result,
145 0 : [](SoundPresetChangeEventList& list, const SoundPresetChangeEventList& events) {
146 0 : list.insert(list.end(), events.begin(), events.end());
147 0 : });
148 :
149 76 : return result;
150 0 : }
151 :
152 2 : SoundPresetChangeEventList PlaybackContext::soundPresets(const track_idx_t trackIdx, const int nominalPositionTick) const
153 : {
154 2 : auto presetsIt = m_soundPresetsByTrack.find(trackIdx);
155 2 : if (presetsIt == m_soundPresetsByTrack.end()) {
156 2 : return {};
157 : }
158 :
159 0 : const SoundPresetsMap& map = presetsIt->second;
160 0 : auto it = muse::findLessOrEqual(map, nominalPositionTick);
161 0 : if (it == map.end()) {
162 0 : return {};
163 : }
164 :
165 0 : return it->second;
166 : }
167 :
168 80 : std::map<timestamp_t, TextArticulationEventList> PlaybackContext::textArticulations(const track_idx_t trackFrom,
169 : const track_idx_t trackTo) const
170 : {
171 80 : std::map<timestamp_t, TextArticulationEventList> result;
172 :
173 80 : collectByTrackRange(m_textArticulationsByTrack, trackFrom, trackTo, m_score, m_usedTracks, result,
174 14 : [](TextArticulationEventList& list, const TextArticulationEvent& event) {
175 14 : list.push_back(event);
176 14 : });
177 :
178 80 : return result;
179 0 : }
180 :
181 34 : TextArticulationEvent PlaybackContext::textArticulation(const track_idx_t trackIdx, const int nominalPositionTick) const
182 : {
183 34 : auto articulationsIt = m_textArticulationsByTrack.find(trackIdx);
184 34 : if (articulationsIt == m_textArticulationsByTrack.end()) {
185 2 : return {};
186 : }
187 :
188 32 : const TextArticulationMap& map = articulationsIt->second;
189 32 : auto it = muse::findLessOrEqual(map, nominalPositionTick);
190 32 : if (it == map.end()) {
191 8 : return {};
192 : }
193 :
194 24 : TextArticulationEvent result = it->second;
195 24 : result.flags.setFlag(TextArticulationEvent::StartsAtPlaybackPosition, it->first == nominalPositionTick);
196 :
197 24 : return result;
198 34 : }
199 :
200 77 : std::map<timestamp_t, SyllableEventList> PlaybackContext::syllables(const track_idx_t trackFrom, const track_idx_t trackTo) const
201 : {
202 77 : std::map<timestamp_t, SyllableEventList> result;
203 :
204 77 : collectByTrackRange(m_syllablesByTrack, trackFrom, trackTo, m_score, m_usedTracks, result,
205 15 : [](SyllableEventList& list, const SyllableEvent& event) {
206 15 : list.push_back(event);
207 15 : });
208 :
209 77 : return result;
210 0 : }
211 :
212 2 : SyllableEvent PlaybackContext::syllable(const track_idx_t trackIdx, const int nominalPositionTick) const
213 : {
214 2 : auto syllablesIt = m_syllablesByTrack.find(trackIdx);
215 2 : if (syllablesIt == m_syllablesByTrack.end()) {
216 2 : return {};
217 : }
218 :
219 0 : const SyllableMap& map = syllablesIt->second;
220 0 : auto it = muse::findLessOrEqual(map, nominalPositionTick);
221 0 : if (it == map.end()) {
222 0 : return {};
223 : }
224 :
225 0 : SyllableEvent result = it->second;
226 0 : result.flags.setFlag(SyllableEvent::StartsAtPlaybackPosition, it->first == nominalPositionTick);
227 :
228 0 : return result;
229 2 : }
230 :
231 83 : DynamicAutomationLayers PlaybackContext::dynamicLevelLayers(const track_idx_t trackFrom, const track_idx_t trackTo) const
232 : {
233 84 : TRACEFUNC;
234 :
235 83 : if (!m_score || !m_score->automationData()) {
236 0 : return {};
237 : }
238 :
239 83 : DynamicAutomationLayers result;
240 83 : const AutomationCurve* lastCurve = nullptr;
241 83 : DynamicAutomationMap lastLevelMap;
242 :
243 451 : for (track_idx_t trackIdx = trackFrom; trackIdx < trackTo; ++trackIdx) {
244 368 : const AutomationCurve* curve = dynamicsCurve(trackIdx);
245 368 : if (!curve || curve->empty()) {
246 328 : continue;
247 : }
248 :
249 40 : if (curve != lastCurve) {
250 12 : lastLevelMap.clear();
251 12 : lastCurve = curve;
252 12 : auto hint = lastLevelMap.end();
253 :
254 : // AutomationCurve ticks are always in expanded utick space, regardless of the Play Repeats setting
255 12 : const TempoTimeline& expandedTimeline = m_score->tempoTimeline(/*expandRepeats*/ true);
256 :
257 12 : if (curve->cbegin()->first > 0) {
258 1 : AutomationPoint naturalPoint;
259 1 : naturalPoint.value.outValue = real_t(NATURAL_DYNAMIC_LEVEL) / real_t(MAX_DYNAMIC_LEVEL);
260 1 : hint = lastLevelMap.insert(hint, { expandedTimeline.utick2utime(0) * 1000000, naturalPoint.value });
261 : }
262 :
263 74 : for (const auto& [tick, point] : *curve) {
264 62 : hint = lastLevelMap.insert(hint, { expandedTimeline.utick2utime(tick) * 1000000, point.value });
265 : }
266 : }
267 :
268 40 : result[static_cast<layer_idx_t>(trackIdx)] = lastLevelMap;
269 : }
270 :
271 83 : return result;
272 83 : }
273 :
274 47 : void PlaybackContext::update(const track_idx_t trackFrom, const track_idx_t trackTo, const int tickFrom, const int tickTo,
275 : bool expandRepeats)
276 : {
277 48 : TRACEFUNC;
278 :
279 47 : IF_ASSERT_FAILED(m_score) {
280 0 : return;
281 : }
282 :
283 47 : IF_ASSERT_FAILED(trackFrom <= trackTo) {
284 0 : return;
285 : }
286 :
287 47 : m_dynamicsCurveByTrack.clear();
288 :
289 116 : for (const RepeatSegment* repeatSegment : m_score->repeatList(expandRepeats)) {
290 69 : const int repeatStartTick = repeatSegment->tick;
291 69 : const int repeatEndTick = repeatSegment->endTick();
292 :
293 69 : if (repeatStartTick > tickTo || repeatEndTick <= tickFrom) {
294 0 : continue;
295 : }
296 :
297 69 : std::vector<const MeasureRepeat*> measureRepeats;
298 69 : int tickPositionOffset = repeatSegment->utick - repeatSegment->tick;
299 :
300 330 : for (const Measure* measure : repeatSegment->measureList()) {
301 261 : const int measureStartTick = measure->tick().ticks();
302 261 : const int measureEndTick = measure->endTick().ticks();
303 :
304 261 : if (measureStartTick > tickTo || measureEndTick <= tickFrom) {
305 10 : continue;
306 : }
307 :
308 1614 : for (const Segment* segment = measure->first(); segment; segment = segment->next()) {
309 1368 : const int segmentTick = segment->tick().ticks();
310 1368 : if (segmentTick > tickTo) {
311 5 : break;
312 : }
313 :
314 1363 : if (segmentTick < tickFrom) {
315 16 : continue;
316 : }
317 :
318 1347 : int segmentStartTick = segmentTick + tickPositionOffset;
319 :
320 1347 : handleSegmentElements(repeatSegment, segment, segmentStartTick, trackFrom, trackTo, measureRepeats);
321 1347 : handleSegmentAnnotations(segment, segmentStartTick, trackFrom, trackTo);
322 : }
323 : }
324 :
325 69 : handleMeasureRepeats(measureRepeats, tickPositionOffset);
326 69 : }
327 47 : }
328 :
329 11 : void PlaybackContext::clear(const track_idx_t trackFrom, const track_idx_t trackTo, const int tickFrom, const int tickTo)
330 : {
331 55 : const auto eraseTickRangeByTrack = [trackFrom, trackTo, tickFrom, tickTo](auto& byTrackMap) {
332 59 : for (auto it = byTrackMap.lower_bound(trackFrom); it != byTrackMap.end() && it->first < trackTo;) {
333 4 : auto& innerMap = it->second;
334 4 : innerMap.erase(innerMap.lower_bound(tickFrom), innerMap.upper_bound(tickTo));
335 :
336 4 : if (innerMap.empty()) {
337 4 : it = byTrackMap.erase(it);
338 : } else {
339 0 : ++it;
340 : }
341 : }
342 66 : };
343 :
344 11 : eraseTickRangeByTrack(m_soundPresetsByTrack);
345 11 : eraseTickRangeByTrack(m_textArticulationsByTrack);
346 11 : eraseTickRangeByTrack(m_syllablesByTrack);
347 11 : eraseTickRangeByTrack(m_playTechniquesByTrack);
348 11 : eraseTickRangeByTrack(m_multiVerseLyricsPositionMap);
349 :
350 11 : muse::remove_if(m_currentVerseNumByChordRest, [trackFrom, trackTo, tickFrom, tickTo](const auto& pair) {
351 0 : const track_idx_t track = pair.first->track();
352 0 : const int tick = pair.first->tick().ticks();
353 0 : return track >= trackFrom && track < trackTo && tick >= tickFrom && tick <= tickTo;
354 : });
355 11 : }
356 :
357 0 : bool PlaybackContext::hasSoundFlags(const track_idx_t trackFrom, const track_idx_t trackTo) const
358 : {
359 0 : auto hasEntryInRange = [trackFrom, trackTo](const auto& byTrackMap) {
360 0 : auto it = byTrackMap.lower_bound(trackFrom);
361 0 : return it != byTrackMap.end() && it->first < trackTo;
362 0 : };
363 :
364 0 : return hasEntryInRange(m_soundPresetsByTrack) || hasEntryInRange(m_textArticulationsByTrack);
365 : }
366 :
367 36 : void PlaybackContext::updatePlayTechMap(const Part* part, const PlayTechAnnotation* annotation, const int segmentPositionTick)
368 : {
369 36 : if (!annotation->playPlayTechAnnotation()) {
370 0 : return;
371 : }
372 36 : const PlayingTechniqueType type = annotation->techniqueType();
373 36 : if (type == PlayingTechniqueType::Undefined) {
374 0 : return;
375 : }
376 :
377 34 : const bool cancelPlayTechniques = (type == PlayingTechniqueType::Natural || type == PlayingTechniqueType::Open)
378 70 : && !m_textArticulationsByTrack.empty();
379 :
380 36 : TextArticulationEvent textArticulation;
381 36 : textArticulation.text = mpe::ORDINARY_PLAYING_TECHNIQUE_CODE;
382 :
383 36 : const TrackRange trackRange = part->trackRange();
384 184 : for (track_idx_t idx = trackRange.startTrack; idx < trackRange.endTrack; ++idx) {
385 148 : m_playTechniquesByTrack[idx][segmentPositionTick] = type;
386 :
387 148 : if (cancelPlayTechniques) {
388 8 : textArticulation.layerIdx = static_cast<layer_idx_t>(idx);
389 8 : m_textArticulationsByTrack[idx][segmentPositionTick] = textArticulation;
390 : }
391 : }
392 36 : }
393 :
394 8 : void PlaybackContext::updateSoundPresetAndTextArticulationMap(const Part* part, const SoundFlagMap& flagsOnSegment,
395 : const int segmentPositionTick)
396 : {
397 52 : auto trackAccepted = [&flagsOnSegment](const SoundFlag* flag, track_idx_t trackIdx) {
398 52 : staff_idx_t staffIdx = track2staff(trackIdx);
399 :
400 52 : if (flag->staffIdx() == staffIdx) {
401 36 : return true;
402 : }
403 :
404 16 : if (flag->applyToAllStaves()) {
405 8 : return !muse::contains(flagsOnSegment, staffIdx);
406 : }
407 :
408 8 : return false;
409 8 : };
410 :
411 8 : const TrackRange trackRange = part->trackRange();
412 :
413 17 : for (const auto& pair : flagsOnSegment) {
414 9 : const SoundFlag* flag = pair.second;
415 :
416 61 : for (track_idx_t trackIdx = trackRange.startTrack; trackIdx < trackRange.endTrack; ++trackIdx) {
417 52 : if (!trackAccepted(flag, trackIdx)) {
418 12 : continue;
419 : }
420 :
421 40 : SoundPresetChangeEventList presets;
422 :
423 40 : for (const String& soundPreset : flag->soundPresets()) {
424 0 : if (soundPreset.empty()) {
425 0 : continue;
426 : }
427 :
428 0 : SoundPresetChangeEvent event;
429 0 : event.code = soundPreset;
430 0 : event.layerIdx = static_cast<layer_idx_t>(trackIdx);
431 0 : presets.emplace_back(std::move(event));
432 0 : }
433 :
434 40 : if (!presets.empty()) {
435 0 : SoundPresetChangeEventList& target = m_soundPresetsByTrack[trackIdx][segmentPositionTick];
436 0 : target.insert(target.end(), std::make_move_iterator(presets.begin()), std::make_move_iterator(presets.end()));
437 : }
438 :
439 40 : if (!flag->playingTechnique().empty()) {
440 40 : TextArticulationEvent event;
441 40 : event.text = flag->playingTechnique();
442 40 : event.layerIdx = static_cast<layer_idx_t>(trackIdx);
443 40 : m_textArticulationsByTrack[trackIdx][segmentPositionTick] = std::move(event);
444 40 : }
445 40 : }
446 :
447 9 : if (flag->playingTechnique() == mpe::ORDINARY_PLAYING_TECHNIQUE_CODE) {
448 5 : for (track_idx_t trackIdx = trackRange.startTrack; trackIdx < trackRange.endTrack; ++trackIdx) {
449 4 : m_playTechniquesByTrack[trackIdx][segmentPositionTick] = PlayingTechniqueType::Natural;
450 : }
451 : }
452 : }
453 8 : }
454 :
455 15 : void PlaybackContext::updateSyllableMap(const TextBase* text, const int segmentPositionTick)
456 : {
457 15 : IF_ASSERT_FAILED(text->isLyrics() || text->isSticking()) {
458 0 : return;
459 : }
460 :
461 15 : if (text->empty()) {
462 0 : return;
463 : }
464 :
465 15 : SyllableEvent syllable;
466 15 : syllable.text = text->plainText();
467 :
468 15 : if (text->isLyrics()) {
469 15 : const Lyrics* lyrics = toLyrics(text);
470 :
471 15 : switch (lyrics->syllabic()) {
472 2 : case LyricsSyllabic::BEGIN:
473 : case LyricsSyllabic::MIDDLE:
474 2 : syllable.flags.setFlag(SyllableEvent::HyphenedToNext);
475 2 : break;
476 13 : case LyricsSyllabic::SINGLE:
477 : case LyricsSyllabic::END:
478 13 : break;
479 : }
480 : }
481 :
482 15 : const staff_idx_t staffIdx = text->staffIdx();
483 :
484 75 : for (voice_idx_t voiceIdx = 0; voiceIdx < VOICES; ++voiceIdx) {
485 60 : track_idx_t trackIdx = staff2track(staffIdx, voiceIdx);
486 60 : syllable.layerIdx = static_cast<layer_idx_t>(trackIdx);
487 60 : m_syllablesByTrack[trackIdx][segmentPositionTick] = syllable;
488 : }
489 15 : }
490 :
491 1347 : void PlaybackContext::handleSegmentAnnotations(const Segment* segment, const int segmentPositionTick,
492 : const track_idx_t trackFrom, const track_idx_t trackTo)
493 : {
494 1347 : std::unordered_map<const Part*, SoundFlagMap> soundFlagsByPart;
495 :
496 1469 : for (const EngravingItem* annotation : segment->annotations()) {
497 122 : if (!annotation || !annotation->part()) {
498 49 : continue;
499 : }
500 :
501 122 : const Part* annotationPart = annotation->part();
502 122 : const TrackRange annotationPartTrackRange = annotationPart->trackRange();
503 122 : if (annotationPartTrackRange.startTrack >= trackTo || annotationPartTrackRange.endTrack <= trackFrom) {
504 13 : continue;
505 : }
506 :
507 109 : if (annotation->isPlayTechAnnotation()) {
508 36 : updatePlayTechMap(annotationPart, toPlayTechAnnotation(annotation), segmentPositionTick);
509 36 : continue;
510 : }
511 :
512 73 : if (annotation->isSticking()) {
513 0 : updateSyllableMap(toTextBase(annotation), segmentPositionTick);
514 0 : continue;
515 : }
516 :
517 73 : if (annotation->isStaffText()) {
518 20 : if (const SoundFlag* flag = toStaffText(annotation)->soundFlag()) {
519 9 : if (soundFlagPlayable(flag)) {
520 9 : soundFlagsByPart[annotationPart].emplace(flag->staffIdx(), flag);
521 : }
522 : }
523 : }
524 : }
525 :
526 1355 : for (const auto& pair : soundFlagsByPart) {
527 8 : const Part* part = pair.first;
528 8 : const SoundFlagMap& flagsOnSegment = pair.second;
529 :
530 8 : if (!flagsOnSegment.empty()) {
531 8 : updateSoundPresetAndTextArticulationMap(part, flagsOnSegment, segmentPositionTick);
532 : }
533 : }
534 1347 : }
535 :
536 1347 : void PlaybackContext::handleSegmentElements(const RepeatSegment* repeat, const Segment* segment,
537 : const int segmentPositionTick,
538 : const track_idx_t trackFrom, const track_idx_t trackTo,
539 : std::vector<const MeasureRepeat*>& foundMeasureRepeats)
540 : {
541 10127 : for (track_idx_t track = trackFrom; track < trackTo; ++track) {
542 8780 : const EngravingItem* item = segment->element(track);
543 8780 : if (!item) {
544 6561 : continue;
545 : }
546 :
547 2219 : if (item->isMeasureRepeat()) {
548 12 : foundMeasureRepeats.push_back(toMeasureRepeat(item));
549 12 : continue;
550 : }
551 :
552 2207 : if (item->isChordRest()) {
553 1388 : m_usedTracks.insert(track);
554 :
555 1388 : const ChordRest* chordRest = toChordRest(item);
556 1388 : if (chordRest->lyrics().empty()) {
557 1370 : continue;
558 : }
559 :
560 18 : const Lyrics* lyrics = nullptr;
561 :
562 18 : auto verseNumIt = m_currentVerseNumByChordRest.find(chordRest);
563 18 : if (verseNumIt == m_currentVerseNumByChordRest.end()) {
564 9 : m_currentVerseNumByChordRest[chordRest] = 0;
565 9 : lyrics = chordRest->lyrics(0);
566 9 : if (chordRest->lyrics().size() > 1) {
567 4 : m_multiVerseLyricsPositionMap[track].insert(chordRest->tick().ticks());
568 : }
569 9 : } else if (hasOnlyOneLyricsVerse(repeat, track)) {
570 2 : lyrics = chordRest->lyrics(0);
571 : } else {
572 7 : verseNumIt->second++;
573 7 : lyrics = chordRest->lyrics(verseNumIt->second);
574 : }
575 :
576 18 : if (lyrics) {
577 15 : updateSyllableMap(lyrics, segmentPositionTick);
578 : }
579 : }
580 : }
581 1347 : }
582 :
583 : template<typename ItemsMap>
584 16 : static void copyItemsInRange(ItemsMap& source, const int rangeStartTick, const int rangeEndTick, const int newItemsOffsetTick)
585 : {
586 16 : auto startIt = source.lower_bound(rangeStartTick);
587 16 : if (startIt == source.end()) {
588 0 : return;
589 : }
590 :
591 16 : auto endIt = source.lower_bound(rangeEndTick);
592 :
593 16 : ItemsMap newItems;
594 32 : for (auto it = startIt; it != endIt; ++it) {
595 16 : int tick = it->first + newItemsOffsetTick;
596 16 : newItems.insert_or_assign(tick, it->second);
597 : }
598 :
599 16 : source.merge(std::move(newItems));
600 16 : }
601 :
602 : template<typename ItemsMap>
603 76 : static void copyItemsInRange(std::map<track_idx_t, ItemsMap>& source, const track_idx_t trackFrom, const track_idx_t trackTo,
604 : const int rangeStartTick, const int rangeEndTick, const int newItemsOffsetTick)
605 : {
606 92 : for (auto it = source.lower_bound(trackFrom); it != source.end() && it->first < trackTo; ++it) {
607 16 : copyItemsInRange(it->second, rangeStartTick, rangeEndTick, newItemsOffsetTick);
608 : }
609 76 : }
610 :
611 69 : void PlaybackContext::handleMeasureRepeats(const std::vector<const MeasureRepeat*>& measureRepeats, const int tickPositionOffset)
612 : {
613 81 : for (const MeasureRepeat* mr : measureRepeats) {
614 12 : const Part* part = mr->part();
615 12 : if (!part) {
616 0 : continue;
617 : }
618 :
619 12 : const Measure* currMeasure = mr->firstMeasureOfGroup();
620 12 : if (!currMeasure) {
621 0 : continue;
622 : }
623 :
624 12 : const Measure* referringMeasure = mr->referringMeasure(currMeasure);
625 12 : if (!referringMeasure) {
626 0 : continue;
627 : }
628 :
629 12 : int currentMeasureTick = currMeasure->tick().ticks();
630 12 : int referringMeasureTick = referringMeasure->tick().ticks();
631 12 : int newItemsOffsetTick = currentMeasureTick - referringMeasureTick;
632 :
633 12 : const TrackRange trackRange = part->trackRange();
634 :
635 24 : for (int num = 0; num < mr->numMeasures(); ++num) {
636 19 : int startTick = referringMeasure->tick().ticks() + tickPositionOffset;
637 19 : int endTick = referringMeasure->endTick().ticks() + tickPositionOffset;
638 :
639 19 : copyItemsInRange(m_soundPresetsByTrack, trackRange.startTrack, trackRange.endTrack, startTick, endTick, newItemsOffsetTick);
640 19 : copyItemsInRange(m_textArticulationsByTrack, trackRange.startTrack, trackRange.endTrack, startTick, endTick,
641 : newItemsOffsetTick);
642 19 : copyItemsInRange(m_syllablesByTrack, trackRange.startTrack, trackRange.endTrack, startTick, endTick, newItemsOffsetTick);
643 19 : copyItemsInRange(m_playTechniquesByTrack, trackRange.startTrack, trackRange.endTrack, startTick, endTick,
644 : newItemsOffsetTick);
645 :
646 19 : currMeasure = currMeasure->nextMeasure();
647 19 : if (!currMeasure) {
648 5 : break;
649 : }
650 :
651 14 : referringMeasure = mr->referringMeasure(currMeasure);
652 14 : if (!referringMeasure) {
653 2 : break;
654 : }
655 : }
656 : }
657 69 : }
658 :
659 2185 : const mu::engraving::AutomationCurve* PlaybackContext::dynamicsCurve(const track_idx_t trackIdx) const
660 : {
661 2185 : auto cacheIt = m_dynamicsCurveByTrack.find(trackIdx);
662 2185 : if (cacheIt != m_dynamicsCurveByTrack.end()) {
663 1771 : return cacheIt->second;
664 : }
665 :
666 414 : const AutomationDataConstPtr automation = m_score ? m_score->automationData() : nullptr;
667 414 : const Staff* staff = automation ? m_score->staff(track2staff(trackIdx)) : nullptr;
668 :
669 414 : const AutomationCurve* curve = nullptr;
670 414 : if (staff) {
671 414 : AutomationCurveKey key = AutomationCurveKey::staff(AutomationType::Dynamics, staff->id(), track2voice(trackIdx));
672 414 : curve = &automation->curve(key);
673 :
674 414 : if (curve->empty()) {
675 412 : key = key.withoutVoice();
676 412 : curve = &automation->curve(key);
677 : }
678 414 : }
679 :
680 414 : m_dynamicsCurveByTrack.emplace(trackIdx, curve);
681 414 : return curve;
682 414 : }
683 :
684 9 : bool PlaybackContext::hasOnlyOneLyricsVerse(const RepeatSegment* repeat, const track_idx_t track) const
685 : {
686 9 : if (m_multiVerseLyricsPositionMap.empty()) {
687 0 : return true;
688 : }
689 :
690 9 : const auto trackIt = m_multiVerseLyricsPositionMap.find(track);
691 9 : if (trackIt == m_multiVerseLyricsPositionMap.cend()) {
692 0 : return true;
693 : }
694 :
695 9 : const int startTick = repeat->tick;
696 9 : const int endTick = repeat->endTick();
697 9 : const auto start = trackIt->second.lower_bound(startTick);
698 9 : const auto end = trackIt->second.lower_bound(endTick);
699 :
700 9 : return start == end;
701 : }
|