Wavetable and reverb

This commit is contained in:
Dejvino
2026-02-28 00:24:17 +01:00
parent e86c06e05f
commit 58af6bd3dc
3 changed files with 261 additions and 8 deletions
+197 -6
View File
@@ -194,6 +194,12 @@ void drawChar(SDL_Renderer* renderer, int x, int y, int size, char c) {
case '>': SDL_RenderDrawLine(renderer, x, y, x+w, y+h2); SDL_RenderDrawLine(renderer, x+w, y+h2, x, y+h); break;
case 'F': SDL_RenderDrawLine(renderer, x, y, x, y+h); SDL_RenderDrawLine(renderer, x, y, x+w, y); SDL_RenderDrawLine(renderer, x, y+h2, x+w2, y+h2); break;
case 'O': drawChar(renderer, x, y, size, '0'); break;
case 'W':
SDL_RenderDrawLine(renderer, x, y, x, y+h);
SDL_RenderDrawLine(renderer, x, y+h, x+w2, y+h2);
SDL_RenderDrawLine(renderer, x+w2, y+h2, x+w, y+h);
SDL_RenderDrawLine(renderer, x+w, y+h, x+w, y);
break;
}
}
@@ -329,6 +335,23 @@ void drawGridCell(SDL_Renderer* renderer, int x, int y, int size, SynthEngine::G
snprintf(buf, 16, "%.0fms", delay_ms);
SDL_SetRenderDrawColor(renderer, 255, 128, 0, 255);
drawString(renderer, x + 5, y + size - 15, 10, buf);
} else if (cell.type == SynthEngine::GridCell::REVERB) {
// Draw R
drawString(renderer, cx - 5, cy - 5, 12, "R");
// Input (Back)
int inDir = (cell.rotation + 2) % 4;
int idx=0, idy=0;
if(inDir==0) idy=-r; else if(inDir==1) idx=r; else if(inDir==2) idy=r; else idx=-r;
SDL_RenderDrawLine(renderer, cx+idx, cy+idy, cx, cy);
// Output (Front)
int outDir = cell.rotation;
int odx=0, ody=0;
if(outDir==0) ody=-r; else if(outDir==1) odx=r; else if(outDir==2) ody=r; else odx=-r;
SDL_RenderDrawLine(renderer, cx, cy, cx+odx, cy+ody);
// Param (Strength)
char buf[16]; snprintf(buf, 16, "%.2f", cell.param);
SDL_SetRenderDrawColor(renderer, 200, 100, 255, 255);
drawString(renderer, x + 5, y + size - 15, 10, buf);
} else if (cell.type == SynthEngine::GridCell::OPERATOR) {
SDL_Rect opRect = {cx - r, cy - r, r*2, r*2};
SDL_RenderDrawRect(renderer, &opRect);
@@ -349,9 +372,171 @@ void drawGridCell(SDL_Renderer* renderer, int x, int y, int size, SynthEngine::G
else if (opType == 4) opChar = '<';
else if (opType == 5) opChar = '>';
drawChar(renderer, cx - 5, cy - 5, 12, opChar);
} else if (cell.type == SynthEngine::GridCell::WAVETABLE) {
drawString(renderer, cx - 5, cy - 5, 12, "W");
// Direction line
int dx=0, dy=0;
if (cell.rotation == 0) dy = -r;
if (cell.rotation == 1) dx = r;
if (cell.rotation == 2) dy = r;
if (cell.rotation == 3) dx = -r;
SDL_RenderDrawLine(renderer, cx, cy, cx+dx, cy+dy);
// Param (Wave index)
int idx = (int)(cell.param * 7.99f);
char buf[4];
snprintf(buf, 4, "%d", idx);
SDL_SetRenderDrawColor(renderer, 128, 128, 255, 255);
drawString(renderer, x + 5, y + size - 15, 10, buf);
}
}
void clearGrid() {
std::lock_guard<std::mutex> lock(engine.gridMutex);
for (int x = 0; x < 5; ++x) {
for (int y = 0; y < 8; ++y) {
SynthEngine::GridCell& c = engine.grid[x][y];
if (c.type == SynthEngine::GridCell::SINK) continue;
if ((c.type == SynthEngine::GridCell::DELAY || c.type == SynthEngine::GridCell::REVERB) && c.buffer) {
delete[] c.buffer;
c.buffer = nullptr;
c.buffer_size = 0;
}
c.type = SynthEngine::GridCell::EMPTY;
c.param = 0.5f;
c.rotation = 0;
c.value = 0.0f;
c.phase = 0.0f;
}
}
}
void randomizeGrid() {
std::lock_guard<std::mutex> lock(engine.gridMutex);
// Number of types to choose from (excluding SINK)
const int numTypes = (int)SynthEngine::GridCell::SINK;
// 1. Clear existing buffers first
for (int x = 0; x < 5; ++x) {
for (int y = 0; y < 8; ++y) {
SynthEngine::GridCell& c = engine.grid[x][y];
if ((c.type == SynthEngine::GridCell::DELAY || c.type == SynthEngine::GridCell::REVERB) && c.buffer) {
delete[] c.buffer;
c.buffer = nullptr;
c.buffer_size = 0;
}
if (c.type != SynthEngine::GridCell::SINK) {
c.type = SynthEngine::GridCell::EMPTY;
}
}
}
int attempts = 0;
bool connected = false;
while (!connected && attempts < 1000) {
attempts++;
// 2. Randomize (without allocation)
for (int x = 0; x < 5; ++x) {
for (int y = 0; y < 8; ++y) {
SynthEngine::GridCell& c = engine.grid[x][y];
if (c.type == SynthEngine::GridCell::SINK) continue;
c.type = (SynthEngine::GridCell::Type)(rand() % numTypes);
c.rotation = rand() % 4;
c.param = (float)rand() / (float)RAND_MAX;
}
}
// 3. Check Connectivity
// BFS from SINK (2,3) backwards
bool visited[5][8] = {false};
std::vector<std::pair<int, int>> q;
q.push_back({2, 3});
visited[2][3] = true;
int head = 0;
while(head < (int)q.size()){
std::pair<int, int> curr = q[head++];
int cx = curr.first;
int cy = curr.second;
// Check neighbors to see if they output to (cx, cy)
int nx[4] = {0, 1, 0, -1};
int ny[4] = {-1, 0, 1, 0};
for(int i=0; i<4; ++i) {
int tx = cx + nx[i];
int ty = cy + ny[i];
if (tx >= 0 && tx < 5 && ty >= 0 && ty < 8) {
if (visited[tx][ty]) continue;
SynthEngine::GridCell& neighbor = engine.grid[tx][ty];
bool pointsToCurr = false;
if (neighbor.type == SynthEngine::GridCell::EMPTY || neighbor.type == SynthEngine::GridCell::SINK) {
pointsToCurr = false;
} else if (neighbor.type == SynthEngine::GridCell::FORK) {
int dx = cx - tx;
int dy = cy - ty;
int dir = -1;
if (dx == 0 && dy == -1) dir = 0; // N
else if (dx == 1 && dy == 0) dir = 1; // E
else if (dx == 0 && dy == 1) dir = 2; // S
else if (dx == -1 && dy == 0) dir = 3; // W
int leftOut = (neighbor.rotation + 3) % 4;
int rightOut = (neighbor.rotation + 1) % 4;
if (dir == leftOut || dir == rightOut) pointsToCurr = true;
} else {
// Standard directional
int dx = cx - tx;
int dy = cy - ty;
int dir = -1;
if (dx == 0 && dy == -1) dir = 0; // N
else if (dx == 1 && dy == 0) dir = 1; // E
else if (dx == 0 && dy == 1) dir = 2; // S
else if (dx == -1 && dy == 0) dir = 3; // W
if (neighbor.rotation == dir) pointsToCurr = true;
}
if (pointsToCurr) {
if (neighbor.type == SynthEngine::GridCell::FIXED_OSCILLATOR ||
neighbor.type == SynthEngine::GridCell::INPUT_OSCILLATOR ||
neighbor.type == SynthEngine::GridCell::WAVETABLE ||
neighbor.type == SynthEngine::GridCell::NOISE) {
connected = true;
break;
}
visited[tx][ty] = true;
q.push_back({tx, ty});
}
}
}
if (connected) break;
}
}
// 4. Allocate buffers for DELAYs
for (int x = 0; x < 5; ++x) {
for (int y = 0; y < 8; ++y) {
SynthEngine::GridCell& c = engine.grid[x][y];
if (c.type == SynthEngine::GridCell::DELAY || c.type == SynthEngine::GridCell::REVERB) {
c.buffer_size = 2 * SAMPLE_RATE;
c.buffer = new float[c.buffer_size]();
c.write_idx = 0;
}
}
}
printf("Randomized in %d attempts. Connected: %s\n", attempts, connected ? "YES" : "NO");
}
int main(int argc, char* argv[]) {
(void)argc; (void)argv;
@@ -460,15 +645,17 @@ int main(int argc, char* argv[]) {
SynthEngine::GridCell::Type newType = oldType;
if (e.button.button == SDL_BUTTON_LEFT) {
// Cycle: EMPTY -> FIXED -> INPUT -> NOISE -> FORK -> WIRE -> OP -> DELAY -> EMPTY
// Cycle: EMPTY -> FIXED -> INPUT -> WAVETABLE -> NOISE -> FORK -> WIRE -> OP -> DELAY -> REVERB -> EMPTY
if (oldType == SynthEngine::GridCell::EMPTY) newType = SynthEngine::GridCell::FIXED_OSCILLATOR;
else if (oldType == SynthEngine::GridCell::FIXED_OSCILLATOR) newType = SynthEngine::GridCell::INPUT_OSCILLATOR;
else if (oldType == SynthEngine::GridCell::INPUT_OSCILLATOR) newType = SynthEngine::GridCell::NOISE;
else if (oldType == SynthEngine::GridCell::INPUT_OSCILLATOR) newType = SynthEngine::GridCell::WAVETABLE;
else if (oldType == SynthEngine::GridCell::WAVETABLE) newType = SynthEngine::GridCell::NOISE;
else if (oldType == SynthEngine::GridCell::NOISE) newType = SynthEngine::GridCell::FORK;
else if (oldType == SynthEngine::GridCell::FORK) newType = SynthEngine::GridCell::WIRE;
else if (oldType == SynthEngine::GridCell::WIRE) newType = SynthEngine::GridCell::OPERATOR;
else if (oldType == SynthEngine::GridCell::OPERATOR) newType = SynthEngine::GridCell::DELAY;
else if (oldType == SynthEngine::GridCell::DELAY) newType = SynthEngine::GridCell::EMPTY;
else if (oldType == SynthEngine::GridCell::DELAY) newType = SynthEngine::GridCell::REVERB;
else if (oldType == SynthEngine::GridCell::REVERB) newType = SynthEngine::GridCell::EMPTY;
} else if (e.button.button == SDL_BUTTON_RIGHT) {
c.rotation = (c.rotation + 1) % 4;
} else if (e.button.button == SDL_BUTTON_MIDDLE) {
@@ -479,14 +666,14 @@ int main(int argc, char* argv[]) {
if (newType != oldType) {
// If old type was DELAY, free its buffer
if (oldType == SynthEngine::GridCell::DELAY && c.buffer) {
if ((oldType == SynthEngine::GridCell::DELAY || oldType == SynthEngine::GridCell::REVERB) && c.buffer) {
delete[] c.buffer;
c.buffer = nullptr;
c.buffer_size = 0;
}
c.type = newType;
// If new type is DELAY, allocate its buffer
if (newType == SynthEngine::GridCell::DELAY) {
if (newType == SynthEngine::GridCell::DELAY || newType == SynthEngine::GridCell::REVERB) {
c.buffer_size = 2 * SAMPLE_RATE; // Max 2 seconds delay
c.buffer = new float[c.buffer_size](); // Allocate and zero-initialize
c.write_idx = 0; // Reset write index
@@ -595,7 +782,11 @@ int main(int argc, char* argv[]) {
}
} else if (e.type == SDL_KEYDOWN) {
if (e.key.repeat == 0) { // Ignore key repeats
if (e.key.keysym.scancode == SDL_SCANCODE_M) {
if (e.key.keysym.scancode == SDL_SCANCODE_INSERT) {
randomizeGrid();
} else if (e.key.keysym.scancode == SDL_SCANCODE_DELETE) {
clearGrid();
} else if (e.key.keysym.scancode == SDL_SCANCODE_M) {
auto_melody_enabled = !auto_melody_enabled;
engine.setGate(false); // Silence synth on mode change
current_key_scancode = 0;