Skip to main content

rustriff_lib/domain/dto/
channel_dto.rs

1use crate::domain::channel::Channel;
2use crate::domain::dto::effect::effect_dto::EffectDto;
3use crate::domain::dto::tone_stack_dto::ToneStackDto;
4use serde::{Deserialize, Serialize};
5use std::sync::atomic::Ordering;
6use tracing::error;
7
8/// Data transfer object for a Channel's settings.
9#[derive(Debug, Serialize, Deserialize, Clone)]
10pub struct ChannelDto {
11    /// Unique identifier for the Channel.
12    pub id: u32,
13    /// Name of the Channel
14    pub name: String,
15    /// The input gain level of the Channel.
16    pub gain: f32,
17    /// The tone stack settings, including bass, mid, treble of the Channel.
18    pub tone_stack: ToneStackDto,
19    /// The volume of the Channel.
20    pub volume: f32,
21    /// The chain of effects the signal is sent through
22    pub effect_chain: Vec<EffectDto>,
23}
24
25impl From<&Channel> for ChannelDto {
26    fn from(channel: &Channel) -> Self {
27        let effect_dtos = match channel.effect_chain().lock() {
28            Ok(chain) => chain.iter().map(|effect| effect.to_dto()).collect(),
29            Err(poisoned) => {
30                error!("Effect chain mutex poisoned for channel {}", channel.id());
31                poisoned
32                    .into_inner()
33                    .iter()
34                    .map(|effect| effect.to_dto())
35                    .collect()
36            }
37        };
38
39        Self {
40            id: channel.id(),
41            name: channel.name().to_string(),
42            gain: channel.gain().load(Ordering::Relaxed),
43            tone_stack: ToneStackDto::from(channel.tone_stack().as_ref()),
44            volume: channel.volume().load(Ordering::Relaxed),
45            effect_chain: effect_dtos,
46        }
47    }
48}