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

Update the amp example to use in place processing. #114

Open
wants to merge 1 commit into
base: develop
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
2 changes: 0 additions & 2 deletions docs/amp/eg-amp-rs.lv2/amp.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
# Every plugin must have a name, described with the doap:name property.
doap:name "Simple Amplifier (Rust Version)" ;
doap:license <http://opensource.org/licenses/isc> ;
# This tells the host that this plugin can not work "in-place"; The input and output buffers may not be the same. This plugin could technically work "in-place", but it would mean that the plugin would receive a mutable and an immutable reference to the same place in memory, which obviously isn't allowed in Rust.
lv2:requiredFeature lv2:inPlaceBroken ;
lv2:optionalFeature lv2:hardRTCapable ;
lv2:port [
# Every port must have at least two types, one that specifies direction
Expand Down
13 changes: 7 additions & 6 deletions docs/amp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use lv2::prelude::*;
// Most useful plugins will have ports for input and output data. In code, these ports are represented by a struct implementing the `PortCollection` trait. Internally, ports are referred to by index. These indices are assigned in ascending order, starting with 0 for the first port. The indices in `amp.ttl` have to match them.
#[derive(PortCollection)]
struct Ports {
gain: InputPort<Control>,
input: InputPort<Audio>,
output: OutputPort<Audio>,
gain: InputPort<InPlaceControl>,
input: InputPort<InPlaceAudio>,
output: OutputPort<InPlaceAudio>,
}
// Every plugin defines a struct for the plugin instance. All persistent data associated with a plugin instance is stored here, and is available to every instance method. In this simple plugin, there is no additional instance data and therefore, this struct is empty.
//
Expand All @@ -24,14 +24,15 @@ impl Plugin for Amp {
}
// The `run()` method is the main process function of the plugin. It processes a block of audio in the audio context. Since this plugin is `lv2:hardRTCapable`, `run()` must be real-time safe, so blocking (e.g. with a mutex) or memory allocation are not allowed.
fn run(&mut self, ports: &mut Ports, _features: &mut (), _: u32) {
let coef = if *(ports.gain) > -90.0 {
10.0_f32.powf(*(ports.gain) * 0.05)
let gain = ports.gain.get();
let coef = if gain > -90.0 {
10.0_f32.powf(gain * 0.05)
} else {
0.0
};

for (in_frame, out_frame) in Iterator::zip(ports.input.iter(), ports.output.iter_mut()) {
*out_frame = in_frame * coef;
out_frame.set(in_frame.get() * coef);
}
}
}
Expand Down