aboutsummaryrefslogtreecommitdiff
path: root/src/args.rs
blob: 5ae1c6948b4764b0f18d8c0a2af1d7072d980e26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use std::path::PathBuf;

/// Whether verbose debug output is enabled.
pub static VERBOSE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Print a debug message only when `--verbose` is set.
#[macro_export]
macro_rules! debug_log {
    ($($arg:tt)*) => {
        if $crate::args::VERBOSE.load(std::sync::atomic::Ordering::Relaxed) {
            eprintln!($($arg)*);
        }
    };
}

/// The full help text embedded at compile time.
pub static HELP_TEXT: &str = include_str!("../help.txt");

// ===========================================================================
// CLI arguments (clap)
// ===========================================================================

#[derive(clap::Parser)]
#[command(name = "dmvop", disable_help_flag = true, disable_version_flag = true)]
pub struct DMVOPArguments {
    // Show help
    #[arg(long = "help", short = 'h')]
    pub help: bool,

    // Instant mode — aggressive VAD for near-real-time output
    #[arg(long = "instant")]
    pub instant: bool,

    // Verbose output (show debug messages)
    #[arg(long = "verbose", short = 'V')]
    pub verbose: bool,

    // Config file path
    #[arg(long = "config", require_equals = true)]
    pub config: Option<PathBuf>,

    // List all available input devices and exit
    #[arg(long = "list-devices", alias = "list", short = 'L')]
    pub list_devices: bool,

    // List all available Whisper models and exit
    #[arg(long = "list-models")]
    pub list_models: bool,

    // Download a specific Whisper model and exit
    #[arg(long = "download-model", alias = "get-model", require_equals = true)]
    pub download_model: Option<String>,

    // Language hint for Whisper (e.g. en, zh, ja). Auto-detects if not set.
    #[arg(long = "lang", require_equals = true)]
    pub lang: Option<String>,

    // Whisper model to use (e.g. tiny, base, small, medium, large-v3)
    #[arg(
        long = "model",
        short = 'm',
        default_value = "base_en",
        require_equals = true
    )]
    pub model: String,

    // Devices (unix/linux device or WASAPI name)
    #[arg(
        long = "device",
        alias = "dev",
        allow_hyphen_values = true,
        require_equals = true,
        default_value = "auto"
    )]
    pub device_name: Option<String>,

    // Format
    // Use %{param} to represent a parameter
    //
    // Supported parameters:
    // vol   : volume 0-100
    // word  : word
    // confid: confidence
    #[arg(long = "format", short, alias = "fmt", require_equals = true)]
    pub format_pattern: Option<String>,

    #[arg(long = "format-file", short = 'S', require_equals = true)]
    pub format_file: Option<PathBuf>,

    // Output (can be specified multiple times)
    #[arg(
        long,
        short = 'O',
        require_equals = true,
        default_value = "stdout",
        num_args = 1
    )]
    pub output: Vec<OutputMode>,

    // MISC
    // Port (default: 5117)
    #[arg(long, short = 'p', default_value_t = 5117, require_equals = true)]
    pub port: u16,

    // Socket file (default: ./dmvop.sock in current directory)
    #[arg(
        long = "socket-file",
        alias = "socket",
        default_value = "./dmvop.sock",
        require_equals = true
    )]
    pub socket_file: PathBuf,

    // Custom directory for model files
    #[arg(long = "models-dir", alias = "models", require_equals = true)]
    pub models_dir: Option<PathBuf>,

    // Subnet mask for UDP broadcast (default: only last octet, e.g., "255.255.255.255.0")
    #[arg(
        long = "subnet-mask",
        alias = "mask",
        default_value = "255.255.255.0",
        require_equals = true
    )]
    pub subnet_mask: String,

    // Post-processing pipeline, e.g. --post="+pinyin()" or --post="+reverse"
    #[arg(long = "post", require_equals = true)]
    pub post: Option<String>,
}

// ===========================================================================
// Configuration file (serde) — mirrors DMVOPArguments with all-Option fields
// ===========================================================================

/// Mirrors [`DMVOPArguments`] as a TOML-serialisable config.
/// Every field is `Option` so the merge logic can tell what was explicitly set.
#[derive(serde::Deserialize, Default)]
#[serde(default)]
pub struct DMVOPConfig {
    pub instant: Option<bool>,
    pub lang: Option<String>,
    pub model: Option<String>,
    pub device: Option<String>,
    pub format: Option<String>,
    pub format_file: Option<PathBuf>,
    pub output: Option<Vec<String>>,
    pub port: Option<u16>,
    pub socket_file: Option<PathBuf>,
    pub models_dir: Option<PathBuf>,
    pub subnet_mask: Option<String>,
    pub post: Option<String>,
}

/// Resolve a path in the config file relative to the config file's directory.
fn resolve_config_path(config_file: &PathBuf, path: &PathBuf) -> PathBuf {
    if path.is_absolute() {
        path.clone()
    } else if let Some(parent) = config_file.parent() {
        parent.join(path)
    } else {
        path.clone()
    }
}

/// Load config file, then merge CLI args on top.
/// Config paths are resolved relative to the config file's directory.
pub fn load_and_merge_config(config: Option<&PathBuf>) -> Option<DMVOPConfig> {
    let config_path = config.cloned().or_else(|| {
        let fallback = PathBuf::from("./dmvop.toml");
        if fallback.exists() {
            Some(fallback)
        } else {
            None
        }
    });

    let config_path = config_path?;
    let content = match std::fs::read_to_string(&config_path) {
        Ok(c) => c,
        Err(e) => {
            eprintln!(
                "[dmvop] Failed to read config '{}': {}",
                config_path.display(),
                e
            );
            std::process::exit(1);
        }
    };

    let mut cfg: DMVOPConfig = match toml::from_str(&content) {
        Ok(c) => c,
        Err(e) => {
            eprintln!(
                "[dmvop] Failed to parse config '{}': {}",
                config_path.display(),
                e
            );
            std::process::exit(1);
        }
    };

    // Resolve relative paths
    if let Some(ref mut f) = cfg.format_file {
        *f = resolve_config_path(&config_path, f);
    }
    if let Some(ref mut d) = cfg.models_dir {
        *d = resolve_config_path(&config_path, d);
    }
    if let Some(ref mut s) = cfg.socket_file {
        *s = resolve_config_path(&config_path, s);
    }

    Some(cfg)
}

/// Override args with config file values (config wins over CLI).
/// When a config file is present, its values take full precedence.
pub fn apply_config(args: &mut DMVOPArguments, cfg: &DMVOPConfig) {
    if let Some(v) = cfg.instant {
        args.instant = v;
    }
    if let Some(ref v) = cfg.lang {
        args.lang = Some(v.clone());
    }
    if let Some(ref v) = cfg.model {
        args.model = v.clone();
    }
    if let Some(ref v) = cfg.device {
        args.device_name = Some(v.clone());
    }
    if let Some(ref v) = cfg.format {
        args.format_pattern = Some(v.clone());
    }
    if let Some(ref v) = cfg.format_file {
        args.format_file = Some(v.clone());
    }
    if let Some(ref v) = cfg.output {
        args.output = v.iter().filter_map(|s| s.parse().ok()).collect();
    }
    if let Some(v) = cfg.port {
        args.port = v;
    }
    if let Some(ref v) = cfg.socket_file {
        args.socket_file = v.clone();
    }
    if let Some(ref v) = cfg.models_dir {
        args.models_dir = Some(v.clone());
    }
    if let Some(ref v) = cfg.subnet_mask {
        args.subnet_mask = v.clone();
    }
    if let Some(ref v) = cfg.post {
        args.post = Some(v.clone());
    }
}

// ===========================================================================
// Output mode enum
// ===========================================================================

#[derive(Clone, Debug)]
#[allow(non_camel_case_types)]
pub enum OutputMode {
    /// Establish a basic TCP service, broadcasting output to all connected sockets
    TCP,
    /// Establish a basic UDP service, broadcasting output to all connected sockets
    UDP,
    /// Broadcast output via UDP broadcast
    UDP_BROADCAST,
    /// Send signal to specified IPC socket
    IPC,
    /// Write directly to stdout
    STDOUT,
    /// Write directly to stderr
    STDERR,
}

impl std::str::FromStr for OutputMode {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "tcp" => Ok(OutputMode::TCP),
            "udp" => Ok(OutputMode::UDP),
            "udp-broadcast" => Ok(OutputMode::UDP_BROADCAST),
            "ipc" => Ok(OutputMode::IPC),
            "stdout" => Ok(OutputMode::STDOUT),
            "stderr" => Ok(OutputMode::STDERR),
            _ => Err(format!(
                "'{}' is not a valid output mode. Valid modes: tcp, udp, udp-broadcast, ipc, stdout, stderr",
                s
            )),
        }
    }
}

// ===========================================================================
// Format helpers
// ===========================================================================

pub fn format_output(pattern: &str, word: &str, confidence: f32, volume: f32) -> String {
    let mut result = pattern.to_string();

    // Volume: clamp dB range (~-60 to 0) to 0–100 scale
    // Typical speech is around -30 dB to -12 dB
    let vol_normalized = ((volume + 60.0).clamp(0.0, 60.0) / 60.0 * 100.0) as u32;

    // Replace known placeholders
    result = result.replace("%{vol}", &vol_normalized.to_string());
    result = result.replace("%{word}", word);
    result = result.replace("%{confid}", &format!("{:.1}", confidence));
    result = result.replace("%{confidence}", &format!("{:.1}", confidence));

    result
}

/// Parse a post-processor spec in the form `+function_name(arg1, arg2, ...)`.
/// Returns `(name, args)`.
pub fn parse_post_spec(spec: &str) -> (&str, Vec<&str>) {
    let spec = spec.trim();
    if !spec.starts_with('+') {
        return (spec, vec![]);
    }
    let inner = &spec[1..];
    if let Some(paren) = inner.find('(') {
        if inner.ends_with(')') {
            let name = inner[..paren].trim();
            let args_str = inner[paren + 1..inner.len() - 1].trim();
            let args: Vec<&str> = if args_str.is_empty() {
                vec![]
            } else {
                // Simple comma split (no nested parens, no escape handling for now)
                args_str.split(',').map(|a| a.trim()).collect()
            };
            return (name, args);
        }
    }
    // No parens → no args
    (inner.trim(), vec![])
}

/// Run the post-processing pipeline on the given text.
pub fn run_post_process(text: &str, spec: Option<&str>) -> String {
    let Some(spec) = spec else {
        return text.to_string();
    };
    let (name, args) = parse_post_spec(spec);
    match crate::post_proc::run(text, name, &args) {
        Some(result) => result,
        None => {
            eprintln!("[dmvop] Unknown post-processor: '{}'", name);
            text.to_string()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_output() {
        let s = format_output("%{vol},%{word},%{confid}", "hello", 95.0, -12.0);
        assert_eq!(s, "80,hello,95.0");
    }

    #[test]
    fn test_format_output_no_volume() {
        let s = format_output("text: %{word}", "world", 0.0, -60.0);
        assert_eq!(s, "text: world");
    }
}