Skip to content

[help] how to control const_hz or hz? #200

@lukassup

Description

@lukassup

Hello,

Thank you for this project.
So far I've had quite a bit of fun and figuring out how things works (which is appreciated a lot)

I am trying to create a simple synth that I want to control the pitch of using an external parameter - let's say keyboard, or GUI, or MIDI, etc...

At the current moment I am a bit stuck how to provide pitch as input control for my oscillator - could you help me guide in the right direction?

fn run<T: SizedSample + FromSample<f32>>(
    device: &Device,
    config: &StreamConfig,
) -> anyhow::Result<()> {
    let one_sec = config.sample_rate as usize;
    let rate = signal::rate(config.sample_rate as f64);

    let mut synth = rate
        .const_hz(440.)
        .sine()
        .take(5 * one_sec)
        .map(|s| s.to_sample::<f32>() * 0.2);

    let (complete_tx, complete_rx) = mpsc::sync_channel::<()>(1);
    let err_fn = |err| eprintln!("an error occured on stream: {}!", err);
    let channels = config.channels as usize;
    let stream = device.build_output_stream(
        config,
        move |data: &mut [T], _| write_data(data, channels, &complete_tx, &mut synth),
        err_fn,
        None,
    )?;
    stream.play()?;
    complete_rx.recv().unwrap();
    stream.pause()?;
    Ok(())
}

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions