optional process to exec. Spawn without wait, not sure how legit
This commit is contained in:
parent
3a8138c901
commit
d443a36f7a
1 changed files with 16 additions and 5 deletions
21
src/main.zig
21
src/main.zig
|
@ -9,6 +9,7 @@ var should_exit = std.atomic.Value(bool).init(false);
|
||||||
|
|
||||||
/// Demo implementation of speech event handler with comprehensive error handling
|
/// Demo implementation of speech event handler with comprehensive error handling
|
||||||
const SpeechHandler = struct {
|
const SpeechHandler = struct {
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
speech_count: u32 = 0,
|
speech_count: u32 = 0,
|
||||||
error_count: u32 = 0,
|
error_count: u32 = 0,
|
||||||
warning_count: u32 = 0,
|
warning_count: u32 = 0,
|
||||||
|
@ -24,13 +25,19 @@ const SpeechHandler = struct {
|
||||||
|
|
||||||
// Print with timestamp for better experience
|
// Print with timestamp for better experience
|
||||||
const timestamp = std.time.timestamp();
|
const timestamp = std.time.timestamp();
|
||||||
std.debug.print("[{}] Speech #{}: {s}\n", .{ timestamp, self.speech_count, text });
|
std.log.debug("[{}] Speech {}->{?s}: {s}", .{
|
||||||
|
timestamp,
|
||||||
|
self.speech_count,
|
||||||
|
self.exec_program,
|
||||||
|
text,
|
||||||
|
});
|
||||||
|
|
||||||
// Execute program if specified
|
// Execute program if specified
|
||||||
if (self.exec_program) |program| {
|
if (self.exec_program) |program| {
|
||||||
var child = std.process.Child.init(&[_][]const u8{ program, text }, std.heap.page_allocator);
|
var child = std.process.Child.init(&[_][]const u8{ program, text }, self.allocator);
|
||||||
_ = child.spawn() catch |err| {
|
child.spawn() catch |err| {
|
||||||
std.log.err("Failed to execute program '{s}': {}", .{ program, err });
|
std.log.err("Failed to execute program '{s}': {}", .{ program, err });
|
||||||
|
return;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -179,7 +186,10 @@ pub fn main() !void {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create handler with statistics tracking
|
// Create handler with statistics tracking
|
||||||
var handler = SpeechHandler{ .exec_program = exec_program };
|
var handler = SpeechHandler{
|
||||||
|
.allocator = allocator,
|
||||||
|
.exec_program = exec_program,
|
||||||
|
};
|
||||||
const speech_handler = stt.SpeechEventHandler{
|
const speech_handler = stt.SpeechEventHandler{
|
||||||
.onSpeechFn = SpeechHandler.onSpeech,
|
.onSpeechFn = SpeechHandler.onSpeech,
|
||||||
.onErrorFn = SpeechHandler.onError,
|
.onErrorFn = SpeechHandler.onError,
|
||||||
|
@ -244,6 +254,7 @@ pub fn main() !void {
|
||||||
_ = stdout.writeAll("\n") catch {};
|
_ = stdout.writeAll("\n") catch {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std.log.info("Program to execute on speech detection: {?s}", .{exec_program});
|
||||||
std.log.info("STT library initialized successfully with configuration:", .{});
|
std.log.info("STT library initialized successfully with configuration:", .{});
|
||||||
std.log.info(" Model path: {s}", .{options.model_path});
|
std.log.info(" Model path: {s}", .{options.model_path});
|
||||||
std.log.info(" Audio device: {s}", .{options.audio_device});
|
std.log.info(" Audio device: {s}", .{options.audio_device});
|
||||||
|
@ -316,7 +327,7 @@ pub fn main() !void {
|
||||||
test "handler functionality" {
|
test "handler functionality" {
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
|
|
||||||
var handler = SpeechHandler{};
|
var handler = SpeechHandler{ .allocator = std.testing.allocator };
|
||||||
const speech_handler = stt.SpeechEventHandler{
|
const speech_handler = stt.SpeechEventHandler{
|
||||||
.onSpeechFn = SpeechHandler.onSpeech,
|
.onSpeechFn = SpeechHandler.onSpeech,
|
||||||
.onErrorFn = SpeechHandler.onError,
|
.onErrorFn = SpeechHandler.onError,
|
||||||
|
|
Loading…
Add table
Reference in a new issue