Skip to main content

rustriff_lib/domain/dto/
amp_config_dto.rs

1use crate::domain::dto::channel_dto::ChannelDto;
2use crate::services::audio_service::AudioService;
3use serde::{Deserialize, Serialize};
4use std::sync::atomic::Ordering;
5
6/// Represents the complete amplifier configuration state.
7///
8/// This DTO is serialized to JSON and sent to the frontend to display the current
9/// settings of the amplifier, including gain, master volume, and active/inactive status.
10#[derive(Serialize, Deserialize, Debug, Clone)]
11pub struct AmpConfigDto {
12    /// The current master volume level.
13    pub master_volume: f32,
14    /// Whether the audio loopback is currently active.
15    pub is_active: bool,
16    /// The list of all channels with their respective settings (gain, tone stack, volume).
17    pub channels: Vec<ChannelDto>,
18    /// The current channel id
19    pub current_channel: u32,
20}
21
22impl AmpConfigDto {
23    /// Constructs an `AmpConfigDto` from the current state of an [`AudioService`].
24    ///
25    /// Reads atomic values from the service's channel and master volume with relaxed memory ordering.
26    ///
27    /// # Arguments
28    ///
29    /// * `service` - The [`AudioService`] to snapshot.
30    pub fn from_service(service: &AudioService) -> Self {
31        let channel = service
32            .channels()
33            .iter()
34            .find(|c| c.id() == *service.current_channel_id())
35            .unwrap();
36
37        Self {
38            master_volume: service.master_volume().load(Ordering::Relaxed),
39            is_active: *service.is_active(),
40            channels: service.channels().iter().map(ChannelDto::from).collect(),
41            current_channel: channel.id(),
42        }
43    }
44}