Skip to content

Commit

Permalink
Update.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamiPerttu committed Apr 16, 2024
1 parent 5e86b62 commit 3dfd718
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
7 changes: 5 additions & 2 deletions examples/grain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,17 @@ where
72.0, 74.0, 76.0, 79.0, 81.0, 84.0, 86.0, 88.0, 91.0, 93.0, 96.0,
];

let mut dna = Dna::new(36);
//let mut dna = Dna::new(37);
let mut dna = Dna::new(102);
let mut c = Net64::wrap(gen_granular(2, &scale, 2.4, 30, &mut dna));

for parameter in dna.parameter_vector().iter() {
println!("{}: {}", parameter.name(), parameter.value());
}

c = c >> (multipass() & 0.2 * reverb_stereo(20.0, 2.0, 0.5));
c = c
>> (multipass()
& 0.2 * reverb2_stereo(10.0, 4.0, 0.5, 1.0, highshelf_hz(5000.0, 1.0, db_amp(-2.0))));

c.set_sample_rate(sample_rate);

Expand Down
8 changes: 5 additions & 3 deletions examples/grain2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ where
];

// We sample this granular synthesizer as source material for another granular synthesizer.
let mut dna = Dna::new(10);
let mut dna = Dna::new(3);
let mut c = gen_granular(1, &scale, 2.0, 30, &mut dna);

for parameter in dna.parameter_vector().iter() {
println!("{}: {}", parameter.name(), parameter.value());
}

let mut dna2 = Dna::new(7);
let mut dna2 = Dna::new(4);
let mut fx = gen_effect(&mut dna2);
for parameter in dna2.parameter_vector().iter() {
println!("{}: {}", parameter.name(), parameter.value());
Expand Down Expand Up @@ -83,7 +83,9 @@ where

let mut c = Net64::wrap(Box::new(granular));

c = c >> (multipass() & 0.2 * reverb_stereo(20.0, 3.0, 0.5)) >> (dcblock() | dcblock());
c = c
>> (multipass()
& 0.2 * reverb2_stereo(10.0, 4.0, 0.5, 1.0, highshelf_hz(5000.0, 1.0, db_amp(-2.0))));

c.set_sample_rate(sample_rate);

Expand Down
27 changes: 17 additions & 10 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ pub fn dissonance_max<T: Num>(f: T) -> T {
T::from_f64(1.0193) * f + T::from_f64(17.4672)
}

/// Convert decibels to gain. 0 dB = 1.0 (unity gain).
/// Convert decibels to gain (aka amplitude). 0 dB = 1.0 (unity gain).
///
/// ### Example
/// ```
Expand All @@ -296,7 +296,7 @@ pub fn db_amp<T: Real>(db: T) -> T {
exp10(db / T::new(20))
}

/// Convert amplitude `gain` (`gain` > 0) to decibels. 1.0 = 0 dB (unity gain).
/// Convert amplitude `gain` (`gain` > 0) to decibels. Gain 1.0 = 0 dB (unity gain).
#[inline]
pub fn amp_db<T: Real>(gain: T) -> T {
log10(gain) * T::new(20)
Expand Down Expand Up @@ -469,14 +469,17 @@ pub fn cos_hz<T: Real>(hz: T, t: T) -> T {
cos(t * hz * T::from_f64(TAU))
}

/// Square wave that oscillates at the specified frequency (Hz). Not bandlimited.
/// Time is input in seconds.
/// Square wave that oscillates in the range -1...1 at the specified frequency (Hz).
/// Not bandlimited. Time is input in seconds.
///
/// ### Example
/// ```
/// use fundsp::hacker::*;
/// assert_eq!(sqr_hz(1.0, 0.0), 1.0);
/// assert_eq!(sqr_hz(1.0, 0.25), 1.0);
/// assert_eq!(sqr_hz(1.0, 0.5), -1.0);
/// assert_eq!(sqr_hz(1.0, 0.75), -1.0);
/// assert_eq!(sqr_hz(1.0, 1.0), 1.0);
/// ```
#[inline]
pub fn sqr_hz<T: Float>(hz: T, t: T) -> T {
Expand All @@ -489,8 +492,8 @@ pub fn sqr_hz<T: Float>(hz: T, t: T) -> T {
}
}

/// Triangle wave that oscillates at the specified frequency (Hz). Not bandlimited.
/// Time is input in seconds.
/// Triangle wave that oscillates in the range -1...1 at the specified frequency (Hz).
/// Not bandlimited. Time is input in seconds.
///
/// ### Example
/// ```
Expand Down Expand Up @@ -519,8 +522,10 @@ pub fn semitone_ratio<T: Real>(x: T) -> T {
exp2(x / T::from_f64(12.0))
}

/// SplitMix hash as an indexed RNG.
/// Returns pseudorandom f64 in 0...1.
/// SplitMix hash as an indexed RNG
/// (using successive values of the hash as an RNG
/// passes statistical tests of randomness).
/// Returns pseudorandom `f64` in 0...1.
#[inline]
pub fn rnd(x: i64) -> f64 {
let x = x as u64 ^ 0x5555555555555555;
Expand All @@ -531,8 +536,10 @@ pub fn rnd(x: i64) -> f64 {
(x >> 11) as f64 / (1u64 << 53) as f64
}

/// Output hash of Krull64 as an indexed RNG.
/// Returns pseudorandom f64 in 0...1.
/// Output hash of Krull64 as an indexed RNG
/// (using successive values of the hash as an RNG
/// passes statistical tests of randomness).
/// Returns pseudorandom `f64` in 0...1.
#[inline]
pub fn rnd2(x: i64) -> f64 {
let x = funutd::hash::hash64g(x as u64);
Expand Down
10 changes: 8 additions & 2 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,10 @@ impl AudioUnit48 for Net48 {
self.sample_rate = sample_rate;
for vertex in &mut self.vertex {
vertex.unit.set_sample_rate(sample_rate);
// Sample rate change counts as a change because
// Sample rate change counts as a change
// to be sent to the backend because
// we cannot change sample rate in the backend
// - it may allocate.
// - it may allocate or do something else inappropriate.
vertex.changed = self.revision;
}
// Take the opportunity to unload some calculations.
Expand All @@ -1007,6 +1008,11 @@ impl AudioUnit48 for Net48 {
fn reset(&mut self) {
for vertex in &mut self.vertex {
vertex.unit.reset();
// Reseting a unit counts as a change
// to be sent to the backend because
// we cannot reset in the backend
// - it may allocate or do something else inappropriate.
vertex.changed = self.revision;
}
// Take the opportunity to unload some calculations.
if !self.is_ordered() {
Expand Down
12 changes: 8 additions & 4 deletions src/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<T: Float> AudioNode for Mls<T> {
#[derive(Default, Clone)]
pub struct Noise<T> {
_marker: std::marker::PhantomData<T>,
rnd: Rnd,
state: u64,
hash: u64,
}

Expand All @@ -169,15 +169,17 @@ impl<T: Float> AudioNode for Noise<T> {
type Setting = ();

fn reset(&mut self) {
self.rnd = Rnd::from_u64(self.hash);
self.state = self.hash;
}

#[inline]
fn tick(
&mut self,
_input: &Frame<Self::Sample, Self::Inputs>,
) -> Frame<Self::Sample, Self::Outputs> {
let value = T::from_f32(self.rnd.f32_in(-1.0, 1.0));
self.state = self.state.wrapping_add(1);
let x = funutd::hash::hash64g(self.state);
let value = T::from_f32((x >> 40) as f32 / (1 << 23) as f32 - 1.0);
[value].into()
}

Expand All @@ -188,7 +190,9 @@ impl<T: Float> AudioNode for Noise<T> {
output: &mut [&mut [Self::Sample]],
) {
for o in output[0][..size].iter_mut() {
*o = T::from_f32(self.rnd.f32_in(-1.0, 1.0));
self.state = self.state.wrapping_add(1);
let x = funutd::hash::hash64g(self.state);
*o = T::from_f32((x >> 40) as f32 / (1 << 23) as f32 - 1.0);
}
}

Expand Down

0 comments on commit 3dfd718

Please sign in to comment.