add mic sensitivity to config
This commit is contained in:
parent
2489210068
commit
31bbf44171
|
@ -12,7 +12,12 @@ use cpal::InputCallbackInfo;
|
|||
use log::debug;
|
||||
use tauri::Window;
|
||||
|
||||
pub async fn monitor(window: Window, threshold: Arc<Mutex<f32>>, level: Arc<Mutex<f32>>) {
|
||||
pub async fn monitor(
|
||||
window: Window,
|
||||
threshold: Arc<Mutex<f32>>,
|
||||
level: Arc<Mutex<f32>>,
|
||||
sens: Arc<Mutex<f32>>,
|
||||
) {
|
||||
let device = initialize().expect("Unable to init audio");
|
||||
debug!("Using device {}", device.name().unwrap());
|
||||
let config = device.default_input_config().unwrap();
|
||||
|
@ -20,7 +25,10 @@ pub async fn monitor(window: Window, threshold: Arc<Mutex<f32>>, level: Arc<Mute
|
|||
.build_input_stream(
|
||||
&config.config(),
|
||||
move |data: &[f32], _: &InputCallbackInfo| {
|
||||
if data.iter().any(|e| e.abs() >= *threshold.lock().unwrap()) {
|
||||
if data
|
||||
.iter()
|
||||
.any(|e| (e.abs() * *sens.lock().unwrap()) >= *threshold.lock().unwrap())
|
||||
{
|
||||
window.emit("mouth-open", "").unwrap();
|
||||
} else {
|
||||
window.emit("mouth-close", "").unwrap();
|
||||
|
@ -28,10 +36,9 @@ pub async fn monitor(window: Window, threshold: Arc<Mutex<f32>>, level: Arc<Mute
|
|||
|
||||
*level.lock().unwrap() = data
|
||||
.iter()
|
||||
.map(|e| e.abs())
|
||||
.map(|e| e.abs() * *sens.lock().unwrap())
|
||||
.max_by(|a, b| a.total_cmp(&b))
|
||||
.unwrap()
|
||||
.clone();
|
||||
.unwrap();
|
||||
},
|
||||
move |err| {
|
||||
println!("Audio error: {:?}", err);
|
||||
|
|
|
@ -11,6 +11,8 @@ pub struct Config {
|
|||
pub background_color: BGColor,
|
||||
#[serde(default = "default_interval")]
|
||||
pub blink_interval: u64,
|
||||
#[serde(default = "default_sens")]
|
||||
pub mic_sens: f32,
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
@ -18,6 +20,11 @@ fn default_interval() -> u64 {
|
|||
1500
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn default_sens() -> f32 {
|
||||
1.0
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum BGColor {
|
||||
|
|
|
@ -33,6 +33,7 @@ struct AudioLevel(Arc<Mutex<f32>>);
|
|||
struct BlinkInterval(Arc<Mutex<u64>>);
|
||||
struct CurrentConfig(Arc<Mutex<Config>>);
|
||||
struct RayToLoad(Arc<Mutex<Option<PathBuf>>>);
|
||||
struct MicSense(Arc<Mutex<f32>>);
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
|
@ -40,6 +41,7 @@ fn main() {
|
|||
let level = Arc::new(Mutex::new(0.));
|
||||
let blink_interval = Arc::new(Mutex::new(1500));
|
||||
let ray = Arc::new(Mutex::new(None));
|
||||
let sens = Arc::new(Mutex::new(1.0));
|
||||
|
||||
if let Some(d) = cache_dir() {
|
||||
use std::fs;
|
||||
|
@ -53,7 +55,8 @@ fn main() {
|
|||
let config = config::load_config();
|
||||
|
||||
{
|
||||
*blink_interval.lock().unwrap() = config.blink_interval
|
||||
*blink_interval.lock().unwrap() = config.blink_interval;
|
||||
*sens.lock().unwrap() = config.mic_sens;
|
||||
}
|
||||
|
||||
let current_conf = Arc::new(Mutex::new(config));
|
||||
|
@ -66,6 +69,7 @@ fn main() {
|
|||
.manage(BlinkInterval(blink_interval.clone()))
|
||||
.manage(CurrentConfig(current_conf.clone()))
|
||||
.manage(RayToLoad(ray.clone()))
|
||||
.manage(MicSense(sens.clone()))
|
||||
// .on_window_event(move |event| match event.event() {
|
||||
// WindowEvent::CloseRequested { .. } => {
|
||||
// debug!("close requested");
|
||||
|
@ -78,7 +82,7 @@ fn main() {
|
|||
.setup(move |app| {
|
||||
let window = app.get_window("main").unwrap();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
monitor(window, threshold, level).await;
|
||||
monitor(window, threshold, level, sens).await;
|
||||
});
|
||||
|
||||
let window = app.get_window("main").unwrap();
|
||||
|
@ -141,6 +145,8 @@ fn main() {
|
|||
log,
|
||||
set_mic_threshold,
|
||||
get_mic_threshold,
|
||||
set_mic_sens,
|
||||
get_mic_sens,
|
||||
get_audio_level,
|
||||
get_blink_interval,
|
||||
set_blink_interval,
|
||||
|
@ -183,6 +189,16 @@ fn get_mic_threshold(current: State<'_, MicThreshold>) -> f32 {
|
|||
*current.0.lock().unwrap()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn set_mic_sens(sens: f32, current: State<'_, MicSense>) {
|
||||
*current.0.lock().unwrap() = sens;
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_mic_sens(current: State<'_, MicSense>) -> f32 {
|
||||
*current.0.lock().unwrap()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_audio_level(level: State<'_, AudioLevel>) -> f32 {
|
||||
*level.0.lock().unwrap()
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
import MainView from "./views/main.svelte";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import { invoke } from "@tauri-apps/api";
|
||||
import { onMount } from "svelte";
|
||||
import { onDestroy, onMount } from "svelte";
|
||||
import { config } from "./store";
|
||||
import type { Config } from "./store";
|
||||
|
||||
|
@ -18,9 +18,6 @@
|
|||
}).catch();
|
||||
}
|
||||
|
||||
$: {
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
$config = (await invoke("get_config")) as Config;
|
||||
await listen("reload-config", async () => {
|
||||
|
@ -31,6 +28,10 @@
|
|||
invoke("set_config", { config: value });
|
||||
});
|
||||
});
|
||||
|
||||
onDestroy(async () => {
|
||||
await invoke("save_current_config");
|
||||
});
|
||||
</script>
|
||||
|
||||
<main>
|
||||
|
|
|
@ -41,8 +41,13 @@
|
|||
<div class="box">
|
||||
<button
|
||||
class="preview"
|
||||
on:click={openImage}
|
||||
on:contextmenu={clearImage}
|
||||
on:click={(e) => {
|
||||
if (e.shiftKey) {
|
||||
clearImage();
|
||||
} else {
|
||||
openImage();
|
||||
}
|
||||
}}
|
||||
on:mouseenter={() =>
|
||||
(menuTimeout = setTimeout(() => (showMenu = true), 200))}
|
||||
on:mouseleave={() => {
|
||||
|
@ -118,8 +123,13 @@
|
|||
user-select: none;
|
||||
gap: 10px;
|
||||
display: flex;
|
||||
align-content: center;
|
||||
justify-content: center;
|
||||
.context {
|
||||
align-self: center;
|
||||
z-index: 900;
|
||||
position: absolute;
|
||||
left: 105%;
|
||||
width: max-content;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,13 +4,18 @@
|
|||
import { invoke } from "@tauri-apps/api";
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
const defaultSens = 1.0;
|
||||
|
||||
let org = $config;
|
||||
|
||||
$: {
|
||||
invoke("set_mic_sens", { sens: $config.mic_sens }).catch(
|
||||
async (e) => await invoke("log", { msg: JSON.stringify(e) })
|
||||
);
|
||||
}
|
||||
|
||||
const onClose = () => {
|
||||
if (org != $config) {
|
||||
invoke("set_blink_interval", { value: $config.blink_interval });
|
||||
invoke("set_blink_interval", { value: $config.blink_interval }).catch();
|
||||
org = $config;
|
||||
}
|
||||
dispatch("close");
|
||||
|
@ -19,7 +24,7 @@
|
|||
|
||||
<div class="settings">
|
||||
<button class="close" on:click={onClose}>X</button>
|
||||
<div>
|
||||
<div class="setting">
|
||||
<label for="bg-color">Background color:</label>
|
||||
<select bind:value={$config.background_color} id="bg-color">
|
||||
<option value="transparent">Transparent</option>
|
||||
|
@ -30,7 +35,7 @@
|
|||
</select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div class="setting">
|
||||
<label for="blink">Blink interval: </label>
|
||||
<input
|
||||
value={$config.blink_interval}
|
||||
|
@ -43,16 +48,20 @@
|
|||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div class="setting">
|
||||
<label for="sens">Mic sensitivity: </label>
|
||||
<input
|
||||
type="range"
|
||||
id="sens"
|
||||
step="0.1"
|
||||
min="0.1"
|
||||
step="0.01"
|
||||
min="0.01"
|
||||
max="1.0"
|
||||
value={defaultSens}
|
||||
value={$config.mic_sens}
|
||||
on:change={(e) => {
|
||||
$config.mic_sens = e.currentTarget.valueAsNumber;
|
||||
}}
|
||||
/>
|
||||
<span>{Math.trunc($config.mic_sens * 100)}%</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -71,7 +80,7 @@
|
|||
background-color: rgba(63, 176, 252, 0.9);
|
||||
border-radius: 15px;
|
||||
border: solid black 2px;
|
||||
padding: 15px;
|
||||
padding: 20px;
|
||||
label {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
@ -91,6 +100,14 @@
|
|||
border: solid black 1px;
|
||||
text-align: center;
|
||||
vertical-align: center;
|
||||
border-radius: 5px;
|
||||
}
|
||||
}
|
||||
|
||||
.setting {
|
||||
display: flex;
|
||||
align-content: center;
|
||||
justify-content: space-between;
|
||||
width: 50%;
|
||||
}
|
||||
</style>
|
||||
|
|
|
@ -16,15 +16,18 @@ export class WebRay {
|
|||
this.frames = frames;
|
||||
this.meta = meta;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class Config {
|
||||
background_color: BGColor;
|
||||
blink_interval: number;
|
||||
mic_sens: number;
|
||||
|
||||
|
||||
constructor(){
|
||||
this.background_color = "transparent";
|
||||
this.blink_interval = 1500;
|
||||
this.mic_sens = 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<script lang="ts" context="module">
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
import { writable } from "svelte/store";
|
||||
const transparent = writable(false);
|
||||
const settings_open = writable(false);
|
||||
const threshold = writable(0.5);
|
||||
|
@ -22,7 +22,7 @@
|
|||
import Tuber from "../components/tube.svelte";
|
||||
import Bar from "../components/bar.svelte";
|
||||
import Settings from "../components/settings.svelte";
|
||||
import Devices from "../components/devices.svelte";
|
||||
// import Devices from "../components/devices.svelte";
|
||||
|
||||
let monitorTimer: NodeJS.Timer;
|
||||
|
||||
|
|
Loading…
Reference in New Issue