tidalcycles / strudel

Web-based environment for live coding algorithmic patterns, incorporating a faithful port of TidalCycles to JavaScript
https://strudel.cc/
GNU Affero General Public License v3.0
677 stars 119 forks source link

Add warp function #1143

Open yaxu opened 3 months ago

yaxu commented 3 months ago

From discussion here: https://discord.com/channels/779427371270275082/937365093082079272/1086643898652311582

Pattern.prototype.enumerate = function () {
  const pat = this.sortHapsByPart()
  return new Pattern(state => {
    const haps = pat.query(state.withSpan(span => span.begin.wholeCycle()))
    const chunks = haps.length
    return haps.map((hap, i) => new Hap(hap.whole, hap.part.intersection(state.span), [hap.value, i, chunks])
                  ).filter(hap => hap.part != undefined)
  }).splitQueries()
}

Pattern.prototype.warp = function (tpat) {
  const pat = this;
  return tpat.enumerate().withValue(v => pat.zoom(Fraction(v[1]).div(v[2]), 
                                                  Fraction(v[1]).add(1).div(v[2]))
                                  ).outerJoin()
}