Skip to content

Commit

Permalink
rename Processor to Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed Nov 29, 2024
1 parent b09d51b commit 5f9fb4c
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 77 deletions.
16 changes: 7 additions & 9 deletions examples/gain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use serde::{Deserialize, Serialize};

use coupler::format::clap::*;
use coupler::format::vst3::*;
use coupler::{
buffers::*, bus::*, editor::*, events::*, host::*, params::*, plugin::*, process::*,
};
use coupler::{buffers::*, bus::*, editor::*, engine::*, events::*, host::*, params::*, plugin::*};

use coupler_reflector::EditorWindow;

Expand All @@ -27,7 +25,7 @@ pub struct Gain {
}

impl Plugin for Gain {
type Processor = GainProcessor;
type Engine = GainEngine;
type Editor = EditorWindow;

fn info() -> PluginInfo {
Expand Down Expand Up @@ -80,8 +78,8 @@ impl Plugin for Gain {
Ok(())
}

fn processor(&mut self, _config: Config) -> Self::Processor {
GainProcessor {
fn engine(&mut self, _config: Config) -> Self::Engine {
GainEngine {
params: self.params.clone(),
}
}
Expand Down Expand Up @@ -112,19 +110,19 @@ impl ClapPlugin for Gain {
}
}

pub struct GainProcessor {
pub struct GainEngine {
params: GainParams,
}

impl GainProcessor {
impl GainEngine {
fn handle_event(&mut self, event: &Event) {
if let Data::ParamChange { id, value } = event.data {
self.params.set_param(id, value);
}
}
}

impl Processor for GainProcessor {
impl Engine for GainEngine {
fn reset(&mut self) {}

fn flush(&mut self, events: Events) {
Expand Down
2 changes: 1 addition & 1 deletion src/process.rs → src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Config {
pub max_buffer_size: usize,
}

pub trait Processor: Send + Sized + 'static {
pub trait Engine: Send + Sized + 'static {
fn reset(&mut self);
fn flush(&mut self, events: Events);
fn process(&mut self, buffers: Buffers, events: Events);
Expand Down
54 changes: 27 additions & 27 deletions src/format/clap/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use super::host::ClapHost;
use crate::buffers::{BufferData, BufferType, Buffers};
use crate::bus::{BusDir, Format};
use crate::editor::Editor;
use crate::engine::{Config, Engine};
use crate::events::{Data, Event, Events};
use crate::host::Host;
use crate::params::{ParamId, ParamInfo, ParamValue};
use crate::plugin::{Plugin, PluginInfo};
use crate::process::{Config, Processor};
use crate::sync::param_gestures::{GestureStates, GestureUpdate, ParamGestures};
use crate::sync::params::ParamValues;
use crate::util::{copy_cstring, slice_from_raw_parts_checked, DisplayParam};
Expand Down Expand Up @@ -57,7 +57,7 @@ pub struct ProcessState<P: Plugin> {
buffer_data: Vec<BufferData>,
buffer_ptrs: Vec<*mut f32>,
events: Vec<Event>,
processor: Option<P::Processor>,
engine: Option<P::Engine>,
}

#[repr(C)]
Expand All @@ -69,10 +69,10 @@ pub struct Instance<P: Plugin> {
pub input_bus_map: Vec<usize>,
pub output_bus_map: Vec<usize>,
pub param_map: Arc<HashMap<ParamId, usize>>,
// Processor -> plugin parameter changes
// Engine -> plugin parameter changes
pub plugin_params: ParamValues,
// Plugin -> processor parameter changes
pub processor_params: ParamValues,
// Plugin -> engine parameter changes
pub engine_params: ParamValues,
pub param_gestures: Arc<ParamGestures>,
pub main_thread_state: UnsafeCell<MainThreadState<P>>,
pub process_state: UnsafeCell<ProcessState<P>>,
Expand Down Expand Up @@ -125,7 +125,7 @@ impl<P: Plugin> Instance<P> {
output_bus_map,
param_map: Arc::new(param_map),
plugin_params: ParamValues::with_count(info.params.len()),
processor_params: ParamValues::with_count(info.params.len()),
engine_params: ParamValues::with_count(info.params.len()),
param_gestures: Arc::new(ParamGestures::with_count(info.params.len())),
main_thread_state: UnsafeCell::new(MainThreadState {
host_params: None,
Expand All @@ -138,7 +138,7 @@ impl<P: Plugin> Instance<P> {
buffer_data: Vec::new(),
buffer_ptrs: Vec::new(),
events: Vec::with_capacity(4096),
processor: None,
engine: None,
}),
}
}
Expand All @@ -154,8 +154,8 @@ impl<P: Plugin> Instance<P> {
}
}

fn sync_processor(&self, events: &mut Vec<Event>) {
for (index, value) in self.processor_params.poll() {
fn sync_engine(&self, events: &mut Vec<Event>) {
for (index, value) in self.engine_params.poll() {
events.push(Event {
time: 0,
data: Data::ParamChange {
Expand Down Expand Up @@ -357,11 +357,11 @@ impl<P: Plugin> Instance<P> {
max_buffer_size: max_frames_count as usize,
};

// Discard any pending plugin -> processor parameter changes, since they will already be
// reflected in the initial state of the processor.
for _ in instance.processor_params.poll() {}
// Discard any pending plugin -> engine parameter changes, since they will already be
// reflected in the initial state of the engine.
for _ in instance.engine_params.poll() {}

process_state.processor = Some(main_thread_state.plugin.processor(config));
process_state.engine = Some(main_thread_state.plugin.engine(config));

true
}
Expand All @@ -371,11 +371,11 @@ impl<P: Plugin> Instance<P> {
let main_thread_state = &mut *instance.main_thread_state.get();
let process_state = &mut *instance.process_state.get();

// Apply any remaining processor -> plugin parameter changes. There won't be any more until
// Apply any remaining engine -> plugin parameter changes. There won't be any more until
// the next call to `activate`.
instance.sync_plugin(main_thread_state);

process_state.processor = None;
process_state.engine = None;
}

unsafe extern "C" fn start_processing(_plugin: *const clap_plugin) -> bool {
Expand All @@ -388,16 +388,16 @@ impl<P: Plugin> Instance<P> {
let instance = &*(plugin as *const Self);
let process_state = &mut *instance.process_state.get();

if let Some(processor) = &mut process_state.processor {
// Flush plugin -> processor parameter changes
if let Some(engine) = &mut process_state.engine {
// Flush plugin -> engine parameter changes
process_state.events.clear();
instance.sync_processor(&mut process_state.events);
instance.sync_engine(&mut process_state.events);

if !process_state.events.is_empty() {
processor.flush(Events::new(&process_state.events));
engine.flush(Events::new(&process_state.events));
}

processor.reset();
engine.reset();
}
}

Expand All @@ -408,7 +408,7 @@ impl<P: Plugin> Instance<P> {
let instance = &*(plugin as *const Self);
let process_state = &mut *instance.process_state.get();

let Some(processor) = &mut process_state.processor else {
let Some(engine) = &mut process_state.engine else {
return CLAP_PROCESS_ERROR;
};

Expand Down Expand Up @@ -471,7 +471,7 @@ impl<P: Plugin> Instance<P> {
}

process_state.events.clear();
instance.sync_processor(&mut process_state.events);
instance.sync_engine(&mut process_state.events);
instance.process_param_events(process.in_events, &mut process_state.events);

let last_sample = process.frames_count.saturating_sub(1);
Expand All @@ -482,7 +482,7 @@ impl<P: Plugin> Instance<P> {
last_sample,
);

processor.process(
engine.process(
Buffers::from_raw_parts(
&process_state.buffer_data,
&process_state.buffer_ptrs,
Expand Down Expand Up @@ -800,9 +800,9 @@ impl<P: Plugin> Instance<P> {
let process_state = &mut *instance.process_state.get();

// If we are in the active state, flush will be called on the audio thread.
if let Some(processor) = &mut process_state.processor {
if let Some(engine) = &mut process_state.engine {
process_state.events.clear();
instance.sync_processor(&mut process_state.events);
instance.sync_engine(&mut process_state.events);
instance.process_param_events(in_, &mut process_state.events);
instance.process_gestures(
&mut process_state.gesture_states,
Expand All @@ -811,7 +811,7 @@ impl<P: Plugin> Instance<P> {
0,
);

processor.flush(Events::new(&process_state.events));
engine.flush(Events::new(&process_state.events));
}
// Otherwise, flush will be called on the main thread.
else {
Expand Down Expand Up @@ -933,7 +933,7 @@ impl<P: Plugin> Instance<P> {
if main_thread_state.plugin.load(&mut StreamReader(stream)).is_ok() {
for (index, param) in instance.info.params.iter().enumerate() {
let value = main_thread_state.plugin.get_param(param.id);
instance.processor_params.set(index, value);
instance.engine_params.set(index, value);

if let Some(editor) = &mut main_thread_state.editor {
editor.param_changed(param.id, value);
Expand Down
12 changes: 6 additions & 6 deletions src/format/clap/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use crate::events::Events;
use clap_sys::plugin_factory::{clap_plugin_factory, CLAP_PLUGIN_FACTORY_ID};
use clap_sys::version::CLAP_VERSION;

use crate::engine::{Config, Engine};
use crate::host::Host;
use crate::params::{ParamId, ParamValue};
use crate::plugin::{Plugin, PluginInfo};
use crate::process::{Config, Processor};

use super::{ClapInfo, ClapPlugin, Factory};

Expand All @@ -25,7 +25,7 @@ const ID: &str = "com.example.plugin";
struct TestPlugin;

impl Plugin for TestPlugin {
type Processor = TestProcessor;
type Engine = TestEngine;
type Editor = TestEditor;

fn info() -> PluginInfo {
Expand Down Expand Up @@ -54,8 +54,8 @@ impl Plugin for TestPlugin {
fn load(&mut self, _input: &mut impl Read) -> io::Result<()> {
Ok(())
}
fn processor(&mut self, _config: Config) -> Self::Processor {
TestProcessor
fn engine(&mut self, _config: Config) -> Self::Engine {
TestEngine
}
fn editor(&mut self, _host: EditorHost, _parent: &ParentWindow) -> Self::Editor {
TestEditor
Expand All @@ -73,9 +73,9 @@ impl ClapPlugin for TestPlugin {
}
}

struct TestProcessor;
struct TestEngine;

impl Processor for TestProcessor {
impl Engine for TestEngine {
fn reset(&mut self) {}
fn flush(&mut self, _events: Events) {}
fn process(&mut self, _buffers: Buffers, _events: Events) {}
Expand Down
6 changes: 3 additions & 3 deletions src/format/vst3/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use vst3::Steinberg::Vst::ProcessData;

use crate::buffers::{BufferData, BufferType, Buffers};
use crate::bus::{BusDir, BusInfo};
use crate::process::Config;
use crate::engine::Config;
use crate::util::slice_from_raw_parts_checked;

pub struct ScratchBuffers {
Expand Down Expand Up @@ -87,7 +87,7 @@ impl ScratchBuffers {
self.moves.reserve(in_out_channels);
}

/// Set up buffer pointers for the processor given a VST3 `ProcessData` struct.
/// Set up buffer pointers for the engine given a VST3 `ProcessData` struct.
///
/// This method is responsible for detecting if any of the buffers for an input bus are aliased
/// by a output buffer and, if so, copying those inputs to scratch buffers. It is also
Expand All @@ -98,7 +98,7 @@ impl ScratchBuffers {
/// the buffer's length exceeds the maximum buffer size. It will return `Ok(None)` if the
/// buffer's length is 0, as hosts are not guaranteed to provide the correct number of inputs
/// and outputs in that case, and we don't need to construct a `Buffers` object as we will be
/// calling `Processor::flush` instead of `Processor::process`.
/// calling `Engine::flush` instead of `Engine::process`.
pub unsafe fn get_buffers(
&mut self,
buses: &[BusInfo],
Expand Down
Loading

0 comments on commit 5f9fb4c

Please sign in to comment.