ai did not understand defer in that function

This commit is contained in:
Emil Lerch 2025-09-15 19:44:21 -07:00
parent b5afdbf719
commit 049ea33c8d
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -390,77 +390,48 @@ pub const AlsaCapture = struct {
// Allocate hardware parameters structure // Allocate hardware parameters structure
var hw_params: ?*c.snd_pcm_hw_params_t = null; var hw_params: ?*c.snd_pcm_hw_params_t = null;
err = c.snd_pcm_hw_params_malloc(@ptrCast(&hw_params)); err = c.snd_pcm_hw_params_malloc(@ptrCast(&hw_params));
if (err < 0) { errdefer self.close();
self.close(); if (err < 0) return SttError.AudioDeviceError;
return SttError.AudioDeviceError;
}
defer c.snd_pcm_hw_params_free(hw_params); defer c.snd_pcm_hw_params_free(hw_params);
// Initialize hardware parameters // Initialize hardware parameters
err = c.snd_pcm_hw_params_any(self.pcm_handle, hw_params); err = c.snd_pcm_hw_params_any(self.pcm_handle, hw_params);
if (err < 0) { if (err < 0) return SttError.AudioDeviceError;
self.close();
return SttError.AudioDeviceError;
}
// 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) { if (err < 0) return SttError.AudioDeviceError;
self.close();
return SttError.AudioDeviceError;
}
// 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) { if (err < 0) return SttError.AudioDeviceError;
self.close();
return SttError.AudioDeviceError;
}
// 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) { if (err < 0) return SttError.AudioDeviceError;
self.close();
return SttError.AudioDeviceError;
}
// 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) { if (err < 0) return SttError.AudioDeviceError;
self.close();
return SttError.AudioDeviceError;
}
// 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) { if (err < 0) return SttError.AudioDeviceError;
self.close();
return SttError.AudioDeviceError;
}
// 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) { if (err < 0) return SttError.AudioDeviceError;
self.close();
return SttError.AudioDeviceError;
}
// 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) { if (err < 0) return SttError.AudioDeviceError;
self.close();
return SttError.AudioDeviceError;
}
// 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) { if (err < 0) return SttError.AudioDeviceError;
self.close();
return SttError.AudioDeviceError;
}
} }
/// Close ALSA device /// Close ALSA device
@ -485,13 +456,9 @@ pub const AlsaCapture = struct {
if (frames_read == -c.EPIPE) { if (frames_read == -c.EPIPE) {
// Underrun occurred, try to recover // Underrun occurred, try to recover
const err = c.snd_pcm_prepare(self.pcm_handle); const err = c.snd_pcm_prepare(self.pcm_handle);
if (err < 0) { if (err < 0) return SttError.AudioDeviceError;
return SttError.AudioDeviceError;
}
return 0; // No data read this time return 0; // No data read this time
} else { } else return SttError.AudioDeviceError;
return SttError.AudioDeviceError;
}
} }
const samples_read = @as(usize, @intCast(frames_read)) * self.channels; const samples_read = @as(usize, @intCast(frames_read)) * self.channels;