refactor: folders/files to group dsp logic

This commit is contained in:
2026-08-11 23:17:47 +02:00
parent d91bcaa20f
commit 34fad6f905
35 changed files with 194 additions and 337 deletions
+35 -109
View File
@@ -1,24 +1,17 @@
use dsp_thomann::dsp408::types::DelayUnit;
use tauri::State;
use crate::state::{AppState, DSPDevice};
use dsp_thomann::dsp408::{DSP408, types};
use std::{net::IpAddr, str::FromStr};
use dsp_thomann::dsp408::types::DelayUnit;
use dsp_thomann::dsp408::{types, DSP408};
use std::sync::{Arc, Mutex};
use std::{net::IpAddr, str::FromStr};
use tauri::State;
impl AppState {
pub fn with_device_mut<F, R>(
&self,
id: u64,
f: F,
) -> Result<R, String>
pub fn with_device_mut<F, R>(&self, id: u64, f: F) -> Result<R, String>
where
F: FnOnce(&mut DSPDevice) -> Result<R, String>,
{
let device = {
let devices = self
.devices
.lock()
.map_err(|e| e.to_string())?;
let devices = self.devices.lock().map_err(|e| e.to_string())?;
devices
.get(&id)
@@ -26,22 +19,14 @@ impl AppState {
.ok_or_else(|| "DSP not found".to_string())?
};
let mut device = device
.lock()
.map_err(|e| e.to_string())?;
let mut device = device.lock().map_err(|e| e.to_string())?;
f(&mut device)
}
pub fn remove_device(
&self,
id: u64,
) -> Result<(), String> {
pub fn remove_device(&self, id: u64) -> Result<(), String> {
let device = {
let mut devices = self.devices
.lock()
.map_err(|e| e.to_string())?;
let mut devices = self.devices.lock().map_err(|e| e.to_string())?;
devices
.remove(&id)
@@ -63,50 +48,29 @@ pub fn create_dsp408(
port: u16,
device_id: u8,
) -> Result<u64, String> {
let ip = IpAddr::from_str(&ip).map_err(|e| e.to_string())?;
let ip = IpAddr::from_str(&ip)
.map_err(|e| e.to_string())?;
let dsp = DSP408::new(ip, port, device_id).map_err(|e| e.to_string())?;
let dsp = DSP408::new(ip, port, device_id)
.map_err(|e| e.to_string())?;
let mut next_id = state
.next_id
.lock()
.map_err(|e| e.to_string())?;
let mut next_id = state.next_id.lock().map_err(|e| e.to_string())?;
let id = *next_id;
*next_id += 1;
let device = DSPDevice {
dsp,
};
let device = DSPDevice { dsp };
let mut devices = state
.devices
.lock()
.map_err(|e| e.to_string())?;
let mut devices = state.devices.lock().map_err(|e| e.to_string())?;
devices.insert(
id,
Arc::new(Mutex::new(device)),
);
devices.insert(id, Arc::new(Mutex::new(device)));
Ok(id)
}
#[tauri::command]
pub async fn remove_dsp408(
state: State<'_, AppState>,
id: u64,
) -> Result<(), String> {
pub async fn remove_dsp408(state: State<'_, AppState>, id: u64) -> Result<(), String> {
state.with_device_mut(id, |device| {
if device.dsp.connected() {
device.dsp
.disconnect()
.map_err(|e| e.to_string())?;
device.dsp.disconnect().map_err(|e| e.to_string())?;
}
Ok(())
@@ -118,28 +82,14 @@ pub async fn remove_dsp408(
}
#[tauri::command]
pub async fn connect_dsp408(
state: State<'_, AppState>,
id: u64,
) -> Result<(), String> {
state.with_device_mut(id, |device| {
device.dsp
.connect()
.map_err(|e| e.to_string())
})
pub async fn connect_dsp408(state: State<'_, AppState>, id: u64) -> Result<(), String> {
state.with_device_mut(id, |device| device.dsp.connect().map_err(|e| e.to_string()))
}
#[tauri::command]
pub async fn disconnect_dsp408(
state: State<'_, AppState>,
id: u64,
) -> Result<(), String> {
pub async fn disconnect_dsp408(state: State<'_, AppState>, id: u64) -> Result<(), String> {
state.with_device_mut(id, |device| {
device.dsp
.disconnect()
.map_err(|e| e.to_string())
device.dsp.disconnect().map_err(|e| e.to_string())
})
}
@@ -148,11 +98,8 @@ pub async fn get_dsp_state(
state: State<'_, AppState>,
id: u64,
) -> Result<dsp_thomann::dsp408::types::DSPState, String> {
state.with_device_mut(id, |device| {
let dsp_state = device.dsp
.state()
.map_err(|e| e.to_string())?;
let dsp_state = device.dsp.state().map_err(|e| e.to_string())?;
Ok(dsp_state.clone())
})
@@ -166,7 +113,8 @@ pub async fn set_channel_gain(
db: f32,
) -> Result<bool, String> {
state.with_device_mut(id, |device| {
let r = device.dsp
let r = device
.dsp
.set_channel_gain(channel, db)
.map_err(|e| e.to_string())?;
@@ -182,7 +130,8 @@ pub async fn set_mute(
muted: bool,
) -> Result<bool, String> {
state.with_device_mut(id, |device| {
let r = device.dsp
let r = device
.dsp
.set_mute(channel, muted)
.map_err(|e| e.to_string())?;
@@ -198,7 +147,8 @@ pub async fn set_channel_inverse_gain(
inverted: bool,
) -> Result<bool, String> {
state.with_device_mut(id, |device| {
let r = device.dsp
let r = device
.dsp
.set_channel_inverse_gain(channel, inverted)
.map_err(|e| e.to_string())?;
@@ -228,10 +178,7 @@ pub async fn get_meter_levels(
id: u64,
) -> Result<dsp_thomann::dsp408::types::Meters, String> {
state.with_device_mut(id, |device| {
device
.dsp
.get_meter_levels()
.map_err(|e| e.to_string())
device.dsp.get_meter_levels().map_err(|e| e.to_string())
})
}
@@ -281,14 +228,7 @@ pub async fn set_compressor(
state.with_device_mut(id, |device| {
device
.dsp
.set_compressor(
channel,
ratio,
attack,
release,
knee,
threshold,
)
.set_compressor(channel, ratio, attack, release, knee, threshold)
.map_err(|e| e.to_string())
})
}
@@ -317,10 +257,7 @@ pub async fn set_delay_unit(
unit: types::DelayUnit,
) -> Result<bool, String> {
state.with_device_mut(id, |device| {
device
.dsp
.set_delay_unit(unit)
.map_err(|e| e.to_string())
device.dsp.set_delay_unit(unit).map_err(|e| e.to_string())
})
}
@@ -330,7 +267,7 @@ pub async fn set_delay(
id: u64,
channel: types::Channel,
delay: f32,
unit: DelayUnit
unit: DelayUnit,
) -> Result<bool, String> {
state.with_device_mut(id, |device| {
device
@@ -449,15 +386,7 @@ pub async fn set_peq_band(
state.with_device_mut(id, |device| {
device
.dsp
.set_peq_band(
channel,
band,
gain_db,
freq_hz,
q,
filter_type,
bypass,
)
.set_peq_band(channel, band, gain_db, freq_hz, q, filter_type, bypass)
.map_err(|e| e.to_string())
})
}
@@ -484,10 +413,7 @@ pub async fn factory_reset(
force: bool,
) -> Result<bool, String> {
state.with_device_mut(id, |device| {
device
.dsp
.factory_reset(force)
.map_err(|e| e.to_string())
device.dsp.factory_reset(force).map_err(|e| e.to_string())
})
}
@@ -563,4 +489,4 @@ pub async fn set_channel_name(
.set_channel_name(channel, &name)
.map_err(|e| e.to_string())
})
}
}