This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
gain_effect.rs
129 lines (114 loc) · 4.49 KB
/
gain_effect.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// author: doomy <notdoomy@protonmail.com>
#[macro_use]
extern crate vst;
use std::sync::Arc;
use vst::prelude::*;
/// Simple Gain Effect.
/// Note that this does not use a proper scale for sound and shouldn't be used in
/// a production amplification effect! This is purely for demonstration purposes,
/// as well as to keep things simple as this is meant to be a starting point for
/// any effect.
struct GainEffect {
// Store a handle to the plugin's parameter object.
params: Arc<GainEffectParameters>,
}
/// The plugin's parameter object contains the values of parameters that can be
/// adjusted from the host. If we were creating an effect that didn't allow the
/// user to modify it at runtime or have any controls, we could omit this part.
///
/// The parameters object is shared between the processing and GUI threads.
/// For this reason, all mutable state in the object has to be represented
/// through thread-safe interior mutability. The easiest way to achieve this
/// is to store the parameters in atomic containers.
struct GainEffectParameters {
// The plugin's state consists of a single parameter: amplitude.
amplitude: AtomicFloat,
}
impl Default for GainEffectParameters {
fn default() -> GainEffectParameters {
GainEffectParameters {
amplitude: AtomicFloat::new(0.5),
}
}
}
// All plugins using `vst` also need to implement the `Plugin` trait. Here, we
// define functions that give necessary info to our host.
impl Plugin for GainEffect {
fn new(_host: HostCallback) -> Self {
// Note that controls will always return a value from 0 - 1.
// Setting a default to 0.5 means it's halfway up.
GainEffect {
params: Arc::new(GainEffectParameters::default()),
}
}
fn get_info(&self) -> Info {
Info {
name: "Gain Effect in Rust".to_string(),
vendor: "Rust DSP".to_string(),
unique_id: 243723072,
version: 1,
inputs: 2,
outputs: 2,
// This `parameters` bit is important; without it, none of our
// parameters will be shown!
parameters: 1,
category: Category::Effect,
..Default::default()
}
}
// Here is where the bulk of our audio processing code goes.
fn process(&mut self, buffer: &mut AudioBuffer<f32>) {
// Read the amplitude from the parameter object
let amplitude = self.params.amplitude.get();
// First, we destructure our audio buffer into an arbitrary number of
// input and output buffers. Usually, we'll be dealing with stereo (2 of each)
// but that might change.
for (input_buffer, output_buffer) in buffer.zip() {
// Next, we'll loop through each individual sample so we can apply the amplitude
// value to it.
for (input_sample, output_sample) in input_buffer.iter().zip(output_buffer) {
*output_sample = *input_sample * amplitude;
}
}
}
// Return the parameter object. This method can be omitted if the
// plugin has no parameters.
fn get_parameter_object(&mut self) -> Arc<dyn PluginParameters> {
Arc::clone(&self.params) as Arc<dyn PluginParameters>
}
}
impl PluginParameters for GainEffectParameters {
// the `get_parameter` function reads the value of a parameter.
fn get_parameter(&self, index: i32) -> f32 {
match index {
0 => self.amplitude.get(),
_ => 0.0,
}
}
// the `set_parameter` function sets the value of a parameter.
fn set_parameter(&self, index: i32, val: f32) {
#[allow(clippy::single_match)]
match index {
0 => self.amplitude.set(val),
_ => (),
}
}
// This is what will display underneath our control. We can
// format it into a string that makes the most since.
fn get_parameter_text(&self, index: i32) -> String {
match index {
0 => format!("{:.2}", (self.amplitude.get() - 0.5) * 2f32),
_ => "".to_string(),
}
}
// This shows the control's name.
fn get_parameter_name(&self, index: i32) -> String {
match index {
0 => "Amplitude",
_ => "",
}
.to_string()
}
}
// This part is important! Without it, our plugin won't work.
plugin_main!(GainEffect);