1#[cfg(feature = "audio")]
9use std::sync::Arc;
10use std::sync::RwLock;
11use std::time::{SystemTime, UNIX_EPOCH};
12use wavecraft_bridge::{BridgeError, InMemoryParameterHost, ParameterHost};
13use wavecraft_protocol::{
14 AudioRuntimePhase, AudioRuntimeStatus, MeterFrame, MeterUpdateNotification, OscilloscopeFrame,
15 ParameterInfo,
16};
17
18#[cfg(feature = "audio")]
19use crate::audio::atomic_params::AtomicParameterBridge;
20
21pub struct DevServerHost {
33 inner: InMemoryParameterHost,
34 latest_meter_frame: Arc<RwLock<Option<MeterFrame>>>,
35 latest_oscilloscope_frame: Arc<RwLock<Option<OscilloscopeFrame>>>,
36 audio_status: Arc<RwLock<AudioRuntimeStatus>>,
37 #[cfg(feature = "audio")]
38 param_bridge: Option<Arc<AtomicParameterBridge>>,
39}
40
41impl DevServerHost {
42 #[cfg_attr(feature = "audio", allow(dead_code))]
51 pub fn new(parameters: Vec<ParameterInfo>) -> Self {
52 let inner = InMemoryParameterHost::new(parameters);
53 let latest_meter_frame = Arc::new(RwLock::new(None));
54 let latest_oscilloscope_frame = Arc::new(RwLock::new(None));
55 let audio_status = Arc::new(RwLock::new(AudioRuntimeStatus {
56 phase: AudioRuntimePhase::Disabled,
57 diagnostic: None,
58 sample_rate: None,
59 buffer_size: None,
60 updated_at_ms: now_millis(),
61 }));
62
63 Self {
64 inner,
65 latest_meter_frame,
66 latest_oscilloscope_frame,
67 audio_status,
68 #[cfg(feature = "audio")]
69 param_bridge: None,
70 }
71 }
72
73 #[cfg(feature = "audio")]
78 pub fn with_param_bridge(
79 parameters: Vec<ParameterInfo>,
80 bridge: Arc<AtomicParameterBridge>,
81 ) -> Self {
82 let inner = InMemoryParameterHost::new(parameters);
83 let latest_meter_frame = Arc::new(RwLock::new(None));
84 let latest_oscilloscope_frame = Arc::new(RwLock::new(None));
85 let audio_status = Arc::new(RwLock::new(AudioRuntimeStatus {
86 phase: AudioRuntimePhase::Disabled,
87 diagnostic: None,
88 sample_rate: None,
89 buffer_size: None,
90 updated_at_ms: now_millis(),
91 }));
92
93 Self {
94 inner,
95 latest_meter_frame,
96 latest_oscilloscope_frame,
97 audio_status,
98 param_bridge: Some(bridge),
99 }
100 }
101
102 pub fn replace_parameters(&self, new_params: Vec<ParameterInfo>) -> Result<(), String> {
113 self.inner.replace_parameters(new_params)
114 }
115
116 pub fn set_latest_meter_frame(&self, update: &MeterUpdateNotification) {
118 let mut meter = self
119 .latest_meter_frame
120 .write()
121 .expect("latest_meter_frame lock poisoned");
122 *meter = Some(MeterFrame {
123 peak_l: update.left_peak,
124 peak_r: update.right_peak,
125 rms_l: update.left_rms,
126 rms_r: update.right_rms,
127 timestamp: update.timestamp_us,
128 });
129 }
130
131 pub fn set_latest_oscilloscope_frame(&self, frame: OscilloscopeFrame) {
133 let mut oscilloscope = self
134 .latest_oscilloscope_frame
135 .write()
136 .expect("latest_oscilloscope_frame lock poisoned");
137 *oscilloscope = Some(frame);
138 }
139
140 pub fn set_audio_status(&self, status: AudioRuntimeStatus) {
142 let mut current = self
143 .audio_status
144 .write()
145 .expect("audio_status lock poisoned");
146 *current = status;
147 }
148}
149
150impl ParameterHost for DevServerHost {
151 fn get_parameter(&self, id: &str) -> Option<ParameterInfo> {
152 self.inner.get_parameter(id)
153 }
154
155 fn set_parameter(&self, id: &str, value: f32) -> Result<(), BridgeError> {
156 let result = self.inner.set_parameter(id, value);
157
158 #[cfg(feature = "audio")]
160 if result.is_ok()
161 && let Some(ref bridge) = self.param_bridge
162 {
163 bridge.write(id, value);
164 }
165
166 result
167 }
168
169 fn get_all_parameters(&self) -> Vec<ParameterInfo> {
170 self.inner.get_all_parameters()
171 }
172
173 fn get_meter_frame(&self) -> Option<MeterFrame> {
174 self.latest_meter_frame
175 .read()
176 .expect("latest_meter_frame lock poisoned")
177 .clone()
178 }
179
180 fn get_oscilloscope_frame(&self) -> Option<OscilloscopeFrame> {
181 self.latest_oscilloscope_frame
182 .read()
183 .expect("latest_oscilloscope_frame lock poisoned")
184 .clone()
185 }
186
187 fn request_resize(&self, _width: u32, _height: u32) -> bool {
188 self.inner.request_resize(_width, _height)
189 }
190
191 fn get_audio_status(&self) -> Option<AudioRuntimeStatus> {
192 Some(
193 self.audio_status
194 .read()
195 .expect("audio_status lock poisoned")
196 .clone(),
197 )
198 }
199}
200
201fn now_millis() -> u64 {
202 SystemTime::now()
203 .duration_since(UNIX_EPOCH)
204 .map_or(0, |duration| duration.as_millis() as u64)
205}
206
207#[cfg(test)]
208mod tests {
209 use super::*;
210 use wavecraft_protocol::ParameterType;
211
212 fn test_params() -> Vec<ParameterInfo> {
213 vec![
214 ParameterInfo {
215 id: "gain".to_string(),
216 name: "Gain".to_string(),
217 param_type: ParameterType::Float,
218 value: 0.5,
219 default: 0.5,
220 min: 0.0,
221 max: 1.0,
222 unit: Some("dB".to_string()),
223 group: Some("Input".to_string()),
224 },
225 ParameterInfo {
226 id: "mix".to_string(),
227 name: "Mix".to_string(),
228 param_type: ParameterType::Float,
229 value: 1.0,
230 default: 1.0,
231 min: 0.0,
232 max: 1.0,
233 unit: Some("%".to_string()),
234 group: None,
235 },
236 ]
237 }
238
239 #[test]
240 fn test_get_parameter() {
241 let host = DevServerHost::new(test_params());
242
243 let param = host.get_parameter("gain").expect("should find gain");
244 assert_eq!(param.id, "gain");
245 assert_eq!(param.name, "Gain");
246 assert!((param.value - 0.5).abs() < f32::EPSILON);
247 }
248
249 #[test]
250 fn test_get_parameter_not_found() {
251 let host = DevServerHost::new(test_params());
252 assert!(host.get_parameter("nonexistent").is_none());
253 }
254
255 #[test]
256 fn test_set_parameter() {
257 let host = DevServerHost::new(test_params());
258
259 host.set_parameter("gain", 0.75).expect("should set gain");
260
261 let param = host.get_parameter("gain").expect("should find gain");
262 assert!((param.value - 0.75).abs() < f32::EPSILON);
263 }
264
265 #[test]
266 fn test_set_parameter_invalid_id() {
267 let host = DevServerHost::new(test_params());
268 let result = host.set_parameter("invalid", 0.5);
269 assert!(result.is_err());
270 }
271
272 #[test]
273 fn test_set_parameter_out_of_range() {
274 let host = DevServerHost::new(test_params());
275
276 let result = host.set_parameter("gain", 1.5);
277 assert!(result.is_err());
278
279 let result = host.set_parameter("gain", -0.1);
280 assert!(result.is_err());
281 }
282
283 #[test]
284 fn test_get_all_parameters() {
285 let host = DevServerHost::new(test_params());
286
287 let params = host.get_all_parameters();
288 assert_eq!(params.len(), 2);
289 assert!(params.iter().any(|p| p.id == "gain"));
290 assert!(params.iter().any(|p| p.id == "mix"));
291 }
292
293 #[test]
294 fn test_get_meter_frame() {
295 let host = DevServerHost::new(test_params());
296 assert!(host.get_meter_frame().is_none());
298
299 host.set_latest_meter_frame(&MeterUpdateNotification {
300 timestamp_us: 42,
301 left_peak: 0.9,
302 left_rms: 0.4,
303 right_peak: 0.8,
304 right_rms: 0.3,
305 });
306
307 let frame = host
308 .get_meter_frame()
309 .expect("meter frame should be populated after update");
310 assert!((frame.peak_l - 0.9).abs() < f32::EPSILON);
311 assert!((frame.rms_r - 0.3).abs() < f32::EPSILON);
312 assert_eq!(frame.timestamp, 42);
313 }
314
315 #[test]
316 fn test_audio_status_roundtrip() {
317 let host = DevServerHost::new(test_params());
318
319 let status = AudioRuntimeStatus {
320 phase: AudioRuntimePhase::RunningInputOnly,
321 diagnostic: None,
322 sample_rate: Some(44100.0),
323 buffer_size: Some(512),
324 updated_at_ms: 100,
325 };
326
327 host.set_audio_status(status.clone());
328
329 let stored = host
330 .get_audio_status()
331 .expect("audio status should always be present in dev host");
332 assert_eq!(stored.phase, status.phase);
333 assert_eq!(stored.buffer_size, status.buffer_size);
334 }
335
336 #[test]
337 fn test_get_oscilloscope_frame() {
338 let host = DevServerHost::new(test_params());
339 assert!(host.get_oscilloscope_frame().is_none());
340
341 host.set_latest_oscilloscope_frame(OscilloscopeFrame {
342 points_l: vec![0.1; 1024],
343 points_r: vec![0.2; 1024],
344 sample_rate: 48_000.0,
345 timestamp: 777,
346 no_signal: false,
347 trigger_mode: wavecraft_protocol::OscilloscopeTriggerMode::RisingZeroCrossing,
348 });
349
350 let frame = host
351 .get_oscilloscope_frame()
352 .expect("oscilloscope frame should be populated");
353 assert_eq!(frame.points_l.len(), 1024);
354 assert_eq!(frame.points_r.len(), 1024);
355 assert_eq!(frame.timestamp, 777);
356 }
357
358 #[tokio::test(flavor = "current_thread")]
359 async fn test_set_audio_status_inside_runtime_does_not_panic() {
360 let host = DevServerHost::new(test_params());
361
362 host.set_audio_status(AudioRuntimeStatus {
363 phase: AudioRuntimePhase::Initializing,
364 diagnostic: None,
365 sample_rate: Some(48000.0),
366 buffer_size: Some(256),
367 updated_at_ms: 200,
368 });
369
370 let stored = host
371 .get_audio_status()
372 .expect("audio status should always be present in dev host");
373 assert_eq!(stored.phase, AudioRuntimePhase::Initializing);
374 assert_eq!(stored.buffer_size, Some(256));
375 }
376}