Skip to content
This repository has been archived by the owner on Aug 19, 2019. It is now read-only.

Update to latest changes in dsp-chain (mainly the addition of the Frame trait). Update deps. #9

Merged
merged 3 commits into from Jun 8, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -2,6 +2,7 @@ language: rust

rust:
- nightly
- beta
- stable

script:
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "volume"
version = "0.3.0"
version = "0.4.0"
authors = ["mitchmindtree <mitchell.nordine@gmail.com>"]
description = "A simple dsp-chain node for multiplying the amplitude of the output buffer by some volume."
readme = "README.md"
Expand All @@ -10,5 +10,5 @@ repository = "https://github.com/RustAudio/volume.git"
homepage = "https://github.com/RustAudio/volume"

[dependencies]
dsp-chain = "0.12.0"
time_calc = "0.10.1"
dsp-chain = "0.13.0"
time_calc = "0.11.0"
37 changes: 16 additions & 21 deletions src/lib.rs
@@ -1,5 +1,4 @@
/// A simple dsp node for multiplying the amplitude of its inputs by some given multiplier.

//! A simple dsp node for multiplying the amplitude of its inputs by some given multiplier.

extern crate dsp;
extern crate time_calc as time;
Expand Down Expand Up @@ -50,13 +49,13 @@ impl Volume {
}


impl<S> dsp::Node<S> for Volume
where S: dsp::Sample + dsp::sample::Duplex<f32>,
impl<F> dsp::Node<F> for Volume
where F: dsp::Frame,
{

#[inline]
fn audio_requested(&mut self, buffer: &mut [S], settings: dsp::Settings) {
use dsp::Sample;
fn audio_requested(&mut self, buffer: &mut [F], sample_hz: f64) {
use dsp::{Frame, Sample};
match self.maybe_prev {

// If the volume used for the previous buffer is different to the volume used for the
Expand All @@ -66,35 +65,31 @@ impl<S> dsp::Node<S> for Volume

// Calculate the interpolation duration in frames along with the volume increment
// to use for interpolation.
let interpolation_frames = ::std::cmp::min(
settings.frames as usize,
time::Ms(self.interpolation_ms).samples(settings.sample_hz as f64) as usize
let interpolation_frames = std::cmp::min(
buffer.len(),
time::Ms(self.interpolation_ms).samples(sample_hz) as usize,
);
let volume_diff = self.current - prev;
let volume_increment = volume_diff * (1.0 / interpolation_frames as f32);
let mut volume = prev;

// Interpolated frames.
for frame in 0..interpolation_frames {
for idx in 0..interpolation_frames {
volume += volume_increment;
for channel in 0..(settings.channels as usize) {
let idx = frame * (settings.channels as usize) + channel;
let sample = &mut buffer[idx];
*sample = (sample.to_sample::<f32>() * volume).to_sample();
}
let frame = &mut buffer[idx];
*frame = frame.scale_amp(volume.to_sample());
}

// Remaining frames.
let start_of_remaining = interpolation_frames * settings.channels as usize;
for idx in start_of_remaining..buffer.len() {
let sample = &mut buffer[idx];
*sample = (sample.to_sample::<f32>() * volume).to_sample();
for idx in interpolation_frames..buffer.len() {
let frame = &mut buffer[idx];
*frame = frame.scale_amp(volume.to_sample());
}
},

// Otherwise, simply multiply every sample by the current volume.
_ => for sample in buffer.iter_mut() {
*sample = (sample.to_sample::<f32>() * self.current).to_sample();
_ => for frame in buffer.iter_mut() {
*frame = frame.scale_amp(self.current.to_sample());
},

}
Expand Down