### Control Voice2Type via TCP API (Bash) Source: https://context7.com/guchang233/voice2type/llms.txt Demonstrates how to control the Voice2Type application and access its configuration through a TCP API server. This example uses `netcat` to send JSON-formatted commands to the server running on localhost:8080. ```bash # Example: Control Voice2Type via netcat # Start recording echo '{"StartRecording": null}' | nc localhost 8080 # Stop recording echo '{"StopRecording": null}' | nc localhost 8080 # Cancel recording echo '{"CancelRecording": null}' | nc localhost 8080 # Get current state echo '{"GetState": null}' | nc localhost 8080 # Response format: # Success: {"Success": {"message": "Request received"}} # Error: {"Error": {"message": "error description"}} # State: {"StateResponse": {"state": "idle"}} ``` -------------------------------- ### Manage Voice2Type Configuration in Rust Source: https://context7.com/guchang233/voice2type/llms.txt Demonstrates how to initialize and configure the `ConfigManager` in Rust. This includes setting API keys, speech services, output modes, hotkeys, and saving the configuration to disk. It covers persistence options and accessing configuration file paths. ```rust use std::sync::Arc; use crate::config::ConfigManager; // Initialize configuration manager let config = Arc::new(ConfigManager::new()); // Get and set API key let api_key = config.get_api_key(); config.set_api_key("your-siliconflow-api-key".to_string()); // Configure speech recognition service // Supported: "siliconflow", "openai", "google", "azure", "baidu", "alibaba", "tencent" config.set_speech_service("siliconflow".to_string()); config.set_api_url("https://api.siliconflow.cn/v1/audio/transcriptions".to_string()); config.set_model_name("FunAudioLLM/SenseVoiceSmall".to_string()); // Configure output behavior config.set_output_mode("clipboard".to_string()); // or "inject" config.set_output_language("auto".to_string()); // "auto", "zh", "en" config.set_allow_emoji(true); config.set_allow_punctuation(true); // Configure hotkey (Windows Virtual Key Code) config.set_hotkey(0x71); // F2 key // Configure trigger mode config.set_trigger_mode("hold".to_string()); // or "toggle" // Enable streaming transcription config.set_enable_streaming(false); config.set_streaming_interval(500); // milliseconds // Status indicator settings config.set_enable_indicator(true); config.set_indicator_success_duration(5000); // ms config.set_indicator_error_duration(5000); // ms // Save configuration to disk config.save().expect("Failed to save configuration"); // Access config file path let config_path = config.config_path(); let log_dir = config.log_dir(); ``` -------------------------------- ### Hotkey and Input Handling in Rust Source: https://context7.com/guchang233/voice2type/llms.txt This snippet demonstrates setting up a global hotkey listener to control audio recording. It uses Tokio's MPSC channels for message passing between the hotkey listener and the main application loop. The hotkey behavior can be configured via a ConfigManager, supporting 'hold' or 'toggle' trigger modes. ```rust use std::sync::Arc; use tokio::sync::mpsc; use crate::config::ConfigManager; use crate::utils::hotkey::{InputMessage, start_hotkey_listener}; // Input message types enum InputMessage { StartRecording, StopRecording, CancelRecording, } // Start hotkey listener let (tx, mut rx) = mpsc::channel::(100); let config = Arc::new(ConfigManager::new()); // Configure hotkey behavior config.set_hotkey(0x71); // F2 key config.set_trigger_mode("hold".to_string()); // "hold" or "toggle" // Launch listener thread start_hotkey_listener(tx.clone(), config.clone()); // Handle messages in async loop loop { match rx.recv().await { Some(InputMessage::StartRecording) => { println!("Recording started..."); } Some(InputMessage::StopRecording) => { println!("Recording stopped, processing..."); } Some(InputMessage::CancelRecording) => { println!("Recording cancelled"); } None => break, } } // Windows Virtual Key Codes for common hotkeys // F1=0x70, F2=0x71, F3=0x72, ..., F12=0x7B // CapsLock=0x14, Ctrl=0x11, Alt=0x12 ``` -------------------------------- ### Interact with Speech Recognition Services in Rust Source: https://context7.com/guchang233/voice2type/llms.txt Shows how to use the `SpeechService` trait and `SpeechServiceFactory` to interact with various speech recognition backends. It covers creating service instances, checking availability, and performing audio transcription. Includes a list of supported service types. ```rust use std::sync::Arc; use crate::config::ConfigManager; use crate::speech::service::{SpeechService, SpeechServiceFactory, SpeechServiceType}; // Create service from configuration let config = Arc::new(ConfigManager::new()); let service = SpeechServiceFactory::create_from_config(&config); // Or create a specific service type let siliconflow_service = SpeechServiceFactory::create(SpeechServiceType::SiliconFlow); let openai_service = SpeechServiceFactory::create(SpeechServiceType::OpenAI); // Check if service is available let available = service.is_available(&config).await; println!("Service '{}' available: {}", service.name(), available); // Recognize audio (audio_data is WAV bytes) let audio_data: Vec = /* WAV audio bytes */; match service.recognize(&audio_data, &config).await { Ok(text) => println!("Transcription: {}", text), Err(e) => eprintln!("Recognition failed: {}", e), } // Supported service types let service_types = [ ("siliconflow", SpeechServiceType::SiliconFlow), ("openai", SpeechServiceType::OpenAI), ("google", SpeechServiceType::GoogleCloud), ("azure", SpeechServiceType::Azure), ("baidu", SpeechServiceType::Baidu), ("alibaba", SpeechServiceType::Alibaba), ("tencent", SpeechServiceType::Tencent), ]; ``` -------------------------------- ### Output Handler and Text Post-Processing in Rust Source: https://context7.com/guchang233/voice2type/llms.txt This Rust code illustrates how to manage text output to applications using either clipboard pasting or keyboard injection. It also shows the `post_process` function's capability to filter emojis and punctuation based on user-defined configuration settings. ```rust use crate::config::ConfigManager; use crate::output::handler::{OutputHandler, post_process}; let config = ConfigManager::new(); let handler = OutputHandler::new(); // Configure output preferences config.set_output_mode("clipboard".to_string()); // "clipboard" or "inject" config.set_allow_emoji(true); config.set_allow_punctuation(true); // Post-process transcribed text let raw_text = "Hello, world! This is a test. 12:30 PM"; // With punctuation enabled config.set_allow_punctuation(true); let processed = post_process(raw_text, &config); // Result: "Hello, world! This is a test. 12:30 PM" // With punctuation disabled (preserves numeric separators) config.set_allow_punctuation(false); let processed = post_process(raw_text, &config); // Result: "Hello world This is a test 12:30 PM" // Filter emojis from text config.set_allow_emoji(false); let text_with_emoji = "Great job! Your time is 12:30."; let clean_text = post_process(text_with_emoji, &config); // Output text to active application let transcribed_text = "Voice to text transcription result"; handler.handle_output(transcribed_text.to_string(), &config).await?; // Streaming output (direct keyboard injection) handler.handle_streaming_output("Real-time ".to_string()); handler.handle_streaming_output("text output".to_string()); ``` -------------------------------- ### Process Audio for Speech Recognition (Rust) Source: https://context7.com/guchang233/voice2type/llms.txt Handles resampling, format conversion, and WAV encoding for speech recognition APIs. It takes raw audio samples at any sample rate and converts them to 16kHz mono WAV format, suitable for API submission. It also includes functionality to calculate audio energy for Voice Activity Detection. ```rust use crate::audio::processor::{resample_and_convert, encode_wav_memory, calculate_audio_energy}; // Resample and convert audio from f32 to i16 format // Input: f32 samples at any rate, Output: i16 samples at 16kHz let input_samples: Vec = /* audio samples from microphone */; let input_rate: u32 = 48000; // Original sample rate let (resampled_i16, output_rate) = resample_and_convert(&input_samples, input_rate); // output_rate will be 16000 (16kHz) // Encode samples to WAV format in memory let wav_bytes = encode_wav_memory(&resampled_i16, output_rate) .expect("Failed to encode WAV"); // Calculate audio energy for VAD (Voice Activity Detection) let energy = calculate_audio_energy(&input_samples); println!("Audio energy level: {}", energy); // Example: Full pipeline from capture to API-ready format let raw_audio: Vec = /* captured f32 audio at 48kHz stereo */; let sample_rate = 48000u32; // 1. Resample to 16kHz mono i16 let (processed, _) = resample_and_convert(&raw_audio, sample_rate); // 2. Encode to WAV bytes let wav_data = encode_wav_memory(&processed, 16000)?; // wav_data is now ready for API submission ``` -------------------------------- ### Interact with Speech Recognition APIs (Rust) Source: https://context7.com/guchang233/voice2type/llms.txt Provides methods for sending audio data to speech recognition APIs and receiving transcription results. It utilizes a global HTTP client with connection pooling for efficient network communication. Supports both direct processing and streaming for real-time transcription. ```rust use crate::api::client::{ApiClient, HTTP_CLIENT}; use crate::config::ConfigManager; use reqwest::multipart::{Form, Part}; // Using the ApiClient struct let client = ApiClient::new(); let config = ConfigManager::new(); // Process audio and get transcription let wav_audio_data: Vec = /* WAV bytes */; match client.process_audio(wav_audio_data.clone(), &config).await { Ok(text) => println!("Result: {}", text), Err(e) => eprintln!("Error: {}", e), } // Streaming audio processing (for real-time transcription) let partial_result = client.process_audio_streaming(wav_audio_data, &config).await?; // Direct API call using HTTP_CLIENT let api_key = config.get_api_key(); let api_url = config.get_api_url(); let form = Form::new() .text("model", config.get_model_name()) .text("language", "zh") .part("file", Part::bytes(wav_audio_data) .file_name("recording.wav") .mime_str("audio/wav")?); let response = HTTP_CLIENT .post(&api_url) .header("Authorization", format!("Bearer {}", api_key)) .multipart(form) .send() .await?; #[derive(serde::Deserialize)] struct TranscriptionResponse { text: String, } let result: TranscriptionResponse = response.json().await?; println!("Transcribed text: {}", result.text); ``` -------------------------------- ### API Client Source: https://context7.com/guchang233/voice2type/llms.txt Provides methods for interacting with speech recognition APIs, including sending audio data and receiving transcription results. Supports both single requests and streaming for real-time transcription. ```APIDOC ## API Client The `ApiClient` provides methods for sending audio to speech recognition APIs and receiving transcription results. It utilizes a global HTTP client with connection pooling for efficiency. ### Methods - **`process_audio(wav_audio_data: Vec, config: &ConfigManager) -> Result`**: Sends WAV audio data to the API and returns the transcribed text. - **`process_audio_streaming(wav_audio_data: Vec, config: &ConfigManager) -> Result`**: Handles streaming audio for real-time transcription. ### Direct API Call Example (Rust) ```rust use crate::api::client::{ApiClient, HTTP_CLIENT}; use crate::config::ConfigManager; use reqwest::multipart::{Form, Part}; // Initialize client and configuration let client = ApiClient::new(); let config = ConfigManager::new(); // Process audio using the client let wav_audio_data: Vec = /* WAV bytes */; match client.process_audio(wav_audio_data.clone(), &config).await { Ok(text) => println!("Result: {}", text), Err(e) => eprintln!("Error: {}", e), } // Direct HTTP request using the global client let api_key = config.get_api_key(); let api_url = config.get_api_url(); let form = Form::new() .text("model", config.get_model_name()) .text("language", "zh") .part("file", Part::bytes(wav_audio_data) .file_name("recording.wav") .mime_str("audio/wav")?); let response = HTTP_CLIENT .post(&api_url) .header("Authorization", format!("Bearer {}", api_key)) .multipart(form) .send() .await?; #[derive(serde::Deserialize)] struct TranscriptionResponse { text: String, } let result: TranscriptionResponse = response.json().await?; println!("Transcribed text: {}", result.text); ``` ``` -------------------------------- ### Audio Processing Source: https://context7.com/guchang233/voice2type/llms.txt Handles audio resampling, format conversion to 16kHz mono WAV, and WAV encoding in memory. Also includes functionality for calculating audio energy for Voice Activity Detection (VAD). ```APIDOC ## Audio Processing The audio processor handles resampling, format conversion, and WAV encoding for speech recognition APIs. Audio is captured at the system's default sample rate and converted to 16kHz mono WAV format. ### Functionality - **Resampling and Conversion**: Converts audio samples from an input format (e.g., f32) and sample rate to 16kHz mono i16 format. - **WAV Encoding**: Encodes processed audio samples into WAV format in memory. - **Audio Energy Calculation**: Computes the energy level of audio input, useful for Voice Activity Detection (VAD). ### Usage Example (Rust) ```rust use crate::audio::processor::{resample_and_convert, encode_wav_memory, calculate_audio_energy}; // Resample and convert audio from f32 to i16 format let input_samples: Vec = /* audio samples from microphone */; let input_rate: u32 = 48000; // Original sample rate let (resampled_i16, output_rate) = resample_and_convert(&input_samples, input_rate); // output_rate will be 16000 (16kHz) // Encode samples to WAV format in memory let wav_bytes = encode_wav_memory(&resampled_i16, output_rate) .expect("Failed to encode WAV"); // Calculate audio energy for VAD let energy = calculate_audio_energy(&input_samples); println!("Audio energy level: {}", energy); // Full pipeline example let raw_audio: Vec = /* captured f32 audio at 48kHz stereo */; let sample_rate = 48000u32; // 1. Resample to 16kHz mono i16 let (processed, _) = resample_and_convert(&raw_audio, sample_rate); // 2. Encode to WAV bytes let wav_data = encode_wav_memory(&processed, 16000)?; // wav_data is now ready for API submission ``` ``` -------------------------------- ### TCP API Server Source: https://context7.com/guchang233/voice2type/llms.txt Exposes a TCP API on port 8080 for external applications to control recording, access configuration, and manage the Voice2Type application. Requests and responses are JSON-formatted. ```APIDOC ## TCP API Server When compiled with the `api_server` feature, Voice2Type exposes a TCP API on port 8080. This allows external applications to integrate by sending JSON-formatted commands to control recording and access configuration. ### Enabling the Feature Add the following to your `Cargo.toml`: ```toml voice2type = { version = "*", features = ["api_server"] } ``` ### API Request Types (JSON) - **Start Recording**: `{"StartRecording": null}` - **Stop Recording**: `{"StopRecording": null}` - **Cancel Recording**: `{"CancelRecording": null}` - **Get Current State**: `{"GetState": null}` - **Set Configuration**: `{"SetConfig": {"key": "output_mode", "value": "clipboard"}}` - **Get Configuration**: `{"GetConfig": {"key": "api_key"}}` ### Example Usage (netcat) ```bash # Start recording $ echo '{"StartRecording": null}' | nc localhost 8080 # Stop recording $ echo '{"StopRecording": null}' | nc localhost 8080 # Get current state $ echo '{"GetState": null}' | nc localhost 8080 ``` ### Response Formats - **Success**: `{"Success": {"message": "Request received"}}` - **Error**: `{"Error": {"message": "error description"}}` - **State Response**: `{"StateResponse": {"state": "idle"}}` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.