This commit is contained in:
Felix Roos 2022-02-09 20:21:58 +01:00
parent e783de3453
commit b0e50db4f6
43 changed files with 28084 additions and 227 deletions

View file

@ -1,11 +1,7 @@
import Fraction from "../pkg/fractionjs.js";
var removeUndefineds = function(xs) {
return xs.filter((x) => x != void 0);
};
function flatten(arr) {
return [].concat(...arr);
}
var id = (a) => a;
const removeUndefineds = (xs) => xs.filter((x) => x != void 0);
const flatten = (arr) => [].concat(...arr);
const id = (a) => a;
function curry(func) {
return function curried(...args) {
if (args.length >= func.length) {
@ -59,16 +55,16 @@ class TimeSpan {
this.end = Fraction(end);
}
get spanCycles() {
var spans = [];
const spans = [];
var begin = this.begin;
var end = this.end;
var end_sam = end.sam();
const end = this.end;
const end_sam = end.sam();
while (end.gt(begin)) {
if (begin.sam().equals(end_sam)) {
spans.push(new TimeSpan(begin, this.end));
break;
}
var next_begin = begin.nextSam();
const next_begin = begin.nextSam();
spans.push(new TimeSpan(begin, next_begin));
begin = next_begin;
}
@ -78,8 +74,8 @@ class TimeSpan {
return new TimeSpan(func_time(this.begin), func_time(this.end));
}
intersection(other) {
var intersect_begin = this.begin.max(other.begin);
var intersect_end = this.end.min(other.end);
const intersect_begin = this.begin.max(other.begin);
const intersect_end = this.end.min(other.end);
if (intersect_begin.gt(intersect_end)) {
return void 0;
}
@ -94,7 +90,7 @@ class TimeSpan {
return new TimeSpan(intersect_begin, intersect_end);
}
intersection_e(other) {
var result = this.intersection(other);
const result = this.intersection(other);
if (result == void 0) {
}
return result;
@ -116,7 +112,7 @@ class Hap {
this.value = value;
}
withSpan(func) {
var whole = this.whole ? func(this.whole) : void 0;
const whole = this.whole ? func(this.whole) : void 0;
return new Hap(whole, func(this.part), this.value);
}
withValue(func) {
@ -140,10 +136,8 @@ class Pattern {
this.query = query2;
}
_splitQueries() {
var pat = this;
var q = function(span) {
return flatten(span.spanCycles.map((subspan) => pat.query(subspan)));
};
const pat = this;
const q = (span) => flatten(span.spanCycles.map((subspan) => pat.query(subspan)));
return new Pattern(q);
}
withQuerySpan(func) {
@ -177,12 +171,12 @@ class Pattern {
return this._filterEvents((hap) => hap.hasOnset());
}
_appWhole(whole_func, pat_val) {
var pat_func = this;
const pat_func = this;
query = function(span) {
var event_funcs = pat_func.query(span);
var event_vals = pat_val.query(span);
const event_funcs = pat_func.query(span);
const event_vals = pat_val.query(span);
apply = function(event_func, event_val) {
var s = event_func.part.intersection(event_val.part);
const s = event_func.part.intersection(event_val.part);
if (s == void 0) {
return void 0;
}
@ -193,7 +187,7 @@ class Pattern {
return new Pattern(query);
}
appBoth(pat_val) {
var whole_func = function(span_a, span_b) {
const whole_func = function(span_a, span_b) {
if (span_a == void 0 || span_B == void 0) {
return void 0;
}
@ -202,16 +196,16 @@ class Pattern {
return this._appWhole(whole_func, pat_val);
}
appLeft(pat_val) {
var pat_func = this;
var query2 = function(span) {
var haps = [];
for (var hap_func of pat_func.query(span)) {
var event_vals = pat_val.query(hap_func.part);
for (var hap_val of event_vals) {
var new_whole = hap_func.whole;
var new_part = hap_func.part.intersection_e(hap_val.part);
var new_value = hap_func.value(hap_val.value);
var hap = new Hap(new_whole, new_part, new_value);
const pat_func = this;
const query2 = function(span) {
const haps = [];
for (const hap_func of pat_func.query(span)) {
const event_vals = pat_val.query(hap_func.part);
for (const hap_val of event_vals) {
const new_whole = hap_func.whole;
const new_part = hap_func.part.intersection_e(hap_val.part);
const new_value = hap_func.value(hap_val.value);
const hap = new Hap(new_whole, new_part, new_value);
haps.push(hap);
}
}
@ -220,16 +214,16 @@ class Pattern {
return new Pattern(query2);
}
appRight(pat_val) {
var pat_func = this;
var query2 = function(span) {
var haps = [];
for (var hap_val of pat_val.query(span)) {
var hap_funcs = pat_func.query(hap_val.part);
for (var hap_func of hap_funcs) {
var new_whole = hap_val.whole;
var new_part = hap_func.part.intersection_e(hap_val.part);
var new_value = hap_func.value(hap_val.value);
var hap = new Hap(new_whole, new_part, new_value);
const pat_func = this;
const query2 = function(span) {
const haps = [];
for (const hap_val of pat_val.query(span)) {
const hap_funcs = pat_func.query(hap_val.part);
for (const hap_func of hap_funcs) {
const new_whole = hap_val.whole;
const new_part = hap_func.part.intersection_e(hap_val.part);
const new_value = hap_func.value(hap_val.value);
const hap = new Hap(new_whole, new_part, new_value);
haps.push(hap);
}
}
@ -256,12 +250,12 @@ class Pattern {
return this._opleft(other, (a) => (b) => Object.assign({}, a, b));
}
_bindWhole(choose_whole, func) {
var pat_val = this;
var query2 = function(span) {
var withWhole = function(a, b) {
const pat_val = this;
const query2 = function(span) {
const withWhole = function(a, b) {
return new Hap(choose_whole(a.whole, b.whole), b.part, b.value);
};
var match = function(a) {
const match = function(a) {
return func(a.value).query(a.part).map((b) => withWhole(a, b));
};
return flatten(pat_val.query(span).map((a) => match(a)));
@ -269,7 +263,7 @@ class Pattern {
return new Pattern(query2);
}
bind(func) {
var whole_func = function(a, b) {
const whole_func = function(a, b) {
if (a == void 0 || b == void 0) {
return void 0;
}
@ -301,43 +295,43 @@ class Pattern {
return patterned;
}
_fast(factor) {
var fastQuery = this.withQueryTime((t) => t.mul(factor));
const fastQuery = this.withQueryTime((t) => t.mul(factor));
return fastQuery.withEventTime((t) => t.div(factor));
}
fast(factor) {
return this._patternify(Pattern.prototype._fast)(factor);
fast(...factor) {
return this._patternify(Pattern.prototype._fast)(...factor);
}
_slow(factor) {
return this._fast(1 / factor);
}
slow(factor) {
return this._patternify(Pattern.prototype._slow)(factor);
slow(...factor) {
return this._patternify(Pattern.prototype._slow)(...factor);
}
_early(offset) {
offset = Fraction(offset);
return this.withQueryTime((t) => t.add(offset)).withEventTime((t) => t.sub(offset));
}
early(factor) {
return this._patternify(Pattern.prototype._early)(factor);
early(...factor) {
return this._patternify(Pattern.prototype._early)(...factor);
}
_late(offset) {
return this._early(0 - offset);
}
late(factor) {
return this._patternify(Pattern.prototype._late)(factor);
late(...factor) {
return this._patternify(Pattern.prototype._late)(...factor);
}
when(binary_pat, func) {
var true_pat = binary_pat._filterValues(id);
var false_pat = binary_pat._filterValues((val) => !val);
var with_pat = true_pat.fmap((_) => (y) => y).appRight(func(this));
var without_pat = false_pat.fmap((_) => (y) => y).appRight(this);
const true_pat = binary_pat._filterValues(id);
const false_pat = binary_pat._filterValues((val) => !val);
const with_pat = true_pat.fmap((_) => (y) => y).appRight(func(this));
const without_pat = false_pat.fmap((_) => (y) => y).appRight(this);
return stack(with_pat, without_pat);
}
off(time_pat, func) {
return stack([this, func(this._early(time_pat))]);
}
every(n, func) {
var pats = Array(n - 1).fill(this);
const pats = Array(n - 1).fill(this);
pats.unshift(func(this));
return slowcat(...pats);
}
@ -345,32 +339,32 @@ class Pattern {
return fastcat(...[this, other]);
}
rev() {
var pat = this;
var query2 = function(span) {
var cycle = span.begin.sam();
var next_cycle = span.begin.nextSam();
var reflect = function(to_reflect) {
var reflected = to_reflect.withTime((time) => cycle.add(next_cycle.sub(time)));
var tmp = reflected.begin;
const pat = this;
const query2 = function(span) {
const cycle = span.begin.sam();
const next_cycle = span.begin.nextSam();
const reflect = function(to_reflect) {
const reflected = to_reflect.withTime((time) => cycle.add(next_cycle.sub(time)));
const tmp = reflected.begin;
reflected.begin = reflected.end;
reflected.end = tmp;
return reflected;
};
var haps = pat.query(reflect(span));
const haps = pat.query(reflect(span));
return haps.map((hap) => hap.withSpan(reflect));
};
return new Pattern(query2)._splitQueries();
}
jux(func, by = 1) {
by /= 2;
var elem_or = function(dict, key, dflt) {
const elem_or = function(dict, key, dflt) {
if (key in dict) {
return dict[key];
}
return dflt;
};
var left = this.withValue((val) => Object.assign({}, val, {pan: elem_or(val, "pan", 0.5) - by}));
var right = this.withValue((val) => Object.assign({}, val, {pan: elem_or(val, "pan", 0.5) + by}));
const left = this.withValue((val) => Object.assign({}, val, {pan: elem_or(val, "pan", 0.5) - by}));
const right = this.withValue((val) => Object.assign({}, val, {pan: elem_or(val, "pan", 0.5) + by}));
return stack([left, func(right)]);
}
}
@ -391,10 +385,8 @@ function reify(thing) {
return pure(thing);
}
function stack(...pats) {
var pats = pats.map((pat) => reify(pat));
var query2 = function(span) {
return flatten(pats.map((pat) => pat.query(span)));
};
const reified = pats.map((pat) => reify(pat));
const query2 = (span) => flatten(reified.map((pat) => pat.query(span)));
return new Pattern(query2);
}
function slowcat(...pats) {
@ -429,15 +421,15 @@ function sequence(...xs) {
return _sequenceCount(xs)[0];
}
function polymeter(steps = 0, ...args) {
var seqs = args.map((a) => _sequenceCount(a));
const seqs = args.map((a) => _sequenceCount(a));
if (seqs.length == 0) {
return silence;
}
if (steps == 0) {
steps = seqs[0][1];
}
var pats = [];
for (var seq of seqs) {
const pats = [];
for (const seq of seqs) {
if (seq[1] == 0) {
next;
}
@ -453,7 +445,7 @@ function pm(args) {
polymeter(args);
}
function polyrhythm(...xs) {
var seqs = xs.map((a) => sequence(a));
const seqs = xs.map((a) => sequence(a));
if (seqs.length == 0) {
return silence;
}
@ -462,6 +454,11 @@ function polyrhythm(...xs) {
function pr(args) {
polyrhythm(args);
}
const fast = curry((a, pat) => pat.fast(a));
const slow = curry((a, pat) => pat.slow(a));
const early = curry((a, pat) => pat.early(a));
const late = curry((a, pat) => pat.late(a));
const rev = (pat) => pat.rev();
export {
Fraction,
TimeSpan,
@ -478,5 +475,10 @@ export {
polyrhythm,
pr,
reify,
silence
silence,
fast,
slow,
early,
late,
rev
};