105 lines
3.5 KiB
Zig
105 lines
3.5 KiB
Zig
//! STT Library Demo Application
|
|
//!
|
|
//! This demonstrates how to use the STT library for speech recognition.
|
|
//! It will be updated in subsequent tasks to use the actual Vosk integration.
|
|
|
|
const std = @import("std");
|
|
const stt = @import("root.zig");
|
|
|
|
/// Demo implementation of speech event handler
|
|
const DemoHandler = struct {
|
|
/// Handle detected speech
|
|
fn onSpeech(ctx: *anyopaque, text: []const u8) void {
|
|
const self: *DemoHandler = @ptrCast(@alignCast(ctx));
|
|
_ = self; // Handler context not used in this simple demo
|
|
|
|
std.debug.print("Detected: {s}\n", .{text});
|
|
}
|
|
|
|
/// Handle errors
|
|
fn onError(ctx: *anyopaque, error_code: stt.SttError, message: []const u8) void {
|
|
const self: *DemoHandler = @ptrCast(@alignCast(ctx));
|
|
_ = self; // Handler context not used in this simple demo
|
|
|
|
std.debug.print("Error {}: {s}\n", .{ error_code, message });
|
|
}
|
|
};
|
|
|
|
pub fn main() !void {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
defer _ = gpa.deinit();
|
|
const allocator = gpa.allocator();
|
|
|
|
std.debug.print("STT Library Demo\n", .{});
|
|
std.debug.print("================\n", .{});
|
|
|
|
// Create demo handler
|
|
var demo_handler = DemoHandler{};
|
|
const speech_handler = stt.SpeechEventHandler{
|
|
.onSpeechFn = DemoHandler.onSpeech,
|
|
.onErrorFn = DemoHandler.onError,
|
|
.ctx = &demo_handler,
|
|
};
|
|
|
|
// Initialize STT session with configuration
|
|
const options = stt.SttOptions{
|
|
.model_path = "zig-out/bin/vosk-model-small-en-us-0.15",
|
|
.audio_device = "hw:3,0",
|
|
.event_handler = speech_handler,
|
|
.sample_rate = 16000,
|
|
.channels = 2,
|
|
.buffer_size = 256,
|
|
};
|
|
|
|
var session = stt.SttSession.init(allocator, options) catch |err| {
|
|
std.debug.print("Failed to initialize STT library: {}\n", .{err});
|
|
return;
|
|
};
|
|
defer session.deinit();
|
|
|
|
std.debug.print("STT library initialized successfully\n", .{});
|
|
std.debug.print("Model path: {s}\n", .{options.model_path});
|
|
std.debug.print("Audio device: {s}\n", .{options.audio_device});
|
|
std.debug.print("Sample rate: {} Hz\n", .{options.sample_rate});
|
|
std.debug.print("Channels: {}\n", .{options.channels});
|
|
std.debug.print("Buffer size: {} frames\n", .{options.buffer_size});
|
|
std.debug.print("\n", .{});
|
|
|
|
// Start listening for speech
|
|
session.start_listening() catch |err| {
|
|
std.debug.print("Failed to start listening: {}\n", .{err});
|
|
return;
|
|
};
|
|
|
|
std.debug.print("Listening for speech... (Press Enter to exit)\n", .{});
|
|
|
|
// Wait for user input to exit (simulating Ctrl+C behavior)
|
|
// In subsequent tasks, this will be replaced with actual audio processing
|
|
const stdin = std.fs.File.stdin();
|
|
var buffer: [1]u8 = undefined;
|
|
_ = stdin.read(&buffer) catch {};
|
|
|
|
std.debug.print("\nStopping speech recognition...\n", .{});
|
|
session.stop_listening();
|
|
|
|
std.debug.print("Demo completed successfully\n", .{});
|
|
}
|
|
|
|
// Test the demo functionality
|
|
test "demo handler functionality" {
|
|
const testing = std.testing;
|
|
|
|
var demo_handler = DemoHandler{};
|
|
const speech_handler = stt.SpeechEventHandler{
|
|
.onSpeechFn = DemoHandler.onSpeech,
|
|
.onErrorFn = DemoHandler.onError,
|
|
.ctx = &demo_handler,
|
|
};
|
|
|
|
// Test that callbacks can be invoked without crashing
|
|
speech_handler.onSpeech("test speech");
|
|
speech_handler.onError(stt.SttError.AudioDeviceError, "test error");
|
|
|
|
// If we get here without crashing, the test passes
|
|
try testing.expect(true);
|
|
}
|