diff --git a/src/main.zig b/src/main.zig index 8b17fcc..c7c9331 100644 --- a/src/main.zig +++ b/src/main.zig @@ -209,6 +209,7 @@ const SpeechHandler = struct { const writer = stream.writer(); try writer.print("{s}", .{error_info.message}); + try writer.print("\n\tCode: {}", .{error_info.error_code}); if (error_info.context) |context| try writer.print("\n\tContext: {s}", .{context}); diff --git a/src/stt.zig b/src/stt.zig index 7d65fda..a2e5e4e 100644 --- a/src/stt.zig +++ b/src/stt.zig @@ -45,6 +45,15 @@ pub const SttError = error{ Timeout, /// Internal library error (should not normally occur) InternalError, + + SetAccessError, + SetFormatError, + SetChannelError, + SetSampleRateError, + SetBufferSizeError, + SetPeriodSizeError, + ApplyParametersError, + PcmPrepareError, }; /// Detailed error information structure @@ -400,38 +409,49 @@ pub const AlsaCapture = struct { // 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 SttError.AudioDeviceError; + if (err < 0) return SttError.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 SttError.AudioDeviceError; + if (err < 0) return SttError.SetFormatError; // Set number of channels err = c.snd_pcm_hw_params_set_channels(self.pcm_handle, hw_params, self.channels); - if (err < 0) return SttError.AudioDeviceError; + if (err < 0) { + // SAFETY: min/max is set in c calls before use just below + var min: c_uint = undefined; + // SAFETY: min/max is set in c calls before use just below + var max: c_uint = undefined; + err = c.snd_pcm_hw_params_get_channels_min(hw_params, &min); + if (err < 0) return SttError.SetChannelError; + err = c.snd_pcm_hw_params_get_channels_max(hw_params, &max); + if (err < 0) return SttError.SetChannelError; + std.log.err("error setting number of channels. Must be between {d} and {d}", .{ min, max }); + return SttError.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 SttError.AudioDeviceError; + if (err < 0) return SttError.SetSampleRateError; // 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 SttError.AudioDeviceError; + if (err < 0) return SttError.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 SttError.AudioDeviceError; + if (err < 0) return SttError.SetPeriodSizeError; // Apply hardware parameters err = c.snd_pcm_hw_params(self.pcm_handle, hw_params); - if (err < 0) return SttError.AudioDeviceError; + if (err < 0) return SttError.ApplyParametersError; // Prepare the PCM for use err = c.snd_pcm_prepare(self.pcm_handle); - if (err < 0) return SttError.AudioDeviceError; + if (err < 0) return SttError.PcmPrepareError; } /// Close ALSA device @@ -690,7 +710,7 @@ pub const SttSession = struct { /// Audio capture thread function with comprehensive error handling fn audioThreadFn(self: *SttSession) void { var retry_count: u32 = 0; - const max_retries = 5; + const max_retries = 5; // not sure this needs retries... const retry_delay_ms = 100; var consecutive_errors: u32 = 0; const max_consecutive_errors = 20;