Skip to content

Commit

Permalink
Generate samples at a multiple of channels (RustAudio#249)
Browse files Browse the repository at this point in the history
* Generate samples at a multiple of channels

* Fix nits
  • Loading branch information
Cocalus authored and est31 committed Nov 16, 2019
1 parent 379f4b1 commit d8723a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/source/delay.rs
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
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
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

0 comments on commit d8723a5

Please sign in to comment.