Debian Codegraph @

BambooTracker/instrument/sample_adpcm.cpp

raw
1 /*
2 * Copyright (C) 2020-2023 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 "sample_adpcm.hpp"
27 #include <algorithm>
28
29 namespace
30 {
31 constexpr int DEF_RT_DELTAN_ = 0x49cd; // 16000Hz
32 constexpr bool DEF_REPET_ = false;
33 }
34
35 SampleADPCM::SampleADPCM(int num)
36 : AbstractInstrumentProperty (num), repeatRange_(0, 0)
37 {
38 clearParameters();
39 }
40
41 bool operator==(const SampleADPCM& a, const SampleADPCM& b) {
42 return (a.rootKeyNum_ == b.rootKeyNum_ && a.rootDeltaN_ == b.rootDeltaN_
43 && a.isRepeated_ == b.isRepeated_ && a.sample_ == b.sample_);
44 }
45
46 std::unique_ptr<SampleADPCM> SampleADPCM::clone()
47 {
48 std::unique_ptr<SampleADPCM> clone = std::make_unique<SampleADPCM>(*this);
49 clone->clearUserInstruments();
50 return clone;
51 }
52
53 void SampleADPCM::clearSample()
54 {
55 startAddress_ = 0;
56 stopAddress_ = 0;
57 sample_ = std::vector<uint8_t>(1);
58 repeatRange_ = SampleRepeatRange(0, (sample_.size() - 1) >> 5); // By 32 bytes
59 }
60
61 bool SampleADPCM::isEdited() const
62 {
63 if (rootKeyNum_ != DEF_ROOT_KEY
64 || rootDeltaN_ != DEF_RT_DELTAN_
65 || isRepeated_ != DEF_REPET_
66 || sample_.size() != 1
67 || sample_.front() != 0)
68 return true;
69 return false;
70 }
71
72 void SampleADPCM::clearParameters()
73 {
74 clearSample();
75
76 rootKeyNum_ = DEF_ROOT_KEY;
77 rootDeltaN_ = DEF_RT_DELTAN_;
78 isRepeated_ = DEF_REPET_;
79 }
80
81 bool SampleADPCM::setRepeatRange(const SampleRepeatRange& range) noexcept
82 {
83 if (sample_.size() <= range.last()) {
84 return false;
85 }
86
87 repeatRange_ = range;
88 return true;
89 }
90
91 bool SampleADPCM::storeSample(const std::vector<uint8_t>& sample)
92 {
93 if (sample.empty()) return false;
94
95 repeatRange_ = repeatRange_.clampLast((sample.size() - 1) >> 5); // By 32 bytes
96 sample_ = sample;
97
98 return true;
99 }
100
101 bool SampleADPCM::storeSample(std::vector<uint8_t>&& sample)
102 {
103 if (sample.empty()) return false;
104
105 repeatRange_ = repeatRange_.clampLast((sample.size() - 1) >> 5); // By 32 bytes
106 sample_ = std::move(sample);
107
108 return true;
109 }