Deluge Firmware 1.3.0
Build date: 2025.04.16
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
206class FileDeserializer : public Deserializer, public FileReader {
207public:
208 FileDeserializer() : FileReader() {}
209 FileDeserializer(uint8_t* inbuf, size_t buflen) : FileReader((char*)inbuf, buflen) {}
210};
211
212class XMLDeserializer : public FileDeserializer {
213public:
214 XMLDeserializer();
215 ~XMLDeserializer() override = default;
216
217 bool prepareToReadTagOrAttributeValueOneCharAtATime() override;
218 char const* readNextTagOrAttributeName() override;
219 char readNextCharOfTagOrAttributeValue() override;
220 int32_t getNumCharsRemainingInValueBeforeEndOfCluster() override;
221
222 int32_t readTagOrAttributeValueInt() override;
223 int32_t readTagOrAttributeValueHex(int32_t errorValue) override;
224 int readTagOrAttributeValueHexBytes(uint8_t* bytes, int32_t maxLen) override;
225
226 int readHexBytesUntil(uint8_t* bytes, int32_t maxLen, char endPos);
227 char const* readNextCharsOfTagOrAttributeValue(int32_t numChars) override;
228 Error readTagOrAttributeValueString(String* string) override;
229 char const* readTagOrAttributeValue() override;
230 bool match(char const ch) override;
231
232 void exitTag(char const* exitTagName = NULL, bool closeObject = false) override;
233 Error openXMLFile(FilePointer* filePointer, char const* firstTagName, char const* altTagName = "",
234 bool ignoreIncorrectFirmware = false);
235 void reset() override;
236
237 Error tryReadingFirmwareTagFromFile(char const* tagName, bool ignoreIncorrectFirmware) override;
238
239private:
240 char charAtEndOfValue;
241 uint8_t xmlArea; // state variable for tokenizer
242
243 int32_t tagDepthCaller; // How deeply indented in XML the main Deluge classes think we are, as data being read.
244 int32_t tagDepthFile; // Will temporarily be different to the above as unwanted / unused XML tags parsed on the way
245 // to finding next useful data.
246
247 char stringBuffer[kFilenameBufferSize];
248
249 void skipUntilChar(char endChar);
250
251 char const* readTagName();
252 char const* readNextAttributeName();
253 char const* readUntilChar(char endChar);
254 char const* readAttributeValue();
255
256 int32_t readIntUntilChar(char endChar);
257 bool getIntoAttributeValue();
258 int32_t readAttributeValueInt();
259
260 Error readStringUntilChar(String* string, char endChar);
261 Error readAttributeValueString(String* string);
262};
263
264class JsonSerializer : public Serializer, public FileWriter {
265public:
266 JsonSerializer();
267 ~JsonSerializer() override = default;
268
269 void writeAttribute(char const* name, int32_t number, bool onNewLine = true) override;
270 void writeAttribute(char const* name, char const* value, bool onNewLine = true) override;
271 void writeAttributeHex(char const* name, int32_t number, int32_t numChars, bool onNewLine = true) override;
272 void writeAttributeHexBytes(char const* name, uint8_t* data, int32_t numBytes, bool onNewLine = true) override;
273 void writeTagNameAndSeperator(char const* tag) override;
274 void writeTag(char const* tag, int32_t number, bool box = false) override;
275 void writeTag(char const* tag, char const* contents, bool box = false, bool quote = true) override;
276 void writeOpeningTag(char const* tag, bool startNewLineAfter = true, bool box = false) override;
277 void writeOpeningTagBeginning(char const* tag, bool box = false, bool newLineBefore = true) override;
278 void writeOpeningTagEnd(bool startNewLineAfter = true) override;
279 void closeTag(bool box = false) override;
280 void writeClosingTag(char const* tag, bool shouldPrintIndents = true, bool box = false) override;
281 void writeArrayStart(char const* tag, bool shouldPrintIndents = true, bool box = false) override;
282 void writeArrayEnding(char const* tag, bool shouldPrintIndents = true, bool box = false) override;
283 void printIndents() override;
284 void insertCommaIfNeeded() override;
285 void write(char const* output) override;
286 Error closeFileAfterWriting(char const* path = nullptr, char const* beginningString = nullptr,
287 char const* endString = nullptr) override;
288 void reset() override;
289 // Begin Json-only API
290
291private:
292 uint8_t indentAmount;
293 bool firstItemHasBeenWritten = false;
294};
295
296class JsonDeserializer : public FileDeserializer {
297public:
298 JsonDeserializer();
299 JsonDeserializer(uint8_t* inbuf, size_t buflen);
300 ~JsonDeserializer() override = default;
301
302 bool prepareToReadTagOrAttributeValueOneCharAtATime() override;
303 char const* readNextTagOrAttributeName() override;
304 char readNextCharOfTagOrAttributeValue() override;
305 int32_t getNumCharsRemainingInValueBeforeEndOfCluster() override;
306
307 int32_t readTagOrAttributeValueInt() override;
308 int32_t readTagOrAttributeValueHex(int32_t errorValue) override;
309 int readTagOrAttributeValueHexBytes(uint8_t* bytes, int32_t maxLen) override;
310
311 int readHexBytesUntil(uint8_t* bytes, int32_t maxLen, char endPos);
312 char const* readNextCharsOfTagOrAttributeValue(int32_t numChars) override;
313 Error readTagOrAttributeValueString(String* string) override;
314 char const* readTagOrAttributeValue() override;
315 bool match(char const ch) override;
316 void exitTag(char const* exitTagName = NULL, bool closeObject = false) override;
317
318 Error openJsonFile(FilePointer* filePointer, char const* firstTagName, char const* altTagName = "",
319 bool ignoreIncorrectFirmware = false);
320 void reset() override;
321 Error tryReadingFirmwareTagFromFile(char const* tagName, bool ignoreIncorrectFirmware) override;
322 void setReplySeqNum(uint8_t msgNum) { replySeqNum = msgNum; }
323 uint8_t getReplySeqNum() { return replySeqNum; }
324
325private:
326 uint8_t replySeqNum = 0;
327 int32_t objectDepth;
328 int32_t arrayDepth;
329
330 enum JsonState { NewFile, KeyRead, ValueRead, ReadError };
331 JsonState readState = NewFile;
332
333 char stringBuffer[kFilenameBufferSize];
334
335 void skipUntilChar(char endChar);
336 char unescape(char inchar);
337 bool skipWhiteSpace(bool commasToo = true);
338 char const* readQuotedString();
339 char const* readKeyName();
340 char const* readNextAttributeName();
341 char const* readUntilChar(char endChar);
342 char const* readAttributeValue();
343
344 int32_t readInt();
345 bool getIntoAttributeValue();
346 int32_t readAttributeValueInt();
347 Error readAttributeValueString(String* string);
348
349 Error readStringUntilChar(String* string, char endChar);
350};
351
352extern XMLSerializer smSerializer;
353extern XMLDeserializer smDeserializer;
354extern JsonSerializer smJsonSerializer;
355extern JsonDeserializer smJsonDeserializer;
356extern Serializer& GetSerializer();
357extern FileDeserializer* activeDeserializer;
358
359namespace StorageManager {
360
361std::expected<FatFS::File, Error> createFile(char const* filePath, bool mayOverwrite);
362Error createXMLFile(char const* pathName, XMLSerializer& writer, bool mayOverwrite = false, bool displayErrors = true);
363Error createJsonFile(char const* pathName, JsonSerializer& writer, bool mayOverwrite = false,
364 bool displayErrors = true);
365Error openXMLFile(FilePointer* filePointer, XMLDeserializer& reader, char const* firstTagName,
366 char const* altTagName = "", bool ignoreIncorrectFirmware = false);
367Error openJsonFile(FilePointer* filePointer, JsonDeserializer& reader, char const* firstTagName,
368 char const* altTagName = "", bool ignoreIncorrectFirmware = false);
369Error openDelugeFile(FileItem* currentFileItem, char const* firstTagName, char const* altTagName = "",
370 bool ignoreIncorrectFirmware = false);
371Error initSD();
372
373bool fileExists(char const* pathName);
374bool fileExists(char const* pathName, FilePointer* fp);
376bool buildPathToFile(const char* fileName);
377
378bool checkSDPresent();
379bool checkSDInitialized();
380
381Instrument* createNewInstrument(OutputType newOutputType, ParamManager* getParamManager = nullptr);
382Error loadInstrumentFromFile(Song* song, InstrumentClip* clip, OutputType outputType, bool mayReadSamplesFromFiles,
383 Instrument** getInstrument, FilePointer* filePointer, String* name, String* dirPath);
384Instrument* createNewNonAudioInstrument(OutputType outputType, int32_t slot, int32_t subSlot);
385
386Error openMidiDeviceDefinitionFile(FilePointer* filePointer);
387Error loadMidiDeviceDefinitionFile(MIDIInstrument* midiInstrument, FilePointer* filePointer, String* fileName,
388 bool updateFileName = true);
389
390Error openPatternFile(FilePointer* filePointer);
391Error loadPatternFile(FilePointer* filePointer, String* fileName, bool overwriteExisting, bool noScaling,
392 bool previewOnly, bool selectedDrumOnly);
393
394Error openFavouriteFile(FilePointer* filePointer);
395Error loadFavouriteFile(FilePointer* filePointer, String* fileName);
396
397Drum* createNewDrum(DrumType drumType);
398Error loadSynthToDrum(Song* song, InstrumentClip* clip, bool mayReadSamplesFromFiles, SoundDrum** getInstrument,
399 FilePointer* filePointer, String* name, String* dirPath);
400void openFilePointer(FilePointer* fp, FileReader& reader);
401
402Error checkSpaceOnCard();
403
404Error openInstrumentFile(OutputType outputType, FilePointer* filePointer);
405} // namespace StorageManager
406
407extern FirmwareVersion song_firmware_version;
408extern FILINFO staticFNO;
409extern FatFS::Directory staticDIR;
410extern const bool writeJsonFlag;
411
412inline bool isCardReady() {
413 return !sdRoutineLock && Error::NONE == StorageManager::initSD();
414}
Definition arpeggiator.h:46
Definition storage_manager.h:185
Definition drum.h:44
Definition storage_manager.h:206
Definition file_item.h:24
Definition storage_manager.h:53
Definition instrument_clip.h:48
Definition instrument.h:45
Definition storage_manager.h:296
Definition storage_manager.h:264
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:104
Definition sound_drum.h:28
Definition d_string.h:46
Definition storage_manager.h:212
Definition storage_manager.h:155