-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[not for merge] Resampler low pass prototype #655
Draft
PetrGlad
wants to merge
11
commits into
RustAudio:master
Choose a base branch
from
PetrGlad:linear-resampler-tweaks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
71dbb87
Resampler low pass prototype
PetrGlad f8eac57
Build fix
PetrGlad 4a322a2
Cleanups
PetrGlad e1a5d7e
Merge remote-tracking branch 'rust-audio/master' into linear-resample…
PetrGlad 8154e44
Cargo update
PetrGlad b5e245d
Build fix: add rustdoc
PetrGlad 55f15b9
Write test output to WAv file for offline analysis.
PetrGlad 58d650f
Low pass filter as resampler postprocessing prototype
PetrGlad 604b96a
Cleanup
PetrGlad e13d3c4
Use existing file for tests
PetrGlad 9bbdaef
cargo fmt
PetrGlad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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