Deluge Firmware 1.3.0
Build date: 2026.06.14
Loading...
Searching...
No Matches
storage_manager.h
1/*
2 * Copyright © 2014-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include "definitions_cxx.hpp"
21#include "extern.h"
22#include "fatfs/fatfs.hpp"
23#include "model/sync.h"
24#include "util/firmware_version.h"
25
26#include <cstdint>
27#include <optional>
28
29extern "C" {
30#include "fatfs/ff.h"
31}
32
33extern void deleteOldSongBeforeLoadingNew();
34
35extern FatFS::Filesystem fileSystem;
36
37class Instrument;
38class PlaybackMode;
41class Song;
42class InstrumentClip;
43class Drum;
44class String;
46class ParamManager;
47class SoundDrum;
48class FileItem;
49class MIDIInstrument;
50
51class SMSharedData {};
52
53class FileReader {
54public:
55 FileReader();
56 FileReader(char* memBuffer, uint32_t bufLen);
57 virtual ~FileReader();
58
59 FIL readFIL;
60 char* fileClusterBuffer;
61 UINT currentReadBufferEndPos;
62 int32_t fileReadBufferCurrentPos;
63
64 FRESULT closeWriter();
65
66 bool peekChar(char* thisChar);
67 bool readChar(char* thisChar);
68 uint32_t bytesRemainingInBuffer() { return currentReadBufferEndPos - fileReadBufferCurrentPos; }
69 char* GetCurrentAddressInBuffer() { return fileClusterBuffer + fileReadBufferCurrentPos; }
70
71protected:
72 bool callRoutines = true;
73 bool readFileCluster();
74 bool readFileClusterIfNecessary();
75
76 void readDone();
77
78 bool memoryBased = false;
79 int32_t readCount; // Used for multitask interleaving.
80 bool reachedBufferEnd;
81 void resetReader();
82};
83
84class FileWriter {
85public:
86 FIL writeFIL;
87 FileWriter();
88 FileWriter(bool inMem);
89
90 virtual ~FileWriter();
91
92 Error closeAfterWriting(char const* path, char const* beginningString, char const* endString);
93
94 void writeByte(int8_t b);
95 void writeBlock(uint8_t* block, uint32_t size);
96 void writeChars(char const* output);
97 FRESULT closeWriter();
98
99 char* getBufferPtr() { return writeClusterBuffer; }
100 int32_t bytesWritten();
101 void setMemoryBased() {
102 memoryBased = true;
103 callRoutines = false;
104 }
105
106protected:
107 void resetWriter();
108 Error writeBufferToFile();
109 bool memoryBased = false;
110 bool callRoutines = true;
111 uint8_t indentAmount;
112 char* writeClusterBuffer;
113 uint32_t bufferSize;
114 int32_t fileWriteBufferCurrentPos;
115 int32_t fileTotalBytesWritten;
116 bool fileAccessFailedDuringWrite;
117};
118
120public:
121 virtual void writeAttribute(char const* name, int32_t number, bool onNewLine = true) = 0;
122 virtual void writeAttribute(char const* name, char const* value, bool onNewLine = true) = 0;
123 virtual void writeAttributeHex(char const* name, int32_t number, int32_t numChars, bool onNewLine = true) = 0;
124 virtual void writeAttributeHexBytes(char const* name, uint8_t* data, int32_t numBytes, bool onNewLine = true) = 0;
125 virtual void writeTagNameAndSeperator(char const* tag) = 0;
126 virtual void writeTag(char const* tag, int32_t number, bool box = false) = 0;
127 virtual void writeTag(char const* tag, char const* contents, bool box = false, bool quote = true) = 0;
128 virtual void writeOpeningTag(char const* tag, bool startNewLineAfter = true, bool box = false) = 0;
129 virtual void writeOpeningTagBeginning(char const* tag, bool box = false, bool newLineBefore = true) = 0;
130 virtual void writeOpeningTagEnd(bool startNewLineAfter = true) = 0;
131 virtual void closeTag(bool box = false) = 0;
132 virtual void writeClosingTag(char const* tag, bool shouldPrintIndents = true, bool box = false) = 0;
133 virtual void writeArrayStart(char const* tag, bool startNewLineAfter = true, bool box = false) = 0;
134 virtual void writeArrayEnding(char const* tag, bool shouldPrintIndents = true, bool box = false) = 0;
135 virtual void printIndents() = 0;
136 virtual void insertCommaIfNeeded() = 0;
137 virtual void write(char const* output) = 0;
138 virtual Error closeFileAfterWriting(char const* path = nullptr, char const* beginningString = nullptr,
139 char const* endString = nullptr) = 0;
140
141 virtual void reset() = 0;
142 void writeFirmwareVersion();
143
144 void writeEarliestCompatibleFirmwareVersion(char const* versionString) {
145 writeAttribute("earliestCompatibleFirmware", versionString);
146 }
147
148 void writeSyncTypeToFile(Song* song, char const* name, SyncType value, bool onNewLine) {
149 writeAttribute(name, (int32_t)value, onNewLine);
150 }
151
152 void writeAbsoluteSyncLevelToFile(Song* song, char const* name, SyncLevel internalValue, bool onNewLine);
153};
154
155class XMLSerializer : public Serializer, public FileWriter {
156public:
157 XMLSerializer();
158 ~XMLSerializer() override = default;
159
160 void writeAttribute(char const* name, int32_t number, bool onNewLine = true) override;
161 void writeAttribute(char const* name, char const* value, bool onNewLine = true) override;
162 void writeAttributeHex(char const* name, int32_t number, int32_t numChars, bool onNewLine = true) override;
163 void writeAttributeHexBytes(char const* name, uint8_t* data, int32_t numBytes, bool onNewLine = true) override;
164 void writeTagNameAndSeperator(char const* tag) override;
165 void writeTag(char const* tag, int32_t number, bool box = false) override;
166 void writeTag(char const* tag, char const* contents, bool box = false, bool quote = true) override;
167 void writeOpeningTag(char const* tag, bool startNewLineAfter = true, bool box = false) override;
168 void writeOpeningTagBeginning(char const* tag, bool box = false, bool newLineBefore = true) override;
169 void writeOpeningTagEnd(bool startNewLineAfter = true) override;
170 void closeTag(bool box = false) override;
171 void writeClosingTag(char const* tag, bool shouldPrintIndents = true, bool box = false) override;
172 void writeArrayStart(char const* tag, bool shouldPrintIndents = true, bool box = true) override;
173 void writeArrayEnding(char const* tag, bool shouldPrintIndents = true, bool box = true) override;
174 void insertCommaIfNeeded() override {}
175 void printIndents() override;
176 void write(char const* output) override;
177 Error closeFileAfterWriting(char const* path = nullptr, char const* beginningString = nullptr,
178 char const* endString = nullptr) override;
179 void reset() override;
180
181private:
182 uint8_t indentAmount;
183};
184
186public:
187 virtual bool prepareToReadTagOrAttributeValueOneCharAtATime() = 0;
188 virtual char readNextCharOfTagOrAttributeValue() = 0;
189 virtual int32_t getNumCharsRemainingInValueBeforeEndOfCluster() = 0;
190
191 virtual char const* readNextTagOrAttributeName() = 0;
192 virtual char const* readTagOrAttributeValue() = 0;
193 virtual int32_t readTagOrAttributeValueInt() = 0;
194 virtual int32_t readTagOrAttributeValueHex(int32_t errorValue) = 0;
195 virtual int readTagOrAttributeValueHexBytes(uint8_t* bytes, int32_t maxLen) = 0;
196 virtual Error tryReadingFirmwareTagFromFile(char const* tagName, bool ignoreIncorrectFirmware = false) = 0;
197
198 virtual char const* readNextCharsOfTagOrAttributeValue(int32_t numChars) = 0;
199 virtual Error readTagOrAttributeValueString(String* string) = 0;
200 virtual bool match(char const ch) = 0;
201 virtual void exitTag(char const* exitTagName = NULL, bool closeObject = false) = 0;
202
203 virtual void reset() = 0;
204
205 Error readTagOrAttributeValueString(std::string& string) {
206 String tmp;
207 Error error = readTagOrAttributeValueString(&tmp);
208 if (error == Error::NONE) {
209 string = tmp.get();
210 }
211 return error;
212 }
213};
214
215class FileDeserializer : public Deserializer, public FileReader {
216public:
217 FileDeserializer() : FileReader() {}
218 FileDeserializer(uint8_t* inbuf, size_t buflen) : FileReader((char*)inbuf, buflen) {}
219};
220
221class XMLDeserializer : public FileDeserializer {
222public:
223 XMLDeserializer();
224 ~XMLDeserializer() override = default;
225
226 bool prepareToReadTagOrAttributeValueOneCharAtATime() override;
227 char const* readNextTagOrAttributeName() override;
228 char readNextCharOfTagOrAttributeValue() override;
229 int32_t getNumCharsRemainingInValueBeforeEndOfCluster() override;
230
231 int32_t readTagOrAttributeValueInt() override;
232 int32_t readTagOrAttributeValueHex(int32_t errorValue) override;
233 int readTagOrAttributeValueHexBytes(uint8_t* bytes, int32_t maxLen) override;
234
235 int readHexBytesUntil(uint8_t* bytes, int32_t maxLen, char endPos);
236 char const* readNextCharsOfTagOrAttributeValue(int32_t numChars) override;
237 Error readTagOrAttributeValueString(String* string) override;
238 char const* readTagOrAttributeValue() override;
239 bool match(char const ch) override;
240
241 void exitTag(char const* exitTagName = NULL, bool closeObject = false) override;
242 Error openXMLFile(FilePointer* filePointer, char const* firstTagName, char const* altTagName = "",
243 bool ignoreIncorrectFirmware = false);
244 void reset() override;
245
246 Error tryReadingFirmwareTagFromFile(char const* tagName, bool ignoreIncorrectFirmware) override;
247
248private:
249 char charAtEndOfValue;
250 uint8_t xmlArea; // state variable for tokenizer
251
252 int32_t tagDepthCaller; // How deeply indented in XML the main Deluge classes think we are, as data being read.
253 int32_t tagDepthFile; // Will temporarily be different to the above as unwanted / unused XML tags parsed on the way
254 // to finding next useful data.
255
256 char stringBuffer[kFilenameBufferSize];
257
258 void skipUntilChar(char endChar);
259
260 char const* readTagName();
261 char const* readNextAttributeName();
262 char const* readUntilChar(char endChar);
263 char const* readAttributeValue();
264
265 int32_t readIntUntilChar(char endChar);
266 bool getIntoAttributeValue();
267 int32_t readAttributeValueInt();
268
269 Error readStringUntilChar(String* string, char endChar);
270 Error readAttributeValueString(String* string);
271};
272
273class JsonSerializer : public Serializer, public FileWriter {
274public:
275 JsonSerializer();
276 ~JsonSerializer() override = default;
277
278 void writeAttribute(char const* name, int32_t number, bool onNewLine = true) override;
279 void writeAttribute(char const* name, char const* value, bool onNewLine = true) override;
280 void writeAttributeHex(char const* name, int32_t number, int32_t numChars, bool onNewLine = true) override;
281 void writeAttributeHexBytes(char const* name, uint8_t* data, int32_t numBytes, bool onNewLine = true) override;
282 void writeTagNameAndSeperator(char const* tag) override;
283 void writeTag(char const* tag, int32_t number, bool box = false) override;
284 void writeTag(char const* tag, char const* contents, bool box = false, bool quote = true) override;
285 void writeOpeningTag(char const* tag, bool startNewLineAfter = true, bool box = false) override;
286 void writeOpeningTagBeginning(char const* tag, bool box = false, bool newLineBefore = true) override;
287 void writeOpeningTagEnd(bool startNewLineAfter = true) override;
288 void closeTag(bool box = false) override;
289 void writeClosingTag(char const* tag, bool shouldPrintIndents = true, bool box = false) override;
290 void writeArrayStart(char const* tag, bool shouldPrintIndents = true, bool box = false) override;
291 void writeArrayEnding(char const* tag, bool shouldPrintIndents = true, bool box = false) override;
292 void printIndents() override;
293 void insertCommaIfNeeded() override;
294 void write(char const* output) override;
295 Error closeFileAfterWriting(char const* path = nullptr, char const* beginningString = nullptr,
296 char const* endString = nullptr) override;
297 void reset() override;
298 // Begin Json-only API
299
300private:
301 uint8_t indentAmount;
302 bool firstItemHasBeenWritten = false;
303};
304
305class JsonDeserializer : public FileDeserializer {
306public:
307 JsonDeserializer();
308 JsonDeserializer(uint8_t* inbuf, size_t buflen);
309 ~JsonDeserializer() override = default;
310
311 bool prepareToReadTagOrAttributeValueOneCharAtATime() override;
312 char const* readNextTagOrAttributeName() override;
313 char readNextCharOfTagOrAttributeValue() override;
314 int32_t getNumCharsRemainingInValueBeforeEndOfCluster() override;
315
316 int32_t readTagOrAttributeValueInt() override;
317 int32_t readTagOrAttributeValueHex(int32_t errorValue) override;
318 int readTagOrAttributeValueHexBytes(uint8_t* bytes, int32_t maxLen) override;
319
320 int readHexBytesUntil(uint8_t* bytes, int32_t maxLen, char endPos);
321 char const* readNextCharsOfTagOrAttributeValue(int32_t numChars) override;
322 Error readTagOrAttributeValueString(String* string) override;
323 char const* readTagOrAttributeValue() override;
324 bool match(char const ch) override;
325 void exitTag(char const* exitTagName = NULL, bool closeObject = false) override;
326
327 Error openJsonFile(FilePointer* filePointer, char const* firstTagName, char const* altTagName = "",
328 bool ignoreIncorrectFirmware = false);
329 void reset() override;
330 Error tryReadingFirmwareTagFromFile(char const* tagName, bool ignoreIncorrectFirmware) override;
331 void setReplySeqNum(uint8_t msgNum) { replySeqNum = msgNum; }
332 uint8_t getReplySeqNum() { return replySeqNum; }
333
334private:
335 uint8_t replySeqNum = 0;
336 int32_t objectDepth;
337 int32_t arrayDepth;
338
339 enum JsonState { NewFile, KeyRead, ValueRead, ReadError };
340 JsonState readState = NewFile;
341
342 char stringBuffer[kFilenameBufferSize];
343
344 void skipUntilChar(char endChar);
345 char unescape(char inchar);
346 bool skipWhiteSpace(bool commasToo = true);
347 char const* readQuotedString();
348 char const* readKeyName();
349 char const* readNextAttributeName();
350 char const* readUntilChar(char endChar);
351 char const* readAttributeValue();
352
353 int32_t readInt();
354 bool getIntoAttributeValue();
355 int32_t readAttributeValueInt();
356 Error readAttributeValueString(String* string);
357
358 Error readStringUntilChar(String* string, char endChar);
359};
360
361extern XMLSerializer smSerializer;
362extern XMLDeserializer smDeserializer;
363extern JsonSerializer smJsonSerializer;
364extern JsonDeserializer smJsonDeserializer;
365extern Serializer& GetSerializer();
366extern FileDeserializer* activeDeserializer;
367
368namespace StorageManager {
369
370std::expected<FatFS::File, Error> createFile(char const* filePath, bool mayOverwrite);
371Error createXMLFile(char const* pathName, XMLSerializer& writer, bool mayOverwrite = false, bool displayErrors = true);
372Error createJsonFile(char const* pathName, JsonSerializer& writer, bool mayOverwrite = false,
373 bool displayErrors = true);
374Error openXMLFile(FilePointer* filePointer, XMLDeserializer& reader, char const* firstTagName,
375 char const* altTagName = "", bool ignoreIncorrectFirmware = false);
376Error openJsonFile(FilePointer* filePointer, JsonDeserializer& reader, char const* firstTagName,
377 char const* altTagName = "", bool ignoreIncorrectFirmware = false);
378Error openDelugeFile(FileItem* currentFileItem, char const* firstTagName, char const* altTagName = "",
379 bool ignoreIncorrectFirmware = false);
380Error initSD();
381
382bool fileExists(char const* pathName);
383bool fileExists(char const* pathName, FilePointer* fp);
385bool buildPathToFile(const char* fileName);
386
387bool checkSDPresent();
388bool checkSDInitialized();
389
390Instrument* createNewInstrument(OutputType newOutputType, ParamManager* getParamManager = nullptr);
391Error loadInstrumentFromFile(Song* song, InstrumentClip* clip, OutputType outputType, bool mayReadSamplesFromFiles,
392 Instrument** getInstrument, FilePointer* filePointer, String* name, String* dirPath);
393Instrument* createNewNonAudioInstrument(OutputType outputType, int32_t slot, int32_t subSlot);
394
395Error openMidiDeviceDefinitionFile(FilePointer* filePointer);
396Error loadMidiDeviceDefinitionFile(MIDIInstrument* midiInstrument, FilePointer* filePointer, String* fileName,
397 bool updateFileName = true);
398
399Error openPatternFile(FilePointer* filePointer);
400Error loadPatternFile(FilePointer* filePointer, String* fileName, bool overwriteExisting, bool noScaling,
401 bool previewOnly, bool selectedDrumOnly);
402
403Error openFavouriteFile(FilePointer* filePointer);
404Error loadFavouriteFile(FilePointer* filePointer, String* fileName);
405
406Drum* createNewDrum(DrumType drumType);
407Error loadSynthToDrum(Song* song, InstrumentClip* clip, bool mayReadSamplesFromFiles, SoundDrum** getInstrument,
408 FilePointer* filePointer, String* name, String* dirPath);
409void openFilePointer(FilePointer* fp, FileReader& reader);
410
411Error checkSpaceOnCard();
412
413Error openInstrumentFile(OutputType outputType, FilePointer* filePointer);
414} // namespace StorageManager
415
416extern FirmwareVersion song_firmware_version;
417extern FILINFO staticFNO;
418extern FatFS::Directory staticDIR;
419extern const bool writeJsonFlag;
420
421inline bool isCardReady() {
422 return !sdRoutineLock && Error::NONE == StorageManager::initSD();
423}
Definition arpeggiator.h:48
Definition storage_manager.h:185
Definition drum.h:44
Definition storage_manager.h:215
Definition file_item.h:24
Definition storage_manager.h:53
Definition instrument_clip.h:48
Definition instrument.h:45
Definition storage_manager.h:305
Definition storage_manager.h:273
Definition midi_instrument.h:37
Definition midi_param_collection.h:30
Definition param_manager.h:174
Definition param_manager.h:45
Definition playback_mode.h:30
Definition storage_manager.h:51
Definition storage_manager.h:119
Definition song.h:103
Definition sound_drum.h:28
Definition d_string.h:41
Definition storage_manager.h:221
Definition storage_manager.h:155