Debian Codegraph @

BambooTracker/module/effect.cpp

raw
1 /*
2 * Copyright (C) 2019-2022 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 "effect.hpp"
27 #include <unordered_map>
28 #include "step.hpp"
29
30 namespace
31 {
32 const std::unordered_map<char, int> CTOHEX = {
33 { '0', 0 },
34 { '1', 1 },
35 { '2', 2 },
36 { '3', 3 },
37 { '4', 4 },
38 { '5', 5 },
39 { '6', 6 },
40 { '7', 7 },
41 { '8', 8 },
42 { '9', 9 },
43 { 'A', 10 },
44 { 'B', 11 },
45 { 'C', 12 },
46 { 'D', 13 },
47 { 'E', 14 },
48 { 'F', 15 }
49 };
50 }
51
52 namespace effect_utils
53 {
54 EffectType validateEffectId(SoundSource src, const std::string& id)
55 {
56 if (id == "00") {
57 switch (src) {
58 case SoundSource::FM:
59 case SoundSource::SSG:
60 case SoundSource::ADPCM:
61 return EffectType::Arpeggio;
62 default:
63 return EffectType::NoEffect;
64 }
65 }
66 else if (id == "01") {
67 switch (src) {
68 case SoundSource::FM:
69 case SoundSource::SSG:
70 case SoundSource::ADPCM:
71 return EffectType::PortamentoUp;
72 default:
73 return EffectType::NoEffect;
74 }
75 }
76 else if (id == "02") {
77 switch (src) {
78 case SoundSource::FM:
79 case SoundSource::SSG:
80 case SoundSource::ADPCM:
81 return EffectType::PortamentoDown;
82 default:
83 return EffectType::NoEffect;
84 }
85 }
86 else if (id == "03") {
87 switch (src) {
88 case SoundSource::FM:
89 case SoundSource::SSG:
90 case SoundSource::ADPCM:
91 return EffectType::TonePortamento;
92 default:
93 return EffectType::NoEffect;
94 }
95 }
96 else if (id == "04") {
97 switch (src) {
98 case SoundSource::FM:
99 case SoundSource::SSG:
100 case SoundSource::ADPCM:
101 return EffectType::Vibrato;
102 default:
103 return EffectType::NoEffect;
104 }
105 }
106 else if (id == "07") {
107 switch (src) {
108 case SoundSource::FM:
109 case SoundSource::SSG:
110 case SoundSource::ADPCM:
111 return EffectType::Tremolo;
112 default:
113 return EffectType::NoEffect;
114 }
115 }
116 else if (id == "08") {
117 switch (src) {
118 case SoundSource::FM:
119 case SoundSource::RHYTHM:
120 case SoundSource::ADPCM:
121 return EffectType::Pan;
122 default:
123 return EffectType::NoEffect;
124 }
125 }
126 else if (id == "0A") {
127 switch (src) {
128 case SoundSource::FM:
129 case SoundSource::SSG:
130 case SoundSource::ADPCM:
131 return EffectType::VolumeSlide;
132 default:
133 return EffectType::NoEffect;
134 }
135 }
136 else if (id == "0B") {
137 return EffectType::PositionJump;
138 }
139 else if (id == "0C") {
140 return EffectType::SongEnd;
141 }
142 else if (id == "0D") {
143 return EffectType::PatternBreak;
144 }
145 else if (id == "0F") {
146 return EffectType::SpeedTempoChange;
147 }
148 else if (id == "0G") {
149 return EffectType::NoteDelay;
150 }
151 else if (id == "0H") {
152 switch (src) {
153 case SoundSource::SSG:
154 return EffectType::AutoEnvelope;
155 default:
156 return EffectType::NoEffect;
157 }
158 }
159 else if (id == "0I") {
160 switch (src) {
161 case SoundSource::SSG:
162 return EffectType::HardEnvHighPeriod;
163 default:
164 return EffectType::NoEffect;
165 }
166 }
167 else if (id == "0J") {
168 switch (src) {
169 case SoundSource::SSG:
170 return EffectType::HardEnvLowPeriod;
171 default:
172 return EffectType::NoEffect;
173 }
174 }
175 else if (id == "0K") {
176 return EffectType::Retrigger;
177 }
178 else if (id == "0O") {
179 return EffectType::Groove;
180 }
181 else if (id == "0P") {
182 switch (src) {
183 case SoundSource::FM:
184 case SoundSource::SSG:
185 case SoundSource::ADPCM:
186 return EffectType::Detune;
187 default:
188 return EffectType::NoEffect;
189 }
190 }
191 else if (id == "0Q") {
192 switch (src) {
193 case SoundSource::FM:
194 case SoundSource::SSG:
195 case SoundSource::ADPCM:
196 return EffectType::NoteSlideUp;
197 default:
198 return EffectType::NoEffect;
199 }
200 }
201 else if (id == "0R") {
202 switch (src) {
203 case SoundSource::FM:
204 case SoundSource::SSG:
205 case SoundSource::ADPCM:
206 return EffectType::NoteSlideDown;
207 default:
208 return EffectType::NoEffect;
209 }
210 }
211 else if (id == "0S") {
212 return EffectType::NoteRelease;
213 }
214 else if (id == "0T") {
215 switch (src) {
216 case SoundSource::FM:
217 case SoundSource::SSG:
218 case SoundSource::ADPCM:
219 return EffectType::TransposeDelay;
220 default:
221 return EffectType::NoEffect;
222 }
223 }
224 else if (id == "0V") {
225 switch (src) {
226 case SoundSource::SSG:
227 return EffectType::ToneNoiseMix;
228 case SoundSource::RHYTHM:
229 return EffectType::MasterVolume;
230 default:
231 return EffectType::NoEffect;
232 }
233 }
234 else if (id == "0W") {
235 switch (src) {
236 case SoundSource::SSG:
237 return EffectType::NoisePitch;
238 default:
239 return EffectType::NoEffect;
240 }
241 }
242 else if (id == "0X") {
243 return EffectType::RegisterAddress0;
244 }
245 else if (id == "0Y") {
246 return EffectType::RegisterAddress1;
247 }
248 else if (id == "0Z") {
249 return EffectType::RegisterValue;
250 }
251 else if (id == "B0") {
252 switch (src) {
253 case SoundSource::FM:
254 return EffectType::Brightness;
255 default:
256 return EffectType::NoEffect;
257 }
258 }
259 else if (id == "EA") {
260 switch (src) {
261 case SoundSource::FM:
262 case SoundSource::SSG:
263 case SoundSource::ADPCM:
264 return EffectType::XVolumeSlide;
265 default:
266 return EffectType::NoEffect;
267 }
268 }
269 else if (id == "ES") {
270 return EffectType::NoteCut;
271 }
272 else if (id == "FB") {
273 switch (src) {
274 case SoundSource::FM:
275 return EffectType::FBControl;
276 default:
277 return EffectType::NoEffect;
278 }
279 }
280 else if (id == "FP") {
281 switch (src) {
282 case SoundSource::FM:
283 case SoundSource::SSG:
284 case SoundSource::ADPCM:
285 return EffectType::FineDetune;
286 default:
287 return EffectType::NoEffect;
288 }
289 }
290 else if (id == "ML") {
291 switch (src) {
292 case SoundSource::FM:
293 return EffectType::MLControl;
294 default:
295 return EffectType::NoEffect;
296 }
297 }
298 else if (id == "RR") {
299 switch (src) {
300 case SoundSource::FM:
301 return EffectType::RRControl;
302 default:
303 return EffectType::NoEffect;
304 }
305 }
306 else {
307 switch (id.front()) {
308 case 'A':
309 switch (src) {
310 case SoundSource::FM:
311 return EffectType::ARControl;
312 default:
313 return EffectType::NoEffect;
314 }
315 case 'D':
316 switch (src) {
317 case SoundSource::FM:
318 return EffectType::DRControl;
319 default:
320 return EffectType::NoEffect;
321 }
322 case 'M':
323 return EffectType::VolumeDelay;
324 case 'T':
325 switch (src) {
326 case SoundSource::FM:
327 return EffectType::TLControl;
328 default:
329 return EffectType::NoEffect;
330 }
331 default:
332 return EffectType::NoEffect;
333 }
334 }
335 }
336
337 Effect validateEffect(SoundSource src, const std::string& id, int value)
338 {
339 if (value == Step::EFF_VAL_NONE) return { EffectType::NoEffect, Step::EFF_VAL_NONE };
340
341 EffectType type = effect_utils::validateEffectId(src, id);
342
343 int v;
344 switch (type) {
345 case EffectType::NoEffect:
346 v = Step::EFF_VAL_NONE;
347 break;
348 case EffectType::VolumeDelay:
349 case EffectType::TLControl:
350 case EffectType::ARControl:
351 case EffectType::DRControl:
352 v = (CTOHEX.at(id[1]) << 8) | value;
353 break;
354 default:
355 v = value;
356 }
357
358 return { type, v };
359 }
360 }