Skip to content

Commit dd03ad6

Browse files
committed
support stack in drawLine
1 parent 2bbd306 commit dd03ad6

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

packages/core/drawLine.mjs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
11
import { gcd } from './fraction.mjs';
22

3-
// TODO: make it work for stacked patterns + support silence
4-
53
function drawLine(pat, chars = 60) {
6-
let s = '';
74
let c = 0;
8-
while (s.length < chars) {
5+
let lines = [''];
6+
const slots = [];
7+
while (lines[0].length < chars) {
98
const haps = pat.queryArc(c, c + 1);
10-
const durations = haps.map((hap) => hap.duration);
9+
const durations = haps.filter((hap) => hap.hasOnset()).map((hap) => hap.duration);
1110
const totalSlots = gcd(...durations).inverse();
12-
s += '|';
13-
haps.forEach((hap) => {
14-
const duration = hap.whole.end.sub(hap.whole.begin);
15-
const slots = totalSlots.mul(duration);
16-
s += Array(slots.valueOf())
17-
.fill()
18-
.map((_, i) => (!i ? hap.value : '-'))
19-
.join('');
20-
});
21-
++c;
11+
slots.push(totalSlots);
12+
const minDuration = durations.reduce((a, b) => a.min(b), durations[0]);
13+
lines = lines.map((line) => line + '|');
14+
15+
for (let i = 0; i < totalSlots; i++) {
16+
const step = c * totalSlots + i;
17+
const [begin, end] = [minDuration.mul(step), minDuration.mul(step + 1)];
18+
const matches = haps.filter((hap) => hap.whole.begin.lte(begin) && hap.whole.end.gte(end));
19+
const missingLines = matches.length - lines.length;
20+
if (missingLines > 0) {
21+
console.log(c, 'missingLines', missingLines);
22+
const emptyCycles =
23+
'|' +
24+
new Array(c)
25+
.fill()
26+
.map((_, l) => Array(slots[l]).fill('.').join(''))
27+
.join('|') +
28+
Array(i).fill('.').join('');
29+
lines = lines.concat(Array(missingLines).fill(emptyCycles));
30+
}
31+
lines = lines.map((line, i) => {
32+
const hap = matches[i];
33+
if (hap) {
34+
const isOnset = hap.whole.begin.eq(begin);
35+
const char = isOnset ? '' + hap.value : '-';
36+
return line + char;
37+
}
38+
return line + '.';
39+
});
40+
}
41+
c++;
2242
}
23-
return s;
43+
return lines.join('\n');
2444
}
2545

2646
export default drawLine;
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fastcat } from '../pattern.mjs';
1+
import { fastcat, stack } from '../pattern.mjs';
22
import { strict as assert } from 'assert';
33
import drawLine from '../drawLine.mjs';
44

@@ -7,5 +7,27 @@ describe('drawLine', () => {
77
assert.equal(drawLine(fastcat(0, [1, 2]), 10), '|0-12|0-12');
88
assert.equal(drawLine(fastcat(0, [1, 2, 3]), 10), '|0--123|0--123');
99
assert.equal(drawLine(fastcat(0, 1, [2, 3]), 10), '|0-1-23|0-1-23');
10+
assert.equal(
11+
drawLine(fastcat(0, stack(1, 2)), 10),
12+
`|01|01|01|01
13+
|.2|.2|.2|.2`,
14+
);
15+
assert.equal(
16+
drawLine(fastcat(0, 1, stack(2, 3)), 10),
17+
`|012|012|012
18+
|..3|..3|..3`,
19+
);
20+
assert.equal(
21+
drawLine(fastcat(0, stack(1, 2, 3)), 10),
22+
`|01|01|01|01
23+
|.2|.2|.2|.2
24+
|.3|.3|.3|.3`,
25+
);
26+
assert.equal(
27+
drawLine(fastcat(0, 1, stack(2, 3, 4)), 10),
28+
`|012|012|012
29+
|..3|..3|..3
30+
|..4|..4|..4`,
31+
);
1032
});
1133
});

0 commit comments

Comments
 (0)