songrec/
config.rs

1use serde::{Deserialize, Serialize};
2
3/// Configuration for SongRec
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct Config {
6    /// Recognition sensitivity (0.0 to 1.0)
7    pub sensitivity: f32,
8    
9    /// Timeout for network requests in seconds
10    pub network_timeout: u64,
11    
12    /// Minimum duration of audio to analyze (in seconds)
13    pub min_audio_duration: f32,
14    
15    /// Maximum duration of audio to analyze (in seconds)  
16    pub max_audio_duration: f32,
17    
18    /// Sample rate for audio processing
19    pub sample_rate: u32,
20    
21    /// Buffer size for audio processing
22    pub buffer_size: usize,
23    
24    /// Whether to enable continuous recognition
25    pub continuous_recognition: bool,
26    
27    /// Interval between recognition attempts in continuous mode (seconds)
28    pub recognition_interval: f32,
29    
30    /// Whether to suppress verbose debug output
31    pub quiet_mode: bool,
32    
33    /// Whether to deduplicate requests (prevent sending same signature multiple times)
34    pub deduplicate_requests: bool,
35    
36    /// Time in seconds to remember signatures for deduplication
37    pub deduplication_cache_duration: u64,
38}
39
40impl Default for Config {
41    fn default() -> Self {
42        Self {
43            sensitivity: 0.5,
44            network_timeout: 20,
45            min_audio_duration: 3.0,
46            max_audio_duration: 12.0,
47            sample_rate: 16000,
48            buffer_size: 4096,
49            continuous_recognition: false,
50            recognition_interval: 5.0,
51            quiet_mode: true, // Default to quiet mode for clean output
52            deduplicate_requests: true,
53            deduplication_cache_duration: 300, // 5 minutes
54        }
55    }
56}
57
58impl Config {
59    /// Create a new config with default values
60    pub fn new() -> Self {
61        Self::default()
62    }
63    
64    /// Set the sensitivity level
65    pub fn with_sensitivity(mut self, sensitivity: f32) -> Self {
66        self.sensitivity = sensitivity.clamp(0.0, 1.0);
67        self
68    }
69    
70    /// Set the network timeout
71    pub fn with_network_timeout(mut self, timeout: u64) -> Self {
72        self.network_timeout = timeout;
73        self
74    }
75    
76    /// Set the minimum audio duration
77    pub fn with_min_audio_duration(mut self, duration: f32) -> Self {
78        self.min_audio_duration = duration;
79        self
80    }
81    
82    /// Set the maximum audio duration
83    pub fn with_max_audio_duration(mut self, duration: f32) -> Self {
84        self.max_audio_duration = duration;
85        self
86    }
87    
88    /// Set the sample rate
89    pub fn with_sample_rate(mut self, sample_rate: u32) -> Self {
90        self.sample_rate = sample_rate;
91        self
92    }
93    
94    /// Set the buffer size
95    pub fn with_buffer_size(mut self, buffer_size: usize) -> Self {
96        self.buffer_size = buffer_size;
97        self
98    }
99    
100    /// Enable or disable continuous recognition
101    pub fn with_continuous_recognition(mut self, enabled: bool) -> Self {
102        self.continuous_recognition = enabled;
103        self
104    }
105    
106    /// Set the recognition interval for continuous mode
107    pub fn with_recognition_interval(mut self, interval: f32) -> Self {
108        self.recognition_interval = interval;
109        self
110    }
111    
112    /// Enable or disable quiet mode (suppress verbose output)
113    pub fn with_quiet_mode(mut self, quiet: bool) -> Self {
114        self.quiet_mode = quiet;
115        self
116    }
117    
118    /// Enable or disable request deduplication
119    pub fn with_deduplication(mut self, enabled: bool) -> Self {
120        self.deduplicate_requests = enabled;
121        self
122    }
123    
124    /// Set the deduplication cache duration
125    pub fn with_deduplication_cache_duration(mut self, duration: u64) -> Self {
126        self.deduplication_cache_duration = duration;
127        self
128    }
129    
130    /// Load configuration from a TOML file
131    pub fn from_file(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
132        let content = std::fs::read_to_string(path)?;
133        let config: Config = toml::from_str(&content)?;
134        Ok(config)
135    }
136    
137    /// Save configuration to a TOML file
138    pub fn to_file(&self, path: &str) -> Result<(), Box<dyn std::error::Error>> {
139        let content = toml::to_string_pretty(self)?;
140        std::fs::write(path, content)?;
141        Ok(())
142    }
143}