Debian Codegraph @

BambooTracker/instrument/sample_adpcm.hpp

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 #pragma once
27
28 #include <vector>
29 #include <cstdint>
30 #include <memory>
31 #include <cmath>
32 #include "abstract_instrument_property.hpp"
33 #include "sample_repeat.hpp"
34
35 class SampleADPCM final : public AbstractInstrumentProperty
36 {
37 public:
38 explicit SampleADPCM(int num);
39
40 friend bool operator==(const SampleADPCM& a, const SampleADPCM& b);
41 friend bool operator!=(const SampleADPCM& a, const SampleADPCM& b) { return !(a == b); }
42
43 std::unique_ptr<SampleADPCM> clone();
44
45 void setRootKeyNumber(int n) noexcept { rootKeyNum_ = n; }
46 int getRootKeyNumber() const noexcept {return rootKeyNum_; }
47 void setRootDeltaN(int dn) noexcept { rootDeltaN_ = dn; }
48 int getRootDeltaN() const noexcept { return rootDeltaN_; }
49
50 void setRepeatEnabled(bool enabled) noexcept { isRepeated_ = enabled; }
51 bool isRepeatable() const noexcept { return isRepeated_; }
52 SampleRepeatFlag getRepeatFlag() const noexcept {
53 if (!isRepeated_) return SampleRepeatFlag::Disabled;
54
55 int flags = SampleRepeatFlag::ShouldRepeat;
56 if (repeatRange_.first() != 0) {
57 flags |= SampleRepeatFlag::ShouldRewriteStart;
58 }
59 if (repeatRange_.last() != sample_.size() - 1) {
60 flags |= SampleRepeatFlag::ShouldRewriteStop;
61 }
62 return static_cast<SampleRepeatFlag>(flags);
63 }
64 bool setRepeatRange(const SampleRepeatRange& range) noexcept;
65 SampleRepeatRange getRepeatRange() const noexcept { return repeatRange_; }
66
67 bool storeSample(const std::vector<uint8_t>& sample);
68 bool storeSample(std::vector<uint8_t>&& sample);
69 std::vector<uint8_t> getSamples() const { return sample_; }
70 void clearSample();
71 void setStartAddress(size_t addr) noexcept { startAddress_ = addr; }
72 size_t getStartAddress() const noexcept { return startAddress_; }
73 void setStopAddress(size_t addr) noexcept { stopAddress_ = addr; }
74 size_t getStopAddress() const noexcept { return stopAddress_; }
75
76 bool isEdited() const override;
77 void clearParameters() override;
78
79 static constexpr int DEF_ROOT_KEY = 60; // C5
80
81 static int calculateADPCMDeltaN(unsigned int rate)
82 {
83 return static_cast<int>(std::round((rate << 16) / 55500.));
84 }
85
86 private:
87 int rootKeyNum_, rootDeltaN_;
88 bool isRepeated_;
89 /// Range (first byte, last byte)
90 SampleRepeatRange repeatRange_;
91 std::vector<uint8_t> sample_;
92 size_t startAddress_, stopAddress_;
93 };