Fix terminal export import

This commit is contained in:
Dejvino
2026-03-01 14:08:41 +01:00
parent 1047d846f9
commit 14ac4401ce
4 changed files with 178 additions and 52 deletions
+56 -16
View File
@@ -42,37 +42,77 @@ SynthEngine::SynthEngine(uint32_t sampleRate)
SynthEngine::~SynthEngine() {
}
void SynthEngine::exportGrid(uint8_t* buffer) {
size_t SynthEngine::exportGrid(uint8_t* buffer) {
SynthLockGuard<SynthMutex> lock(gridMutex);
size_t idx = 0;
uint8_t count = 0;
for(int y=0; y<GRID_H; ++y) {
for(int x=0; x<GRID_W; ++x) {
GridCell& c = grid[x][y];
buffer[idx++] = (uint8_t)c.type;
buffer[idx++] = (uint8_t)(c.param * 255.0f);
buffer[idx++] = (uint8_t)c.rotation;
if (grid[x][y].type != GridCell::EMPTY) count++;
}
}
}
void SynthEngine::importGrid(const uint8_t* buffer) {
SynthLockGuard<SynthMutex> lock(gridMutex);
size_t idx = 0;
buffer[idx++] = count;
for(int y=0; y<GRID_H; ++y) {
for(int x=0; x<GRID_W; ++x) {
GridCell& c = grid[x][y];
uint8_t t = buffer[idx++];
uint8_t p = buffer[idx++];
uint8_t r = buffer[idx++];
GridCell::Type newType = (GridCell::Type)t;
c.type = newType;
if (c.type != GridCell::EMPTY) {
buffer[idx++] = (uint8_t)x;
buffer[idx++] = (uint8_t)y;
buffer[idx++] = (uint8_t)c.type;
buffer[idx++] = (uint8_t)(c.param * 255.0f);
buffer[idx++] = (uint8_t)c.rotation;
}
}
}
buffer[idx++] = count;
return idx;
}
int SynthEngine::importGrid(const uint8_t* buffer, size_t size) {
if (size < 2) return 1;
uint8_t countStart = buffer[0];
uint8_t countEnd = buffer[size - 1];
if (countStart != countEnd) return 2;
size_t expectedSize = 1 + countStart * 5 + 1;
if (size != expectedSize) return 3;
SynthLockGuard<SynthMutex> lock(gridMutex);
// Clear grid first
for (int x = 0; x < GRID_W; ++x) {
for (int y = 0; y < GRID_H; ++y) {
GridCell& c = grid[x][y];
if (c.type == GridCell::SINK) continue;
c.type = GridCell::EMPTY;
c.param = 0.5f;
c.rotation = 0;
c.value = 0.0f;
c.phase = 0.0f;
c.next_value = 0.0f;
}
}
size_t idx = 1;
for(int i=0; i<countStart; ++i) {
uint8_t x = buffer[idx++];
uint8_t y = buffer[idx++];
uint8_t t = buffer[idx++];
uint8_t p = buffer[idx++];
uint8_t r = buffer[idx++];
if (x < GRID_W && y < GRID_H) {
GridCell& c = grid[x][y];
c.type = (GridCell::Type)t;
c.param = (float)p / 255.0f;
c.rotation = r;
}
}
rebuildProcessingOrder_locked();
return 0;
}
void SynthEngine::clearGrid() {