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::tools::tts::text_to_speech(
29                &text,
30                crate::tools::tts::TtsProvider::Edge,
31                output,
32            )?;
33            println!("✓ Audio saved: {}", path.display());
34            println!("  → Play: ffplay {} -nodisp -autoexit", path.display());
35        }
36        VoiceCommand::Transcribe { audio_file, language } => {
37            println!("🎤 Transcribing audio...");
38            let text = crate::tools::stt::transcribe(
39                &audio_file,
40                language.as_deref(),
41                crate::tools::stt::SttBackend::OpenAIApi,
42            )?;
43            println!("✓ Transcription:\n\n{text}");
44        }
45        VoiceCommand::ListProviders => {
46            println!("Available TTS providers:");
47            println!("  edge      — Microsoft Edge TTS (free, built-in voices)");
48            println!("  openai    — OpenAI TTS API (requires API key)");
49            println!("  say       — macOS built-in (free)");
50            println!("  espeak    — Linux espeak (free, install: apt install espeak)");
51            println!();
52            println!("Available STT providers:");
53            println!("  openai    — OpenAI Whisper API (requires API key)");
54            println!("  whisper   — Local whisper CLI (install: pip install openai-whisper)");
55            println!("  whisper-cpp — Local whisper.cpp (fast, no Python needed)");
56        }
57    }
58    Ok(())
59}