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

Generate samples at a multiple of channels #249

Merged
merged 2 commits into from
Nov 16, 2019
Merged
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
2 changes: 1 addition & 1 deletion src/source/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ where
I::Item: Sample,
{
let duration_ns = duration.as_secs() * 1000000000 + duration.subsec_nanos() as u64;
let samples = duration_ns * input.sample_rate() as u64 * input.channels() as u64 / 1000000000;
let samples = duration_ns * input.sample_rate() as u64 / 1000000000 * input.channels() as u64;

Delay {
input: input,
Expand Down
37 changes: 31 additions & 6 deletions src/source/pausable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,45 @@ use Sample;
use Source;

/// Internal function that builds a `Pausable` object.
pub fn pausable<I>(source: I, paused: bool) -> Pausable<I> {
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: paused,
paused_channels,
remaining_paused_samples: 0,
}
}

#[derive(Clone, Debug)]
pub struct Pausable<I> {
input: I,
paused: bool,
paused_channels: Option<u16>,
remaining_paused_samples: u16,
}

impl<I> Pausable<I> {
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) {
self.paused = paused;
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.
Expand Down Expand Up @@ -54,7 +73,13 @@ where

#[inline]
fn next(&mut self) -> Option<I::Item> {
if self.paused {
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());
}

Expand Down
3 changes: 1 addition & 2 deletions src/source/periodic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ where
// 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_nanos() / 1_000_000;
let sample_rate = source.sample_rate() * source.channels() as u32;
let update_frequency = (update_ms * sample_rate) / 1000;
let update_frequency = (update_ms * source.sample_rate()) / 1000 * source.channels() as u32;

PeriodicAccess {
input: source,
Expand Down