1
0
Fork 0

Add the data needed for the analyzer to Data

This commit is contained in:
Robbert van der Helm 2023-03-20 14:56:17 +01:00
parent 01f9aa52cb
commit 990fe33ff7
4 changed files with 37 additions and 16 deletions

1
Cargo.lock generated
View file

@ -3756,6 +3756,7 @@ dependencies = [
name = "spectral_compressor" name = "spectral_compressor"
version = "0.3.0" version = "0.3.0"
dependencies = [ dependencies = [
"atomic_float",
"crossbeam", "crossbeam",
"nih_plug", "nih_plug",
"nih_plug_vizia", "nih_plug_vizia",

View file

@ -16,6 +16,7 @@ nih_plug_vizia = { path = "../../nih_plug_vizia" }
realfft = "3.0" realfft = "3.0"
# For the GUI # For the GUI
atomic_float = "0.1"
crossbeam = "0.8" crossbeam = "0.8"
open = "3.0" open = "3.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View file

@ -14,15 +14,17 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
use atomic_float::AtomicF32;
use crossbeam::atomic::AtomicCell; use crossbeam::atomic::AtomicCell;
use nih_plug::prelude::*; use nih_plug::prelude::*;
use nih_plug_vizia::vizia::prelude::*; use nih_plug_vizia::vizia::prelude::*;
use nih_plug_vizia::widgets::*; use nih_plug_vizia::widgets::*;
use nih_plug_vizia::{assets, create_vizia_editor, ViziaState, ViziaTheming}; use nih_plug_vizia::{assets, create_vizia_editor, ViziaState, ViziaTheming};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::Arc; use std::sync::{Arc, Mutex};
use self::mode_button::EditorModeButton; use self::mode_button::EditorModeButton;
use crate::analyzer::AnalyzerData;
use crate::{SpectralCompressor, SpectralCompressorParams}; use crate::{SpectralCompressor, SpectralCompressorParams};
mod mode_button; mod mode_button;
@ -52,11 +54,16 @@ pub enum EditorMode {
AnalyzerVisible, AnalyzerVisible,
} }
#[derive(Lens)] #[derive(Clone, Lens)]
struct Data { pub struct Data {
params: Arc<SpectralCompressorParams>, pub(crate) params: Arc<SpectralCompressorParams>,
editor_mode: Arc<AtomicCell<EditorMode>>, /// Determines which parts of the GUI are visible, and in turn decides the GUI's size.
pub(crate) editor_mode: Arc<AtomicCell<EditorMode>>,
pub(crate) analyzer_data: Arc<Mutex<triple_buffer::Output<AnalyzerData>>>,
/// Used by the analyzer to determine which FFT bins belong to which frequencies.
pub(crate) sample_rate: Arc<AtomicF32>,
} }
impl Model for Data {} impl Model for Data {}
@ -69,20 +76,12 @@ pub(crate) fn default_state(editor_mode: Arc<AtomicCell<EditorMode>>) -> Arc<Viz
}) })
} }
pub(crate) fn create( pub(crate) fn create(editor_state: Arc<ViziaState>, editor_data: Data) -> Option<Box<dyn Editor>> {
params: Arc<SpectralCompressorParams>,
editor_state: Arc<ViziaState>,
) -> Option<Box<dyn Editor>> {
create_vizia_editor(editor_state, ViziaTheming::Custom, move |cx, _| { create_vizia_editor(editor_state, ViziaTheming::Custom, move |cx, _| {
assets::register_noto_sans_light(cx); assets::register_noto_sans_light(cx);
assets::register_noto_sans_thin(cx); assets::register_noto_sans_thin(cx);
Data { editor_data.clone().build(cx);
params: params.clone(),
editor_mode: params.editor_mode.clone(),
}
.build(cx);
ResizeHandle::new(cx); ResizeHandle::new(cx);

View file

@ -15,12 +15,14 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
use analyzer::AnalyzerData; use analyzer::AnalyzerData;
use atomic_float::AtomicF32;
use crossbeam::atomic::AtomicCell; use crossbeam::atomic::AtomicCell;
use editor::EditorMode; use editor::EditorMode;
use nih_plug::prelude::*; use nih_plug::prelude::*;
use nih_plug_vizia::ViziaState; use nih_plug_vizia::ViziaState;
use realfft::num_complex::Complex32; use realfft::num_complex::Complex32;
use realfft::{ComplexToReal, RealFftPlanner, RealToComplex}; use realfft::{ComplexToReal, RealFftPlanner, RealToComplex};
use std::sync::atomic::Ordering;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use triple_buffer::TripleBuffer; use triple_buffer::TripleBuffer;
@ -54,6 +56,9 @@ pub struct SpectralCompressor {
/// The current buffer config, used for updating the compressors. /// The current buffer config, used for updating the compressors.
buffer_config: BufferConfig, buffer_config: BufferConfig,
/// The current sample rate. Stores the same information as in `BufferConfig`, but this can be
/// shared with the editor where it's used to compute frequencies for the spectrum analyzer.
sample_rate: Arc<AtomicF32>,
/// An adapter that performs most of the overlap-add algorithm for us. /// An adapter that performs most of the overlap-add algorithm for us.
stft: util::StftHelper<1>, stft: util::StftHelper<1>,
@ -166,6 +171,7 @@ impl Default for SpectralCompressor {
max_buffer_size: 0, max_buffer_size: 0,
process_mode: ProcessMode::Realtime, process_mode: ProcessMode::Realtime,
}, },
sample_rate: Arc::new(AtomicF32::new(1.0)),
// These three will be set to the correct values in the initialize function // These three will be set to the correct values in the initialize function
stft: util::StftHelper::new(2, MAX_WINDOW_SIZE, 0), stft: util::StftHelper::new(2, MAX_WINDOW_SIZE, 0),
@ -311,7 +317,17 @@ impl Plugin for SpectralCompressor {
} }
fn editor(&self, _async_executor: AsyncExecutor<Self>) -> Option<Box<dyn Editor>> { fn editor(&self, _async_executor: AsyncExecutor<Self>) -> Option<Box<dyn Editor>> {
editor::create(self.params.clone(), self.params.editor_state.clone()) editor::create(
self.params.editor_state.clone(),
editor::Data {
params: self.params.clone(),
editor_mode: self.params.editor_mode.clone(),
analyzer_data: self.analyzer_output_data.clone(),
sample_rate: self.sample_rate.clone(),
},
)
} }
fn initialize( fn initialize(
@ -323,6 +339,10 @@ impl Plugin for SpectralCompressor {
// Needed to update the compressors later // Needed to update the compressors later
self.buffer_config = *buffer_config; self.buffer_config = *buffer_config;
// And this is used in the editor to draw the analyzer
self.sample_rate
.store(buffer_config.sample_rate, Ordering::Relaxed);
// This plugin can accept a variable number of audio channels, so we need to resize // This plugin can accept a variable number of audio channels, so we need to resize
// channel-dependent data structures accordingly // channel-dependent data structures accordingly
let num_output_channels = audio_io_layout let num_output_channels = audio_io_layout