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