Debian Codegraph @

BambooTracker/instrument/instrument.hpp

raw
1 /*
2 * Copyright (C) 2018-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 <string>
29 #include <memory>
30 #include <unordered_map>
31 #include <vector>
32 #include "envelope_fm.hpp"
33 #include "lfo_fm.hpp"
34 #include "sequence_property.hpp"
35 #include "sample_repeat.hpp"
36 #include "instrument_property_defs.hpp"
37 #include "enum_hash.hpp"
38 #include "bamboo_tracker_defs.hpp"
39
40 class InstrumentsManager;
41
42 enum class InstrumentType : int { FM, SSG, ADPCM, Drumkit };
43
44 class AbstractInstrument
45 {
46 public:
47 virtual ~AbstractInstrument() = default;
48
49 inline int getNumber() const noexcept { return number_; }
50 inline void setNumber(int n) noexcept { number_ = n; }
51 inline SoundSource getSoundSource() const noexcept { return sndSrc_; }
52 inline InstrumentType getType() const noexcept { return instType_; }
53 inline std::string getName() const noexcept { return name_; }
54 inline void setName(const std::string& name) { name_ = name; }
55 bool isRegisteredWithManager() const;
56 virtual AbstractInstrument* clone() = 0;
57
58 protected:
59 InstrumentsManager* owner_;
60 int number_;
61 std::string name_; // UTF-8
62 AbstractInstrument(int number, SoundSource src, InstrumentType type, const std::string& name, InstrumentsManager* owner);
63
64 private:
65 const SoundSource sndSrc_;
66 const InstrumentType instType_;
67 };
68
69
70 class InstrumentFM final : public AbstractInstrument
71 {
72 public:
73 InstrumentFM(int number, const std::string& name, InstrumentsManager* owner);
74 AbstractInstrument* clone() override;
75
76 inline void setEnvelopeNumber(int n) noexcept { envNum_ = n; }
77 inline int getEnvelopeNumber() const noexcept { return envNum_; }
78 int getEnvelopeParameter(FMEnvelopeParameter param) const;
79 bool getOperatorEnabled(int n) const;
80
81 inline void setLFOEnabled(bool enabled) noexcept { lfoEnabled_ = enabled; }
82 inline bool getLFOEnabled() const noexcept { return lfoEnabled_; }
83 inline void setLFONumber(int n) noexcept { lfoNum_ = n; }
84 inline int getLFONumber() const noexcept { return lfoNum_; }
85 int getLFOParameter(FMLFOParameter param) const;
86
87 void setOperatorSequenceEnabled(FMEnvelopeParameter param, bool enabled);
88 bool getOperatorSequenceEnabled(FMEnvelopeParameter param) const;
89 void setOperatorSequenceNumber(FMEnvelopeParameter param, int n);
90 int getOperatorSequenceNumber(FMEnvelopeParameter param) const;
91 std::vector<FMOperatorSequenceUnit> getOperatorSequenceSequence(FMEnvelopeParameter param) const;
92 InstrumentSequenceLoopRoot getOperatorSequenceLoopRoot(FMEnvelopeParameter param) const;
93 InstrumentSequenceRelease getOperatorSequenceRelease(FMEnvelopeParameter param) const;
94 FMOperatorSequenceIter getOperatorSequenceSequenceIterator(FMEnvelopeParameter param) const;
95
96 void setArpeggioEnabled(FMOperatorType op, bool enabled);
97 bool getArpeggioEnabled(FMOperatorType op) const;
98 void setArpeggioNumber(FMOperatorType op, int n);
99 int getArpeggioNumber(FMOperatorType op) const;
100 SequenceType getArpeggioType(FMOperatorType op) const;
101 std::vector<ArpeggioUnit> getArpeggioSequence(FMOperatorType op) const;
102 InstrumentSequenceLoopRoot getArpeggioLoopRoot(FMOperatorType op) const;
103 InstrumentSequenceRelease getArpeggioRelease(FMOperatorType op) const;
104 ArpeggioIter getArpeggioSequenceIterator(FMOperatorType op) const;
105
106 void setPitchEnabled(FMOperatorType op, bool enabled);
107 bool getPitchEnabled(FMOperatorType op) const;
108 void setPitchNumber(FMOperatorType op, int n);
109 int getPitchNumber(FMOperatorType op) const;
110 SequenceType getPitchType(FMOperatorType op) const;
111 std::vector<PitchUnit> getPitchSequence(FMOperatorType op) const;
112 InstrumentSequenceLoopRoot getPitchLoopRoot(FMOperatorType op) const;
113 InstrumentSequenceRelease getPitchRelease(FMOperatorType op) const;
114 PitchIter getPitchSequenceIterator(FMOperatorType op) const;
115
116 void setPanEnabled(bool enabled);
117 bool getPanEnabled() const;
118 void setPanNumber(int n);
119 int getPanNumber() const;
120 std::vector<PanUnit> getPanSequence() const;
121 InstrumentSequenceLoopRoot getPanLoopRoot() const;
122 InstrumentSequenceRelease getPanRelease() const;
123 PanIter getPanSequenceIterator() const;
124
125 void setEnvelopeResetEnabled(FMOperatorType op, bool enabled);
126 bool getEnvelopeResetEnabled(FMOperatorType op) const;
127
128 private:
129 int envNum_;
130 bool lfoEnabled_;
131 int lfoNum_;
132 std::unordered_map<FMEnvelopeParameter, bool> opSeqEnabled_;
133 std::unordered_map<FMEnvelopeParameter, int> opSeqNum_;
134 std::unordered_map<FMOperatorType, bool> arpEnabled_;
135 std::unordered_map<FMOperatorType, int> arpNum_;
136 std::unordered_map<FMOperatorType, bool> ptEnabled_;
137 std::unordered_map<FMOperatorType, int> ptNum_;
138 bool panEnabled_;
139 int panNum_;
140
141 std::unordered_map<FMOperatorType, bool> envResetEnabled_;
142 };
143
144
145 class InstrumentSSG final : public AbstractInstrument
146 {
147 public:
148 InstrumentSSG(int number, const std::string& name, InstrumentsManager* owner);
149 AbstractInstrument* clone() override;
150
151 inline void setWaveformEnabled(bool enabled) noexcept { wfEnabled_ = enabled; }
152 inline bool getWaveformEnabled() const noexcept { return wfEnabled_; }
153 inline void setWaveformNumber(int n) noexcept { wfNum_ = n; }
154 inline int getWaveformNumber() const noexcept { return wfNum_; }
155 std::vector<SSGWaveformUnit> getWaveformSequence() const;
156 InstrumentSequenceLoopRoot getWaveformLoopRoot() const;
157 InstrumentSequenceRelease getWaveformRelease() const;
158 SSGWaveformIter getWaveformSequenceIterator() const;
159
160 inline void setToneNoiseEnabled(bool enabled) noexcept { tnEnabled_ = enabled; }
161 inline bool getToneNoiseEnabled() const noexcept { return tnEnabled_; }
162 inline void setToneNoiseNumber(int n) noexcept { tnNum_ = n; }
163 inline int getToneNoiseNumber() const noexcept { return tnNum_; }
164 std::vector<SSGToneNoiseUnit> getToneNoiseSequence() const;
165 InstrumentSequenceLoopRoot getToneNoiseLoopRoot() const;
166 InstrumentSequenceRelease getToneNoiseRelease() const;
167 SSGToneNoiseIter getToneNoiseSequenceIterator() const;
168
169 inline void setEnvelopeEnabled(bool enabled) noexcept { envEnabled_ = enabled; }
170 inline bool getEnvelopeEnabled() const noexcept { return envEnabled_; }
171 inline void setEnvelopeNumber(int n) noexcept { envNum_ = n; }
172 inline int getEnvelopeNumber() const noexcept { return envNum_; }
173 std::vector<SSGEnvelopeUnit> getEnvelopeSequence() const;
174 InstrumentSequenceLoopRoot getEnvelopeLoopRoot() const;
175 InstrumentSequenceRelease getEnvelopeRelease() const;
176 SSGEnvelopeIter getEnvelopeSequenceIterator() const;
177
178 inline void setArpeggioEnabled(bool enabled) noexcept { arpEnabled_ = enabled; }
179 inline bool getArpeggioEnabled() const noexcept { return arpEnabled_; }
180 inline void setArpeggioNumber(int n) noexcept { arpNum_ = n; }
181 inline int getArpeggioNumber() const noexcept { return arpNum_; }
182 SequenceType getArpeggioType() const;
183 std::vector<ArpeggioUnit> getArpeggioSequence() const;
184 InstrumentSequenceLoopRoot getArpeggioLoopRoot() const;
185 InstrumentSequenceRelease getArpeggioRelease() const;
186 ArpeggioIter getArpeggioSequenceIterator() const;
187
188 inline void setPitchEnabled(bool enabled) noexcept { ptEnabled_ = enabled; }
189 inline bool getPitchEnabled() const noexcept { return ptEnabled_; }
190 inline void setPitchNumber(int n) noexcept { ptNum_ = n; }
191 inline int getPitchNumber() const noexcept { return ptNum_; }
192 SequenceType getPitchType() const;
193 std::vector<PitchUnit> getPitchSequence() const;
194 InstrumentSequenceLoopRoot getPitchLoopRoot() const;
195 InstrumentSequenceRelease getPitchRelease() const;
196 PitchIter getPitchSequenceIterator() const;
197
198 private:
199 bool wfEnabled_;
200 int wfNum_;
201 bool tnEnabled_;
202 int tnNum_;
203 bool envEnabled_;
204 int envNum_;
205 bool arpEnabled_;
206 int arpNum_;
207 bool ptEnabled_;
208 int ptNum_;
209 };
210
211
212 class InstrumentADPCM final : public AbstractInstrument
213 {
214 public:
215 InstrumentADPCM(int number, const std::string& name, InstrumentsManager* owner);
216 AbstractInstrument* clone() override;
217
218 inline void setSampleNumber(int n) noexcept { sampNum_ = n; }
219 inline int getSampleNumber() const noexcept { return sampNum_; }
220 int getSampleRootKeyNumber() const;
221 int getSampleRootDeltaN() const;
222 bool isSampleRepeatable() const;
223 SampleRepeatFlag getSampleRepeatFlag() const;
224 SampleRepeatRange getSampleRepeatRange() const;
225 std::vector<uint8_t> getRawSample() const;
226 size_t getSampleStartAddress() const;
227 size_t getSampleStopAddress() const;
228
229 inline void setEnvelopeEnabled(bool enabled) noexcept { envEnabled_ = enabled; }
230 inline bool getEnvelopeEnabled() const noexcept { return envEnabled_; }
231 inline void setEnvelopeNumber(int n) noexcept { envNum_ = n; }
232 inline int getEnvelopeNumber() const noexcept { return envNum_; }
233 std::vector<ADPCMEnvelopeUnit> getEnvelopeSequence() const;
234 InstrumentSequenceLoopRoot getEnvelopeLoopRoot() const;
235 InstrumentSequenceRelease getEnvelopeRelease() const;
236 ADPCMEnvelopeIter getEnvelopeSequenceIterator() const;
237
238 inline void setArpeggioEnabled(bool enabled) noexcept { arpEnabled_ = enabled; }
239 inline bool getArpeggioEnabled() const noexcept { return arpEnabled_; }
240 inline void setArpeggioNumber(int n) noexcept { arpNum_ = n; }
241 inline int getArpeggioNumber() const noexcept { return arpNum_; }
242 SequenceType getArpeggioType() const;
243 std::vector<ArpeggioUnit> getArpeggioSequence() const;
244 InstrumentSequenceLoopRoot getArpeggioLoopRoot() const;
245 InstrumentSequenceRelease getArpeggioRelease() const;
246 ArpeggioIter getArpeggioSequenceIterator() const;
247
248 inline void setPitchEnabled(bool enabled) noexcept { ptEnabled_ = enabled; }
249 inline bool getPitchEnabled() const noexcept { return ptEnabled_; }
250 inline void setPitchNumber(int n) noexcept { ptNum_ = n; }
251 inline int getPitchNumber() const noexcept { return ptNum_; }
252 SequenceType getPitchType() const;
253 std::vector<PitchUnit> getPitchSequence() const;
254 InstrumentSequenceLoopRoot getPitchLoopRoot() const;
255 InstrumentSequenceRelease getPitchRelease() const;
256 PitchIter getPitchSequenceIterator() const;
257
258 inline void setPanEnabled(bool enabled) noexcept { panEnabled_ = enabled; }
259 inline bool getPanEnabled() const noexcept { return panEnabled_; }
260 inline void setPanNumber(int n) noexcept { panNum_ = n; }
261 inline int getPanNumber() const noexcept { return panNum_; }
262 std::vector<PanUnit> getPanSequence() const;
263 InstrumentSequenceLoopRoot getPanLoopRoot() const;
264 InstrumentSequenceRelease getPanRelease() const;
265 PanIter getPanSequenceIterator() const;
266
267 private:
268 int sampNum_;
269 bool envEnabled_;
270 int envNum_;
271 bool arpEnabled_;
272 int arpNum_;
273 bool ptEnabled_;
274 int ptNum_;
275 bool panEnabled_;
276 int panNum_;
277 };
278
279
280 class InstrumentDrumkit final : public AbstractInstrument
281 {
282 public:
283 InstrumentDrumkit(int number, const std::string& name, InstrumentsManager* owner);
284 AbstractInstrument* clone() override;
285
286 std::vector<int> getAssignedKeys() const;
287
288 void setSampleEnabled(int key, bool enabled);
289 bool getSampleEnabled(int key) const;
290 void setSampleNumber(int key, int n);
291 int getSampleNumber(int key) const;
292 int getSampleRootKeyNumber(int key) const;
293 int getSampleRootDeltaN(int key) const;
294 bool isSampleRepeatable(int key) const;
295 SampleRepeatFlag getSampleRepeatFlag(int key) const;
296 SampleRepeatRange getSampleRepeatRange(int key) const;
297 std::vector<uint8_t> getRawSample(int key) const;
298 size_t getSampleStartAddress(int key) const;
299 size_t getSampleStopAddress(int key) const;
300
301 void setPitch(int key, int pitch);
302 int getPitch(int key) const;
303
304 void setPan(int key, int pan);
305 int getPan(int key) const;
306
307 private:
308 struct KitProperty { int sampNum, pitch, pan; };
309 std::unordered_map<int, KitProperty> kit_;
310 };