-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSparkPresetBuilder.cpp
538 lines (471 loc) · 18.1 KB
/
SparkPresetBuilder.cpp
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
* SparkDataControl.cpp
*
* Created on: 19.08.2021
* Author: stangreg
*/
#include "SparkPresetBuilder.h"
void SparkPresetBuilder::updatePresetListUUID(int bnk, int pre, string uuid) {
presetUUIDs[uuid] = make_pair(bnk, pre);
}
SparkPresetBuilder::SparkPresetBuilder() : presetBanksNames{} {
}
void SparkPresetBuilder::init() {
// removing for now until further investigation
// SPIFFS.begin(true);
// Creating vector of presets
Serial.println("Initializing PresetBuilder");
resetHWPresets();
initializePresetListFromFS();
}
Preset SparkPresetBuilder::getPresetFromJson(char *json) {
Preset resultPreset;
string jsonString(json);
JsonDocument jsonPreset;
DeserializationError err = deserializeJson(jsonPreset, json);
if (err) {
Serial.print(F("deserializeJson() failed with code "));
Serial.println(err.f_str());
}
// Preset number is not used currently
resultPreset.presetNumber = 0;
// myObject.hasOwnProperty(key) checks if the object contains an entry for key
// preset UUID
string presetUUID = jsonPreset["UUID"].as<string>();
resultPreset.uuid = presetUUID;
// preset NAME
string presetName = jsonPreset["Name"].as<string>();
resultPreset.name = presetName;
// preset VERSION
string presetVersion = jsonPreset["Version"].as<string>();
resultPreset.version = presetVersion;
// preset Description
string presetDescription = jsonPreset["Description"].as<string>();
resultPreset.description = presetDescription;
// preset Icon
string presetIcon = jsonPreset["Icon"].as<string>();
resultPreset.icon = presetIcon;
// preset BPM
float presetBpm = jsonPreset["BPM"].as<float>();
resultPreset.bpm = presetBpm;
// all pedals
JsonArray pedalArray = jsonPreset["Pedals"];
for (JsonObject currentJsonPedal : pedalArray) {
Pedal currentPedal;
currentPedal.name = currentJsonPedal["Name"].as<string>();
currentPedal.isOn = currentJsonPedal["IsOn"].as<bool>();
JsonArray pedalParamArray = currentJsonPedal["Parameters"];
int i = 0;
for (float currentJsonPedalParam : pedalParamArray) {
Parameter currentParam;
currentParam.number = i;
currentParam.special = 0x91;
currentParam.value = currentJsonPedalParam;
currentPedal.parameters.push_back(currentParam);
i++;
}
resultPreset.pedals.push_back(currentPedal);
}
// preset checksum
string presetChecksum = jsonPreset["Checksum"].as<string>();
if (presetChecksum == "null") {
Serial.println("Checksum not found, trying legacy format.");
presetChecksum = jsonPreset["Filler"].as<string>();
}
resultPreset.checksum = stoi(presetChecksum, 0, 16);
resultPreset.isEmpty = false;
resultPreset.json = jsonString;
// DEBUG_PRINTLN("JSON:");
// DEBUG_PRINTLN(resultPreset.json.c_str());
return resultPreset;
}
// string SparkPresetBuilder::getJsonFromPreset(preset pset){};
void SparkPresetBuilder::initializePresetListFromFS() {
presetUUIDs.clear();
Serial.println("Reading custom presets from filesystem.");
presetBanksNames.clear();
string allPresetsAsText;
vector<string> tmpVector;
bool createUUIDFile = false;
DEBUG_PRINTLN("Trying to read preset list file");
File file = LittleFS.open(presetListUUIDFileName);
if (!file) {
Serial.println("ERROR while trying to open presets list file");
createUUIDFile = true;
}
if (createUUIDFile) {
file = LittleFS.open(presetListFileName);
}
string fileContent;
while (file.available()) {
fileContent += file.read();
}
DEBUG_PRINTF("File read: %s\n", fileContent.c_str());
stringstream fileStream(fileContent);
file.close();
string line;
int bank = 1;
int preset = 1;
while (getline(fileStream, line)) {
line.erase(remove(line.begin(), line.end(), '\r'), line.end());
line.erase(remove(line.begin(), line.end(), '\n'), line.end());
// Lines starting with '-' and empty lines
// are ignored and can be used for comments in the file
if (line.rfind("-", 0) != 0 && !line.empty()) {
// store UUIDs into filename
string presetFilename;
string uuid;
stringstream lineStream = stringstream(line);
lineStream >> presetFilename;
if (!createUUIDFile) {
lineStream >> uuid;
presetUUIDs[uuid] = make_pair(bank, preset);
}
tmpVector.push_back(presetFilename);
preset++;
if (tmpVector.size() == PRESETS_PER_BANK) {
presetBanksNames.push_back(tmpVector);
tmpVector.clear();
bank++;
preset = 1;
}
}
}
if (tmpVector.size() > 0) {
while (tmpVector.size() < 4) {
Serial.println("Last bank not full, filling with last preset to get bank complete");
tmpVector.push_back(tmpVector.back());
}
presetBanksNames.push_back(tmpVector);
}
// initHWPresets();
if (createUUIDFile) {
buildPresetUUIDs();
}
}
void SparkPresetBuilder::initHWPresets() {
// Determine the number of HW presets and store
numberOfHWPresets_ = numberOfHWBanks_ * PRESETS_PER_BANK;
resetHWPresets();
// Read HWPresets from file, if present
for (int presetNum = 1; presetNum <= numberOfHWPresets_; presetNum++) {
string filename = "HWPreset" + to_string(presetNum) + ".json";
Preset hwPreset = readPresetFromFile(filename);
if (!(hwPreset.isEmpty)) {
hwPresets.at(presetNum - 1) = hwPreset;
string uuid = hwPreset.uuid;
updatePresetListUUID(0, presetNum, uuid);
}
}
}
void SparkPresetBuilder::buildPresetUUIDs() {
Serial.print("Building UUID file from scratch...");
File presetUUIDFile = LittleFS.open(presetListUUIDFileName, FILE_WRITE);
if (!presetUUIDFile) {
Serial.println("ERROR: Could not open preset UUID file");
}
for (int bnk = 1; bnk <= getNumberOfBanks(); bnk++) {
for (int pre = 1; pre <= PRESETS_PER_BANK; pre++) {
Preset tmpPreset = getPreset(bnk, pre);
string uuid = tmpPreset.uuid;
string presetName = presetBanksNames[bnk - 1][pre - 1];
string printLine = presetName + " " + uuid + "\n";
presetUUIDFile.print(printLine.c_str());
presetUUIDs[uuid] = make_pair(bnk, pre);
}
}
presetUUIDFile.close();
Serial.println("done.");
}
Preset SparkPresetBuilder::getPreset(int bank, int pre) {
Preset retPreset;
// HW preset
if (bank == 0) {
if (pre > hwPresets.size()) {
Serial.println("Requested HW preset out of bounds.");
return retPreset;
}
return hwPresets.at(pre - 1);
}
// Custom preset
if (bank > 0) {
if (pre > PRESETS_PER_BANK) {
Serial.println("Requested preset out of bounds.");
return retPreset;
}
if (bank > presetBanksNames.size()) {
Serial.println("Requested bank out of bounds.");
return retPreset;
}
string presetFilename = presetBanksNames[bank - 1][pre - 1];
retPreset = readPresetFromFile(presetFilename);
}
return retPreset;
}
pair<int, int> SparkPresetBuilder::getBankPresetNumFromUUID(string uuid) {
pair<int, int> result;
try {
result = presetUUIDs.at(uuid);
DEBUG_PRINTF("Found UUID %s as preset %d - %d\n", uuid.c_str(), std::get<0>(result), std::get<1>(result));
} catch (std::out_of_range exception) {
Serial.print("Preset not found.");
result = make_pair(0, 0);
}
return result;
}
const int SparkPresetBuilder::getNumberOfBanks() const {
return presetBanksNames.size();
}
int SparkPresetBuilder::storePreset(Preset newPreset, int bnk, int pre) {
string presetNamePrefix = newPreset.name;
string presetUUID = newPreset.uuid;
if (presetNamePrefix == "null" || presetNamePrefix.empty()) {
presetNamePrefix = "Preset";
}
string presetFileName = savePresetToFile(presetNamePrefix, newPreset);
// Then insert the preset into the right position
string filestrPreset = "";
string filestrPresetUUID = "";
string oldListFile;
int lineCount = 1;
int insertPosition = 4 * (bnk - 1) + pre;
File presetListFile;
File presetUUIDListFile;
presetUUIDListFile = LittleFS.open(presetListUUIDFileName);
if (!presetUUIDListFile) {
Serial.println("ERROR while trying to open presets list file");
return STORE_PRESET_ERROR_OPEN;
}
string fileContent;
while (presetUUIDListFile.available()) {
fileContent += presetUUIDListFile.read();
}
stringstream stream(fileContent);
string line;
while (getline(stream, line)) {
string preset;
string uuid;
stringstream(line) >> preset >> uuid;
if (lineCount != insertPosition) {
// Lines starting with '-' and empty lines
// are ignored and can be used for comments in the file
if (line.rfind("-", 0) != 0 && !line.empty()) {
if (((lineCount - 1) % 4) == 0) {
// New bank separator addd to file for better readability
char bank_string[20] = "";
int size = sizeof bank_string;
snprintf(bank_string, size, "%d ", ((lineCount - 1) / 4) + 1);
filestrPreset += "-- Bank ";
filestrPreset += bank_string;
filestrPreset += "\n";
}
lineCount++;
filestrPreset += preset + "\n";
filestrPresetUUID += preset + " " + uuid + "\n";
}
} else {
filestrPreset += presetFileName + "\n";
filestrPresetUUID += presetFileName + " " + presetUUID + "\n";
// Adding old line so it is not lost.
filestrPreset += preset + "\n";
filestrPresetUUID += preset + " " + uuid + "\n";
lineCount++;
}
}
presetListFile = LittleFS.open(presetListFileName, FILE_WRITE);
presetUUIDListFile = LittleFS.open(presetListUUIDFileName, FILE_WRITE);
if (!presetListFile || !presetUUIDListFile) {
Serial.println("ERROR while trying to open presets list files for writing.");
return STORE_PRESET_ERROR_OPEN;
}
bool success = presetListFile.print(filestrPreset.c_str()) && presetUUIDListFile.print(filestrPresetUUID.c_str());
presetListFile.close();
presetUUIDListFile.close();
if (success) {
Serial.printf("Successfully stored new preset to %d-%d\n", bnk, pre);
initializePresetListFromFS();
return STORE_PRESET_OK;
}
return STORE_PRESET_UNKNOWN_ERROR;
}
int SparkPresetBuilder::deletePreset(int bnk, int pre) {
// Remove the preset
string filestrPreset = "";
string filestrPresetUUID = "";
File presetListFile;
File presetListUUIDFile;
string presetFileToDelete = "";
int lineCount = 1;
int presetCount = 1;
int deletePosition = 4 * (bnk - 1) + pre;
presetListUUIDFile = LittleFS.open(presetListUUIDFileName);
if (!presetListUUIDFile) {
Serial.println("ERROR while trying to open presets list file");
return STORE_PRESET_ERROR_OPEN;
}
// Read file content into stream
string fileContent;
while (presetListUUIDFile.available()) {
fileContent += presetListUUIDFile.read();
}
presetListUUIDFile.close();
stringstream stream(fileContent);
string line;
DEBUG_PRINTF("DELETE - File content: %s\n", fileContent.c_str());
string preset;
string uuid;
while (getline(stream, line)) {
if (lineCount != deletePosition) {
// Lines starting with '-' and empty lines
// are ignored and can be used for comments in the file
if (line.rfind("-", 0) != 0 && !line.empty()) {
stringstream(line) >> preset >> uuid;
if (((lineCount - 1) % 4) == 0) {
// New bank separator added to file for better readability
char bank_string[20] = "";
int size = sizeof bank_string;
snprintf(bank_string, size, "%d ", ((lineCount - 1) / 4) + 1);
filestrPreset += "-- Bank ";
filestrPreset += bank_string;
filestrPreset += "\n";
}
filestrPreset += preset + "\n";
filestrPresetUUID += preset + " " + uuid + "\n";
lineCount++;
presetCount++;
}
} else {
// Just increase the line counter, so deleted line
// does not get into new content.
// presetCounter not increased to count presets properly
stringstream(line) >> preset >> uuid;
presetFileToDelete = "/" + preset;
lineCount++;
}
}
presetListFile = LittleFS.open(presetListFileName, FILE_WRITE);
presetListUUIDFile = LittleFS.open(presetListUUIDFileName, FILE_WRITE);
if (!presetListFile || !presetListUUIDFile) {
Serial.println("ERROR opening preset files for writing.");
return STORE_PRESET_ERROR_OPEN;
}
bool success = presetListFile.print(filestrPreset.c_str()) && presetListUUIDFile.print(filestrPresetUUID.c_str());
presetListFile.close();
presetListUUIDFile.close();
if (success) {
initializePresetListFromFS();
if (LittleFS.remove(presetFileToDelete.c_str())) {
return DELETE_PRESET_OK;
} else {
return DELETE_PRESET_FILE_NOT_EXIST;
}
}
return DELETE_PRESET_UNKNOWN_ERROR;
}
void SparkPresetBuilder::insertHWPreset(int number, const Preset &preset) {
if (number < 0 || number > numberOfHWPresets_ - 1) {
Serial.println("ERROR: HW Preset not inserted, preset number out of bounds.");
return;
}
hwPresets.at(number) = preset;
string filename = "HWPreset" + to_string(number + 1);
savePresetToFile(filename, preset, true);
string uuid = preset.uuid;
updatePresetListUUID(0, number + 1, uuid);
}
string SparkPresetBuilder::savePresetToFile(string filename, const Preset &preset, bool overwrite) {
Serial.println("Saving preset:");
Serial.println(preset.json.c_str());
string presetNameWithPath;
// remove any blanks from the name for a new filename
filename.erase(remove_if(filename.begin(),
filename.end(),
[](char chr) {
return not(regex_match(string(1, chr), regex("[A-z0-9_]")));
}),
filename.end());
// cut down name to 24 characters (a potential counter + .json will then increase to 30);
const int nameLength = filename.length();
filename.resize(min(24, nameLength));
string presetFileName = filename + ".json";
Serial.printf("Store preset with filename %s\n", presetFileName.c_str());
int counter = 0;
presetFileName = "/" + presetFileName;
File presetFile = LittleFS.open(presetFileName.c_str());
if (!overwrite) {
while (presetFile && presetFile.size() != 0) {
counter++;
Serial.printf("ERROR: File '%s' already exists! Saving as copy.\n", presetFileName.c_str());
char counterStr[2];
int size = sizeof counterStr;
snprintf(counterStr, size, "%d", counter);
presetFileName = filename + counterStr + ".json";
presetFile.close();
presetFile = LittleFS.open(presetFileName.c_str());
}
}
presetFile.close();
presetFile = LittleFS.open(presetFileName.c_str(), FILE_WRITE);
// Store the json string to a new file
presetFile.print(preset.json.c_str());
presetFile.close();
return presetFileName;
}
Preset SparkPresetBuilder::readPresetFromFile(string filename) {
Preset retPreset;
string presetJsonString;
string fullFilename = "/" + filename;
DEBUG_PRINTF("Trying to read preset %s...", fullFilename.c_str());
File file = LittleFS.open(fullFilename.c_str());
if (file) {
while (file.available()) {
presetJsonString += (char)file.read();
}
file.close();
retPreset = getPresetFromJson(&presetJsonString[0]);
DEBUG_PRINTLN("done.");
DEBUG_PRINTF("Read preset: %s\n", retPreset.json.c_str());
return retPreset;
} else {
Serial.printf("Error while opening file %s, returning empty preset.\n", fullFilename.c_str());
return retPreset;
}
}
void SparkPresetBuilder::resetHWPresets() {
hwPresets.clear();
Preset examplePreset;
for (int i = 0; i < numberOfHWPresets_; i++) {
hwPresets.push_back(examplePreset);
}
}
void SparkPresetBuilder::validateChecksums(vector<byte> checksums) {
// TODO: Once all 8 Spark 2 HW presets are supported, this needs to be adjusted
if (hwPresets.size() < numberOfHWPresets_ || checksums.size() < numberOfHWPresets_) {
Serial.printf("ERROR: Vector HW Presets (size: %d) or Checksums (size: %d) not in the expected size (%d).\n", hwPresets.size(), checksums.size(), numberOfHWPresets_);
return;
}
bool success = true;
// Compare checksums of stored HW presets with received checksums
for (int presetNum = 0; presetNum < numberOfHWPresets_; presetNum++) {
byte presetChk = hwPresets.at(presetNum).checksum;
byte check = (byte)checksums.at(presetNum);
if (presetChk != check) {
Serial.printf("HW checksum for preset %d changed (Cache: %02x / Amp: %02x), invalidating cache.\n", presetNum + 1, presetChk, check);
Preset emptyPreset;
hwPresets.at(presetNum) = emptyPreset;
success = false;
}
}
if (success) {
Serial.println("Cached HW presets are valid.");
}
}
bool SparkPresetBuilder::isHWPresetMissing(int num) {
if (num < 1 || num > numberOfHWPresets_) {
return false;
}
if (hwPresets.at(num - 1).isEmpty) {
return true;
}
return false;
}