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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
#![allow(clippy::cast_precision_loss)]
mod retain;
mod window_function;
mod window_size;
mod window_type;
mod windowed_fft;
use crate::{
retain::RetainAlgorithm, window_size::WindowSize, window_type::WindowType,
windowed_fft::WindowedRealFft,
};
use egui::{Align, CentralPanel, ComboBox, Layout, Slider, Vec2};
use nice_plug::prelude::*;
use nice_plug_egui::{
EguiSettings, EguiState, create_egui_editor, resizable_window::ResizableWindow,
};
use rtrb::RingBuffer;
use std::sync::Arc;
pub const DEFAULT_WINDOW_TYPE: WindowType = WindowType::Hann;
const MIN_WINDOW_WIDTH: u32 = 600;
const MIN_WINDOW_HEIGHT: u32 = 350;
const GUI_TO_AUDIO_CHANNEL_CAPACITY: usize = 128;
pub struct Retain {
params: Arc<RetainParams>,
msg_channel: AudioMsgChannel,
initial_gui_state: Option<GuiState>,
fft_left: WindowedRealFft,
fft_right: WindowedRealFft,
retain_algorithm: RetainAlgorithm,
}
impl Default for Retain {
fn default() -> Self {
let (to_audio_tx, from_gui_rx) = RingBuffer::new(GUI_TO_AUDIO_CHANNEL_CAPACITY);
let params = RetainParams::default();
let fft_left = WindowedRealFft::new((params.window_size.value() as usize).into());
let fft_right = WindowedRealFft::new((params.window_size.value() as usize).into());
Self {
params: Arc::new(params),
msg_channel: AudioMsgChannel { from_gui_rx },
initial_gui_state: Some(GuiState {
msg_channel: GuiMsgChannel { to_audio_tx },
}),
fft_left,
fft_right,
retain_algorithm: RetainAlgorithm::new(),
}
}
}
#[derive(Params)]
pub struct RetainParams {
/// This allows the resizing of the plugin to be restored
#[persist = "editor_state"]
editor_state: Arc<EguiState>,
/// the number of top frequencies to keep
#[id = "order"]
pub order: IntParam,
/// the window size of the fft
/// this effects the frequency resolution and can make for cool effects
#[id = "window_size"]
pub window_size: IntParam,
/// the type of window function used to reduce spectral leakage or clicking sounds
/// playing with this makes a big difference in output
#[id = "window_type"]
pub window_type: EnumParam<WindowType>,
/// whether the plugin retains or removes the highest magnitude frequencies
#[id = "complement"]
pub complement: BoolParam,
}
impl Default for RetainParams {
fn default() -> Self {
Self {
editor_state: EguiState::from_size(MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT),
order: IntParam::new("Order", 1, IntRange::Linear { min: 0, max: 32768 }),
window_size: IntParam::new(
"Window Size",
1024,
IntRange::Linear {
min: 256,
max: 32768,
},
)
.non_automatable()
.hide(),
window_type: EnumParam::new("Window Type", WindowType::Hann)
.non_automatable()
.hide(),
complement: BoolParam::new("Complement", false).non_automatable().hide(),
}
}
}
pub struct GuiState {
msg_channel: GuiMsgChannel,
}
#[derive(Debug)]
pub enum GuiToAudioMsg {
WindowSizeUpdate,
WindowTypeUpdate,
}
pub struct GuiMsgChannel {
to_audio_tx: rtrb::Producer<GuiToAudioMsg>,
}
pub struct AudioMsgChannel {
from_gui_rx: rtrb::Consumer<GuiToAudioMsg>,
}
impl Plugin for Retain {
const NAME: &'static str = "Retain";
const VENDOR: &'static str = "haemolacriaa";
const URL: &'static str = "https://git.haemolacriaa.com/retain/";
const EMAIL: &'static str = "";
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const AUDIO_IO_LAYOUTS: &'static [AudioIOLayout] = &[AudioIOLayout {
main_input_channels: NonZeroU32::new(2),
main_output_channels: NonZeroU32::new(2),
..AudioIOLayout::const_default()
}];
const SAMPLE_ACCURATE_AUTOMATION: bool = false;
type SysExMessage = ();
type BackgroundTask = ();
fn initialize(
&mut self,
_audio_io_layout: &AudioIOLayout,
_buffer_config: &BufferConfig,
context: &mut impl InitContext<Self>,
) -> bool {
context.set_latency_samples(self.params.window_size.value().cast_unsigned());
true
}
fn editor(&mut self, _async_executor: AsyncExecutor<Self>) -> Option<Box<dyn Editor>> {
let params = self.params.clone();
let egui_state = params.editor_state.clone();
create_egui_editor(
self.params.editor_state.clone(),
self.initial_gui_state.take().unwrap(),
EguiSettings::default(),
|_egui_ctx, _queue, _gui_state| {},
move |ui, setter, _queue, gui_state| {
ResizableWindow::new("retainwindow")
.min_size(Vec2::new(MIN_WINDOW_WIDTH as f32, MIN_WINDOW_HEIGHT as f32))
.show(ui, egui_state.as_ref(), |ui| {
CentralPanel::default().show_inside(ui, |ui| {
ui.heading("Retain");
ui.horizontal(|ui| {
ui.with_layout(Layout::top_down(Align::LEFT), |ui| {
let mut order = params.order.value();
let window_size = params.window_size.value();
let slider = ui.add(
Slider::new(&mut order, 0..=window_size)
.text("Order")
.logarithmic(true),
);
if slider.changed() {
setter.set_parameter(¶ms.order, order);
}
let mut window_size = Into::<WindowSize>::into(
params.window_size.value() as usize,
);
ComboBox::from_label("Window Size")
.selected_text(window_size.as_str())
.show_ui(ui, |ui| {
for size in WindowSize::iter() {
let display = size.as_str();
let inner = size.inner();
if ui
.selectable_value(
&mut window_size,
size,
display,
)
.changed()
{
setter.set_parameter(
¶ms.window_size,
inner as i32,
);
if let Err(e) = gui_state.msg_channel.to_audio_tx.push(GuiToAudioMsg::WindowSizeUpdate) {
nice_error!("Failed to send message to audio thread: {}", e);
}
}
}
});
});
ui.with_layout(Layout::top_down(Align::RIGHT), |ui| {
let mut complement = params.complement.value();
if ui.checkbox(&mut complement, "Complement").changed() {
setter.set_parameter(¶ms.complement, complement);
}
let window_type = params.window_type.value();
let selected = window_type.as_str();
let mut window_type = window_type.as_byte();
ComboBox::from_label("Window Function")
.selected_text(selected)
.show_ui(ui, |ui| {
for function in WindowType::iter() {
let display = function.as_str();
let byte = function.as_byte();
if ui
.selectable_value(
&mut window_type,
function.as_byte(),
display,
)
.changed()
{
setter.set_parameter(
¶ms.window_type,
byte.into(),
);
if let Err(e) = gui_state.msg_channel.to_audio_tx.push(GuiToAudioMsg::WindowTypeUpdate) {
nice_error!("Failed to send message to audio thread: {}", e);
}
}
}
});
});
});
});
});
},
)
}
fn process(
&mut self,
buffer: &mut Buffer,
_aux: &mut AuxiliaryBuffers,
context: &mut impl ProcessContext<Self>,
) -> ProcessStatus {
// this is to prevent multiple updates from slowing this thread down if it somehow happens
let mut previously_updated_window_size = false;
let mut previously_updated_window_type = false;
while let Ok(msg) = self.msg_channel.from_gui_rx.pop() {
match msg {
GuiToAudioMsg::WindowSizeUpdate => {
if previously_updated_window_size {
continue;
}
let window_size = self.params.window_size.value() as usize;
self.fft_left.window_size(window_size.into());
self.fft_right.window_size(window_size.into());
context.set_latency_samples(window_size as u32);
previously_updated_window_size = true;
}
GuiToAudioMsg::WindowTypeUpdate => {
if previously_updated_window_type {
continue;
}
let window_type = self.params.window_type.value();
self.fft_left.window_function(&window_type);
self.fft_right.window_function(&window_type);
previously_updated_window_type = true;
}
}
}
if let [left, right] = buffer.as_slice() {
for sample in left.iter_mut() {
if self.fft_left.push_back_input(*sample) {
self.fft_left.forward();
let order = self.params.order.value() as usize;
let complement = self.params.complement.value();
self.retain_algorithm.calculate(self.fft_left.get_spectrum(), order, complement);
self.fft_left.inverse();
self.fft_left.clear_input();
}
*sample = self.fft_left.pop_front_output();
}
for sample in right.iter_mut() {
if self.fft_right.push_back_input(*sample) {
self.fft_right.forward();
let order = self.params.order.value() as usize;
let complement = self.params.complement.value();
self.retain_algorithm.calculate(self.fft_right.get_spectrum(), order, complement);
self.fft_right.inverse();
self.fft_right.clear_input();
}
*sample = self.fft_right.pop_front_output();
}
}
ProcessStatus::Normal
}
fn params(&self) -> Arc<dyn Params> {
self.params.clone()
}
}
impl ClapPlugin for Retain {
const CLAP_ID: &'static str = "com.haemolacriaa.retain";
const CLAP_DESCRIPTION: Option<&'static str> =
Some("Retain is a real-time audio plugin that retains the nth largest magnitude frequencies in a signal");
const CLAP_MANUAL_URL: Option<&'static str> = Some(Self::URL);
const CLAP_SUPPORT_URL: Option<&'static str> = None;
const CLAP_FEATURES: &'static [ClapFeature] = &[
ClapFeature::AudioEffect,
ClapFeature::Filter,
ClapFeature::Stereo,
];
}
impl Vst3Plugin for Retain {
const VST3_CLASS_ID: [u8; 16] = *b"Retainhaemolacir";
const VST3_SUBCATEGORIES: &'static [Vst3SubCategory] = &[
Vst3SubCategory::Fx,
Vst3SubCategory::Filter,
Vst3SubCategory::Stereo,
];
}
//nice_export_clap!(Retain);
nice_export_vst3!(Retain);
|