From cd2bbafcb6c6e7b418ab5451ef9dcebe08ef416f Mon Sep 17 00:00:00 2001 From: Weird Constructor Date: Thu, 27 May 2021 20:14:43 +0200 Subject: [PATCH] handling sample loading errors gracefully --- src/matrix.rs | 4 ++++ src/nodes/node_conf.rs | 21 +++++++++++++++++++-- tests/basics.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/matrix.rs b/src/matrix.rs index 1be5469..75db120 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -597,6 +597,10 @@ impl Matrix { } } + pub fn pop_error(&mut self) -> Option { + self.config.pop_error() + } + /// Assign [SAtom] values to input parameters and atoms. pub fn set_param(&mut self, param: ParamId, at: SAtom) { self.config.set_param(param, at); diff --git a/src/nodes/node_conf.rs b/src/nodes/node_conf.rs index 5c40144..9408779 100644 --- a/src/nodes/node_conf.rs +++ b/src/nodes/node_conf.rs @@ -147,6 +147,9 @@ pub struct NodeConfigurator { /// for nodes. sample_lib: SampleLibrary, + /// Error messages: + errors: Vec, + /// Contains (automateable) parameters params: std::collections::HashMap, /// Stores the most recently set parameter values @@ -231,6 +234,7 @@ impl NodeConfigurator { (NodeConfigurator { nodes, shared, + errors: vec![], sample_lib: SampleLibrary::new(), feedback_filter: FeedbackFilter::new(), output_fb_values: vec![], @@ -277,6 +281,10 @@ impl NodeConfigurator { } } + pub fn pop_error(&mut self) -> Option { + self.errors.pop() + } + pub fn unique_index_for(&self, ni: &NodeId) -> Option { self.node2idx.get(&ni).copied() } @@ -299,8 +307,17 @@ impl NodeConfigurator { pub fn set_param(&mut self, param: ParamId, at: SAtom) { if param.is_atom() { let at = - if let SAtom::AudioSample((path, None)) = at { - self.sample_lib.load(&path).unwrap().clone() + if let SAtom::AudioSample((path, None)) = at.clone() { + match self.sample_lib.load(&path) { + Ok(sample) => sample.clone(), + Err(e) => { + self.errors.push( + format!( + "Couldn't load sample '{}': {:?}", + path, e)); + at + }, + } } else { at }; diff --git a/tests/basics.rs b/tests/basics.rs index 6515d44..0f88315 100644 --- a/tests/basics.rs +++ b/tests/basics.rs @@ -1199,3 +1199,29 @@ fn check_node_sampl_reload() { assert_eq!(fft[0], (441, 940)); } } + +#[test] +fn check_node_sampl_load_err() { + let (node_conf, mut node_exec) = new_node_engine(); + let mut matrix = Matrix::new(node_conf, 3, 3); + + let smpl = NodeId::Sampl(0); + let out = NodeId::Out(0); + matrix.place(0, 0, Cell::empty(smpl) + .out(None, None, smpl.out("sig"))); + matrix.place(0, 1, Cell::empty(out) + .input(out.inp("ch1"), None, None)); + matrix.sync().unwrap(); + + let sample_p = smpl.inp_param("sample").unwrap(); + let freq_p = smpl.inp_param("freq").unwrap(); + matrix.set_param(sample_p, SAtom::audio_unloaded("tests/sample_NOSIN.wav")); + + let (rms, min, max) = run_and_get_l_rms_mimax(&mut node_exec, 50.0); + assert_float_eq!(rms, 0.0); + assert_float_eq!(min, 0.0); + assert_float_eq!(max, 0.0); + + let err = matrix.pop_error(); + assert_eq!(err.unwrap(), "Couldn't load sample 'tests/sample_NOSIN.wav': LoadError(IoError(Os { code: 2, kind: NotFound, message: \"No such file or directory\" }))"); +}