diff options
Diffstat (limited to 'src/synth/noise.rs')
-rw-r--r-- | src/synth/noise.rs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/synth/noise.rs b/src/synth/noise.rs index 4d90f3b..68c120f 100644 --- a/src/synth/noise.rs +++ b/src/synth/noise.rs @@ -1,7 +1,8 @@ use std::mem; use super::*; -use ::rand::{XorShiftRng, Rng}; +use ::rand::{XorShiftRng, Rng, SeedableRng}; +use ::rand::os::OsRng; #[derive(Debug)] pub struct Noise { @@ -19,8 +20,32 @@ impl Generator for Noise { &self.buf } + fn buffer<'a>(&'a self) -> &'a SampleBuffer { &self.buf } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } } +static mut _rand_gen: Option<OsRng> = None; + +pub struct NoiseFactory; + +impl GeneratorFactory for NoiseFactory { + fn new(&self, params: &mut FactoryParameters) -> Result<GenBox, GenFactoryError> { + if unsafe { &_rand_gen }.is_none() { + unsafe {_rand_gen = Some(OsRng::new().expect("Couldn't initialize OS random")); } + } + + let mut seed: [u32; 4] = Default::default(); + for i in seed.iter_mut() { + *i = unsafe { &mut _rand_gen }.as_mut().unwrap().next_u32(); + } + + Ok(Box::new(Noise { + rng: XorShiftRng::from_seed(seed), + buf: SampleBuffer::new(params.env.default_buffer_size), + })) + } +} + +pub static Factory: NoiseFactory = NoiseFactory; |