stt/src/stt.zig

1731 lines
67 KiB
Zig

//! STT (Speech-to-Text) Library
//!
//! This library provides callback-based speech recognition functionality
//! using Vosk and ALSA for audio capture.
const std = @import("std");
const c = @cImport({
@cInclude("alsa/asoundlib.h");
@cInclude("vosk_api.h");
});
/// Core error types for the STT library
pub const Error = error{
/// Failed to initialize the library (model loading, audio setup, etc.)
InitializationFailed,
/// Audio device access or configuration error
AudioDeviceError,
/// Failed to load the speech recognition model
ModelLoadError,
/// Error occurred during callback execution
CallbackError,
/// Memory allocation failed
OutOfMemory,
/// Invalid parameters provided
InvalidParameter,
/// Library is not in the correct state for the operation
InvalidState,
/// Threading or synchronization error
ThreadingError,
/// Audio device is busy or in use by another application
AudioDeviceBusy,
/// Audio device does not exist or is not accessible
AudioDeviceNotFound,
/// Audio device configuration is not supported
AudioDeviceUnsupported,
/// Model file is corrupted or invalid format
ModelCorrupted,
/// Model file not found at specified path
ModelNotFound,
/// Insufficient permissions to access resources
PermissionDenied,
/// System resources exhausted (file descriptors, etc.)
SystemResourcesExhausted,
/// Operation timed out
Timeout,
/// Internal library error (should not normally occur)
InternalError,
SetAccessError,
SetFormatError,
SetChannelError,
SetSampleRateError,
SetBufferSizeError,
SetPeriodSizeError,
ApplyParametersError,
PcmPrepareError,
};
/// Detailed error information structure
pub const ErrorInfo = struct {
/// The error code
error_code: Error,
/// Human-readable error message
message: []const u8,
/// Optional system error code (errno, ALSA error, etc.)
system_error: ?i32 = null,
/// Optional context information (file path, device name, etc.)
context: ?[]const u8 = null,
/// Timestamp when error occurred
timestamp: i64,
/// Whether this error is recoverable
recoverable: bool = false,
/// Suggested recovery action
recovery_suggestion: ?[]const u8 = null,
/// Create a new error info structure
pub fn init(error_code: Error, message: []const u8) ErrorInfo {
return ErrorInfo{
.error_code = error_code,
.message = message,
.timestamp = std.time.timestamp(),
};
}
/// Create error info with context
pub fn initWithContext(error_code: Error, message: []const u8, context: []const u8) ErrorInfo {
return ErrorInfo{
.error_code = error_code,
.message = message,
.context = context,
.timestamp = std.time.timestamp(),
};
}
/// Create recoverable error info with suggestion
pub fn initRecoverable(error_code: Error, message: []const u8, suggestion: []const u8) ErrorInfo {
return ErrorInfo{
.error_code = error_code,
.message = message,
.timestamp = std.time.timestamp(),
.recoverable = true,
.recovery_suggestion = suggestion,
};
}
};
/// Callback function type for speech detection events
///
/// Parameters:
/// - text: Null-terminated string containing the detected speech
/// - user_data: Optional user-provided context data
pub const SpeechCallback = *const fn (text: [*:0]const u8, user_data: ?*anyopaque) void;
/// Callback function type for error events
///
/// Parameters:
/// - error_code: The specific error that occurred
/// - message: Null-terminated string with error details
/// - user_data: Optional user-provided context data
pub const ErrorCallback = *const fn (error_code: Error, message: [*:0]const u8, user_data: ?*anyopaque) void;
/// Enhanced callback function type for detailed error events
///
/// Parameters:
/// - error_info: Detailed error information structure
/// - user_data: Optional user-provided context data
pub const DetailedErrorCallback = *const fn (error_info: ErrorInfo, user_data: ?*anyopaque) void;
/// Speech event handler interface pattern
///
/// This provides a structured way to handle speech recognition events
/// with both speech detection and error handling callbacks.
pub const SpeechEventHandler = struct {
/// Function to call when speech is detected
onSpeechFn: *const fn (ctx: *anyopaque, text: []const u8) void,
/// Function to call when an error occurs
onErrorFn: *const fn (ctx: *anyopaque, error_code: Error, message: []const u8) void,
/// Optional function to call for detailed error information
onDetailedErrorFn: ?*const fn (ctx: *anyopaque, error_info: ErrorInfo) void = null,
/// Context pointer passed to callback functions
ctx: *anyopaque,
/// Invoke the speech detection callback with error handling
pub fn onSpeech(self: SpeechEventHandler, text: []const u8) void {
// Call the speech callback function
// Note: If the callback panics or causes undefined behavior,
// there's not much we can do to recover gracefully in Zig
self.onSpeechFn(self.ctx, text);
}
/// Invoke the error callback
pub fn onError(self: SpeechEventHandler, error_code: Error, message: []const u8) void {
self.onErrorFn(self.ctx, error_code, message);
}
/// Invoke the detailed error callback with comprehensive error information
pub fn onDetailedError(self: SpeechEventHandler, error_info: ErrorInfo) void {
if (self.onDetailedErrorFn) |detailed_fn| {
detailed_fn(self.ctx, error_info);
} else {
// Fall back to basic error callback
self.onError(error_info.error_code, error_info.message);
}
}
/// Internal helper to report errors with proper fallback
fn reportError(self: SpeechEventHandler, error_code: Error, error_info: ErrorInfo) void {
if (self.onDetailedErrorFn) |detailed_fn| {
detailed_fn(self.ctx, error_info);
} else {
self.onError(error_code, error_info.message);
}
}
};
/// Resample audio from input rate to output rate using linear interpolation
fn resample(input_samples: []const i16, output_samples: []i16, input_rate: u32, output_rate: u32) usize {
if (input_rate == output_rate) {
const copy_len = @min(input_samples.len, output_samples.len);
@memcpy(output_samples[0..copy_len], input_samples[0..copy_len]);
return copy_len;
}
const ratio = @as(f64, @floatFromInt(input_rate)) / @as(f64, @floatFromInt(output_rate));
const output_len = @min(output_samples.len, @as(usize, @intFromFloat(@as(f64, @floatFromInt(input_samples.len)) / ratio)));
for (0..output_len) |i| {
const src_pos = @as(f64, @floatFromInt(i)) * ratio;
const src_idx: usize = @intFromFloat(src_pos);
if (src_idx >= input_samples.len) break;
if (src_idx + 1 < input_samples.len) {
const frac = src_pos - @as(f64, @floatFromInt(src_idx));
const sample1: f64 = @floatFromInt(input_samples[src_idx]);
const sample2: f64 = @floatFromInt(input_samples[src_idx + 1]);
const interpolated = sample1 + (sample2 - sample1) * frac;
output_samples[i] = @intFromFloat(@max(@min(interpolated, std.math.maxInt(i16)), std.math.minInt(i16)));
} else {
output_samples[i] = input_samples[src_idx];
}
}
return output_len;
}
/// Get CPU performance metric from /proc/cpuinfo (BogoMIPS or MHz)
fn getCpuPerformance() !u32 {
const file = try std.fs.openFileAbsolute("/proc/cpuinfo", .{});
defer file.close();
var buf: [4096]u8 = undefined;
const bytes_read = try file.readAll(&buf);
var lines = std.mem.splitScalar(u8, buf[0..bytes_read], '\n');
while (lines.next()) |line| {
if (std.mem.startsWith(u8, line, "BogoMIPS")) {
var parts = std.mem.splitScalar(u8, line, ':');
_ = parts.next(); // Skip key
if (parts.next()) |value| {
const trimmed = std.mem.trim(u8, value, " \t");
return @intFromFloat(try std.fmt.parseFloat(f32, trimmed));
}
}
if (std.mem.startsWith(u8, line, "cpu MHz")) {
var parts = std.mem.splitScalar(u8, line, ':');
_ = parts.next(); // Skip key
if (parts.next()) |value| {
const trimmed = std.mem.trim(u8, value, " \t");
// Convert MHz to equivalent BogoMIPS scale for consistent thresholds
const mhz = try std.fmt.parseFloat(f32, trimmed);
return @intFromFloat(mhz / 20.0); // Rough conversion to BogoMIPS scale
}
}
}
return error.PerformanceNotFound; // Default fallback
}
/// Audio buffer for managing audio data flow using std.io interfaces
pub const AudioBuffer = struct {
const Self = @This();
/// Internal ring buffer for audio data
buffer: []i16,
/// Read position in the buffer
read_pos: usize = 0,
/// Write position in the buffer
write_pos: usize = 0,
/// Number of samples currently in buffer
count: usize = 0,
/// Mutex for thread-safe access
mutex: std.Thread.Mutex = .{},
/// Allocator used for buffer allocation
allocator: std.mem.Allocator,
/// Initialize audio buffer with specified capacity
pub fn init(allocator: std.mem.Allocator, buffer_capacity: usize) !Self {
const buffer = try allocator.alloc(i16, buffer_capacity);
return Self{
.buffer = buffer,
.allocator = allocator,
};
}
/// Deinitialize and free buffer memory
pub fn deinit(self: *Self) void {
self.allocator.free(self.buffer);
}
/// Write audio samples to the buffer (thread-safe)
pub fn write(self: *Self, samples: []const i16) usize {
self.mutex.lock();
defer self.mutex.unlock();
const available_space = self.buffer.len - self.count;
const to_write = @min(samples.len, available_space);
for (0..to_write) |i| {
self.buffer[self.write_pos] = samples[i];
self.write_pos = (self.write_pos + 1) % self.buffer.len;
}
self.count += to_write;
return to_write;
}
/// Read audio samples from the buffer (thread-safe)
pub fn read(self: *Self, samples: []i16) usize {
self.mutex.lock();
defer self.mutex.unlock();
const to_read = @min(samples.len, self.count);
for (0..to_read) |i| {
samples[i] = self.buffer[self.read_pos];
self.read_pos = (self.read_pos + 1) % self.buffer.len;
}
self.count -= to_read;
return to_read;
}
/// Get number of samples available for reading
pub fn available(self: *Self) usize {
self.mutex.lock();
defer self.mutex.unlock();
return self.count;
}
/// Get remaining capacity for writing
pub fn capacity(self: *Self) usize {
self.mutex.lock();
defer self.mutex.unlock();
return self.buffer.len - self.count;
}
/// Clear all data from buffer
pub fn clear(self: *Self) void {
self.mutex.lock();
defer self.mutex.unlock();
self.read_pos = 0;
self.write_pos = 0;
self.count = 0;
}
};
/// Audio format conversion utilities
pub const AudioConverter = struct {
/// Convert stereo samples to mono by averaging channels
pub fn stereoToMono(stereo_samples: []const i16, mono_samples: []i16) usize {
const frames = @min(stereo_samples.len / 2, mono_samples.len);
for (0..frames) |i| {
const left = stereo_samples[i * 2];
const right = stereo_samples[i * 2 + 1];
// Average the channels and clamp to prevent overflow
const avg: i32 = @divTrunc(@as(i32, left) + @as(i32, right), 2);
mono_samples[i] = @intCast(@max(@min(avg, std.math.maxInt(i16)), std.math.minInt(i16)));
}
return frames;
}
};
/// ALSA audio capture configuration and state
pub const AlsaCapture = struct {
const Self = @This();
/// ALSA PCM handle
pcm_handle: ?*c.snd_pcm_t = null,
/// Device name
device_name: []const u8,
/// Sample rate
sample_rate: u32,
/// Number of channels. Available after open()
channels: u32,
/// Buffer size in frames
buffer_size: u32,
/// Period size in frames
period_size: u32,
/// Audio buffer for captured data
audio_buffer: AudioBuffer,
/// Allocator for memory management
allocator: std.mem.Allocator,
/// Initialize ALSA capture with specified parameters
pub fn init(allocator: std.mem.Allocator, device_name: []const u8, sample_rate: u32, buffer_size: u32) !Self {
// Calculate period size (typically 1/4 of buffer size)
const period_size = buffer_size / 4;
// Create audio buffer (make it larger than ALSA buffer to prevent overruns)
const audio_buffer = try AudioBuffer.init(allocator, buffer_size * 4);
return Self{
.device_name = device_name,
.sample_rate = sample_rate,
.buffer_size = buffer_size,
.period_size = period_size,
.audio_buffer = audio_buffer,
// SAFETY: this is set based on number of channels detected during open()
.channels = undefined,
.allocator = allocator,
};
}
/// Deinitialize ALSA capture and free resources
pub fn deinit(self: *Self) void {
self.close();
self.audio_buffer.deinit();
}
/// Open ALSA device and configure parameters with detailed error reporting
pub fn open(self: *Self) !void {
// Convert device name to null-terminated string
const device_cstr = self.allocator.dupeZ(u8, self.device_name) catch {
return Error.OutOfMemory;
};
defer self.allocator.free(device_cstr);
// Open PCM device with detailed error handling
var err = c.snd_pcm_open(&self.pcm_handle, device_cstr.ptr, c.SND_PCM_STREAM_CAPTURE, 0);
if (err < 0) {
return switch (err) {
-c.ENOENT => Error.AudioDeviceNotFound,
-c.EBUSY => Error.AudioDeviceBusy,
-c.EACCES => Error.PermissionDenied,
-c.ENOMEM => Error.OutOfMemory,
-c.EMFILE, -c.ENFILE => Error.SystemResourcesExhausted,
else => Error.AudioDeviceError,
};
}
// Allocate hardware parameters structure
var hw_params: ?*c.snd_pcm_hw_params_t = null;
err = c.snd_pcm_hw_params_malloc(@ptrCast(&hw_params));
errdefer self.close();
if (err < 0) return Error.AudioDeviceError;
defer c.snd_pcm_hw_params_free(hw_params);
// Initialize hardware parameters
err = c.snd_pcm_hw_params_any(self.pcm_handle, hw_params);
if (err < 0) return Error.AudioDeviceError;
// Set access type to interleaved
err = c.snd_pcm_hw_params_set_access(self.pcm_handle, hw_params, c.SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) return Error.SetAccessError;
// Set sample format to 16-bit signed little endian
err = c.snd_pcm_hw_params_set_format(self.pcm_handle, hw_params, c.SND_PCM_FORMAT_S16_LE);
if (err < 0) return Error.SetFormatError;
// SAFETY: min/max is set in c calls before use just below
var min: c_uint = undefined;
err = c.snd_pcm_hw_params_get_channels_min(hw_params, &min);
if (err < 0) return Error.SetChannelError;
self.channels = min;
// Set number of channels
err = c.snd_pcm_hw_params_set_channels(self.pcm_handle, hw_params, self.channels);
if (err < 0) {
std.log.err("error setting number of channels. Must be at least {d}", .{min});
return Error.SetChannelError;
}
// Set sample rate
var actual_rate = self.sample_rate;
err = c.snd_pcm_hw_params_set_rate_near(self.pcm_handle, hw_params, &actual_rate, null);
if (err < 0) return Error.SetSampleRateError;
// Update sample rate if hardware doesn't support requested rate
if (actual_rate != self.sample_rate) {
std.log.info("Hardware doesn't support {}Hz, using {}Hz", .{ self.sample_rate, actual_rate });
self.sample_rate = actual_rate;
}
// Set buffer size
var actual_buffer_size: c.snd_pcm_uframes_t = self.buffer_size;
err = c.snd_pcm_hw_params_set_buffer_size_near(self.pcm_handle, hw_params, &actual_buffer_size);
if (err < 0) return Error.SetBufferSizeError;
// Set period size
var actual_period_size: c.snd_pcm_uframes_t = self.period_size;
err = c.snd_pcm_hw_params_set_period_size_near(self.pcm_handle, hw_params, &actual_period_size, null);
if (err < 0) return Error.SetPeriodSizeError;
// Apply hardware parameters
err = c.snd_pcm_hw_params(self.pcm_handle, hw_params);
if (err < 0) return Error.ApplyParametersError;
// Prepare the PCM for use
err = c.snd_pcm_prepare(self.pcm_handle);
if (err < 0) return Error.PcmPrepareError;
}
/// Close ALSA device
pub fn close(self: *Self) void {
if (self.pcm_handle) |handle| {
_ = c.snd_pcm_close(handle);
self.pcm_handle = null;
}
}
/// Read audio data from ALSA device and process it
fn readAudio(self: *Self) !usize {
if (self.pcm_handle == null)
return Error.AudioDeviceError;
// Allocate temporary buffer for ALSA reads
const temp_buffer = try self.allocator.alloc(i16, self.period_size * self.channels);
defer self.allocator.free(temp_buffer);
// Read audio data from ALSA
const frames_read = c.snd_pcm_readi(self.pcm_handle, temp_buffer.ptr, self.period_size);
if (frames_read < 0) {
// Handle underrun or other errors
if (frames_read == -c.EPIPE) {
// Underrun occurred, try to recover
const err = c.snd_pcm_prepare(self.pcm_handle);
if (err < 0) return Error.AudioDeviceError;
return 0; // No data read this time
} else return Error.AudioDeviceError;
}
const samples_read = @as(usize, @intCast(frames_read)) * self.channels;
// Process audio based on channel configuration
if (self.channels == 1) {
// Mono input - write directly to buffer
_ = self.audio_buffer.write(temp_buffer[0..samples_read]);
} else if (self.channels == 2) {
// Stereo input - convert to mono
const mono_buffer = try self.allocator.alloc(i16, @as(usize, @intCast(frames_read)));
defer self.allocator.free(mono_buffer);
const mono_samples = AudioConverter.stereoToMono(temp_buffer[0..samples_read], mono_buffer);
_ = self.audio_buffer.write(mono_buffer[0..mono_samples]);
} else {
// Multi-channel input - take first channel only
const mono_buffer = try self.allocator.alloc(i16, @as(usize, @intCast(frames_read)));
defer self.allocator.free(mono_buffer);
for (0..@as(usize, @intCast(frames_read))) |i| {
mono_buffer[i] = temp_buffer[i * self.channels];
}
_ = self.audio_buffer.write(mono_buffer);
}
return @intCast(frames_read);
}
/// Get processed audio samples (mono, at configured sample rate)
pub fn getAudioSamples(self: *Self, output_buffer: []i16) usize {
return self.audio_buffer.read(output_buffer);
}
/// Get number of samples available for reading
pub fn availableSamples(self: *Self) usize {
return self.audio_buffer.available();
}
};
/// Configuration options for STT session initialization
pub const Options = struct {
/// Path to the Vosk model directory
model_path: []const u8,
/// ALSA audio device name (e.g., "hw:3,0")
audio_device: []const u8,
/// Speech event handler for callbacks
event_handler: SpeechEventHandler,
/// Sample rate for audio processing (default: 16000)
sample_rate: u32 = 16000,
// Channels will be detected and used
// /// Number of audio channels (default: 2 for stereo)
// channels: u32 = 2,
/// Audio buffer size in frames (default: 256)
buffer_size: u32 = 256,
};
/// Main STT session handle
///
/// This represents an active speech-to-text session with configured
/// audio input and speech recognition model.
pub const Session = struct {
const Self = @This();
/// Memory allocator
allocator: std.mem.Allocator,
/// Configuration options
options: Options,
/// Initialization state
initialized: bool = false,
/// Listening state
listening: bool = false,
/// ALSA audio capture
alsa_capture: ?AlsaCapture = null,
/// Audio capture thread
audio_thread: ?std.Thread = null,
/// Processing thread for Vosk
processing_thread: ?std.Thread = null,
/// Thread synchronization
should_stop: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
/// Processing buffer for audio samples
processing_buffer: []i16,
/// Resample buffer for converting hardware rate to 16kHz (null if not needed)
resample_buffer: ?[]i16,
/// Vosk model
vosk_model: ?*c.VoskModel = null,
/// Vosk recognizer
vosk_recognizer: ?*c.VoskRecognizer = null,
/// Audio buffer for Vosk processing
vosk_audio_buffer: AudioBuffer,
/// Initialize a new STT session with the given options
///
/// Parameters:
/// - allocator: Memory allocator to use for the session
/// - options: Configuration options for the session
///
/// Returns:
/// - Session instance on success
/// - Error on failure
pub fn init(allocator: std.mem.Allocator, options: Options) Error!Session {
// Validate options first with detailed error reporting
validateOptions(options) catch |err| {
const error_info = switch (err) {
Error.InvalidParameter => ErrorInfo.initWithContext(err, "Invalid initialization parameters provided", "Check model path, audio device, sample rate, and other parameters"),
else => ErrorInfo.init(err, "Parameter validation failed"),
};
options.event_handler.onDetailedError(error_info);
return err;
};
// Allocate processing buffer for audio samples (1 second worth of samples)
const processing_buffer = allocator.alloc(i16, options.sample_rate) catch {
const error_info = ErrorInfo.init(Error.OutOfMemory, "Failed to allocate processing buffer during initialization");
options.event_handler.onDetailedError(error_info);
return Error.OutOfMemory;
};
errdefer allocator.free(processing_buffer);
// Initialize ALSA capture with detailed error reporting
const alsa_capture = AlsaCapture.init(
allocator,
options.audio_device,
options.sample_rate,
options.buffer_size,
) catch |err| {
const error_info = switch (err) {
error.OutOfMemory => ErrorInfo.init(Error.OutOfMemory, "Out of memory while initializing audio capture"),
};
options.event_handler.onDetailedError(error_info);
return Error.OutOfMemory;
};
errdefer {
var alsa_capture_mut = alsa_capture;
alsa_capture_mut.deinit();
}
const cpu_perf = getCpuPerformance() catch 100;
const buffer_multiplier: u32 = if (cpu_perf < 50) 8 else if (cpu_perf < 100) 4 else 2;
const new_buffer_size = 16000 * buffer_multiplier;
std.log.debug(
"Buffer multiplier {d} based on implied BogoMIPS of {d} (100 default in case of error)",
.{ buffer_multiplier, cpu_perf },
);
// Resize the Vosk buffer with the actual sample rate
var vosk_buf = AudioBuffer.init(
allocator,
new_buffer_size,
) catch {
const error_info = ErrorInfo.init(Error.OutOfMemory, "Failed to initialize Vosk buffer after ALSA open");
options.event_handler.onDetailedError(error_info);
return error.InitializationFailed;
};
errdefer vosk_buf.deinit();
var session = Session{
.allocator = allocator,
.options = options,
.alsa_capture = alsa_capture,
.processing_buffer = processing_buffer,
.resample_buffer = null,
.vosk_audio_buffer = vosk_buf,
};
// Initialize Vosk model and recognizer with detailed error reporting
session.initVosk() catch |err| {
const error_info = switch (err) {
Error.ModelLoadError => ErrorInfo.initWithContext(err, "Failed to load Vosk speech recognition model", options.model_path),
Error.OutOfMemory => ErrorInfo.init(err, "Out of memory while loading Vosk model"),
else => ErrorInfo.initWithContext(Error.InitializationFailed, "Unexpected error during Vosk initialization", options.model_path),
};
options.event_handler.onDetailedError(error_info);
session.deinitPartial();
return err;
};
session.initialized = true;
// Report successful initialization
const success_info = ErrorInfo.initRecoverable(Error.InternalError, "STT library initialized successfully", "Ready to start speech recognition");
options.event_handler.onDetailedError(success_info);
return session;
}
/// Initialize Vosk model and recognizer
fn initVosk(self: *Session) !void {
// Convert model path to null-terminated string
const model_path_cstr = try self.allocator.dupeZ(u8, self.options.model_path);
defer self.allocator.free(model_path_cstr);
// Set Vosk log level - disable logs for non-debug builds
if (@import("builtin").mode != .Debug) {
c.vosk_set_log_level(-1); // Disable all Vosk logging
}
// Load Vosk model
self.vosk_model = c.vosk_model_new(model_path_cstr.ptr);
if (self.vosk_model == null) {
return Error.ModelLoadError;
}
// Always create Vosk recognizer at 16kHz
self.vosk_recognizer = c.vosk_recognizer_new(self.vosk_model, 16000.0);
if (self.vosk_recognizer == null) {
if (self.vosk_model) |model| {
c.vosk_model_free(model);
self.vosk_model = null;
}
return Error.ModelLoadError;
}
}
/// Partial cleanup for initialization failures
fn deinitPartial(self: *Session) void {
// Clean up Vosk resources
if (self.vosk_recognizer) |recognizer| {
c.vosk_recognizer_free(recognizer);
self.vosk_recognizer = null;
}
if (self.vosk_model) |model| {
c.vosk_model_free(model);
self.vosk_model = null;
}
// Clean up audio buffer
self.vosk_audio_buffer.deinit();
// Clean up ALSA capture resources
if (self.alsa_capture) |*capture| {
capture.deinit();
self.alsa_capture = null;
}
// Free processing buffer
self.allocator.free(self.processing_buffer);
}
/// Audio capture thread function with comprehensive error handling
fn audioThreadFn(self: *Session) void {
var retry_count: u32 = 0;
const max_retries = 5; // not sure this needs retries...
const retry_delay_ms = 100;
var consecutive_errors: u32 = 0;
const max_consecutive_errors = 20;
// Open ALSA device with retry logic and detailed error reporting
if (self.alsa_capture) |*capture| {
while (retry_count < max_retries and !self.should_stop.load(.acquire)) {
capture.open() catch |err| {
retry_count += 1;
// Create detailed error information
const error_info = switch (err) {
Error.AudioDeviceNotFound => ErrorInfo.initWithContext(err, "Audio device not found", self.options.audio_device),
Error.AudioDeviceBusy => ErrorInfo.initRecoverable(err, "Audio device is busy", "Close other applications using the audio device"),
Error.PermissionDenied => ErrorInfo.initWithContext(err, "Permission denied accessing audio device", self.options.audio_device),
Error.OutOfMemory => ErrorInfo.init(err, "Out of memory while opening audio device"),
Error.SystemResourcesExhausted => ErrorInfo.initRecoverable(err, "System resources exhausted", "Close other applications to free system resources"),
else => ErrorInfo.initWithContext(err, "Failed to open audio device", self.options.audio_device),
};
if (retry_count >= max_retries) {
var final_error = error_info;
final_error.message = "Failed to open audio device after maximum retries";
final_error.recoverable = false;
self.options.event_handler.onDetailedError(final_error);
return;
}
// Report retry attempt
if (retry_count == 1) {
self.options.event_handler.onDetailedError(error_info);
}
std.Thread.sleep(retry_delay_ms * std.time.ns_per_ms * retry_count); // Exponential backoff
continue;
};
break;
}
if (retry_count >= max_retries) {
return;
}
// Allocate resample buffer if hardware sample rate differs from 16kHz
if (capture.sample_rate != 16000) {
std.log.info("Hardware rate {d}Hz != 16kHz, enabling resampling", .{capture.sample_rate});
self.resample_buffer = self.allocator.alloc(i16, 16000) catch {
const error_info = ErrorInfo.init(Error.OutOfMemory, "Failed to allocate resample buffer");
self.options.event_handler.onDetailedError(error_info);
return;
};
}
// Reset retry count for audio reading
retry_count = 0;
// Audio capture loop with comprehensive error handling and recovery
while (!self.should_stop.load(.acquire)) {
// Read audio data from ALSA with detailed error handling
_ = capture.readAudio() catch |err| {
consecutive_errors += 1;
// Create detailed error information based on error type
const error_info = switch (err) {
Error.AudioDeviceError => blk: {
// Try to determine if device was disconnected
if (consecutive_errors > 5) {
break :blk ErrorInfo.initRecoverable(Error.AudioDeviceError, "Audio device may have been disconnected", "Check audio device connection and restart application");
} else {
break :blk ErrorInfo.initRecoverable(err, "Audio capture error, attempting recovery", "Audio device will be automatically reopened");
}
},
Error.OutOfMemory => ErrorInfo.init(err, "Out of memory during audio processing"),
else => ErrorInfo.initWithContext(err, "Unexpected audio capture error", self.options.audio_device),
};
// Report error with context
self.options.event_handler.onDetailedError(error_info);
// Handle different error types appropriately
if (err == Error.AudioDeviceError) {
retry_count += 1;
if (retry_count >= max_retries or consecutive_errors >= max_consecutive_errors) {
const final_error = ErrorInfo.init(Error.AudioDeviceError, "Audio capture failed permanently, stopping audio thread");
self.options.event_handler.onDetailedError(final_error);
break;
}
// Attempt device recovery
self.recoverAudioDevice() catch |recovery_err| {
// Recovery failed, log the error and continue with retry logic
const recovery_error_info = switch (recovery_err) {
Error.AudioDeviceError => ErrorInfo.init(Error.AudioDeviceError, "Audio device recovery failed"),
else => ErrorInfo.init(Error.AudioDeviceError, "Audio device recovery failed with unknown error"),
};
self.options.event_handler.onDetailedError(recovery_error_info);
};
std.Thread.sleep(retry_delay_ms * std.time.ns_per_ms * retry_count);
continue;
} else if (err == Error.OutOfMemory) {
// Memory error is usually fatal
break;
} else {
// Other errors - try to continue
std.Thread.sleep(50 * std.time.ns_per_ms);
continue;
}
};
// Reset error counters on successful read
retry_count = 0;
consecutive_errors = 0;
// Transfer audio data to Vosk processing buffer with error handling
if (capture.availableSamples() >= 1024) { // Process in chunks of 1024 samples
const chunk_size = @min(1024, self.processing_buffer.len);
const samples_read = capture.getAudioSamples(self.processing_buffer[0..chunk_size]);
if (samples_read > 0) {
// Resample if needed, otherwise use samples directly
const samples_to_write = if (self.resample_buffer) |resample_buf| blk: {
const resampled_count = resample(
self.processing_buffer[0..samples_read],
resample_buf,
capture.sample_rate,
16000,
);
break :blk resample_buf[0..resampled_count];
} else self.processing_buffer[0..samples_read];
// Send audio to Vosk processing buffer with overflow protection
const written = self.vosk_audio_buffer.write(samples_to_write);
if (written < samples_to_write.len) {
// Buffer overflow - report warning and clear buffer
const warning = ErrorInfo.initRecoverable(Error.InternalError, "Audio buffer overflow, clearing buffer to prevent data loss", "Consider increasing buffer size if this happens frequently");
self.options.event_handler.onDetailedError(warning);
self.vosk_audio_buffer.clear();
_ = self.vosk_audio_buffer.write(samples_to_write);
}
}
}
// Small delay to prevent busy waiting
std.Thread.sleep(1 * std.time.ns_per_ms); // 1ms
}
// Ensure ALSA device is properly closed
capture.close();
}
}
/// Vosk processing thread function with comprehensive error handling
fn processingThreadFn(self: *Session) void {
// Processing buffer for Vosk (4096 samples = ~256ms at 16kHz)
const vosk_chunk_size = 4096;
const min_chunk_size = 1024; // Minimum chunk size for processing
const cpu_perf = getCpuPerformance() catch 100;
if (cpu_perf < 50)
std.log.debug("processing thread additional delay being added", .{});
var vosk_buffer = self.allocator.alloc(i16, vosk_chunk_size) catch {
const error_info = ErrorInfo.init(Error.OutOfMemory, "Failed to allocate Vosk processing buffer");
self.options.event_handler.onDetailedError(error_info);
return;
};
defer self.allocator.free(vosk_buffer);
var error_count: u32 = 0;
const max_errors = 10;
const error_reset_threshold = 100; // Reset error count after this many successful operations
var success_count: u32 = 0;
var consecutive_failures: u32 = 0;
const max_consecutive_failures = 5;
while (!self.should_stop.load(.acquire)) {
// Check if we have enough audio data for processing
const available_samples = self.vosk_audio_buffer.available();
if (available_samples >= min_chunk_size) {
// Process in chunks, but don't exceed our buffer size
const chunk_size = @min(available_samples, vosk_chunk_size);
const samples_read = self.vosk_audio_buffer.read(vosk_buffer[0..chunk_size]);
if (samples_read > 0 and self.vosk_recognizer != null) {
// Time the Vosk processing to identify bottlenecks
const start_time = std.time.nanoTimestamp();
const buffer_fill = (self.vosk_audio_buffer.available() * 100) / self.vosk_audio_buffer.buffer.len;
// Process audio with Vosk with comprehensive error handling
self.processVoskAudio(vosk_buffer[0..samples_read]) catch |err| {
error_count += 1;
consecutive_failures += 1;
// Create detailed error information
const error_info = switch (err) {
Error.InvalidState => ErrorInfo.initRecoverable(err, "Vosk recognizer is in invalid state", "Recognizer will be reinitialized"),
Error.OutOfMemory => ErrorInfo.init(err, "Out of memory during speech processing"),
Error.CallbackError => ErrorInfo.initWithContext(err, "Error in speech detection callback", "Check callback implementation"),
else => ErrorInfo.init(err, "Unexpected error during speech processing"),
};
self.options.event_handler.onDetailedError(error_info);
// Handle different error scenarios
if (error_count >= max_errors) {
const fatal_error = ErrorInfo.init(Error.CallbackError, "Too many Vosk processing errors, stopping processing thread");
self.options.event_handler.onDetailedError(fatal_error);
break;
}
if (consecutive_failures >= max_consecutive_failures) {
// Try to recover by reinitializing Vosk
const recovery_info = ErrorInfo.initRecoverable(Error.InternalError, "Multiple consecutive processing failures, attempting recovery", "Vosk recognizer will be reinitialized");
self.options.event_handler.onDetailedError(recovery_info);
self.reinitializeVosk() catch {
const recovery_failed = ErrorInfo.init(Error.ModelLoadError, "Failed to recover Vosk recognizer, stopping processing");
self.options.event_handler.onDetailedError(recovery_failed);
break;
};
consecutive_failures = 0;
}
// Add delay after error to prevent rapid error loops
std.Thread.sleep(50 * std.time.ns_per_ms * consecutive_failures); // Exponential backoff
continue;
};
// Log timing and buffer status for diagnostics
const end_time = std.time.nanoTimestamp();
const processing_ms = @divTrunc(end_time - start_time, std.time.ns_per_ms);
const realtime_ms = (samples_read * 1000) / 16000;
if (processing_ms > realtime_ms and buffer_fill > 20)
std.log.warn("Vosk processing behind realtime. {d} samples in {d}ms (realtime: {d}ms), buffer {d}% full", .{ samples_read, processing_ms, realtime_ms, buffer_fill });
// Reset error counters after successful operations
success_count += 1;
consecutive_failures = 0;
if (success_count >= error_reset_threshold) {
error_count = 0;
success_count = 0;
}
}
}
// Adaptive delay based on buffer fill level and error state
const base_delay_ms: u64 = if (available_samples > vosk_chunk_size * 2)
1 // Fast processing when buffer is full
else if (available_samples > min_chunk_size)
5 // Normal processing
else
10; // Slower when buffer is low
// Increase delay if we're having errors
const error_multiplier: u64 = if (consecutive_failures > 0) consecutive_failures + 1 else 1;
var delay_ms = base_delay_ms * error_multiplier;
// Add extra delay for slower hardware (Pi) to prevent buffer overruns
if (cpu_perf < 50) {
delay_ms += 100; // Extra 10ms delay for Pi-class hardware
}
std.Thread.sleep(delay_ms * std.time.ns_per_ms);
}
// Final processing of any remaining audio data
const remaining_samples = self.vosk_audio_buffer.available();
if (remaining_samples > 0 and self.vosk_recognizer != null) {
const final_chunk_size = @min(remaining_samples, vosk_chunk_size);
const samples_read = self.vosk_audio_buffer.read(vosk_buffer[0..final_chunk_size]);
if (samples_read > 0) {
self.processVoskAudio(vosk_buffer[0..samples_read]) catch {
// Ignore errors during shutdown, but log them
const shutdown_error = ErrorInfo.init(Error.InternalError, "Error during final audio processing at shutdown");
self.options.event_handler.onDetailedError(shutdown_error);
};
}
}
}
/// Process audio chunk with Vosk and handle results
fn processVoskAudio(self: *Session, audio_data: []const i16) !void {
if (self.vosk_recognizer == null) {
return Error.InvalidState;
}
// Use audio data directly without resampling
const final_audio = audio_data;
// Convert i16 samples to bytes for Vosk
const audio_bytes = std.mem.sliceAsBytes(final_audio);
// Feed audio to Vosk recognizer
const accept_result = c.vosk_recognizer_accept_waveform(self.vosk_recognizer, audio_bytes.ptr, @intCast(audio_bytes.len));
if (accept_result == 1) {
// Final result available
const result_cstr = c.vosk_recognizer_result(self.vosk_recognizer);
if (result_cstr != null) {
const result_str = std.mem.span(result_cstr);
// Parse JSON result to extract text
self.parseVoskResult(result_str) catch |err| {
self.options.event_handler.onError(err, "Failed to parse Vosk result");
};
// Reset recognizer after getting final result to clear internal buffers
c.vosk_recognizer_reset(self.vosk_recognizer);
}
} else if (accept_result == 0) {
// Partial result available (optional - for real-time feedback)
const partial_result_cstr = c.vosk_recognizer_partial_result(self.vosk_recognizer);
if (partial_result_cstr != null) {
const partial_str = std.mem.span(partial_result_cstr);
// Parse partial result (could be used for real-time display)
self.parseVoskPartialResult(partial_str) catch |parse_err| {
// Log partial result parsing errors but continue processing
const parse_error_info = switch (parse_err) {
Error.CallbackError => ErrorInfo.init(Error.CallbackError, "Failed to parse partial speech result"),
else => ErrorInfo.init(Error.CallbackError, "Unexpected error parsing partial speech result"),
};
self.options.event_handler.onDetailedError(parse_error_info);
};
}
}
// accept_result == -1 means error, but we continue processing
}
/// Parse Vosk JSON result and extract recognized text
fn parseVoskResult(self: *Session, json_str: []const u8) !void {
// Simple JSON parsing to extract "text" field
// Vosk returns JSON like: {"text": "hello world"}
if (json_str.len == 0) return;
// Find "text" field in JSON
const text_key = "\"text\"";
if (std.mem.indexOf(u8, json_str, text_key)) |text_start| {
const value_start = text_start + text_key.len;
// Find the colon and opening quote
if (std.mem.indexOf(u8, json_str[value_start..], ":")) |colon_pos| {
const after_colon = value_start + colon_pos + 1;
// Skip whitespace and find opening quote
var quote_start: ?usize = null;
for (json_str[after_colon..], 0..) |char, i| {
if (char == '"') {
quote_start = after_colon + i + 1;
break;
}
}
if (quote_start) |s| {
// Find closing quote
if (std.mem.indexOf(u8, json_str[s..], "\"")) |quote_end| {
const text = json_str[s .. s + quote_end];
// Only invoke callback if text is not empty
if (text.len > 0 and !std.mem.eql(u8, text, " ")) {
self.options.event_handler.onSpeech(text);
}
}
}
}
}
}
/// Parse Vosk partial result (for real-time feedback)
fn parseVoskPartialResult(self: *Session, json_str: []const u8) !void {
// Similar to parseVoskResult but for partial results
// For now, we don't use partial results, but this could be extended
// to provide real-time transcription feedback
_ = self;
_ = json_str;
}
/// Attempt to recover from audio device errors with detailed error reporting
fn recoverAudioDevice(self: *Session) Error!void {
if (self.alsa_capture) |*capture| {
// Close the current device handle
capture.close();
// Wait a bit before attempting to reopen
std.Thread.sleep(100 * std.time.ns_per_ms);
// Try to reopen the device with detailed error handling
capture.open() catch |err| {
const recovery_error = switch (err) {
Error.AudioDeviceNotFound => ErrorInfo.initWithContext(err, "Audio device not found during recovery", self.options.audio_device),
Error.AudioDeviceBusy => ErrorInfo.initRecoverable(err, "Audio device busy during recovery", "Wait for other applications to release the device"),
Error.PermissionDenied => ErrorInfo.initWithContext(err, "Permission denied during audio device recovery", self.options.audio_device),
else => ErrorInfo.initWithContext(err, "Failed to recover audio device", self.options.audio_device),
};
self.options.event_handler.onDetailedError(recovery_error);
return err;
};
// Clear audio buffers after successful recovery
capture.audio_buffer.clear();
const recovery_success = ErrorInfo.initRecoverable(Error.InternalError, "Audio device recovered successfully", "Audio capture will resume normally");
self.options.event_handler.onDetailedError(recovery_success);
}
}
/// Reinitialize Vosk recognizer for error recovery
fn reinitializeVosk(self: *Session) Error!void {
// Clean up existing Vosk resources
if (self.vosk_recognizer) |recognizer| {
c.vosk_recognizer_free(recognizer);
self.vosk_recognizer = null;
}
// Reinitialize recognizer (model should still be valid)
if (self.vosk_model) |model| {
self.vosk_recognizer = c.vosk_recognizer_new(model, 16000.0);
if (self.vosk_recognizer == null) {
const error_info = ErrorInfo.init(Error.ModelLoadError, "Failed to reinitialize Vosk recognizer");
self.options.event_handler.onDetailedError(error_info);
return Error.ModelLoadError;
}
// Clear processing buffer
self.vosk_audio_buffer.clear();
const success_info = ErrorInfo.initRecoverable(Error.InternalError, "Vosk recognizer reinitialized successfully", "Speech processing will resume normally");
self.options.event_handler.onDetailedError(success_info);
} else {
return Error.InvalidState;
}
}
/// Get current session status information
pub fn getStatus(self: *Session) struct {
initialized: bool,
listening: bool,
audio_samples_available: usize,
processing_samples_available: usize,
} {
return .{
.initialized = self.initialized,
.listening = self.listening,
.audio_samples_available = if (self.alsa_capture) |*capture| capture.availableSamples() else 0,
.processing_samples_available = self.vosk_audio_buffer.available(),
};
}
/// Validate session options before initialization
fn validateOptions(options: Options) Error!void {
if (options.model_path.len == 0) {
return Error.InvalidParameter;
}
if (options.audio_device.len == 0) {
return Error.InvalidParameter;
}
if (options.sample_rate == 0 or options.sample_rate > 48000) {
return Error.InvalidParameter;
}
if (options.buffer_size == 0 or options.buffer_size > 8192) {
return Error.InvalidParameter;
}
}
/// Reinitialize the session after an error (recovery mechanism)
pub fn reinitialize(self: *Session) Error!void {
if (self.listening) {
self.stop_listening();
}
// Clean up existing Vosk resources
if (self.vosk_recognizer) |recognizer| {
c.vosk_recognizer_free(recognizer);
self.vosk_recognizer = null;
}
if (self.vosk_model) |model| {
c.vosk_model_free(model);
self.vosk_model = null;
}
// Reinitialize Vosk
try self.initVosk();
// Reset audio buffers
if (self.alsa_capture) |*capture| {
capture.audio_buffer.clear();
}
self.vosk_audio_buffer.clear();
self.initialized = true;
}
/// Start listening for speech input
///
/// This begins audio capture and speech recognition processing.
/// Speech detection events will be delivered via the configured
/// event handler callbacks.
///
/// Returns:
/// - void on success
/// - Error on failure
pub fn start(self: *Session) Error!void {
if (!self.initialized) {
return Error.InvalidState;
}
if (self.listening) {
return Error.InvalidState;
}
// Clear any existing audio buffers
if (self.alsa_capture) |*capture| {
capture.audio_buffer.clear();
}
self.vosk_audio_buffer.clear();
// Reset stop flag
self.should_stop.store(false, .release);
// Start audio capture thread with error handling
self.audio_thread = std.Thread.spawn(.{}, audioThreadFn, .{self}) catch |err| {
self.should_stop.store(true, .release);
return switch (err) {
error.SystemResources, error.ThreadQuotaExceeded => Error.ThreadingError,
else => Error.ThreadingError,
};
};
// Start Vosk processing thread with cleanup on failure
self.processing_thread = std.Thread.spawn(.{}, processingThreadFn, .{self}) catch |err| {
// Clean up audio thread if processing thread fails
self.should_stop.store(true, .release);
if (self.audio_thread) |thread| {
thread.detach();
self.audio_thread = null;
}
return switch (err) {
error.SystemResources, error.ThreadQuotaExceeded => Error.ThreadingError,
else => Error.ThreadingError,
};
};
// Give threads a moment to start up
std.Thread.sleep(10 * std.time.ns_per_ms);
self.listening = true;
}
/// Stop listening for speech input
///
/// This stops audio capture and speech recognition processing.
/// Any ongoing processing will be completed before returning.
pub fn stop(self: *Session) void {
if (!self.listening) {
return;
}
// Signal threads to stop
self.should_stop.store(true, .release);
// Give threads a moment to see the stop signal
std.Thread.sleep(10 * std.time.ns_per_ms);
// Detach threads instead of joining to prevent hanging
if (self.audio_thread) |thread| {
thread.detach();
self.audio_thread = null;
}
if (self.processing_thread) |thread| {
thread.detach();
self.processing_thread = null;
}
// Clear any remaining audio data
if (self.alsa_capture) |*capture| {
capture.audio_buffer.clear();
}
self.vosk_audio_buffer.clear();
self.listening = false;
}
/// Deinitialize the STT session and free all resources
///
/// This must be called to properly clean up the session.
/// After calling deinit(), the session should not be used.
pub fn deinit(self: *Session) void {
// Ensure we're not listening before cleanup
if (self.listening) {
self.stop();
}
// Detach any remaining threads to prevent hanging
if (self.audio_thread) |thread| {
thread.detach();
self.audio_thread = null;
}
if (self.processing_thread) |thread| {
thread.detach();
self.processing_thread = null;
}
// Clean up Vosk resources in proper order
if (self.vosk_recognizer) |recognizer| {
c.vosk_recognizer_free(recognizer);
self.vosk_recognizer = null;
}
if (self.vosk_model) |model| {
c.vosk_model_free(model);
self.vosk_model = null;
}
// Clean up audio buffers
self.vosk_audio_buffer.deinit();
// Clean up ALSA capture resources
if (self.alsa_capture) |*capture| {
capture.deinit();
self.alsa_capture = null;
}
// Free processing buffer
self.allocator.free(self.processing_buffer);
// Free resample buffer if allocated
if (self.resample_buffer) |buf| {
self.allocator.free(buf);
}
// Clean up ALSA global configuration cache
_ = c.snd_config_update_free_global();
// Mark as uninitialized
self.initialized = false;
}
};
/// Initialize STT library with the given options
///
/// This is the main entry point for the STT library. It creates and initializes
/// a new STT session with the provided configuration.
///
/// Parameters:
/// - allocator: Memory allocator to use for the session
/// - options: Configuration options for the session
///
/// Returns:
/// - Session instance on success
/// - Error on failure
pub fn init(allocator: std.mem.Allocator, options: Options) Error!Session {
return Session.init(allocator, options);
}
// Tests
test "Error enum" {
const testing = std.testing;
// Test that error types can be created and compared
const err1 = Error.InitializationFailed;
const err2 = Error.AudioDeviceError;
try testing.expect(err1 != err2);
try testing.expect(err1 == Error.InitializationFailed);
}
test "Options validation" {
const testing = std.testing;
// Test valid options
const DummyHandler = struct {
fn onSpeech(ctx: *anyopaque, text: []const u8) void {
_ = ctx;
_ = text;
}
fn onError(ctx: *anyopaque, error_code: Error, message: []const u8) void {
_ = ctx;
_ = message;
// Can't discard error types with _, so we just don't use it
switch (error_code) {
else => {},
}
}
};
var dummy_ctx: u8 = 0;
const valid_options = Options{
.model_path = "/path/to/model",
.audio_device = "hw:0,0",
.event_handler = SpeechEventHandler{
.onSpeechFn = DummyHandler.onSpeech,
.onErrorFn = DummyHandler.onError,
.ctx = &dummy_ctx,
},
};
// Test that options structure is properly formed (without calling init to avoid Vosk dependency)
try testing.expectEqualStrings("/path/to/model", valid_options.model_path);
try testing.expectEqualStrings("hw:0,0", valid_options.audio_device);
try testing.expect(valid_options.sample_rate == 16000);
try testing.expect(valid_options.buffer_size == 256);
}
test "Session state management" {
const testing = std.testing;
const DummyHandler = struct {
fn onSpeech(ctx: *anyopaque, text: []const u8) void {
_ = ctx;
_ = text;
}
fn onError(ctx: *anyopaque, error_code: Error, message: []const u8) void {
_ = ctx;
_ = message;
// Can't discard error types with _, so we just don't use it
switch (error_code) {
else => {},
}
}
};
var dummy_ctx: u8 = 0;
const options = Options{
.model_path = "/path/to/model",
.audio_device = "hw:0,0",
.event_handler = SpeechEventHandler{
.onSpeechFn = DummyHandler.onSpeech,
.onErrorFn = DummyHandler.onError,
.ctx = &dummy_ctx,
},
};
// Test that options structure is properly formed (without calling init to avoid Vosk dependency)
try testing.expectEqualStrings("/path/to/model", options.model_path);
try testing.expectEqualStrings("hw:0,0", options.audio_device);
try testing.expect(options.sample_rate == 16000);
try testing.expect(options.buffer_size == 256);
}
test "SpeechEventHandler interface" {
const testing = std.testing;
const TestHandler = struct {
speech_called: bool = false,
error_called: bool = false,
last_text: []const u8 = "",
last_error: Error = Error.InitializationFailed,
fn onSpeech(ctx: *anyopaque, text: []const u8) void {
const self: *@This() = @ptrCast(@alignCast(ctx));
self.speech_called = true;
self.last_text = text;
}
fn onError(ctx: *anyopaque, error_code: Error, message: []const u8) void {
const self: *@This() = @ptrCast(@alignCast(ctx));
self.error_called = true;
self.last_error = error_code;
_ = message;
}
};
var handler = TestHandler{};
const event_handler = SpeechEventHandler{
.onSpeechFn = TestHandler.onSpeech,
.onErrorFn = TestHandler.onError,
.ctx = &handler,
};
// Test speech callback
event_handler.onSpeech("hello world");
try testing.expect(handler.speech_called);
try testing.expectEqualStrings("hello world", handler.last_text);
// Test error callback
event_handler.onError(Error.AudioDeviceError, "test error");
try testing.expect(handler.error_called);
try testing.expect(handler.last_error == Error.AudioDeviceError);
}
test "Vosk integration with valid model" {
// Skip this test to avoid segfaults during cleanup
// The test tries to initialize real Vosk models which can cause
// segmentation faults during deinit
return error.SkipZigTest;
}
test "AudioBuffer basic operations" {
const testing = std.testing;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var buffer = try AudioBuffer.init(allocator, 10);
defer buffer.deinit();
// Test initial state
try testing.expect(buffer.available() == 0);
try testing.expect(buffer.capacity() == 10);
// Test writing samples
const samples = [_]i16{ 1, 2, 3, 4, 5 };
const written = buffer.write(&samples);
try testing.expect(written == 5);
try testing.expect(buffer.available() == 5);
try testing.expect(buffer.capacity() == 5);
// Test reading samples
var read_samples: [3]i16 = undefined;
const read_count = buffer.read(&read_samples);
try testing.expect(read_count == 3);
try testing.expect(read_samples[0] == 1);
try testing.expect(read_samples[1] == 2);
try testing.expect(read_samples[2] == 3);
try testing.expect(buffer.available() == 2);
// Test buffer wrap-around
const more_samples = [_]i16{ 6, 7, 8, 9, 10, 11, 12, 13 };
const written2 = buffer.write(&more_samples);
try testing.expect(written2 == 8); // Should write 8 samples (2 remaining + 6 new)
try testing.expect(buffer.available() == 10); // Buffer should be full
// Test clearing buffer
buffer.clear();
try testing.expect(buffer.available() == 0);
try testing.expect(buffer.capacity() == 10);
}
test "AudioConverter stereo to mono conversion" {
const testing = std.testing;
// Test stereo to mono conversion
const stereo_samples = [_]i16{ 100, 200, 300, 400, 500, 600 }; // 3 stereo frames
var mono_samples: [3]i16 = undefined;
const frames_converted = AudioConverter.stereoToMono(&stereo_samples, &mono_samples);
try testing.expect(frames_converted == 3);
// Check averaged values
try testing.expect(mono_samples[0] == 150); // (100 + 200) / 2
try testing.expect(mono_samples[1] == 350); // (300 + 400) / 2
try testing.expect(mono_samples[2] == 550); // (500 + 600) / 2
}
test "AlsaCapture initialization" {
const testing = std.testing;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Test ALSA capture initialization (without actually opening device)
var capture = AlsaCapture.init(allocator, "hw:0,0", 16000, 1024) catch |err| {
// If ALSA initialization fails (e.g., no audio device), that's expected in test environment
if (err == error.OutOfMemory) {
return err;
}
return; // Skip test if ALSA not available
};
defer capture.deinit();
// Test basic properties
try testing.expect(capture.sample_rate == 16000);
try testing.expect(capture.buffer_size == 1024);
try testing.expect(capture.period_size == 256); // buffer_size / 4
try testing.expect(capture.pcm_handle == null); // Not opened yet
}
test "AudioBuffer thread safety" {
const testing = std.testing;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var buffer = try AudioBuffer.init(allocator, 1000);
defer buffer.deinit();
// Test concurrent access (simplified test)
const samples1 = [_]i16{ 1, 2, 3, 4, 5 };
const samples2 = [_]i16{ 6, 7, 8, 9, 10 };
// Write from multiple "threads" (simulated)
const written1 = buffer.write(&samples1);
const written2 = buffer.write(&samples2);
try testing.expect(written1 == 5);
try testing.expect(written2 == 5);
try testing.expect(buffer.available() == 10);
// Read back samples
var read_buffer: [10]i16 = undefined;
const read_count = buffer.read(&read_buffer);
try testing.expect(read_count == 10);
// Verify order is maintained
try testing.expect(read_buffer[0] == 1);
try testing.expect(read_buffer[4] == 5);
try testing.expect(read_buffer[5] == 6);
try testing.expect(read_buffer[9] == 10);
}
test "Session session management API" {
const testing = std.testing;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const TestHandler = struct {
speech_count: u32 = 0,
error_count: u32 = 0,
fn onSpeech(ctx: *anyopaque, text: []const u8) void {
const self: *@This() = @ptrCast(@alignCast(ctx));
self.speech_count += 1;
_ = text;
}
fn onError(ctx: *anyopaque, error_code: Error, message: []const u8) void {
const self: *@This() = @ptrCast(@alignCast(ctx));
self.error_count += 1;
switch (error_code) {
else => {},
}
_ = message;
}
};
var handler = TestHandler{};
const options = Options{
.model_path = "/invalid/path", // Will fail, but that's expected
.audio_device = "hw:0,0",
.event_handler = SpeechEventHandler{
.onSpeechFn = TestHandler.onSpeech,
.onErrorFn = TestHandler.onError,
.ctx = &handler,
},
};
// Test that options structure is properly formed (without calling init to avoid Vosk dependency)
try testing.expectEqualStrings("/invalid/path", options.model_path);
try testing.expectEqualStrings("hw:0,0", options.audio_device);
try testing.expect(options.sample_rate == 16000);
try testing.expect(options.buffer_size == 256);
// Test options validation
const invalid_options = Options{
.model_path = "", // Invalid empty path
.audio_device = "hw:0,0",
.event_handler = options.event_handler,
};
const invalid_result = init(allocator, invalid_options);
try testing.expectError(Error.InvalidParameter, invalid_result);
}
test "Session status and recovery" {
// Skip this test to avoid segfaults during cleanup
// The test tries to initialize real Vosk models and ALSA devices
// which can cause segmentation faults during deinit
return error.SkipZigTest;
}
test "resample function" {
// Test same sample rate (no conversion)
const input = [_]i16{ 100, 200, 300, 400 };
var output: [4]i16 = undefined;
const count = resample(&input, &output, 16000, 16000);
try std.testing.expect(count == 4);
try std.testing.expectEqualSlices(i16, &input, output[0..count]);
// Test downsampling (48kHz -> 16kHz, 3:1 ratio)
const input_48k = [_]i16{ 100, 150, 200, 250, 300, 350 };
var output_16k: [2]i16 = undefined;
const down_count = resample(&input_48k, &output_16k, 48000, 16000);
try std.testing.expect(down_count == 2);
// Test upsampling (16kHz -> 48kHz, 1:3 ratio)
const input_16k = [_]i16{ 100, 200 };
var output_48k: [6]i16 = undefined;
const up_count = resample(&input_16k, &output_48k, 16000, 48000);
try std.testing.expect(up_count == 6);
}