mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-22 02:58:32 +00:00
support stack in drawLine
This commit is contained in:
parent
2bbd306b94
commit
dd03ad6c14
@ -1,26 +1,46 @@
|
|||||||
import { gcd } from './fraction.mjs';
|
import { gcd } from './fraction.mjs';
|
||||||
|
|
||||||
// TODO: make it work for stacked patterns + support silence
|
|
||||||
|
|
||||||
function drawLine(pat, chars = 60) {
|
function drawLine(pat, chars = 60) {
|
||||||
let s = '';
|
|
||||||
let c = 0;
|
let c = 0;
|
||||||
while (s.length < chars) {
|
let lines = [''];
|
||||||
|
const slots = [];
|
||||||
|
while (lines[0].length < chars) {
|
||||||
const haps = pat.queryArc(c, c + 1);
|
const haps = pat.queryArc(c, c + 1);
|
||||||
const durations = haps.map((hap) => hap.duration);
|
const durations = haps.filter((hap) => hap.hasOnset()).map((hap) => hap.duration);
|
||||||
const totalSlots = gcd(...durations).inverse();
|
const totalSlots = gcd(...durations).inverse();
|
||||||
s += '|';
|
slots.push(totalSlots);
|
||||||
haps.forEach((hap) => {
|
const minDuration = durations.reduce((a, b) => a.min(b), durations[0]);
|
||||||
const duration = hap.whole.end.sub(hap.whole.begin);
|
lines = lines.map((line) => line + '|');
|
||||||
const slots = totalSlots.mul(duration);
|
|
||||||
s += Array(slots.valueOf())
|
for (let i = 0; i < totalSlots; i++) {
|
||||||
.fill()
|
const step = c * totalSlots + i;
|
||||||
.map((_, i) => (!i ? hap.value : '-'))
|
const [begin, end] = [minDuration.mul(step), minDuration.mul(step + 1)];
|
||||||
.join('');
|
const matches = haps.filter((hap) => hap.whole.begin.lte(begin) && hap.whole.end.gte(end));
|
||||||
});
|
const missingLines = matches.length - lines.length;
|
||||||
++c;
|
if (missingLines > 0) {
|
||||||
|
console.log(c, 'missingLines', missingLines);
|
||||||
|
const emptyCycles =
|
||||||
|
'|' +
|
||||||
|
new Array(c)
|
||||||
|
.fill()
|
||||||
|
.map((_, l) => Array(slots[l]).fill('.').join(''))
|
||||||
|
.join('|') +
|
||||||
|
Array(i).fill('.').join('');
|
||||||
|
lines = lines.concat(Array(missingLines).fill(emptyCycles));
|
||||||
|
}
|
||||||
|
lines = lines.map((line, i) => {
|
||||||
|
const hap = matches[i];
|
||||||
|
if (hap) {
|
||||||
|
const isOnset = hap.whole.begin.eq(begin);
|
||||||
|
const char = isOnset ? '' + hap.value : '-';
|
||||||
|
return line + char;
|
||||||
|
}
|
||||||
|
return line + '.';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
c++;
|
||||||
}
|
}
|
||||||
return s;
|
return lines.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
export default drawLine;
|
export default drawLine;
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { fastcat } from '../pattern.mjs';
|
import { fastcat, stack } 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';
|
||||||
|
|
||||||
@ -7,5 +7,27 @@ describe('drawLine', () => {
|
|||||||
assert.equal(drawLine(fastcat(0, [1, 2]), 10), '|0-12|0-12');
|
assert.equal(drawLine(fastcat(0, [1, 2]), 10), '|0-12|0-12');
|
||||||
assert.equal(drawLine(fastcat(0, [1, 2, 3]), 10), '|0--123|0--123');
|
assert.equal(drawLine(fastcat(0, [1, 2, 3]), 10), '|0--123|0--123');
|
||||||
assert.equal(drawLine(fastcat(0, 1, [2, 3]), 10), '|0-1-23|0-1-23');
|
assert.equal(drawLine(fastcat(0, 1, [2, 3]), 10), '|0-1-23|0-1-23');
|
||||||
|
assert.equal(
|
||||||
|
drawLine(fastcat(0, stack(1, 2)), 10),
|
||||||
|
`|01|01|01|01
|
||||||
|
|.2|.2|.2|.2`,
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
drawLine(fastcat(0, 1, stack(2, 3)), 10),
|
||||||
|
`|012|012|012
|
||||||
|
|..3|..3|..3`,
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
drawLine(fastcat(0, stack(1, 2, 3)), 10),
|
||||||
|
`|01|01|01|01
|
||||||
|
|.2|.2|.2|.2
|
||||||
|
|.3|.3|.3|.3`,
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
drawLine(fastcat(0, 1, stack(2, 3, 4)), 10),
|
||||||
|
`|012|012|012
|
||||||
|
|..3|..3|..3
|
||||||
|
|..4|..4|..4`,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user