Debian Codegraph @

BambooTracker/instrument/sequence_property.hpp

raw
1 /*
2 * Copyright (C) 2018-2020 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #pragma once
27 #include <vector>
28 #include <memory>
29 #include <map>
30 #include <deque>
31 #include <algorithm>
32 #include <utility>
33 #include "abstract_instrument_property.hpp"
34 #include "sequence_iterator_interface.hpp"
35
36 struct InstrumentSequenceBaseUnit
37 {
38 int data;
39
40 static constexpr int ERR_DATA = -1;
41
42 InstrumentSequenceBaseUnit() noexcept;
43 explicit InstrumentSequenceBaseUnit(int d) noexcept;
44 virtual ~InstrumentSequenceBaseUnit() = default;
45
46 friend bool operator==(const InstrumentSequenceBaseUnit& a, const InstrumentSequenceBaseUnit& b)
47 {
48 return a.data == b.data;
49 }
50
51 friend bool operator!=(const InstrumentSequenceBaseUnit& a, const InstrumentSequenceBaseUnit& b) { return !(a == b); }
52 };
53
54 struct InstrumentSequenceExtendUnit : public InstrumentSequenceBaseUnit
55 {
56 enum SubdataType : int
57 {
58 UnusedSubdata,
59 RawSubdata,
60 RatioSubdata,
61 ShiftSubdata
62 } type;
63 /// - If bit 17 is 0,
64 /// * If bit 16 is 0, bit 0-15 is raw data
65 /// * If bit 16 is 1, bit 0-7 is 2nd and bit 8-15 is 1st ratio value
66 /// - If bit 17 is 1,
67 /// * If bit 16 is 0, bit 0-15 is right shift value
68 /// * If bit 16 is 1, bit 0-15 is left shift value
69 int subdata;
70
71 InstrumentSequenceExtendUnit() noexcept;
72 explicit InstrumentSequenceExtendUnit(int d, SubdataType subType, int subData) noexcept;
73
74 static InstrumentSequenceExtendUnit makeOnlyDataUnit(int data) noexcept;
75 static InstrumentSequenceExtendUnit makeRawUnit(int data, int sub) noexcept;
76 static InstrumentSequenceExtendUnit makeRatioUnit(int data, int subFirst, int subSecond) noexcept;
77 static InstrumentSequenceExtendUnit makeShiftUnit(int data, int rshift) noexcept;
78 static InstrumentSequenceExtendUnit makeUnitWithDecode(int data, int subsrc) noexcept;
79
80 void getSubdataAsRaw(int& raw) const noexcept;
81 void getSubdataAsRatio(int& first, int& second) const noexcept;
82 void getSubdataAsShift(int& rshift) const noexcept;
83
84 friend bool operator==(const InstrumentSequenceExtendUnit& a, const InstrumentSequenceExtendUnit& b)
85 {
86 return a.data == b.data && a.type == b.type && a.subdata == b.subdata;
87 }
88
89 friend bool operator!=(const InstrumentSequenceExtendUnit& a, const InstrumentSequenceExtendUnit& b) { return !(a == b); }
90 };
91
92 class InstrumentSequenceLoop
93 {
94 public:
95 using Ptr = std::shared_ptr<InstrumentSequenceLoop>;
96
97 static constexpr int INFINITE_LOOP = 1;
98 /// Loop in a closed interval [begin, end]
99 InstrumentSequenceLoop(int begin, int end, int times = INFINITE_LOOP);
100
101 friend bool operator==(const InstrumentSequenceLoop& a, const InstrumentSequenceLoop& b)
102 {
103 return (a.begin_ == b.begin_ && a.end_ == b.end_ && a.times_ == b.times_);
104 }
105
106 friend bool operator!=(const InstrumentSequenceLoop& a, const InstrumentSequenceLoop& b) { return !(a == b); }
107
108 void setBeginPos(int pos);
109 inline int getBeginPos() const noexcept { return begin_; }
110 void setEndPos(int pos);
111 inline int getEndPos() const noexcept { return end_; }
112 inline void setTimes(int times) noexcept { times_ = times; }
113 inline int getTimes() const noexcept { return times_; }
114 inline bool isInfinite() const noexcept { return times_ == INFINITE_LOOP; }
115
116 inline Ptr getInnerLoopBeginAt(int pos) const { return childs_.at(pos); }
117
118 bool addInnerLoop(const InstrumentSequenceLoop& inner);
119 bool changeInnerLoop(int prevBegin, int prevEnd, const InstrumentSequenceLoop& loop);
120 void removeInnerLoop(int begin, int end);
121 void removeAllInnerLoops();
122 std::vector<InstrumentSequenceLoop> getAllInnerLoops()const;
123
124 inline bool hasInnerLoop() const { return !childs_.empty(); }
125 inline bool hasInnerLoopBeginAt(int pos) const { return childs_.count(pos); }
126 bool isOverlapped(int begin, int end) const;
127 inline bool isOverlapped(const InstrumentSequenceLoop& other) const
128 {
129 return isOverlapped(other.begin_, other.end_);
130 }
131 bool isContainable(int begin, int end) const;
132 inline bool isContainable(const InstrumentSequenceLoop& other) const
133 {
134 return isContainable(other.begin_, other.end_);
135 }
136 bool hasSameRegion(int begin, int end) const;
137 inline bool hasSameRegion(const InstrumentSequenceLoop& other) const
138 {
139 return hasSameRegion(other.begin_, other.end_);
140 }
141 InstrumentSequenceLoop clone() const;
142
143 protected:
144 int begin_, end_, times_;
145 std::map<int, Ptr> childs_;
146 };
147
148 class InstrumentSequenceLoopRoot : public InstrumentSequenceLoop
149 {
150 public:
151 InstrumentSequenceLoopRoot(int size)
152 : InstrumentSequenceLoop(0, size - 1) {}
153
154 inline int size() const { return end_; }
155 inline void extend() { setEndPos(end_ + 1); }
156 inline void shrink() { if (end_) setEndPos(end_ - 1); }
157 inline void resize(int size) { setEndPos(size - 1); }
158 inline void clear()
159 {
160 removeAllInnerLoops();
161 setEndPos(0);
162 }
163 inline Ptr getLoopBeginAt(int pos) const
164 {
165 return getInnerLoopBeginAt(pos);
166 }
167 inline bool addLoop(const InstrumentSequenceLoop& loop)
168 {
169 return addInnerLoop(loop);
170 }
171 inline bool changeLoop(int prevBegin, int prevEnd, const InstrumentSequenceLoop& loop)
172 {
173 return changeInnerLoop(prevBegin, prevEnd, loop);
174 }
175 inline void removeLoop(int begin, int end)
176 {
177 removeInnerLoop(begin, end);
178 }
179 inline void removeAllLoops()
180 {
181 removeAllInnerLoops();
182 }
183 inline std::vector<InstrumentSequenceLoop> getAllLoops() const
184 {
185 return getAllInnerLoops();
186 }
187 inline bool hasLoop() const
188 {
189 return hasInnerLoop();
190 }
191 inline bool hasLoopBeginAt(int pos) const
192 {
193 return hasInnerLoopBeginAt(pos);
194 }
195 InstrumentSequenceLoopRoot clone() const;
196 };
197
198 namespace inst_utils
199 {
200 class LoopStack
201 {
202 private:
203 struct StackItem
204 {
205 InstrumentSequenceLoop::Ptr loop;
206 int count;
207 bool isInfinite;
208
209 explicit StackItem(const InstrumentSequenceLoop::Ptr& ptr);
210 };
211 std::deque<StackItem> stack_;
212
213 public:
214 explicit LoopStack(const std::shared_ptr<InstrumentSequenceLoopRoot>& ptr);
215 void clear();
216 void pushLoopsAtPos(int pos);
217 int checkLoopEndAndNextPos(int curPos);
218 };
219 }
220
221 class InstrumentSequenceRelease
222 {
223 public:
224 enum ReleaseType
225 {
226 NoRelease,
227 FixedRelease,
228 AbsoluteRelease,
229 RelativeRelease
230 };
231
232 static constexpr int DISABLE_RELEASE = -1;
233
234 explicit InstrumentSequenceRelease(ReleaseType getType, int getBeginPos = DISABLE_RELEASE);
235
236 friend bool operator==(const InstrumentSequenceRelease& a, const InstrumentSequenceRelease& b)
237 {
238 return (a.type_ == b.type_ && a.begin_ == b.begin_);
239 }
240
241 friend bool operator!=(const InstrumentSequenceRelease& a, const InstrumentSequenceRelease& b) { return !(a == b); }
242
243 inline ReleaseType getType() const noexcept { return type_; }
244 void setType(ReleaseType type);
245 void setBeginPos(int pos);
246 inline int getBeginPos() const noexcept { return begin_; }
247
248 inline bool isEnabled() const noexcept
249 {
250 return (type_ != ReleaseType::NoRelease && begin_ != DISABLE_RELEASE);
251 }
252
253 void disable();
254
255 private:
256 ReleaseType type_;
257 int begin_;
258 };
259
260 template<class T>
261 class InstrumentSequenceProperty final : public AbstractInstrumentProperty
262 {
263 public:
264 InstrumentSequenceProperty(int num, SequenceType defaultType, const T& defaultUnit, const T& errorUnit, int relReleaseDenom = 1)
265 : AbstractInstrumentProperty(num),
266 DEF_TYPE_(defaultType),
267 DEF_UNIT_(defaultUnit),
268 ERR_UNIT_(errorUnit),
269 REL_RELEASE_DENOM_(relReleaseDenom),
270 loop_(std::make_shared<InstrumentSequenceLoopRoot>(1)),
271 release_(InstrumentSequenceRelease::NoRelease)
272 {
273 clearParameters();
274 }
275
276 friend bool operator==(const InstrumentSequenceProperty& a, const InstrumentSequenceProperty& b)
277 {
278 return (a.type_ == b.type_ && a.seq_ == b.seq_ && a.loop_ == b.loop_ && a.release_ == b.release_);
279 }
280
281 friend bool operator!=(const InstrumentSequenceProperty& a, const InstrumentSequenceProperty& b) { return !(a == b); }
282
283 std::unique_ptr<InstrumentSequenceProperty> clone()
284 {
285 std::unique_ptr<InstrumentSequenceProperty> clone = std::make_unique<InstrumentSequenceProperty>(*this);
286 clone->clearUserInstruments();
287 return clone;
288 }
289
290 inline void setType(SequenceType type) noexcept { type_ = type; }
291 inline SequenceType getType() const noexcept { return type_; }
292
293 bool isEdited() const override
294 {
295 return (seq_.size() > 1 || seq_.front() != DEF_UNIT_ || loop_->hasLoop() || release_.getType() != InstrumentSequenceRelease::NoRelease);
296 }
297
298 void clearParameters() override
299 {
300 type_ = DEF_TYPE_;
301 seq_ = { DEF_UNIT_ };
302 loop_->clear();
303 release_.disable();
304 }
305
306 //***** Sequence *****
307 inline size_t getSequenceSize() const { return seq_.size(); }
308 T getSequenceUnit(int n) const { return seq_.at(static_cast<size_t>(n)); }
309 inline std::vector<T> getSequence() const { return seq_; }
310
311 void addSequenceUnit(const T& unit) {
312 seq_.push_back(unit);
313 loop_->extend();
314 }
315
316 void removeSequenceUnit()
317 {
318 seq_.pop_back();
319 loop_->shrink();
320 if (release_.getBeginPos() == static_cast<int>(seq_.size()))
321 release_.disable();
322 }
323
324 void setSequenceUnit(int n, const T& unit) { seq_.at(static_cast<size_t>(n)) = unit; }
325
326 //***** Loop *****
327 inline InstrumentSequenceLoopRoot getLoopRoot() const { return *loop_; }
328 inline void addLoop(const InstrumentSequenceLoop& loop) const { loop_->addLoop(loop); }
329 inline void removeLoop(int begin, int end) const { loop_->removeLoop(begin, end); }
330 inline void clearLoops() const { loop_->removeAllLoops(); }
331 inline void changeLoop(int prevBegin, int prevEnd, const InstrumentSequenceLoop& loop)
332 {
333 loop_->changeLoop(prevBegin, prevEnd, loop);
334 }
335
336 //***** Release *****
337 InstrumentSequenceRelease getRelease() const noexcept { return release_; }
338 inline void setRelease(const InstrumentSequenceRelease& release) { release_ = release; }
339
340 class Iterator final : public SequenceIteratorInterface<T>
341 {
342 public:
343 explicit Iterator(const InstrumentSequenceProperty* seqProp)
344 : SequenceIteratorInterface<T>(0),
345 seqProp_(seqProp),
346 loopStack_(seqProp->loop_),
347 isRelease_(false),
348 relReleaseRate_(1.)
349 {
350 }
351
352 SequenceType type() const override
353 {
354 return seqProp_->type_;
355 }
356
357 T data() const override
358 {
359 if (this->hasEnded() || static_cast<int>(seqProp_->seq_.size()) <= this->pos_)
360 return seqProp_->ERR_UNIT_;
361 return (isRelease_ ? seqProp_->getSequenceUnit(this->pos_, relReleaseRate_)
362 : seqProp_->getSequenceUnit(this->pos_));
363 }
364
365 int next() override
366 {
367 if (this->hasEnded()) return this->END_SEQ_POS;
368
369 if (this->state_ == SequenceIteratorState::NotBegin) {
370 return this->pos_;
371 }
372
373 this->pos_ = loopStack_.checkLoopEndAndNextPos(this->pos_);
374 loopStack_.pushLoopsAtPos(this->pos_);
375
376 // Range check
377 if ((!isRelease_ && seqProp_->release_.isEnabled() && this->pos_ >= seqProp_->release_.getBeginPos())
378 || this->pos_ >= static_cast<int>(seqProp_->seq_.size())) {
379 this->pos_ = this->END_SEQ_POS;
380 }
381
382 return this->pos_;
383 }
384
385 int front() override
386 {
387 this->state_ = SequenceIteratorState::Run;
388 loopStack_.clear();
389 isRelease_ = false;
390 relReleaseRate_ = 1.;
391
392 if (seqProp_->release_.getBeginPos()) {
393 this->pos_ = 0;
394 loopStack_.pushLoopsAtPos(0);
395 }
396 else {
397 this->pos_ = this->END_SEQ_POS;
398 }
399
400 return this->pos_;
401 }
402
403 int release() override
404 {
405 const std::vector<T>& seq = seqProp_->seq_;
406 const InstrumentSequenceRelease& release = seqProp_->release_;
407
408 int next = this->END_SEQ_POS;
409 isRelease_ = true;
410 loopStack_.clear();
411 switch (release.getType()) {
412 case InstrumentSequenceRelease::NoRelease:
413 break;
414 case InstrumentSequenceRelease::FixedRelease:
415 {
416 next = release.getBeginPos();
417 break;
418 }
419 case InstrumentSequenceRelease::AbsoluteRelease:
420 {
421 int crtr;
422 if (this->pos_ == this->END_SEQ_POS) {
423 if (int relBegin = release.getBeginPos()) {
424 crtr = seq[static_cast<size_t>(relBegin - 1)].data;
425 }
426 else {
427 next = relBegin;
428 break;
429 }
430 }
431 else {
432 crtr = seq[static_cast<size_t>(this->pos_)].data;
433 }
434
435 auto&& it = std::find_if(seq.begin() + release.getBeginPos(), seq.end(),
436 [crtr](const T& unit) { return (unit.data <= crtr); });
437 if (it != seq.end()) next = std::distance(seq.begin(), it);
438 break;
439 }
440 case InstrumentSequenceRelease::RelativeRelease:
441 {
442 next = release.getBeginPos();
443 if (this->hasEnded()) {
444 if (next) relReleaseRate_ = seq[static_cast<size_t>(next - 1)].data / seqProp_->REL_RELEASE_DENOM_;
445 }
446 else {
447 relReleaseRate_ = seq[static_cast<size_t>(this->pos_)].data / seqProp_->REL_RELEASE_DENOM_;
448 }
449 break;
450 }
451 }
452 this->pos_ = next;
453
454 if (next == this->END_SEQ_POS) {
455 this->state_ = SequenceIteratorState::End;
456 }
457 else {
458 this->state_ = SequenceIteratorState::RunRelease;
459 loopStack_.pushLoopsAtPos(this->pos_);
460 }
461
462 return this->pos_;
463 }
464
465 int end() override
466 {
467 this->pos_ = this->END_SEQ_POS;
468 this->state_ = SequenceIteratorState::End;
469 return this->END_SEQ_POS;
470 }
471
472 private:
473 const InstrumentSequenceProperty* seqProp_;
474 inst_utils::LoopStack loopStack_;
475 bool isRelease_;
476 double relReleaseRate_;
477 };
478
479 std::unique_ptr<InstrumentSequenceProperty::Iterator> getIterator()
480 {
481 return std::make_unique<Iterator>(this);
482 }
483
484 private:
485 const SequenceType DEF_TYPE_;
486 const T DEF_UNIT_;
487 const T ERR_UNIT_;
488 const double REL_RELEASE_DENOM_;
489
490 SequenceType type_;
491 std::vector<T> seq_;
492 std::shared_ptr<InstrumentSequenceLoopRoot> loop_;
493 InstrumentSequenceRelease release_;
494
495 inline T getSequenceUnit(int n, double relRate) const
496 {
497 T unit = seq_.at(static_cast<size_t>(n));
498 unit.data = static_cast<int>(unit.data * relRate);
499 return unit;
500 }
501 };