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())
})
}
}
+32 -37
View File
@@ -1,5 +1,5 @@
mod state;
mod commands;
mod state;
use state::AppState;
@@ -9,42 +9,37 @@ pub fn run() {
.plugin(tauri_plugin_opener::init())
.manage(AppState::new())
.invoke_handler(tauri::generate_handler![
commands::create_dsp408,
commands::remove_dsp408,
commands::get_dsp_state,
commands::connect_dsp408, // 0x10
commands::disconnect_dsp408, // 0x11
commands::set_delay_unit, // 0x15
commands::recall_preset, // 0x20
commands::store_current_configuration, // 0x21
commands::factory_reset, // 0x23
commands::set_current_preset_name, // 0x26
commands::copy_channel, // 0x2A
commands::set_compressor, // 0x30
commands::set_low_pass, // 0x31
commands::set_high_pass, // 0x32
commands::set_peq_band, // 0x33
commands::set_channel_gain, // 0x34
commands::set_mute, // 0x35
commands::set_channel_inverse_gain, // 0x36
commands::set_delay, // 0x38
commands::set_input_source, // 0x39
commands::set_matrix_routing, // 0x3A
commands::link_channels, // 0x3B
commands::set_bypass_peq, // 0x3C
commands::set_channel_name, // 0x3D
commands::set_gate, // 0x3E
commands::set_limiter, // 0x3F
commands::get_meter_levels, // 0x40
commands::set_matrix_gain, // 0x41
commands::set_geq_band, // 0x48
commands::set_geq_bypass, // 0x49
]
)
commands::create_dsp408,
commands::remove_dsp408,
commands::get_dsp_state,
commands::connect_dsp408, // 0x10
commands::disconnect_dsp408, // 0x11
commands::set_delay_unit, // 0x15
commands::recall_preset, // 0x20
commands::store_current_configuration, // 0x21
commands::factory_reset, // 0x23
commands::set_current_preset_name, // 0x26
commands::copy_channel, // 0x2A
commands::set_compressor, // 0x30
commands::set_low_pass, // 0x31
commands::set_high_pass, // 0x32
commands::set_peq_band, // 0x33
commands::set_channel_gain, // 0x34
commands::set_mute, // 0x35
commands::set_channel_inverse_gain, // 0x36
commands::set_delay, // 0x38
commands::set_input_source, // 0x39
commands::set_matrix_routing, // 0x3A
commands::link_channels, // 0x3B
commands::set_bypass_peq, // 0x3C
commands::set_channel_name, // 0x3D
commands::set_gate, // 0x3E
commands::set_limiter, // 0x3F
commands::get_meter_levels, // 0x40
commands::set_matrix_gain, // 0x41
commands::set_geq_band, // 0x48
commands::set_geq_bypass, // 0x49
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
+3 -3
View File
@@ -1,6 +1,6 @@
use std::sync::{Mutex, Arc};
use std::collections::HashMap;
use dsp_thomann::dsp408::DSP408;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
pub struct DSPDevice {
pub dsp: DSP408,
@@ -18,4 +18,4 @@ impl AppState {
next_id: Mutex::new(1),
}
}
}
}