better error reporting/handling around channels
This commit is contained in:
parent
520eeeb99e
commit
3bb4b45dde
2 changed files with 30 additions and 9 deletions
|
@ -209,6 +209,7 @@ const SpeechHandler = struct {
|
||||||
const writer = stream.writer();
|
const writer = stream.writer();
|
||||||
|
|
||||||
try writer.print("{s}", .{error_info.message});
|
try writer.print("{s}", .{error_info.message});
|
||||||
|
try writer.print("\n\tCode: {}", .{error_info.error_code});
|
||||||
|
|
||||||
if (error_info.context) |context|
|
if (error_info.context) |context|
|
||||||
try writer.print("\n\tContext: {s}", .{context});
|
try writer.print("\n\tContext: {s}", .{context});
|
||||||
|
|
38
src/stt.zig
38
src/stt.zig
|
@ -45,6 +45,15 @@ pub const SttError = error{
|
||||||
Timeout,
|
Timeout,
|
||||||
/// Internal library error (should not normally occur)
|
/// Internal library error (should not normally occur)
|
||||||
InternalError,
|
InternalError,
|
||||||
|
|
||||||
|
SetAccessError,
|
||||||
|
SetFormatError,
|
||||||
|
SetChannelError,
|
||||||
|
SetSampleRateError,
|
||||||
|
SetBufferSizeError,
|
||||||
|
SetPeriodSizeError,
|
||||||
|
ApplyParametersError,
|
||||||
|
PcmPrepareError,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Detailed error information structure
|
/// Detailed error information structure
|
||||||
|
@ -400,38 +409,49 @@ pub const AlsaCapture = struct {
|
||||||
|
|
||||||
// Set access type to interleaved
|
// Set access type to interleaved
|
||||||
err = c.snd_pcm_hw_params_set_access(self.pcm_handle, hw_params, c.SND_PCM_ACCESS_RW_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
|
// 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);
|
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
|
// Set number of channels
|
||||||
err = c.snd_pcm_hw_params_set_channels(self.pcm_handle, hw_params, self.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
|
// Set sample rate
|
||||||
var actual_rate = self.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);
|
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
|
// Set buffer size
|
||||||
var actual_buffer_size: c.snd_pcm_uframes_t = self.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);
|
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
|
// Set period size
|
||||||
var actual_period_size: c.snd_pcm_uframes_t = self.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);
|
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
|
// Apply hardware parameters
|
||||||
err = c.snd_pcm_hw_params(self.pcm_handle, hw_params);
|
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
|
// Prepare the PCM for use
|
||||||
err = c.snd_pcm_prepare(self.pcm_handle);
|
err = c.snd_pcm_prepare(self.pcm_handle);
|
||||||
if (err < 0) return SttError.AudioDeviceError;
|
if (err < 0) return SttError.PcmPrepareError;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Close ALSA device
|
/// Close ALSA device
|
||||||
|
@ -690,7 +710,7 @@ pub const SttSession = struct {
|
||||||
/// Audio capture thread function with comprehensive error handling
|
/// Audio capture thread function with comprehensive error handling
|
||||||
fn audioThreadFn(self: *SttSession) void {
|
fn audioThreadFn(self: *SttSession) void {
|
||||||
var retry_count: u32 = 0;
|
var retry_count: u32 = 0;
|
||||||
const max_retries = 5;
|
const max_retries = 5; // not sure this needs retries...
|
||||||
const retry_delay_ms = 100;
|
const retry_delay_ms = 100;
|
||||||
var consecutive_errors: u32 = 0;
|
var consecutive_errors: u32 = 0;
|
||||||
const max_consecutive_errors = 20;
|
const max_consecutive_errors = 20;
|
||||||
|
|
Loading…
Add table
Reference in a new issue