Debian Codegraph @

BambooTracker/module/track.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 "track.hpp"
27 #include <utility>
28 #include <algorithm>
29 #include "utils.hpp"
30
31 namespace
32 {
33 constexpr int PATTERN_SIZE = 256;
34 constexpr int MAX_ORDER_SIZE = 256;
35 }
36
37 Track::Track(int number, SoundSource source, int channelInSource, int defPattenSize)
38 : effetDisplayWidth_(0), visibility_(true)
39
40 {
41 setAttribute(number, source, channelInSource);
42
43 patterns_.reserve(PATTERN_SIZE);
44 for (int i = 0; i < PATTERN_SIZE; ++i) {
45 patterns_.emplace_back(i, defPattenSize);
46 }
47
48 patterns_[0].increaseUsedCount();
49 order_.push_back(0); // Set first order
50 }
51
52 void Track::setAttribute(int number, SoundSource source, int channelInSource) noexcept
53 {
54 attrib_.number = number;
55 attrib_.source = source;
56 attrib_.channelInSource = channelInSource;
57 }
58
59 OrderInfo Track::getOrderInfo(int order) const
60 {
61 OrderInfo info;
62 info.trackAttribute = attrib_;
63 info.order = order;
64 info.patten = order_.at(static_cast<size_t>(order));
65 return info;
66 }
67
68 size_t Track::getOrderSize() const
69 {
70 return order_.size();
71 }
72
73 bool Track::canAddNewOrder() const
74 {
75 return order_.size() < MAX_ORDER_SIZE;
76 }
77
78 Pattern& Track::getPattern(int num)
79 {
80 return patterns_.at(static_cast<size_t>(num));
81 }
82
83 Pattern& Track::getPatternFromOrderNumber(int num)
84 {
85 return getPattern(order_.at(static_cast<size_t>(num)));
86 }
87
88 int Track::searchFirstUneditedUnusedPattern() const
89 {
90 auto it = utils::findIf(patterns_, [](const Pattern& pattern) {
91 return (!pattern.hasEvent() && !pattern.getUsedCount());
92 });
93 return (it == patterns_.cend() ? -1 : std::distance(patterns_.cbegin(), it));
94 }
95
96 int Track::clonePattern(int num)
97 {
98 int n = searchFirstUneditedUnusedPattern();
99 if (n == -1) return num;
100 else {
101 patterns_.at(static_cast<size_t>(n)) = patterns_.at(static_cast<size_t>(num)).clone(n);
102 return n;
103 }
104 }
105
106 std::vector<int> Track::getEditedPatternIndices() const
107 {
108 return utils::findIndicesIf(patterns_, [](const Pattern& pattern) { return pattern.hasEvent(); });
109 }
110
111 std::set<int> Track::getRegisteredInstruments() const
112 {
113 std::set<int> set;
114 for (const Pattern& pattern : patterns_) {
115 auto&& insts = pattern.getRegisteredInstruments();
116 std::copy(insts.cbegin(), insts.cend(), std::inserter(set, set.end()));
117 }
118 return set;
119 }
120
121 void Track::registerPatternToOrder(int order, int pattern)
122 {
123 patterns_.at(static_cast<size_t>(pattern)).increaseUsedCount();
124 patterns_.at(static_cast<size_t>(order_.at(static_cast<size_t>(order)))).decreaseUsedCount();
125 order_.at(static_cast<size_t>(order)) = pattern;
126 }
127
128 void Track::insertOrderBelow(int order)
129 {
130 int n = searchFirstUneditedUnusedPattern();
131 if (n == -1) n = PATTERN_SIZE - 1;
132
133 if (order == static_cast<int>(order_.size()) - 1) order_.push_back(n);
134 else order_.insert(order_.begin() + order + 1, n);
135 patterns_[static_cast<size_t>(n)].increaseUsedCount();
136 }
137
138 void Track::deleteOrder(int order)
139 {
140 patterns_.at(static_cast<size_t>(order_.at(static_cast<size_t>(order)))).decreaseUsedCount();
141 order_.erase(order_.begin() + order);
142 }
143
144 void Track::swapOrder(int a, int b)
145 {
146 std::swap(order_.at(static_cast<size_t>(a)), order_.at((static_cast<size_t>(b))));
147 }
148
149 void Track::changeDefaultPatternSize(size_t size)
150 {
151 for (auto& ptn : patterns_) ptn.changeSize(size);
152 }
153
154 void Track::clearUnusedPatterns()
155 {
156 for (Pattern& pattern : patterns_) {
157 if (!pattern.getUsedCount() && pattern.hasEvent())
158 pattern.clear();
159 }
160 }
161
162 void Track::replaceDuplicateInstrumentsInPatterns(const std::unordered_map<int, int>& map)
163 {
164 for (Pattern& pattern : patterns_) {
165 if (pattern.hasEvent()) {
166 for (size_t i = 0; i < pattern.getSize(); ++i) {
167 Step& step = pattern.getStep(static_cast<int>(i));
168 int inst = step.getInstrumentNumber();
169 if (map.count(inst)) step.setInstrumentNumber(map.at(inst));
170 }
171 }
172 }
173 }
174
175 void Track::transpose(int semitones, const std::vector<int>& excludeInsts)
176 {
177 for (Pattern& pattern : patterns_) pattern.transpose(semitones, excludeInsts);
178 }