Skip to content

Commit

Permalink
refactor Random --> Chance
Browse files Browse the repository at this point in the history
  • Loading branch information
heuermh committed May 19, 2023
1 parent 35ae136 commit 7392215
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 89 deletions.
4 changes: 2 additions & 2 deletions import.ck
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ Machine.add(path + "lick/fn/Trunc.ck");
Machine.add(path + "lick/fn/Uniform.ck");

// random suppliers
Machine.add(path + "lick/fn/Random.ck");
Machine.add(path + "lick/fn/GaussianRandom.ck");
Machine.add(path + "lick/fn/Chance.ck");
Machine.add(path + "lick/fn/Gaussian.ck");

// random predicates
Machine.add(path + "lick/fn/AlmostAlways.ck");
Expand Down
55 changes: 12 additions & 43 deletions lick/duration/Humanize.ck
Original file line number Diff line number Diff line change
Expand Up @@ -55,64 +55,33 @@ public class Humanize
// todo: add swing


fun static dur humanize(dur value, Random random)
fun static dur humanize(dur value, Chance chance)
{
return humanize(value, 0.05, 0.05, random);
return humanize(value, 0.05, 0.05, chance);
}

fun static dur humanize(dur value, float anticipation, float delay, Random random)
fun static dur humanize(dur value, float anticipation, float delay, Chance chance)
{
return value - random.get() * anticipation * value + random.get() * delay * value;
return value - chance.get() * anticipation * value + chance.get() * delay * value;
}

fun static dur anticipate(dur value, Random random)
fun static dur anticipate(dur value, Chance chance)
{
return anticipate(value, 0.1, random);
return anticipate(value, 0.1, chance);
}

fun static dur anticipate(dur value, float anticipation, Random random)
fun static dur anticipate(dur value, float anticipation, Chance chance)
{
return value - random.get() * anticipation * value;
return value - chance.get() * anticipation * value;
}

fun static dur delay(dur value, Random random)
fun static dur delay(dur value, Chance chance)
{
return delay(value, 0.1, random);
return delay(value, 0.1, chance);
}

fun static dur delay(dur value, float delay, Random random)
fun static dur delay(dur value, float delay, Chance chance)
{
return value + random.get() * delay * value;
}


fun dur humanizeGaussian(dur value)
{
return humanizeGaussian(value, 0.05, 0.05);
}

fun dur humanizeGaussian(dur value, float anticipation, float delay)
{
return value + GaussianRandom.nextGaussian() * anticipation * value - GaussianRandom.nextGaussian() * delay * value;
}

fun static dur anticipateGaussian(dur value)
{
return anticipateGaussian(value, 0.1);
}

fun static dur anticipateGaussian(dur value, float anticipation)
{
return value - GaussianRandom.nextGaussian() * anticipation * value;
}

fun static dur delayGaussian(dur value)
{
return delayGaussian(value, 0.1);
}

fun static dur delayGaussian(dur value, float delay)
{
return value + GaussianRandom.nextGaussian() * delay * value;
return value + chance.get() * delay * value;
}
}
8 changes: 4 additions & 4 deletions lick/duration/HumanizedDurProvider.ck
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public class HumanizedDurProvider extends DurProvider
{
0.05 => float anticipation;
0.05 => float delay;
Random random;
Chance chance;

fun dur evaluate()
{
return Humanize.humanize(duration, anticipation, delay, random);
return Humanize.humanize(duration, anticipation, delay, chance);
}

fun static HumanizedDurProvider create(dur duration, float anticipation, float delay)
Expand All @@ -40,13 +40,13 @@ public class HumanizedDurProvider extends DurProvider
return humanizedDurProvider;
}

fun static HumanizedDurProvider create(dur duration, float anticipation, float delay, Random random)
fun static HumanizedDurProvider create(dur duration, float anticipation, float delay, Chance chance)
{
HumanizedDurProvider humanizedDurProvider;
duration => humanizedDurProvider.duration;
anticipation => humanizedDurProvider.anticipation;
delay => humanizedDurProvider.delay;
random @=> humanizedDurProvider.random;
chance @=> humanizedDurProvider.chance;
return humanizedDurProvider;
}
}
2 changes: 1 addition & 1 deletion lick/duration/IntervalDurProvider.ck
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class IntervalDurProvider extends HumanizedDurProvider
fun dur evaluate()
{
interval.evaluate(f) * durProvider.get() => dur duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}

fun static IntervalDurProvider create(TimeSignature timeSignature, Interval interval, float f, DurProvider durProvider)
Expand Down
32 changes: 16 additions & 16 deletions lick/duration/TimeSignature.ck
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BreveProvider extends HumanizedDurProvider
{
timeSignature.d => duration;
//return super.evaluate(); not supported
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -44,7 +44,7 @@ class SemibreveProvider extends HumanizedDurProvider
fun dur evaluate()
{
timeSignature.w => duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -60,7 +60,7 @@ class MinimProvider extends HumanizedDurProvider
fun dur evaluate()
{
timeSignature.h => duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -76,7 +76,7 @@ class CrotchetProvider extends HumanizedDurProvider
fun dur evaluate()
{
timeSignature.q => duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -92,7 +92,7 @@ class TripletQuarterProvider extends HumanizedDurProvider
fun dur evaluate()
{
timeSignature.triplet(timeSignature.q) => duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -103,7 +103,7 @@ class QuaverProvider extends HumanizedDurProvider
fun dur evaluate()
{
timeSignature.e => duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -119,7 +119,7 @@ class DottedEighthProvider extends HumanizedDurProvider
fun dur evaluate()
{
timeSignature.dotted(timeSignature.e) => duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -130,7 +130,7 @@ class TripletEighthProvider extends HumanizedDurProvider
fun dur evaluate()
{
timeSignature.triplet(timeSignature.e) => duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -141,7 +141,7 @@ class SemiquaverProvider extends HumanizedDurProvider
fun dur evaluate()
{
timeSignature.s => duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -157,7 +157,7 @@ class TripletSixteenthProvider extends HumanizedDurProvider
fun dur evaluate()
{
timeSignature.triplet(timeSignature.s) => duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -168,7 +168,7 @@ class DemiSemiquaverProvider extends HumanizedDurProvider
fun dur evaluate()
{
timeSignature.t => duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -184,7 +184,7 @@ class HemiDemiSemiquaverProvider extends HumanizedDurProvider
fun dur evaluate()
{
timeSignature.f => duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -200,7 +200,7 @@ class SemiHemiDemiSemiquaverProvider extends HumanizedDurProvider
fun dur evaluate()
{
timeSignature.u => duration;
return Humanize.humanize(duration, anticipation, delay, timeSignature.random);
return Humanize.humanize(duration, anticipation, delay, timeSignature.chance);
}
}

Expand All @@ -223,7 +223,7 @@ public class TimeSignature
dur t;
dur f;
dur u;
Random random;
Chance chance;

fun void update()
{
Expand Down Expand Up @@ -609,13 +609,13 @@ public class TimeSignature
return timeSignature;
}

fun static TimeSignature create(int bar, int beat, int bpm, Random random)
fun static TimeSignature create(int bar, int beat, int bpm, Chance chance)
{
TimeSignature timeSignature;
bar => timeSignature.bar;
beat => timeSignature.beat;
bpm => timeSignature.bpm;
random @=> timeSignature.random;
chance @=> timeSignature.chance;
timeSignature.update();
return timeSignature;
}
Expand Down
8 changes: 4 additions & 4 deletions lick/effect/Mixer.ck
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ public class Mixer extends Effect

fun void randomize()
{
Random r;
randomize(r);
Chance c;
randomize(c);
}

fun void randomize(Random r)
fun void randomize(Chance c)
{
1.0 => float remaining;
inputs.size() => int n;
for (0 => int i; i < n; i++)
{
r.get() * remaining => float g;
c.get() * remaining => float g;
g => input(i).gain;
remaining - g => remaining;
}
Expand Down
4 changes: 2 additions & 2 deletions lick/fn/AlmostAlways.ck
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

public class AlmostAlways extends Predicate
{
Random r;
Chance c;

fun int test()
{
return r.almostAlways();
return c.almostAlways();
}
}
2 changes: 1 addition & 1 deletion lick/fn/Random.ck → lick/fn/Chance.ck
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

public class Random extends FloatSupplier
public class Chance extends FloatSupplier
{
fun float get()
{
Expand Down
4 changes: 2 additions & 2 deletions lick/fn/CoinFlip.ck
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

public class CoinFlip extends Predicate
{
Random r;
Chance c;

fun int test()
{
return r.coinFlip();
return c.coinFlip();
}
}
4 changes: 2 additions & 2 deletions lick/fn/Frequently.ck
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

public class Frequently extends Predicate
{
Random r;
Chance c;

fun int test()
{
return r.frequently();
return c.frequently();
}
}
2 changes: 1 addition & 1 deletion lick/fn/GaussianRandom.ck → lick/fn/Gaussian.ck
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

public class GaussianRandom extends Random
public class Gaussian extends Chance
{
//
// adapted without caching from org.apache.commmons.math3.random.AbstractRandomGenerator,
Expand Down
4 changes: 2 additions & 2 deletions lick/fn/Infrequently.ck
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

public class Infrequently extends Predicate
{
Random r;
Chance c;

fun int test()
{
return r.infrequently();
return c.infrequently();
}
}
4 changes: 2 additions & 2 deletions lick/fn/Often.ck
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

public class Often extends Predicate
{
Random r;
Chance c;

fun int test()
{
return r.often();
return c.often();
}
}
4 changes: 2 additions & 2 deletions lick/fn/Rarely.ck
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

public class Rarely extends Predicate
{
Random r;
Chance c;

fun int test()
{
return r.rarely();
return c.rarely();
}
}
4 changes: 2 additions & 2 deletions lick/fn/Sometimes.ck
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

public class Sometimes extends Predicate
{
Random r;
Chance c;

fun int test()
{
return r.sometimes();
return c.sometimes();
}
}

0 comments on commit 7392215

Please sign in to comment.