Struct screech::basic::Oscillator[][src]

pub struct Oscillator {
    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);

assert_eq!(
    primary.sample(vec![&mut oscillator]).unwrap(),
    vec![
        0.0, 0.0, 0.25, 0.25, 0.5, 0.5, -0.25, -0.25,
        0.0, 0.0, 0.25, 0.25, 0.5, 0.5, -0.25, -0.25,
        0.0, 0.0, 0.25, 0.25, 0.5, 0.5, -0.25, -0.25,
        0.0, 0.0, 0.25, 0.25, 0.5, 0.5, -0.25, -0.25,
    ],
);

Fields

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);

assert_eq!(
    primary.sample(vec![&mut oscillator]).unwrap(),
    vec![
        -1.0, -1.0, -0.5, -0.5, 0.0, 0.0, 0.5, 0.5,
        1.0, 1.0, 0.5, 0.5, 0.0, 0.0, -0.5, -0.5,
        -1.0, -1.0, -0.5, -0.5, 0.0, 0.0, 0.5, 0.5,
        1.0, 1.0, 0.5, 0.5, 0.0, 0.0, -0.5, -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);

assert_eq!(
         primary.sample(vec![&mut oscillator]).unwrap(),
        vec![-1.0, -1.0, -1.0, -1.0, -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);

assert_eq!(
        primary.sample(vec![&mut oscillator]).unwrap(),
        vec![
             0.0, 0.0,  0.25,  0.25,  0.5,  0.5,  0.75,  0.75,
             1.0, 1.0, -0.75, -0.75, -0.5, -0.5, -0.25, -0.25,
        ],
);

Render the next real time signal

Trait Implementations

move one buffersize forward in discrete time

get id for instance, this is to identify this source when building the output

Get a list of sources

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.