1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//! Live state endpoint handlers.
use axum::{extract::State, Json};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Instant, SystemTime, UNIX_EPOCH};
use vibelang_core::FadeTarget;
use crate::{
models::{
ActiveFade, ActiveSequence, ActiveSynth, FadeTargetType, LiveState, LoopState, LoopStatus,
MeterLevel, MeterLevels, TimeSignature, TransportState,
},
AppState,
};
/// Build transport state with loop information
fn build_transport_state(s: &vibelang_core::State) -> TransportState {
let server_time_ms = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_millis() as u64)
.unwrap_or(0);
// Calculate loop_beats from longest active sequence
let loop_beats = s
.sequences
.values()
.filter(|seq| seq.playing)
.map(|seq| seq.config.length.to_f64())
.max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
let loop_beat = loop_beats.map(|lb| s.current_beat.to_f64() % lb);
TransportState {
bpm: s.tempo,
time_signature: TimeSignature {
numerator: s.time_sig.numerator,
denominator: s.time_sig.denominator,
},
running: s.playing,
current_beat: s.current_beat.to_f64(),
quantization_beats: 1.0, // Default quantization
loop_beats,
loop_beat,
server_time_ms: Some(server_time_ms),
}
}
/// GET /live - Get complete live state
pub async fn get_live_state(State(state): State<Arc<AppState>>) -> Json<LiveState> {
let live = state
.with_state(|s| {
let transport = build_transport_state(s);
// Build active synths from all running voice nodes
let active_synths: Vec<ActiveSynth> = s
.voices
.iter()
.flat_map(|(voice_id, vs)| {
vs.active_nodes.iter().map(move |node_id| ActiveSynth {
node_id: node_id.raw() as i32,
synthdef_name: vs.config.synthdef.clone(),
voice_name: Some(voice_id.raw().to_string()),
group_path: Some(vs.config.group.raw().to_string()),
created_at_beat: None,
})
})
.collect();
// Build active sequences
let active_sequences: Vec<ActiveSequence> = s
.sequences
.iter()
.filter(|(_, ss)| ss.playing)
.map(|(id, ss)| ActiveSequence {
name: id.raw().to_string(),
start_beat: 0.0, // Not tracked in core
current_position: ss.position.to_f64(),
loop_beats: ss.config.length.to_f64(),
iteration: None,
play_once: Some(!ss.looping),
})
.collect();
// Build active fades
let now = Instant::now();
let active_fades: Vec<ActiveFade> = s
.active_fades
.iter()
.enumerate()
.map(|(idx, af)| {
let (target_type, target_name) = match &af.config.target {
FadeTarget::Group(g) => (FadeTargetType::Group, g.raw().to_string()),
FadeTarget::Voice(v) => (FadeTargetType::Voice, v.raw().to_string()),
FadeTarget::Pattern(p) => (FadeTargetType::Group, p.raw().to_string()), // Map to group type
FadeTarget::Melody(m) => (FadeTargetType::Group, m.raw().to_string()),
FadeTarget::Effect(e) => (FadeTargetType::Effect, e.raw().to_string()),
};
let current = af.current_value(now, s.tempo);
let progress = if af.config.duration.to_beats() > 0.0 {
(current - af.start_value) / (af.config.to - af.start_value)
} else {
1.0
};
ActiveFade {
id: format!("fade_{}", idx),
name: None,
target_type,
target_name,
param_name: af.config.param.clone(),
start_value: af.start_value,
target_value: af.config.to,
current_value: Some(current),
duration_beats: af.config.duration.to_beats(),
start_beat: None,
progress: progress.clamp(0.0, 1.0) as f64,
}
})
.collect();
// Build active notes map (voice name -> active note numbers)
let active_notes: HashMap<String, Vec<u8>> = s
.voices
.iter()
.filter(|(_, vs)| !vs.note_nodes.is_empty())
.map(|(id, vs)| {
(
id.raw().to_string(),
vs.note_nodes.keys().copied().collect(),
)
})
.collect();
// Build patterns status
let patterns_status: HashMap<String, LoopStatus> = s
.patterns
.iter()
.map(|(id, ps)| {
(
id.raw().to_string(),
LoopStatus {
state: if ps.playing {
LoopState::Playing
} else {
LoopState::Stopped
},
start_beat: None,
stop_beat: None,
},
)
})
.collect();
// Build melodies status
let melodies_status: HashMap<String, LoopStatus> = s
.melodies
.iter()
.map(|(id, ms)| {
(
id.raw().to_string(),
LoopStatus {
state: if ms.playing {
LoopState::Playing
} else {
LoopState::Stopped
},
start_beat: None,
stop_beat: None,
},
)
})
.collect();
LiveState {
transport,
active_synths,
active_sequences,
active_fades,
active_notes: if active_notes.is_empty() {
None
} else {
Some(active_notes)
},
patterns_status: Some(patterns_status),
melodies_status: Some(melodies_status),
}
})
.await;
Json(live)
}
/// GET /live/transport - Get transport state only
pub async fn get_transport_state(State(state): State<Arc<AppState>>) -> Json<TransportState> {
let transport = state.with_state(build_transport_state).await;
Json(transport)
}
/// GET /live/fades - Get active fades
pub async fn get_active_fades(State(state): State<Arc<AppState>>) -> Json<Vec<ActiveFade>> {
let fades = state
.with_state(|s| {
let now = Instant::now();
s.active_fades
.iter()
.enumerate()
.map(|(idx, af)| {
let (target_type, target_name) = match &af.config.target {
FadeTarget::Group(g) => (FadeTargetType::Group, g.raw().to_string()),
FadeTarget::Voice(v) => (FadeTargetType::Voice, v.raw().to_string()),
FadeTarget::Pattern(p) => (FadeTargetType::Group, p.raw().to_string()),
FadeTarget::Melody(m) => (FadeTargetType::Group, m.raw().to_string()),
FadeTarget::Effect(e) => (FadeTargetType::Effect, e.raw().to_string()),
};
let current = af.current_value(now, s.tempo);
let progress = if af.config.duration.to_beats() > 0.0 {
(current - af.start_value) / (af.config.to - af.start_value)
} else {
1.0
};
ActiveFade {
id: format!("fade_{}", idx),
name: None,
target_type,
target_name,
param_name: af.config.param.clone(),
start_value: af.start_value,
target_value: af.config.to,
current_value: Some(current),
duration_beats: af.config.duration.to_beats(),
start_beat: None,
progress: progress.clamp(0.0, 1.0) as f64,
}
})
.collect::<Vec<_>>()
})
.await;
Json(fades)
}
/// GET /live/meters - Get meter levels for all groups
///
/// Returns real-time meter levels from the link synths.
/// The system_link_audio synthdef sends SendTrig messages at ~20Hz with meter data.
/// If meters haven't been updated in 200ms, they decay to 0.
pub async fn get_meters(State(state): State<Arc<AppState>>) -> Json<MeterLevels> {
let meters = state
.with_state(|s| {
s.groups
.iter()
.map(|(group_id, group_state)| {
// Look up meter levels by the link synth node ID
let (peak_l, peak_r, rms_l, rms_r) = group_state
.link_synth_node_id
.and_then(|node_id| s.meter_levels.get(&node_id))
.map(|m| m.decayed())
.unwrap_or((0.0, 0.0, 0.0, 0.0));
(
group_id.raw().to_string(),
MeterLevel {
peak_left: peak_l,
peak_right: peak_r,
rms_left: rms_l,
rms_right: rms_r,
},
)
})
.collect::<HashMap<_, _>>()
})
.await;
Json(meters)
}