feat: add set gain
This commit is contained in:
+153
-14
@@ -1,14 +1,58 @@
|
||||
use tauri::State;
|
||||
use crate::state::AppState;
|
||||
use crate::state::{AppState, DSPDevice};
|
||||
use dsp_thomann::dsp408::DSP408;
|
||||
use std::{net::IpAddr, str::FromStr};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[tauri::command]
|
||||
pub fn device_count(
|
||||
state: State<AppState>
|
||||
) -> usize {
|
||||
let devices = state.devices.lock().unwrap();
|
||||
devices.len()
|
||||
impl AppState {
|
||||
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())?;
|
||||
|
||||
devices
|
||||
.get(&id)
|
||||
.cloned()
|
||||
.ok_or_else(|| "DSP not found".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> {
|
||||
|
||||
let device = {
|
||||
let mut devices = self.devices
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
devices
|
||||
.remove(&id)
|
||||
.ok_or_else(|| format!("DSP {} not found", id))?
|
||||
};
|
||||
|
||||
// Drop happens here.
|
||||
// DSPDevice owns DSP408.
|
||||
drop(device);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -17,19 +61,114 @@ pub fn create_dsp408(
|
||||
ip: String,
|
||||
port: u16,
|
||||
device_id: u8,
|
||||
) -> Result<usize, String> {
|
||||
) -> Result<u64, 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 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 mut devices = state
|
||||
.devices
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let ip = IpAddr::from_str(&ip).map_err(|e| e.to_string())?;
|
||||
devices.insert(
|
||||
id,
|
||||
Arc::new(Mutex::new(device)),
|
||||
);
|
||||
|
||||
let dsp = DSP408::new(ip, port, device_id)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
devices.push(dsp);
|
||||
#[tauri::command]
|
||||
pub async fn remove_dsp408(
|
||||
state: State<'_, AppState>,
|
||||
id: u64,
|
||||
) -> Result<(), String> {
|
||||
|
||||
Ok(devices.len())
|
||||
}
|
||||
state.with_device_mut(id, |device| {
|
||||
|
||||
if device.dsp.connected() {
|
||||
device.dsp
|
||||
.disconnect()
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
state.remove_device(id)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[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())
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
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())
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
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())?;
|
||||
|
||||
Ok(dsp_state.clone())
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn set_channel_gain(
|
||||
state: State<'_, AppState>,
|
||||
id: u64,
|
||||
channel: dsp_thomann::dsp408::types::Channel,
|
||||
db: f32,
|
||||
) -> Result<bool, String> {
|
||||
state.with_device_mut(id, |device| {
|
||||
let r = device.dsp
|
||||
.set_channel_gain(channel, db)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(r)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user