Debian Codegraph @

BambooTracker/instrument/sequence_property.cpp

raw
1 /*
2 * Copyright (C) 2018-2021 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 #include "sequence_property.hpp"
27 #include "utils.hpp"
28
29 InstrumentSequenceBaseUnit::InstrumentSequenceBaseUnit() noexcept
30 : data(ERR_DATA)
31 {
32 }
33
34 InstrumentSequenceBaseUnit::InstrumentSequenceBaseUnit(int d) noexcept
35 : data(d)
36 {
37 }
38
39 InstrumentSequenceExtendUnit::InstrumentSequenceExtendUnit() noexcept
40 : InstrumentSequenceBaseUnit(),
41 type(InstrumentSequenceExtendUnit::UnusedSubdata),
42 subdata(InstrumentSequenceBaseUnit::ERR_DATA)
43 {
44 }
45
46 InstrumentSequenceExtendUnit::InstrumentSequenceExtendUnit(int d, SubdataType subType, int subData) noexcept
47 : InstrumentSequenceBaseUnit(d), type(subType), subdata(subData)
48 {
49 }
50
51 InstrumentSequenceExtendUnit InstrumentSequenceExtendUnit::makeOnlyDataUnit(int data) noexcept
52 {
53 return InstrumentSequenceExtendUnit(data, SubdataType::UnusedSubdata, ERR_DATA);
54 }
55
56 InstrumentSequenceExtendUnit InstrumentSequenceExtendUnit::makeRawUnit(int data, int sub) noexcept
57 {
58 return InstrumentSequenceExtendUnit(data, SubdataType::RawSubdata, sub);
59 }
60
61 InstrumentSequenceExtendUnit InstrumentSequenceExtendUnit::makeRatioUnit(int data, int subFirst, int subSecond) noexcept
62 {
63 return InstrumentSequenceExtendUnit(data, SubdataType::RatioSubdata, ((1 << 16) | (subFirst << 8) | subSecond));
64 }
65
66 InstrumentSequenceExtendUnit InstrumentSequenceExtendUnit::makeShiftUnit(int data, int rshift) noexcept
67 {
68 int sub = (rshift > 0) ? ((2 << 16) | rshift) : ((3 << 16) | -rshift);
69 return InstrumentSequenceExtendUnit(data, SubdataType::ShiftSubdata, sub);
70 }
71
72 InstrumentSequenceExtendUnit InstrumentSequenceExtendUnit::makeUnitWithDecode(int data, int subsrc) noexcept
73 {
74 SubdataType type;
75 if (subsrc & 0x20000) type = SubdataType::ShiftSubdata;
76 else type = (subsrc & 0x10000) ? SubdataType::RatioSubdata : SubdataType::RawSubdata;
77 return InstrumentSequenceExtendUnit(data, type, subsrc);
78 }
79
80 void InstrumentSequenceExtendUnit::getSubdataAsRaw(int& raw) const noexcept
81 {
82 raw = subdata;
83 }
84
85 void InstrumentSequenceExtendUnit::getSubdataAsRatio(int& first, int& second) const noexcept
86 {
87 first = (subdata & 0x0ff00) >> 8;
88 second = subdata & 0x000ff;
89 }
90
91 void InstrumentSequenceExtendUnit::getSubdataAsShift(int& rshift) const noexcept
92 {
93 rshift = (subdata & 0x10000) ? -(0x0ffff & data) : (0x0ffff & data);
94 }
95
96 namespace
97 {
98 inline InstrumentSequenceLoop::Ptr makeSequenceLoopPtr(const InstrumentSequenceLoop& loop)
99 {
100 return std::make_shared<InstrumentSequenceLoop>(loop);
101 }
102 }
103
104 InstrumentSequenceLoop::InstrumentSequenceLoop(int begin, int end, int times)
105 : begin_(begin), end_(end), times_(times)
106 {
107 }
108
109 void InstrumentSequenceLoop::setBeginPos(int pos)
110 {
111 if (begin_ < pos && hasInnerLoop()) {
112 auto&& it = utils::findIf(childs_, [pos](const auto& pair) {
113 return pos <= pair.second->end_;
114 });
115 if (it == childs_.end()) {
116 removeAllInnerLoops();
117 }
118 else {
119 if (it->second->begin_ < pos) it->second->setBeginPos(pos);
120 childs_.erase(childs_.begin(), it);
121 }
122 }
123 begin_ = pos;
124 }
125
126 void InstrumentSequenceLoop::setEndPos(int pos)
127 {
128 if (pos < end_ && hasInnerLoop()) {
129 auto&& it = std::find_if(childs_.rbegin(), childs_.rend(), [pos](const auto& pair) {
130 return pair.second->begin_ <= pos;
131 });
132 if (it == childs_.rend()) {
133 removeAllInnerLoops();
134 }
135 else {
136 if (pos < it->second->end_) it->second->setEndPos(pos);
137 childs_.erase(it.base(), childs_.end());
138 }
139 }
140 end_ = pos;
141 }
142
143 bool InstrumentSequenceLoop::addInnerLoop(const InstrumentSequenceLoop& inner)
144 {
145 for (auto& pair : childs_) {
146 Ptr& loop = pair.second;
147 if (loop->isOverlapped(inner)) {
148 if (loop->hasSameRegion(inner)) {
149 loop->times_ = inner.times_;
150 return true;
151 }
152 else if (loop->isContainable(inner)) {
153 return loop->addInnerLoop(inner);
154 }
155 else { // Illegal region
156 return false;
157 }
158 }
159 else if (loop->begin_ < inner.begin_) break;
160 }
161
162 // Add new region
163 childs_.insert(std::make_pair(inner.begin_, makeSequenceLoopPtr(inner)));
164 return true;
165 }
166
167 bool InstrumentSequenceLoop::changeInnerLoop(int prevBegin, int prevEnd, const InstrumentSequenceLoop& loop)
168 {
169 if (hasInnerLoopBeginAt(prevBegin)) {
170 InstrumentSequenceLoop::Ptr lpt = getInnerLoopBeginAt(prevBegin);
171 if (lpt->end_ != prevEnd) return lpt->changeInnerLoop(prevBegin, prevEnd, loop);
172 if (prevBegin != loop.begin_) lpt->setBeginPos(loop.begin_);
173 if (prevEnd != loop.end_) lpt->setEndPos(loop.end_);
174 if (lpt->times_ != loop.times_) lpt->setTimes(loop.times_);
175 return true;
176 }
177 else {
178 return false;
179 }
180 }
181
182 void InstrumentSequenceLoop::removeInnerLoop(int begin, int end)
183 {
184 for (auto& pair : childs_) {
185 Ptr& loop = pair.second;
186 if (loop->isOverlapped(begin, end)) {
187 if (loop->hasSameRegion(begin, end)) {
188 childs_.erase(pair.first);
189 }
190 else if (loop->isContainable(begin, end)) {
191 loop->removeInnerLoop(begin, end);
192 }
193 return;
194 }
195 else if (loop->begin_ < begin) return;
196 }
197 }
198
199 void InstrumentSequenceLoop::removeAllInnerLoops()
200 {
201 childs_.clear();
202 }
203
204 std::vector<InstrumentSequenceLoop> InstrumentSequenceLoop::getAllInnerLoops() const
205 {
206 std::vector<InstrumentSequenceLoop> list;
207 for (const auto& pair : childs_) {
208 list.push_back(*pair.second);
209 auto in = pair.second->getAllInnerLoops();
210 list.insert(list.end(), in.begin(), in.end());
211 }
212 return list;
213 }
214
215 bool InstrumentSequenceLoop::isOverlapped(int begin, int end) const
216 {
217 if (begin_ == begin) return true;
218 if (begin_ < begin) return (begin <= end_);
219 else /* begin < begin_ */ return (begin_ <= end);
220 }
221
222 bool InstrumentSequenceLoop::isContainable(int begin, int end) const
223 {
224 return ((begin_ < begin && end <= end_)
225 || (begin_ == begin && end < end_));
226 }
227
228 bool InstrumentSequenceLoop::hasSameRegion(int begin, int end) const
229 {
230 return (begin_ == begin && end_ == end);
231 }
232
233 InstrumentSequenceLoop InstrumentSequenceLoop::clone() const
234 {
235 InstrumentSequenceLoop l(begin_, end_, times_);
236 for (const auto& pair : childs_) {
237 l.childs_.insert(std::make_pair(pair.first, makeSequenceLoopPtr(pair.second->clone())));
238 }
239 return l;
240 }
241
242 InstrumentSequenceLoopRoot InstrumentSequenceLoopRoot::clone() const
243 {
244 InstrumentSequenceLoopRoot r(end_);
245 for (const auto& pair : childs_) {
246 r.childs_.insert(std::make_pair(pair.first, makeSequenceLoopPtr(pair.second->clone())));
247 }
248 return r;
249 }
250
251 namespace inst_utils
252 {
253 LoopStack::StackItem::StackItem(const InstrumentSequenceLoop::Ptr& ptr)
254 : loop(ptr), count(ptr->getTimes()), isInfinite(ptr->isInfinite())
255 {
256 }
257
258 LoopStack::LoopStack(const std::shared_ptr<InstrumentSequenceLoopRoot>& ptr)
259 : stack_{ StackItem(std::static_pointer_cast<InstrumentSequenceLoop>(ptr)) }
260 {
261 }
262
263 void LoopStack::clear()
264 {
265 stack_.erase(stack_.begin() + 1, stack_.end());
266 }
267
268 void LoopStack::pushLoopsAtPos(int pos)
269 {
270 while (true) {
271 InstrumentSequenceLoop::Ptr& loop = stack_.back().loop;
272 if (!loop->hasInnerLoopBeginAt(pos)) break;
273 stack_.emplace_back(loop->getInnerLoopBeginAt(pos));
274 }
275 }
276
277 int LoopStack::checkLoopEndAndNextPos(int curPos)
278 {
279 while (stack_.size() > 1) {
280 StackItem& item = stack_.back();
281 if (item.loop->getEndPos() != curPos) {
282 return curPos + 1;
283 }
284 if (item.isInfinite) {
285 return item.loop->getBeginPos();
286 }
287 if (--(item.count)) {
288 return item.loop->getBeginPos();
289 }
290 else {
291 stack_.pop_back();
292 }
293 }
294 return curPos + 1;
295 }
296 }
297
298 InstrumentSequenceRelease::InstrumentSequenceRelease(ReleaseType type, int beginPos)
299 : type_(type),
300 begin_(type_ == ReleaseType::NoRelease ? DISABLE_RELEASE : beginPos)
301 {
302 }
303
304 void InstrumentSequenceRelease::setType(ReleaseType type)
305 {
306 type_ = type;
307 if (type == ReleaseType::NoRelease) begin_ = DISABLE_RELEASE;
308 }
309 void InstrumentSequenceRelease::setBeginPos(int pos)
310 {
311 if (type_ != ReleaseType::NoRelease) begin_ = pos;
312 }
313
314 void InstrumentSequenceRelease::disable()
315 {
316 type_ = ReleaseType::NoRelease;
317 begin_ = DISABLE_RELEASE;
318 }