prototyping an API for getting min and max buffersize

This commit is contained in:
JoshuaBatty 2020-05-05 15:34:52 +02:00
parent 5fa5ce593d
commit f762e5dd8f
1 changed files with 33 additions and 25 deletions

View File

@ -180,6 +180,9 @@ pub type ChannelCount = u16;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct SampleRate(pub u32); pub struct SampleRate(pub u32);
/// The desired number of frames for the hardware buffer.
pub type BufferSize = u32;
/// The set of parameters used to describe how to open a stream. /// The set of parameters used to describe how to open a stream.
/// ///
/// The sample format is omitted in favour of using a sample type. /// The sample format is omitted in favour of using a sample type.
@ -187,6 +190,16 @@ pub struct SampleRate(pub u32);
pub struct StreamConfig { pub struct StreamConfig {
pub channels: ChannelCount, pub channels: ChannelCount,
pub sample_rate: SampleRate, pub sample_rate: SampleRate,
pub buffer_size: BufferSize,
}
/// Describes the minimum and maximum supported buffer size for the device
/// and if requested buffersize must be a power of 2 value.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SupportedBufferSizeRange {
pub min: BufferSize,
pub max: BufferSize,
pub requires_power_of_two: bool,
} }
/// Describes a range of supported stream configurations, retrieved via the /// Describes a range of supported stream configurations, retrieved via the
@ -198,6 +211,8 @@ pub struct SupportedStreamConfigRange {
pub(crate) min_sample_rate: SampleRate, pub(crate) min_sample_rate: SampleRate,
/// Maximum value for the samples rate of the supported formats. /// Maximum value for the samples rate of the supported formats.
pub(crate) max_sample_rate: SampleRate, pub(crate) max_sample_rate: SampleRate,
/// Buffersize ranges supported by the device
pub(crate) buffer_size: SupportedBufferSizeRange,
/// Type of data expected by the device. /// Type of data expected by the device.
pub(crate) sample_format: SampleFormat, pub(crate) sample_format: SampleFormat,
} }
@ -208,6 +223,7 @@ pub struct SupportedStreamConfigRange {
pub struct SupportedStreamConfig { pub struct SupportedStreamConfig {
channels: ChannelCount, channels: ChannelCount,
sample_rate: SampleRate, sample_rate: SampleRate,
buffer_size: BufferSize,
sample_format: SampleFormat, sample_format: SampleFormat,
} }
@ -289,6 +305,10 @@ impl SupportedStreamConfig {
self.sample_rate self.sample_rate
} }
pub fn buffer_size(&self) -> BufferSize {
self.buffer_size
}
pub fn sample_format(&self) -> SampleFormat { pub fn sample_format(&self) -> SampleFormat {
self.sample_format self.sample_format
} }
@ -297,6 +317,7 @@ impl SupportedStreamConfig {
StreamConfig { StreamConfig {
channels: self.channels, channels: self.channels,
sample_rate: self.sample_rate, sample_rate: self.sample_rate,
buffer_size: self.buffer_size,
} }
} }
} }
@ -492,33 +513,32 @@ impl SupportedStreamConfigRange {
self.max_sample_rate self.max_sample_rate
} }
pub fn buffer_size(&self) -> &SupportedBufferSizeRange {
&self.buffer_size
}
pub fn sample_format(&self) -> SampleFormat { pub fn sample_format(&self) -> SampleFormat {
self.sample_format self.sample_format
} }
/// Retrieve a `SupportedStreamConfig` with the given sample rate. /// Retrieve a `SupportedStreamConfig` with the given sample rate and buffer size.
/// ///
/// **panic!**s if the given `sample_rate` is outside the range specified within this /// **panic!**s if the given `sample_rate` or the give `buffer_size` is outside the range specified within this
/// `SupportedStreamConfigRange` instance. /// `SupportedStreamConfigRange` instance.
pub fn with_sample_rate(self, sample_rate: SampleRate) -> SupportedStreamConfig { pub fn with_sample_rate_and_buffer_size(self, sample_rate: SampleRate, buffer_size: BufferSize) -> SupportedStreamConfig {
assert!(self.min_sample_rate <= sample_rate && sample_rate <= self.max_sample_rate); assert!(self.min_sample_rate <= sample_rate && sample_rate <= self.max_sample_rate);
assert!(self.buffer_size.min <= buffer_size && buffer_size <= self.buffer_size.max);
if self.buffer_size.requires_power_of_two {
assert!(buffer_size.is_power_of_two());
}
SupportedStreamConfig { SupportedStreamConfig {
channels: self.channels, channels: self.channels,
sample_format: self.sample_format, sample_format: self.sample_format,
buffer_size,
sample_rate, sample_rate,
} }
} }
/// Turns this `SupportedStreamConfigRange` into a `SupportedStreamConfig` corresponding to the maximum samples rate.
#[inline]
pub fn with_max_sample_rate(self) -> SupportedStreamConfig {
SupportedStreamConfig {
channels: self.channels,
sample_rate: self.max_sample_rate,
sample_format: self.sample_format,
}
}
/// A comparison function which compares two `SupportedStreamConfigRange`s in terms of their priority of /// A comparison function which compares two `SupportedStreamConfigRange`s in terms of their priority of
/// use as a default stream format. /// use as a default stream format.
/// ///
@ -596,18 +616,6 @@ impl From<SupportedStreamConfig> for StreamConfig {
} }
} }
impl From<SupportedStreamConfig> for SupportedStreamConfigRange {
#[inline]
fn from(format: SupportedStreamConfig) -> SupportedStreamConfigRange {
SupportedStreamConfigRange {
channels: format.channels,
min_sample_rate: format.sample_rate,
max_sample_rate: format.sample_rate,
sample_format: format.sample_format,
}
}
}
// If a backend does not provide an API for retrieving supported formats, we query it with a bunch // If a backend does not provide an API for retrieving supported formats, we query it with a bunch
// of commonly used rates. This is always the case for wasapi and is sometimes the case for alsa. // of commonly used rates. This is always the case for wasapi and is sometimes the case for alsa.
// //