Skip to content
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

[Discussion] Minimize unnecessary resampling #646

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion examples/mix_multiple_sources.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use rodio::constants::DEFAULT_SAMPLE_RATE;
use rodio::mixer;
use rodio::source::{SineWave, Source};
use std::error::Error;
use std::time::Duration;

fn main() -> Result<(), Box<dyn Error>> {
// Construct a dynamic controller and mixer, stream_handle, and sink.
let (controller, mixer) = mixer::mixer::<f32>(2, 44_100);
let (controller, mixer) = mixer::mixer::<f32>(2, DEFAULT_SAMPLE_RATE);
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());

Expand Down
5 changes: 3 additions & 2 deletions examples/noise_generator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Noise generator example. Use the "noise" feature to enable the noise generator sources.

use rodio::constants::DEFAULT_SAMPLE_RATE;
use std::error::Error;

#[cfg(feature = "noise")]
Expand All @@ -14,7 +15,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let interval_duration = Duration::from_millis(1500);

stream_handle.mixer().add(
white(cpal::SampleRate(48000))
white(cpal::SampleRate(DEFAULT_SAMPLE_RATE))
.amplify(0.1)
.take_duration(noise_duration),
);
Expand All @@ -23,7 +24,7 @@ fn main() -> Result<(), Box<dyn Error>> {
thread::sleep(interval_duration);

stream_handle.mixer().add(
pink(cpal::SampleRate(48000))
pink(cpal::SampleRate(DEFAULT_SAMPLE_RATE))
.amplify(0.1)
.take_duration(noise_duration),
);
Expand Down
3 changes: 2 additions & 1 deletion examples/signal_generator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Test signal generator example.

use rodio::constants::DEFAULT_SAMPLE_RATE;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -11,7 +12,7 @@ fn main() -> Result<(), Box<dyn Error>> {

let test_signal_duration = Duration::from_millis(1000);
let interval_duration = Duration::from_millis(1500);
let sample_rate = cpal::SampleRate(48000);
let sample_rate = cpal::SampleRate(DEFAULT_SAMPLE_RATE);

println!("Playing 1000 Hz tone");
stream_handle.mixer().add(
Expand Down
5 changes: 5 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Rodio common constants

/// The sample rate (Hz) output stream uses by default.
/// Prefer this one in the processing chain to minimize sample rate conversions.
pub const DEFAULT_SAMPLE_RATE: u32 = 44_100;
1 change: 0 additions & 1 deletion src/conversions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
This module contains function that will convert from one PCM format to another.

This includes conversion between sample formats, channels or sample rates.

*/

pub use self::channels::ChannelCountConverter;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ mod spatial_sink;
mod stream;

pub mod buffer;
pub mod constants;
pub mod decoder;
pub mod mixer;
pub mod queue;
Expand Down
6 changes: 3 additions & 3 deletions src/source/empty.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::marker::PhantomData;
use std::time::Duration;

use crate::{Sample, Source};

use super::SeekError;
use crate::constants::DEFAULT_SAMPLE_RATE;
use crate::{Sample, Source};

/// An empty source.
#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -50,7 +50,7 @@ where

#[inline]
fn sample_rate(&self) -> u32 {
48000
DEFAULT_SAMPLE_RATE
}

#[inline]
Expand Down
6 changes: 3 additions & 3 deletions src/source/empty_callback.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::marker::PhantomData;
use std::time::Duration;

use crate::{Sample, Source};

use super::SeekError;
use crate::constants::DEFAULT_SAMPLE_RATE;
use crate::{Sample, Source};

/// An empty source which executes a callback function
pub struct EmptyCallback<S> {
Expand Down Expand Up @@ -53,7 +53,7 @@ where

#[inline]
fn sample_rate(&self) -> u32 {
48000
DEFAULT_SAMPLE_RATE
}

#[inline]
Expand Down
12 changes: 5 additions & 7 deletions src/source/sine.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::time::Duration;

use crate::constants::DEFAULT_SAMPLE_RATE;
use crate::source::{Function, SignalGenerator};
use crate::Source;
use std::time::Duration;

use super::SeekError;

/// An infinite source that produces a sine.
///
/// Always has a sample rate of 48kHz and one channel.
/// Always has default sample rate.
///
/// This source is a thin interface on top of `SignalGenerator` provided for
/// your convenience.
Expand All @@ -17,12 +17,10 @@ pub struct SineWave {
}

impl SineWave {
const SAMPLE_RATE: u32 = 48000;

/// The frequency of the sine.
#[inline]
pub fn new(freq: f32) -> SineWave {
let sr = cpal::SampleRate(Self::SAMPLE_RATE);
let sr = cpal::SampleRate(DEFAULT_SAMPLE_RATE);
SineWave {
test_sine: SignalGenerator::new(sr, freq, Function::Sine),
}
Expand Down Expand Up @@ -51,7 +49,7 @@ impl Source for SineWave {

#[inline]
fn sample_rate(&self) -> u32 {
Self::SAMPLE_RATE
self.test_sine.sample_rate()
}

#[inline]
Expand Down
3 changes: 2 additions & 1 deletion src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::marker::Sync;
use std::sync::Arc;
use std::{error, fmt};

use crate::constants::DEFAULT_SAMPLE_RATE;
use crate::decoder;
use crate::mixer::{mixer, Mixer, MixerSource};
use crate::sink::Sink;
Expand All @@ -12,7 +13,7 @@ use cpal::{
SupportedBufferSize,
};

const HZ_44100: cpal::SampleRate = cpal::SampleRate(44_100);
const HZ_44100: cpal::SampleRate = cpal::SampleRate(DEFAULT_SAMPLE_RATE);

/// `cpal::Stream` container.
/// Use `mixer()` method to control output.
Expand Down
Loading