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 : #ifndef MU_ENGRAVING_COMPAT_EVENT_H
24 : #define MU_ENGRAVING_COMPAT_EVENT_H
25 :
26 : #include <map>
27 : #include <vector>
28 :
29 : #include "midiinstrumenteffects.h"
30 : #include "midicoreevent.h"
31 :
32 : namespace mu::engraving {
33 : class Note;
34 : class Harmony;
35 :
36 : enum class BeatType : unsigned char;
37 :
38 : //---------------------------------------------------------
39 : // PlayEvent
40 : // interface to Synthesizer
41 : //---------------------------------------------------------
42 :
43 : class PlayEvent : public MidiCoreEvent
44 : {
45 : OBJECT_ALLOCATOR(engraving, PlayEvent)
46 : protected:
47 : float _tuning = .0f;
48 :
49 : public:
50 3 : PlayEvent()
51 3 : : MidiCoreEvent() {}
52 0 : PlayEvent(const MidiCoreEvent& e)
53 0 : : MidiCoreEvent(e) {}
54 2126 : PlayEvent(uint8_t t, uint8_t c, uint8_t a, uint8_t b)
55 2126 : : MidiCoreEvent(t, c, a, b) {}
56 : float tuning() const { return _tuning; }
57 503 : void setTuning(float v) { _tuning = v; }
58 : };
59 :
60 : //---------------------------------------------------------
61 : // NPlayEvent
62 : // used for Sequencer interface
63 : //---------------------------------------------------------
64 :
65 : class NPlayEvent : public PlayEvent
66 : {
67 : OBJECT_ALLOCATOR(engraving, NPlayEvent)
68 :
69 : public:
70 0 : NPlayEvent()
71 0 : : PlayEvent() {}
72 2126 : NPlayEvent(uint8_t t, uint8_t c, uint8_t a, uint8_t b)
73 2126 : : PlayEvent(t, c, a, b) {}
74 0 : NPlayEvent(const MidiCoreEvent& e)
75 0 : : PlayEvent(e) {}
76 : NPlayEvent(BeatType beatType);
77 :
78 0 : const Note* note() const { return _note; }
79 503 : void setNote(const Note* v) { _note = v; }
80 : const Harmony* harmony() const { return _harmony; }
81 0 : void setHarmony(const Harmony* v) { _harmony = v; }
82 :
83 1898 : void setEffect(MidiInstrumentEffect effect) { _effect = effect; }
84 1902 : MidiInstrumentEffect effect() const { return _effect; }
85 :
86 1603 : size_t getOriginatingStaff() const { return _origin; }
87 2573 : void setOriginatingStaff(size_t i) { _origin = i; }
88 2 : void setDiscard(size_t d) { _discard = d; }
89 2036 : size_t discard() const { return _discard; }
90 : bool isMuted() const;
91 : void setPortamento(bool p) { _portamento = p; }
92 0 : bool portamento() const
93 : {
94 0 : return _portamento == true
95 0 : || (this->type() == ME_CONTROLLER
96 0 : && (this->controller() == CTRL_PORTAMENTO || this->controller() == CTRL_PORTAMENTO_CONTROL
97 0 : || this->controller() == CTRL_PORTAMENTO_TIME_MSB || this->controller() == CTRL_PORTAMENTO_TIME_LSB));
98 : }
99 :
100 : private:
101 :
102 : const Note* _note = nullptr;
103 : const Harmony* _harmony = nullptr;
104 : size_t _origin = size_t(-1);
105 : size_t _discard = 0;
106 : bool _portamento = false;
107 : MidiInstrumentEffect _effect = MidiInstrumentEffect::NONE;
108 : };
109 :
110 : //---------------------------------------------------------
111 : // Event
112 : //---------------------------------------------------------
113 :
114 : class Event : public PlayEvent
115 : {
116 : OBJECT_ALLOCATOR(engraving, Event)
117 :
118 : int _ontime;
119 : int _noquantOntime;
120 : int _noquantDuration;
121 : int _duration;
122 : int _tpc; // tonal pitch class
123 : int _voice;
124 : std::vector<Event> _notes;
125 : uint8_t* _edata; // always zero terminated (_data[_len] == 0; )
126 : int _len;
127 : int _metaType;
128 : const Note* _note;
129 :
130 : public:
131 : Event();
132 : Event(const Event&);
133 : Event(int t);
134 : ~Event();
135 : bool operator==(const Event&) const;
136 :
137 : void dump() const;
138 :
139 : int noquantOntime() const { return _noquantOntime; }
140 : void setNoquantOntime(int v) { _noquantOntime = v; }
141 : int noquantDuration() const { return _noquantDuration; }
142 : void setNoquantDuration(int v) { _noquantDuration = v; }
143 :
144 0 : int ontime() const { return _ontime; }
145 3 : void setOntime(int v) { _ontime = v; }
146 :
147 0 : int duration() const { return _duration; }
148 2 : void setDuration(int v) { _duration = v; }
149 0 : int voice() const { return _voice; }
150 0 : void setVoice(int val) { _voice = val; }
151 0 : int offtime() const { return _ontime + _duration; }
152 0 : std::vector<Event>& notes() { return _notes; }
153 : const uint8_t* edata() const { return _edata; }
154 : void setEData(uint8_t* d) { _edata = d; }
155 : int len() const { return _len; }
156 0 : void setLen(int l) { _len = l; }
157 : int metaType() const { return _metaType; }
158 : void setMetaType(int v) { _metaType = v; }
159 0 : int tpc() const { return _tpc; }
160 : void setTpc(int v) { _tpc = v; }
161 : const Note* note() const { return _note; }
162 : void setNote(const Note* v) { _note = v; }
163 : };
164 :
165 : //---------------------------------------------------------
166 : // EventList
167 : // EventsHolder
168 : //---------------------------------------------------------
169 :
170 : class EventList : public std::vector<Event>
171 : {
172 : OBJECT_ALLOCATOR(engraving, EventList)
173 : public:
174 : void insert(const Event&);
175 : void insertNote(int channel, Note*);
176 : };
177 :
178 : class EventsHolder
179 : {
180 : OBJECT_ALLOCATOR(engraving, EventsHolder)
181 :
182 : using events_multimap_t = std::multimap<int, NPlayEvent>;
183 : std::vector<events_multimap_t> _channels;
184 : public:
185 27437 : [[nodiscard]] size_t size() const { return _channels.size(); }
186 : events_multimap_t& operator[](std::size_t idx);
187 : const events_multimap_t& operator[](std::size_t idx) const;
188 : void mergePitchWheelEvents(EventsHolder& pitchWheelEvents);
189 : void fixupMIDI();
190 : };
191 :
192 : typedef EventList::iterator iEvent;
193 : typedef EventList::const_iterator ciEvent;
194 : }
195 : #endif
|