Debian Codegraph @

BambooTracker/module/module.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 "module.hpp"
27 #include <algorithm>
28 #include <iterator>
29 #include "utils.hpp"
30
31 Module::Module(const std::string& filePath, const std::string& title,
32 const std::string& author, const std::string& copyright,
33 const std::string& comment, unsigned int tickFreq)
34 : filePath_(filePath),
35 title_(title),
36 author_(author),
37 copyright_(copyright),
38 comment_(comment),
39 tickFreq_(tickFreq),
40 stepHl1Dist_(4),
41 stepHl2Dist_(16),
42 mixType_(MixerType::PC_9821_PC_9801_86),
43 customLevelFM_(0),
44 customLevelSSG_(0)
45 {
46 songs_.emplace_back(0);
47 addGroove();
48 }
49
50 size_t Module::getSongCount() const
51 {
52 return songs_.size();
53 }
54
55 size_t Module::getGrooveCount() const
56 {
57 return grooves_.size();
58 }
59
60 void Module::addSong(SongType songType, const std::string& title)
61 {
62 songs_.emplace_back(static_cast<int>(songs_.size()), songType, title);
63 }
64
65 void Module::addSong(int n, SongType songType, const std::string& title, bool isUsedTempo,
66 int tempo, int groove, int speed, size_t defaultPatternSize)
67 {
68 if (n < static_cast<int>(songs_.size()))
69 songs_.at(static_cast<size_t>(n))
70 = Song(n, songType, title, isUsedTempo, tempo, groove, speed, defaultPatternSize);
71 else
72 songs_.emplace_back(
73 n, songType, title, isUsedTempo, tempo, groove, speed, defaultPatternSize);
74 }
75
76 void Module::sortSongs(const std::vector<int>& numbers)
77 {
78 std::vector<Song> newSongs;
79 newSongs.reserve(songs_.size());
80
81 for (auto& n : numbers) {
82 auto it = std::make_move_iterator(songs_.begin() + n);
83 it->setNumber(static_cast<int>(newSongs.size()));
84 newSongs.push_back(*it);
85 }
86
87 songs_ = std::move(newSongs);
88 }
89
90 Song& Module::getSong(int num)
91 {
92 return *utils::findIf(songs_, [num](Song& s) { return s.getNumber() == num; });;
93 }
94
95 void Module::addGroove()
96 {
97 // Default groove is "6 6"
98 grooves_.push_back({ 6, 6 });
99 }
100
101 void Module::removeGroove(int num)
102 {
103 grooves_.erase(grooves_.begin() + num);
104 }
105
106 void Module::setGroove(int num, const std::vector<int>& seq)
107 {
108 grooves_.at(static_cast<size_t>(num)) = seq;
109 }
110
111 void Module::setGrooves(const std::vector<std::vector<int>>& seqs)
112 {
113 grooves_ = seqs;
114 }
115
116 Groove Module::getGroove(int num) const
117 {
118 return grooves_.at(static_cast<size_t>(num));
119 }
120
121 std::set<int> Module::getRegisterdInstruments() const
122 {
123 std::set<int> set;
124 for (const Song& song : songs_) {
125 auto&& subset = song.getRegisteredInstruments();
126 std::copy(subset.begin(), subset.end(), std::inserter(set, set.end()));
127 }
128 return set;
129 }
130
131 void Module::clearUnusedPatterns()
132 {
133 for (Song& song : songs_) song.clearUnusedPatterns();
134 }
135
136 void Module::replaceDuplicateInstrumentsInPatterns(const std::unordered_map<int, int>& map)
137 {
138 for (Song& song : songs_) song.replaceDuplicateInstrumentsInPatterns(map);
139 }