Tensor field MIDI engine for reactive improvisation.
Rooted in the Chinese mathematical tradition β I Ching hexagrams, Taoist conservation of chi, the Book of Changes as a tensor field of possibility. Every conversation is music. Every agent has a rhythm.
lau-tensor-midi models a multi-agent conversation as a live musical score. Each agent has a rhythmic personality (cadence), and their interactions β excitement, pushback, questions, silence β are represented as nudges that perturb a shared conversation tensor. The system decides when each agent "speaks" (plays notes), what notes they play, and how the tempo flows β all governed by a conservation-of-energy invariant inspired by Taoist physics.
Think of it as a jazz rhythm section for AI agents: nobody calls the tune, the music emerges.
A conversation tensor T β β^{B Γ A Γ N} encodes the full musical state:
- B = bars (time)
- A = agents (who's playing)
- N = note dimensions (pitch, velocity, duration, onset, channel)
Energy flows through this tensor like chi through a body. The total energy is budgeted and conserved β no agent can play more than the field allows. BPM adapts to energy (flow follows energy, wu-wei). Entropy measures how evenly distributed the musical ideas are across time.
Add to your Cargo.toml:
[dependencies]
lau-tensor-midi = "0.1.0"Or use cargo:
cargo add lau-tensor-midiRequires Rust 2021 edition. Only external dependency: serde (with derive).
use lau_tensor_midi::*;
// Create the engine with 4 agents, 8 bars, and an energy budget of 500
let mut engine = ReactiveImprovEngine::new(default_cadences(), 8, 500.0);
// Send a nudge: the Architect is excited and the Implementer feels it
engine.nudge(Nudge::new(
NudgeType::Excitement(0.7),
"Architect",
Some("Implementer".into()),
0.9,
0,
));
// Compute and commit each agent's musical contribution
for agent in &["Architect", "Implementer", "Critic", "Historian"] {
engine.compute_draft(agent);
engine.commit_draft(agent);
}
// Advance the simulation
for _ in 0..960 {
engine.tick();
}
// See the state of the conversation
println!("{}", engine.summary());Output looks like:
π΅ Conversation Field: 4 agents, 32 notes
β‘ Energy: 8.432 / budget 500.000
π₯ BPM: 82.4 (flowing)
π₯ Entropy: 1.832 bits
π« Nudges: 1 active
π Drafts: 0 pending notes
π Tick: 960
| Type | Description |
|---|---|
TensorMidiId |
Opaque newtype identifier for tensor entities. Hashable, comparable, serializable. |
NoteVector |
A single note event: pitch, velocity, duration, onset, channel. Has an energy() method. |
AgentCadence |
The rhythmic fingerprint of an agent: BPM, swing, articulation, energy baseline, variation. |
ConversationTensor |
The tensor field: bars Γ agents Γ notes. Tracks energy, entropy, BPM. |
Nudge / NudgeType |
Reactive signals that perturb the field (Excitement, Pushback, Question, TopicShift, Silence). |
ReactiveImprovEngine |
The full system: tensor + nudges + draft management + conservation budget. |
let note = NoteVector::new(60, 100, 480, 0, 0); // C4, velocity 100, quarter note, tick 0, channel 0
let energy = note.energy(); // velocity/127 * duration/480
let rest = NoteVector::silence(100, 480, 2); // zero-energy restlet cadence = AgentCadence::new("my-agent", 120.0);
// .tick_interval() β ms per tick
// .swing_offset(beat) β timing offset for swing feel
// .next_onset(tick, energy) β when agent should speak next
// .should_speak(now, last_spoke) β boolean decisionlet tensor = ConversationTensor::new(agents, 8);
// .energy_at(tick) β total energy at a point in time
// .agent_energy(id, bar) β per-agent energy in a bar
// .bar_energy(bar) β total energy in a bar
// .tensor_entropy() β Shannon entropy of note distribution
// .adapt_bpm(energy) β Taoist BPM flow
// .is_conserved(tol) β conservation invariant checklet mut engine = ReactiveImprovEngine::new(agents, 8, 500.0);
engine.nudge(/* ... */);
engine.compute_draft("Architect"); // generates notes
engine.commit_draft("Architect"); // commits if budget allows
engine.tick(); // advance simulation
engine.summary(); // human-readable state| Function | Agent | BPM | Swing | Articulation | Energy | Variation |
|---|---|---|---|---|---|---|
architect_cadence() |
Architect | 80 | 0.3 | 0.7 (legato) | 0.6 | 0.4 |
implementer_cadence() |
Implementer | 120 | 0.1 | 0.3 (staccato) | 0.8 | 0.2 |
critic_cadence() |
Critic | 90 | 0.5 | 0.5 | 0.4 | 0.6 |
historian_cadence() |
Historian | 70 | 0.7 | 0.9 (very legato) | 0.3 | 0.8 |
default_cadences() returns all four.
Nudge β energy_delta β adapt_bpm β compute_draft β commit_draft (if budget allows) β tick
- Nudges arrive from agents or the system, each carrying an energy delta (positive or negative).
- BPM adapts via a sigmoid mapping: low energy β 60 BPM, high energy β 120 BPM. The transition is smoothed (80/20 blend) to avoid jarring tempo jumps.
- Draft computation: an agent's energy (baseline + nudge modifiers) determines how many notes they produce (1β16). Pitches are drawn from a pentatonic scale rooted at C4 β the five-note Chinese scale (C D E G A). Swing offsets are applied to off-beat notes.
- Commit gate: a draft is only committed if the total energy stays within the conservation budget. If not, the draft is rejected.
- Tick: old nudges are cleaned up (older than 4 bars), BPM continues to flow.
Notes are generated from the five-note Chinese pentatonic scale: C, D, E, G, A (intervals 0, 2, 4, 7, 9). Octave shifts happen when the scale wraps around, keeping pitches in MIDI range [0, 127].
The system uses no external RNG. "Randomness" for variation and should_speak decisions comes from deterministic hash functions (Knuth multiplicative hash: tick Γ 2654435761). This means the same conversation state always produces the same output β reproducible improvisation.
Where v = velocity [0, 127] and d = duration in ticks (480 = one quarter note).
The exponential moving average ensures smooth tempo evolution β the system never forces, it flows (wu-wei).
Where
Off-beat positions get delayed:
Where
The total energy of all committed notes must not exceed the conservation budget. Drafts that would violate this are rejected.
Where
71 tests covering all types and their interactions:
TensorMidiId: construction, equality, hashing, cloning, display, serdeNoteVector: energy calculations, silence, serdeAgentCadence: tick intervals, swing offsets, onset timing, speak decisions, BPM clampingConversationTensor: energy at tick/bar, entropy (empty, uniform, concentrated), conservation, BPM adaptation, serdeNudge: energy deltas for all types, cadence effects, strength clampingReactiveImprovEngine: full pipeline (nudge β draft β commit β tick), budget enforcement, topic shifts, agent should-speak logic- Pre-built cadences: value verification
Run tests:
cargo testMIT