Skip to content

Commit 8c903a8

Browse files
committed
Return error instead of panicing when we get a parameter event with an invalid ID
1 parent 3a83a7a commit 8c903a8

3 files changed

Lines changed: 32 additions & 9 deletions

File tree

examples/gain-plugin/src/plugin.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io::{Read, Write};
44
use std::rc::Rc;
55

66
use plinth_plugin::error::Error;
7-
use plinth_plugin::{export_clap, export_vst3, Event, Host, HostInfo, Parameters, Plugin, ProcessorConfig};
7+
use plinth_plugin::{Event, Host, HostInfo, Parameters, Plugin, ProcessorConfig, export_clap, export_vst3, tracing};
88
use plinth_plugin::clap::ClapPlugin;
99
use plinth_plugin::vst3::Vst3Plugin;
1010

@@ -38,7 +38,12 @@ impl Plugin for GainPlugin {
3838
}
3939

4040
fn process_event(&mut self, event: &Event) {
41-
self.parameters.process_event(event);
41+
match self.parameters.process_event(event) {
42+
Ok(_) => {},
43+
Err(e) => {
44+
tracing::error!("Error processing event: {e:?}");
45+
},
46+
}
4247
}
4348

4449
fn create_processor(&self, _config: ProcessorConfig) -> Self::Processor {

examples/gain-plugin/src/processor.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use plinth_plugin::{plinth_core::signals::signal::{Signal, SignalMut}, Event, FloatParameter, Parameters, ProcessState, Processor, Transport};
1+
use plinth_plugin::{Event, FloatParameter, Parameters, ProcessState, Processor, Transport, plinth_core::signals::signal::{Signal, SignalMut}, tracing};
22

33
use crate::parameters::{GainParameter, GainParameters};
44

@@ -30,7 +30,12 @@ impl Processor for GainPluginProcessor {
3030
events: impl Iterator<Item = Event>
3131
) -> ProcessState {
3232
for event in events {
33-
self.parameters.process_event(&event);
33+
match self.parameters.process_event(&event) {
34+
Ok(_) => {},
35+
Err(e) => {
36+
tracing::error!("Error processing event: {e:?}");
37+
},
38+
}
3439
}
3540

3641
let gain_db = self.parameters.value::<FloatParameter>(GainParameter::Gain);
@@ -40,14 +45,19 @@ impl Processor for GainPluginProcessor {
4045
for sample in channel.iter_mut() {
4146
*sample *= gain;
4247
}
43-
}
48+
}
4449

4550
ProcessState::Normal
4651
}
4752

4853
fn process_events(&mut self, events: impl Iterator<Item = Event>) {
4954
for event in events {
50-
self.parameters.process_event(&event);
55+
match self.parameters.process_event(&event) {
56+
Ok(_) => {},
57+
Err(e) => {
58+
tracing::error!("Error processing event: {e:?}");
59+
},
60+
}
5161
}
5262
}
5363
}

plinth-plugin/src/parameters.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,28 @@ pub trait Parameters {
5050
self.typed::<T>(id).unwrap().modulated_plain()
5151
}
5252

53-
fn process_event(&self, event: &Event) {
53+
fn process_event(&self, event: &Event) -> Result<(), Error> {
5454
match event {
5555
Event::ParameterValue { id, value, .. } => {
56-
let parameter = self.get(*id).unwrap_or_else(|| panic!("Tried to get parameter with id {id} but it doesn't exist"));
56+
let Some(parameter) = self.get(*id) else {
57+
return Err(Error::ParameterIdError(*id));
58+
};
59+
5760
parameter.set_normalized_value(*value).unwrap();
5861
},
5962

6063
Event::ParameterModulation { id, amount, .. } => {
61-
let parameter = self.get(*id).unwrap_or_else(|| panic!("Tried to get parameter with id {id} but it doesn't exist"));
64+
let Some(parameter) = self.get(*id) else {
65+
return Err(Error::ParameterIdError(*id));
66+
};
67+
6268
parameter.set_normalized_modulation(*amount);
6369
},
6470

6571
_ => {},
6672
}
73+
74+
Ok(())
6775
}
6876

6977
fn reset(&self) {

0 commit comments

Comments
 (0)