Skip to content

Commit

Permalink
feat(playback): allow to specify sample rate and skip useless resample
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesterday17 committed Sep 22, 2024
1 parent 661fe1b commit dee0e19
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion anni-playback/examples/gapless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Player {
thread::spawn({
let controls = controls.clone();
move || {
let decoder = Decoder::new(controls, thread_killer.1.clone());
let decoder = Decoder::new(controls, 48000, thread_killer.1.clone());
decoder.start();
}
});
Expand Down
2 changes: 1 addition & 1 deletion anni-playback/examples/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Player {
thread::spawn({
let controls = controls.clone();
move || {
let decoder = Decoder::new(controls, thread_killer.1.clone());
let decoder = Decoder::new(controls, 48000, thread_killer.1.clone());
decoder.start();
}
});
Expand Down
2 changes: 1 addition & 1 deletion anni-playback/examples/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Player {
thread::spawn({
let controls = controls.clone();
move || {
let decoder = Decoder::new(controls, thread_killer.1.clone());
let decoder = Decoder::new(controls, 48000, thread_killer.1.clone());
decoder.start();
}
});
Expand Down
19 changes: 12 additions & 7 deletions anni-playback/src/cpal_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,18 @@ impl CpalOutput {
return;
}

let mut samples = if let Some(resampler) = &mut self.resampler {
// If there is a resampler, then write resampled values
// instead of the normal `samples`.
resampler.resample(decoded).unwrap_or(&[])
} else {
self.sample_buffer.copy_interleaved_ref(decoded);
self.sample_buffer.samples()
let need_resample = decoded.spec().rate != self.spec.rate;
let mut samples = match (need_resample, &mut self.resampler) {
(true, Some(resampler)) => {
// If there is a resampler, then write resampled values
// instead of the normal `samples`.
resampler.resample(decoded).unwrap_or(&[])
}
_ => {
// no resampler, or the target sampe rate is the same as the input
self.sample_buffer.copy_interleaved_ref(decoded);
self.sample_buffer.samples()
}
};

if self.controls.is_normalizing() {
Expand Down
5 changes: 2 additions & 3 deletions anni-playback/src/decoder/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ pub struct Decoder {

impl Decoder {
/// Creates a new decoder.
pub fn new(controls: Controls, thread_killer: Receiver<bool>) -> Self {
// TODO: allow specifying sample rate by user
let spec = SignalSpec::new_with_layout(44100, Layout::Stereo);
pub fn new(controls: Controls, sample_rate: u32, thread_killer: Receiver<bool>) -> Self {
let spec = SignalSpec::new_with_layout(sample_rate, Layout::Stereo);

Decoder {
thread_killer,
Expand Down
14 changes: 12 additions & 2 deletions anni-playback/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,21 @@ pub struct AnniPlayer {
cache_store: CacheStore, // root of cache
}

pub struct AnniPlayerOptions {
pub sample_rate: u32,
pub cache_path: PathBuf,
}

impl AnniPlayer {
pub fn new(
provider: TypedPriorityProvider<ProviderProxy>,
cache_path: PathBuf,
options: AnniPlayerOptions,
) -> (Self, Receiver<PlayerEvent>) {
let AnniPlayerOptions {
sample_rate,
cache_path,
} = options;

let (controls, receiver, killer) = {
let (sender, receiver) = mpsc::channel();
let controls = Controls::new(sender);
Expand All @@ -48,7 +58,7 @@ impl AnniPlayer {
.spawn({
let controls = controls.clone();
move || {
let decoder = Decoder::new(controls, thread_killer.1);
let decoder = Decoder::new(controls, sample_rate, thread_killer.1);

decoder.start();
}
Expand Down

0 comments on commit dee0e19

Please sign in to comment.