perf(rodio): reduce control polling during playback
This commit is contained in:
1 parent
3c1c598e34
commit
1e6a8b1aee
70 files changed
+9378
No files matched your search
+103
@@ -0,0 +1,103 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Internal function that builds a `Amplify` object.
|
||||
pub fn amplify<I>(input: I, factor: f32) -> Amplify<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
Amplify { input, factor }
|
||||
}
|
||||
|
||||
/// Filter that modifies each sample by a given value.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Amplify<I> {
|
||||
input: I,
|
||||
factor: f32,
|
||||
}
|
||||
|
||||
impl<I> Amplify<I> {
|
||||
/// Modifies the amplification factor.
|
||||
#[inline]
|
||||
pub fn set_factor(&mut self, factor: f32) {
|
||||
self.factor = factor;
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for Amplify<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
self.input.next().map(|value| value.amplify(self.factor))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> ExactSizeIterator for Amplify<I>
|
||||
where
|
||||
I: Source + ExactSizeIterator,
|
||||
I::Item: Sample,
|
||||
{
|
||||
}
|
||||
|
||||
impl<I> Source for Amplify<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
}
|
||||
}
|
||||
+251
@@ -0,0 +1,251 @@
|
||||
use std::f32::consts::PI;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::Source;
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
// Implemented following http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
|
||||
|
||||
/// Internal function that builds a `BltFilter` object.
|
||||
pub fn low_pass<I>(input: I, freq: u32) -> BltFilter<I>
|
||||
where
|
||||
I: Source<Item = f32>,
|
||||
{
|
||||
low_pass_with_q(input, freq, 0.5)
|
||||
}
|
||||
|
||||
pub fn high_pass<I>(input: I, freq: u32) -> BltFilter<I>
|
||||
where
|
||||
I: Source<Item = f32>,
|
||||
{
|
||||
high_pass_with_q(input, freq, 0.5)
|
||||
}
|
||||
|
||||
/// Same as low_pass but allows the q value (bandwidth) to be changed
|
||||
pub fn low_pass_with_q<I>(input: I, freq: u32, q: f32) -> BltFilter<I>
|
||||
where
|
||||
I: Source<Item = f32>,
|
||||
{
|
||||
BltFilter {
|
||||
input,
|
||||
formula: BltFormula::LowPass { freq, q },
|
||||
applier: None,
|
||||
x_n1: 0.0,
|
||||
x_n2: 0.0,
|
||||
y_n1: 0.0,
|
||||
y_n2: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Same as high_pass but allows the q value (bandwidth) to be changed
|
||||
pub fn high_pass_with_q<I>(input: I, freq: u32, q: f32) -> BltFilter<I>
|
||||
where
|
||||
I: Source<Item = f32>,
|
||||
{
|
||||
BltFilter {
|
||||
input,
|
||||
formula: BltFormula::HighPass { freq, q },
|
||||
applier: None,
|
||||
x_n1: 0.0,
|
||||
x_n2: 0.0,
|
||||
y_n1: 0.0,
|
||||
y_n2: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BltFilter<I> {
|
||||
input: I,
|
||||
formula: BltFormula,
|
||||
applier: Option<BltApplier>,
|
||||
x_n1: f32,
|
||||
x_n2: f32,
|
||||
y_n1: f32,
|
||||
y_n2: f32,
|
||||
}
|
||||
|
||||
impl<I> BltFilter<I> {
|
||||
/// Modifies this filter so that it becomes a low-pass filter.
|
||||
pub fn to_low_pass(&mut self, freq: u32) {
|
||||
self.to_low_pass_with_q(freq, 0.5);
|
||||
}
|
||||
|
||||
/// Modifies this filter so that it becomes a high-pass filter
|
||||
pub fn to_high_pass(&mut self, freq: u32) {
|
||||
self.to_high_pass_with_q(freq, 0.5);
|
||||
}
|
||||
|
||||
/// Same as to_low_pass but allows the q value (bandwidth) to be changed
|
||||
pub fn to_low_pass_with_q(&mut self, freq: u32, q: f32) {
|
||||
self.formula = BltFormula::LowPass { freq, q };
|
||||
self.applier = None;
|
||||
}
|
||||
|
||||
/// Same as to_high_pass but allows the q value (bandwidth) to be changed
|
||||
pub fn to_high_pass_with_q(&mut self, freq: u32, q: f32) {
|
||||
self.formula = BltFormula::HighPass { freq, q };
|
||||
self.applier = None;
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for BltFilter<I>
|
||||
where
|
||||
I: Source<Item = f32>,
|
||||
{
|
||||
type Item = f32;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<f32> {
|
||||
let last_in_frame = self.input.current_frame_len() == Some(1);
|
||||
|
||||
if self.applier.is_none() {
|
||||
self.applier = Some(self.formula.to_applier(self.input.sample_rate()));
|
||||
}
|
||||
|
||||
let sample = match self.input.next() {
|
||||
None => return None,
|
||||
Some(s) => s,
|
||||
};
|
||||
|
||||
let result = self
|
||||
.applier
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.apply(sample, self.x_n1, self.x_n2, self.y_n1, self.y_n2);
|
||||
|
||||
self.y_n2 = self.y_n1;
|
||||
self.x_n2 = self.x_n1;
|
||||
self.y_n1 = result;
|
||||
self.x_n1 = sample;
|
||||
|
||||
if last_in_frame {
|
||||
self.applier = None;
|
||||
}
|
||||
|
||||
Some(result)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> ExactSizeIterator for BltFilter<I> where I: Source<Item = f32> + ExactSizeIterator {}
|
||||
|
||||
impl<I> Source for BltFilter<I>
|
||||
where
|
||||
I: Source<Item = f32>,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum BltFormula {
|
||||
LowPass { freq: u32, q: f32 },
|
||||
HighPass { freq: u32, q: f32 },
|
||||
}
|
||||
|
||||
impl BltFormula {
|
||||
fn to_applier(&self, sampling_frequency: u32) -> BltApplier {
|
||||
match *self {
|
||||
BltFormula::LowPass { freq, q } => {
|
||||
let w0 = 2.0 * PI * freq as f32 / sampling_frequency as f32;
|
||||
|
||||
let alpha = w0.sin() / (2.0 * q);
|
||||
let b1 = 1.0 - w0.cos();
|
||||
let b0 = b1 / 2.0;
|
||||
let b2 = b0;
|
||||
let a0 = 1.0 + alpha;
|
||||
let a1 = -2.0 * w0.cos();
|
||||
let a2 = 1.0 - alpha;
|
||||
|
||||
BltApplier {
|
||||
b0: b0 / a0,
|
||||
b1: b1 / a0,
|
||||
b2: b2 / a0,
|
||||
a1: a1 / a0,
|
||||
a2: a2 / a0,
|
||||
}
|
||||
}
|
||||
BltFormula::HighPass { freq, q } => {
|
||||
let w0 = 2.0 * PI * freq as f32 / sampling_frequency as f32;
|
||||
let cos_w0 = w0.cos();
|
||||
let alpha = w0.sin() / (2.0 * q);
|
||||
|
||||
let b0 = (1.0 + cos_w0) / 2.0;
|
||||
let b1 = -1.0 - cos_w0;
|
||||
let b2 = b0;
|
||||
let a0 = 1.0 + alpha;
|
||||
let a1 = -2.0 * cos_w0;
|
||||
let a2 = 1.0 - alpha;
|
||||
|
||||
BltApplier {
|
||||
b0: b0 / a0,
|
||||
b1: b1 / a0,
|
||||
b2: b2 / a0,
|
||||
a1: a1 / a0,
|
||||
a2: a2 / a0,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct BltApplier {
|
||||
b0: f32,
|
||||
b1: f32,
|
||||
b2: f32,
|
||||
a1: f32,
|
||||
a2: f32,
|
||||
}
|
||||
|
||||
impl BltApplier {
|
||||
#[inline]
|
||||
fn apply(&self, x_n: f32, x_n1: f32, x_n2: f32, y_n1: f32, y_n2: f32) -> f32 {
|
||||
self.b0 * x_n + self.b1 * x_n1 + self.b2 * x_n2 - self.a1 * y_n1 - self.a2 * y_n2
|
||||
}
|
||||
}
|
||||
+268
@@ -0,0 +1,268 @@
|
||||
use std::cmp;
|
||||
use std::mem;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Internal function that builds a `Buffered` object.
|
||||
#[inline]
|
||||
pub fn buffered<I>(input: I) -> Buffered<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
let total_duration = input.total_duration();
|
||||
let first_frame = extract(input);
|
||||
|
||||
Buffered {
|
||||
current_frame: first_frame,
|
||||
position_in_frame: 0,
|
||||
total_duration,
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator that at the same time extracts data from the iterator and stores it in a buffer.
|
||||
pub struct Buffered<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
/// Immutable reference to the next frame of data. Cannot be `Frame::Input`.
|
||||
current_frame: Arc<Frame<I>>,
|
||||
|
||||
/// The position in number of samples of this iterator inside `current_frame`.
|
||||
position_in_frame: usize,
|
||||
|
||||
/// Obtained once at creation and never modified again.
|
||||
total_duration: Option<Duration>,
|
||||
}
|
||||
|
||||
enum Frame<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
/// Data that has already been extracted from the iterator. Also contains a pointer to the
|
||||
/// next frame.
|
||||
Data(FrameData<I>),
|
||||
|
||||
/// No more data.
|
||||
End,
|
||||
|
||||
/// Unextracted data. The `Option` should never be `None` and is only here for easier data
|
||||
/// processing.
|
||||
Input(Mutex<Option<I>>),
|
||||
}
|
||||
|
||||
struct FrameData<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
data: Vec<I::Item>,
|
||||
channels: u16,
|
||||
rate: u32,
|
||||
next: Mutex<Arc<Frame<I>>>,
|
||||
}
|
||||
|
||||
impl<I> Drop for FrameData<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
fn drop(&mut self) {
|
||||
// This is necessary to prevent stack overflows deallocating long chains of the mutually
|
||||
// recursive `Frame` and `FrameData` types. This iteratively traverses as much of the
|
||||
// chain as needs to be deallocated, and repeatedly "pops" the head off the list. This
|
||||
// solves the problem, as when the time comes to actually deallocate the `FrameData`,
|
||||
// the `next` field will contain a `Frame::End`, or an `Arc` with additional references,
|
||||
// so the depth of recursive drops will be bounded.
|
||||
while let Ok(arc_next) = self.next.get_mut() {
|
||||
if let Some(next_ref) = Arc::get_mut(arc_next) {
|
||||
// This allows us to own the next Frame.
|
||||
let next = mem::replace(next_ref, Frame::End);
|
||||
if let Frame::Data(next_data) = next {
|
||||
// Swap the current FrameData with the next one, allowing the current one
|
||||
// to go out of scope.
|
||||
*self = next_data;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds a frame from the input iterator.
|
||||
fn extract<I>(mut input: I) -> Arc<Frame<I>>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
let frame_len = input.current_frame_len();
|
||||
|
||||
if frame_len == Some(0) {
|
||||
return Arc::new(Frame::End);
|
||||
}
|
||||
|
||||
let channels = input.channels();
|
||||
let rate = input.sample_rate();
|
||||
let data: Vec<I::Item> = input
|
||||
.by_ref()
|
||||
.take(cmp::min(frame_len.unwrap_or(32768), 32768))
|
||||
.collect();
|
||||
|
||||
if data.is_empty() {
|
||||
return Arc::new(Frame::End);
|
||||
}
|
||||
|
||||
Arc::new(Frame::Data(FrameData {
|
||||
data,
|
||||
channels,
|
||||
rate,
|
||||
next: Mutex::new(Arc::new(Frame::Input(Mutex::new(Some(input))))),
|
||||
}))
|
||||
}
|
||||
|
||||
impl<I> Buffered<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
/// Advances to the next frame.
|
||||
fn next_frame(&mut self) {
|
||||
let next_frame = {
|
||||
let mut next_frame_ptr = match &*self.current_frame {
|
||||
Frame::Data(FrameData { next, .. }) => next.lock().unwrap(),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let next_frame = match &**next_frame_ptr {
|
||||
Frame::Data(_) => next_frame_ptr.clone(),
|
||||
Frame::End => next_frame_ptr.clone(),
|
||||
Frame::Input(input) => {
|
||||
let input = input.lock().unwrap().take().unwrap();
|
||||
extract(input)
|
||||
}
|
||||
};
|
||||
|
||||
*next_frame_ptr = next_frame.clone();
|
||||
next_frame
|
||||
};
|
||||
|
||||
self.current_frame = next_frame;
|
||||
self.position_in_frame = 0;
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for Buffered<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
let current_sample;
|
||||
let advance_frame;
|
||||
|
||||
match &*self.current_frame {
|
||||
Frame::Data(FrameData { data, .. }) => {
|
||||
current_sample = Some(data[self.position_in_frame]);
|
||||
self.position_in_frame += 1;
|
||||
advance_frame = self.position_in_frame >= data.len();
|
||||
}
|
||||
|
||||
Frame::End => {
|
||||
current_sample = None;
|
||||
advance_frame = false;
|
||||
}
|
||||
|
||||
Frame::Input(_) => unreachable!(),
|
||||
};
|
||||
|
||||
if advance_frame {
|
||||
self.next_frame();
|
||||
}
|
||||
|
||||
current_sample
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
// TODO:
|
||||
(0, None)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: uncomment when `size_hint` is fixed
|
||||
/*impl<I> ExactSizeIterator for Amplify<I> where I: Source + ExactSizeIterator, I::Item: Sample {
|
||||
}*/
|
||||
|
||||
impl<I> Source for Buffered<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
match &*self.current_frame {
|
||||
Frame::Data(FrameData { data, .. }) => Some(data.len() - self.position_in_frame),
|
||||
Frame::End => Some(0),
|
||||
Frame::Input(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
match *self.current_frame {
|
||||
Frame::Data(FrameData { channels, .. }) => channels,
|
||||
Frame::End => 1,
|
||||
Frame::Input(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
match *self.current_frame {
|
||||
Frame::Data(FrameData { rate, .. }) => rate,
|
||||
Frame::End => 44100,
|
||||
Frame::Input(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.total_duration
|
||||
}
|
||||
|
||||
/// Can not support seek, in the end state we lose the underlying source
|
||||
/// which makes seeking back impossible.
|
||||
#[inline]
|
||||
fn try_seek(&mut self, _: Duration) -> Result<(), SeekError> {
|
||||
Err(SeekError::NotSupported {
|
||||
underlying_source: std::any::type_name::<Self>(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Clone for Buffered<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn clone(&self) -> Buffered<I> {
|
||||
Buffered {
|
||||
current_frame: self.current_frame.clone(),
|
||||
position_in_frame: self.position_in_frame,
|
||||
total_duration: self.total_duration,
|
||||
}
|
||||
}
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Combines channels in input into a single mono source, then plays that mono sound
|
||||
/// to each channel at the volume given for that channel.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ChannelVolume<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
input: I,
|
||||
// Channel number is used as index for amplification value.
|
||||
channel_volumes: Vec<f32>,
|
||||
// Current listener being processed.
|
||||
current_channel: usize,
|
||||
current_sample: Option<I::Item>,
|
||||
}
|
||||
|
||||
impl<I> ChannelVolume<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
pub fn new(mut input: I, channel_volumes: Vec<f32>) -> ChannelVolume<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
let mut sample = None;
|
||||
for _ in 0..input.channels() {
|
||||
if let Some(s) = input.next() {
|
||||
sample = Some(
|
||||
sample
|
||||
.get_or_insert_with(I::Item::zero_value)
|
||||
.saturating_add(s),
|
||||
);
|
||||
}
|
||||
}
|
||||
ChannelVolume {
|
||||
input,
|
||||
channel_volumes,
|
||||
current_channel: 0,
|
||||
current_sample: sample,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the volume for a given channel number. Will panic if channel number
|
||||
/// was invalid.
|
||||
pub fn set_volume(&mut self, channel: usize, volume: f32) {
|
||||
self.channel_volumes[channel] = volume;
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for ChannelVolume<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
// return value
|
||||
let ret = self
|
||||
.current_sample
|
||||
.map(|sample| sample.amplify(self.channel_volumes[self.current_channel]));
|
||||
self.current_channel += 1;
|
||||
if self.current_channel >= self.channel_volumes.len() {
|
||||
self.current_channel = 0;
|
||||
self.current_sample = None;
|
||||
for _ in 0..self.input.channels() {
|
||||
if let Some(s) = self.input.next() {
|
||||
self.current_sample = Some(
|
||||
self.current_sample
|
||||
.get_or_insert_with(I::Item::zero_value)
|
||||
.saturating_add(s),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> ExactSizeIterator for ChannelVolume<I>
|
||||
where
|
||||
I: Source + ExactSizeIterator,
|
||||
I::Item: Sample,
|
||||
{
|
||||
}
|
||||
|
||||
impl<I> Source for ChannelVolume<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.channel_volumes.len() as u16
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use cpal::FromSample;
|
||||
|
||||
use crate::source::{FadeIn, Mix, TakeDuration};
|
||||
use crate::{Sample, Source};
|
||||
|
||||
/// Mixes one sound fading out with another sound fading in for the given duration.
|
||||
///
|
||||
/// Only the crossfaded portion (beginning of fadeout, beginning of fadein) is returned.
|
||||
pub fn crossfade<I1, I2>(
|
||||
input_fadeout: I1,
|
||||
input_fadein: I2,
|
||||
duration: Duration,
|
||||
) -> Crossfade<I1, I2>
|
||||
where
|
||||
I1: Source,
|
||||
I2: Source,
|
||||
I1::Item: FromSample<I2::Item> + Sample,
|
||||
I2::Item: Sample,
|
||||
{
|
||||
let mut input_fadeout = input_fadeout.take_duration(duration);
|
||||
input_fadeout.set_filter_fadeout();
|
||||
let input_fadein = input_fadein.take_duration(duration).fade_in(duration);
|
||||
input_fadeout.mix(input_fadein)
|
||||
}
|
||||
|
||||
pub type Crossfade<I1, I2> = Mix<TakeDuration<I1>, FadeIn<TakeDuration<I2>>>;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::buffer::SamplesBuffer;
|
||||
fn dummysource(length: u8) -> SamplesBuffer<f32> {
|
||||
let data: Vec<f32> = (1..=length).map(f32::from).collect();
|
||||
SamplesBuffer::new(1, 1, data)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_crossfade() {
|
||||
let source1 = dummysource(10);
|
||||
let source2 = dummysource(10);
|
||||
let mut mixed = crossfade(
|
||||
source1,
|
||||
source2,
|
||||
Duration::from_secs(5) + Duration::from_nanos(1),
|
||||
);
|
||||
assert_eq!(mixed.next(), Some(1.0));
|
||||
assert_eq!(mixed.next(), Some(2.0));
|
||||
assert_eq!(mixed.next(), Some(3.0));
|
||||
assert_eq!(mixed.next(), Some(4.0));
|
||||
assert_eq!(mixed.next(), Some(5.0));
|
||||
assert_eq!(mixed.next(), None);
|
||||
|
||||
let source1 = dummysource(10);
|
||||
let source2 = dummysource(10).amplify(0.0);
|
||||
let mut mixed = crossfade(
|
||||
source1,
|
||||
source2,
|
||||
Duration::from_secs(5) + Duration::from_nanos(1),
|
||||
);
|
||||
assert_eq!(mixed.next(), Some(1.0 * 1.0));
|
||||
assert_eq!(mixed.next(), Some(2.0 * 0.8));
|
||||
assert_eq!(mixed.next(), Some(3.0 * 0.6));
|
||||
assert_eq!(mixed.next(), Some(4.0 * 0.4));
|
||||
assert_eq!(mixed.next(), Some(5.0 * 0.2));
|
||||
assert_eq!(mixed.next(), None);
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
fn remaining_samples(until_playback: Duration, sample_rate: u32, channels: u16) -> usize {
|
||||
let ns = until_playback.as_secs() * 1_000_000_000 + until_playback.subsec_nanos() as u64;
|
||||
let samples = ns * channels as u64 * sample_rate as u64 / 1_000_000_000;
|
||||
samples as usize
|
||||
}
|
||||
|
||||
/// Internal function that builds a `Delay` object.
|
||||
pub fn delay<I>(input: I, duration: Duration) -> Delay<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
Delay {
|
||||
remaining_samples: remaining_samples(duration, input.sample_rate(), input.channels()),
|
||||
requested_duration: duration,
|
||||
input,
|
||||
}
|
||||
}
|
||||
|
||||
/// A source that delays the given source by a certain amount.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Delay<I> {
|
||||
input: I,
|
||||
remaining_samples: usize,
|
||||
requested_duration: Duration,
|
||||
}
|
||||
|
||||
impl<I> Delay<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for Delay<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = <I as Iterator>::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<<I as Iterator>::Item> {
|
||||
if self.remaining_samples >= 1 {
|
||||
self.remaining_samples -= 1;
|
||||
Some(Sample::zero_value())
|
||||
} else {
|
||||
self.input.next()
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let (min, max) = self.input.size_hint();
|
||||
(
|
||||
min + self.remaining_samples,
|
||||
max.map(|v| v + self.remaining_samples),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Source for Delay<I>
|
||||
where
|
||||
I: Iterator + Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input
|
||||
.current_frame_len()
|
||||
.map(|val| val + self.remaining_samples)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input
|
||||
.total_duration()
|
||||
.map(|val| val + self.requested_duration)
|
||||
}
|
||||
|
||||
/// Pos is seen from the perspective of the api user.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// let mut source = inner_source.delay(Duration::from_secs(10));
|
||||
/// source.try_seek(Duration::from_secs(15));
|
||||
///
|
||||
/// // inner_source is now at pos: Duration::from_secs(5);
|
||||
/// ```
|
||||
///
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
if pos < self.requested_duration {
|
||||
self.input.try_seek(Duration::ZERO)?;
|
||||
let until_playback = self.requested_duration - pos;
|
||||
self.remaining_samples =
|
||||
remaining_samples(until_playback, self.sample_rate(), self.channels());
|
||||
}
|
||||
let compensated_for_delay = pos.saturating_sub(self.requested_duration);
|
||||
self.input.try_seek(compensated_for_delay)
|
||||
}
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// When the inner source is empty this decrements an `AtomicUsize`.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Done<I> {
|
||||
input: I,
|
||||
signal: Arc<AtomicUsize>,
|
||||
signal_sent: bool,
|
||||
}
|
||||
|
||||
impl<I> Done<I> {
|
||||
#[inline]
|
||||
pub fn new(input: I, signal: Arc<AtomicUsize>) -> Done<I> {
|
||||
Done {
|
||||
input,
|
||||
signal,
|
||||
signal_sent: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Source> Iterator for Done<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
let next = self.input.next();
|
||||
if !self.signal_sent && next.is_none() {
|
||||
self.signal.fetch_sub(1, Ordering::Relaxed);
|
||||
self.signal_sent = true;
|
||||
}
|
||||
next
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Source for Done<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
use std::marker::PhantomData;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// An empty source.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct Empty<S>(PhantomData<S>);
|
||||
|
||||
impl<S> Default for Empty<S> {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Empty<S> {
|
||||
#[inline]
|
||||
pub fn new() -> Empty<S> {
|
||||
Empty(PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Iterator for Empty<S> {
|
||||
type Item = S;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<S> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Source for Empty<S>
|
||||
where
|
||||
S: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
1
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
48000
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
Some(Duration::new(0, 0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, _: Duration) -> Result<(), SeekError> {
|
||||
Err(SeekError::NotSupported {
|
||||
underlying_source: std::any::type_name::<Self>(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
use std::marker::PhantomData;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// An empty source which executes a callback function
|
||||
pub struct EmptyCallback<S> {
|
||||
pub phantom_data: PhantomData<S>,
|
||||
pub callback: Box<dyn Send + Fn()>,
|
||||
}
|
||||
|
||||
impl<S> EmptyCallback<S> {
|
||||
#[inline]
|
||||
pub fn new(callback: Box<dyn Send + Fn()>) -> EmptyCallback<S> {
|
||||
EmptyCallback {
|
||||
phantom_data: PhantomData,
|
||||
callback,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Iterator for EmptyCallback<S> {
|
||||
type Item = S;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<S> {
|
||||
(self.callback)();
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Source for EmptyCallback<S>
|
||||
where
|
||||
S: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
1
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
48000
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
Some(Duration::new(0, 0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, _: Duration) -> Result<(), SeekError> {
|
||||
Err(SeekError::NotSupported {
|
||||
underlying_source: std::any::type_name::<Self>(),
|
||||
})
|
||||
}
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Internal function that builds a `FadeIn` object.
|
||||
pub fn fadein<I>(input: I, duration: Duration) -> FadeIn<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
let duration = duration.as_secs() * 1000000000 + duration.subsec_nanos() as u64;
|
||||
|
||||
FadeIn {
|
||||
input,
|
||||
remaining_ns: duration as f32,
|
||||
total_ns: duration as f32,
|
||||
}
|
||||
}
|
||||
|
||||
/// Filter that modifies raises the volume from silence over a time period.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct FadeIn<I> {
|
||||
input: I,
|
||||
remaining_ns: f32,
|
||||
total_ns: f32,
|
||||
}
|
||||
|
||||
impl<I> FadeIn<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for FadeIn<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
if self.remaining_ns <= 0.0 {
|
||||
return self.input.next();
|
||||
}
|
||||
|
||||
let factor = 1.0 - self.remaining_ns / self.total_ns;
|
||||
self.remaining_ns -=
|
||||
1000000000.0 / (self.input.sample_rate() as f32 * self.channels() as f32);
|
||||
self.input.next().map(|value| value.amplify(factor))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> ExactSizeIterator for FadeIn<I>
|
||||
where
|
||||
I: Source + ExactSizeIterator,
|
||||
I::Item: Sample,
|
||||
{
|
||||
}
|
||||
|
||||
impl<I> Source for FadeIn<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
use crate::source::{from_iter, FromIter};
|
||||
|
||||
/// Builds a source that chains sources built from a factory.
|
||||
///
|
||||
/// The `factory` parameter is a function that produces a source. The source is then played.
|
||||
/// Whenever the source ends, `factory` is called again in order to produce the source that is
|
||||
/// played next.
|
||||
///
|
||||
/// If the `factory` closure returns `None`, then the sound ends.
|
||||
pub fn from_factory<F, S>(factory: F) -> FromIter<FromFactoryIter<F>>
|
||||
where
|
||||
F: FnMut() -> Option<S>,
|
||||
{
|
||||
from_iter(FromFactoryIter { factory })
|
||||
}
|
||||
|
||||
/// Internal type used by `from_factory`.
|
||||
pub struct FromFactoryIter<F> {
|
||||
factory: F,
|
||||
}
|
||||
|
||||
impl<F, S> Iterator for FromFactoryIter<F>
|
||||
where
|
||||
F: FnMut() -> Option<S>,
|
||||
{
|
||||
type Item = S;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<S> {
|
||||
(self.factory)()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(0, None)
|
||||
}
|
||||
}
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Builds a source that chains sources provided by an iterator.
|
||||
///
|
||||
/// The `iterator` parameter is an iterator that produces a source. The source is then played.
|
||||
/// Whenever the source ends, the `iterator` is used again in order to produce the source that is
|
||||
/// played next.
|
||||
///
|
||||
/// If the `iterator` produces `None`, then the sound ends.
|
||||
pub fn from_iter<I>(iterator: I) -> FromIter<I::IntoIter>
|
||||
where
|
||||
I: IntoIterator,
|
||||
{
|
||||
let mut iterator = iterator.into_iter();
|
||||
let first_source = iterator.next();
|
||||
|
||||
FromIter {
|
||||
iterator,
|
||||
current_source: first_source,
|
||||
}
|
||||
}
|
||||
|
||||
/// A source that chains sources provided by an iterator.
|
||||
#[derive(Clone)]
|
||||
pub struct FromIter<I>
|
||||
where
|
||||
I: Iterator,
|
||||
{
|
||||
// The iterator that provides sources.
|
||||
iterator: I,
|
||||
// Is only ever `None` if the first element of the iterator is `None`.
|
||||
current_source: Option<I::Item>,
|
||||
}
|
||||
|
||||
impl<I> Iterator for FromIter<I>
|
||||
where
|
||||
I: Iterator,
|
||||
I::Item: Iterator + Source,
|
||||
<I::Item as Iterator>::Item: Sample,
|
||||
{
|
||||
type Item = <I::Item as Iterator>::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<<I::Item as Iterator>::Item> {
|
||||
loop {
|
||||
if let Some(src) = &mut self.current_source {
|
||||
if let Some(value) = src.next() {
|
||||
return Some(value);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(src) = self.iterator.next() {
|
||||
self.current_source = Some(src);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
if let Some(cur) = &self.current_source {
|
||||
(cur.size_hint().0, None)
|
||||
} else {
|
||||
(0, None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Source for FromIter<I>
|
||||
where
|
||||
I: Iterator,
|
||||
I::Item: Iterator + Source,
|
||||
<I::Item as Iterator>::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
// This function is non-trivial because the boundary between the current source and the
|
||||
// next must be a frame boundary as well.
|
||||
//
|
||||
// The current sound is free to return `None` for `current_frame_len()`, in which case
|
||||
// we *should* return the number of samples remaining the current sound.
|
||||
// This can be estimated with `size_hint()`.
|
||||
//
|
||||
// If the `size_hint` is `None` as well, we are in the worst case scenario. To handle this
|
||||
// situation we force a frame to have a maximum number of samples indicate by this
|
||||
// constant.
|
||||
const THRESHOLD: usize = 10240;
|
||||
|
||||
// Try the current `current_frame_len`.
|
||||
if let Some(src) = &self.current_source {
|
||||
if let Some(val) = src.current_frame_len() {
|
||||
if val != 0 {
|
||||
return Some(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Try the size hint.
|
||||
if let Some(src) = &self.current_source {
|
||||
if let Some(val) = src.size_hint().1 {
|
||||
if val < THRESHOLD && val != 0 {
|
||||
return Some(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise we use the constant value.
|
||||
Some(THRESHOLD)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
if let Some(src) = &self.current_source {
|
||||
src.channels()
|
||||
} else {
|
||||
// Dummy value that only happens if the iterator was empty.
|
||||
2
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
if let Some(src) = &self.current_source {
|
||||
src.sample_rate()
|
||||
} else {
|
||||
// Dummy value that only happens if the iterator was empty.
|
||||
44100
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
if let Some(source) = self.current_source.as_mut() {
|
||||
source.try_seek(pos)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::buffer::SamplesBuffer;
|
||||
use crate::source::{from_iter, Source};
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
let mut rx = from_iter((0..2).map(|n| {
|
||||
if n == 0 {
|
||||
SamplesBuffer::new(1, 48000, vec![10i16, -10, 10, -10])
|
||||
} else if n == 1 {
|
||||
SamplesBuffer::new(2, 96000, vec![5i16, 5, 5, 5])
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(rx.channels(), 1);
|
||||
assert_eq!(rx.sample_rate(), 48000);
|
||||
assert_eq!(rx.next(), Some(10));
|
||||
assert_eq!(rx.next(), Some(-10));
|
||||
assert_eq!(rx.next(), Some(10));
|
||||
assert_eq!(rx.next(), Some(-10));
|
||||
/*assert_eq!(rx.channels(), 2);
|
||||
assert_eq!(rx.sample_rate(), 96000);*/
|
||||
// FIXME: not working
|
||||
assert_eq!(rx.next(), Some(5));
|
||||
assert_eq!(rx.next(), Some(5));
|
||||
assert_eq!(rx.next(), Some(5));
|
||||
assert_eq!(rx.next(), Some(5));
|
||||
assert_eq!(rx.next(), None);
|
||||
}
|
||||
}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
use std::cmp;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::source::uniform::UniformSourceIterator;
|
||||
use crate::source::SeekError;
|
||||
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: FromSample<I2::Item> + Sample,
|
||||
I2: Source,
|
||||
I2::Item: Sample,
|
||||
{
|
||||
let channels = input1.channels();
|
||||
let rate = input1.sample_rate();
|
||||
|
||||
Mix {
|
||||
input1: UniformSourceIterator::new(input1, channels, rate),
|
||||
input2: UniformSourceIterator::new(input2, channels, rate),
|
||||
}
|
||||
}
|
||||
|
||||
/// Filter that modifies each sample by a given value.
|
||||
#[derive(Clone)]
|
||||
pub struct Mix<I1, I2>
|
||||
where
|
||||
I1: Source,
|
||||
I1::Item: FromSample<I2::Item> + Sample,
|
||||
I2: Source,
|
||||
I2::Item: Sample,
|
||||
{
|
||||
input1: UniformSourceIterator<I1, I1::Item>,
|
||||
input2: UniformSourceIterator<I2, I2::Item>,
|
||||
}
|
||||
|
||||
impl<I1, I2> Iterator for Mix<I1, I2>
|
||||
where
|
||||
I1: Source,
|
||||
I1::Item: FromSample<I2::Item> + Sample,
|
||||
I2: Source,
|
||||
I2::Item: Sample,
|
||||
{
|
||||
type Item = I1::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I1::Item> {
|
||||
let s1 = self.input1.next();
|
||||
let s2 = self.input2.next();
|
||||
|
||||
match (s1, s2) {
|
||||
(Some(s1), Some(s2)) => Some(s1.saturating_add(CpalSample::from_sample(s2))),
|
||||
(Some(s1), None) => Some(s1),
|
||||
(None, Some(s2)) => Some(CpalSample::from_sample(s2)),
|
||||
(None, None) => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let s1 = self.input1.size_hint();
|
||||
let s2 = self.input2.size_hint();
|
||||
|
||||
let min = cmp::max(s1.0, s2.0);
|
||||
let max = match (s1.1, s2.1) {
|
||||
(Some(s1), Some(s2)) => Some(cmp::max(s1, s2)),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
(min, max)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I1, I2> ExactSizeIterator for Mix<I1, I2>
|
||||
where
|
||||
I1: Source + ExactSizeIterator,
|
||||
I1::Item: FromSample<I2::Item> + Sample,
|
||||
I2: Source + ExactSizeIterator,
|
||||
I2::Item: Sample,
|
||||
{
|
||||
}
|
||||
|
||||
impl<I1, I2> Source for Mix<I1, I2>
|
||||
where
|
||||
I1: Source,
|
||||
I1::Item: FromSample<I2::Item> + Sample,
|
||||
I2: Source,
|
||||
I2::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
let f1 = self.input1.current_frame_len();
|
||||
let f2 = self.input2.current_frame_len();
|
||||
|
||||
match (f1, f2) {
|
||||
(Some(f1), Some(f2)) => Some(cmp::min(f1, f2)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input1.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input1.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
let f1 = self.input1.total_duration();
|
||||
let f2 = self.input2.total_duration();
|
||||
|
||||
match (f1, f2) {
|
||||
(Some(f1), Some(f2)) => Some(cmp::max(f1, f2)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Will only attempt a seek if both underlying sources support seek.
|
||||
#[inline]
|
||||
fn try_seek(&mut self, _: Duration) -> Result<(), SeekError> {
|
||||
Err(SeekError::NotSupported {
|
||||
underlying_source: std::any::type_name::<Self>(),
|
||||
})
|
||||
|
||||
// uncomment when #510 is implemented (query position of playback)
|
||||
// TODO use source_intact to check if rollback makes sense
|
||||
|
||||
// let org_pos = self.input1.playback_pos();
|
||||
// self.input1.try_seek(pos)?;
|
||||
//
|
||||
// let res = self.input2.try_seek(pos);
|
||||
// if res.is_err() { // rollback seek in input1
|
||||
// self.input1.try_seek(org_pos)?;
|
||||
// }
|
||||
//
|
||||
// res
|
||||
}
|
||||
}
|
||||
+491
@@ -0,0 +1,491 @@
|
||||
//! Sources of sound and various filters.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use cpal::FromSample;
|
||||
|
||||
use crate::Sample;
|
||||
|
||||
pub use self::amplify::Amplify;
|
||||
pub use self::blt::BltFilter;
|
||||
pub use self::buffered::Buffered;
|
||||
pub use self::channel_volume::ChannelVolume;
|
||||
pub use self::crossfade::Crossfade;
|
||||
pub use self::delay::Delay;
|
||||
pub use self::done::Done;
|
||||
pub use self::empty::Empty;
|
||||
pub use self::empty_callback::EmptyCallback;
|
||||
pub use self::fadein::FadeIn;
|
||||
pub use self::from_factory::{from_factory, FromFactoryIter};
|
||||
pub use self::from_iter::{from_iter, FromIter};
|
||||
pub use self::mix::Mix;
|
||||
pub use self::pausable::Pausable;
|
||||
pub use self::periodic::PeriodicAccess;
|
||||
pub use self::position::TrackPosition;
|
||||
pub use self::repeat::Repeat;
|
||||
pub use self::samples_converter::SamplesConverter;
|
||||
pub use self::sine::SineWave;
|
||||
pub use self::skip::SkipDuration;
|
||||
pub use self::skippable::Skippable;
|
||||
pub use self::spatial::Spatial;
|
||||
pub use self::speed::Speed;
|
||||
pub use self::stoppable::Stoppable;
|
||||
pub use self::take::TakeDuration;
|
||||
pub use self::uniform::UniformSourceIterator;
|
||||
pub use self::zero::Zero;
|
||||
|
||||
mod amplify;
|
||||
mod blt;
|
||||
mod buffered;
|
||||
mod channel_volume;
|
||||
mod crossfade;
|
||||
mod delay;
|
||||
mod done;
|
||||
mod empty;
|
||||
mod empty_callback;
|
||||
mod fadein;
|
||||
mod from_factory;
|
||||
mod from_iter;
|
||||
mod mix;
|
||||
mod pausable;
|
||||
mod periodic;
|
||||
mod position;
|
||||
mod repeat;
|
||||
mod samples_converter;
|
||||
mod sine;
|
||||
mod skip;
|
||||
mod skippable;
|
||||
mod spatial;
|
||||
mod speed;
|
||||
mod stoppable;
|
||||
mod take;
|
||||
mod uniform;
|
||||
mod zero;
|
||||
|
||||
/// A source of samples.
|
||||
///
|
||||
/// # A quick lesson about sounds
|
||||
///
|
||||
/// ## Sampling
|
||||
///
|
||||
/// A sound is a vibration that propagates through air and reaches your ears. This vibration can
|
||||
/// be represented as an analog signal.
|
||||
///
|
||||
/// In order to store this signal in the computer's memory or on the disk, we perform what is
|
||||
/// called *sampling*. The consists in choosing an interval of time (for example 20µs) and reading
|
||||
/// the amplitude of the signal at each interval (for example, if the interval is 20µs we read the
|
||||
/// amplitude every 20µs). By doing so we obtain a list of numerical values, each value being
|
||||
/// called a *sample*.
|
||||
///
|
||||
/// Therefore a sound can be represented in memory by a frequency and a list of samples. The
|
||||
/// frequency is expressed in hertz and corresponds to the number of samples that have been
|
||||
/// read per second. For example if we read one sample every 20µs, the frequency would be
|
||||
/// 50000 Hz. In reality, common values for the frequency are 44100, 48000 and 96000.
|
||||
///
|
||||
/// ## Channels
|
||||
///
|
||||
/// But a frequency and a list of values only represent one signal. When you listen to a sound,
|
||||
/// your left and right ears don't receive exactly the same signal. In order to handle this,
|
||||
/// we usually record not one but two different signals: one for the left ear and one for the right
|
||||
/// ear. We say that such a sound has two *channels*.
|
||||
///
|
||||
/// Sometimes sounds even have five or six channels, each corresponding to a location around the
|
||||
/// head of the listener.
|
||||
///
|
||||
/// The standard in audio manipulation is to *interleave* the multiple channels. In other words,
|
||||
/// in a sound with two channels the list of samples contains the first sample of the first
|
||||
/// channel, then the first sample of the second channel, then the second sample of the first
|
||||
/// channel, then the second sample of the second channel, and so on. The same applies if you have
|
||||
/// more than two channels. The rodio library only supports this schema.
|
||||
///
|
||||
/// Therefore in order to represent a sound in memory in fact we need three characteristics: the
|
||||
/// frequency, the number of channels, and the list of samples.
|
||||
///
|
||||
/// ## The `Source` trait
|
||||
///
|
||||
/// A Rust object that represents a sound should implement the `Source` trait.
|
||||
///
|
||||
/// The three characteristics that describe a sound are provided through this trait:
|
||||
///
|
||||
/// - The number of channels can be retrieved with `channels`.
|
||||
/// - The frequency can be retrieved with `sample_rate`.
|
||||
/// - The list of values can be retrieved by iterating on the source. The `Source` trait requires
|
||||
/// that the `Iterator` trait be implemented as well.
|
||||
///
|
||||
/// # Frames
|
||||
///
|
||||
/// The samples rate and number of channels of some sound sources can change by itself from time
|
||||
/// to time.
|
||||
///
|
||||
/// > **Note**: As a basic example, if you play two audio files one after the other and treat the
|
||||
/// > whole as a single source, then the channels and samples rate of that source may change at the
|
||||
/// > transition between the two files.
|
||||
///
|
||||
/// However, for optimization purposes rodio supposes that the number of channels and the frequency
|
||||
/// stay the same for long periods of time and avoids calling `channels()` and
|
||||
/// `sample_rate` too frequently.
|
||||
///
|
||||
/// In order to properly handle this situation, the `current_frame_len()` method should return
|
||||
/// the number of samples that remain in the iterator before the samples rate and number of
|
||||
/// channels can potentially change.
|
||||
///
|
||||
pub trait Source: Iterator
|
||||
where
|
||||
Self::Item: Sample,
|
||||
{
|
||||
/// Returns the number of samples before the current frame ends. `None` means "infinite" or
|
||||
/// "until the sound ends".
|
||||
/// Should never return 0 unless there's no more data.
|
||||
///
|
||||
/// After the engine has finished reading the specified number of samples, it will check
|
||||
/// whether the value of `channels()` and/or `sample_rate()` have changed.
|
||||
fn current_frame_len(&self) -> Option<usize>;
|
||||
|
||||
/// Returns the number of channels. Channels are always interleaved.
|
||||
fn channels(&self) -> u16;
|
||||
|
||||
/// Returns the rate at which the source should be played. In number of samples per second.
|
||||
fn sample_rate(&self) -> u32;
|
||||
|
||||
/// Returns the total duration of this source, if known.
|
||||
///
|
||||
/// `None` indicates at the same time "infinite" or "unknown".
|
||||
fn total_duration(&self) -> Option<Duration>;
|
||||
|
||||
/// Stores the source in a buffer in addition to returning it. This iterator can be cloned.
|
||||
|
||||
#[inline]
|
||||
fn buffered(self) -> Buffered<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
buffered::buffered(self)
|
||||
}
|
||||
|
||||
/// Mixes this source with another one.
|
||||
#[inline]
|
||||
fn mix<S>(self, other: S) -> Mix<Self, S>
|
||||
where
|
||||
Self: Sized,
|
||||
Self::Item: FromSample<S::Item>,
|
||||
S: Source,
|
||||
S::Item: Sample,
|
||||
{
|
||||
mix::mix(self, other)
|
||||
}
|
||||
|
||||
/// Repeats this source forever.
|
||||
///
|
||||
/// Note that this works by storing the data in a buffer, so the amount of memory used is
|
||||
/// proportional to the size of the sound.
|
||||
#[inline]
|
||||
fn repeat_infinite(self) -> Repeat<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
repeat::repeat(self)
|
||||
}
|
||||
|
||||
/// Takes a certain duration of this source and then stops.
|
||||
#[inline]
|
||||
fn take_duration(self, duration: Duration) -> TakeDuration<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
take::take_duration(self, duration)
|
||||
}
|
||||
|
||||
/// Delays the sound by a certain duration.
|
||||
///
|
||||
/// The rate and channels of the silence will use the same format as the first frame of the
|
||||
/// source.
|
||||
#[inline]
|
||||
fn delay(self, duration: Duration) -> Delay<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
delay::delay(self, duration)
|
||||
}
|
||||
|
||||
/// Immediately skips a certain duration of this source.
|
||||
///
|
||||
/// If the specified duration is longer than the source itself, `skip_duration` will skip to the end of the source.
|
||||
#[inline]
|
||||
fn skip_duration(self, duration: Duration) -> SkipDuration<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
skip::skip_duration(self, duration)
|
||||
}
|
||||
|
||||
/// Amplifies the sound by the given value.
|
||||
#[inline]
|
||||
fn amplify(self, value: f32) -> Amplify<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
amplify::amplify(self, value)
|
||||
}
|
||||
|
||||
/// Mixes this sound fading out with another sound fading in for the given duration.
|
||||
///
|
||||
/// Only the crossfaded portion (beginning of self, beginning of other) is returned.
|
||||
#[inline]
|
||||
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)
|
||||
}
|
||||
|
||||
/// Fades in the sound.
|
||||
#[inline]
|
||||
fn fade_in(self, duration: Duration) -> FadeIn<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
fadein::fadein(self, duration)
|
||||
}
|
||||
|
||||
/// Calls the `access` closure on `Self` the first time the source is iterated and every
|
||||
/// time `period` elapses.
|
||||
///
|
||||
/// Later changes in either `sample_rate()` or `channels_count()` won't be reflected in
|
||||
/// the rate of access.
|
||||
///
|
||||
/// The rate is based on playback speed, so both the following will call `access` when the
|
||||
/// same samples are reached:
|
||||
/// `periodic_access(Duration::from_secs(1), ...).speed(2.0)`
|
||||
/// `speed(2.0).periodic_access(Duration::from_secs(2), ...)`
|
||||
#[inline]
|
||||
fn periodic_access<F>(self, period: Duration, access: F) -> PeriodicAccess<Self, F>
|
||||
where
|
||||
Self: Sized,
|
||||
F: FnMut(&mut Self),
|
||||
{
|
||||
periodic::periodic(self, period, access)
|
||||
}
|
||||
|
||||
/// Changes the play speed of the sound. Does not adjust the samples, only the play speed.
|
||||
#[inline]
|
||||
fn speed(self, ratio: f32) -> Speed<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
speed::speed(self, ratio)
|
||||
}
|
||||
|
||||
/// Adds a basic reverb effect.
|
||||
///
|
||||
/// This function requires the source to implement `Clone`. This can be done by using
|
||||
/// `buffered()`.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// let source = source.buffered().reverb(Duration::from_millis(100), 0.7);
|
||||
/// ```
|
||||
#[inline]
|
||||
fn reverb(self, duration: Duration, amplitude: f32) -> Mix<Self, Delay<Amplify<Self>>>
|
||||
where
|
||||
Self: Sized + Clone,
|
||||
{
|
||||
let echo = self.clone().amplify(amplitude).delay(duration);
|
||||
self.mix(echo)
|
||||
}
|
||||
|
||||
/// Converts the samples of this source to another type.
|
||||
#[inline]
|
||||
fn convert_samples<D>(self) -> SamplesConverter<Self, D>
|
||||
where
|
||||
Self: Sized,
|
||||
D: Sample,
|
||||
{
|
||||
SamplesConverter::new(self)
|
||||
}
|
||||
|
||||
/// Makes the sound pausable.
|
||||
// TODO: add example
|
||||
#[inline]
|
||||
fn pausable(self, initially_paused: bool) -> Pausable<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
pausable::pausable(self, initially_paused)
|
||||
}
|
||||
|
||||
/// Makes the sound stoppable.
|
||||
// TODO: add example
|
||||
#[inline]
|
||||
fn stoppable(self) -> Stoppable<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
stoppable::stoppable(self)
|
||||
}
|
||||
|
||||
fn skippable(self) -> Skippable<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
skippable::skippable(self)
|
||||
}
|
||||
|
||||
/// Start tracking the elapsed duration since the start of the underlying
|
||||
/// source.
|
||||
///
|
||||
/// If a speedup and or delay is applied after this that will not be reflected
|
||||
/// in the position returned by [`get_pos`](TrackPosition::get_pos).
|
||||
///
|
||||
/// This can get confusing when using [`get_pos()`](TrackPosition::get_pos)
|
||||
/// together with [`Source::try_seek()`] as the the latter does take all
|
||||
/// speedup's and delay's into account. Its recommended therefore to apply
|
||||
/// track_position after speedup's and delay's.
|
||||
fn track_position(self) -> TrackPosition<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
position::track_position(self)
|
||||
}
|
||||
|
||||
/// Applies a low-pass filter to the source.
|
||||
/// **Warning**: Probably buggy.
|
||||
#[inline]
|
||||
fn low_pass(self, freq: u32) -> BltFilter<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
Self: Source<Item = f32>,
|
||||
{
|
||||
blt::low_pass(self, freq)
|
||||
}
|
||||
|
||||
/// Applies a high-pass filter to the source.
|
||||
#[inline]
|
||||
fn high_pass(self, freq: u32) -> BltFilter<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
Self: Source<Item = f32>,
|
||||
{
|
||||
blt::high_pass(self, freq)
|
||||
}
|
||||
|
||||
/// Applies a low-pass filter to the source while allowing the q (bandwidth) to be changed.
|
||||
#[inline]
|
||||
fn low_pass_with_q(self, freq: u32, q: f32) -> BltFilter<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
Self: Source<Item = f32>,
|
||||
{
|
||||
blt::low_pass_with_q(self, freq, q)
|
||||
}
|
||||
|
||||
/// Applies a high-pass filter to the source while allowing the q (bandwidth) to be changed.
|
||||
#[inline]
|
||||
fn high_pass_with_q(self, freq: u32, q: f32) -> BltFilter<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
Self: Source<Item = f32>,
|
||||
{
|
||||
blt::high_pass_with_q(self, freq, q)
|
||||
}
|
||||
|
||||
// There is no `can_seek()` method as it is impossible to use correctly. Between
|
||||
// checking if a source supports seeking and actually seeking the sink can
|
||||
// switch to a new source.
|
||||
|
||||
/// Attempts to seek to a given position in the current source.
|
||||
///
|
||||
/// As long as the duration of the source is known seek is guaranteed to saturate
|
||||
/// at the end of the source. For example given a source that reports a total duration
|
||||
/// of 42 seconds calling `try_seek()` with 60 seconds as argument will seek to
|
||||
/// 42 seconds.
|
||||
///
|
||||
/// # Errors
|
||||
/// This function will return [`SeekError::NotSupported`] if one of the underlying
|
||||
/// sources does not support seeking.
|
||||
///
|
||||
/// It will return an error if an implementation ran
|
||||
/// into one during the seek.
|
||||
///
|
||||
/// Seeking beyond the end of a source might return an error if the total duration of
|
||||
/// the source is not known.
|
||||
#[allow(unused_variables)]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
Err(SeekError::NotSupported {
|
||||
underlying_source: std::any::type_name::<Self>(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// We might add decoders requiring new error types, without non_exhaustive
|
||||
// this would break users builds
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum SeekError {
|
||||
#[error("Streaming is not supported by source: {underlying_source}")]
|
||||
NotSupported { underlying_source: &'static str },
|
||||
#[cfg(feature = "symphonia")]
|
||||
#[error("Error seeking: {0}")]
|
||||
SymphoniaDecoder(#[from] crate::decoder::symphonia::SeekError),
|
||||
#[cfg(feature = "wav")]
|
||||
#[error("Error seeking in wav source: {0}")]
|
||||
HoundDecoder(std::io::Error),
|
||||
#[error("An error occurred")]
|
||||
Other(Box<dyn std::error::Error + Send>),
|
||||
}
|
||||
|
||||
impl SeekError {
|
||||
pub fn source_intact(&self) -> bool {
|
||||
match self {
|
||||
SeekError::NotSupported { .. } => true,
|
||||
#[cfg(feature = "symphonia")]
|
||||
SeekError::SymphoniaDecoder(_) => false,
|
||||
#[cfg(feature = "wav")]
|
||||
SeekError::HoundDecoder(_) => false,
|
||||
SeekError::Other(_) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! source_pointer_impl {
|
||||
($($sig:tt)+) => {
|
||||
impl $($sig)+ {
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
(**self).current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
(**self).channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
(**self).sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
(**self).total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
(**self).try_seek(pos)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
source_pointer_impl!(<S> Source for Box<dyn Source<Item = S>> where S: Sample,);
|
||||
|
||||
source_pointer_impl!(<S> Source for Box<dyn Source<Item = S> + Send> where S: Sample,);
|
||||
|
||||
source_pointer_impl!(<S> Source for Box<dyn Source<Item = S> + Send + Sync> where S: Sample,);
|
||||
|
||||
source_pointer_impl!(<'a, S, C> Source for &'a mut C where S: Sample, C: Source<Item = S>,);
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Internal function that builds a `Pausable` object.
|
||||
pub fn pausable<I>(source: I, paused: bool) -> Pausable<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
let paused_channels = if paused {
|
||||
Some(source.channels())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Pausable {
|
||||
input: source,
|
||||
paused_channels,
|
||||
remaining_paused_samples: 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Pausable<I> {
|
||||
input: I,
|
||||
paused_channels: Option<u16>,
|
||||
remaining_paused_samples: u16,
|
||||
}
|
||||
|
||||
impl<I> Pausable<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
/// Sets whether the filter applies.
|
||||
///
|
||||
/// If set to true, the inner sound stops playing and no samples are processed from it.
|
||||
#[inline]
|
||||
pub fn set_paused(&mut self, paused: bool) {
|
||||
match (self.paused_channels, paused) {
|
||||
(None, true) => self.paused_channels = Some(self.input.channels()),
|
||||
(Some(_), false) => self.paused_channels = None,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for Pausable<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
if self.remaining_paused_samples > 0 {
|
||||
self.remaining_paused_samples -= 1;
|
||||
return Some(I::Item::zero_value());
|
||||
}
|
||||
|
||||
if let Some(paused_channels) = self.paused_channels {
|
||||
self.remaining_paused_samples = paused_channels - 1;
|
||||
return Some(I::Item::zero_value());
|
||||
}
|
||||
|
||||
self.input.next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Source for Pausable<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
}
|
||||
}
|
||||
+174
@@ -0,0 +1,174 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Internal function that builds a `PeriodicAccess` object.
|
||||
pub fn periodic<I, F>(source: I, period: Duration, modifier: F) -> PeriodicAccess<I, F>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
// TODO: handle the fact that the samples rate can change
|
||||
// TODO: generally, just wrong
|
||||
let update_ms = period.as_secs() as u32 * 1_000 + period.subsec_millis();
|
||||
let update_frequency = (update_ms * source.sample_rate()) / 1000 * source.channels() as u32;
|
||||
|
||||
PeriodicAccess {
|
||||
input: source,
|
||||
modifier,
|
||||
// Can overflow when subtracting if this is 0
|
||||
update_frequency: if update_frequency == 0 {
|
||||
1
|
||||
} else {
|
||||
update_frequency
|
||||
},
|
||||
samples_until_update: 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls a function on a source every time a period elapsed.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PeriodicAccess<I, F> {
|
||||
// The inner source.
|
||||
input: I,
|
||||
|
||||
// Closure that gets access to `inner`.
|
||||
modifier: F,
|
||||
|
||||
// The frequency with which local_volume should be updated by remote_volume
|
||||
update_frequency: u32,
|
||||
|
||||
// How many samples remain until it is time to update local_volume with remote_volume.
|
||||
samples_until_update: u32,
|
||||
}
|
||||
|
||||
impl<I, F> PeriodicAccess<I, F>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
F: FnMut(&mut I),
|
||||
{
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, F> Iterator for PeriodicAccess<I, F>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
F: FnMut(&mut I),
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
self.samples_until_update -= 1;
|
||||
if self.samples_until_update == 0 {
|
||||
(self.modifier)(&mut self.input);
|
||||
self.samples_until_update = self.update_frequency;
|
||||
}
|
||||
|
||||
self.input.next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, F> Source for PeriodicAccess<I, F>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
F: FnMut(&mut I),
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::cell::RefCell;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::buffer::SamplesBuffer;
|
||||
use crate::source::Source;
|
||||
|
||||
#[test]
|
||||
fn stereo_access() {
|
||||
// Stereo, 1Hz audio buffer
|
||||
let inner = SamplesBuffer::new(2, 1, vec![10i16, -10, 10, -10, 20, -20]);
|
||||
|
||||
let cnt = RefCell::new(0);
|
||||
|
||||
let mut source = inner.periodic_access(Duration::from_millis(1000), |_src| {
|
||||
*cnt.borrow_mut() += 1;
|
||||
});
|
||||
|
||||
assert_eq!(*cnt.borrow(), 0);
|
||||
// Always called on first access!
|
||||
assert_eq!(source.next(), Some(10));
|
||||
assert_eq!(*cnt.borrow(), 1);
|
||||
// Called every 1 second afterwards
|
||||
assert_eq!(source.next(), Some(-10));
|
||||
assert_eq!(*cnt.borrow(), 1);
|
||||
assert_eq!(source.next(), Some(10));
|
||||
assert_eq!(*cnt.borrow(), 2);
|
||||
assert_eq!(source.next(), Some(-10));
|
||||
assert_eq!(*cnt.borrow(), 2);
|
||||
assert_eq!(source.next(), Some(20));
|
||||
assert_eq!(*cnt.borrow(), 3);
|
||||
assert_eq!(source.next(), Some(-20));
|
||||
assert_eq!(*cnt.borrow(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fast_access_overflow() {
|
||||
// 1hz is lower than 0.5 samples per 5ms
|
||||
let inner = SamplesBuffer::new(1, 1, vec![10i16, -10, 10, -10, 20, -20]);
|
||||
let mut source = inner.periodic_access(Duration::from_millis(5), |_src| {});
|
||||
|
||||
source.next();
|
||||
source.next(); // Would overflow here.
|
||||
}
|
||||
}
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Internal function that builds a `TrackPosition` object. See trait docs for
|
||||
/// details
|
||||
pub fn track_position<I>(source: I) -> TrackPosition<I> {
|
||||
TrackPosition {
|
||||
input: source,
|
||||
samples_counted: 0,
|
||||
offset_duration: 0.0,
|
||||
current_frame_sample_rate: 0,
|
||||
current_frame_channels: 0,
|
||||
current_frame_len: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TrackPosition<I> {
|
||||
input: I,
|
||||
samples_counted: usize,
|
||||
offset_duration: f64,
|
||||
current_frame_sample_rate: u32,
|
||||
current_frame_channels: u16,
|
||||
current_frame_len: Option<usize>,
|
||||
}
|
||||
|
||||
impl<I> TrackPosition<I> {
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> TrackPosition<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
/// Returns the position of the underlying source relative to its start.
|
||||
///
|
||||
/// If a speedup and or delay is applied after applying a
|
||||
/// [`Source::track_position`] it will not be reflected in the position
|
||||
/// returned by [`get_pos`](TrackPosition::get_pos).
|
||||
///
|
||||
/// This can get confusing when using [`get_pos()`](TrackPosition::get_pos)
|
||||
/// together with [`Source::try_seek()`] as the the latter does take all
|
||||
/// speedup's and delay's into account. Its recommended therefore to apply
|
||||
/// track_position after speedup's and delay's.
|
||||
#[inline]
|
||||
pub fn get_pos(&self) -> Duration {
|
||||
let seconds = self.samples_counted as f64
|
||||
/ self.input.sample_rate() as f64
|
||||
/ self.input.channels() as f64
|
||||
+ self.offset_duration;
|
||||
Duration::from_secs_f64(seconds)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_current_frame(&mut self) {
|
||||
self.current_frame_len = self.current_frame_len();
|
||||
self.current_frame_sample_rate = self.sample_rate();
|
||||
self.current_frame_channels = self.channels();
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for TrackPosition<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
// This should only be executed once at the first call to next.
|
||||
if self.current_frame_len.is_none() {
|
||||
self.set_current_frame();
|
||||
}
|
||||
|
||||
let item = self.input.next();
|
||||
if item.is_some() {
|
||||
self.samples_counted += 1;
|
||||
|
||||
// At the end of a frame add the duration of this frame to
|
||||
// offset_duration and start collecting samples again.
|
||||
if Some(self.samples_counted) == self.current_frame_len() {
|
||||
self.offset_duration += self.samples_counted as f64
|
||||
/ self.current_frame_sample_rate as f64
|
||||
/ self.current_frame_channels as f64;
|
||||
|
||||
// Reset.
|
||||
self.samples_counted = 0;
|
||||
self.set_current_frame();
|
||||
};
|
||||
};
|
||||
item
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Source for TrackPosition<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
let result = self.input.try_seek(pos);
|
||||
if result.is_ok() {
|
||||
self.offset_duration = pos.as_secs_f64();
|
||||
// This assumes that the seek implementation of the codec always
|
||||
// starts again at the beginning of a frame. Which is the case with
|
||||
// symphonia.
|
||||
self.samples_counted = 0;
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::buffer::SamplesBuffer;
|
||||
use crate::source::Source;
|
||||
|
||||
#[test]
|
||||
fn test_position() {
|
||||
let inner = SamplesBuffer::new(1, 1, vec![10i16, -10, 10, -10, 20, -20]);
|
||||
let mut source = inner.track_position();
|
||||
|
||||
assert_eq!(source.get_pos().as_secs_f32(), 0.0);
|
||||
source.next();
|
||||
assert_eq!(source.get_pos().as_secs_f32(), 1.0);
|
||||
|
||||
source.next();
|
||||
assert_eq!(source.get_pos().as_secs_f32(), 2.0);
|
||||
|
||||
assert_eq!(source.try_seek(Duration::new(1, 0)).is_ok(), true);
|
||||
assert_eq!(source.get_pos().as_secs_f32(), 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_in_presence_of_speedup() {
|
||||
let inner = SamplesBuffer::new(1, 1, vec![10i16, -10, 10, -10, 20, -20]);
|
||||
let mut source = inner.speed(2.0).track_position();
|
||||
|
||||
assert_eq!(source.get_pos().as_secs_f32(), 0.0);
|
||||
source.next();
|
||||
assert_eq!(source.get_pos().as_secs_f32(), 0.5);
|
||||
|
||||
source.next();
|
||||
assert_eq!(source.get_pos().as_secs_f32(), 1.0);
|
||||
|
||||
assert_eq!(source.try_seek(Duration::new(1, 0)).is_ok(), true);
|
||||
assert_eq!(source.get_pos().as_secs_f32(), 1.0);
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::source::buffered::Buffered;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Internal function that builds a `Repeat` object.
|
||||
pub fn repeat<I>(input: I) -> Repeat<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
let input = input.buffered();
|
||||
Repeat {
|
||||
inner: input.clone(),
|
||||
next: input,
|
||||
}
|
||||
}
|
||||
|
||||
/// A source that repeats the given source.
|
||||
pub struct Repeat<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
inner: Buffered<I>,
|
||||
next: Buffered<I>,
|
||||
}
|
||||
|
||||
impl<I> Iterator for Repeat<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = <I as Iterator>::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<<I as Iterator>::Item> {
|
||||
if let Some(value) = self.inner.next() {
|
||||
return Some(value);
|
||||
}
|
||||
|
||||
self.inner = self.next.clone();
|
||||
self.inner.next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
// infinite
|
||||
(0, None)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Source for Repeat<I>
|
||||
where
|
||||
I: Iterator + Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
match self.inner.current_frame_len() {
|
||||
Some(0) => self.next.current_frame_len(),
|
||||
a => a,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
match self.inner.current_frame_len() {
|
||||
Some(0) => self.next.channels(),
|
||||
_ => self.inner.channels(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
match self.inner.current_frame_len() {
|
||||
Some(0) => self.next.sample_rate(),
|
||||
_ => self.inner.sample_rate(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.inner.try_seek(pos)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Clone for Repeat<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn clone(&self) -> Repeat<I> {
|
||||
Repeat {
|
||||
inner: self.inner.clone(),
|
||||
next: self.next.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
use std::marker::PhantomData;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
use cpal::{FromSample, Sample as CpalSample};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// An iterator that reads from a `Source` and converts the samples to a specific rate and
|
||||
/// channels count.
|
||||
///
|
||||
/// It implements `Source` as well, but all the data is guaranteed to be in a single frame whose
|
||||
/// channels and samples rate have been passed to `new`.
|
||||
#[derive(Clone)]
|
||||
pub struct SamplesConverter<I, D> {
|
||||
inner: I,
|
||||
dest: PhantomData<D>,
|
||||
}
|
||||
|
||||
impl<I, D> SamplesConverter<I, D> {
|
||||
#[inline]
|
||||
pub fn new(input: I) -> SamplesConverter<I, D> {
|
||||
SamplesConverter {
|
||||
inner: input,
|
||||
dest: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.inner
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.inner
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, D> Iterator for SamplesConverter<I, D>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
D: FromSample<I::Item> + Sample,
|
||||
{
|
||||
type Item = D;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<D> {
|
||||
self.inner.next().map(|s| CpalSample::from_sample(s))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.inner.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, D> ExactSizeIterator for SamplesConverter<I, D>
|
||||
where
|
||||
I: Source + ExactSizeIterator,
|
||||
I::Item: Sample,
|
||||
D: FromSample<I::Item> + Sample,
|
||||
{
|
||||
}
|
||||
|
||||
impl<I, D> Source for SamplesConverter<I, D>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
D: FromSample<I::Item> + Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.inner.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.inner.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.inner.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.inner.total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.inner.try_seek(pos)
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
use std::f32::consts::PI;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::Source;
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// An infinite source that produces a sine.
|
||||
///
|
||||
/// Always has a rate of 48kHz and one channel.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SineWave {
|
||||
freq: f32,
|
||||
num_sample: usize,
|
||||
}
|
||||
|
||||
impl SineWave {
|
||||
/// The frequency of the sine.
|
||||
#[inline]
|
||||
pub fn new(freq: f32) -> SineWave {
|
||||
SineWave {
|
||||
freq,
|
||||
num_sample: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for SineWave {
|
||||
type Item = f32;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<f32> {
|
||||
self.num_sample = self.num_sample.wrapping_add(1);
|
||||
|
||||
let value = 2.0 * PI * self.freq * self.num_sample as f32 / 48000.0;
|
||||
Some(value.sin())
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for SineWave {
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
1
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
48000
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, _: Duration) -> Result<(), SeekError> {
|
||||
// This is a constant sound, normal seeking would not have any effect.
|
||||
// While changing the phase of the sine wave could change how it sounds in
|
||||
// combination with another sound (beating) such precision is not the intend
|
||||
// of seeking
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
+259
@@ -0,0 +1,259 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
const NS_PER_SECOND: u128 = 1_000_000_000;
|
||||
|
||||
/// Internal function that builds a `SkipDuration` object.
|
||||
pub fn skip_duration<I>(mut input: I, duration: Duration) -> SkipDuration<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
do_skip_duration(&mut input, duration);
|
||||
SkipDuration {
|
||||
input,
|
||||
skipped_duration: duration,
|
||||
}
|
||||
}
|
||||
|
||||
/// Skips specified `duration` of the given `input` source from it's current position.
|
||||
fn do_skip_duration<I>(input: &mut I, mut duration: Duration)
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
while duration > Duration::new(0, 0) {
|
||||
if input.current_frame_len().is_none() {
|
||||
// Sample rate and the amount of channels will be the same till the end.
|
||||
do_skip_duration_unchecked(input, duration);
|
||||
return;
|
||||
}
|
||||
|
||||
// .unwrap() safety: if `current_frame_len()` is None, the body of the `if` statement
|
||||
// above returns before we get here.
|
||||
let frame_len: usize = input.current_frame_len().unwrap();
|
||||
// If frame_len is zero, then there is no more data to skip. Instead
|
||||
// just bail out.
|
||||
if frame_len == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
let ns_per_sample: u128 =
|
||||
NS_PER_SECOND / input.sample_rate() as u128 / input.channels() as u128;
|
||||
|
||||
// Check if we need to skip only part of the current frame.
|
||||
if frame_len as u128 * ns_per_sample > duration.as_nanos() {
|
||||
skip_samples(input, (duration.as_nanos() / ns_per_sample) as usize);
|
||||
return;
|
||||
}
|
||||
|
||||
skip_samples(input, frame_len);
|
||||
|
||||
duration -= Duration::from_nanos((frame_len * ns_per_sample as usize) as u64);
|
||||
}
|
||||
}
|
||||
|
||||
/// Skips specified `duration` from the `input` source assuming that sample rate
|
||||
/// and amount of channels are not changing.
|
||||
fn do_skip_duration_unchecked<I>(input: &mut I, duration: Duration)
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
let samples_per_channel: u128 =
|
||||
duration.as_nanos() * input.sample_rate() as u128 / NS_PER_SECOND;
|
||||
let samples_to_skip: u128 = samples_per_channel * input.channels() as u128;
|
||||
|
||||
skip_samples(input, samples_to_skip as usize);
|
||||
}
|
||||
|
||||
/// Skips `n` samples from the given `input` source.
|
||||
fn skip_samples<I>(input: &mut I, n: usize)
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
for _ in 0..n {
|
||||
if input.next().is_none() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A source that skips specified duration of the given source from it's current position.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SkipDuration<I> {
|
||||
input: I,
|
||||
skipped_duration: Duration,
|
||||
}
|
||||
|
||||
impl<I> SkipDuration<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for SkipDuration<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = <I as Iterator>::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.input.next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Source for SkipDuration<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration().map(|val| {
|
||||
val.checked_sub(self.skipped_duration)
|
||||
.unwrap_or_else(|| Duration::from_secs(0))
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::buffer::SamplesBuffer;
|
||||
use crate::source::Source;
|
||||
|
||||
fn test_skip_duration_samples_left(
|
||||
channels: u16,
|
||||
sample_rate: u32,
|
||||
seconds: u32,
|
||||
seconds_to_skip: u32,
|
||||
) {
|
||||
let data: Vec<f32> = (1..=(sample_rate * channels as u32 * seconds))
|
||||
.map(|_| 0f32)
|
||||
.collect();
|
||||
let test_buffer = SamplesBuffer::new(channels, sample_rate, data);
|
||||
let seconds_left = seconds.saturating_sub(seconds_to_skip);
|
||||
|
||||
let samples_left_expected = (sample_rate * channels as u32 * seconds_left) as usize;
|
||||
let samples_left = test_buffer
|
||||
.skip_duration(Duration::from_secs(seconds_to_skip as u64))
|
||||
.count();
|
||||
|
||||
assert_eq!(samples_left, samples_left_expected);
|
||||
}
|
||||
|
||||
macro_rules! skip_duration_test_block {
|
||||
($(channels: $ch:expr, sample rate: $sr:expr, seconds: $sec:expr, seconds to skip: $sec_to_skip:expr;)+) => {
|
||||
$(
|
||||
test_skip_duration_samples_left($ch, $sr, $sec, $sec_to_skip);
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skip_duration_shorter_than_source() {
|
||||
skip_duration_test_block! {
|
||||
channels: 1, sample rate: 44100, seconds: 5, seconds to skip: 3;
|
||||
channels: 1, sample rate: 96000, seconds: 5, seconds to skip: 3;
|
||||
|
||||
channels: 2, sample rate: 44100, seconds: 5, seconds to skip: 3;
|
||||
channels: 2, sample rate: 96000, seconds: 5, seconds to skip: 3;
|
||||
|
||||
channels: 4, sample rate: 44100, seconds: 5, seconds to skip: 3;
|
||||
channels: 4, sample rate: 96000, seconds: 5, seconds to skip: 3;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skip_duration_zero_duration() {
|
||||
skip_duration_test_block! {
|
||||
channels: 1, sample rate: 44100, seconds: 5, seconds to skip: 0;
|
||||
channels: 1, sample rate: 96000, seconds: 5, seconds to skip: 0;
|
||||
|
||||
channels: 2, sample rate: 44100, seconds: 5, seconds to skip: 0;
|
||||
channels: 2, sample rate: 96000, seconds: 5, seconds to skip: 0;
|
||||
|
||||
channels: 4, sample rate: 44100, seconds: 5, seconds to skip: 0;
|
||||
channels: 4, sample rate: 96000, seconds: 5, seconds to skip: 0;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skip_duration_longer_than_source() {
|
||||
skip_duration_test_block! {
|
||||
channels: 1, sample rate: 44100, seconds: 1, seconds to skip: 5;
|
||||
channels: 1, sample rate: 96000, seconds: 10, seconds to skip: 11;
|
||||
|
||||
channels: 2, sample rate: 44100, seconds: 1, seconds to skip: 5;
|
||||
channels: 2, sample rate: 96000, seconds: 10, seconds to skip: 11;
|
||||
|
||||
channels: 4, sample rate: 44100, seconds: 1, seconds to skip: 5;
|
||||
channels: 4, sample rate: 96000, seconds: 10, seconds to skip: 11;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skip_duration_equal_to_source_length() {
|
||||
skip_duration_test_block! {
|
||||
channels: 1, sample rate: 44100, seconds: 1, seconds to skip: 1;
|
||||
channels: 1, sample rate: 96000, seconds: 10, seconds to skip: 10;
|
||||
|
||||
channels: 2, sample rate: 44100, seconds: 1, seconds to skip: 1;
|
||||
channels: 2, sample rate: 96000, seconds: 10, seconds to skip: 10;
|
||||
|
||||
channels: 4, sample rate: 44100, seconds: 1, seconds to skip: 1;
|
||||
channels: 4, sample rate: 96000, seconds: 10, seconds to skip: 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::Sample;
|
||||
use crate::Source;
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Internal function that builds a `Skippable` object.
|
||||
pub fn skippable<I>(source: I) -> Skippable<I> {
|
||||
Skippable {
|
||||
input: source,
|
||||
do_skip: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Skippable<I> {
|
||||
input: I,
|
||||
do_skip: bool,
|
||||
}
|
||||
|
||||
impl<I> Skippable<I> {
|
||||
/// Skips the current source
|
||||
#[inline]
|
||||
pub fn skip(&mut self) {
|
||||
self.do_skip = true;
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for Skippable<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
if self.do_skip {
|
||||
None
|
||||
} else {
|
||||
self.input.next()
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Source for Skippable<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::source::ChannelVolume;
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Combines channels in input into a single mono source, then plays that mono sound
|
||||
/// to each channel at the volume given for that channel.
|
||||
#[derive(Clone)]
|
||||
pub struct Spatial<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
input: ChannelVolume<I>,
|
||||
}
|
||||
|
||||
fn dist_sq(a: [f32; 3], b: [f32; 3]) -> f32 {
|
||||
a.iter()
|
||||
.zip(b.iter())
|
||||
.map(|(a, b)| (a - b) * (a - b))
|
||||
.sum::<f32>()
|
||||
}
|
||||
|
||||
impl<I> Spatial<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
pub fn new(
|
||||
input: I,
|
||||
emitter_position: [f32; 3],
|
||||
left_ear: [f32; 3],
|
||||
right_ear: [f32; 3],
|
||||
) -> Spatial<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
let mut ret = Spatial {
|
||||
input: ChannelVolume::new(input, vec![0.0, 0.0]),
|
||||
};
|
||||
ret.set_positions(emitter_position, left_ear, right_ear);
|
||||
ret
|
||||
}
|
||||
|
||||
/// Sets the position of the emitter and ears in the 3D world.
|
||||
pub fn set_positions(
|
||||
&mut self,
|
||||
emitter_pos: [f32; 3],
|
||||
left_ear: [f32; 3],
|
||||
right_ear: [f32; 3],
|
||||
) {
|
||||
debug_assert!(left_ear != right_ear);
|
||||
let left_dist_sq = dist_sq(left_ear, emitter_pos);
|
||||
let right_dist_sq = dist_sq(right_ear, emitter_pos);
|
||||
let max_diff = dist_sq(left_ear, right_ear).sqrt();
|
||||
let left_dist = left_dist_sq.sqrt();
|
||||
let right_dist = right_dist_sq.sqrt();
|
||||
let left_diff_modifier = (((left_dist - right_dist) / max_diff + 1.0) / 4.0 + 0.5).min(1.0);
|
||||
let right_diff_modifier =
|
||||
(((right_dist - left_dist) / max_diff + 1.0) / 4.0 + 0.5).min(1.0);
|
||||
let left_dist_modifier = (1.0 / left_dist_sq).min(1.0);
|
||||
let right_dist_modifier = (1.0 / right_dist_sq).min(1.0);
|
||||
self.input
|
||||
.set_volume(0, left_diff_modifier * left_dist_modifier);
|
||||
self.input
|
||||
.set_volume(1, right_diff_modifier * right_dist_modifier);
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for Spatial<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
self.input.next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> ExactSizeIterator for Spatial<I>
|
||||
where
|
||||
I: Source + ExactSizeIterator,
|
||||
I::Item: Sample,
|
||||
{
|
||||
}
|
||||
|
||||
impl<I> Source for Spatial<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
}
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Internal function that builds a `Speed` object.
|
||||
pub fn speed<I>(input: I, factor: f32) -> Speed<I> {
|
||||
Speed { input, factor }
|
||||
}
|
||||
|
||||
/// Filter that modifies each sample by a given value.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Speed<I> {
|
||||
input: I,
|
||||
factor: f32,
|
||||
}
|
||||
|
||||
impl<I> Speed<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
/// Modifies the speed factor.
|
||||
#[inline]
|
||||
pub fn set_factor(&mut self, factor: f32) {
|
||||
self.factor = factor;
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for Speed<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
self.input.next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> ExactSizeIterator for Speed<I>
|
||||
where
|
||||
I: Source + ExactSizeIterator,
|
||||
I::Item: Sample,
|
||||
{
|
||||
}
|
||||
|
||||
impl<I> Source for Speed<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
(self.input.sample_rate() as f32 * self.factor) as u32
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration().map(|d| d.mul_f32(self.factor))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
/* TODO: This might be wrong, I do not know how speed achieves its speedup
|
||||
* so I can not reason about the correctness.
|
||||
* <dvdsk noreply@davidsk.dev> */
|
||||
|
||||
// even after 24 hours of playback f32 has enough precision
|
||||
let pos_accounting_for_speedup = pos.mul_f32(self.factor);
|
||||
self.input.try_seek(pos_accounting_for_speedup)
|
||||
}
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Internal function that builds a `Stoppable` object.
|
||||
pub fn stoppable<I>(source: I) -> Stoppable<I> {
|
||||
Stoppable {
|
||||
input: source,
|
||||
stopped: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Stoppable<I> {
|
||||
input: I,
|
||||
stopped: bool,
|
||||
}
|
||||
|
||||
impl<I> Stoppable<I> {
|
||||
/// Stops the sound.
|
||||
#[inline]
|
||||
pub fn stop(&mut self) {
|
||||
self.stopped = true;
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for Stoppable<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
if self.stopped {
|
||||
None
|
||||
} else {
|
||||
self.input.next()
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Source for Stoppable<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
}
|
||||
}
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// Internal function that builds a `TakeDuration` object.
|
||||
pub fn take_duration<I>(input: I, duration: Duration) -> TakeDuration<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
TakeDuration {
|
||||
current_frame_len: input.current_frame_len(),
|
||||
duration_per_sample: TakeDuration::get_duration_per_sample(&input),
|
||||
input,
|
||||
remaining_duration: duration,
|
||||
requested_duration: duration,
|
||||
filter: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A filter that can be applied to a `TakeDuration`.
|
||||
#[derive(Clone, Debug)]
|
||||
enum DurationFilter {
|
||||
FadeOut,
|
||||
}
|
||||
impl DurationFilter {
|
||||
fn apply<I: Iterator>(
|
||||
&self,
|
||||
sample: <I as Iterator>::Item,
|
||||
parent: &TakeDuration<I>,
|
||||
) -> <I as Iterator>::Item
|
||||
where
|
||||
I::Item: Sample,
|
||||
{
|
||||
use self::DurationFilter::*;
|
||||
match self {
|
||||
FadeOut => {
|
||||
let remaining = parent.remaining_duration.as_millis() as f32;
|
||||
let total = parent.requested_duration.as_millis() as f32;
|
||||
sample.amplify(remaining / total)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const NANOS_PER_SEC: u64 = 1_000_000_000;
|
||||
|
||||
/// A source that truncates the given source to a certain duration.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TakeDuration<I> {
|
||||
input: I,
|
||||
remaining_duration: Duration,
|
||||
requested_duration: Duration,
|
||||
filter: Option<DurationFilter>,
|
||||
// Remaining samples in current frame.
|
||||
current_frame_len: Option<usize>,
|
||||
// Only updated when the current frame len is exhausted.
|
||||
duration_per_sample: Duration,
|
||||
}
|
||||
|
||||
impl<I> TakeDuration<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
/// Returns the duration elapsed for each sample extracted.
|
||||
#[inline]
|
||||
fn get_duration_per_sample(input: &I) -> Duration {
|
||||
let ns = NANOS_PER_SEC / (input.sample_rate() as u64 * input.channels() as u64);
|
||||
// \|/ the maximum value of `ns` is one billion, so this can't fail
|
||||
Duration::new(0, ns as u32)
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
}
|
||||
|
||||
pub fn set_filter_fadeout(&mut self) {
|
||||
self.filter = Some(DurationFilter::FadeOut);
|
||||
}
|
||||
|
||||
pub fn clear_filter(&mut self) {
|
||||
self.filter = None;
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for TakeDuration<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = <I as Iterator>::Item;
|
||||
|
||||
fn next(&mut self) -> Option<<I as Iterator>::Item> {
|
||||
if let Some(frame_len) = self.current_frame_len.take() {
|
||||
if frame_len > 0 {
|
||||
self.current_frame_len = Some(frame_len - 1);
|
||||
} else {
|
||||
self.current_frame_len = self.input.current_frame_len();
|
||||
// Sample rate might have changed
|
||||
self.duration_per_sample = Self::get_duration_per_sample(&self.input);
|
||||
}
|
||||
}
|
||||
|
||||
if self.remaining_duration <= self.duration_per_sample {
|
||||
None
|
||||
} else if let Some(sample) = self.input.next() {
|
||||
let sample = match &self.filter {
|
||||
Some(filter) => filter.apply(sample, self),
|
||||
None => sample,
|
||||
};
|
||||
|
||||
self.remaining_duration -= self.duration_per_sample;
|
||||
|
||||
Some(sample)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: size_hint
|
||||
}
|
||||
|
||||
impl<I> Source for TakeDuration<I>
|
||||
where
|
||||
I: Iterator + Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
let remaining_nanos = self.remaining_duration.as_secs() * NANOS_PER_SEC
|
||||
+ self.remaining_duration.subsec_nanos() as u64;
|
||||
let nanos_per_sample = self.duration_per_sample.as_secs() * NANOS_PER_SEC
|
||||
+ self.duration_per_sample.subsec_nanos() as u64;
|
||||
let remaining_samples = (remaining_nanos / nanos_per_sample) as usize;
|
||||
|
||||
self.input
|
||||
.current_frame_len()
|
||||
.filter(|value| *value < remaining_samples)
|
||||
.or(Some(remaining_samples))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
if let Some(duration) = self.input.total_duration() {
|
||||
if duration < self.requested_duration {
|
||||
Some(duration)
|
||||
} else {
|
||||
Some(self.requested_duration)
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
}
|
||||
}
|
||||
+210
@@ -0,0 +1,210 @@
|
||||
use std::cmp;
|
||||
use std::time::Duration;
|
||||
|
||||
use cpal::FromSample;
|
||||
|
||||
use crate::conversions::{ChannelCountConverter, DataConverter, SampleRateConverter};
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// An iterator that reads from a `Source` and converts the samples to a specific rate and
|
||||
/// channels count.
|
||||
///
|
||||
/// It implements `Source` as well, but all the data is guaranteed to be in a single frame whose
|
||||
/// channels and samples rate have been passed to `new`.
|
||||
#[derive(Clone)]
|
||||
pub struct UniformSourceIterator<I, D>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
D: Sample,
|
||||
{
|
||||
inner: Option<DataConverter<ChannelCountConverter<SampleRateConverter<Take<I>>>, D>>,
|
||||
target_channels: u16,
|
||||
target_sample_rate: u32,
|
||||
total_duration: Option<Duration>,
|
||||
}
|
||||
|
||||
impl<I, D> UniformSourceIterator<I, D>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
D: Sample,
|
||||
{
|
||||
#[inline]
|
||||
pub fn new(
|
||||
input: I,
|
||||
target_channels: u16,
|
||||
target_sample_rate: u32,
|
||||
) -> UniformSourceIterator<I, D> {
|
||||
let total_duration = input.total_duration();
|
||||
let input = UniformSourceIterator::bootstrap(input, target_channels, target_sample_rate);
|
||||
|
||||
UniformSourceIterator {
|
||||
inner: Some(input),
|
||||
target_channels,
|
||||
target_sample_rate,
|
||||
total_duration,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn bootstrap(
|
||||
input: I,
|
||||
target_channels: u16,
|
||||
target_sample_rate: u32,
|
||||
) -> DataConverter<ChannelCountConverter<SampleRateConverter<Take<I>>>, D> {
|
||||
// Limit the frame length to something reasonable
|
||||
let frame_len = input.current_frame_len().map(|x| x.min(32768));
|
||||
|
||||
let from_channels = input.channels();
|
||||
let from_sample_rate = input.sample_rate();
|
||||
|
||||
let input = Take {
|
||||
iter: input,
|
||||
n: frame_len,
|
||||
};
|
||||
let input = SampleRateConverter::new(
|
||||
input,
|
||||
cpal::SampleRate(from_sample_rate),
|
||||
cpal::SampleRate(target_sample_rate),
|
||||
from_channels,
|
||||
);
|
||||
let input = ChannelCountConverter::new(input, from_channels, target_channels);
|
||||
|
||||
DataConverter::new(input)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, D> Iterator for UniformSourceIterator<I, D>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
D: FromSample<I::Item> + Sample,
|
||||
{
|
||||
type Item = D;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<D> {
|
||||
if let Some(value) = self.inner.as_mut().unwrap().next() {
|
||||
return Some(value);
|
||||
}
|
||||
|
||||
let input = self
|
||||
.inner
|
||||
.take()
|
||||
.unwrap()
|
||||
.into_inner()
|
||||
.into_inner()
|
||||
.into_inner()
|
||||
.iter;
|
||||
|
||||
let mut input =
|
||||
UniformSourceIterator::bootstrap(input, self.target_channels, self.target_sample_rate);
|
||||
|
||||
let value = input.next();
|
||||
self.inner = Some(input);
|
||||
value
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.inner.as_ref().unwrap().size_hint().0, None)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, D> Source for UniformSourceIterator<I, D>
|
||||
where
|
||||
I: Iterator + Source,
|
||||
I::Item: Sample,
|
||||
D: FromSample<I::Item> + Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.target_channels
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.target_sample_rate
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.total_duration
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
if let Some(input) = self.inner.as_mut() {
|
||||
input
|
||||
.inner_mut()
|
||||
.inner_mut()
|
||||
.inner_mut()
|
||||
.inner_mut()
|
||||
.try_seek(pos)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct Take<I> {
|
||||
iter: I,
|
||||
n: Option<usize>,
|
||||
}
|
||||
|
||||
impl<I> Take<I> {
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.iter
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for Take<I>
|
||||
where
|
||||
I: Iterator,
|
||||
{
|
||||
type Item = <I as Iterator>::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<<I as Iterator>::Item> {
|
||||
if let Some(n) = &mut self.n {
|
||||
if *n != 0 {
|
||||
*n -= 1;
|
||||
self.iter.next()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
self.iter.next()
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
if let Some(n) = self.n {
|
||||
let (lower, upper) = self.iter.size_hint();
|
||||
|
||||
let lower = cmp::min(lower, n);
|
||||
|
||||
let upper = match upper {
|
||||
Some(x) if x < n => Some(x),
|
||||
_ => Some(n),
|
||||
};
|
||||
|
||||
(lower, upper)
|
||||
} else {
|
||||
self.iter.size_hint()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> ExactSizeIterator for Take<I> where I: ExactSizeIterator {}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
use std::marker::PhantomData;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
|
||||
/// An infinite source that produces zero.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Zero<S> {
|
||||
channels: u16,
|
||||
sample_rate: u32,
|
||||
num_samples: Option<usize>,
|
||||
marker: PhantomData<S>,
|
||||
}
|
||||
|
||||
impl<S> Zero<S> {
|
||||
#[inline]
|
||||
pub fn new(channels: u16, sample_rate: u32) -> Zero<S> {
|
||||
Zero {
|
||||
channels,
|
||||
sample_rate,
|
||||
num_samples: None,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn new_samples(channels: u16, sample_rate: u32, num_samples: usize) -> Zero<S> {
|
||||
Zero {
|
||||
channels,
|
||||
sample_rate,
|
||||
num_samples: Some(num_samples),
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Iterator for Zero<S>
|
||||
where
|
||||
S: Sample,
|
||||
{
|
||||
type Item = S;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<S> {
|
||||
if let Some(num_samples) = self.num_samples {
|
||||
if num_samples > 0 {
|
||||
self.num_samples = Some(num_samples - 1);
|
||||
Some(S::zero_value())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
Some(S::zero_value())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Source for Zero<S>
|
||||
where
|
||||
S: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.num_samples
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.channels
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.sample_rate
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, _: Duration) -> Result<(), SeekError> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user