update cpal to 0.15 (#478)

This commit is contained in:
François 2023-02-17 13:05:23 +01:00 committed by GitHub
parent 317ec569fe
commit 0c0e086b6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 118 additions and 26 deletions

View file

@ -11,7 +11,7 @@ exclude = ["assets/**", "tests/**"]
edition = "2018"
[dependencies]
cpal = "0.14"
cpal = "0.15"
claxon = { version = "0.4.2", optional = true }
hound = { version = "3.3.1", optional = true }
lewton = { version = "0.10", optional = true }

View file

@ -1,4 +1,4 @@
use cpal::Sample as CpalSample;
use cpal::{FromSample, Sample as CpalSample};
use std::marker::PhantomData;
/// Converts the samples data type to `O`.
@ -29,13 +29,13 @@ impl<I, O> Iterator for DataConverter<I, O>
where
I: Iterator,
I::Item: Sample,
O: Sample,
O: FromSample<I::Item> + Sample,
{
type Item = O;
#[inline]
fn next(&mut self) -> Option<O> {
self.input.next().map(|s| CpalSample::from(&s))
self.input.next().map(|s| CpalSample::from_sample(s))
}
#[inline]
@ -48,7 +48,7 @@ impl<I, O> ExactSizeIterator for DataConverter<I, O>
where
I: ExactSizeIterator,
I::Item: Sample,
O: Sample,
O: FromSample<I::Item> + Sample,
{
}
@ -93,7 +93,7 @@ impl Sample for u16 {
#[inline]
fn amplify(self, value: f32) -> u16 {
self.to_i16().amplify(value).to_u16()
((self as f32) * value) as u16
}
#[inline]

View file

@ -5,6 +5,7 @@ use std::time::Duration;
use crate::stream::{OutputStreamHandle, PlayError};
use crate::{queue, source::Done, Sample, Source};
use cpal::FromSample;
/// Handle to an device that outputs sounds.
///
@ -61,8 +62,8 @@ impl Sink {
pub fn append<S>(&self, source: S)
where
S: Source + Send + 'static,
S::Item: Sample,
S::Item: Send,
f32: FromSample<S::Item>,
S::Item: Sample + Send,
{
let controls = self.controls.clone();

View file

@ -1,5 +1,7 @@
use std::time::Duration;
use cpal::FromSample;
use crate::source::{FadeIn, Mix, TakeDuration};
use crate::{Sample, Source};
@ -14,7 +16,7 @@ pub fn crossfade<I1, I2>(
where
I1: Source,
I2: Source,
I1::Item: Sample,
I1::Item: FromSample<I2::Item> + Sample,
I2::Item: Sample,
{
let mut input_fadeout = input_fadeout.take_duration(duration);

View file

@ -3,12 +3,13 @@ use std::time::Duration;
use crate::source::uniform::UniformSourceIterator;
use crate::{Sample, Source};
use cpal::{FromSample, Sample as CpalSample};
/// Internal function that builds a `Mix` object.
pub fn mix<I1, I2>(input1: I1, input2: I2) -> Mix<I1, I2>
where
I1: Source,
I1::Item: Sample,
I1::Item: FromSample<I2::Item> + Sample,
I2: Source,
I2::Item: Sample,
{
@ -26,18 +27,18 @@ where
pub struct Mix<I1, I2>
where
I1: Source,
I1::Item: Sample,
I1::Item: FromSample<I2::Item> + Sample,
I2: Source,
I2::Item: Sample,
{
input1: UniformSourceIterator<I1, I1::Item>,
input2: UniformSourceIterator<I2, I1::Item>,
input2: UniformSourceIterator<I2, I2::Item>,
}
impl<I1, I2> Iterator for Mix<I1, I2>
where
I1: Source,
I1::Item: Sample,
I1::Item: FromSample<I2::Item> + Sample,
I2: Source,
I2::Item: Sample,
{
@ -49,9 +50,9 @@ where
let s2 = self.input2.next();
match (s1, s2) {
(Some(s1), Some(s2)) => Some(s1.saturating_add(s2)),
(Some(s1), Some(s2)) => Some(s1.saturating_add(CpalSample::from_sample(s2))),
(Some(s1), None) => Some(s1),
(None, Some(s2)) => Some(s2),
(None, Some(s2)) => Some(CpalSample::from_sample(s2)),
(None, None) => None,
}
}
@ -74,7 +75,7 @@ where
impl<I1, I2> ExactSizeIterator for Mix<I1, I2>
where
I1: Source + ExactSizeIterator,
I1::Item: Sample,
I1::Item: FromSample<I2::Item> + Sample,
I2: Source + ExactSizeIterator,
I2::Item: Sample,
{
@ -83,7 +84,7 @@ where
impl<I1, I2> Source for Mix<I1, I2>
where
I1: Source,
I1::Item: Sample,
I1::Item: FromSample<I2::Item> + Sample,
I2: Source,
I2::Item: Sample,
{

View file

@ -2,6 +2,8 @@
use std::time::Duration;
use cpal::FromSample;
use crate::Sample;
pub use self::amplify::Amplify;
@ -158,6 +160,7 @@ where
fn mix<S>(self, other: S) -> Mix<Self, S>
where
Self: Sized,
Self::Item: FromSample<S::Item>,
S: Source,
S::Item: Sample,
{
@ -224,6 +227,7 @@ where
fn take_crossfade_with<S: Source>(self, other: S, duration: Duration) -> Crossfade<Self, S>
where
Self: Sized,
Self::Item: FromSample<S::Item>,
<S as Iterator>::Item: Sample,
{
crossfade::crossfade(self, other, duration)

View file

@ -2,7 +2,7 @@ use std::marker::PhantomData;
use std::time::Duration;
use crate::{Sample, Source};
use cpal::Sample as CpalSample;
use cpal::{FromSample, Sample as CpalSample};
/// An iterator that reads from a `Source` and converts the samples to a specific rate and
/// channels count.
@ -47,13 +47,13 @@ impl<I, D> Iterator for SamplesConverter<I, D>
where
I: Source,
I::Item: Sample,
D: Sample,
D: FromSample<I::Item> + Sample,
{
type Item = D;
#[inline]
fn next(&mut self) -> Option<D> {
self.inner.next().map(|s| CpalSample::from(&s))
self.inner.next().map(|s| CpalSample::from_sample(s))
}
#[inline]
@ -66,7 +66,7 @@ impl<I, D> ExactSizeIterator for SamplesConverter<I, D>
where
I: Source + ExactSizeIterator,
I::Item: Sample,
D: Sample,
D: FromSample<I::Item> + Sample,
{
}
@ -74,7 +74,7 @@ impl<I, D> Source for SamplesConverter<I, D>
where
I: Source,
I::Item: Sample,
D: Sample,
D: FromSample<I::Item> + Sample,
{
#[inline]
fn current_frame_len(&self) -> Option<usize> {

View file

@ -1,6 +1,8 @@
use std::cmp;
use std::time::Duration;
use cpal::FromSample;
use crate::conversions::{ChannelCountConverter, DataConverter, SampleRateConverter};
use crate::{Sample, Source};
@ -77,7 +79,7 @@ impl<I, D> Iterator for UniformSourceIterator<I, D>
where
I: Source,
I::Item: Sample,
D: Sample,
D: FromSample<I::Item> + Sample,
{
type Item = D;
@ -114,7 +116,7 @@ impl<I, D> Source for UniformSourceIterator<I, D>
where
I: Iterator + Source,
I::Item: Sample,
D: Sample,
D: FromSample<I::Item> + Sample,
{
#[inline]
fn current_frame_len(&self) -> Option<usize> {

View file

@ -2,6 +2,8 @@ use std::f32;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use cpal::FromSample;
use crate::source::Spatial;
use crate::stream::{OutputStreamHandle, PlayError};
use crate::{Sample, Sink, Source};
@ -55,6 +57,7 @@ impl SpatialSink {
pub fn append<S>(&self, source: S)
where
S: Source + Send + 'static,
f32: FromSample<S::Item>,
S::Item: Sample + Send,
{
let positions = self.positions.clone();

View file

@ -219,14 +219,65 @@ impl CpalDeviceExt for cpal::Device {
.for_each(|d| *d = mixer_rx.next().unwrap_or(0f32))
},
error_callback,
None,
),
cpal::SampleFormat::F64 => self.build_output_stream::<f64, _, _>(
&format.config(),
move |data, _| {
data.iter_mut()
.for_each(|d| *d = mixer_rx.next().map(Sample::from_sample).unwrap_or(0f64))
},
error_callback,
None,
),
cpal::SampleFormat::I8 => self.build_output_stream::<i8, _, _>(
&format.config(),
move |data, _| {
data.iter_mut()
.for_each(|d| *d = mixer_rx.next().map(Sample::from_sample).unwrap_or(0i8))
},
error_callback,
None,
),
cpal::SampleFormat::I16 => self.build_output_stream::<i16, _, _>(
&format.config(),
move |data, _| {
data.iter_mut()
.for_each(|d| *d = mixer_rx.next().map(|s| s.to_i16()).unwrap_or(0i16))
.for_each(|d| *d = mixer_rx.next().map(Sample::from_sample).unwrap_or(0i16))
},
error_callback,
None,
),
cpal::SampleFormat::I32 => self.build_output_stream::<i32, _, _>(
&format.config(),
move |data, _| {
data.iter_mut()
.for_each(|d| *d = mixer_rx.next().map(Sample::from_sample).unwrap_or(0i32))
},
error_callback,
None,
),
cpal::SampleFormat::I64 => self.build_output_stream::<i64, _, _>(
&format.config(),
move |data, _| {
data.iter_mut()
.for_each(|d| *d = mixer_rx.next().map(Sample::from_sample).unwrap_or(0i64))
},
error_callback,
None,
),
cpal::SampleFormat::U8 => self.build_output_stream::<u8, _, _>(
&format.config(),
move |data, _| {
data.iter_mut().for_each(|d| {
*d = mixer_rx
.next()
.map(Sample::from_sample)
.unwrap_or(u8::max_value() / 2)
})
},
error_callback,
None,
),
cpal::SampleFormat::U16 => self.build_output_stream::<u16, _, _>(
&format.config(),
@ -234,12 +285,40 @@ impl CpalDeviceExt for cpal::Device {
data.iter_mut().for_each(|d| {
*d = mixer_rx
.next()
.map(|s| s.to_u16())
.map(Sample::from_sample)
.unwrap_or(u16::max_value() / 2)
})
},
error_callback,
None,
),
cpal::SampleFormat::U32 => self.build_output_stream::<u32, _, _>(
&format.config(),
move |data, _| {
data.iter_mut().for_each(|d| {
*d = mixer_rx
.next()
.map(Sample::from_sample)
.unwrap_or(u32::max_value() / 2)
})
},
error_callback,
None,
),
cpal::SampleFormat::U64 => self.build_output_stream::<u64, _, _>(
&format.config(),
move |data, _| {
data.iter_mut().for_each(|d| {
*d = mixer_rx
.next()
.map(Sample::from_sample)
.unwrap_or(u64::max_value() / 2)
})
},
error_callback,
None,
),
_ => return Err(cpal::BuildStreamError::StreamConfigNotSupported),
}
.map(|stream| (mixer_tx, stream))
}