fmapNested prototype

This commit is contained in:
Felix Roos 2022-02-12 22:05:49 +01:00
parent 64e24e5fc1
commit c20d5e5be9

View File

@ -1,4 +1,4 @@
import { Pattern as _Pattern, stack, TimeSpan, Hap } from '../../strudel.mjs';
import { Pattern as _Pattern, stack, TimeSpan, Hap, reify } from '../../strudel.mjs';
import voicings from 'chord-voicings';
const { dictionaryVoicing, minTopNoteDiff, lefthand } = voicings;
@ -13,16 +13,22 @@ const getVoicing = (chord, lastVoicing, range = ['F3', 'A4']) =>
const Pattern = _Pattern as any;
Pattern.prototype.voicings = function (range = ['F3', 'A4']) {
let lastVoicing;
Pattern.prototype.fmapNested = function (func) {
return new Pattern((span) =>
this.query(span)
.map((event) => {
lastVoicing = getVoicing(event.value, lastVoicing, range);
return stack(...lastVoicing)
return reify(func(event))
.query(span)
.map((hap) => new Hap(event.whole, event.part, hap.value));
})
.flat()
);
};
Pattern.prototype.voicings = function (range = ['F3', 'A4']) {
let lastVoicing;
return this.fmapNested((event) => {
lastVoicing = getVoicing(event.value, lastVoicing, range);
return stack(...lastVoicing);
});
};