Improve ALSA buffer size

* Go back to 4 periods at 125ms.
* Deal strictly in period time and periods to set ALSA buffer.
* Rename `buffer` to `period_buffer`.
* Add comments and change some other var names to add clarity.
* Let ALSA calculate the size of `period_buffer`.
This commit is contained in:
JasonLG1979 2021-06-05 16:23:13 -05:00
parent 33c07f0e2b
commit 4af095e741
2 changed files with 33 additions and 30 deletions

View file

@ -24,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [playback] `alsamixer`: complete rewrite (breaking) - [playback] `alsamixer`: complete rewrite (breaking)
- [playback] `alsamixer`: query card dB range for the `log` volume control unless specified otherwise - [playback] `alsamixer`: query card dB range for the `log` volume control unless specified otherwise
- [playback] `alsamixer`: use `--device` name for `--mixer-card` unless specified otherwise - [playback] `alsamixer`: use `--device` name for `--mixer-card` unless specified otherwise
- [audio_backend, alsa] Deal strictly in period time and number of periods to set ALSA buffer to be sample rate, channel count and format independent in `alsa-backend`
- [audio_backend, alsa] Use ALSA to calculate the size of internal buffer in `alsa-backend`
### Deprecated ### Deprecated
- [connect] The `discovery` module was deprecated in favor of the `librespot-discovery` crate - [connect] The `discovery` module was deprecated in favor of the `librespot-discovery` crate
@ -41,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [playback] `alsamixer`: make `cubic` consistent between cards that report minimum volume as mute, and cards that report some dB value - [playback] `alsamixer`: make `cubic` consistent between cards that report minimum volume as mute, and cards that report some dB value
- [playback] `alsamixer`: make `--volume-ctrl {linear|log}` work as expected - [playback] `alsamixer`: make `--volume-ctrl {linear|log}` work as expected
- [playback] `alsa`, `gstreamer`, `pulseaudio`: always output in native endianness - [playback] `alsa`, `gstreamer`, `pulseaudio`: always output in native endianness
- [audio_backend, alsa] Return to a default buffer of approximately 0.5 secs in `alsa-backend`
## [0.2.0] - 2021-05-04 ## [0.2.0] - 2021-05-04

View file

@ -2,9 +2,9 @@ use super::{Open, Sink, SinkAsBytes};
use crate::config::AudioFormat; use crate::config::AudioFormat;
use crate::convert::Converter; use crate::convert::Converter;
use crate::decoder::AudioPacket; use crate::decoder::AudioPacket;
use crate::{NUM_CHANNELS, SAMPLES_PER_SECOND, SAMPLE_RATE}; use crate::{NUM_CHANNELS, SAMPLE_RATE};
use alsa::device_name::HintIter; use alsa::device_name::HintIter;
use alsa::pcm::{Access, Format, Frames, HwParams, PCM}; use alsa::pcm::{Access, Format, HwParams, PCM};
use alsa::{Direction, Error, ValueOr}; use alsa::{Direction, Error, ValueOr};
use std::cmp::min; use std::cmp::min;
use std::ffi::CString; use std::ffi::CString;
@ -12,14 +12,15 @@ use std::io;
use std::process::exit; use std::process::exit;
use std::time::Duration; use std::time::Duration;
const BUFFERED_LATENCY: Duration = Duration::from_millis(125); // 125 ms Period time * 4 periods = 0.5 sec buffer.
const BUFFERED_PERIODS: Frames = 4; const PERIOD_TIME: Duration = Duration::from_millis(125);
const NUM_PERIODS: u32 = 4;
pub struct AlsaSink { pub struct AlsaSink {
pcm: Option<PCM>, pcm: Option<PCM>,
format: AudioFormat, format: AudioFormat,
device: String, device: String,
buffer: Vec<u8>, period_buffer: Vec<u8>,
} }
fn list_outputs() { fn list_outputs() {
@ -39,7 +40,7 @@ fn list_outputs() {
} }
} }
fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, Frames), Box<Error>> { fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, usize), Box<Error>> {
let pcm = PCM::new(dev_name, Direction::Playback, false)?; let pcm = PCM::new(dev_name, Direction::Playback, false)?;
let alsa_format = match format { let alsa_format = match format {
AudioFormat::F64 => Format::float64(), AudioFormat::F64 => Format::float64(),
@ -54,28 +55,30 @@ fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, Frames), Box
AudioFormat::S24_3 => Format::S243BE, AudioFormat::S24_3 => Format::S243BE,
}; };
// http://www.linuxjournal.com/article/6735?page=0,1#N0x19ab2890.0x19ba78d8 let bytes_per_period = {
// latency = period_size * periods / (rate * bytes_per_frame)
// For stereo samples encoded as 32-bit float, one frame has a length of eight bytes.
let mut period_size = ((SAMPLES_PER_SECOND * format.size() as u32) as f32
* (BUFFERED_LATENCY.as_secs_f32() / BUFFERED_PERIODS as f32))
as Frames;
{
let hwp = HwParams::any(&pcm)?; let hwp = HwParams::any(&pcm)?;
hwp.set_access(Access::RWInterleaved)?; hwp.set_access(Access::RWInterleaved)?;
hwp.set_format(alsa_format)?; hwp.set_format(alsa_format)?;
hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest)?; hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest)?;
hwp.set_channels(NUM_CHANNELS as u32)?; hwp.set_channels(NUM_CHANNELS as u32)?;
period_size = hwp.set_period_size_near(period_size, ValueOr::Greater)?; // Deal strictly in time and periods.
hwp.set_buffer_size_near(period_size * BUFFERED_PERIODS)?; hwp.set_periods(NUM_PERIODS, ValueOr::Nearest)?;
hwp.set_period_time_near(PERIOD_TIME.as_micros() as u32, ValueOr::Nearest)?;
pcm.hw_params(&hwp)?; pcm.hw_params(&hwp)?;
let swp = pcm.sw_params_current()?; let swp = pcm.sw_params_current()?;
swp.set_start_threshold(hwp.get_buffer_size()? - hwp.get_period_size()?)?; // Don't assume we got what we wanted.
pcm.sw_params(&swp)?; // Ask to make sure.
} let frames_per_period = hwp.get_period_size()?;
Ok((pcm, period_size)) swp.set_start_threshold(hwp.get_buffer_size()? - frames_per_period)?;
pcm.sw_params(&swp)?;
// Let ALSA do the math for us.
pcm.frames_to_bytes(frames_per_period) as usize
};
Ok((pcm, bytes_per_period))
} }
impl Open for AlsaSink { impl Open for AlsaSink {
@ -97,7 +100,7 @@ impl Open for AlsaSink {
pcm: None, pcm: None,
format, format,
device: name, device: name,
buffer: vec![], period_buffer: vec![],
} }
} }
} }
@ -107,12 +110,9 @@ impl Sink for AlsaSink {
if self.pcm.is_none() { if self.pcm.is_none() {
let pcm = open_device(&self.device, self.format); let pcm = open_device(&self.device, self.format);
match pcm { match pcm {
Ok((p, period_size)) => { Ok((p, bytes_per_period)) => {
self.pcm = Some(p); self.pcm = Some(p);
// Create a buffer for all samples for a full period self.period_buffer = Vec::with_capacity(bytes_per_period);
self.buffer = Vec::with_capacity(
period_size as usize * BUFFERED_PERIODS as usize * self.format.size(),
);
} }
Err(e) => { Err(e) => {
error!("Alsa error PCM open {}", e); error!("Alsa error PCM open {}", e);
@ -147,15 +147,15 @@ impl SinkAsBytes for AlsaSink {
let mut processed_data = 0; let mut processed_data = 0;
while processed_data < data.len() { while processed_data < data.len() {
let data_to_buffer = min( let data_to_buffer = min(
self.buffer.capacity() - self.buffer.len(), self.period_buffer.capacity() - self.period_buffer.len(),
data.len() - processed_data, data.len() - processed_data,
); );
self.buffer self.period_buffer
.extend_from_slice(&data[processed_data..processed_data + data_to_buffer]); .extend_from_slice(&data[processed_data..processed_data + data_to_buffer]);
processed_data += data_to_buffer; processed_data += data_to_buffer;
if self.buffer.len() == self.buffer.capacity() { if self.period_buffer.len() == self.period_buffer.capacity() {
self.write_buf(); self.write_buf();
self.buffer.clear(); self.period_buffer.clear();
} }
} }
@ -169,7 +169,7 @@ impl AlsaSink {
fn write_buf(&mut self) { fn write_buf(&mut self) {
let pcm = self.pcm.as_mut().unwrap(); let pcm = self.pcm.as_mut().unwrap();
let io = pcm.io_bytes(); let io = pcm.io_bytes();
match io.writei(&self.buffer) { match io.writei(&self.period_buffer) {
Ok(_) => (), Ok(_) => (),
Err(err) => pcm.try_recover(err, false).unwrap(), Err(err) => pcm.try_recover(err, false).unwrap(),
}; };