Skip to content

Commit

Permalink
Add noise module
Browse files Browse the repository at this point in the history
  • Loading branch information
sharph committed Sep 28, 2024
1 parent 123761a commit 6e00af0
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/synth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ impl TransitionDetector {
pub enum SynthModuleType {
OutputModuleV0(output::OutputModule),
OscillatorModuleV0(oscillator::OscillatorModule),
NoiseModuleV0(oscillator::NoiseModule),
GridSequencerModuleV0(sequencer::GridSequencerModuleV0),
GridSequencerModuleV1(sequencer::GridSequencerModule),
PatternSequencerModuleV0(sequencer::PatternSequencerModule),
Expand All @@ -326,6 +327,7 @@ pub fn enum_to_sharedsynthmodule(synthmoduleenum: SynthModuleType) -> SharedSynt
match synthmoduleenum {
SynthModuleType::OutputModuleV0(m) => Arc::new(RwLock::new(m)),
SynthModuleType::OscillatorModuleV0(m) => Arc::new(RwLock::new(m)),
SynthModuleType::NoiseModuleV0(m) => Arc::new(RwLock::new(m)),
SynthModuleType::GridSequencerModuleV0(m) => {
Arc::new(RwLock::new(sequencer::GridSequencerModule::from(m)))
}
Expand Down Expand Up @@ -354,6 +356,11 @@ pub fn any_module_to_enum(module: Box<&dyn SynthModule>) -> Result<SynthModuleTy
&module,
)));
}
if let Some(module) = module.downcast_ref::<oscillator::NoiseModule>() {
return Ok(SynthModuleType::NoiseModuleV0(prep_for_serialization(
&module,
)));
}
if let Some(module) = module.downcast_ref::<sequencer::GridSequencerModule>() {
return Ok(SynthModuleType::GridSequencerModuleV1(
prep_for_serialization(&module),
Expand Down Expand Up @@ -410,6 +417,12 @@ pub fn get_catalog() -> Vec<(String, Box<dyn Fn(&AudioConfig) -> SharedSynthModu
Arc::new(RwLock::new(oscillator::OscillatorModule::new(audio_config)))
}),
),
(
oscillator::NoiseModule::get_name(),
Box::new(|audio_config| {
Arc::new(RwLock::new(oscillator::NoiseModule::new(audio_config)))
}),
),
(
sequencer::GridSequencerModule::get_name(),
Box::new(|audio_config| {
Expand Down
91 changes: 91 additions & 0 deletions src/synth/oscillator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,94 @@ mod dco_tests {
assert!((buf[0] - 1.0).abs() < 0.00001); // should continue smoothly into next buffer
}
}

#[derive(Serialize, Deserialize, Clone)]
pub struct NoiseModule {
id: String,
out: AudioBuffer,
}

impl NoiseModule {
pub fn new(audio_config: &AudioConfig) -> Self {
Self {
id: uuid::Uuid::new_v4().into(),
out: AudioBuffer::new(Some(audio_config.buffer_size)),
}
}

pub fn get_name() -> String {
"Noise".to_string()
}
}

impl SynthModule for NoiseModule {
fn get_name(&self) -> String {
Self::get_name()
}

fn get_id(&self) -> String {
self.id.clone()
}

fn set_audio_config(&mut self, audio_config: &AudioConfig) {
self.out.resize(audio_config.buffer_size);
}

fn get_input(&self, _input_idx: u8) -> Result<Option<(SharedSynthModule, u8)>, ()> {
Err(())
}

fn get_num_inputs(&self) -> u8 {
0
}

fn get_input_label(&self, _input_idx: u8) -> Result<Option<String>, ()> {
Err(())
}

fn set_input(
&mut self,
_input_idx: u8,
_src_module: SharedSynthModule,
_src_port: u8,
) -> Result<(), ()> {
Err(())
}

fn get_num_outputs(&self) -> u8 {
1
}

fn get_output(&self, output_idx: u8) -> Result<AudioBuffer, ()> {
if output_idx == 0 {
Ok(self.out.clone())
} else {
Err(())
}
}

fn get_output_label(&self, output_idx: u8) -> Result<Option<String>, ()> {
if output_idx == 0 {
Ok(None)
} else {
Err(())
}
}

fn disconnect_input(&mut self, _input_idx: u8) -> Result<(), ()> {
Err(())
}

fn calc(&mut self) {
self.out.with_write(|out| {
let out = out.unwrap();
for sample in out.iter_mut() {
*sample = (rand::random::<f32>() - 0.5) * 2.0;
}
});
}

fn as_any(&self) -> &dyn Any {
return self;
}
}

0 comments on commit 6e00af0

Please sign in to comment.