Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/conversions/sample_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::conversions::Sample;

use num_rational::Ratio;
use std::mem;
use std::time::Duration;
use crate::Source;

/// Iterator that converts from a certain sample rate to another.
#[derive(Clone, Debug)]
Expand All @@ -15,6 +17,7 @@ where
from: u32,
/// We convert chunks of `from` samples into chunks of `to` samples.
to: u32,
to_full: u32,
/// Number of channels in the stream
channels: cpal::ChannelCount,
/// One sample per channel, extracted from `input`.
Expand Down Expand Up @@ -61,6 +64,7 @@ where
assert!(num_channels >= 1);
assert!(from >= 1);
assert!(to >= 1);
let to_full = to;

let (first_samples, next_samples) = if from == to {
// if `from` == `to` == 1, then we just pass through
Expand All @@ -84,6 +88,7 @@ where
input,
from,
to,
to_full,
channels: num_channels,
current_frame_pos_in_chunk: 0,
next_output_frame_pos_in_chunk: 0,
Expand Down Expand Up @@ -120,6 +125,28 @@ where
}
}

impl<I> Source for SampleRateConverter<I>
where
I: Source,
I::Item: Sample,
{
fn current_frame_len(&self) -> Option<usize> {
None
}

fn channels(&self) -> u16 {
self.input.channels()
}

fn sample_rate(&self) -> u32 {
self.to_full
}

fn total_duration(&self) -> Option<Duration> {
self.input.total_duration()
}
}

impl<I> Iterator for SampleRateConverter<I>
where
I: Iterator,
Expand Down
115 changes: 115 additions & 0 deletions src/source/low_pass.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

luckily we already have a low_pass filter, see:
https://docs.rs/rodio/latest/rodio/source/trait.Source.html#method.low_pass

Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use std::time::Duration;
use num_rational::Ratio;
use crate::conversions::SampleRateConverter;
use crate::{Sample, Source};

pub struct LowPass<I>
where
I: Iterator,
{
input: I,
prev: Option<I::Item>
}

impl<I> LowPass<I>
where
I: Iterator,
I::Item: Sample,
{
#[inline]
pub fn new(
mut input: I,
) -> LowPass<I> {
LowPass {
input,
prev: None
}
}
}

impl<I> Source for LowPass<I>
where
I: Source,
I::Item: Sample,
{
fn current_frame_len(&self) -> Option<usize> {
None
}

fn channels(&self) -> u16 {
self.input.channels()
}

fn sample_rate(&self) -> u32 {
self.input.sample_rate()
}

fn total_duration(&self) -> Option<Duration> {
self.input.total_duration()
}
}

impl<I> Iterator for LowPass<I>
where
I: Iterator,
I::Item: Sample + Clone,
{
type Item = I::Item;

fn next(&mut self) -> Option<Self::Item> {
self.input.next().and_then(|s| {
let x = self.prev.map(|p| (p.saturating_add(s)).amplify(0.5));
self.prev.replace(s);
x
})
}
}

#[cfg(test)]
mod test {
use super::*;
use crate::conversions::SampleRateConverter;
use crate::source::SineWave;
use crate::Source;
use crate::{OutputStreamBuilder};
use std::thread;
use std::time::Duration;
use symphonia::core::meta::StandardTagKey::Encoder;

#[test]
fn test_low_pass() {
let stream_handle = OutputStreamBuilder::open_default_stream().unwrap();
let mixer = stream_handle.mixer();
{
// Generate sine wave.
let wave = SineWave::new(740.0)
.amplify(0.1)
.take_duration(Duration::from_secs(1));

let rate_in = wave.sample_rate();
let channels_in = wave.channels();
let out_freq = 44_100;
let output1 = SampleRateConverter::new(
wave,
cpal::SampleRate(rate_in),
cpal::SampleRate(out_freq * 2),
channels_in,
);

let lo_pass = LowPass::new(output1);

let rate_in = lo_pass.sample_rate();
let output2 = SampleRateConverter::new(
lo_pass,
cpal::SampleRate(rate_in),
cpal::SampleRate(out_freq),
channels_in,
);

mixer.add(output2);
}
WavFile

thread::sleep(Duration::from_millis(1000));
}
}
2 changes: 2 additions & 0 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ mod zero;

#[cfg(feature = "noise")]
mod noise;
mod low_pass;

#[cfg(feature = "noise")]
pub use self::noise::{pink, white, PinkNoise, WhiteNoise};

Expand Down
Loading