simplify buffer resizing

This way is less verbose, much more simple and less brittle.
This commit is contained in:
JasonLG1979 2021-10-06 11:20:09 -05:00
parent 095536f100
commit 9ef53f5ffb

View file

@ -7,7 +7,6 @@ use alsa::device_name::HintIter;
use alsa::pcm::{Access, Format, HwParams, PCM}; use alsa::pcm::{Access, Format, HwParams, PCM};
use alsa::{Direction, ValueOr}; use alsa::{Direction, ValueOr};
use std::cmp::min; use std::cmp::min;
use std::cmp::Ordering;
use std::process::exit; use std::process::exit;
use std::time::Duration; use std::time::Duration;
use thiserror::Error; use thiserror::Error;
@ -226,16 +225,8 @@ impl Sink for AlsaSink {
let (pcm, bytes_per_period) = open_device(&self.device, self.format)?; let (pcm, bytes_per_period) = open_device(&self.device, self.format)?;
self.pcm = Some(pcm); self.pcm = Some(pcm);
match self.period_buffer.capacity().cmp(&bytes_per_period) { if self.period_buffer.capacity() != bytes_per_period {
Ordering::Greater => { self.period_buffer = Vec::with_capacity(bytes_per_period);
self.period_buffer.truncate(bytes_per_period);
self.period_buffer.shrink_to_fit();
}
Ordering::Less => {
let extra = bytes_per_period - self.period_buffer.len();
self.period_buffer.reserve_exact(extra);
}
Ordering::Equal => (),
} }
// Should always match the "Period Buffer size in bytes: " trace! message. // Should always match the "Period Buffer size in bytes: " trace! message.