-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.cpp
More file actions
212 lines (192 loc) · 7.8 KB
/
Copy pathstorage.cpp
File metadata and controls
212 lines (192 loc) · 7.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
// ============================================================
// CardputerGroovebox - storage.cpp
// ============================================================
#include "storage.h"
#include "sequencer.h"
#include "sampler.h"
#include "wavetable.h"
#include <SD.h>
#include <string.h>
uint8_t g_curProject = 0;
#define GBX_MAGIC 0x31584247u // "GBX1"
#define GBX_VERSION 2 // v2: polyphonic cells + per-track VOICES
// ---- serialized structures (POD, packed layout kept stable) ----
struct __attribute__((packed)) SaveSynth {
uint8_t oscMode, wtIndex;
float cutoff, reso, envAmt, ampDec, filtDec, volume;
};
struct __attribute__((packed)) SaveDrumLane {
uint8_t engine, type, chokeGroup;
float volume, tune, decay;
char sampleName[SAMPLE_NAME_LEN]; // empty = none
};
// ---------- v1 (legacy, mono cells) ----------
struct __attribute__((packed)) SaveCellV1 {
uint8_t note, octave, flags; // bit0 accent, bit1 slide
};
struct __attribute__((packed)) ProjectFileV1 {
uint32_t magic;
uint16_t version;
uint16_t bpm;
uint8_t songLoopStart;
uint8_t song[SONG_LENGTH];
SaveSynth synths[NUM_SYNTHS];
SaveDrumLane lanes[NUM_DRUM_LANES];
SaveCellV1 cells[NUM_PATTERNS][NUM_SYNTHS][NUM_STEPS];
uint8_t drums[NUM_PATTERNS][NUM_STEPS];
};
// ---------- v2 (poly cells + voices) ----------
struct __attribute__((packed)) SaveCell {
uint8_t note[MAX_POLY];
uint8_t oct [MAX_POLY];
uint8_t flags; // bit0 accent, bit1 slide
};
struct __attribute__((packed)) ProjectFile {
uint32_t magic;
uint16_t version;
uint16_t bpm;
uint8_t songLoopStart;
uint8_t song[SONG_LENGTH];
uint8_t voices[NUM_SYNTHS]; // 1..MAX_POLY per synth track
SaveSynth synths[NUM_SYNTHS];
SaveDrumLane lanes[NUM_DRUM_LANES];
SaveCell cells[NUM_PATTERNS][NUM_SYNTHS][NUM_STEPS];
uint8_t drums[NUM_PATTERNS][NUM_STEPS];
};
static void slotPath(uint8_t slot, char* out, size_t n) {
snprintf(out, n, "%s/P%u.gbx", DIR_PROJECTS, (unsigned)(slot + 1));
}
bool storageProjectExists(uint8_t slot) {
char path[64]; slotPath(slot, path, sizeof(path));
return SD.exists(path);
}
bool storageSaveProject(uint8_t slot) {
static ProjectFile pf; // static: too big for stack
memset(&pf, 0, sizeof(pf));
pf.magic = GBX_MAGIC;
pf.version = GBX_VERSION;
pf.bpm = g_bpm;
pf.songLoopStart = g_songLoopStart;
memcpy(pf.song, g_song, SONG_LENGTH);
for (int s = 0; s < NUM_SYNTHS; s++) {
SynthVoice& v = g_synths[s].v[0];
pf.voices[s] = g_synths[s].voices;
pf.synths[s] = { (uint8_t)v.oscMode, v.wtIndex,
v.fltCutoff, v.fltReso, v.fltEnvAmt,
v.ampDecRate, v.filtDecRate, v.volume };
}
for (int l = 0; l < NUM_DRUM_LANES; l++) {
DrumLane& d = g_drumLanes[l];
SaveDrumLane& o = pf.lanes[l];
o.engine = d.engine; o.type = d.type; o.chokeGroup = d.chokeGroup;
o.volume = d.volume; o.tune = d.tune; o.decay = d.decay;
if (d.engine == ENG_SMPL && d.sampleSlot >= 0 && d.sampleSlot < g_numSamples)
strncpy(o.sampleName, g_samples[d.sampleSlot].name, SAMPLE_NAME_LEN - 1);
}
for (int p = 0; p < NUM_PATTERNS; p++) {
for (int s = 0; s < NUM_SYNTHS; s++)
for (int st = 0; st < NUM_STEPS; st++) {
const SynthCell& c = g_patterns[p].synth[s][st];
SaveCell& o = pf.cells[p][s][st];
for (int i = 0; i < MAX_POLY; i++) { o.note[i] = c.note[i]; o.oct[i] = c.oct[i]; }
o.flags = (uint8_t)((c.accent ? 1 : 0) | (c.slide ? 2 : 0));
}
memcpy(pf.drums[p], g_patterns[p].drums, NUM_STEPS);
}
char path[64]; slotPath(slot, path, sizeof(path));
SD.remove(path);
File f = SD.open(path, FILE_WRITE);
if (!f) return false;
size_t written = f.write((uint8_t*)&pf, sizeof(pf));
f.close();
return written == sizeof(pf);
}
// shared param/lane/song apply used by both format loaders
static void applySynth(int s, const SaveSynth& in, uint8_t voices) {
SynthTrack& t = g_synths[s];
t.forEach([&](SynthVoice& v) {
v.oscMode = (OscMode)(in.oscMode < OSC_COUNT ? in.oscMode : OSC_SAW);
v.wtIndex = (in.wtIndex < g_numWavetables) ? in.wtIndex : 0;
v.fltCutoff = in.cutoff;
v.fltReso = in.reso;
v.fltEnvAmt = in.envAmt;
v.ampDecRate = in.ampDec;
v.filtDecRate = in.filtDec;
v.volume = in.volume;
v.active = false;
});
t.setVoices(voices);
}
static void applyLane(int l, const SaveDrumLane& o) {
DrumLane& d = g_drumLanes[l];
d.engine = (DrumEngine)(o.engine < ENG_COUNT ? o.engine : ENG_808);
d.type = (o.type < DT_COUNT) ? o.type : DT_KICK;
d.chokeGroup = o.chokeGroup;
d.volume = o.volume; d.tune = o.tune; d.decay = o.decay;
d.sampleSlot = -1;
d.sv.init(); d.smp.init();
if (d.engine == ENG_SMPL && o.sampleName[0])
d.sampleSlot = (int8_t)samplerLoad(o.sampleName); // -1 if missing
}
bool storageLoadProject(uint8_t slot) {
char path[64]; slotPath(slot, path, sizeof(path));
File f = SD.open(path, FILE_READ);
if (!f) return false;
// header peek: magic + version decide the format
uint8_t head[8];
if (f.read(head, 8) != 8) { f.close(); return false; }
uint32_t magic; memcpy(&magic, head, 4);
uint16_t version; memcpy(&version, head + 4, 2);
if (magic != GBX_MAGIC || (version != 1 && version != 2)) { f.close(); return false; }
f.seek(0);
bool wasPlaying = g_playing;
g_playing = false; // pause audio triggering while we swap data
bool ok = false;
samplerClearAll();
if (version == 2) {
static ProjectFile pf;
size_t got = f.read((uint8_t*)&pf, sizeof(pf));
f.close();
if (got == sizeof(pf)) {
g_bpm = pf.bpm;
g_songLoopStart = pf.songLoopStart;
memcpy(g_song, pf.song, SONG_LENGTH);
for (int s = 0; s < NUM_SYNTHS; s++) applySynth(s, pf.synths[s], pf.voices[s]);
for (int l = 0; l < NUM_DRUM_LANES; l++) applyLane(l, pf.lanes[l]);
for (int p = 0; p < NUM_PATTERNS; p++) {
for (int s = 0; s < NUM_SYNTHS; s++)
for (int st = 0; st < NUM_STEPS; st++) {
const SaveCell& c = pf.cells[p][s][st];
SynthCell& o = g_patterns[p].synth[s][st];
for (int i = 0; i < MAX_POLY; i++) { o.note[i] = c.note[i]; o.oct[i] = c.oct[i]; }
o.accent = c.flags & 1; o.slide = c.flags & 2;
}
memcpy(g_patterns[p].drums, pf.drums[p], NUM_STEPS);
}
ok = true;
}
} else { // v1: legacy mono cells, migrate on load
static ProjectFileV1 pf;
size_t got = f.read((uint8_t*)&pf, sizeof(pf));
f.close();
if (got == sizeof(pf)) {
g_bpm = pf.bpm;
g_songLoopStart = pf.songLoopStart;
memcpy(g_song, pf.song, SONG_LENGTH);
for (int s = 0; s < NUM_SYNTHS; s++) applySynth(s, pf.synths[s], 1);
for (int l = 0; l < NUM_DRUM_LANES; l++) applyLane(l, pf.lanes[l]);
for (int p = 0; p < NUM_PATTERNS; p++) {
for (int s = 0; s < NUM_SYNTHS; s++)
for (int st = 0; st < NUM_STEPS; st++) {
const SaveCellV1& c = pf.cells[p][s][st];
g_patterns[p].synth[s][st].setMono(c.note, c.octave,
c.flags & 1, c.flags & 2);
}
memcpy(g_patterns[p].drums, pf.drums[p], NUM_STEPS);
}
ok = true;
}
}
g_playing = wasPlaying && ok;
return ok;
}