|
| 1 | +# Kord AI Coding Agent Instructions |
| 2 | + |
| 3 | +Kord is a music theory library and CLI tool with ML capabilities, built in Rust with multi-platform support (native, WASM, WASI). |
| 4 | + |
| 5 | +## Architecture Overview |
| 6 | + |
| 7 | +**Workspace Structure**: Cargo workspace with two main crates: |
| 8 | +- `kord/`: Core library (`klib`) + CLI binary with music theory, audio analysis, and ML training |
| 9 | +- `kord-web/`: Leptos + Axum web app running as WASI service with embedded frontend |
| 10 | + |
| 11 | +**Core Library Design**: Music theory primitives built around `Note`, `Chord`, `Modifier` traits with extensive parsing via Pest grammar (`chord.pest`). The `Chordable` trait provides builder pattern for chord construction. |
| 12 | + |
| 13 | +**Feature Flag Architecture**: Heavily feature-gated for different deployment targets: |
| 14 | +- `cli`: CLI functionality (required for binary) |
| 15 | +- `analyze = ["analyze_mic", "analyze_file"]`: Audio processing with FFT/spectrum analysis |
| 16 | +- `ml = ["ml_train", "ml_infer"]`: Machine learning with Burn framework |
| 17 | +- `ml_gpu`: GPU acceleration via burn-tch/burn-wgpu |
| 18 | +- `wasm`/`wasi`: WebAssembly compilation targets |
| 19 | +- `audio`: Audio playback via rodio |
| 20 | + |
| 21 | +## Key Development Workflows |
| 22 | + |
| 23 | +**Platform-Specific Builds**: |
| 24 | +```bash |
| 25 | +# Native CLI (default features) |
| 26 | +cargo build |
| 27 | + |
| 28 | +# Web frontend + backend |
| 29 | +cd kord-web && cargo leptos build --release |
| 30 | +LEPTOS_OUTPUT_NAME=kord-web cargo build --lib --release --target wasm32-wasip2 --no-default-features --features ssr |
| 31 | + |
| 32 | +# WASI binary for Wasmer |
| 33 | +cargo wasi build --release --no-default-features --features wasi --features cli --features ml_infer --features analyze_file |
| 34 | + |
| 35 | +# WASM for NPM |
| 36 | +wasm-pack build --features ml_infer --features wasm |
| 37 | +``` |
| 38 | + |
| 39 | +**Linux Dependencies**: Always install `libasound2-dev` for ALSA support before building (see CI workflow). |
| 40 | + |
| 41 | +**ML Training Pipeline**: Uses Burn framework with configurable backends (CPU/GPU). Training config in `TrainConfig` struct controls hyperparameters, data simulation, and model architecture. |
| 42 | + |
| 43 | +## Project-Specific Patterns |
| 44 | + |
| 45 | +**Error Handling**: Uses `anyhow::Result` aliased as `Res<T>` and `Void` for `Result<(), Error>` throughout. |
| 46 | + |
| 47 | +**Static Data**: Extensive use of `LazyLock` for computed static arrays (e.g., `ALL_PITCHES`, `KNOWN_MODIFIER_SETS`). See `kord/src/core/modifier.rs` for patterns. |
| 48 | + |
| 49 | +**Parser Architecture**: Pest grammar in `chord.pest` with hand-written parser logic in `core/parser.rs`. Follow existing patterns for extending chord notation. |
| 50 | + |
| 51 | +**Multi-Target Compilation**: Heavy use of `#[cfg(feature = "...")]` guards. New features should follow the hierarchical feature flag pattern (base → specific implementations). |
| 52 | + |
| 53 | +**Audio Processing**: FFT-based analysis in `analyze/` module. Frequency space standardized at 8192 bins covering up to C9. |
| 54 | + |
| 55 | +## Integration Points |
| 56 | + |
| 57 | +**Frontend-Backend**: Leptos SSR with rust-embed for static assets. Backend runs as WASI service with custom request/response translation layer. |
| 58 | + |
| 59 | +**ML Pipeline**: Binary samples stored in `samples/` and `noise/` directories. Training uses `KordItem` structs with frequency space + label format. |
| 60 | + |
| 61 | +**Build System**: Custom `build.rs` sets platform-specific cfg flags. Uses workspace profiles including `wasm-release` for optimized WASM builds. |
| 62 | + |
| 63 | +**External Dependencies**: Rodio for audio, Symphonia for file formats, Burn for ML, Leptos for web, Pest for parsing. |
| 64 | + |
| 65 | +## Critical Conventions |
| 66 | + |
| 67 | +- Use `#[coverage(off)]` for non-testable code (UI, training loops) |
| 68 | +- ML constants in `ml/base/mod.rs` (FREQUENCY_SPACE_SIZE, NUM_CLASSES, etc.) |
| 69 | +- Follow existing trait patterns for music theory types (HasStaticName, Parsable, etc.) |
| 70 | +- Feature flags control compilation - always check dependencies when adding new functionality |
0 commit comments