Skip to main content

sparrow_tools/
voice.rs

1//! Voice command for Sparrow.
2//!
3//! Integrates TTS and STT tools into a unified voice interface.
4
5use std::path::PathBuf;
6
7/// Voice operation mode.
8pub enum VoiceCommand {
9    /// Convert text to speech
10    Speak {
11        text: String,
12        output: Option<PathBuf>,
13    },
14    /// Transcribe audio to text
15    Transcribe {
16        audio_file: PathBuf,
17        language: Option<String>,
18    },
19    /// List available voices/providers
20    ListProviders,
21}
22
23/// Handle a voice command.
24pub fn handle_voice(cmd: VoiceCommand) -> anyhow::Result<()> {
25    match cmd {
26        VoiceCommand::Speak { text, output } => {
27            println!("🔊 Converting text to speech...");
28            let path = crate::tts::text_to_speech(&text, crate::tts::TtsProvider::Edge, output)?;
29            println!("✓ Audio saved: {}", path.display());
30            println!("  → Play: ffplay {} -nodisp -autoexit", path.display());
31        }
32        VoiceCommand::Transcribe {
33            audio_file,
34            language,
35        } => {
36            println!("🎤 Transcribing audio...");
37            let text = crate::stt::transcribe(
38                &audio_file,
39                language.as_deref(),
40                crate::stt::SttBackend::OpenAIApi,
41            )?;
42            println!("✓ Transcription:\n\n{text}");
43        }
44        VoiceCommand::ListProviders => {
45            println!("Available TTS providers:");
46            println!("  edge      — Microsoft Edge TTS (free, built-in voices)");
47            println!("  openai    — OpenAI TTS API (requires API key)");
48            println!("  say       — macOS built-in (free)");
49            println!("  espeak    — Linux espeak (free, install: apt install espeak)");
50            println!();
51            println!("Available STT providers:");
52            println!("  openai    — OpenAI Whisper API (requires API key)");
53            println!("  whisper   — Local whisper CLI (install: pip install openai-whisper)");
54            println!("  whisper-cpp — Local whisper.cpp (fast, no Python needed)");
55        }
56    }
57    Ok(())
58}