Debian Codegraph @

BambooTracker/module/pattern.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 "pattern.hpp"
27 #include <algorithm>
28 #include "effect.hpp"
29 #include "note.hpp"
30 #include "utils.hpp"
31
32 namespace
33 {
34 constexpr size_t MAX_STEP_SIZE = 256;
35 }
36
37 Pattern::Pattern(int n, size_t defSize)
38 : num_(n), size_(defSize), steps_(defSize), usedCnt_(0)
39 {
40 }
41
42 Pattern::Pattern(int n, size_t size, const std::vector<Step>& steps)
43 : num_(n), size_(size), steps_(steps), usedCnt_(0)
44 {
45 }
46
47 Step& Pattern::getStep(int n)
48 {
49 return steps_.at(static_cast<size_t>(n));
50 }
51
52 size_t Pattern::getSize() const
53 {
54 for (size_t i = 0; i < size_; ++i) {
55 for (int j = 0; j < Step::N_EFFECT; ++j) {
56 if (!steps_[i].hasEffectValue(j)) continue;
57 // "SoundSource::FM" is dummy, these effects are not related with sound source
58 switch (effect_utils::validateEffectId(SoundSource::FM, steps_[i].getEffectId(j))) {
59 case EffectType::PositionJump:
60 case EffectType::SongEnd:
61 case EffectType::PatternBreak:
62 return i + 1;
63 default:
64 break;
65 }
66 }
67 }
68 return size_;
69 }
70
71 void Pattern::changeSize(size_t size)
72 {
73 if (size && size <= MAX_STEP_SIZE) {
74 size_ = size;
75 if (steps_.size() < size) steps_.resize(size);
76 }
77 }
78
79 void Pattern::insertStep(int n)
80 {
81 if (n < static_cast<int>(size_))
82 steps_.emplace(steps_.begin() + n);
83 }
84
85 void Pattern::deletePreviousStep(int n)
86 {
87 if (!n) return;
88
89 steps_.erase(steps_.begin() + n - 1);
90 if (steps_.size() < size_)
91 steps_.resize(size_);
92 }
93
94 bool Pattern::hasEvent() const
95 {
96 auto endIt = steps_.cbegin() + static_cast<int>(size_);
97 return std::any_of(steps_.cbegin(), endIt,
98 [](const Step& step) { return step.hasEvent(); });
99 }
100
101 std::vector<int> Pattern::getEditedStepIndices() const
102 {
103 auto endIt = steps_.cbegin() + static_cast<int>(size_);
104 return utils::findIndicesIf(steps_.cbegin(), endIt,
105 [](const Step& step) { return step.hasEvent(); });
106 }
107
108 std::set<int> Pattern::getRegisteredInstruments() const
109 {
110 std::set<int> set;
111 for (size_t i = 0; i < size_; ++i) {
112 const Step& step = steps_.at(i);
113 if (step.hasInstrument()) set.insert(step.getInstrumentNumber());
114 }
115 return set;
116 }
117
118 Pattern Pattern::clone(int asNumber)
119 {
120 return Pattern(asNumber, size_, steps_);
121 }
122
123 void Pattern::transpose(int semitones, const std::vector<int>& excludeInsts)
124 {
125 for (size_t i = 0; i < size_; ++i) {
126 Step& step = steps_.at(i);
127 int note = step.getNoteNumber();
128 if (step.hasGeneralNote() && std::none_of(excludeInsts.begin(), excludeInsts.end(),
129 [a = step.getInstrumentNumber()](int b) { return a == b; })) {
130 step.setNoteNumber(utils::clamp(note + semitones, 0, Note::NOTE_NUMBER_RANGE - 1));
131 }
132 }
133 }
134
135 void Pattern::clear()
136 {
137 steps_ = std::vector<Step>(size_);
138 }