mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-24 03:58:53 +00:00
fix drawLine polyrhythm
This commit is contained in:
parent
58a936cf33
commit
283e5fde69
@ -5,17 +5,15 @@ function drawLine(pat, chars = 60) {
|
|||||||
let pos = Fraction(0);
|
let pos = Fraction(0);
|
||||||
let lines = [''];
|
let lines = [''];
|
||||||
let emptyLine = ''; // this will be the "reference" empty line, which will be copied into extra lines
|
let emptyLine = ''; // this will be the "reference" empty line, which will be copied into extra lines
|
||||||
const slots = [];
|
|
||||||
while (lines[0].length < chars) {
|
while (lines[0].length < chars) {
|
||||||
const haps = pat.queryArc(cycle, cycle + 1);
|
const haps = pat.queryArc(cycle, cycle + 1);
|
||||||
const durations = haps.filter((hap) => hap.hasOnset()).map((hap) => hap.duration);
|
const durations = haps.filter((hap) => hap.hasOnset()).map((hap) => hap.duration);
|
||||||
const totalSlots = gcd(...durations).inverse(); // number of character slots for the current cycle
|
const charFraction = gcd(...durations);
|
||||||
slots.push(totalSlots); // remember slots for possible empty lines needed in a later cycle
|
const totalSlots = charFraction.inverse(); // number of character slots for the current cycle
|
||||||
const minDuration = durations.reduce((a, b) => a.min(b), durations[0]); // min duration = step length
|
|
||||||
lines = lines.map((line) => line + '|'); // add pipe character before each cycle
|
lines = lines.map((line) => line + '|'); // add pipe character before each cycle
|
||||||
emptyLine += '|';
|
emptyLine += '|';
|
||||||
for (let i = 0; i < totalSlots; i++) {
|
for (let i = 0; i < totalSlots; i++) {
|
||||||
const [begin, end] = [pos, pos.add(minDuration)];
|
const [begin, end] = [pos, pos.add(charFraction)];
|
||||||
const matches = haps.filter((hap) => hap.whole.begin.lte(begin) && hap.whole.end.gte(end));
|
const matches = haps.filter((hap) => hap.whole.begin.lte(begin) && hap.whole.end.gte(end));
|
||||||
const missingLines = matches.length - lines.length;
|
const missingLines = matches.length - lines.length;
|
||||||
if (missingLines > 0) {
|
if (missingLines > 0) {
|
||||||
@ -31,7 +29,7 @@ function drawLine(pat, chars = 60) {
|
|||||||
return line + '.';
|
return line + '.';
|
||||||
});
|
});
|
||||||
emptyLine += '.';
|
emptyLine += '.';
|
||||||
pos = pos.add(minDuration);
|
pos = pos.add(charFraction);
|
||||||
}
|
}
|
||||||
cycle++;
|
cycle++;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { fastcat, stack, slowcat, silence } from '../pattern.mjs';
|
import { fastcat, stack, slowcat, silence, pure } from '../pattern.mjs';
|
||||||
import { strict as assert } from 'assert';
|
import { strict as assert } from 'assert';
|
||||||
import drawLine from '../drawLine.mjs';
|
import drawLine from '../drawLine.mjs';
|
||||||
|
|
||||||
@ -16,6 +16,10 @@ describe('drawLine', () => {
|
|||||||
it('supports unequal silence', () => {
|
it('supports unequal silence', () => {
|
||||||
assert.equal(drawLine(fastcat(0, silence, [1, 2]), 10), '|0-..12|0-..12');
|
assert.equal(drawLine(fastcat(0, silence, [1, 2]), 10), '|0-..12|0-..12');
|
||||||
});
|
});
|
||||||
|
it('supports polyrhythms', () => {
|
||||||
|
'0*2 1*3';
|
||||||
|
assert.equal(drawLine(fastcat(pure(0).fast(2), pure(1).fast(3)), 10), '|0--0--1-1-1-');
|
||||||
|
});
|
||||||
it('supports multiple lines', () => {
|
it('supports multiple lines', () => {
|
||||||
assert.equal(
|
assert.equal(
|
||||||
drawLine(fastcat(0, stack(1, 2)), 10),
|
drawLine(fastcat(0, stack(1, 2)), 10),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user