From 9380e3e79eb91f5922e656ec1f985dd384055467 Mon Sep 17 00:00:00 2001 From: Dejvino Date: Sat, 28 Feb 2026 20:13:32 +0100 Subject: [PATCH] Guaranteed random grid output --- main.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 18 deletions(-) diff --git a/main.cpp b/main.cpp index 0c53af1..6c644a5 100644 --- a/main.cpp +++ b/main.cpp @@ -739,6 +739,8 @@ void randomizeGrid() { c.type = (SynthEngine::GridCell::Type)(rand() % numTypes); c.rotation = rand() % 4; c.param = (float)rand() / (float)RAND_MAX; + c.value = 0.0f; + c.phase = 0.0f; } } @@ -819,30 +821,80 @@ void randomizeGrid() { } } } - validGrid = hasSoundSource && hasGate; - } + + if (hasSoundSource && hasGate) { + // 4. Prune unreachable elements + for (int x = 0; x < SynthEngine::GRID_W; ++x) { + for (int y = 0; y < SynthEngine::GRID_H; ++y) { + if (!visited[x][y]) { + engine.grid[x][y].type = SynthEngine::GridCell::EMPTY; + engine.grid[x][y].param = 0.5f; + engine.grid[x][y].rotation = 0; + } + } + } - // 4. Prune unreachable elements if a valid grid was found - if (validGrid) { - for (int x = 0; x < SynthEngine::GRID_W; ++x) { - for (int y = 0; y < SynthEngine::GRID_H; ++y) { - if (!visited[x][y]) { - engine.grid[x][y].type = SynthEngine::GridCell::EMPTY; - engine.grid[x][y].param = 0.5f; - engine.grid[x][y].rotation = 0; + // 5. Allocate buffers for DELAYs and REVERBs + for (int x = 0; x < SynthEngine::GRID_W; ++x) { + for (int y = 0; y < SynthEngine::GRID_H; ++y) { + SynthEngine::GridCell& c = engine.grid[x][y]; + if (c.type == SynthEngine::GridCell::DELAY || c.type == SynthEngine::GridCell::REVERB || c.type == SynthEngine::GridCell::PITCH_SHIFTER) { + c.buffer_size = 2 * SAMPLE_RATE; + c.buffer = new float[c.buffer_size](); + c.write_idx = 0; + } + } + } + + // 6. Run Simulation + engine.setGate(true); + float oldFreq = engine.getFrequency(); + engine.setFrequency(440.0f); + bool soundDetected = false; + for(int i=0; i<1000; ++i) { + float val = engine.processGridStep(); + if (fabsf(val) > 0.001f) { + soundDetected = true; + break; + } + } + engine.setGate(false); + engine.setFrequency(oldFreq); + + if (soundDetected) { + validGrid = true; + // Reset values to avoid initial pop + for (int x = 0; x < SynthEngine::GRID_W; ++x) { + for (int y = 0; y < SynthEngine::GRID_H; ++y) { + engine.grid[x][y].value = 0.0f; + } + } + } else { + // Failed simulation, cleanup buffers + for (int x = 0; x < SynthEngine::GRID_W; ++x) { + for (int y = 0; y < SynthEngine::GRID_H; ++y) { + SynthEngine::GridCell& c = engine.grid[x][y]; + if (c.buffer) { + delete[] c.buffer; + c.buffer = nullptr; + c.buffer_size = 0; + } + } } } } } - // 5. Allocate buffers for DELAYs and REVERBs that are still present - for (int x = 0; x < SynthEngine::GRID_W; ++x) { - for (int y = 0; y < SynthEngine::GRID_H; ++y) { - SynthEngine::GridCell& c = engine.grid[x][y]; - if (c.type == SynthEngine::GridCell::DELAY || c.type == SynthEngine::GridCell::REVERB || c.type == SynthEngine::GridCell::PITCH_SHIFTER) { - c.buffer_size = 2 * SAMPLE_RATE; - c.buffer = new float[c.buffer_size](); - c.write_idx = 0; + // If failed after all attempts, ensure grid is clean + if (!validGrid) { + for (int x = 0; x < SynthEngine::GRID_W; ++x) { + for (int y = 0; y < SynthEngine::GRID_H; ++y) { + SynthEngine::GridCell& c = engine.grid[x][y]; + if (c.type != SynthEngine::GridCell::SINK) { + c.type = SynthEngine::GridCell::EMPTY; + c.param = 0.5f; + c.rotation = 0; + } } } }