diff --git a/README.md b/README.md index 5e0b6e5..6e3d366 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,10 @@ galdr [--latency ] [--poly ] [--rate ] [--midi-name ] galdr creates a virtual MIDI input port named **galdr**; connect orca or any sequencer/DAW to it. `--midi-name` connects to an existing port instead. -Cursor keys move; type a glyph to place it; Backspace clears; Esc quits. + +Keys: cursor keys move; type a glyph to place it; Backspace clears; `?` toggles +help; Esc quits. Resize the grid with `(` / `)` (width) and `_` / `+` (height) +by one ruler division, or Ctrl+arrows by one cell (orca's bindings). Runtime deps: PipeWire and ALSA (MIDI). Built in Rust; reuses the native PipeWire path from spectrust. diff --git a/src/dsp.rs b/src/dsp.rs index acbfa9a..0d5593d 100644 --- a/src/dsp.rs +++ b/src/dsp.rs @@ -9,7 +9,7 @@ //! params, lowercase a-z read MIDI CC (a = CC 0). A neighbour to the left/right //! supplies a parameter; `M` instead reads the left cell as a second signal. -use crate::grid::{Grid, H, SLOTS, W}; +use crate::grid::{Grid, MAX_W, SLOTS}; pub const MAX_STATE: usize = 4 * SLOTS; pub const TAU: f32 = std::f32::consts::TAU; @@ -110,20 +110,23 @@ pub fn compile(grid: &Grid) -> Program { let mut nodes = Vec::new(); let mut st = 0usize; let mut has_env = false; - for row in 0..H { - for col in 0..W { + for row in 0..grid.h { + for col in 0..grid.w { let Some((op, ssize)) = op_of(grid.get(col, row)) else { continue }; if matches!(op, Op::Env) { has_env = true; } - let input = - if row > 0 && occupied(grid.get(col, row - 1)) { Some((row - 1) * W + col) } else { None }; - let lsig = if col > 0 { Some(row * W + col - 1) } else { None }; + let input = if row > 0 && occupied(grid.get(col, row - 1)) { + Some((row - 1) * MAX_W + col) + } else { + None + }; + let lsig = if col > 0 { Some(row * MAX_W + col - 1) } else { None }; let left = if col > 0 { param_of(grid.get(col - 1, row)) } else { Param::None }; - let right = if col + 1 < W { param_of(grid.get(col + 1, row)) } else { Param::None }; + let right = if col + 1 < grid.w { param_of(grid.get(col + 1, row)) } else { Param::None }; let this_st = st; st = (st + ssize).min(MAX_STATE - 4); - nodes.push(Node { op, out: row * W + col, input, lsig, left, right, st: this_st }); + nodes.push(Node { op, out: row * MAX_W + col, input, lsig, left, right, st: this_st }); } } Program { nodes, has_env } diff --git a/src/grid.rs b/src/grid.rs index 2c3fc1c..a76415a 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -1,31 +1,55 @@ -//! The fixed-size character grid that is the editor surface. +//! The character grid — the editor surface. Logical size (`w`×`h`) is +//! resizable up to the max; cells are stored at a fixed `MAX_W` stride so a +//! resize never remaps existing content and compiled slot indices stay stable. -pub const W: usize = 24; -pub const H: usize = 16; -pub const SLOTS: usize = W * H; +pub const MAX_W: usize = 40; +pub const MAX_H: usize = 24; +pub const SLOTS: usize = MAX_W * MAX_H; +pub const DEFAULT_W: usize = 24; +pub const DEFAULT_H: usize = 16; +pub const RULER: usize = 8; #[derive(Clone)] pub struct Grid { + pub w: usize, + pub h: usize, cells: Vec, } impl Grid { pub fn new() -> Self { - Grid { cells: vec!['.'; SLOTS] } + Grid { w: DEFAULT_W, h: DEFAULT_H, cells: vec!['.'; SLOTS] } } #[inline] pub fn get(&self, col: usize, row: usize) -> char { - if col < W && row < H { - self.cells[row * W + col] + if col < self.w && row < self.h { + self.cells[row * MAX_W + col] } else { '.' } } pub fn set(&mut self, col: usize, row: usize, c: char) { - if col < W && row < H { - self.cells[row * W + col] = c; + if col < self.w && row < self.h { + self.cells[row * MAX_W + col] = c; } } + + /// Resize to nh×nw, clamped to [1, MAX]. Content keeps its position. + pub fn resize(&mut self, nh: usize, nw: usize) { + self.h = nh.clamp(1, MAX_H); + self.w = nw.clamp(1, MAX_W); + } +} + +/// orca-c's ruler snap: resize by one ruler division, snapped to a +/// ruler-aligned size (1, 9, 17, ...) rather than a plain +/-1. +pub fn ruler_snap(size: usize, delta_rulers: isize) -> usize { + if size == 0 { + return if delta_rulers > 0 { RULER * delta_rulers as usize } else { 1 }; + } + let size = if delta_rulers < 0 { size + RULER - 1 } else { size }; + let n = ((size as isize - 1) / RULER as isize + delta_rulers).max(0); + (RULER as isize * n + 1) as usize } diff --git a/src/tui.rs b/src/tui.rs index 823002c..d531bfc 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -1,7 +1,7 @@ //! Grid editor. crossterm renderer adapted from orca-rs's grid view: a centered -//! canvas with per-glyph colour and a change-diff so only cells that actually -//! change get repainted (a full clear+redraw each frame flickers over SSH on -//! terminals that ignore synchronized-update, e.g. macOS Terminal). `?` toggles +//! canvas with per-glyph colour, ruler marks, resize bindings, and a change-diff +//! so only cells that actually change repaint (a full clear+redraw each frame +//! flickers over SSH on terminals that ignore synchronized-update). `?` toggles //! a detailed help overlay; run a spectrum analyzer on the side if you want one. use std::io::{self, Write}; @@ -19,7 +19,7 @@ use crossterm::terminal::{ use crossterm::{execute, queue, terminal}; use crate::dsp::{compile, Program}; -use crate::grid::{Grid, H, W}; +use crate::grid::{ruler_snap, Grid, RULER}; const HELP: &[&str] = &[ "galdr — grid synthesizer", @@ -40,6 +40,7 @@ const HELP: &[&str] = &[ "ROUTING M mix — adds the cell to its LEFT ! output to master", "", "KEYS arrows move · type a glyph to place · Backspace clears", + " ( ) width -/+ _ + height -/+ Ctrl+arrows resize by one", " ? toggle this help · Esc quit", ]; @@ -58,7 +59,6 @@ fn glyph_color(c: char) -> Color { '!' => Color::Magenta, '0'..='9' => Color::Grey, 'a'..='z' => Color::DarkYellow, - '.' => Color::DarkGrey, _ => Color::White, } } @@ -68,13 +68,24 @@ struct Renderer { status: String, tw: u16, th: u16, + gw: usize, + gh: usize, help: bool, init: bool, } impl Renderer { fn new() -> Self { - Renderer { cells: Vec::new(), status: String::new(), tw: 0, th: 0, help: false, init: false } + Renderer { + cells: Vec::new(), + status: String::new(), + tw: 0, + th: 0, + gw: 0, + gh: 0, + help: false, + init: false, + } } } @@ -102,6 +113,8 @@ pub fn run( continue; } let ctrl = k.modifiers.contains(KeyModifiers::CONTROL); + let mut resized = false; + let mut edited = false; match k.code { KeyCode::Esc => { if help { @@ -112,22 +125,61 @@ pub fn run( } KeyCode::Char('c') if ctrl => return Ok(()), KeyCode::Char('?') => help = !help, - _ if help => {} // any other key: ignore while help is open + _ if help => {} + KeyCode::Char('(') => { + grid.resize(grid.h, ruler_snap(grid.w, -1)); + resized = true; + } + KeyCode::Char(')') => { + grid.resize(grid.h, ruler_snap(grid.w, 1)); + resized = true; + } + KeyCode::Char('_') => { + grid.resize(ruler_snap(grid.h, -1), grid.w); + resized = true; + } + KeyCode::Char('+') => { + grid.resize(ruler_snap(grid.h, 1), grid.w); + resized = true; + } + KeyCode::Left if ctrl => { + grid.resize(grid.h, grid.w.saturating_sub(1)); + resized = true; + } + KeyCode::Right if ctrl => { + grid.resize(grid.h, grid.w + 1); + resized = true; + } + KeyCode::Up if ctrl => { + grid.resize(grid.h.saturating_sub(1), grid.w); + resized = true; + } + KeyCode::Down if ctrl => { + grid.resize(grid.h + 1, grid.w); + resized = true; + } KeyCode::Left => cx = cx.saturating_sub(1), - KeyCode::Right => cx = (cx + 1).min(W - 1), + KeyCode::Right => cx = (cx + 1).min(grid.w - 1), KeyCode::Up => cy = cy.saturating_sub(1), - KeyCode::Down => cy = (cy + 1).min(H - 1), + KeyCode::Down => cy = (cy + 1).min(grid.h - 1), KeyCode::Backspace | KeyCode::Delete => { grid.set(cx, cy, '.'); - prog.store(Arc::new(compile(&grid))); + edited = true; } KeyCode::Char(c) => { grid.set(cx, cy, if c == ' ' { '.' } else { c }); - prog.store(Arc::new(compile(&grid))); - cx = (cx + 1).min(W - 1); + edited = true; + cx = (cx + 1).min(grid.w - 1); } _ => {} } + if resized { + cx = cx.min(grid.w - 1); + cy = cy.min(grid.h - 1); + } + if resized || edited { + prog.store(Arc::new(compile(&grid))); + } } })(); @@ -149,15 +201,20 @@ fn draw( ) -> io::Result<()> { let (tw, th) = terminal::size()?; - // Full repaint only on first frame, resize, or a help toggle - never every - // frame (that is the flicker). - let full = !r.init || tw != r.tw || th != r.th || help != r.help; + let full = !r.init + || tw != r.tw + || th != r.th + || help != r.help + || grid.w != r.gw + || grid.h != r.gh; if full { queue!(out, Clear(ClearType::All))?; - r.cells = vec![('\0', Cs::Fg(Color::Reset)); W * H]; + r.cells = vec![('\0', Cs::Fg(Color::Reset)); grid.w * grid.h]; r.status.clear(); r.tw = tw; r.th = th; + r.gw = grid.w; + r.gh = grid.h; r.help = help; r.init = true; } @@ -179,27 +236,40 @@ fn draw( return out.flush(); } - let off_x = (tw as usize).saturating_sub(W) / 2; - let off_y = (th as usize).saturating_sub(H + 1) / 2; - for row in 0..H { - for col in 0..W { - let c = grid.get(col, row); - let cs = if col == cx && row == cy { Cs::Cursor } else { Cs::Fg(glyph_color(c)) }; - let idx = row * W + col; - if r.cells[idx] == (c, cs) { + let off_x = (tw as usize).saturating_sub(grid.w) / 2; + let off_y = (th as usize).saturating_sub(grid.h + 1) / 2; + for row in 0..grid.h { + for col in 0..grid.w { + let raw = grid.get(col, row); + // ruler mark on empty intersections, for orientation while sizing + let disp = if raw == '.' { + if row % RULER == 0 && col % RULER == 0 { '+' } else { '.' } + } else { + raw + }; + let cs = if col == cx && row == cy { + Cs::Cursor + } else if raw == '.' { + Cs::Fg(Color::DarkGrey) + } else { + Cs::Fg(glyph_color(raw)) + }; + let idx = row * grid.w + col; + if r.cells[idx] == (disp, cs) { continue; } - r.cells[idx] = (c, cs); + r.cells[idx] = (disp, cs); queue!(out, MoveTo((off_x + col) as u16, (off_y + row) as u16), SetAttribute(Attribute::Reset))?; match cs { Cs::Cursor => queue!(out, SetAttribute(Attribute::Reverse))?, Cs::Fg(color) => queue!(out, SetForegroundColor(color))?, } - queue!(out, Print(c))?; + queue!(out, Print(disp))?; } } - let status = format!("voices {voices} latency {latency}f ? help Esc quit"); + let status = + format!("voices {voices} {}x{} latency {latency}f ? help Esc quit", grid.w, grid.h); if status != r.status { r.status = status.clone(); let pad: String = format!("{status: