-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreset_manager.hpp
More file actions
337 lines (287 loc) · 11.8 KB
/
Copy pathpreset_manager.hpp
File metadata and controls
337 lines (287 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#pragma once
#include <juce_audio_processors/juce_audio_processors.h>
#include "update_data.hpp"
namespace pluginshared {
/**
* @note must create after building AudioProcessorValueTreeState
*/
class PresetManager : juce::ValueTree::Listener {
public:
inline static const juce::File defaultDirectory{
juce::File::getSpecialLocation(juce::File::SpecialLocationType::userDocumentsDirectory)
.getChildFile(JucePlugin_Manufacturer)
.getChildFile(JucePlugin_Name)};
inline static const juce::String extension{"xml"};
inline static const juce::String presetNameProperty{"presetName"};
inline static const juce::String kVersionProperty{"version"};
inline static const juce::String kDefaultPresetName = "default";
inline static const juce::String kFactoryPresetAutoNamePrefix = "Factory ";
enum class PresetScope {
kDefault,
kFactory,
kUser,
};
PresetManager(juce::AudioProcessorValueTreeState& apvts, juce::AudioProcessor& p, UpdateData::GithubInfo info)
: valueTreeState(apvts)
, processor_(p)
, update_data_(info) {
// Create a default Preset Directory, if it doesn't exist
if (!defaultDirectory.exists()) {
const auto result = defaultDirectory.createDirectory();
if (result.failed()) {
DBG("Could not create preset directory: " + result.getErrorMessage());
}
}
// Migrate old .preset files to .xml
const auto oldFiles =
defaultDirectory.findChildFiles(juce::File::TypesOfFileToFind::findFiles, false, "*.preset");
for (const auto& f : oldFiles) {
const auto newFile = f.getParentDirectory().getChildFile(f.getFileNameWithoutExtension() + "." + extension);
f.moveFileTo(newFile);
}
apvts.state.setProperty(presetNameProperty, kDefaultPresetName, nullptr);
apvts.state.setProperty(kVersionProperty, JucePlugin_VersionString, nullptr);
valueTreeState.state.addListener(this);
currentPreset.referTo(valueTreeState.state.getPropertyAsValue(presetNameProperty, nullptr));
p.getCurrentProgramStateInformation(default_state_block_);
}
void AddFactoryPreset(const char* xml, int xml_size, const juce::String& name) {
factory_presets_.push_back({name, xml, xml_size});
}
void savePreset(const juce::String& presetName) {
if (presetName.isEmpty() || presetName == kDefaultPresetName) return;
currentPreset.setValue(presetName);
juce::MemoryBlock block;
processor_.getStateInformation(block);
const auto newFile = defaultDirectory.getChildFile(presetName + "." + extension);
if (newFile.existsAsFile()) {
newFile.deleteFile();
}
juce::FileOutputStream stream{newFile};
if (!stream.write(block.getData(), block.getSize())) {
DBG("Could not create preset file: " + newFile.getFullPathName());
}
current_scope_ = PresetScope::kUser;
current_user_preset_index_ = getUserPresetNames().indexOf(presetName);
current_factory_preset_index_ = -1;
}
void deletePreset(const juce::String& presetName) {
if (presetName.isEmpty() || presetName == kDefaultPresetName) return;
const auto presetFile = defaultDirectory.getChildFile(presetName + "." + extension);
if (!presetFile.existsAsFile()) {
DBG("Preset file does not exist for: " + presetName);
return;
}
if (!presetFile.deleteFile()) {
DBG("Preset file " + presetFile.getFullPathName() + " could not be deleted");
return;
}
currentPreset.setValue("*deleted*");
current_scope_ = PresetScope::kDefault;
current_factory_preset_index_ = -1;
current_user_preset_index_ = -1;
}
void loadPreset(const juce::String& presetName) {
if (presetName.isEmpty()) {
return;
}
const auto presetFile = defaultDirectory.getChildFile(presetName + "." + extension);
if (!presetFile.existsAsFile()) {
DBG("Preset file does not exist for: " + presetName);
return;
}
juce::FileInputStream c{presetFile};
if (c.failedToOpen()) {
return;
}
juce::MemoryBlock block;
c.readIntoMemoryBlock(block);
processor_.setStateInformation(block.getData(), static_cast<int>(block.getSize()));
currentPreset.setValue(presetName);
current_scope_ = PresetScope::kUser;
current_user_preset_index_ = getUserPresetNames().indexOf(presetName);
current_factory_preset_index_ = -1;
}
bool loadUserPresetByIndex(int index) {
const auto userPresets = getUserPresetNames();
if (index < 0 || index >= userPresets.size()) {
return false;
}
loadPreset(userPresets[index]);
current_scope_ = PresetScope::kUser;
current_user_preset_index_ = index;
current_factory_preset_index_ = -1;
return true;
}
bool loadFactoryPresetByIndex(int index) {
if (index < 0 || index >= static_cast<int>(factory_presets_.size())) {
return false;
}
const auto& preset = factory_presets_[static_cast<size_t>(index)];
processor_.setStateInformation(preset.xml, preset.xml_size);
const auto& name = preset.name;
currentPreset.setValue(name);
current_scope_ = PresetScope::kFactory;
current_factory_preset_index_ = index;
current_user_preset_index_ = -1;
return true;
}
std::pair<int, juce::String> loadNextPreset() {
if (current_scope_ == PresetScope::kFactory) {
return loadNextFactoryPreset();
}
if (current_scope_ == PresetScope::kUser) {
return loadNextUserPreset();
}
if (!factory_presets_.empty() && loadFactoryPresetByIndex(0)) {
return {0, getCurrentPreset()};
}
const auto userPresets = getUserPresetNames();
if (!userPresets.isEmpty() && loadUserPresetByIndex(0)) {
return {0, getCurrentPreset()};
}
return {-1, getCurrentPreset()};
}
std::pair<int, juce::String> loadPreviousPreset() {
if (current_scope_ == PresetScope::kFactory) {
return loadPreviousFactoryPreset();
}
if (current_scope_ == PresetScope::kUser) {
return loadPreviousUserPreset();
}
if (!factory_presets_.empty() && loadFactoryPresetByIndex(0)) {
return {0, getCurrentPreset()};
}
const auto userPresets = getUserPresetNames();
if (!userPresets.isEmpty() && loadUserPresetByIndex(0)) {
return {0, getCurrentPreset()};
}
return {-1, getCurrentPreset()};
}
juce::StringArray getFactoryPresetNames() const {
juce::StringArray presets;
for (const auto& preset : factory_presets_) {
presets.add(preset.name);
}
return presets;
}
juce::StringArray getUserPresetNames() const {
return getAllPresets();
}
juce::StringArray getAllPresets() const {
juce::StringArray presets;
const auto fileArray =
defaultDirectory.findChildFiles(juce::File::TypesOfFileToFind::findFiles, false, "*." + extension);
for (const auto& file : fileArray) {
presets.add(file.getFileNameWithoutExtension());
}
return presets;
}
juce::String getCurrentPreset() const {
return currentPreset.toString();
}
void loadDefaultPatch() {
processor_.setStateInformation(default_state_block_.getData(),
static_cast<int>(default_state_block_.getSize()));
currentPreset.setValue(kDefaultPresetName);
current_scope_ = PresetScope::kDefault;
current_factory_preset_index_ = -1;
current_user_preset_index_ = -1;
if (external_load_default_operations) {
external_load_default_operations();
}
}
UpdateData& GetUpdateData() {
return update_data_;
}
/**
* @brief make audio processor goes into default state, value tree is automatic done
* @note when call this, the processor will automatic suspend
*/
std::function<void()> external_load_default_operations;
private:
std::pair<int, juce::String> loadNextFactoryPreset() {
if (factory_presets_.empty()) {
return {-1, getCurrentPreset()};
}
int currentIndex = current_factory_preset_index_;
if (currentIndex < 0 || currentIndex >= static_cast<int>(factory_presets_.size())) {
currentIndex = -1;
}
const int nextIndex = (currentIndex + 1) % static_cast<int>(factory_presets_.size());
if (loadFactoryPresetByIndex(nextIndex)) {
return {nextIndex, getCurrentPreset()};
}
return {-1, getCurrentPreset()};
}
std::pair<int, juce::String> loadPreviousFactoryPreset() {
if (factory_presets_.empty()) {
return {-1, getCurrentPreset()};
}
int currentIndex = current_factory_preset_index_;
if (currentIndex < 0 || currentIndex >= static_cast<int>(factory_presets_.size())) {
currentIndex = 0;
}
const int size = static_cast<int>(factory_presets_.size());
const int previousIndex = (currentIndex - 1 + size) % size;
if (loadFactoryPresetByIndex(previousIndex)) {
return {previousIndex, getCurrentPreset()};
}
return {-1, getCurrentPreset()};
}
std::pair<int, juce::String> loadNextUserPreset() {
const auto userPresets = getUserPresetNames();
if (userPresets.isEmpty()) {
return {-1, getCurrentPreset()};
}
int currentIndex = current_user_preset_index_;
if (currentIndex < 0 || currentIndex >= userPresets.size()) {
currentIndex = userPresets.indexOf(currentPreset.toString());
if (currentIndex < 0) currentIndex = -1;
}
const int nextIndex = (currentIndex + 1) % userPresets.size();
if (loadUserPresetByIndex(nextIndex)) {
return {nextIndex, getCurrentPreset()};
}
return {-1, getCurrentPreset()};
}
std::pair<int, juce::String> loadPreviousUserPreset() {
const auto userPresets = getUserPresetNames();
if (userPresets.isEmpty()) {
return {-1, getCurrentPreset()};
}
int currentIndex = current_user_preset_index_;
if (currentIndex < 0 || currentIndex >= userPresets.size()) {
currentIndex = userPresets.indexOf(currentPreset.toString());
if (currentIndex < 0) currentIndex = 0;
}
const int size = userPresets.size();
const int previousIndex = (currentIndex - 1 + size) % size;
if (loadUserPresetByIndex(previousIndex)) {
return {previousIndex, getCurrentPreset()};
}
return {-1, getCurrentPreset()};
}
void valueTreeRedirected(juce::ValueTree& treeWhichHasBeenChanged) override {
currentPreset.referTo(treeWhichHasBeenChanged.getPropertyAsValue(presetNameProperty, nullptr));
current_scope_ = PresetScope::kDefault;
current_factory_preset_index_ = -1;
current_user_preset_index_ = -1;
}
juce::AudioProcessorValueTreeState& valueTreeState;
juce::MemoryBlock default_state_block_;
juce::AudioProcessor& processor_;
juce::Value currentPreset;
UpdateData update_data_;
struct FactoryPreset {
juce::String name;
const char* xml;
int xml_size;
};
std::vector<FactoryPreset> factory_presets_;
int current_factory_preset_index_{-1};
int current_user_preset_index_{-1};
PresetScope current_scope_{PresetScope::kDefault};
friend class PresetPanel;
};
} // namespace pluginshared