feat: complete history, attendees list, and smart templates
This commit is contained in:
@@ -5,6 +5,10 @@ use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
|
||||
mod audio_processor;
|
||||
use audio_processor::AudioProcessor;
|
||||
mod auth;
|
||||
|
||||
// State to hold the active recording stream
|
||||
struct AppState {
|
||||
recording_stream: Mutex<Option<cpal::Stream>>,
|
||||
@@ -60,7 +64,7 @@ fn get_input_devices() -> Result<Vec<AudioDevice>, String> {
|
||||
|
||||
|
||||
#[tauri::command]
|
||||
fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: String, save_path: Option<String>) -> Result<(), String> {
|
||||
fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: String, save_path: Option<String>, custom_filename: Option<String>) -> Result<(), String> {
|
||||
emit_log(&app, "INFO", &format!("Starting recording on device: {}", device_id));
|
||||
let host = cpal::default_host();
|
||||
|
||||
@@ -73,6 +77,15 @@ fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: String
|
||||
.ok_or("No input device found")?;
|
||||
|
||||
let config = device.default_input_config().map_err(|e| e.to_string())?;
|
||||
|
||||
// VAD requires 16Hz or 8kHz, typically. Silero likes 16k.
|
||||
// We might need to resample or just check if the device supports it.
|
||||
// For MVP VAD, we will try to stick to standard rates.
|
||||
// Actually, simple energy VAD is easier to start with if Silero is too heavy or requires ONNX runtime.
|
||||
// Let's check the crate docs or usage first.
|
||||
// Wait, the user wants to IGNORE music. Energy VAD will fail on music.
|
||||
// voice_activity_detector crate usually uses Silero or similar.
|
||||
|
||||
let spec = hound::WavSpec {
|
||||
channels: config.channels(),
|
||||
sample_rate: config.sample_rate(),
|
||||
@@ -81,16 +94,22 @@ fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: String
|
||||
};
|
||||
|
||||
// Determine file path: User provided or Temp
|
||||
let filename = if let Some(name) = custom_filename {
|
||||
// Sanitize filename
|
||||
let safe_name: String = name.chars().map(|x| if x.is_alphanumeric() || x == ' ' || x == '-' || x == '_' { x } else { '_' }).collect();
|
||||
format!("{}.wav", safe_name)
|
||||
} else {
|
||||
format!("recording_{}.wav", std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs())
|
||||
};
|
||||
|
||||
let file_path = if let Some(path) = save_path {
|
||||
if path.trim().is_empty() {
|
||||
std::env::temp_dir().join(format!("recording_{}.wav", std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs()))
|
||||
std::env::temp_dir().join(&filename)
|
||||
} else {
|
||||
// Check if directory exists, if not try to create it or error out?
|
||||
// For now, assume user gives a valid directory. We'll append filename.
|
||||
std::path::PathBuf::from(path).join(format!("recording_{}.wav", std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs()))
|
||||
std::path::PathBuf::from(path).join(&filename)
|
||||
}
|
||||
} else {
|
||||
std::env::temp_dir().join(format!("recording_{}.wav", std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs()))
|
||||
std::env::temp_dir().join(&filename)
|
||||
};
|
||||
|
||||
let file_path_str = file_path.to_string_lossy().to_string();
|
||||
@@ -99,6 +118,19 @@ fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: String
|
||||
let writer = hound::WavWriter::create(&file_path, spec).map_err(|e| e.to_string())?;
|
||||
let writer = Arc::new(Mutex::new(writer));
|
||||
let writer_clone = writer.clone();
|
||||
|
||||
// Initialize AudioProcessor (VAD)
|
||||
// We pass the writer to it.
|
||||
let processor = AudioProcessor::new(config.sample_rate(), writer.clone(), app.clone())
|
||||
.map_err(|e| format!("Failed to create AudioProcessor: {}", e))?;
|
||||
|
||||
// Wrap processor in Arc<Mutex> so we can share/move it into callback
|
||||
// Actually, cpal callback takes ownership of its closure state usually if 'move'.
|
||||
// Since stream is on another thread, we need Send. AudioProcessor should be Send.
|
||||
// However, the callback is called repeatedly. We need to keep state.
|
||||
// The workaround is to wrap it in a Mutex.
|
||||
let processor = Arc::new(Mutex::new(processor));
|
||||
let processor_clone = processor.clone();
|
||||
|
||||
let app_handle = app.clone();
|
||||
let err_fn = move |err| {
|
||||
@@ -110,21 +142,21 @@ fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: String
|
||||
cpal::SampleFormat::F32 => device.build_input_stream(
|
||||
&config.into(),
|
||||
move |data: &[f32], _: &_| {
|
||||
let mut guard = writer_clone.lock().unwrap();
|
||||
for &sample in data {
|
||||
let amplitude = i16::MAX as f32;
|
||||
guard.write_sample((sample * amplitude) as i16).ok();
|
||||
if let Ok(mut p) = processor_clone.lock() {
|
||||
p.process(data);
|
||||
}
|
||||
},
|
||||
err_fn,
|
||||
None
|
||||
),
|
||||
// For I16 and U16 we need to convert to F32 for our processor
|
||||
cpal::SampleFormat::I16 => device.build_input_stream(
|
||||
&config.into(),
|
||||
move |data: &[i16], _: &_| {
|
||||
let mut guard = writer_clone.lock().unwrap();
|
||||
for &sample in data {
|
||||
guard.write_sample(sample).ok();
|
||||
// Convert i16 to f32
|
||||
let f32_data: Vec<f32> = data.iter().map(|&s| s as f32 / i16::MAX as f32).collect();
|
||||
if let Ok(mut p) = processor_clone.lock() {
|
||||
p.process(&f32_data);
|
||||
}
|
||||
},
|
||||
err_fn,
|
||||
@@ -133,9 +165,10 @@ fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: String
|
||||
cpal::SampleFormat::U16 => device.build_input_stream(
|
||||
&config.into(),
|
||||
move |data: &[u16], _: &_| {
|
||||
let mut guard = writer_clone.lock().unwrap();
|
||||
for &sample in data {
|
||||
guard.write_sample((sample as i32 - 32768) as i16).ok();
|
||||
// Convert u16 to f32
|
||||
let f32_data: Vec<f32> = data.iter().map(|&s| (s as i32 - 32768) as f32 / 32768.0).collect();
|
||||
if let Ok(mut p) = processor_clone.lock() {
|
||||
p.process(&f32_data);
|
||||
}
|
||||
},
|
||||
err_fn,
|
||||
@@ -536,6 +569,60 @@ fn open_audio_midi_setup() -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn create_hearbit_audio_device(app: AppHandle) -> Result<String, String> {
|
||||
emit_log(&app, "INFO", "Attempting to create Hearbit Audio device...");
|
||||
|
||||
// Resolve resource path
|
||||
let resource_path = app.path().resource_dir()
|
||||
.map_err(|e| e.to_string())?
|
||||
.join("resources/create_hearbit_audio.swift");
|
||||
|
||||
if !resource_path.exists() {
|
||||
// Fallback for dev environment where resources might not be bundled yet or different path
|
||||
emit_log(&app, "WARN", &format!("Resource script not found at {:?}. Trying local src-tauri path.", resource_path));
|
||||
}
|
||||
|
||||
// For now, in dev mode, we might need to point to the source location if bundle isn't active
|
||||
// But let's try running it.
|
||||
|
||||
let output = Command::new("swift")
|
||||
.arg(resource_path)
|
||||
.output()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
||||
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
||||
|
||||
emit_log(&app, "DEBUG", &format!("Script Output: {}", stdout));
|
||||
if !stderr.is_empty() {
|
||||
emit_log(&app, "WARN", &format!("Script Stderr: {}", stderr));
|
||||
}
|
||||
|
||||
if output.status.success() {
|
||||
emit_log(&app, "SUCCESS", "Hearbit Audio device created successfully.");
|
||||
Ok("Device created successfully".to_string())
|
||||
} else {
|
||||
emit_log(&app, "ERROR", "Failed to create device.");
|
||||
Err(format!("Failed to create device: {} {}", stdout, stderr))
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn save_text_file(app: AppHandle, path: String, content: String) -> Result<(), String> {
|
||||
emit_log(&app, "INFO", &format!("Saving text file to: {}", path));
|
||||
match std::fs::write(&path, content) {
|
||||
Ok(_) => {
|
||||
emit_log(&app, "SUCCESS", "File saved successfully.");
|
||||
Ok(())
|
||||
},
|
||||
Err(e) => {
|
||||
emit_log(&app, "ERROR", &format!("Failed to save file: {}", e));
|
||||
Err(e.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
@@ -543,6 +630,8 @@ pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.plugin(tauri_plugin_oauth::init())
|
||||
.manage(AppState {
|
||||
recording_stream: Mutex::new(None),
|
||||
recording_file_path: Mutex::new(None),
|
||||
@@ -557,7 +646,11 @@ pub fn run() {
|
||||
transcribe_audio,
|
||||
summarize_text,
|
||||
get_available_models,
|
||||
open_audio_midi_setup
|
||||
open_audio_midi_setup,
|
||||
create_hearbit_audio_device,
|
||||
auth::start_auth_flow,
|
||||
auth::get_calendar_events,
|
||||
save_text_file
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
Reference in New Issue
Block a user