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) 2026 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 : #pragma once
24 :
25 : #include <cstddef>
26 : #include <map>
27 : #include <vector>
28 :
29 : #include "engraving/automation/automationtypes.h"
30 : #include "engraving/types/bps.h"
31 :
32 : namespace mu::engraving {
33 : using PausesMap = std::map<int, double>; // utick -> held seconds
34 : using TempoValues = std::vector<std::pair<int, double> >; // utick -> bps, sorted by utick, no duplicate uticks
35 :
36 : struct TempoTimePoint {
37 : int utick = 0;
38 : double time = 0.0; // cumulative nominal elapsed time at utick, pauses already included
39 : double bps = 0.0; // tempo held flat from utick until the next point (or indefinitely, for the last one)
40 : double pause = 0.0; // seconds held at utick before "time" was reached, if this point is a pause
41 : };
42 :
43 : class TempoTimeline
44 : {
45 : public:
46 : void rebuild(const AutomationCurve& tempoCurve, const PausesMap& pauses);
47 : void rebuild(const TempoValues& tempoValues, const PausesMap& pauses);
48 :
49 : double utick2utime(int utick) const;
50 : int utime2utick(double time) const;
51 : BeatsPerSecond tempo(int utick) const;
52 :
53 3246 : const PausesMap& pauses() const { return m_pauses; }
54 64 : const std::vector<TempoTimePoint>& points() const { return m_points; }
55 :
56 2149 : const BeatsPerSecond& tempoMultiplier() const { return m_tempoMultiplier; }
57 : void setTempoMultiplier(const BeatsPerSecond& bps);
58 :
59 : private:
60 : std::vector<TempoTimePoint> m_points; // sorted by utick for O(logN) lookup
61 : PausesMap m_pauses;
62 : BeatsPerSecond m_tempoMultiplier = 1.0;
63 :
64 : mutable size_t m_utick2utimeHint = 0; // cached m_points index for utick2utime()
65 : mutable size_t m_utime2utickHint = 0; // cached m_points index for utime2utick()
66 : mutable size_t m_tempoHint = 0; // cached m_points index for tempo()
67 : };
68 : }
|