Skip to content

Commit

Permalink
switch from nanorand to rand
Browse files Browse the repository at this point in the history
nanorand has some bugs and weirdness that make me not fully trust it

Absolucy/nanorand-rs#24

Absolucy/nanorand-rs#21

for a fundamental thing like randomness i'd rather use a battle-tested lib
  • Loading branch information
tesselode committed Feb 14, 2021
1 parent 029d928 commit df90d63
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 56 deletions.
9 changes: 2 additions & 7 deletions kira/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ hound = { version = "3.4.0", optional = true }
indexmap = "1.6.1"
lewton = { version = "0.10.1", optional = true }
minimp3 = { version = "0.5.0", optional = true }
rand = "0.8.3"
serde = { version = "1.0.118", optional = true, features = ["derive"] }
thiserror = "1.0.23"
uuid = "0.8.1"

[target.'cfg(not(target_arch="wasm32"))'.dependencies]
nanorand = "0.5.1"

[target.'cfg(target_arch="wasm32")'.dependencies]
nanorand = { version = "0.5.1", features = ["getrandom"] }
uuid = { version = "0.8.1", features = ["v4"] }
4 changes: 1 addition & 3 deletions kira/src/arrangement/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::hash::Hash;

use uuid::Uuid;

use crate::util::generate_uuid;

use super::ArrangementHandle;

/// A unique identifier for an [`Arrangement`](super::Arrangement).
Expand All @@ -20,7 +18,7 @@ pub struct ArrangementId {
impl ArrangementId {
pub(crate) fn new() -> Self {
Self {
uuid: generate_uuid(),
uuid: Uuid::new_v4(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions kira/src/audio_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::fmt::Debug;

use uuid::Uuid;

use crate::{util::generate_uuid, Frame};
use crate::Frame;

/// Produces a constant flow of audio data in real time.
pub trait AudioStream: Debug + Send + 'static {
Expand Down Expand Up @@ -40,7 +40,7 @@ pub struct AudioStreamId {
impl AudioStreamId {
pub(crate) fn new() -> Self {
Self {
uuid: generate_uuid(),
uuid: Uuid::new_v4(),
}
}
}
4 changes: 1 addition & 3 deletions kira/src/group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ use handle::GroupHandle;
pub use set::GroupSet;
use uuid::Uuid;

use crate::util::generate_uuid;

/// A unique identifier for a group.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(
Expand All @@ -34,7 +32,7 @@ pub struct GroupId {
impl GroupId {
pub(crate) fn new() -> Self {
Self {
uuid: generate_uuid(),
uuid: Uuid::new_v4(),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions kira/src/instance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ use crate::{
parameter::{Parameter, Parameters},
playable::{PlayableId, Playables},
sequence::SequenceInstanceId,
util::generate_uuid,
value::CachedValue,
value::Value,
};
Expand All @@ -92,7 +91,7 @@ pub struct InstanceId {
impl InstanceId {
pub(crate) fn new() -> Self {
Self {
uuid: generate_uuid(),
uuid: Uuid::new_v4(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions kira/src/metronome/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod settings;
use flume::Sender;
use uuid::Uuid;

use crate::{parameter::Parameters, tempo::Tempo, util::generate_uuid, value::CachedValue, Value};
use crate::{parameter::Parameters, tempo::Tempo, value::CachedValue, Value};
use handle::MetronomeHandle;
pub(crate) use metronomes::Metronomes;
pub use settings::MetronomeSettings;
Expand All @@ -29,7 +29,7 @@ pub struct MetronomeId {
impl MetronomeId {
pub(crate) fn new() -> Self {
Self {
uuid: generate_uuid(),
uuid: Uuid::new_v4(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions kira/src/mixer/effect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::fmt::Debug;

use uuid::Uuid;

use crate::{frame::Frame, parameter::Parameters, util::generate_uuid};
use crate::{frame::Frame, parameter::Parameters};

/// A unique identifier for an effect.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
Expand All @@ -25,7 +25,7 @@ pub struct EffectId {
impl EffectId {
pub(crate) fn new() -> Self {
Self {
uuid: generate_uuid(),
uuid: Uuid::new_v4(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions kira/src/mixer/track/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use uuid::Uuid;

use indexmap::IndexMap;

use crate::{frame::Frame, parameter::Parameters, util::generate_uuid};
use crate::{frame::Frame, parameter::Parameters};

use super::{
effect::{Effect, EffectId, EffectSettings},
Expand All @@ -26,7 +26,7 @@ pub struct SubTrackId {
impl SubTrackId {
pub(crate) fn new() -> Self {
Self {
uuid: generate_uuid(),
uuid: Uuid::new_v4(),
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions kira/src/parameter/parameter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use uuid::Uuid;

use crate::util::generate_uuid;

use super::{handle::ParameterHandle, tween::Tween};

/// A unique identifier for a parameter.
Expand All @@ -18,7 +16,7 @@ pub struct ParameterId {
impl ParameterId {
pub(crate) fn new() -> Self {
Self {
uuid: generate_uuid(),
uuid: Uuid::new_v4(),
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions kira/src/sequence/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use std::sync::{atomic::Ordering, Arc};

use atomic::Atomic;
use flume::Sender;
use nanorand::{tls_rng, RNG};
use rand::{thread_rng, Rng};
use uuid::Uuid;

use crate::{
group::{groups::Groups, GroupId},
metronome::{MetronomeId, Metronomes},
util::generate_uuid,
Tempo,
};

Expand All @@ -28,7 +27,7 @@ pub struct SequenceInstanceId {
impl SequenceInstanceId {
pub(crate) fn new() -> Self {
Self {
uuid: generate_uuid(),
uuid: Uuid::new_v4(),
}
}
}
Expand Down Expand Up @@ -176,8 +175,8 @@ impl SequenceInstance {
self.start_step(self.position + 1);
}
SequenceStep::PlayRandom(choices, settings) => {
let choice_index = tls_rng().generate_range(0, choices.len());
if !self.muted {
let choice_index = thread_rng().gen_range(0..choices.len());
output_command_queue.push(SequenceOutputCommand::PlaySound(
choices[choice_index],
*settings,
Expand Down
4 changes: 1 addition & 3 deletions kira/src/sound/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::hash::Hash;

use uuid::Uuid;

use crate::util::generate_uuid;

use super::handle::SoundHandle;

/// A unique identifier for a [`Sound`](crate::sound::Sound).
Expand All @@ -20,7 +18,7 @@ pub struct SoundId {
impl SoundId {
pub(crate) fn new() -> Self {
Self {
uuid: generate_uuid(),
uuid: Uuid::new_v4(),
}
}
}
Expand Down
16 changes: 0 additions & 16 deletions kira/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
use nanorand::RNG;
use uuid::Uuid;

pub fn lerp(a: f64, b: f64, amount: f64) -> f64 {
a + (b - a) * amount
}

pub fn inverse_lerp(start: f64, end: f64, point: f64) -> f64 {
(point - start) / (end - start)
}

pub fn generate_uuid() -> Uuid {
let mut rng = nanorand::tls_rng();
let mut random_bytes: [u8; 16] = [0; 16];
for i in 0..16 {
random_bytes[i] = rng.generate();
}
uuid::Builder::from_slice(&random_bytes)
.unwrap()
.set_variant(uuid::Variant::RFC4122)
.set_version(uuid::Version::Random)
.build()
}
12 changes: 5 additions & 7 deletions kira/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use std::ops::Range;

use nanorand::{tls_rng, RNG};
use rand::{thread_rng, Rng};

use crate::{
parameter::{handle::ParameterHandle, Mapping, ParameterId, Parameters},
util::lerp,
};
use crate::parameter::{handle::ParameterHandle, Mapping, ParameterId, Parameters};

/// A value that something can be set to.
#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -57,8 +54,9 @@ pub struct CachedValue<T: From<f64> + Into<f64> + Copy> {

impl<T: From<f64> + Into<f64> + Copy> CachedValue<T> {
fn pick_random(lower: T, upper: T) -> T {
let fraction = f64::from(tls_rng().generate::<u32>()) / f64::from(std::u32::MAX);
lerp(lower.into(), upper.into(), fraction).into()
let lower: f64 = lower.into();
let upper: f64 = upper.into();
thread_rng().gen_range(lower..upper).into()
}

/// Creates a `CachedValue` with an initial value setting
Expand Down

0 comments on commit df90d63

Please sign in to comment.