Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable buffer size #512

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 53 additions & 17 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::dynamic_mixer::{self, DynamicMixerController};
use crate::sink::Sink;
use crate::source::Source;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{Sample, SupportedStreamConfig};
use cpal::{Sample, SampleFormat, StreamConfig, SupportedStreamConfig};

/// `cpal::Stream` container. Also see the more useful `OutputStreamHandle`.
///
Expand Down Expand Up @@ -51,6 +51,20 @@ impl OutputStream {
Ok((out, handle))
}

pub fn try_from_config(
device: &cpal::Device,
config: &StreamConfig,
sample_format: &SampleFormat,
) -> Result<(Self, OutputStreamHandle), StreamError> {
let (mixer, _stream) = device.try_new_output_stream(&config, &sample_format)?;
_stream.play()?;
PetrGlad marked this conversation as resolved.
Show resolved Hide resolved
let out = Self { mixer, _stream };
let handle = OutputStreamHandle {
mixer: Arc::downgrade(&out.mixer),
};
Ok((out, handle))
}

/// Return a new stream & handle using the default output device.
///
/// On failure will fallback to trying any non-default output devices.
Expand Down Expand Up @@ -192,28 +206,36 @@ impl error::Error for StreamError {
pub(crate) trait CpalDeviceExt {
fn new_output_stream_with_format(
&self,
format: cpal::SupportedStreamConfig,
config: &cpal::StreamConfig,
sample_format: &cpal::SampleFormat,
) -> Result<(Arc<DynamicMixerController<f32>>, cpal::Stream), cpal::BuildStreamError>;

fn try_new_output_stream_config(
&self,
config: cpal::SupportedStreamConfig,
) -> Result<(Arc<DynamicMixerController<f32>>, cpal::Stream), StreamError>;

fn try_new_output_stream(
&self,
config: &cpal::StreamConfig,
sample_format: &cpal::SampleFormat,
) -> Result<(Arc<DynamicMixerController<f32>>, cpal::Stream), StreamError>;
}

impl CpalDeviceExt for cpal::Device {
fn new_output_stream_with_format(
&self,
format: cpal::SupportedStreamConfig,
config: &cpal::StreamConfig,
sample_format: &cpal::SampleFormat,
) -> Result<(Arc<DynamicMixerController<f32>>, cpal::Stream), cpal::BuildStreamError> {
let (mixer_tx, mut mixer_rx) =
dynamic_mixer::mixer::<f32>(format.channels(), format.sample_rate().0);
dynamic_mixer::mixer::<f32>(config.channels, config.sample_rate.0);

let error_callback = |err| eprintln!("an error occurred on output stream: {}", err);

match format.sample_format() {
match sample_format {
cpal::SampleFormat::F32 => self.build_output_stream::<f32, _, _>(
&format.config(),
&config,
move |data, _| {
data.iter_mut()
.for_each(|d| *d = mixer_rx.next().unwrap_or(0f32))
Expand All @@ -222,7 +244,7 @@ impl CpalDeviceExt for cpal::Device {
None,
),
cpal::SampleFormat::F64 => self.build_output_stream::<f64, _, _>(
&format.config(),
&config,
move |data, _| {
data.iter_mut()
.for_each(|d| *d = mixer_rx.next().map(Sample::from_sample).unwrap_or(0f64))
Expand All @@ -231,7 +253,7 @@ impl CpalDeviceExt for cpal::Device {
None,
),
cpal::SampleFormat::I8 => self.build_output_stream::<i8, _, _>(
&format.config(),
&config,
move |data, _| {
data.iter_mut()
.for_each(|d| *d = mixer_rx.next().map(Sample::from_sample).unwrap_or(0i8))
Expand All @@ -240,7 +262,7 @@ impl CpalDeviceExt for cpal::Device {
None,
),
cpal::SampleFormat::I16 => self.build_output_stream::<i16, _, _>(
&format.config(),
&config,
move |data, _| {
data.iter_mut()
.for_each(|d| *d = mixer_rx.next().map(Sample::from_sample).unwrap_or(0i16))
Expand All @@ -249,7 +271,7 @@ impl CpalDeviceExt for cpal::Device {
None,
),
cpal::SampleFormat::I32 => self.build_output_stream::<i32, _, _>(
&format.config(),
&config,
move |data, _| {
data.iter_mut()
.for_each(|d| *d = mixer_rx.next().map(Sample::from_sample).unwrap_or(0i32))
Expand All @@ -258,7 +280,7 @@ impl CpalDeviceExt for cpal::Device {
None,
),
cpal::SampleFormat::I64 => self.build_output_stream::<i64, _, _>(
&format.config(),
&config,
move |data, _| {
data.iter_mut()
.for_each(|d| *d = mixer_rx.next().map(Sample::from_sample).unwrap_or(0i64))
Expand All @@ -267,7 +289,7 @@ impl CpalDeviceExt for cpal::Device {
None,
),
cpal::SampleFormat::U8 => self.build_output_stream::<u8, _, _>(
&format.config(),
&config,
move |data, _| {
data.iter_mut().for_each(|d| {
*d = mixer_rx
Expand All @@ -280,7 +302,7 @@ impl CpalDeviceExt for cpal::Device {
None,
),
cpal::SampleFormat::U16 => self.build_output_stream::<u16, _, _>(
&format.config(),
&config,
move |data, _| {
data.iter_mut().for_each(|d| {
*d = mixer_rx
Expand All @@ -293,7 +315,7 @@ impl CpalDeviceExt for cpal::Device {
None,
),
cpal::SampleFormat::U32 => self.build_output_stream::<u32, _, _>(
&format.config(),
&config,
move |data, _| {
data.iter_mut().for_each(|d| {
*d = mixer_rx
Expand All @@ -306,7 +328,7 @@ impl CpalDeviceExt for cpal::Device {
None,
),
cpal::SampleFormat::U64 => self.build_output_stream::<u64, _, _>(
&format.config(),
&config,
move |data, _| {
data.iter_mut().for_each(|d| {
*d = mixer_rx
Expand All @@ -327,10 +349,24 @@ impl CpalDeviceExt for cpal::Device {
&self,
config: SupportedStreamConfig,
) -> Result<(Arc<DynamicMixerController<f32>>, cpal::Stream), StreamError> {
self.new_output_stream_with_format(config).or_else(|err| {
self.new_output_stream_with_format(&config.config(), &config.sample_format()).or_else(|err| {
// look through all supported formats to see if another works
supported_output_formats(self)?
.find_map(|format| self.new_output_stream_with_format(&format.config(), &config.sample_format()).ok())
// return original error if nothing works
.ok_or(StreamError::BuildStreamError(err))
})
}

fn try_new_output_stream(
&self,
config: &StreamConfig,
sample_format: &SampleFormat,
) -> Result<(Arc<DynamicMixerController<f32>>, cpal::Stream), StreamError> {
self.new_output_stream_with_format(&config, &sample_format).or_else(|err| {
// look through all supported formats to see if another works
supported_output_formats(self)?
.find_map(|format| self.new_output_stream_with_format(format).ok())
.find_map(|format| self.new_output_stream_with_format(&format.config(), &sample_format).ok())
// return original error if nothing works
.ok_or(StreamError::BuildStreamError(err))
})
Expand Down
Loading