From ed5d8f9cea18da71ee8b5469354744b90b968301 Mon Sep 17 00:00:00 2001 From: Weird Constructor Date: Mon, 18 Jul 2022 18:48:25 +0200 Subject: [PATCH] limit loaded sample length to 1 minute --- src/sample_lib.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/sample_lib.rs b/src/sample_lib.rs index 310d8ba..92c10e1 100644 --- a/src/sample_lib.rs +++ b/src/sample_lib.rs @@ -19,15 +19,18 @@ impl From for SampleLoadError { } } +const MAX_SAMPLE_LEN_S: usize = 60; // 60 seconds of audio is about 20MB + /// Loads and stores samples, for use as SAtom parameters for /// nodes. pub struct SampleLibrary { loaded_samples: HashMap, + max_length_s: usize, } impl SampleLibrary { pub fn new() -> Self { - Self { loaded_samples: HashMap::new() } + Self { loaded_samples: HashMap::new(), max_length_s: MAX_SAMPLE_LEN_S } } /// Synchronous/blocking loading of a sample from `path`. @@ -49,15 +52,17 @@ impl SampleLibrary { let mut v = vec![rd.spec().sample_rate as f32]; + let max_sample_count = self.max_length_s * rd.spec().sample_rate as usize; + match rd.spec().sample_format { hound::SampleFormat::Float => { - for s in rd.samples::().step_by(channels) { + for s in rd.samples::().step_by(channels).take(max_sample_count) { v.push(s?); } } // http://blog.bjornroche.com/2009/12/int-float-int-its-jungle-out-there.html hound::SampleFormat::Int => { - for s in rd.samples::().step_by(channels) { + for s in rd.samples::().step_by(channels).take(max_sample_count) { let s = s?; let s = s as f32 / (0x8000 as f32); v.push(s);