Struct screech::basic::Oscillator [−][src]
pub struct Oscillator {
pub output: ExternalSignal,
pub frequency: f32,
pub amplitude: f32,
// some fields omitted
}
Expand description
Basic saw ramp oscillator.
use screech::core::{Primary, DynamicTracker};
use screech::basic::Oscillator;
const BUFFER_SIZE: usize = 16;
let sample_rate = 4;
let mut primary = Primary::<BUFFER_SIZE>::new(sample_rate);
let mut oscillator = Oscillator::new(&mut primary);
oscillator.frequency = 1.0;
oscillator.amplitude = 0.5;
primary.add_monitor(oscillator.output);
primary.output_mono();
assert_eq!(
primary.sample(vec![&mut oscillator]).unwrap(),
&[
0.0, 0.25, 0.5, -0.25,
0.0, 0.25, 0.5, -0.25,
0.0, 0.25, 0.5, -0.25,
0.0, 0.25, 0.5, -0.25,
],
);
Fields
output: ExternalSignal
main audio output
frequency: f32
oscillator frequency per second
amplitude: f32
amplitude peak to peak centered around 0.0
for example an amplitude of 0.5
will generate a saw
wave between -0.5
and 0.5
Implementations
Create a new saw oscillator with a default
frequency of 1.0
and an amplitute of 0.5
.
Set the main output to triangle
use screech::core::Primary;
use screech::basic::Oscillator;
const BUFFER_SIZE: usize = 16;
let sample_rate = 4;
let mut primary = Primary::<BUFFER_SIZE>::new(sample_rate);
let mut oscillator = Oscillator::new(&mut primary);
oscillator.frequency = 0.5;
oscillator.amplitude = 1.0;
oscillator.output_triangle();
primary.add_monitor(oscillator.output);
primary.output_mono();
assert_eq!(
primary.sample(vec![&mut oscillator]).unwrap(),
&[
-1.0, -0.5, 0.0, 0.5,
1.0, 0.5, 0.0, -0.5,
-1.0, -0.5, 0.0, 0.5,
1.0, 0.5, 0.0, -0.5,
],
);
Set the main output to square
with a duty cycle between 0.0
(0%) and 1.0
(100%).
use screech::core::Primary;
use screech::basic::Oscillator;
const BUFFER_SIZE: usize = 4;
let sample_rate = 4;
let mut primary = Primary::<BUFFER_SIZE>::new(sample_rate);
let mut oscillator = Oscillator::new(&mut primary);
oscillator.frequency = 1.0;
oscillator.amplitude = 1.0;
// 25% duty cycle
oscillator.output_square(0.25);
primary.add_monitor(oscillator.output);
primary.output_mono();
assert_eq!(
primary.sample(vec![&mut oscillator]).unwrap(),
&[-1.0, -1.0, -1.0, 1.0],
);
Set the main output to saw
use screech::core::Primary;
use screech::basic::Oscillator;
const BUFFER_SIZE: usize = 8;
let sample_rate = 4;
let mut primary = Primary::<BUFFER_SIZE>::new(sample_rate);
let mut oscillator = Oscillator::new(&mut primary);
oscillator.frequency = 0.5;
oscillator.amplitude = 1.0;
oscillator.output_saw();
primary.add_monitor(oscillator.output);
primary.output_mono();
assert_eq!(
primary.sample(vec![&mut oscillator]).unwrap(),
&[
0.0, 0.25, 0.5, 0.75,
1.0, -0.75, -0.5, -0.25,
],
);