pub trait Source<MessageData = ()>: Send {
// Required methods
fn sample(
&mut self,
tracker: &mut dyn Tracker<MessageData>,
sample_rate: usize
);
fn get_source_id(&self) -> &usize;
}
Expand description
To implement Source
means something can be a source of sound/voltage,
Two things are required by the trait:
- keeping track of what the source is using an unique
usize
id - using the sample method to fill one or multiple
Signal
buffers with f32 data.
A very basic example might be to implement a DC offset module
use screech::traits::{Tracker, Source};
use screech::{Screech, Input, Output};
struct Offset {
id: usize,
input: Input,
output: Output,
offset: f32,
}
impl Offset {
fn new(screech: &mut Screech, offset: f32) -> Self {
// obtain a unique identifier from the main sreech instance
let id = screech.create_source_id();
Offset {
id,
// initialize a new input to keep track of input connected to our source
input: screech.init_input(&id, "signal_in"),
// initialize a new output [`Signal`] buffer for our id and name
output: screech.init_output(&id, "signal_out"),
offset,
}
}
}
impl Source for Offset {
fn sample(&mut self, tracker: &mut dyn Tracker, sample_rate: usize) {
for i in 0..*tracker.get_buffer_size() {
// set offset as initial value
let mut signal = self.offset;
// add all inputs to the signal
for input in tracker.get_input(&self.input).unwrap().into_iter() {
if let Some(s) = tracker.get_output(&input).and_then(|o| o.samples.get(i)) {
signal += s;
}
}
// add signal to the final output
let output = tracker.get_mut_output(&self.output).unwrap();
output.samples[i] = signal;
}
}
fn get_source_id(&self) -> &usize {
&self.id
}
}
Required Methods§
sourcefn sample(&mut self, tracker: &mut dyn Tracker<MessageData>, sample_rate: usize)
fn sample(&mut self, tracker: &mut dyn Tracker<MessageData>, sample_rate: usize)
function that gets called by crate::Screech
during sampling.
use the reference to the tracker to update relevant Signal
s
sourcefn get_source_id(&self) -> &usize
fn get_source_id(&self) -> &usize
get reference to the id for the source,
this is used to uniquely identify this source when sampling Signal
s