From 6fe5bba155f94f67437c4f109e90bba282d22dd0 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Wed, 10 Sep 2025 18:22:18 -0700 Subject: [PATCH] better modeling --- src/main.zig | 69 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/src/main.zig b/src/main.zig index 196f85c..853495a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -132,6 +132,7 @@ fn signalHandler(sig: c_int) callconv(.c) void { pub fn main() !void { const stdout = std.fs.File.stdout(); + const stderr = std.fs.File.stderr(); var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); @@ -164,10 +165,55 @@ pub fn main() !void { .ctx = &demo_handler, }; - // Initialize STT session with existing audio device configuration - // These parameters maintain the existing working behavior + // Parse command line arguments + const args = try std.process.argsAlloc(allocator); + defer std.process.argsFree(allocator, args); + + var model_path: ?[]const u8 = null; + + // Parse --model argument + for (args[1..]) |arg| { + if (std.mem.startsWith(u8, arg, "--model=")) { + model_path = arg[8..]; // Skip "--model=" + break; + } + } + + // If no model specified, try default locations + const default_paths = [_][]const u8{ + "vosk-model-small-en-us-0.15", + "zig-out/bin/vosk-model-small-en-us-0.15", + "/usr/share/vosk/models/vosk-model-small-en-us-0.15", + }; + + if (model_path == null) { + for (default_paths) |path| { + std.fs.cwd().access(path, .{}) catch continue; + model_path = path; + break; + } + } + + // Check if model path exists + if (model_path == null) { + _ = try stderr.writeAll("Error: Vosk model not found.\n\n"); + _ = try stderr.writeAll("Usage: stt-demo [--model=]\n\n"); + _ = try stderr.writeAll("Locations searched:\n"); + inline for (default_paths) |path| + _ = try stderr.writeAll("\t" ++ path ++ "\n"); + _ = try stderr.writeAll("Please download the model. A fine model can be downloaded from:\n"); + _ = try stderr.writeAll("\thttps://alphacephei.com/vosk/models/vosk-model-small-en-us-0.15.zip\n"); + std.process.exit(1); + } + + std.fs.cwd().access(model_path.?, .{}) catch { + std.log.err("Model path does not exist: {s}", .{model_path.?}); + std.process.exit(1); + }; + + // Initialize STT session with resolved model path const options = stt.SttOptions{ - .model_path = "zig-out/bin/vosk-model-small-en-us-0.15", + .model_path = model_path.?, .audio_device = "default", // Use ALSA default device from alsa.conf .event_handler = speech_handler, .sample_rate = 16000, // Standard sample rate for speech recognition @@ -249,20 +295,11 @@ pub fn main() !void { std.log.info(" Speech detections: {}", .{stats.speech_count}); std.log.info(" Fatal errors: {}", .{stats.error_count}); std.log.info(" Recoverable errors: {}", .{stats.recoverable_error_count}); - std.log.info(" Total issues: {}", .{stats.total_issues}); + std.log.info(" Total issues: {}\n", .{stats.total_issues}); - if (stats.speech_count > 0) { - std.log.info("✓ Speech recognition worked successfully!", .{}); - if (stats.recoverable_error_count > 0) { - std.log.info("ℹ System recovered from {} issues during operation.", .{stats.recoverable_error_count}); - } - } else if (stats.error_count > 0) { - std.log.info("✗ Fatal errors occurred during speech recognition.", .{}); - } else if (stats.recoverable_error_count > 0) { - std.log.info("⚠ Recoverable issues occurred but system continued operating.", .{}); - } else { - std.log.info("ℹ No speech was detected during this session.", .{}); - } + // Print seperately since ^^ are info calls and vv is an error call + if (stats.error_count > 0) + std.log.err("✗ {d} fatal errors occurred during speech recognition.", .{stats.error_count}); _ = stdout.writeAll("Session completed successfully.\n") catch {}; }