Skip to content

Commit

Permalink
Rename Superseconds and Superbeats
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikNatanael committed Jan 11, 2024
1 parent 39c7065 commit a891927
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 205 deletions.
12 changes: 6 additions & 6 deletions knyst/examples/beat_callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use knyst::{
controller::{print_error_handler, StartBeat},
graph::Mult,
prelude::*,
time::Superbeats,
time::Beats,
};
use rand::{thread_rng, Rng};
use std::time::Duration;
Expand Down Expand Up @@ -71,20 +71,20 @@ fn main() -> Result<()> {
k.schedule_change(ParameterChange::beats(
node0.input("freq"),
freq as Sample,
time + Superbeats::from_beats_f32(0.5),
time + Beats::from_beats_f32(0.5),
));
i += 1;
if time > Superbeats::from_beats(32) {
if time > Beats::from_beats(32) {
None
} else {
if i % 2 == 0 {
Some(Superbeats::from_beats(2))
Some(Beats::from_beats(2))
} else {
Some(Superbeats::from_beats_f32(1.25))
Some(Beats::from_beats_f32(1.25))
}
}
},
StartBeat::Multiple(Superbeats::from_beats(4)), // Start after a multiple of 4 beats
StartBeat::Multiple(Beats::from_beats(4)), // Start after a multiple of 4 beats
));
let mut input = String::new();
loop {
Expand Down
127 changes: 127 additions & 0 deletions knyst/examples/filtered_noise.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use std::time::Duration;

use anyhow::Result;
#[allow(unused)]
use knyst::{
audio_backend::{CpalBackend, CpalBackendOptions, JackBackend},
controller::print_error_handler,
envelope::Envelope,
handles::{graph_output, handle, Handle},
modal_interface::knyst_commands,
prelude::{delay::static_sample_delay, *},
sphere::{KnystSphere, SphereSettings},
};
use knyst::{
envelope::envelope_gen,
gen::filter::svf::{svf_dynamic, svf_filter, SvfFilterType},
};
use rand::{thread_rng, Rng};
fn main() -> Result<()> {
let mut backend = CpalBackend::new(CpalBackendOptions::default())?;
// let mut backend = JackBackend::new("Knyst<3JACK")?;
let _sphere = KnystSphere::start(
&mut backend,
SphereSettings {
num_inputs: 1,
num_outputs: 1,
..Default::default()
},
print_error_handler,
);

let mut rng = thread_rng();
// loop {
// let freq = rng.gen_range(100.0..1000.0);
// let length = rng.gen_range(0.5..4.0);
// dbg!(freq, length);
// spawn_filtered_noise(freq, length);
// std::thread::sleep(Duration::from_secs_f32(length));
// }
loop {
let mut chord = [1.0, 5. / 4., 3. / 2., 17. / 8., 7. / 4.];
let freq = rng.gen_range(100.0..200.0);
for f in &mut chord {
*f *= freq;
}
changed_harmony_chord(&chord);

std::thread::sleep(Duration::from_secs_f32(10.));
}

// graph_output(0, white_noise());

// graph_output(0, (sine(wt).freq(200.)).repeat_outputs(1));

// Wait for ENTER
println!("Press ENTER to exit");
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
Ok(())
}

fn spawn_filtered_noise(freq: f32, length: f32) {
let mut rng = thread_rng();
let filtered_noise = upload_graph(knyst_commands().default_graph_settings(), || {
let env = envelope_gen(
0.0,
vec![
(1.0, rng.gen_range(0.7..1.9)),
(0.5, length * 0.34),
(0.0, length * 0.66),
],
knyst::envelope::SustainMode::NoSustain,
StopAction::FreeGraph,
);
let source = pink_noise();
let sig = svf_filter(
SvfFilterType::Band,
freq,
rng.gen_range(2000.0..10000.),
0.0,
)
.input(source);
let sig = sig * env * 0.01;
graph_output(0, sig.channels(2));
});
graph_output(0, filtered_noise);
}

fn changed_harmony_chord(new_chord: &[f32]) {
let mut rng = thread_rng();
if rng.gen::<f32>() > 0.4 {
for f in new_chord {
let length = rng.gen_range(6.0..14.0);
let speaker = rng.gen_range(0..4);
let filtered_noise = upload_graph(knyst_commands().default_graph_settings(), || {
let env = envelope_gen(
0.0,
vec![(1.0, 3.), (1.0, length - 5.), (0.0, 2.)],
knyst::envelope::SustainMode::NoSustain,
StopAction::FreeGraph,
);
let source = white_noise();
let mut sigs = vec![];
for i in 0..5 {
let freq_detune = [1.0, 1.001, 0.999, 1.002, 0.998][i];
let q_env = envelope_gen(
1.0 / rng.gen_range(0.001..0.008),
vec![(1. / 0.0003, length)],
knyst::envelope::SustainMode::NoSustain,
StopAction::Continue,
);

let sig = svf_dynamic(SvfFilterType::Band)
.cutoff_freq(f * freq_detune)
.q(q_env)
.gain(0.0)
.input(source);
sigs.push(sig);
}
let sig = sigs[0] + sigs[1] + sigs[2] + sigs[3] + sigs[4];
let sig = sig * env * 0.01;
graph_output(speaker, sig);
});
graph_output(0, filtered_noise);
}
}
}
57 changes: 25 additions & 32 deletions knyst/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
handles::{GraphHandle, Handle},
inputs,
scheduling::MusicalTimeMap,
time::Superbeats,
time::Beats,
KnystError,
};
use crossbeam_channel::{unbounded, Receiver, Sender};
Expand Down Expand Up @@ -137,9 +137,7 @@ pub trait KnystCommands {
/// Add a new beat callback. See [`BeatCallback`] for documentation.
fn schedule_beat_callback(
&mut self,
callback: impl FnMut(Superbeats, &mut MultiThreadedKnystCommands) -> Option<Superbeats>
+ Send
+ 'static,
callback: impl FnMut(Beats, &mut MultiThreadedKnystCommands) -> Option<Beats> + Send + 'static,
start_time: StartBeat,
) -> CallbackHandle;
/// Disconnect (undo) a [`Connection`]
Expand Down Expand Up @@ -371,12 +369,10 @@ impl KnystCommands for MultiThreadedKnystCommands {
/// Add a new beat callback. See [`BeatCallback`] for documentation.
fn schedule_beat_callback(
&mut self,
callback: impl FnMut(Superbeats, &mut MultiThreadedKnystCommands) -> Option<Superbeats>
+ Send
+ 'static,
callback: impl FnMut(Beats, &mut MultiThreadedKnystCommands) -> Option<Beats> + Send + 'static,
start_time: StartBeat,
) -> CallbackHandle {
let c = BeatCallback::new(callback, Superbeats::ZERO);
let c = BeatCallback::new(callback, Beats::ZERO);
let handle = c.handle();
let command = Command::ScheduleBeatCallback(c, start_time);
self.sender.send(command).unwrap();
Expand Down Expand Up @@ -713,9 +709,9 @@ impl CallbackHandle {
/// The beat on which a callback should start, either an absolute beat value or the next multiple of some number of beats.
pub enum StartBeat {
/// An absolute time in beat
Absolute(Superbeats),
Absolute(Beats),
/// The next multiple of this number of beats
Multiple(Superbeats),
Multiple(Beats),
}

/// Callback that is scheduled in [`Superbeats`]. The closure inside the
Expand All @@ -729,18 +725,15 @@ pub enum StartBeat {
/// can return the time to wait until it gets called again or `None` to remove
/// the callback.
pub struct BeatCallback {
callback:
Box<dyn FnMut(Superbeats, &mut MultiThreadedKnystCommands) -> Option<Superbeats> + Send>,
next_timestamp: Superbeats,
callback: Box<dyn FnMut(Beats, &mut MultiThreadedKnystCommands) -> Option<Beats> + Send>,
next_timestamp: Beats,
free_flag: Arc<AtomicBool>,
}
impl BeatCallback {
/// Create a new [`BeatCallback`] with a given start time
fn new(
callback: impl FnMut(Superbeats, &mut MultiThreadedKnystCommands) -> Option<Superbeats>
+ Send
+ 'static,
start_time: Superbeats,
callback: impl FnMut(Beats, &mut MultiThreadedKnystCommands) -> Option<Beats> + Send + 'static,
start_time: Beats,
) -> Self {
let free_flag = Arc::new(AtomicBool::new(false));
Self {
Expand Down Expand Up @@ -942,10 +935,10 @@ impl Controller {
StartBeat::Absolute(beats) => beats,
StartBeat::Multiple(beats) => {
let mut i = 1;
while beats * Superbeats::from_beats(i) < current_beats {
while beats * Beats::from_beats(i) < current_beats {
i += 1;
}
beats * Superbeats::from_beats(i)
beats * Beats::from_beats(i)
}
};
// println!(
Expand Down Expand Up @@ -1040,7 +1033,7 @@ impl Controller {
let c = &mut self.beat_callbacks[i - 1];
if c.next_timestamp < current_time_beats
|| c.next_timestamp.checked_sub(current_time_beats).unwrap()
< Superbeats::from_beats_f32(0.25)
< Beats::from_beats_f32(0.25)
{
if let CallbackResult::Delete = c.run_callback(&mut k) {
self.beat_callbacks.remove(i - 1);
Expand Down Expand Up @@ -1141,41 +1134,41 @@ mod tests {
graph_output(0, once_trig());
});
schedule_bundle(
crate::graph::Time::Superseconds(Superseconds::from_samples(5, sr as u64)),
crate::graph::Time::Superseconds(Seconds::from_samples(5, sr as u64)),
|| {
graph_output(0, once_trig());
},
);
schedule_bundle(
crate::graph::Time::Superseconds(Superseconds::from_samples(10, sr as u64)),
crate::graph::Time::Superseconds(Seconds::from_samples(10, sr as u64)),
|| {
graph_output(0, once_trig());
},
);
let mut og = None;
schedule_bundle(
crate::graph::Time::Superseconds(Superseconds::from_samples(16, sr as u64)),
crate::graph::Time::Superseconds(Seconds::from_samples(16, sr as u64)),
|| {
og = Some(one_gen());
graph_output(0, og.unwrap());
},
);
let og = og.unwrap();
schedule_bundle(
crate::graph::Time::Superseconds(Superseconds::from_samples(17, sr as u64)),
crate::graph::Time::Superseconds(Seconds::from_samples(17, sr as u64)),
|| {
og.passthrough(2.0);
},
);
schedule_bundle(
crate::graph::Time::Superseconds(Superseconds::from_samples(19, sr as u64)),
crate::graph::Time::Superseconds(Seconds::from_samples(19, sr as u64)),
|| {
og.passthrough(3.0);
},
);
// Try with the pure KnystCommands methods as well.
knyst_commands().start_scheduling_bundle(knyst::graph::Time::Superseconds(
Superseconds::from_samples(20, sr as u64),
Seconds::from_samples(20, sr as u64),
));
og.passthrough(4.0);
knyst_commands().upload_scheduling_bundle();
Expand Down Expand Up @@ -1216,28 +1209,28 @@ mod tests {
graph_output(0, once_trig());
});
schedule_bundle(
crate::graph::Time::Superseconds(Superseconds::from_samples(5, sr as u64)),
crate::graph::Time::Superseconds(Seconds::from_samples(5, sr as u64)),
|| {
graph_output(0, once_trig());
},
);
schedule_bundle(
crate::graph::Time::Superseconds(Superseconds::from_samples(10, sr as u64)),
crate::graph::Time::Superseconds(Seconds::from_samples(10, sr as u64)),
|| {
graph_output(0, once_trig());
},
);
let mut og = None;
schedule_bundle(
crate::graph::Time::Superseconds(Superseconds::from_samples(16, sr as u64)),
crate::graph::Time::Superseconds(Seconds::from_samples(16, sr as u64)),
|| {
og = Some(one_gen());
graph_output(0, og.unwrap());
},
);
let og = og.unwrap();
schedule_bundle(
crate::graph::Time::Superseconds(Superseconds::from_samples(17, sr as u64)),
crate::graph::Time::Superseconds(Seconds::from_samples(17, sr as u64)),
|| {
og.passthrough(2.0);

Expand All @@ -1246,14 +1239,14 @@ mod tests {
},
);
schedule_bundle(
crate::graph::Time::Superseconds(Superseconds::from_samples(19, sr as u64)),
crate::graph::Time::Superseconds(Seconds::from_samples(19, sr as u64)),
|| {
og.passthrough(3.0);
},
);
// Try with the pure KnystCommands methods as well.
knyst_commands().start_scheduling_bundle(knyst::graph::Time::Superseconds(
Superseconds::from_samples(20, sr as u64),
Seconds::from_samples(20, sr as u64),
));
og.passthrough(4.0);
knyst_commands().upload_scheduling_bundle();
Expand Down
Loading

0 comments on commit a891927

Please sign in to comment.