Skip to main content

midi_flux_bridge/
lib.rs

1//! # midi-flux-bridge
2//!
3//! Bridges tensor-midi timing schedules to FLUX coordination bytecode.
4//! Agent dialogue timing computed as tensor contractions becomes executable
5//! coordination instructions.
6//!
7//! ## Modules
8//!
9//! - [`tensor_schedule`] — Import tensor-midi-style timing schedules
10//! - [`flux_bytecode`] — Generate FLUX timing instructions
11//! - [`bridge`] — Core bridge: TensorSchedule → Vec<FluxOp>
12//! - [`conductor`] — Execute FLUX timing bytecode
13//! - [`swing`] — Swing timing computation and groove templates
14//! - [`alignment`] — Agent alignment verification and drift detection
15
16pub mod alignment;
17pub mod bridge;
18pub mod conductor;
19pub mod flux_bytecode;
20pub mod swing;
21pub mod tensor_schedule;
22
23use serde::{Deserialize, Serialize};
24
25/// Timing parameters for a single agent in the coordination schedule.
26#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
27pub struct AgentTiming {
28    pub agent_id: String,
29    pub bpm: f64,
30    pub swing: f64,
31    pub offset_ms: f64,
32    pub cadence: Cadence,
33}
34
35/// Cadence pattern for an agent's timing.
36#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
37pub enum Cadence {
38    /// Regular fixed interval in milliseconds.
39    Regular { interval_ms: f64 },
40    /// Custom beat pattern where each value is a duration in ms.
41    Pattern { beats: Vec<f64> },
42    /// Reactive timing with a minimum gap between responses.
43    Reactive { min_gap_ms: f64 },
44}
45
46/// A single FLUX timing instruction.
47#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
48pub enum FluxOp {
49    /// Wait for the specified duration in milliseconds.
50    Wait(f64),
51    /// Signal a specific agent to act.
52    Signal(String),
53    /// Synchronization barrier; block until `n` agents have signaled.
54    SyncBarrier(usize),
55    /// Change the tempo to a new BPM.
56    TempoChange(f64),
57    /// Set the cadence for a named agent.
58    CadenceSet(String, Cadence),
59    /// Halt execution.
60    Halt,
61}
62
63/// A complete FLUX schedule with metadata.
64#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
65pub struct FluxSchedule {
66    pub ops: Vec<FluxOp>,
67    pub total_duration_ms: f64,
68    pub agent_count: usize,
69}
70
71/// Mutable state of the FLUX conductor during execution.
72#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
73pub struct ConductorState {
74    pub time_ms: f64,
75    pub active_agents: Vec<String>,
76    pub pending_barriers: usize,
77    pub current_bpm: f64,
78}
79
80impl ConductorState {
81    /// Create a new conductor state at time zero with the given BPM.
82    pub fn new(bpm: f64) -> Self {
83        Self {
84            time_ms: 0.0,
85            active_agents: Vec::new(),
86            pending_barriers: 0,
87            current_bpm: bpm,
88        }
89    }
90}
91
92impl FluxSchedule {
93    /// Create an empty schedule.
94    pub fn empty() -> Self {
95        Self {
96            ops: Vec::new(),
97            total_duration_ms: 0.0,
98            agent_count: 0,
99        }
100    }
101}
102
103impl Default for FluxSchedule {
104    fn default() -> Self {
105        Self::empty()
106    }
107}
108
109impl Default for ConductorState {
110    fn default() -> Self {
111        Self::new(120.0)
112    }
113}