Uppercase operator glyphs, more effects, drop embedded spectrum

- Operators are now UPPERCASE (orca-style): O A U V N sources, L H B SVF
  filters (with resonance), E G R envelope/gain/ringmod, S C F D shapers
  (soft/clip/fold/decimate), M mix, ! out. Digits = constants, lowercase = CC.
- TUI rewritten on crossterm reusing orca-rs's grid-view approach (centered
  canvas, per-glyph colour); the spectrum is gone - run spectrust on the side.
- Drops the ratatui and rustfft deps.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GLUwWE2KmFPzhKaf67tWbx
This commit is contained in:
Bendik Aagaard Lynghaug
2026-09-13 14:06:12 +02:00
co-authored by Claude Opus 4.8
parent d2778e169a
commit e68d50a78f
7 changed files with 305 additions and 500 deletions
+91 -95
View File
@@ -1,149 +1,145 @@
//! Terminal editor: the grid on the left, a live spectrum of the master on the
//! right, a status line below. Editing recompiles and hot-swaps the program.
//! Grid editor. crossterm renderer adapted from orca-rs's grid view (same
//! centered-canvas + per-glyph colour approach); no embedded scope - run a
//! spectrum analyzer (e.g. spectrust) on the side if you want one.
use std::io::{self, Write};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Duration;
use arc_swap::ArcSwap;
use ratatui::crossterm::event::{self, Event as CEvent, KeyCode, KeyEventKind, KeyModifiers};
use ratatui::prelude::*;
use ratatui::widgets::{Block, Borders, Paragraph};
use rtrb::Consumer;
use rustfft::{num_complex::Complex, FftPlanner};
use crossterm::cursor::{Hide, MoveTo, Show};
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use crossterm::style::{Attribute, Color, Print, SetAttribute, SetForegroundColor};
use crossterm::terminal::{
disable_raw_mode, enable_raw_mode, BeginSynchronizedUpdate, Clear, ClearType,
EndSynchronizedUpdate, EnterAlternateScreen, LeaveAlternateScreen,
};
use crossterm::{execute, queue, terminal};
use crate::dsp::{compile, Program};
use crate::grid::{Grid, H, W};
const FFT: usize = 1024;
// Foreground colour by glyph category. (orca-rs uses one Normal colour; galdr
// tints by role so a patch reads at a glance.)
fn glyph_color(c: char) -> Color {
match c {
'O' | 'A' | 'U' | 'V' | 'N' => Color::Cyan, // sources
'L' | 'H' | 'B' => Color::Blue, // filters
'S' | 'C' | 'F' | 'D' => Color::Yellow, // shapers
'E' | 'G' | 'R' | 'M' => Color::Green, // amp / mod
'!' => Color::Magenta, // output
'0'..='9' => Color::Grey, // constants
'a'..='z' => Color::DarkYellow, // CC registers
'.' => Color::DarkGrey, // empty
_ => Color::White,
}
}
pub fn run(
prog: Arc<ArcSwap<Program>>,
mut scope: Consumer<f32>,
voices: Arc<AtomicU32>,
mut grid: Grid,
latency: u32,
) -> std::io::Result<()> {
let mut terminal = ratatui::init();
let mut samples = vec![0.0f32; FFT];
let mut planner = FftPlanner::new();
let fft = planner.plan_fft_forward(FFT);
let hann: Vec<f32> = (0..FFT)
.map(|i| 0.5 * (1.0 - (std::f32::consts::TAU * i as f32 / FFT as f32).cos()))
.collect();
) -> io::Result<()> {
enable_raw_mode()?;
let mut out = io::stdout();
execute!(out, EnterAlternateScreen, Hide)?;
let (mut cx, mut cy) = (0usize, 0usize);
let res = main_loop(&mut out, &prog, &voices, &mut grid, latency, &mut cx, &mut cy);
execute!(out, Show, LeaveAlternateScreen)?;
disable_raw_mode()?;
res
}
fn main_loop(
out: &mut io::Stdout,
prog: &Arc<ArcSwap<Program>>,
voices: &Arc<AtomicU32>,
grid: &mut Grid,
latency: u32,
cx: &mut usize,
cy: &mut usize,
) -> io::Result<()> {
loop {
// Pull whatever the audio thread produced into the rolling window.
while let Ok(s) = scope.pop() {
samples.rotate_left(1);
samples[FFT - 1] = s;
}
let mut buf: Vec<Complex<f32>> =
samples.iter().zip(&hann).map(|(s, w)| Complex { re: s * w, im: 0.0 }).collect();
fft.process(&mut buf);
let mags: Vec<f32> = buf[..FFT / 2].iter().map(|c| c.norm()).collect();
draw(out, grid, *cx, *cy, voices.load(Ordering::Relaxed), latency)?;
let nv = voices.load(Ordering::Relaxed);
terminal.draw(|f| ui(f, &grid, cx, cy, &mags, nv, latency))?;
if event::poll(std::time::Duration::from_millis(33))? {
if let CEvent::Key(k) = event::read()? {
if event::poll(Duration::from_millis(33))? {
if let Event::Key(k) = event::read()? {
if k.kind != KeyEventKind::Press {
continue;
}
if k.code == KeyCode::Esc
|| (k.code == KeyCode::Char('c') && k.modifiers.contains(KeyModifiers::CONTROL))
{
break;
return Ok(());
}
let mut changed = false;
match k.code {
KeyCode::Left => cx = cx.saturating_sub(1),
KeyCode::Right => cx = (cx + 1).min(W - 1),
KeyCode::Up => cy = cy.saturating_sub(1),
KeyCode::Down => cy = (cy + 1).min(H - 1),
KeyCode::Left => *cx = cx.saturating_sub(1),
KeyCode::Right => *cx = (*cx + 1).min(W - 1),
KeyCode::Up => *cy = cy.saturating_sub(1),
KeyCode::Down => *cy = (*cy + 1).min(H - 1),
KeyCode::Backspace | KeyCode::Delete => {
grid.set(cx, cy, '.');
grid.set(*cx, *cy, '.');
changed = true;
}
KeyCode::Char(c) => {
grid.set(cx, cy, if c == ' ' { '.' } else { c });
grid.set(*cx, *cy, if c == ' ' { '.' } else { c });
changed = true;
*cx = (*cx + 1).min(W - 1);
}
_ => {}
}
if changed {
prog.store(Arc::new(compile(&grid)));
prog.store(Arc::new(compile(grid)));
}
}
}
}
ratatui::restore();
Ok(())
}
fn ui(f: &mut Frame, grid: &Grid, cx: usize, cy: usize, mags: &[f32], voices: u32, latency: u32) {
let root = Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).split(f.area());
let cols = Layout::horizontal([Constraint::Length((W + 3) as u16), Constraint::Min(10)]).split(root[0]);
fn draw(
out: &mut io::Stdout,
grid: &Grid,
cx: usize,
cy: usize,
voices: u32,
latency: u32,
) -> io::Result<()> {
let (tw, th) = terminal::size()?;
let off_x = (tw as usize).saturating_sub(W) / 2;
let off_y = (th as usize).saturating_sub(H + 1) / 2;
// Grid pane.
let mut lines = Vec::with_capacity(H);
queue!(out, BeginSynchronizedUpdate, Clear(ClearType::All))?;
for row in 0..H {
let mut spans = Vec::with_capacity(W);
queue!(out, MoveTo(off_x as u16, (off_y + row) as u16))?;
for col in 0..W {
let ch = grid.get(col, row);
let mut st = match ch {
'~' | '^' | '_' | 'v' | '*' => Style::default().fg(Color::Cyan),
'f' | 's' => Style::default().fg(Color::Yellow),
'e' => Style::default().fg(Color::Green),
'!' => Style::default().fg(Color::Magenta),
'.' => Style::default().fg(Color::DarkGray),
_ => Style::default().fg(Color::White),
};
let c = grid.get(col, row);
queue!(out, SetAttribute(Attribute::Reset))?;
if col == cx && row == cy {
st = st.add_modifier(Modifier::REVERSED);
}
spans.push(Span::styled(ch.to_string(), st));
}
lines.push(Line::from(spans));
}
f.render_widget(
Paragraph::new(lines).block(Block::default().borders(Borders::ALL).title(" galdr ")),
cols[0],
);
// Spectrum pane.
let area = cols[1];
let inner_w = area.width.saturating_sub(2) as usize;
let inner_h = area.height.saturating_sub(2) as usize;
let mut srows = vec![String::new(); inner_h];
if inner_w > 0 && inner_h > 0 {
let maxm = mags.iter().cloned().fold(1e-6f32, f32::max);
for x in 0..inner_w {
// log-ish bin mapping into the lower spectrum
let frac = (x as f32 / inner_w as f32).powf(1.6);
let bin = ((frac * mags.len() as f32) as usize).min(mags.len() - 1);
let mag = (mags[bin] / maxm).sqrt();
let filled = (mag * inner_h as f32) as usize;
for y in 0..inner_h {
srows[inner_h - 1 - y].push(if y < filled { '█' } else { ' ' });
queue!(out, SetAttribute(Attribute::Reverse))?;
} else {
queue!(out, SetForegroundColor(glyph_color(c)))?;
}
queue!(out, Print(c))?;
}
}
let scope_lines: Vec<Line> = srows.into_iter().map(|s| Line::from(Span::styled(s, Style::default().fg(Color::Green)))).collect();
f.render_widget(
Paragraph::new(scope_lines).block(Block::default().borders(Borders::ALL).title(" spectrum ")),
area,
);
// Status line.
// Status line beneath the grid.
let status = format!(
" voices {voices} · latency {latency}f · glyphs ~^_v* f e s ! · Esc quit "
);
f.render_widget(
Paragraph::new(status).style(Style::default().fg(Color::Black).bg(Color::Cyan)),
root[1],
"voices {voices} latency {latency}f O~ A^ U pulse V tri N noise | L H B filt | S C F D shape | E env G gain R ring M mix | ! out Esc quit"
);
let s: String = status.chars().take(tw as usize).collect();
queue!(
out,
MoveTo(0, th.saturating_sub(1)),
SetAttribute(Attribute::Reset),
SetForegroundColor(Color::DarkGrey),
Print(s)
)?;
queue!(out, EndSynchronizedUpdate)?;
out.flush()
}